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
23 struct mbuf m_freelist
, m_usedlist
;
31 m_freelist
.m_next
= m_freelist
.m_prev
= &m_freelist
;
32 m_usedlist
.m_next
= m_usedlist
.m_prev
= &m_usedlist
;
40 * Find a nice value for msize
41 * XXX if_maxlinkhdr already in mtu
43 msize
= (if_mtu
>if_mru
?if_mtu
:if_mru
) +
44 if_maxlinkhdr
+ sizeof(struct m_hdr
) + 6;
48 * Get an mbuf from the free list, if there are none
51 * Because fragmentation can occur if we alloc new mbufs and
52 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
53 * which tells m_free to actually free() it
58 register struct mbuf
*m
;
63 if (m_freelist
.m_next
== &m_freelist
) {
64 m
= (struct mbuf
*)malloc(msize
);
65 if (m
== NULL
) goto end_error
;
67 if (mbuf_alloced
> mbuf_thresh
)
69 if (mbuf_alloced
> mbuf_max
)
70 mbuf_max
= mbuf_alloced
;
72 m
= m_freelist
.m_next
;
76 /* Insert it in the used list */
77 insque(m
,&m_usedlist
);
78 m
->m_flags
= (flags
| M_USEDLIST
);
81 m
->m_size
= msize
- sizeof(struct m_hdr
);
87 DEBUG_ARG("m = %lx", (long )m
);
97 DEBUG_ARG("m = %lx", (long )m
);
100 /* Remove from m_usedlist */
101 if (m
->m_flags
& M_USEDLIST
)
104 /* If it's M_EXT, free() it */
105 if (m
->m_flags
& M_EXT
)
109 * Either free() it or put it on the free list
111 if (m
->m_flags
& M_DOFREE
) {
114 } else if ((m
->m_flags
& M_FREELIST
) == 0) {
115 insque(m
,&m_freelist
);
116 m
->m_flags
= M_FREELIST
; /* Clobber other flags */
122 * Copy data from one mbuf to the end of
123 * the other.. if result is too big for one mbuf, malloc()
124 * an M_EXT data segment
128 register struct mbuf
*m
, *n
;
131 * If there's no room, realloc
133 if (M_FREEROOM(m
) < n
->m_len
)
134 m_inc(m
,m
->m_size
+MINCSIZE
);
136 memcpy(m
->m_data
+m
->m_len
, n
->m_data
, n
->m_len
);
137 m
->m_len
+= n
->m_len
;
143 /* make m size bytes large */
151 /* some compiles throw up on gotos. This one we can fake. */
152 if(m
->m_size
>size
) return;
154 if (m
->m_flags
& M_EXT
) {
155 datasize
= m
->m_data
- m
->m_ext
;
156 m
->m_ext
= (char *)realloc(m
->m_ext
,size
);
157 /* if (m->m_ext == NULL)
158 * return (struct mbuf *)NULL;
160 m
->m_data
= m
->m_ext
+ datasize
;
163 datasize
= m
->m_data
- m
->m_dat
;
164 dat
= (char *)malloc(size
);
166 * return (struct mbuf *)NULL;
168 memcpy(dat
, m
->m_dat
, m
->m_size
);
171 m
->m_data
= m
->m_ext
+ datasize
;
201 * Copy len bytes from m, starting off bytes into n
204 m_copy(n
, m
, off
, len
)
208 if (len
> M_FREEROOM(n
))
211 memcpy((n
->m_data
+ n
->m_len
), (m
->m_data
+ off
), len
);
218 * Given a pointer into an mbuf, return the mbuf
219 * XXX This is a kludge, I should eliminate the need for it
220 * Fortunately, it's not used often
229 DEBUG_ARG("dat = %lx", (long )dat
);
231 /* bug corrected for M_EXT buffers */
232 for (m
= m_usedlist
.m_next
; m
!= &m_usedlist
; m
= m
->m_next
) {
233 if (m
->m_flags
& M_EXT
) {
234 if( (char *)dat
>=m
->m_ext
&& (char *)dat
<(m
->m_ext
+ m
->m_size
) )
237 if( (char *)dat
>= m
->m_dat
&& (char *)dat
<(m
->m_dat
+ m
->m_size
) )
242 DEBUG_ERROR((dfd
, "dtom failed"));
244 return (struct mbuf
*)0;