Heap

API for doing dynamic memory allocation.

Description

This provides malloc()/free() functionality with locking. The shared resource heap needs to be protected from concurrent access when OS has been started. os_malloc() function grabs a mutex before calling malloc().

API

void *os_malloc(size_t size)

Operating system level malloc().

This ensures that a safe malloc occurs within the context of the OS. Depending on platform, the OS may rely on libc’s malloc() implementation, which is not guaranteed to be thread-safe. This malloc() will always be thread-safe.

Return

A pointer to the memory region allocated.

Parameters
  • size: The number of bytes to allocate

void os_free(void *mem)

Operating system level free().

See description of os_malloc() for reasoning.

Free’s memory allocated by malloc.

Parameters
  • mem: The memory to free.

void *os_realloc(void *ptr, size_t size)

Operating system level realloc().

See description of os_malloc() for reasoning.

Reallocates the memory at ptr, to be size contiguouos bytes.

Return

A pointer to memory of size, or NULL on failure to allocate

Parameters
  • ptr: A pointer to the memory to allocate

  • size: The number of contiguouos bytes to allocate at that location