mbuf.c revision 49bc093d7c1e8b1854c6e9ebcb966d4f9538615e
/*
* Copyright (c) 1995 Danny Gasparovski
*
* Please read the file COPYRIGHT for the
* terms and conditions of the copyright.
*/
/*
* mbuf's in SLiRP are much simpler than the real mbufs in
* FreeBSD. They are fixed size, determined by the MTU,
* so that one whole packet can fit. Mbuf's cannot be
* chained together. If there's more data than the mbuf
* could hold, an external malloced buffer is pointed to
* by m_ext (and the data pointers) and M_EXT is set in
* the flags
*/
#include <slirp.h>
void
{
mbuf_alloced = 0;
}
void
{
/*
* Find a nice value for msize
* XXX if_maxlinkhdr already in mtu
*/
}
/*
* Get an mbuf from the free list, if there are none
* malloc one
*
* Because fragmentation can occur if we alloc new mbufs and
* free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
* which tells m_free to actually free() it
*/
struct mbuf *
{
register struct mbuf *m;
int flags = 0;
#ifdef VBOX_WITH_SYNC_SLIRP
int on_free_list = 0;
#endif
DEBUG_CALL("m_get");
if (m == NULL) {
goto end_error;
}
mbuf_alloced++;
if (mbuf_alloced > mbuf_thresh)
if (mbuf_alloced > mbuf_max)
VBOX_SLIRP_LOCK(m->m_mutex);
} else {
m = m_freelist.m_next;
VBOX_SLIRP_LOCK(m->m_mutex);
if (n != NULL)
VBOX_SLIRP_LOCK(n->m_mutex);
VBOX_SLIRP_UNLOCK(n->m_mutex);
}
/* Insert it in the used list */
if (n != &m_usedlist)
VBOX_SLIRP_LOCK(n->m_mutex);
if (n != &m_usedlist)
VBOX_SLIRP_LOCK(n->m_mutex);
VBOX_SLIRP_UNLOCK(m->m_mutex);
/* Initialise it */
m->m_len = 0;
m->m_nextpkt = 0;
m->m_prevpkt = 0;
DEBUG_ARG("m = %lx", (long )m);
return m;
}
void
{
DEBUG_CALL("m_free");
DEBUG_ARG("m = %lx", (long )m);
if(m) {
/* Remove from m_usedlist */
if (m->m_flags & M_USEDLIST) {
struct mbuf *p, *n;
p = (m->m_prev);
n = (m->m_next);
VBOX_SLIRP_LOCK(m->m_next);
if (n != NULL)
VBOX_SLIRP_LOCK(n->m_next);
if (p != NULL)
VBOX_SLIRP_UNLOCK(p->m_next);
if (n != NULL)
VBOX_SLIRP_UNLOCK(n->m_next);
if (p != NULL)
VBOX_SLIRP_LOCK(p->m_next);
}
/* If it's M_EXT, free() it */
/*
* Either free() it or put it on the free list
*/
VBOX_SLIRP_UNLOCK(m->m_mutex);
free(m);
#ifdef VBOX_WITH_SYNC_SLIRP
m = NULL;
#endif
mbuf_alloced--;
} else if ((m->m_flags & M_FREELIST) == 0) {
}
} /* if(m) */
}
/*
* Copy data from one mbuf to the end of
* the other.. if result is too big for one mbuf, malloc()
* an M_EXT data segment
*/
void
{
/*
* If there's no room, realloc
*/
if (M_FREEROOM(m) < n->m_len)
}
/* make m size bytes large */
void
struct mbuf *m;
int size;
{
int datasize;
/* some compiles throw up on gotos. This one we can fake. */
return;
}
/* if (m->m_ext == NULL)
* return (struct mbuf *)NULL;
*/
} else {
char *dat;
/* if (dat == NULL)
* return (struct mbuf *)NULL;
*/
}
}
void
struct mbuf *m;
int len;
{
if (m == NULL)
return;
if (len >= 0) {
/* Trim from head */
} else {
/* Trim from tail */
}
}
/*
* Copy len bytes from m, starting off bytes into n
*/
int
struct mbuf *n, *m;
{
if (len > M_FREEROOM(n)) {
return -1;
}
return 0;
}
/*
* Given a pointer into an mbuf, return the mbuf
* XXX This is a kludge, I should eliminate the need for it
* Fortunately, it's not used often
*/
struct mbuf *
{
struct mbuf *m;
DEBUG_CALL("dtom");
/* bug corrected for M_EXT buffers */
#ifndef VBOX_WITH_SYNC_SLIRP
#else
m = m_usedlist.m_next;
while(1) {
#endif
return m;
}
} else {
return m;
}
}
#ifdef VBOX_WITH_SYNC_SLIRP
m = mnext;
#endif
}
return (struct mbuf *)0;
}