Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

chunkallocator : A Memory Chunk Allocator.


Detailed Description

The chunkallocator is a little and simple memory manager for "chunks" or bins of constant size. It it designed to be efficient: it allocates and frees theese memory bins very fast. On the other side, it isn't as flexible the C memory manager (provided by malloc() and Co.).

The chunkallocator is a good alternative to malloc() for structures such as linked list, whose elements are of constant size and are frequently allocated and freed.

You can have many different chunkallocators in your application for different purposes: they all handle independent memory blocks.

Example usage:

   chunkallocator *c;
   List *lst;
   
   // Allocate a new Memory Chunk Allocator for handling list items.
   c = chunkallocator_new (sizeof (List));
   
   // Allocate memory.
   lst = chunkallocator_alloc (c);
   
   // Free allocated memory.
   chunkallocator_free (c, lst);
   
   // Do more complicated things....
   ...
   
   // After using the memory allocator, free it.
   chunkallocator_destroy (c);

Data Structures

struct  chunkallocator
 Chunkallocator structure. More...

struct  chunkblock
 Chunkblock. More...


Defines

#define CHUNKBLOCK_MINSIZE   32
 Minimum of Chunkblock.


Functions

int chunkallocator_increase (chunkallocator *a)
 Allocate a new chunkblock.

chunkallocatorchunkallocator_new (size_t binsize)
 Create a new chunkallocator.

void chunkallocator_destroy (chunkallocator *a)
 Destroy chunkallocator.

void * chunkallocator_alloc (chunkallocator *a)
 Allocate chunk.

void chunkallocator_free (chunkallocator *a, void *memblock)
 Free a chunk.


Define Documentation

#define CHUNKBLOCK_MINSIZE   32
 

This is the size of the first chunkblock to be created. If full, subsequent chunkblocks double its predecessor's size.

Definition at line 42 of file chunkallocator.h.


Function Documentation

void* chunkallocator_alloc chunkallocator   a
 

This function returns a pointer to a new allocated memory chunk. The chunkallocator handles the memory management to do so, and of course, to allocate a chunk of the correct size efficiently.

Internally, the allocator checks if there are dead chunks, that is, previously deallocated chunks that lie fragmented in the memory. If so it assigns its memory. If not, then it returns new memory from the memory pool. If there isn't any memory available either, then it creates a new chunkblock.

Parameters:
a  A pointer to a chunkallocator.
Returns:
A pointer to new memory.

Definition at line 236 of file chunkallocator.c.

void chunkallocator_destroy chunkallocator   a
 

This function frees the memory of the chunkallocator, freeing the structure and all its chunk/memory blocks.

Parameters:
a  A pointer to a chunkallocator.

Definition at line 205 of file chunkallocator.c.

void chunkallocator_free chunkallocator   a,
void *    memblock
 

This function deallocates the chunk/memory block pointed by the argument.

Internally, the allocator just returns it to the memory pool by linking the bin to the list of deallocated (and recyclable) memory chunks.

Parameters:
a  A pointer to a chunkallocator.
memblock  A pointer to a memory chunk to be freed.

Definition at line 297 of file chunkallocator.c.

int chunkallocator_increase chunkallocator   a [static]
 

This function allocates a new chunkblock that doubles the size of its previous block (in the memblock array) and sets the corresponding boundary pointer beginptr and endptr.

Parameters:
a  A pointer to a chunkallocator.
Returns:
0 if O.K., 1 if failed to allocate new chunkblock.

Definition at line 106 of file chunkallocator.c.

chunkallocator* chunkallocator_new size_t    binsize
 

This function creates a new chunkallocator for bins of the given size.

Parameters:
binsize  The size of the bins that the allocator should allocate.
Returns:
A pointer to a new created chunkallocator.

Definition at line 146 of file chunkallocator.c.


Generated on Sun Jun 13 20:51:42 2004 for libgnn Gradient Retropropagation Machine Library by doxygen1.2.18