ram: Move migration_bitmap_mutex into RAMState
[qemu/ar7.git] / slirp / mbuf.c
blob5ff24559fd4a2308fcb71283df9311c36ff733c4
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 "qemu/osdep.h"
19 #include "slirp.h"
21 #define MBUF_THRESH 30
24 * Find a nice value for msize
26 #define SLIRP_MSIZE\
27 (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
29 void
30 m_init(Slirp *slirp)
32 slirp->m_freelist.qh_link = slirp->m_freelist.qh_rlink = &slirp->m_freelist;
33 slirp->m_usedlist.qh_link = slirp->m_usedlist.qh_rlink = &slirp->m_usedlist;
36 void m_cleanup(Slirp *slirp)
38 struct mbuf *m, *next;
40 m = (struct mbuf *) slirp->m_usedlist.qh_link;
41 while ((struct quehead *) m != &slirp->m_usedlist) {
42 next = m->m_next;
43 if (m->m_flags & M_EXT) {
44 g_free(m->m_ext);
46 g_free(m);
47 m = next;
49 m = (struct mbuf *) slirp->m_freelist.qh_link;
50 while ((struct quehead *) m != &slirp->m_freelist) {
51 next = m->m_next;
52 g_free(m);
53 m = next;
58 * Get an mbuf from the free list, if there are none
59 * allocate one
61 * Because fragmentation can occur if we alloc new mbufs and
62 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
63 * which tells m_free to actually g_free() it
65 struct mbuf *
66 m_get(Slirp *slirp)
68 register struct mbuf *m;
69 int flags = 0;
71 DEBUG_CALL("m_get");
73 if (slirp->m_freelist.qh_link == &slirp->m_freelist) {
74 m = g_malloc(SLIRP_MSIZE);
75 slirp->mbuf_alloced++;
76 if (slirp->mbuf_alloced > MBUF_THRESH)
77 flags = M_DOFREE;
78 m->slirp = slirp;
79 } else {
80 m = (struct mbuf *) slirp->m_freelist.qh_link;
81 remque(m);
84 /* Insert it in the used list */
85 insque(m,&slirp->m_usedlist);
86 m->m_flags = (flags | M_USEDLIST);
88 /* Initialise it */
89 m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
90 m->m_data = m->m_dat;
91 m->m_len = 0;
92 m->m_nextpkt = NULL;
93 m->m_prevpkt = NULL;
94 m->resolution_requested = false;
95 m->expiration_date = (uint64_t)-1;
96 DEBUG_ARG("m = %p", m);
97 return m;
100 void
101 m_free(struct mbuf *m)
104 DEBUG_CALL("m_free");
105 DEBUG_ARG("m = %p", m);
107 if(m) {
108 /* Remove from m_usedlist */
109 if (m->m_flags & M_USEDLIST)
110 remque(m);
112 /* If it's M_EXT, free() it */
113 if (m->m_flags & M_EXT) {
114 g_free(m->m_ext);
117 * Either free() it or put it on the free list
119 if (m->m_flags & M_DOFREE) {
120 m->slirp->mbuf_alloced--;
121 g_free(m);
122 } else if ((m->m_flags & M_FREELIST) == 0) {
123 insque(m,&m->slirp->m_freelist);
124 m->m_flags = M_FREELIST; /* Clobber other flags */
126 } /* if(m) */
130 * Copy data from one mbuf to the end of
131 * the other.. if result is too big for one mbuf, allocate
132 * an M_EXT data segment
134 void
135 m_cat(struct mbuf *m, struct mbuf *n)
138 * If there's no room, realloc
140 if (M_FREEROOM(m) < n->m_len)
141 m_inc(m,m->m_size+MINCSIZE);
143 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
144 m->m_len += n->m_len;
146 m_free(n);
150 /* make m size bytes large */
151 void
152 m_inc(struct mbuf *m, int size)
154 int datasize;
156 /* some compiles throw up on gotos. This one we can fake. */
157 if(m->m_size>size) return;
159 if (m->m_flags & M_EXT) {
160 datasize = m->m_data - m->m_ext;
161 m->m_ext = g_realloc(m->m_ext, size);
162 m->m_data = m->m_ext + datasize;
163 } else {
164 char *dat;
165 datasize = m->m_data - m->m_dat;
166 dat = g_malloc(size);
167 memcpy(dat, m->m_dat, m->m_size);
169 m->m_ext = dat;
170 m->m_data = m->m_ext + datasize;
171 m->m_flags |= M_EXT;
174 m->m_size = size;
180 void
181 m_adj(struct mbuf *m, int len)
183 if (m == NULL)
184 return;
185 if (len >= 0) {
186 /* Trim from head */
187 m->m_data += len;
188 m->m_len -= len;
189 } else {
190 /* Trim from tail */
191 len = -len;
192 m->m_len -= len;
198 * Copy len bytes from m, starting off bytes into n
201 m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
203 if (len > M_FREEROOM(n))
204 return -1;
206 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
207 n->m_len += len;
208 return 0;
213 * Given a pointer into an mbuf, return the mbuf
214 * XXX This is a kludge, I should eliminate the need for it
215 * Fortunately, it's not used often
217 struct mbuf *
218 dtom(Slirp *slirp, void *dat)
220 struct mbuf *m;
222 DEBUG_CALL("dtom");
223 DEBUG_ARG("dat = %p", dat);
225 /* bug corrected for M_EXT buffers */
226 for (m = (struct mbuf *) slirp->m_usedlist.qh_link;
227 (struct quehead *) m != &slirp->m_usedlist;
228 m = m->m_next) {
229 if (m->m_flags & M_EXT) {
230 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
231 return m;
232 } else {
233 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
234 return m;
238 DEBUG_ERROR((dfd, "dtom failed"));
240 return (struct mbuf *)0;