Project

General

Profile

Bug #80 ยป libmchain.diff

slynko, 01/21/2006 11:53 AM

View differences:

sys/kern/libmchain/subr_mchain.c 21 Jan 2006 11:45:03 -0000
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
......
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/uio.h>
......
MODULE_VERSION(libmchain, 1);
#define MBERROR(format, args...) printf("%s(%d): "format, __func__ , \
__LINE__ ,## args)
#define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
__LINE__ , ## __VA_ARGS__)
#define MBPANIC(format, args...) printf("%s(%d): "format, __func__ , \
__LINE__ ,## args)
#define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
__LINE__ , ## __VA_ARGS__)
/*
* Various helper functions
......
int
mb_put_uint16be(struct mbchain *mbp, u_int16_t x)
{
x = htobes(x);
x = htobe16(x);
return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
}
int
mb_put_uint16le(struct mbchain *mbp, u_int16_t x)
{
x = htoles(x);
x = htole16(x);
return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
}
int
mb_put_uint32be(struct mbchain *mbp, u_int32_t x)
{
x = htobel(x);
x = htobe32(x);
return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
}
int
mb_put_uint32le(struct mbchain *mbp, u_int32_t x)
{
x = htolel(x);
x = htole32(x);
return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
}
int
mb_put_int64be(struct mbchain *mbp, int64_t x)
{
x = htobeq(x);
x = htobe64(x);
return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
}
int
mb_put_int64le(struct mbchain *mbp, int64_t x)
{
x = htoleq(x);
x = htole64(x);
return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
}
......
caddr_t dst;
c_caddr_t src;
int cplen, error, mleft, count;
size_t srclen, dstlen;
m = mbp->mb_cur;
mleft = mbp->mb_mleft;
......
continue;
}
cplen = mleft > size ? size : mleft;
srclen = dstlen = cplen;
dst = mtod(m, caddr_t) + m->m_len;
switch (type) {
case MB_MCUSTOM:
error = mbp->mb_copy(mbp, source, dst, cplen);
srclen = size;
dstlen = mleft;
error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
if (error)
return error;
break;
......
bzero(dst, cplen);
break;
}
size -= cplen;
source += cplen;
m->m_len += cplen;
mleft -= cplen;
mbp->mb_count += cplen;
size -= srclen;
source += srclen;
m->m_len += dstlen;
mleft -= dstlen;
mbp->mb_count += dstlen;
}
mbp->mb_cur = m;
mbp->mb_mleft = mleft;
......
return error;
uiop->uio_offset += left;
uiop->uio_resid -= left;
uiop->uio_iov->iov_base += left;
uiop->uio_iov->iov_base =
(char *)uiop->uio_iov->iov_base + left;
uiop->uio_iov->iov_len -= left;
size -= left;
}
......
u_int16_t v;
int error = md_get_uint16(mdp, &v);
*x = letohs(v);
if (x != NULL)
*x = le16toh(v);
return error;
}
......
u_int16_t v;
int error = md_get_uint16(mdp, &v);
*x = betohs(v);
if (x != NULL)
*x = be16toh(v);
return error;
}
......
int error;
error = md_get_uint32(mdp, &v);
*x = betohl(v);
if (x != NULL)
*x = be32toh(v);
return error;
}
......
int error;
error = md_get_uint32(mdp, &v);
*x = letohl(v);
if (x != NULL)
*x = le32toh(v);
return error;
}
......
int error;
error = md_get_int64(mdp, &v);
*x = betohq(v);
if (x != NULL)
*x = be64toh(v);
return error;
}
......
int error;
error = md_get_int64(mdp, &v);
*x = letohq(v);
if (x != NULL)
*x = le64toh(v);
return error;
}
......
return error;
uiop->uio_offset += left;
uiop->uio_resid -= left;
uiop->uio_iov->iov_base += left;
uiop->uio_iov->iov_base =
(char *)uiop->uio_iov->iov_base + left;
uiop->uio_iov->iov_len -= left;
size -= left;
}
sys/sys/mchain.h 20 Jan 2006 17:32:41 -0000
#ifndef _SYS_MCHAIN_H_
#define _SYS_MCHAIN_H_
#include <machine/endian.h>
/*
* This macros probably belongs to the endian.h
*/
#if _BYTE_ORDER == _LITTLE_ENDIAN
#define htoles(x) ((u_int16_t)(x))
#define letohs(x) ((u_int16_t)(x))
#define htolel(x) ((u_int32_t)(x))
#define letohl(x) ((u_int32_t)(x))
#define htoleq(x) ((int64_t)(x))
#define letohq(x) ((int64_t)(x))
#define htobes(x) (htons(x))
#define betohs(x) (ntohs(x))
#define htobel(x) (htonl(x))
#define betohl(x) (ntohl(x))
static __inline int64_t
htobeq(int64_t x)
{
return (int64_t)htonl((u_int32_t)(x >> 32)) |
(int64_t)htonl((u_int32_t)(x & 0xffffffff)) << 32;
}
static __inline int64_t
betohq(int64_t x)
{
return (int64_t)ntohl((u_int32_t)(x >> 32)) |
(int64_t)ntohl((u_int32_t)(x & 0xffffffff)) << 32;
}
#else /* (BYTE_ORDER == LITTLE_ENDIAN) */
#error "Macros for Big-Endians are incomplete"
/*
#define htoles(x) ((u_int16_t)(x))
#define letohs(x) ((u_int16_t)(x))
#define htolel(x) ((u_int32_t)(x))
#define letohl(x) ((u_int32_t)(x))
*/
#endif /* (BYTE_ORDER == LITTLE_ENDIAN) */
#ifdef _KERNEL
/*
* Type of copy for mb_{put|get}_mem()
*/
#define MB_MSYSTEM 0 /* use bcopy() */
#define MB_MUSER 1 /* use copyin()/copyout() */
#define MB_MINLINE 2 /* use an inline copy loop */
#define MB_MUSER 1 /* use copyin()/copyout() */
#define MB_MINLINE 2 /* use an inline copy loop */
#define MB_MZERO 3 /* bzero(), mb_put_mem only */
#define MB_MCUSTOM 4 /* use an user defined function */
struct mbuf;
struct mbchain;
typedef int mb_copy_t(struct mbchain *mbp, c_caddr_t src, caddr_t dst, int len);
typedef int mb_copy_t(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
size_t *srclen, size_t *dstlen);
struct mbchain {
struct mbuf * mb_top; /* head of mbufs chain */
    (1-1/1)