mbuf.c (3088B)
1 /* $Id: //depot/blt/srv/network/mbuf.c#1 $ 2 ** 3 ** Copyright 1999 Sidney Cammeresi 4 ** All rights reserved. 5 ** 6 ** Redistribution and use in source and binary forms, with or without 7 ** modification, are permitted provided that the following conditions 8 ** are met: 9 ** 1. Redistributions of source code must retain the above copyright 10 ** notice, this list of conditions, and the following disclaimer. 11 ** 2. Redistributions in binary form must reproduce the above copyright 12 ** notice, this list of conditions, and the following disclaimer in the 13 ** documentation and/or other materials provided with the distribution. 14 ** 3. The name of the author may not be used to endorse or promote products 15 ** derived from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <unistd.h> 30 #include <blt/qsem.h> 31 #include <blt/network/mbuf.h> 32 33 #define MBUF_INIT_MEM 0x1000 /* start with one page of mbufs */ 34 #define MCL_INIT_MEM 0x4000 /* start with eight clusters */ 35 36 static struct mbuf *free_mbuf = NULL; 37 static unsigned int **free_cluster = NULL; 38 39 static qsem_t *free_mbuf_sem, *free_cluster_sem; 40 41 void mbinit (void) 42 { 43 unsigned int i, **cl; 44 struct mbuf *m; 45 46 m = sbrk (MBUF_INIT_MEM); 47 for (i = 0; i < MBUF_INIT_MEM / sizeof (struct mbuf); i++) 48 { 49 m[i].m_next = free_mbuf; 50 m[i].m_nextpkt = NULL; 51 m[i].m_len = MLEN; 52 m[i].m_data = (char *) &m[i] + sizeof (struct mbuf); 53 m[i].m_type = MT_FREE; 54 m[i].m_flags = 0; 55 free_mbuf = &m[i]; 56 } 57 58 cl = sbrk (MCL_INIT_MEM); 59 for (i = 0; i < MCL_INIT_MEM / 0x800; i++, cl += 0x800 / sizeof (int)) 60 { 61 *cl = (unsigned int *) free_cluster; 62 free_cluster = cl; 63 } 64 65 free_mbuf_sem = qsem_create (1); 66 free_cluster_sem = qsem_create (1); 67 } 68 69 struct mbuf *mget (void) 70 { 71 struct mbuf *mbuf; 72 73 qsem_acquire (free_mbuf_sem); 74 if ((mbuf = free_mbuf) == NULL) 75 { 76 /* allocate more memory */ 77 } 78 else 79 free_mbuf = free_mbuf->m_next; 80 qsem_release (free_mbuf_sem); 81 mbuf->m_next = NULL; 82 return mbuf; 83 } 84 85 char *mgetcl (struct mbuf *mbuf) 86 { 87 } 88 89 void mput (struct mbuf *mbuf) 90 { 91 mbuf->m_nextpkt = NULL; 92 mbuf->m_len = MLEN; 93 mbuf->m_data = (char *) mbuf + sizeof (struct mbuf); 94 mbuf->m_type = MT_FREE; 95 mbuf->m_flags = 0; 96 mputcl (mbuf); 97 qsem_acquire (free_mbuf_sem); 98 mbuf->m_next = free_mbuf; 99 free_mbuf = mbuf->m_next; 100 qsem_release (free_mbuf_sem); 101 } 102 103 void mputcl (struct mbuf *mbuf) 104 { 105 } 106