memb.h

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 Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: memb.h,v 1.6 2009/04/06 21:18:04 adamdunkels Exp $
00034  */
00035 
00036 /**
00037  * \addtogroup mem
00038  * @{
00039  */
00040 
00041 
00042 /**
00043  * \defgroup memb Memory block management functions
00044  *
00045  * The memory block allocation routines provide a simple yet powerful
00046  * set of functions for managing a set of memory blocks of fixed
00047  * size. A set of memory blocks is statically declared with the
00048  * MEMB() macro. Memory blocks are allocated from the declared
00049  * memory by the memb_alloc() function, and are deallocated with the
00050  * memb_free() function.
00051  *
00052  * @{
00053  */
00054 
00055 
00056 /**
00057  * \file
00058  *         Memory block allocation routines.
00059  * \author
00060  *         Adam Dunkels <adam@sics.se>
00061  *
00062  */
00063 
00064 #ifndef __MEMB_H__
00065 #define __MEMB_H__
00066 
00067 #include "sys/cc.h"
00068 
00069 /**
00070  * Declare a memory block.
00071  *
00072  * This macro is used to statically declare a block of memory that can
00073  * be used by the block allocation functions. The macro statically
00074  * declares a C array with a size that matches the specified number of
00075  * blocks and their individual sizes.
00076  *
00077  * Example:
00078  \code
00079 MEMB(connections, struct connection, 16);
00080  \endcode
00081  *
00082  * \param name The name of the memory block (later used with
00083  * memb_init(), memb_alloc() and memb_free()).
00084  *
00085  * \param structure The name of the struct that the memory block holds
00086  *
00087  * \param num The total number of memory chunks in the block.
00088  *
00089  */
00090 #define MEMB(name, structure, num) \
00091         static char CC_CONCAT(name,_memb_count)[num]; \
00092         static structure CC_CONCAT(name,_memb_mem)[num]; \
00093         static struct memb name = {sizeof(structure), num, \
00094                                           CC_CONCAT(name,_memb_count), \
00095                                           (void *)CC_CONCAT(name,_memb_mem)}
00096 
00097 struct memb {
00098   unsigned short size;
00099   unsigned short num;
00100   char *count;
00101   void *mem;
00102 };
00103 
00104 /**
00105  * Initialize a memory block that was declared with MEMB().
00106  *
00107  * \param m A memory block previously declared with MEMB().
00108  */
00109 void  memb_init(struct memb *m);
00110 
00111 /**
00112  * Allocate a memory block from a block of memory declared with MEMB().
00113  *
00114  * \param m A memory block previously declared with MEMB().
00115  */
00116 void *memb_alloc(struct memb *m);
00117 
00118 /**
00119  * Deallocate a memory block from a memory block previously declared
00120  * with MEMB().
00121  *
00122  * \param m m A memory block previously declared with MEMB().
00123  *
00124  * \param ptr A pointer to the memory block that is to be deallocated.
00125  *
00126  * \return The new reference count for the memory block (should be 0
00127  * if successfully deallocated) or -1 if the pointer "ptr" did not
00128  * point to a legal memory block.
00129  */
00130 char  memb_free(struct memb *m, void *ptr);
00131 
00132 int memb_inmemb(struct memb *m, void *ptr);
00133 
00134 
00135 /** @} */
00136 /** @} */
00137 
00138 #endif /* __MEMB_H__ */

Generated on Mon Apr 11 14:23:28 2011 for Contiki 2.5 by  doxygen 1.6.1