libmchain.diff

slynko, 01/21/2006 11:53 AM

Download (6.99 KB)

View differences:

sys/kern/libmchain/subr_mchain.c 21 Jan 2006 11:45:03 -0000
10 10
 * 2. Redistributions in binary form must reproduce the above copyright
11 11
 *    notice, this list of conditions and the following disclaimer in the
12 12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. All advertising materials mentioning features or use of this software
14
 *    must display the following acknowledgement:
15
 *    This product includes software developed by Boris Popov.
16 13
 * 4. Neither the name of the author nor the names of any co-contributors
17 14
 *    may be used to endorse or promote products derived from this software
18 15
 *    without specific prior written permission.
......
37 34
#include <sys/param.h>
38 35
#include <sys/systm.h>
39 36
#include <sys/kernel.h>
37
#include <sys/endian.h>
40 38
#include <sys/errno.h>
41
#include <sys/malloc.h>
42 39
#include <sys/mbuf.h>
43 40
#include <sys/module.h>
44 41
#include <sys/uio.h>
......
47 44

  
48 45
MODULE_VERSION(libmchain, 1);
49 46

  
50
#define MBERROR(format, args...) printf("%s(%d): "format, __func__ , \
51
				    __LINE__ ,## args)
47
#define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
48
				    __LINE__ , ## __VA_ARGS__)
52 49

  
53
#define MBPANIC(format, args...) printf("%s(%d): "format, __func__ , \
54
				    __LINE__ ,## args)
50
#define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
51
				    __LINE__ , ## __VA_ARGS__)
55 52

  
56 53
/*
57 54
 * Various helper functions
......
157 154
int
158 155
mb_put_uint16be(struct mbchain *mbp, u_int16_t x)
159 156
{
160
	x = htobes(x);
157
	x = htobe16(x);
161 158
	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
162 159
}
163 160

  
164 161
int
165 162
mb_put_uint16le(struct mbchain *mbp, u_int16_t x)
166 163
{
167
	x = htoles(x);
164
	x = htole16(x);
168 165
	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
169 166
}
170 167

  
171 168
int
172 169
mb_put_uint32be(struct mbchain *mbp, u_int32_t x)
173 170
{
174
	x = htobel(x);
171
	x = htobe32(x);
175 172
	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
176 173
}
177 174

  
178 175
int
179 176
mb_put_uint32le(struct mbchain *mbp, u_int32_t x)
180 177
{
181
	x = htolel(x);
178
	x = htole32(x);
182 179
	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
183 180
}
184 181

  
185 182
int
186 183
mb_put_int64be(struct mbchain *mbp, int64_t x)
187 184
{
188
	x = htobeq(x);
185
	x = htobe64(x);
189 186
	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
190 187
}
191 188

  
192 189
int
193 190
mb_put_int64le(struct mbchain *mbp, int64_t x)
194 191
{
195
	x = htoleq(x);
192
	x = htole64(x);
196 193
	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
197 194
}
198 195

  
......
203 200
	caddr_t dst;
204 201
	c_caddr_t src;
205 202
	int cplen, error, mleft, count;
203
	size_t srclen, dstlen;
206 204

  
207 205
	m = mbp->mb_cur;
208 206
	mleft = mbp->mb_mleft;
......
219 217
			continue;
220 218
		}
221 219
		cplen = mleft > size ? size : mleft;
220
		srclen = dstlen = cplen;
222 221
		dst = mtod(m, caddr_t) + m->m_len;
223 222
		switch (type) {
224 223
		    case MB_MCUSTOM:
225
			error = mbp->mb_copy(mbp, source, dst, cplen);
224
			srclen = size;
225
			dstlen = mleft;
226
			error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
226 227
			if (error)
227 228
				return error;
228 229
			break;
......
242 243
			bzero(dst, cplen);
243 244
			break;
244 245
		}
245
		size -= cplen;
246
		source += cplen;
247
		m->m_len += cplen;
248
		mleft -= cplen;
249
		mbp->mb_count += cplen;
246
		size -= srclen;
247
		source += srclen;
248
		m->m_len += dstlen;
249
		mleft -= dstlen;
250
		mbp->mb_count += dstlen;
250 251
	}
251 252
	mbp->mb_cur = m;
252 253
	mbp->mb_mleft = mleft;
......
295 296
			return error;
296 297
		uiop->uio_offset += left;
297 298
		uiop->uio_resid -= left;
298
		uiop->uio_iov->iov_base += left;
299
		uiop->uio_iov->iov_base =
300
		    (char *)uiop->uio_iov->iov_base + left;
299 301
		uiop->uio_iov->iov_len -= left;
300 302
		size -= left;
301 303
	}
......
393 395
	u_int16_t v;
394 396
	int error = md_get_uint16(mdp, &v);
395 397

  
396
	*x = letohs(v);
398
	if (x != NULL)
399
		*x = le16toh(v);
397 400
	return error;
398 401
}
399 402

  
......
402 405
	u_int16_t v;
403 406
	int error = md_get_uint16(mdp, &v);
404 407

  
405
	*x = betohs(v);
408
	if (x != NULL)
409
		*x = be16toh(v);
406 410
	return error;
407 411
}
408 412

  
......
419 423
	int error;
420 424

  
421 425
	error = md_get_uint32(mdp, &v);
422
	*x = betohl(v);
426
	if (x != NULL)
427
		*x = be32toh(v);
423 428
	return error;
424 429
}
425 430

  
......
430 435
	int error;
431 436

  
432 437
	error = md_get_uint32(mdp, &v);
433
	*x = letohl(v);
438
	if (x != NULL)
439
		*x = le32toh(v);
434 440
	return error;
435 441
}
436 442

  
......
447 453
	int error;
448 454

  
449 455
	error = md_get_int64(mdp, &v);
450
	*x = betohq(v);
456
	if (x != NULL)
457
		*x = be64toh(v);
451 458
	return error;
452 459
}
453 460

  
......
458 465
	int error;
459 466

  
460 467
	error = md_get_int64(mdp, &v);
461
	*x = letohq(v);
468
	if (x != NULL)
469
		*x = le64toh(v);
462 470
	return error;
463 471
}
464 472

  
......
546 554
			return error;
547 555
		uiop->uio_offset += left;
548 556
		uiop->uio_resid -= left;
549
		uiop->uio_iov->iov_base += left;
557
		uiop->uio_iov->iov_base =
558
		    (char *)uiop->uio_iov->iov_base + left;
550 559
		uiop->uio_iov->iov_len -= left;
551 560
		size -= left;
552 561
	}
sys/sys/mchain.h 20 Jan 2006 17:32:41 -0000
35 35
#ifndef _SYS_MCHAIN_H_
36 36
#define _SYS_MCHAIN_H_
37 37

  
38
#include <machine/endian.h>
39

  
40
/*
41
 * This macros probably belongs to the endian.h
42
 */
43
#if _BYTE_ORDER == _LITTLE_ENDIAN
44

  
45
#define htoles(x)	((u_int16_t)(x))
46
#define letohs(x)	((u_int16_t)(x))
47
#define	htolel(x)	((u_int32_t)(x))
48
#define	letohl(x)	((u_int32_t)(x))
49
#define	htoleq(x)	((int64_t)(x))
50
#define	letohq(x)	((int64_t)(x))
51

  
52
#define htobes(x)	(htons(x))
53
#define betohs(x)	(ntohs(x))
54
#define htobel(x)	(htonl(x))
55
#define betohl(x)	(ntohl(x))
56

  
57
static __inline int64_t
58
htobeq(int64_t x)
59
{
60
	return (int64_t)htonl((u_int32_t)(x >> 32)) |
61
	    (int64_t)htonl((u_int32_t)(x & 0xffffffff)) << 32;
62
}
63

  
64
static __inline int64_t
65
betohq(int64_t x)
66
{
67
	return (int64_t)ntohl((u_int32_t)(x >> 32)) |
68
	    (int64_t)ntohl((u_int32_t)(x & 0xffffffff)) << 32;
69
}
70

  
71
#else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
72

  
73
#error "Macros for Big-Endians are incomplete"
74

  
75
/*
76
#define htoles(x)	((u_int16_t)(x))
77
#define letohs(x)	((u_int16_t)(x))
78
#define	htolel(x)	((u_int32_t)(x))
79
#define	letohl(x)	((u_int32_t)(x))
80
*/
81
#endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
82

  
83

  
84 38
#ifdef _KERNEL
85 39

  
86 40
/*
87 41
 * Type of copy for mb_{put|get}_mem()
88 42
 */
89 43
#define	MB_MSYSTEM	0		/* use bcopy() */
90
#define MB_MUSER	1		/* use copyin()/copyout() */
91
#define MB_MINLINE	2		/* use an inline copy loop */
44
#define	MB_MUSER	1		/* use copyin()/copyout() */
45
#define	MB_MINLINE	2		/* use an inline copy loop */
92 46
#define	MB_MZERO	3		/* bzero(), mb_put_mem only */
93 47
#define	MB_MCUSTOM	4		/* use an user defined function */
94 48

  
95 49
struct mbuf;
96 50
struct mbchain;
97 51

  
98
typedef int mb_copy_t(struct mbchain *mbp, c_caddr_t src, caddr_t dst, int len);
52
typedef int mb_copy_t(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
53
    size_t *srclen, size_t *dstlen);
99 54

  
100 55
struct mbchain {
101 56
	struct mbuf *	mb_top;		/* head of mbufs chain */