Memory Allocation API

Location: include/sof/lib/alloc.h

group alloc_api

Heap zone flags

SOF_MEM_FLAG_NO_COPY

Indicates that original content should not be copied by realloc.

SOF_MEM_FLAG_COHERENT

Indicates that if we should return uncached address.

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.

5) Runtime Shared Zone. Similar to Runtime Zone, but content may be used and fred from any enabled core.

6) System Shared Zone. Similar to System Zone, but content may be used from any enabled core.

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

Values:

enumerator SOF_MEM_ZONE_SYS

System zone.

enumerator SOF_MEM_ZONE_SYS_RUNTIME

System-runtime zone.

enumerator SOF_MEM_ZONE_RUNTIME

Runtime zone.

enumerator SOF_MEM_ZONE_BUFFER

Buffer zone.

enumerator SOF_MEM_ZONE_RUNTIME_SHARED

Runtime shared zone.

enumerator SOF_MEM_ZONE_SYS_SHARED

System shared zone.

Functions

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

Allocates memory block.

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.

Returns:

Pointer to the allocated memory or NULL if failed.

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.

Parameters:
  • flags – Flags, see SOF_MEM_FLAG_…

  • caps – Capabilities, see SOF_MEM_CAPS_…

  • bytes – Size in bytes.

  • alignment – Alignment in bytes.

Returns:

Pointer to the allocated memory or NULL if failed.

static inline 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.

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.

Returns:

Pointer to the resized memory of NULL if failed.

static inline 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.

Parameters:

s – String.

Returns:

Length of the string in bytes.

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

Compares two strings, see man strcmp.

Parameters:
  • s1 – First string to compare.

  • s2 – Second string to compare.

Returns:

See man strcmp.