Memory Pool
Create a pool of memory blocks
Typedefs | Functions
memory_pool.h File Reference
#include <stdlib.h>
#include <stdbool.h>
Include dependency graph for memory_pool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct memory_pool memory_pool_t
 

Functions

memory_pool_tmemory_pool_init (size_t count, size_t block_size)
 memory pool initializer More...
 
bool memory_pool_destroy (memory_pool_t *mp)
 
void * memory_pool_acquire (memory_pool_t *mp)
 memory pool pop function More...
 
bool memory_pool_release (memory_pool_t *mp, void *data)
 memory pool push function More...
 
size_t memory_pool_available (memory_pool_t *mp)
 memory pool availability function More...
 
void memory_pool_dump (memory_pool_t *mp)
 memory pool dump function More...
 

Typedef Documentation

◆ memory_pool_t

typedef struct memory_pool memory_pool_t

Definition at line 23 of file memory_pool.h.

Function Documentation

◆ memory_pool_acquire()

void* memory_pool_acquire ( memory_pool_t mp)

memory pool pop function

Parameters
mpa pointer to a memory pool object
Returns
NULL if nothing to pop
data a pointer to top data block

Definition at line 214 of file memory_pool.c.

◆ memory_pool_available()

size_t memory_pool_available ( memory_pool_t mp)

memory pool availability function

Parameters
mpa pointer to a memory pool object
Returns
number of available data blocks
0 if no data blocks available or mp invalid

Definition at line 293 of file memory_pool.c.

◆ memory_pool_destroy()

bool memory_pool_destroy ( memory_pool_t mp)
Todo:
make sure that header gets freed as well with datablock

Definition at line 154 of file memory_pool.c.

◆ memory_pool_dump()

void memory_pool_dump ( memory_pool_t mp)

memory pool dump function

Parameters
mpa pointer to a memory pool object

Iterates through all free data blocks in memory pool, printing metadata about each to user.

Definition at line 310 of file memory_pool.c.

◆ memory_pool_init()

memory_pool_t* memory_pool_init ( size_t  count,
size_t  block_size 
)

memory pool initializer

Parameters
countnumber of nodes (headers) in pool
block_sizesize of each data block pointed to by nodes
Returns
NULL if OOM, or if for loop not fully traversed
memory address of mp if successful
Todo:
make sure this does not leak memory if for loop not fully traversed (case n != count)

Allocates one memory pool struct and count number of memory pool block and header combinations. Also allocates memory for count number of pointers in shadow pointer array.

out of memory

Definition at line 90 of file memory_pool.c.

◆ memory_pool_release()

bool memory_pool_release ( memory_pool_t mp,
void *  data 
)

memory pool push function

Parameters
mpa pointer to a memory pool object
dataa pointer to a data block with attached header
Returns
true if successful

Definition at line 253 of file memory_pool.c.