ipmi: express dependencies with kconfig
[qemu/ar7.git] / slirp / mbuf.c
blob521c02c96739f4b3c333d9767e9a0874f05fea05
1 /*
2 * Copyright (c) 1995 Danny Gasparovski
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 /*
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 g_malloced buffer is pointed to
14 * by m_ext (and the data pointers) and M_EXT is set in
15 * the flags
18 #include "slirp.h"
20 #define MBUF_THRESH 30
23 * Find a nice value for msize
25 #define SLIRP_MSIZE\
26 (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
28 void
29 m_init(Slirp *slirp)
31 slirp->m_freelist.qh_link = slirp->m_freelist.qh_rlink = &slirp->m_freelist;
32 slirp->m_usedlist.qh_link = slirp->m_usedlist.qh_rlink = &slirp->m_usedlist;
35 void m_cleanup(Slirp *slirp)
37 struct mbuf *m, *next;
39 m = (struct mbuf *) slirp->m_usedlist.qh_link;
40 while ((struct quehead *) m != &slirp->m_usedlist) {
41 next = m->m_next;
42 if (m->m_flags & M_EXT) {
43 g_free(m->m_ext);
45 g_free(m);
46 m = next;
48 m = (struct mbuf *) slirp->m_freelist.qh_link;
49 while ((struct quehead *) m != &slirp->m_freelist) {
50 next = m->m_next;
51 g_free(m);
52 m = next;
57 * Get an mbuf from the free list, if there are none
58 * allocate one
60 * Because fragmentation can occur if we alloc new mbufs and
61 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
62 * which tells m_free to actually g_free() it
64 struct mbuf *
65 m_get(Slirp *slirp)
67 register struct mbuf *m;
68 int flags = 0;
70 DEBUG_CALL("m_get");
72 if (slirp->m_freelist.qh_link == &slirp->m_freelist) {
73 m = g_malloc(SLIRP_MSIZE);
74 slirp->mbuf_alloced++;
75 if (slirp->mbuf_alloced > MBUF_THRESH)
76 flags = M_DOFREE;
77 m->slirp = slirp;
78 } else {
79 m = (struct mbuf *) slirp->m_freelist.qh_link;
80 remque(m);
83 /* Insert it in the used list */
84 insque(m,&slirp->m_usedlist);
85 m->m_flags = (flags | M_USEDLIST);
87 /* Initialise it */
88 m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
89 m->m_data = m->m_dat;
90 m->m_len = 0;
91 m->m_nextpkt = NULL;
92 m->m_prevpkt = NULL;
93 m->resolution_requested = false;
94 m->expiration_date = (uint64_t)-1;
95 DEBUG_ARG("m = %p", m);
96 return m;
99 void
100 m_free(struct mbuf *m)
103 DEBUG_CALL("m_free");
104 DEBUG_ARG("m = %p", m);
106 if(m) {
107 /* Remove from m_usedlist */
108 if (m->m_flags & M_USEDLIST)
109 remque(m);
111 /* If it's M_EXT, free() it */
112 if (m->m_flags & M_EXT) {
113 g_free(m->m_ext);
116 * Either free() it or put it on the free list
118 if (m->m_flags & M_DOFREE) {
119 m->slirp->mbuf_alloced--;
120 g_free(m);
121 } else if ((m->m_flags & M_FREELIST) == 0) {
122 insque(m,&m->slirp->m_freelist);
123 m->m_flags = M_FREELIST; /* Clobber other flags */
125 } /* if(m) */
129 * Copy data from one mbuf to the end of
130 * the other.. if result is too big for one mbuf, allocate
131 * an M_EXT data segment
133 void
134 m_cat(struct mbuf *m, struct mbuf *n)
137 * If there's no room, realloc
139 if (M_FREEROOM(m) < n->m_len)
140 m_inc(m, m->m_len + n->m_len);
142 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
143 m->m_len += n->m_len;
145 m_free(n);
149 /* make m 'size' bytes large from m_data */
150 void
151 m_inc(struct mbuf *m, int size)
153 int gapsize;
155 /* some compilers throw up on gotos. This one we can fake. */
156 if (M_ROOM(m) > size) {
157 return;
160 if (m->m_flags & M_EXT) {
161 gapsize = m->m_data - m->m_ext;
162 m->m_ext = g_realloc(m->m_ext, size + gapsize);
163 } else {
164 gapsize = m->m_data - m->m_dat;
165 m->m_ext = g_malloc(size + gapsize);
166 memcpy(m->m_ext, m->m_dat, m->m_size);
167 m->m_flags |= M_EXT;
170 m->m_data = m->m_ext + gapsize;
171 m->m_size = size + gapsize;
176 void
177 m_adj(struct mbuf *m, int len)
179 if (m == NULL)
180 return;
181 if (len >= 0) {
182 /* Trim from head */
183 m->m_data += len;
184 m->m_len -= len;
185 } else {
186 /* Trim from tail */
187 len = -len;
188 m->m_len -= len;
194 * Copy len bytes from m, starting off bytes into n
197 m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
199 if (len > M_FREEROOM(n))
200 return -1;
202 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
203 n->m_len += len;
204 return 0;
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
213 struct mbuf *
214 dtom(Slirp *slirp, void *dat)
216 struct mbuf *m;
218 DEBUG_CALL("dtom");
219 DEBUG_ARG("dat = %p", dat);
221 /* bug corrected for M_EXT buffers */
222 for (m = (struct mbuf *) slirp->m_usedlist.qh_link;
223 (struct quehead *) m != &slirp->m_usedlist;
224 m = m->m_next) {
225 if (m->m_flags & M_EXT) {
226 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
227 return m;
228 } else {
229 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
230 return m;
234 DEBUG_ERROR("dtom failed");
236 return (struct mbuf *)0;