Contiki 2.6
|
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 Contiki operating system. 00030 * 00031 * Author: Adam Dunkels <adam@sics.se> 00032 * 00033 * $Id: memb.c,v 1.4 2009/04/06 21:18:03 adamdunkels 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 "contiki.h" 00049 #include "lib/memb.h" 00050 00051 /*---------------------------------------------------------------------------*/ 00052 void 00053 memb_init(struct memb *m) 00054 { 00055 memset(m->count, 0, m->num); 00056 memset(m->mem, 0, m->size * m->num); 00057 } 00058 /*---------------------------------------------------------------------------*/ 00059 void * 00060 memb_alloc(struct memb *m) 00061 { 00062 int i; 00063 00064 for(i = 0; i < m->num; ++i) { 00065 if(m->count[i] == 0) { 00066 /* If this block was unused, we increase the reference count to 00067 indicate that it now is used and return a pointer to the 00068 memory block. */ 00069 ++(m->count[i]); 00070 return (void *)((char *)m->mem + (i * m->size)); 00071 } 00072 } 00073 00074 /* No free block was found, so we return NULL to indicate failure to 00075 allocate block. */ 00076 return NULL; 00077 } 00078 /*---------------------------------------------------------------------------*/ 00079 char 00080 memb_free(struct memb *m, void *ptr) 00081 { 00082 int i; 00083 char *ptr2; 00084 00085 /* Walk through the list of blocks and try to find the block to 00086 which the pointer "ptr" points to. */ 00087 ptr2 = (char *)m->mem; 00088 for(i = 0; i < m->num; ++i) { 00089 00090 if(ptr2 == (char *)ptr) { 00091 /* We've found to block to which "ptr" points so we decrease the 00092 reference count and return the new value of it. */ 00093 if(m->count[i] > 0) { 00094 /* Make sure that we don't deallocate free memory. */ 00095 --(m->count[i]); 00096 } 00097 return m->count[i]; 00098 } 00099 ptr2 += m->size; 00100 } 00101 return -1; 00102 } 00103 /*---------------------------------------------------------------------------*/ 00104 int 00105 memb_inmemb(struct memb *m, void *ptr) 00106 { 00107 return (char *)ptr >= (char *)m->mem && 00108 (char *)ptr < (char *)m->mem + (m->num * m->size); 00109 } 00110 /*---------------------------------------------------------------------------*/ 00111 00112 /** @} */