mchain.h revision 111217
172980Sbp/*
272980Sbp * Copyright (c) 2000, 2001 Boris Popov
372980Sbp * All rights reserved.
472980Sbp *
572980Sbp * Redistribution and use in source and binary forms, with or without
672980Sbp * modification, are permitted provided that the following conditions
772980Sbp * are met:
872980Sbp * 1. Redistributions of source code must retain the above copyright
972980Sbp *    notice, this list of conditions and the following disclaimer.
1072980Sbp * 2. Redistributions in binary form must reproduce the above copyright
1172980Sbp *    notice, this list of conditions and the following disclaimer in the
1272980Sbp *    documentation and/or other materials provided with the distribution.
1372980Sbp * 3. All advertising materials mentioning features or use of this software
1472980Sbp *    must display the following acknowledgement:
1572980Sbp *    This product includes software developed by Boris Popov.
1672980Sbp * 4. Neither the name of the author nor the names of any co-contributors
1772980Sbp *    may be used to endorse or promote products derived from this software
1872980Sbp *    without specific prior written permission.
1972980Sbp *
2072980Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2172980Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2272980Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2372980Sbp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2472980Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2572980Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2672980Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2772980Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2872980Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2972980Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3072980Sbp * SUCH DAMAGE.
3172980Sbp *
3272980Sbp * $FreeBSD: head/sys/sys/mchain.h 111217 2003-02-21 16:24:49Z bmilekic $
3372980Sbp */
3472980Sbp#ifndef _SYS_MCHAIN_H_
3572980Sbp#define _SYS_MCHAIN_H_
3672980Sbp
3772980Sbp#include <machine/endian.h>
3872980Sbp
39107940Srobert#ifndef _KERNEL
4072980Sbp/*
4172980Sbp * This macros probably belongs to the endian.h
4272980Sbp */
4372980Sbp#if (BYTE_ORDER == LITTLE_ENDIAN)
4472980Sbp
45111217Sbmilekic#define	htoles(x)	((u_int16_t)(x))
46111217Sbmilekic#define	letohs(x)	((u_int16_t)(x))
4772980Sbp#define	htolel(x)	((u_int32_t)(x))
4872980Sbp#define	letohl(x)	((u_int32_t)(x))
4972980Sbp#define	htoleq(x)	((int64_t)(x))
5072980Sbp#define	letohq(x)	((int64_t)(x))
5172980Sbp
52111217Sbmilekic#define	htobes(x)	(__htons(x))
53111217Sbmilekic#define	betohs(x)	(__ntohs(x))
54111217Sbmilekic#define	htobel(x)	(__htonl(x))
55111217Sbmilekic#define	betohl(x)	(__ntohl(x))
5672980Sbp
5772980Sbpstatic __inline int64_t
5872980Sbphtobeq(int64_t x)
5972980Sbp{
6090868Smike	return (int64_t)__htonl((u_int32_t)(x >> 32)) |
6190868Smike	    (int64_t)__htonl((u_int32_t)(x & 0xffffffff)) << 32;
6272980Sbp}
6372980Sbp
6472980Sbpstatic __inline int64_t
6572980Sbpbetohq(int64_t x)
6672980Sbp{
6790868Smike	return (int64_t)__ntohl((u_int32_t)(x >> 32)) |
6890868Smike	    (int64_t)__ntohl((u_int32_t)(x & 0xffffffff)) << 32;
6972980Sbp}
7072980Sbp
7172980Sbp#else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
7272980Sbp
7372980Sbp#error "Macros for Big-Endians are incomplete"
7472980Sbp
7572980Sbp/*
76111217Sbmilekic#define	htoles(x)	((u_int16_t)(x))
77111217Sbmilekic#define	letohs(x)	((u_int16_t)(x))
7872980Sbp#define	htolel(x)	((u_int32_t)(x))
7972980Sbp#define	letohl(x)	((u_int32_t)(x))
8072980Sbp*/
8172980Sbp#endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
8291394Stmm#endif	/* _KERNEL */
8372980Sbp
8472980Sbp
8572980Sbp#ifdef _KERNEL
8672980Sbp
8772980Sbp/*
8872980Sbp * Type of copy for mb_{put|get}_mem()
8972980Sbp */
9072980Sbp#define	MB_MSYSTEM	0		/* use bcopy() */
91111217Sbmilekic#define	MB_MUSER	1		/* use copyin()/copyout() */
92111217Sbmilekic#define	MB_MINLINE	2		/* use an inline copy loop */
9372980Sbp#define	MB_MZERO	3		/* bzero(), mb_put_mem only */
9472980Sbp#define	MB_MCUSTOM	4		/* use an user defined function */
9572980Sbp
9672980Sbpstruct mbuf;
9772980Sbpstruct mbchain;
9872980Sbp
99106667Sjhbtypedef int mb_copy_t(struct mbchain *mbp, c_caddr_t src, caddr_t dst, size_t len);
10072980Sbp
10172980Sbpstruct mbchain {
10272980Sbp	struct mbuf *	mb_top;		/* head of mbufs chain */
10372980Sbp	struct mbuf * 	mb_cur;		/* current mbuf */
10472980Sbp	int		mb_mleft;	/* free space in the current mbuf */
10572980Sbp	int		mb_count;	/* total number of bytes */
10672980Sbp	mb_copy_t *	mb_copy;	/* user defined copy function */
10772980Sbp	void *		mb_udata;	/* user data */
10872980Sbp};
10972980Sbp
11072980Sbpstruct mdchain {
11172980Sbp	struct mbuf *	md_top;		/* head of mbufs chain */
11272980Sbp	struct mbuf * 	md_cur;		/* current mbuf */
11372980Sbp	u_char *	md_pos;		/* offset in the current mbuf */
11472980Sbp};
11572980Sbp
11672980Sbpint  mb_init(struct mbchain *mbp);
11772980Sbpvoid mb_initm(struct mbchain *mbp, struct mbuf *m);
11872980Sbpvoid mb_done(struct mbchain *mbp);
11972980Sbpstruct mbuf *mb_detach(struct mbchain *mbp);
12072980Sbpint  mb_fixhdr(struct mbchain *mbp);
12172980Sbpcaddr_t mb_reserve(struct mbchain *mbp, int size);
12272980Sbp
12372980Sbpint  mb_put_uint8(struct mbchain *mbp, u_int8_t x);
12472980Sbpint  mb_put_uint16be(struct mbchain *mbp, u_int16_t x);
12572980Sbpint  mb_put_uint16le(struct mbchain *mbp, u_int16_t x);
12672980Sbpint  mb_put_uint32be(struct mbchain *mbp, u_int32_t x);
12772980Sbpint  mb_put_uint32le(struct mbchain *mbp, u_int32_t x);
12872980Sbpint  mb_put_int64be(struct mbchain *mbp, int64_t x);
12972980Sbpint  mb_put_int64le(struct mbchain *mbp, int64_t x);
13072980Sbpint  mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type);
13172980Sbpint  mb_put_mbuf(struct mbchain *mbp, struct mbuf *m);
13272980Sbpint  mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size);
13372980Sbp
13472980Sbpint  md_init(struct mdchain *mdp);
13572980Sbpvoid md_initm(struct mdchain *mbp, struct mbuf *m);
13672980Sbpvoid md_done(struct mdchain *mdp);
13772980Sbpvoid md_append_record(struct mdchain *mdp, struct mbuf *top);
13872980Sbpint  md_next_record(struct mdchain *mdp);
13972980Sbpint  md_get_uint8(struct mdchain *mdp, u_int8_t *x);
14072980Sbpint  md_get_uint16(struct mdchain *mdp, u_int16_t *x);
14172980Sbpint  md_get_uint16le(struct mdchain *mdp, u_int16_t *x);
14272980Sbpint  md_get_uint16be(struct mdchain *mdp, u_int16_t *x);
14372980Sbpint  md_get_uint32(struct mdchain *mdp, u_int32_t *x);
14472980Sbpint  md_get_uint32be(struct mdchain *mdp, u_int32_t *x);
14572980Sbpint  md_get_uint32le(struct mdchain *mdp, u_int32_t *x);
14672980Sbpint  md_get_int64(struct mdchain *mdp, int64_t *x);
14772980Sbpint  md_get_int64be(struct mdchain *mdp, int64_t *x);
14872980Sbpint  md_get_int64le(struct mdchain *mdp, int64_t *x);
14972980Sbpint  md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type);
15072980Sbpint  md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **m);
15172980Sbpint  md_get_uio(struct mdchain *mdp, struct uio *uiop, int size);
15272980Sbp
15372980Sbp#endif	/* ifdef _KERNEL */
15472980Sbp
15572980Sbp#endif	/* !_SYS_MCHAIN_H_ */
156