GSK Reference Manual | ||||
---|---|---|---|---|
GskMemPool; GskMemPoolFixed; void gsk_mem_pool_construct (GskMemPool *pool); void gsk_mem_pool_construct_with_scratch_buf (GskMemPool *pool, gpointer buffer, gsize buffer_size); gpointer gsk_mem_pool_alloc (GskMemPool *pool, gsize size); gpointer gsk_mem_pool_alloc0 (GskMemPool *pool, gsize size); char* gsk_mem_pool_strdup (GskMemPool *pool, const char *str); void gsk_mem_pool_destruct (GskMemPool *pool); void gsk_mem_pool_fixed_construct (GskMemPoolFixed *pool, gsize size); gpointer gsk_mem_pool_fixed_alloc (GskMemPoolFixed *pool); gpointer gsk_mem_pool_fixed_alloc0 (GskMemPoolFixed *pool); void gsk_mem_pool_fixed_free (GskMemPoolFixed *pool, gpointer from_pool); void gsk_mem_pool_fixed_destruct (GskMemPoolFixed *pool);
GSK provides two pooling memory allocators. The first allocator is allocate-only, and can allocate blocks of variable size. The second allocator is alloc-and-free, but must allocate blocks of a fixed-size, which must be chosen when you construct the pool.
Both of these classes use the global allocator (g_new and g_free) as their underlying store.
These may only be accessed by one thread at a time: you should make sure to have a mutex to lock if multiple threads have access to the same mempool. I suspect the majority of use cases already must be mutex-protected for other reasons, but stronger evidence is welcome.
typedef struct { } GskMemPool;
A memory pool. It should be created on the stack or inside another object.
typedef struct { } GskMemPoolFixed;
A fixed-size memory pool. It should be created on the stack or inside another object.
void gsk_mem_pool_construct (GskMemPool *pool);
Initialize the members of a mem-pool. (The memory for the mem-pool structure itself must be provided: either as a part of another structure or on the stack.)
pool : |
the mem-pool to initialize. |
void gsk_mem_pool_construct_with_scratch_buf (GskMemPool *pool, gpointer buffer, gsize buffer_size);
pool : |
|
buffer : |
|
buffer_size : |
gpointer gsk_mem_pool_alloc (GskMemPool *pool, gsize size);
Allocate memory from a pool, This function terminates the program if there is an out-of-memory condition.
pool : |
area to allocate memory from. |
size : |
number of bytes to allocate. |
Returns : | the slab of memory allocated from the pool. |
gpointer gsk_mem_pool_alloc0 (GskMemPool *pool, gsize size);
Allocate memory from a pool, initializing it to 0. This function terminates the program if there is an out-of-memory condition.
pool : |
area to allocate memory from. |
size : |
number of bytes to allocate. |
Returns : | the slab of memory allocated from the pool. |
char* gsk_mem_pool_strdup (GskMemPool *pool, const char *str);
Allocated space from the mem-pool to store the given string (including its terminal NUL character) and copy the string onto that buffer.
If str
is NULL, then NULL is returned.
pool : |
area to allocate memory from. |
str : |
a string to duplicate, or NULL. |
Returns : | a copy of str , allocated from pool .
|
void gsk_mem_pool_destruct (GskMemPool *pool);
Destroy all chunk associated with the given mem-pool.
pool : |
the pool to destroy. |
void gsk_mem_pool_fixed_construct (GskMemPoolFixed *pool, gsize size);
Set up a fixed-size memory allocator for use.
pool : |
the fixed-size memory pool to construct. |
size : |
size of the allocation to take from the mem-pool. |
gpointer gsk_mem_pool_fixed_alloc (GskMemPoolFixed *pool);
Allocate a block of the Fixed-Pool's size.
pool : |
the pool to allocate a block from. |
Returns : | the allocated memory. |
gpointer gsk_mem_pool_fixed_alloc0 (GskMemPoolFixed *pool);
Allocate a block of the Fixed-Pool's size. Set its contents to 0.
pool : |
the pool to allocate a block from. |
Returns : | the allocated, zeroed memory. |
void gsk_mem_pool_fixed_free (GskMemPoolFixed *pool, gpointer from_pool);
Recycle some of the pool's memory back to it.
pool : |
the pool to return memory to. |
from_pool : |
the memory to return to this pool. It must have been allocated from this pool. |
void gsk_mem_pool_fixed_destruct (GskMemPoolFixed *pool);
Free all memory associated with this pool.
pool : |
the pool to destroy. |