888de86e45c4e222c52e70a86f85937dde9f87d6
[qemu.git] / slirp / mbuf.c
blob888de86e45c4e222c52e70a86f85937dde9f87d6
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 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 static int mbuf_alloced;
21 struct mbuf m_freelist, m_usedlist;
22 #define MBUF_THRESH 30
25 * Find a nice value for msize
26 * XXX if_maxlinkhdr already in mtu
28 #define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + sizeof(struct m_hdr ) + 6)
30 void
31 m_init(void)
33 m_freelist.m_next = m_freelist.m_prev = &m_freelist;
34 m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
38 * Get an mbuf from the free list, if there are none
39 * malloc one
41 * Because fragmentation can occur if we alloc new mbufs and
42 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
43 * which tells m_free to actually free() it
45 struct mbuf *
46 m_get(void)
48 register struct mbuf *m;
49 int flags = 0;
51 DEBUG_CALL("m_get");
53 if (m_freelist.m_next == &m_freelist) {
54 m = (struct mbuf *)malloc(SLIRP_MSIZE);
55 if (m == NULL) goto end_error;
56 mbuf_alloced++;
57 if (mbuf_alloced > MBUF_THRESH)
58 flags = M_DOFREE;
59 } else {
60 m = m_freelist.m_next;
61 remque(m);
64 /* Insert it in the used list */
65 insque(m,&m_usedlist);
66 m->m_flags = (flags | M_USEDLIST);
68 /* Initialise it */
69 m->m_size = SLIRP_MSIZE - sizeof(struct m_hdr);
70 m->m_data = m->m_dat;
71 m->m_len = 0;
72 m->m_nextpkt = NULL;
73 m->m_prevpkt = NULL;
74 end_error:
75 DEBUG_ARG("m = %lx", (long )m);
76 return m;
79 void
80 m_free(struct mbuf *m)
83 DEBUG_CALL("m_free");
84 DEBUG_ARG("m = %lx", (long )m);
86 if(m) {
87 /* Remove from m_usedlist */
88 if (m->m_flags & M_USEDLIST)
89 remque(m);
91 /* If it's M_EXT, free() it */
92 if (m->m_flags & M_EXT)
93 free(m->m_ext);
96 * Either free() it or put it on the free list
98 if (m->m_flags & M_DOFREE) {
99 free(m);
100 mbuf_alloced--;
101 } else if ((m->m_flags & M_FREELIST) == 0) {
102 insque(m,&m_freelist);
103 m->m_flags = M_FREELIST; /* Clobber other flags */
105 } /* if(m) */
109 * Copy data from one mbuf to the end of
110 * the other.. if result is too big for one mbuf, malloc()
111 * an M_EXT data segment
113 void
114 m_cat(struct mbuf *m, struct mbuf *n)
117 * If there's no room, realloc
119 if (M_FREEROOM(m) < n->m_len)
120 m_inc(m,m->m_size+MINCSIZE);
122 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
123 m->m_len += n->m_len;
125 m_free(n);
129 /* make m size bytes large */
130 void
131 m_inc(struct mbuf *m, int size)
133 int datasize;
135 /* some compiles throw up on gotos. This one we can fake. */
136 if(m->m_size>size) return;
138 if (m->m_flags & M_EXT) {
139 datasize = m->m_data - m->m_ext;
140 m->m_ext = (char *)realloc(m->m_ext,size);
141 m->m_data = m->m_ext + datasize;
142 } else {
143 char *dat;
144 datasize = m->m_data - m->m_dat;
145 dat = (char *)malloc(size);
146 memcpy(dat, m->m_dat, m->m_size);
148 m->m_ext = dat;
149 m->m_data = m->m_ext + datasize;
150 m->m_flags |= M_EXT;
153 m->m_size = size;
159 void
160 m_adj(struct mbuf *m, int len)
162 if (m == NULL)
163 return;
164 if (len >= 0) {
165 /* Trim from head */
166 m->m_data += len;
167 m->m_len -= len;
168 } else {
169 /* Trim from tail */
170 len = -len;
171 m->m_len -= len;
177 * Copy len bytes from m, starting off bytes into n
180 m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
182 if (len > M_FREEROOM(n))
183 return -1;
185 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
186 n->m_len += len;
187 return 0;
192 * Given a pointer into an mbuf, return the mbuf
193 * XXX This is a kludge, I should eliminate the need for it
194 * Fortunately, it's not used often
196 struct mbuf *
197 dtom(void *dat)
199 struct mbuf *m;
201 DEBUG_CALL("dtom");
202 DEBUG_ARG("dat = %lx", (long )dat);
204 /* bug corrected for M_EXT buffers */
205 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) {
206 if (m->m_flags & M_EXT) {
207 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
208 return m;
209 } else {
210 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
211 return m;
215 DEBUG_ERROR((dfd, "dtom failed"));
217 return (struct mbuf *)0;