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

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.

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

Allocate a buffer of given size from given heap.

Return
Pointer to allocated buffer, or NULL on failure.
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 share count by once 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

Public Members

void *heap_fixed_t::free_p
size_t heap_fixed_t::size
struct

Public Members

void *heap_dynamic_t::free_p
struct

Public Members

void *heap_t::buf_p
size_t heap_t::size
void *heap_t::next_p
struct heap_fixed_t heap_t::fixed[HEAP_FIXED_SIZES_MAX]
struct heap_dynamic_t heap_t::dynamic