9.2. heap — Heap

Source code: src/alloc/heap.h, src/alloc/heap.c

Test code: tst/alloc/heap/main.c

Test coverage: src/alloc/heap.c


Defines

HEAP_FIXED_SIZES_MAX

Number of fixed size buffer sizes.

Functions

int heap_init(struct heap_t *self_p, void *buf_p, size_t size, size_t sizes[HEAP_FIXED_SIZES_MAX])

Initialize given heap.

Return
zero(0) or negative error code.
Parameters
  • self_p: Heap to initialize.
  • buf_p: Heap memory buffer.
  • size: Size of the heap memory buffer.
  • sizes: Fixed buffer sizes configuration.

void *heap_alloc(struct heap_t *self_p, size_t size)

Allocate a buffer of given size from given heap. Tries to allocate a fixed size buffer, and allocates a buffer from the dynamic heap if the requested buffer size is greater than the biggest fixed size buffer.

Return
Pointer to allocated buffer, or NULL if no memory could be allocated.
Parameters
  • self_p: Heap to allocate from.
  • size: Number of bytes to allocate.

int heap_free(struct heap_t *self_p, void *buf_p)

Decrement the buffer share counter by one and free the buffer if the count becomes zero(0).

Return
Share count after the free, or negative error code.
Parameters
  • self_p: Heap of given buffer.
  • buf_p: Memory buffer to free.

int heap_share(struct heap_t *self_p, const void *buf_p, int count)

Share given buffer count times.

Return
zero(0) or negative error code.
Parameters
  • self_p: Heap of given buffer.
  • buf_p: Buffer to share.
  • count: Share count.

struct heap_fixed_t

Public Members

void *free_p
size_t size
struct heap_dynamic_t

Public Members

void *free_p
struct heap_t
#include <heap.h>

The heap struct.

Public Members

void *buf_p
size_t size
void *next_p
struct heap_fixed_t fixed[HEAP_FIXED_SIZES_MAX]
struct heap_dynamic_t dynamic
struct mutex_t mutex