Memory Allocation API

Location: include/sof/lib/alloc.h

group alloc_api

Heap zone flags

SOF_MEM_FLAG_SHARED

Indicates that allocated memory block must be shareable between DSP cores.

SOF_MEM_FLAG_NO_COPY

Indicates that original content should not be copied by realloc.

Defines

bzero(ptr, size)

Zeroes memory block.

Parameters
  • ptr: Pointer to the memory block.

  • size: Size of the block in bytes.

Enums

enum mem_zone

Heap Memory Zones.

The heap has three different zones from where memory can be allocated :-

1) System Zone. Fixed size heap where alloc always succeeds and is never freed. Used by any init code that will never give up the memory.

2) System Runtime Zone. Heap zone intended for runtime objects allocated by the kernel part of the code.

3) Runtime Zone. Main and larger heap zone where allocs are not guaranteed to succeed. Memory can be freed here.

4) Buffer Zone. Largest heap zone intended for audio buffers.

See platform/memory.h for heap size configuration and mappings.

Values:

SOF_MEM_ZONE_SYS = 0

System zone.

SOF_MEM_ZONE_SYS_RUNTIME

System-runtime zone.

SOF_MEM_ZONE_RUNTIME

Runtime zone.

SOF_MEM_ZONE_BUFFER

Buffer zone.

Functions

void *rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes)

Allocates memory block.

Return

Pointer to the allocated memory or NULL if failed.

Note

Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). Use rballoc(), rballoc_align() to allocate memory for buffers.

Parameters
  • zone: Zone to allocate memory from, see enum mem_zone.

  • flags: Flags, see SOF_MEM_FLAG_…

  • caps: Capabilities, see SOF_MEM_CAPS_…

  • bytes: Size in bytes.

void *rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes)

Similar to rmalloc(), guarantees that returned block is zeroed.

Note

Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). rballoc(), rballoc_align() to allocate memory for buffers.

void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes, uint32_t alignment)

Allocates memory block from SOF_MEM_ZONE_BUFFER.

Return

Pointer to the allocated memory or NULL if failed.

Parameters
  • flags: Flags, see SOF_MEM_FLAG_…

  • caps: Capabilities, see SOF_MEM_CAPS_…

  • bytes: Size in bytes.

  • alignment: Alignment in bytes.

static void *rballoc(uint32_t flags, uint32_t caps, size_t bytes)

Similar to rballoc_align(), returns buffer aligned to PLATFORM_DCACHE_ALIGN.

void *rbrealloc_align(void *ptr, uint32_t flags, uint32_t caps, size_t bytes, size_t old_bytes, uint32_t alignment)

Changes size of the memory block allocated from SOF_MEM_ZONE_BUFFER.

Return

Pointer to the resized memory of NULL if failed.

Parameters
  • ptr: Address of the block to resize.

  • flags: Flags, see SOF_MEM_FLAG_…

  • caps: Capabilities, see SOF_MEM_CAPS_…

  • bytes: New size in bytes.

  • old_bytes: Old size in bytes.

  • alignment: Alignment in bytes.

static void *rbrealloc(void *ptr, uint32_t flags, uint32_t caps, size_t bytes, size_t old_bytes)

Similar to rballoc_align(), returns resized buffer aligned to PLATFORM_DCACHE_ALIGN.

void rfree(void *ptr)

Frees the memory block.

Note

Blocks from SOF_MEM_ZONE_SYS cannot be freed, such a call causes panic.

Parameters
  • ptr: Pointer to the memory block.

void *rzalloc_core_sys(int core, size_t bytes)

Allocates memory block from the system heap reserved for the specified core.

Parameters
  • core: Core id.

  • bytes: Size in bytes.

int rstrlen(const char *s)

Calculates length of the null-terminated string.

Return

Length of the string in bytes.

Parameters
  • s: String.

int rstrcmp(const char *s1, const char *s2)

Compares two strings, see man strcmp.

Return

See man strcmp.

Parameters
  • s1: First string to compare.

  • s2: Second string to compare.