Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

apps/telnetd/memb.c File Reference


Detailed Description

Memory block allocation routines.

Author:
Adam Dunkels <adam@sics.se>
The memory block allocation routines provide a simple yet powerful set of functions for managing a set of memory blocks of fixed size. A set of memory blocks is statically declared with the MEMB() macro. Memory blocks are allocated from the declared memory by the memb_alloc() function, and are deallocated with the memb_free() function.

Note:
Because of namespace clashes only one MEMB() can be declared per C module, and the name scope of a MEMB() memory block is local to each C module.
The following example shows how to declare and use a memory block called "cmem" which has 8 chunks of memory with each memory chunk being 20 bytes large.

 MEMB(cmem, 20, 8);

 int main(int argc, char *argv[]) {
    char *ptr;
    
    memb_init(&cmem);

    ptr = memb_alloc(&cmem);

    if(ptr != NULL) {
       do_something(ptr);
    } else {
       printf("Could not allocate memory.\n");
    }

    if(memb_free(ptr) == 0) {
       printf("Deallocation succeeded.\n");
    }
 }

#include <string.h>
#include "memb.h"

Include dependency graph for memb.c:

Include dependency graph

Functions

void memb_init (struct memb_blocks *m)
 Initialize a memory block that was declared with MEMB().

char * memb_alloc (struct memb_blocks *m)
 Allocate a memory block from a block of memory declared with MEMB().

char memb_free (struct memb_blocks *m, char *ptr)
 Deallocate a memory block from a memory block previously declared with MEMB().

char memb_ref (struct memb_blocks *m, char *ptr)
 Increase the reference count for a memory chunk.


Generated on Tue Oct 7 15:51:14 2003 for uIP 0.9 by doxygen 1.3.3