To which you create a `MemPool` instance and give the function a max amount of memory you wish or require for your data.
To which you create a `MemPool` instance and give the function a max amount of memory you wish or require for your data.
Remember not to exceed that memory amount or the allocation functions of the allocator will give you a NULL pointer.
Remember not to exceed that memory amount or the allocation functions of the allocator will give you a NULL pointer.
So we create a pool that will malloc 10K bytes.
So we create a pool that will malloc 10K bytes.
```c
```c
MemPool pool = MemPool_Create(10000);
MemPool pool = CreateMemPool(10000);
```
```
When we finish using the pool's memory, we clean it up by using `MemPool_Destroy`.
When we finish using the pool's memory, we clean it up by using `DestroyMemPool`.
```c
```c
MemPool_Destroy(&pool);
DestroyMemPool(&pool);
```
```
Alternatively, if you're not in a position to use any kind of dynamic allocation from the operating system, you have the option to utilize an existing buffer as memory for the mempool:
Alternatively, if you're not in a position to use any kind of dynamic allocation from the operating system, you have the option to utilize an existing buffer as memory for the mempool:
```c
```c
char mem[64000];
char mem[64000];
MemPool pool = MemPool_FromBuffer(mem, sizeof mem);
MemPool pool = CreateMemPoolFromBuffer(mem, sizeof mem);
`MemPool_Alloc` returns a (zeroed) pointer to a memory block.
`MemPoolAlloc` returns a (zeroed) pointer to a memory block.
```c
```c
// allocate an int pointer.
// allocate an int pointer.
int *i = MemPool_Alloc(&pool, sizeof *i);
int *i = MemPoolAlloc(&pool, sizeof *i);
```
```
`MemPool_Realloc` works similar but it takes an existing pointers and resizes its data, it does NOT zero the memory as it exists for resizing existing data. Please note that if you resize a smaller size, the data WILL BE TRUNCATED/CUT OFF.
If the `ptr` argument for `MemPool_Realloc` is `NULL`, it will work just like a call to `MemPool_Alloc`.
`MemPoolRealloc` works similar but it takes an existing pointers and resizes its data. Please note that if you resize a smaller size, the data WILL BE TRUNCATED/CUT OFF.
If the `ptr` argument for `MemPoolRealloc` is `NULL`, it will work just like a call to `MemPoolAlloc`.
```c
```c
// allocate an int pointer.
// allocate an int pointer.
int *i = MemPool_Realloc(&pool, NULL, sizeof *i);
int *i = MemPoolRealloc(&pool, NULL, sizeof *i);
// resize the pointer into an int array of 10 cells!
// resize the pointer into an int array of 10 cells!
i = MemPool_Realloc(&pool, i, sizeof *i * 10);
i = MemPoolRealloc(&pool, i, sizeof *i * 10);
```
```
To deallocate memory back to the pool, there's also two functions:
To deallocate memory back to the pool, there's also two functions:
`MemPool_Free` will deallocate the pointer data back to the memory pool.
`MemPoolFree` will deallocate the pointer data back to the memory pool.
```c
```c
// i is now deallocated! Remember that i is NOT NULL!
// i is now deallocated! Remember that i is NOT NULL!
MemPool_Free(&pool, i);
MemPoolFree(&pool, i);
```
```
`MemPool_CleanUp` instead takes a pointer to an allocated pointer and then calls `MemPool_Free` for that pointer and then sets it to NULL.
`MemPoolCleanUp` instead takes a pointer to an allocated pointer and then calls `MemPoolFree` for that pointer and then sets it to NULL.
```c
```c
// deallocates i and sets the pointer to NULL.
// deallocates i and sets the pointer to NULL.
MemPool_CleanUp(&pool, (void **)&i);
MemPoolCleanUp(&pool, (void **)&i);
// i is now NULL.
// i is now NULL.
```
```
Using `MemPool_CleanUp` is basically a shorthand way of doing this code:
Using `MemPoolCleanUp` is basically a shorthand way of doing this code:
```c
```c
// i is now deallocated! Remember that i is NOT NULL!
// i is now deallocated! Remember that i is NOT NULL!
MemPool_Free(&pool, i);
MemPoolFree(&pool, i);
// i is now NULL obviously.
// i is now NULL obviously.
i = NULL;
i = NULL;
```
```
Given that the memory pool is based on a stack allocator, that means to refill the stack, you must free in the opposite order you allocate memory. You can still free memory out of order but that'll create memory block fragments.
Freed memory blocks that are not absorbed into the stack allocator are placed into the allocator's free list. If you have too many nodes, the allocation functions might take a while to calculate as the allocating functions first check the free list for any memory blocks it can give you before falling back to the stack allocator.
If you have too many nodes (You can check by accessing the `freeList` struct and its `len` member like: `pool.freeList.len`), then you'll have to defragment the free list. Defragmenting consists of merging together memory blocks that are physically near one another.
You have two options in terms of defragging:
You can manually call the defrag function:
```c
bool MemPool_DeFrag(MemPool *mempool);
```
... which will return true or false depending if it was able to merge any nodes.
Or you can set a node limit and enable auto defragging.
Auto defragging is disabled by default, you can turn it on or off either by calling:
```c
void MemPool_ToggleAutoDefrag(MemPool *mempool);
```
or set it directly from the `freeList` struct member:
```c
pool.freeList.autoDefrag = true; // set to on.
pool.freeList.autoDefrag = false; // set to off.
pool.freeList.autoDefrag ^= true; // toggle on or off.
```
For auto defragging to work, you must also set a maximum memory block node limit. You can set it directly with the `maxNodes` member in the `freeList` struct member:
If there's a moment when you want to not only free all your allocated data but refresh the allocator in its entirety, there's:
```c
```c
// set free memory block node limit to 100!
pool.freeList.maxNodes = 100UL;
void MemPoolReset(MemPool *mempool);
```
```
If auto defragging is not enabled, then the node limit is ignored of course.
which will completely re-merge all freelist blocks and zero out the allocator's buffer memory.
Make sure to `NULL` all living pointers when doing this.
Finally, to get the total amount of memory remaining (to make sure you don't accidentally over-allocate), you utilize this function:
Finally, to get the total amount of memory remaining (to make sure you don't accidentally over-allocate), you utilize this function:
@ -170,14 +144,14 @@ The raylib Object Pool is a fast and minimal fixed-size allocator.
**Purpose**:
**Purpose**:
raylib Object Pool was created as a complement to the raylib Memory Pool.
raylib Object Pool was created as a complement to the raylib Memory Pool.
Due to the general purpose nature of raylib Memory Pool, memory block fragmentations can affect allocation and deallocation speeds. Because of this, the raylib Object pool succeeds by having no fragmentation and accommodating for allocating fixed-size data while the raylib memory pool accommodates allocating variadic/differently sized data.
Due to the general purpose nature of raylib Memory Pool, memory block fragmentations can affect allocation and deallocation speeds. Because of this, the raylib Object pool succeeds by having no fragmentation and accommodating for allocating fixed-size data while the raylib memory pool accommodates allocating variadic/differently sized data.
**Implementation**:
**Implementation**:
The object pool is implemented as a hybrid array-stack of cells that are large enough to hold the size of your data at initialization:
The object pool is implemented as a hybrid array-stack of cells that are large enough to hold the size of your data at initialization:
```c
```c
typedef struct ObjPool {
typedef struct ObjPool {
Stack stack;
size_t objSize, freeBlocks;
uintptr_t mem, offs;
size_t objSize, freeBlocks, memSize;
} ObjPool;
} ObjPool;
```
```
@ -185,8 +159,8 @@ Due to the general purpose nature of raylib Memory Pool, memory block fragmentat
The object pool is designed to be used as a direct object.
The object pool is designed to be used as a direct object.
`ObjPool_Free` will deallocate the object pointer data back to the memory pool.
`ObjPoolFree` will deallocate the object pointer data back to the memory pool.
```c
```c
ObjPool_Free(&vector_pool, origin);
ObjPoolFree(&vector_pool, origin);
```
```
Like raylib memory pool, the raylib object pool also comes with a convenient clean up function that takes a pointer to an allocated pointer, frees it, and sets the pointer to NULL for you!
Like raylib memory pool, the raylib object pool also comes with a convenient clean up function that takes a pointer to an allocated pointer, frees it, and sets the pointer to NULL for you!