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. | |
| chunkallocator * | chunkallocator_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. | |
|
|
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. |
|
|
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.
Definition at line 236 of file chunkallocator.c. |
|
|
This function frees the memory of the chunkallocator, freeing the structure and all its chunk/memory blocks.
Definition at line 205 of file chunkallocator.c. |
|
||||||||||||
|
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.
Definition at line 297 of file chunkallocator.c. |
|
|
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.
Definition at line 106 of file chunkallocator.c. |
|
|
This function creates a new chunkallocator for bins of the given size.
Definition at line 146 of file chunkallocator.c. |
1.2.18