2 * \addtogroup exampleapps
\r
8 * Memory block allocation routines.
\r
9 * \author Adam Dunkels <adam@sics.se>
\r
11 * The memory block allocation routines provide a simple yet powerful
\r
12 * set of functions for managing a set of memory blocks of fixed
\r
13 * size. A set of memory blocks is statically declared with the
\r
14 * MEMB() macro. Memory blocks are allocated from the declared
\r
15 * memory by the memb_alloc() function, and are deallocated with the
\r
16 * memb_free() function.
\r
18 * \note Because of namespace clashes only one MEMB() can be
\r
19 * declared per C module, and the name scope of a MEMB() memory
\r
20 * block is local to each C module.
\r
22 * The following example shows how to declare and use a memory block
\r
23 * called "cmem" which has 8 chunks of memory with each memory chunk
\r
24 * being 20 bytes large.
\r
29 int main(int argc, char *argv[]) {
\r
34 ptr = memb_alloc(&cmem);
\r
39 printf("Could not allocate memory.\n");
\r
42 if(memb_free(ptr) == 0) {
\r
43 printf("Deallocation succeeded.\n");
\r
54 /*------------------------------------------------------------------------------*/
\r
56 * Initialize a memory block that was declared with MEMB().
\r
58 * \param m A memory block previosly declared with MEMB().
\r
60 /*------------------------------------------------------------------------------*/
\r
62 memb_init(struct memb_blocks *m)
\r
64 memset(m->mem, (m->size + 1) * m->num, 0);
\r
66 /*------------------------------------------------------------------------------*/
\r
68 * Allocate a memory block from a block of memory declared with MEMB().
\r
70 * \param m A memory block previosly declared with MEMB().
\r
72 /*------------------------------------------------------------------------------*/
\r
74 memb_alloc(struct memb_blocks *m)
\r
80 for(i = 0; i < m->num; ++i) {
\r
82 /* If this block was unused, we increase the reference count to
\r
83 indicate that it now is used and return a pointer to the
\r
84 first byte following the reference counter. */
\r
91 /* No free block was found, so we return NULL to indicate failure to
\r
95 /*------------------------------------------------------------------------------*/
\r
97 * Deallocate a memory block from a memory block previously declared
\r
100 * \param m m A memory block previosly declared with MEMB().
\r
102 * \param ptr A pointer to the memory block that is to be deallocated.
\r
104 * \return The new reference count for the memory block (should be 0
\r
105 * if successfully deallocated) or -1 if the pointer "ptr" did not
\r
106 * point to a legal memory block.
\r
108 /*------------------------------------------------------------------------------*/
\r
110 memb_free(struct memb_blocks *m, char *ptr)
\r
115 /* Walk through the list of blocks and try to find the block to
\r
116 which the pointer "ptr" points to. */
\r
118 for(i = 0; i < m->num; ++i) {
\r
120 if(ptr2 == ptr - 1) {
\r
121 /* We've found to block to which "ptr" points so we decrease the
\r
122 reference count and return the new value of it. */
\r
125 ptr2 += m->size + 1;
\r
129 /*------------------------------------------------------------------------------*/
\r
131 * Increase the reference count for a memory chunk.
\r
133 * \note No sanity checks are currently made.
\r
135 * \param m m A memory block previosly declared with MEMB().
\r
137 * \param ptr A pointer to the memory chunk for which the reference
\r
138 * count should be increased.
\r
140 * \return The new reference count.
\r
142 /*------------------------------------------------------------------------------*/
\r
144 memb_ref(struct memb_blocks *m, char *ptr)
\r
146 return ++*(ptr - 1);
\r
148 /*------------------------------------------------------------------------------*/
\r