sgdk
|
Pool object management unit. More...
Go to the source code of this file.
Classes | |
struct | Pool |
Object pool allocator structure. More... | |
Functions | |
Pool * | POOL_create (u16 size, u16 objectSize) |
Create and allocate a new object pool allocator. | |
void | POOL_destroy (Pool *pool) |
Release the specified object pool allocator. | |
void | POOL_reset (Pool *pool, bool clear) |
Reset the 'object' pool allocator. | |
void * | POOL_allocate (Pool *pool) |
Allocate a new 'object' from the specified object pool. | |
void | POOL_release (Pool *pool, void *object, bool maintainCoherency) |
Release an objet from the specified object pool. | |
u16 | POOL_getFree (Pool *pool) |
u16 | POOL_getNumAllocated (Pool *pool) |
void ** | POOL_getFirst (Pool *pool) |
s16 | POOL_find (Pool *pool, void *object) |
Pool object management unit.
This unit provides methods to manage dynamic object allocation.
You can use Pool object to handle dynamic allocation from a fixed set of objects.
For instance if you may need to handle dynamically bullets for your game and you want to have at max 20 bullets, you can handle it that way:
Pool* bulletPool = POOL_create(20, sizeof(Bullet)); ... // create a new bullet Bullet* bullet = POOL_allocate(bulletPool); // check if bullet was correctly created and do your stuff.. if (bullet != NULL) { ... } ... // release your bullet POOL_release(bulletPool, bullet);
Pool object is also very useful for fast iteration over allocated objects:
Bullet** bullets = POOL_getFirst(bulletPool); u16 num = POOL_getNumAllocated(bulletPool); while(num--) { Bullet* bullet = *bullets++; // do whatever you need on your bullet ... }
void* POOL_allocate | ( | Pool * | pool | ) |
Allocate a new 'object' from the specified object pool.
pool | Object pool allocator |
Create and allocate a new object pool allocator.
size | the capacity of the pool (in number of object) |
objectSize | the size of a single object (usually you should use sizeof(Struct) here, always aligned on 2) |
void POOL_destroy | ( | Pool * | pool | ) |
Release the specified object pool allocator.
pool | Object pool allocator to release |
pool | Object pool allocator |
object | Object to get slot position |
void** POOL_getFirst | ( | Pool * | pool | ) |
pool | Object pool allocator |
pool | Object pool allocator |
pool | Object pool allocator |
Release an objet from the specified object pool.
pool | Object pool allocator |
object | Object to release |
maintainCoherency | set it to TRUE if you want to keep coherency for stack iteration (see POOL_getFirst()). Set it to FALSE for faster release process if you don't require object iteration through alloc stack. |