lib/memb.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the uIP TCP/IP stack
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: memb.c,v 1.1 2006/06/12 08:21:43 adam Exp $
00034  */
00035 
00036 /**
00037  * \addtogroup memb
00038  * @{
00039  */
00040 
00041  /**
00042  * \file
00043  * Memory block allocation routines.
00044  * \author Adam Dunkels <adam@sics.se>
00045  */
00046 #include <string.h>
00047 
00048 #include "memb.h"
00049 
00050 /*---------------------------------------------------------------------------*/
00051 void
00052 memb_init(struct memb_blocks *m)
00053 {
00054   memset(m->count, 0, m->num);
00055   memset(m->mem, 0, m->size * m->num);
00056 }
00057 /*---------------------------------------------------------------------------*/
00058 void *
00059 memb_alloc(struct memb_blocks *m)
00060 {
00061   int i;
00062 
00063   for(i = 0; i < m->num; ++i) {
00064     if(m->count[i] == 0) {
00065       /* If this block was unused, we increase the reference count to
00066          indicate that it now is used and return a pointer to the
00067          memory block. */
00068       ++(m->count[i]);
00069       return (void *)((char *)m->mem + (i * m->size));
00070     }
00071   }
00072 
00073   /* No free block was found, so we return NULL to indicate failure to
00074      allocate block. */
00075   return NULL;
00076 }
00077 /*---------------------------------------------------------------------------*/
00078 char
00079 memb_free(struct memb_blocks *m, void *ptr)
00080 {
00081   int i;
00082   char *ptr2;
00083 
00084   /* Walk through the list of blocks and try to find the block to
00085      which the pointer "ptr" points to. */
00086   ptr2 = (char *)m->mem;
00087   for(i = 0; i < m->num; ++i) {
00088     
00089     if(ptr2 == (char *)ptr) {
00090       /* We've found to block to which "ptr" points so we decrease the
00091          reference count and return the new value of it. */
00092       if(m->count[i] > 0) {
00093         /* Make sure that we don't deallocate free memory. */
00094         --(m->count[i]);
00095       }
00096       return m->count[i];
00097     }
00098     ptr2 += m->size;
00099   }
00100   return -1;
00101 }
00102 /*---------------------------------------------------------------------------*/
00103 
00104 /** @} */

Generated on Mon Jun 12 10:23:01 2006 for uIP 1.0 by  doxygen 1.4.6