Loading...
Searching...
No Matches
memory array allocator

memory array allocator More...

Detailed Description

memory array allocator

pseudo dynamic allocation in static memory arrays

Author
Tobias Heider heide.nosp@m.rt@n.nosp@m.m.ifi.nosp@m..lmu.nosp@m..de
Koen Zandberg koen@.nosp@m.berg.nosp@m.zand..nosp@m.net

Data Structures

struct  memarray_t
 Memory pool. More...
 
struct  memarray_element
 Memory pool element. More...
 

Typedefs

typedef struct memarray_element memarray_element_t
 Memory pool element.
 

Functions

void memarray_init (memarray_t *mem, void *data, size_t size, size_t num)
 Initialize memarray pool with free list.
 
static void * memarray_alloc (memarray_t *mem)
 Allocate memory chunk in memarray pool.
 
static void * memarray_calloc (memarray_t *mem)
 Allocate and clear memory chunk in memarray pool.
 
static void memarray_free (memarray_t *mem, void *ptr)
 Free memory chunk in memarray pool.
 
void memarray_extend (memarray_t *mem, void *data, size_t num)
 Extend the memarray with a new memory region.
 
int memarray_reduce (memarray_t *mem, void *data, size_t num)
 Reduce the memarray space, subtracting the memory pool.
 
size_t memarray_available (memarray_t *mem)
 Returns the number of blocks available.
 

Typedef Documentation

◆ memarray_element_t

Memory pool element.

Internal memarray element struct to increase code readability

Function Documentation

◆ memarray_alloc()

static void * memarray_alloc ( memarray_t mem)
inlinestatic

Allocate memory chunk in memarray pool.

Precondition
mem != NULL
Note
Allocated structure is not cleared before returned
Parameters
[in,out]memmemarray pool to allocate block in
Returns
pointer to allocated structure, if enough memory was available
NULL, on failure

Definition at line 78 of file memarray.h.

◆ memarray_available()

size_t memarray_available ( memarray_t mem)

Returns the number of blocks available.

Parameters
[in]memmemarray pool
Returns
Number of elements available in the memarray pool

◆ memarray_calloc()

static void * memarray_calloc ( memarray_t mem)
inlinestatic

Allocate and clear memory chunk in memarray pool.

Precondition
mem != NULL
Parameters
[in,out]memmemarray pool to allocate block in
Returns
pointer to allocated structure, if enough memory was available
NULL, on failure

Definition at line 99 of file memarray.h.

◆ memarray_extend()

void memarray_extend ( memarray_t mem,
void *  data,
size_t  num 
)

Extend the memarray with a new memory region.

This function extends the memarray pool with a new memory region. The region must be able to fit the supplied number of elements of the size used when initializing this memarray.

Precondition
mem != NULL
data != NULL
num != 0
Parameters
[in,out]memmemarray pool to extend
[in]datapointer to user-allocated data
[in]numnumber of elements in data

◆ memarray_free()

static void memarray_free ( memarray_t mem,
void *  ptr 
)
inlinestatic

Free memory chunk in memarray pool.

Precondition
mem != NULL
ptr != NULL
Parameters
[in,out]memmemarray pool to free block in
[in]ptrpointer to memarray chunk

Definition at line 117 of file memarray.h.

◆ memarray_init()

void memarray_init ( memarray_t mem,
void *  data,
size_t  size,
size_t  num 
)

Initialize memarray pool with free list.

Precondition
mem != NULL
data != NULL
size >= sizeof(void*)
num != 0
Parameters
[in,out]memmemarray pool to initialize
[in]datapointer to user-allocated data
[in]sizesize of a single element in data
[in]numnumber of elements in data

◆ memarray_reduce()

int memarray_reduce ( memarray_t mem,
void *  data,
size_t  num 
)

Reduce the memarray space, subtracting the memory pool.

It is up to the user to free all chunks in the reduced pool. The function will check if all elements in the pool are freed.

Parameters
[in,out]memmemarray pool to reduce
[in]datapointer to the user-allocated data to reduce
[in]numnumber of elements to reduce the data pool with