2 * Copyright (c) 1995 Danny Gasparovski
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
9 * mbuf's in SLiRP are much simpler than the real mbufs in
10 * FreeBSD. They are fixed size, determined by the MTU,
11 * so that one whole packet can fit. Mbuf's cannot be
12 * chained together. If there's more data than the mbuf
13 * could hold, an external malloced buffer is pointed to
14 * by m_ext (and the data pointers) and M_EXT is set in
21 struct mbuf m_freelist
, m_usedlist
;
22 #define MBUF_THRESH 30
26 * Find a nice value for msize
27 * XXX if_maxlinkhdr already in mtu
29 #define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + sizeof(struct m_hdr ) + 6)
34 m_freelist
.m_next
= m_freelist
.m_prev
= &m_freelist
;
35 m_usedlist
.m_next
= m_usedlist
.m_prev
= &m_usedlist
;
39 * Get an mbuf from the free list, if there are none
42 * Because fragmentation can occur if we alloc new mbufs and
43 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
44 * which tells m_free to actually free() it
49 register struct mbuf
*m
;
54 if (m_freelist
.m_next
== &m_freelist
) {
55 m
= (struct mbuf
*)malloc(SLIRP_MSIZE
);
56 if (m
== NULL
) goto end_error
;
58 if (mbuf_alloced
> MBUF_THRESH
)
60 if (mbuf_alloced
> mbuf_max
)
61 mbuf_max
= mbuf_alloced
;
63 m
= m_freelist
.m_next
;
67 /* Insert it in the used list */
68 insque(m
,&m_usedlist
);
69 m
->m_flags
= (flags
| M_USEDLIST
);
72 m
->m_size
= SLIRP_MSIZE
- sizeof(struct m_hdr
);
78 DEBUG_ARG("m = %lx", (long )m
);
88 DEBUG_ARG("m = %lx", (long )m
);
91 /* Remove from m_usedlist */
92 if (m
->m_flags
& M_USEDLIST
)
95 /* If it's M_EXT, free() it */
96 if (m
->m_flags
& M_EXT
)
100 * Either free() it or put it on the free list
102 if (m
->m_flags
& M_DOFREE
) {
105 } else if ((m
->m_flags
& M_FREELIST
) == 0) {
106 insque(m
,&m_freelist
);
107 m
->m_flags
= M_FREELIST
; /* Clobber other flags */
113 * Copy data from one mbuf to the end of
114 * the other.. if result is too big for one mbuf, malloc()
115 * an M_EXT data segment
119 register struct mbuf
*m
, *n
;
122 * If there's no room, realloc
124 if (M_FREEROOM(m
) < n
->m_len
)
125 m_inc(m
,m
->m_size
+MINCSIZE
);
127 memcpy(m
->m_data
+m
->m_len
, n
->m_data
, n
->m_len
);
128 m
->m_len
+= n
->m_len
;
134 /* make m size bytes large */
142 /* some compiles throw up on gotos. This one we can fake. */
143 if(m
->m_size
>size
) return;
145 if (m
->m_flags
& M_EXT
) {
146 datasize
= m
->m_data
- m
->m_ext
;
147 m
->m_ext
= (char *)realloc(m
->m_ext
,size
);
148 /* if (m->m_ext == NULL)
149 * return (struct mbuf *)NULL;
151 m
->m_data
= m
->m_ext
+ datasize
;
154 datasize
= m
->m_data
- m
->m_dat
;
155 dat
= (char *)malloc(size
);
157 * return (struct mbuf *)NULL;
159 memcpy(dat
, m
->m_dat
, m
->m_size
);
162 m
->m_data
= m
->m_ext
+ datasize
;
192 * Copy len bytes from m, starting off bytes into n
195 m_copy(n
, m
, off
, len
)
199 if (len
> M_FREEROOM(n
))
202 memcpy((n
->m_data
+ n
->m_len
), (m
->m_data
+ off
), len
);
209 * Given a pointer into an mbuf, return the mbuf
210 * XXX This is a kludge, I should eliminate the need for it
211 * Fortunately, it's not used often
220 DEBUG_ARG("dat = %lx", (long )dat
);
222 /* bug corrected for M_EXT buffers */
223 for (m
= m_usedlist
.m_next
; m
!= &m_usedlist
; m
= m
->m_next
) {
224 if (m
->m_flags
& M_EXT
) {
225 if( (char *)dat
>=m
->m_ext
&& (char *)dat
<(m
->m_ext
+ m
->m_size
) )
228 if( (char *)dat
>= m
->m_dat
&& (char *)dat
<(m
->m_dat
+ m
->m_size
) )
233 DEBUG_ERROR((dfd
, "dtom failed"));
235 return (struct mbuf
*)0;