Vision Apps User Guide
Memory allocation APIs

Introduction

This section contains APIs for memory allocation.

Data Structures

struct  app_mem_heap_prm_t
 Heap initialization parameters. More...
 
struct  app_mem_init_prm_t
 Memory module initialization parameters. More...
 
struct  app_mem_stats_t
 Heap statistics and information. More...
 

Functions

static void * APP_MEM_ALIGNPTR (void *val, uint32_t align)
 Align ptr value to 'align' bytes. More...
 
static uint64_t APP_MEM_ALIGN64 (uint64_t val, uint32_t align)
 Align 64b value to 'align' bytes. More...
 
static uint32_t APP_MEM_ALIGN32 (uint32_t val, uint32_t align)
 Align 32b value to 'align' bytes. More...
 
static void appMemEnableL1DandL2CacheWb (void)
 Enable L1D and L2 Cache by performing a WB. More...
 
void appMemInitPrmSetDefault (app_mem_init_prm_t *prm)
 Set defaults to app_mem_init_prm_t. More...
 
int32_t appMemInit (app_mem_init_prm_t *prm)
 Init heaps for memory allocation. More...
 
int32_t appMemDeInit (void)
 De-Init heaps for memory allocation. More...
 
void * appMemAlloc (uint32_t heap_id, uint32_t size, uint32_t align)
 Alloc memory from specific heap. More...
 
bool appMemRegionQuery (uint32_t heap_id)
 Check is specified memory region is enabled. More...
 
int32_t tivxMemRegionTranslate (uint32_t mem_heap_region, uint32_t *heap_id)
 Convert tivx enumerated memory regions to app memory regions. More...
 
int32_t appMemResetScratchHeap (uint32_t heap_id)
 Reset scratch memory. More...
 
int32_t appMemFree (uint32_t heap_id, void *ptr, uint32_t size)
 Free memory that was previously allocated. More...
 
uint32_t appMemGetDmaBufFd (void *virPtr, volatile uint32_t *dmaBufFdOffset)
 Return dmaBufFd of a CMEM buffer pointer. More...
 
int32_t appMemTranslateDmaBufFd (uint32_t dmaBufFd, uint32_t size, uint64_t *virtPtr, uint64_t *phyPtr)
 Translates a given 'dmaBufFd' to virtual and physical addresses. More...
 
void appMemCloseDmaBufFd (uint32_t dmaBufFd)
 Close the dmaBufFd of a CMEM buffer pointer. More...
 
uint64_t appMemGetVirt2PhyBufPtr (uint64_t virtPtr, uint32_t heap_id)
 Return physical ptr corresponds to a CMEM virtual ptr. More...
 
int32_t appMemStats (uint32_t heap_id, app_mem_stats_t *stats)
 Return heap statistics and information. More...
 
void appMemCacheInv (void *ptr, uint32_t size)
 Invalidate a section of memory from cache. More...
 
void appMemCacheWb (void *ptr, uint32_t size)
 Write back a section of memory from cache. More...
 
void appMemCacheWbInv (void *ptr, uint32_t size)
 Write back and invalidate a section of memory from cache. More...
 
void appMemPrintMemAllocInfo (void)
 Print memory allocation from shared memory region. More...
 
uint64_t appMemShared2PhysPtr (uint64_t shared_ptr, uint32_t heap_id)
 Return the physical pointer from a shared pointer. More...
 
uint64_t appMemShared2TargetPtr (uint64_t shared_ptr)
 Return the physical pointer from a shared pointer. More...
 
void * appMemMap (void *phys_addr, uint32_t size)
 Map the provided physical memory to a virtual address. More...
 
int32_t appMemUnMap (void *virt_addr, uint32_t size)
 Unmap the provided virtual memory. More...
 
uint32_t appMemGetNumAllocs (void)
 Get number of memory allocation. More...
 

Macros

#define APP_MEM_HEAP_NAME_MAX   (16u)
 Max characters to use for heap name. More...
 

Memory heap ID

typedef uint64_t(* app_mem_shared_target_fxn) (const uint64_t sharedAddr)
 Shared pointer to target pointer address translation callback function. More...
 
typedef uint64_t(* app_mem_target_shared_fxn) (const uint64_t targetAddr)
 target pointer to shared pointer address translation callback function. More...
 
#define APP_MEM_HEAP_DDR   (0u)
 Heap located in DDR. More...
 
#define APP_MEM_HEAP_L3   (1u)
 Heap located in L3 memory (MSMC) More...
 
#define APP_MEM_HEAP_L2   (2u)
 Heap located in L2 local memory of a CPU. More...
 
#define APP_MEM_HEAP_L1   (3u)
 Heap located in L1 local memory of a CPU. More...
 
#define APP_MEM_HEAP_DDR_SCRATCH   (4u)
 Heap located in DDR and is used as scratch. More...
 
#define APP_MEM_HEAP_DDR_NON_CACHE   (5u)
 Heap located in DDR and in non-cache memory of persistent property. More...
 
#define APP_MEM_HEAP_DDR_NON_CACHE_SCRATCH   (6u)
 Heap located in DDR and in non-cache memory of scratch property. More...
 
#define APP_MEM_HEAP_DDR_WT_CACHE   (7u)
 Heap located in DDR and in cache memory which is configured as write through. More...
 
#define APP_MEM_HEAP_MAX   (8u)
 Max heaps in system. More...
 

Memory heap creation flags

#define APP_MEM_HEAP_FLAGS_TYPE_LINEAR_ALLOCATE   (0x00000001u)
 Heap flag to indicate heap is of type "linear allocater". More...
 
#define APP_MEM_HEAP_FLAGS_IS_SHARED   (0x00000004u)
 Heap flag to indicate if memory that is allocated will be shared with another CPU. More...
 
#define APP_MEM_HEAP_FLAGS_DO_CLEAR_ON_ALLOC   (0x00000008u)
 Heap flag to indicate if memory should be set to 0 after alloc. More...
 

Macro Definition Documentation

◆ APP_MEM_HEAP_NAME_MAX

#define APP_MEM_HEAP_NAME_MAX   (16u)

Max characters to use for heap name.

◆ APP_MEM_HEAP_DDR

#define APP_MEM_HEAP_DDR   (0u)

Heap located in DDR.

◆ APP_MEM_HEAP_L3

#define APP_MEM_HEAP_L3   (1u)

Heap located in L3 memory (MSMC)

◆ APP_MEM_HEAP_L2

#define APP_MEM_HEAP_L2   (2u)

Heap located in L2 local memory of a CPU.

◆ APP_MEM_HEAP_L1

#define APP_MEM_HEAP_L1   (3u)

Heap located in L1 local memory of a CPU.

◆ APP_MEM_HEAP_DDR_SCRATCH

#define APP_MEM_HEAP_DDR_SCRATCH   (4u)

Heap located in DDR and is used as scratch.

◆ APP_MEM_HEAP_DDR_NON_CACHE

#define APP_MEM_HEAP_DDR_NON_CACHE   (5u)

Heap located in DDR and in non-cache memory of persistent property.

◆ APP_MEM_HEAP_DDR_NON_CACHE_SCRATCH

#define APP_MEM_HEAP_DDR_NON_CACHE_SCRATCH   (6u)

Heap located in DDR and in non-cache memory of scratch property.

◆ APP_MEM_HEAP_DDR_WT_CACHE

#define APP_MEM_HEAP_DDR_WT_CACHE   (7u)

Heap located in DDR and in cache memory which is configured as write through.

◆ APP_MEM_HEAP_MAX

#define APP_MEM_HEAP_MAX   (8u)

Max heaps in system.

◆ APP_MEM_HEAP_FLAGS_TYPE_LINEAR_ALLOCATE

#define APP_MEM_HEAP_FLAGS_TYPE_LINEAR_ALLOCATE   (0x00000001u)

Heap flag to indicate heap is of type "linear allocater".

Here alloc increments a offset by alloc size and free resets the offset. This is used typically for L2 memory to allocate it as scratch among multiple algorithms on DSP.

When this flag is not set heap type is normal dynamic memory allocator heap

◆ APP_MEM_HEAP_FLAGS_IS_SHARED

#define APP_MEM_HEAP_FLAGS_IS_SHARED   (0x00000004u)

Heap flag to indicate if memory that is allocated will be shared with another CPU.

◆ APP_MEM_HEAP_FLAGS_DO_CLEAR_ON_ALLOC

#define APP_MEM_HEAP_FLAGS_DO_CLEAR_ON_ALLOC   (0x00000008u)

Heap flag to indicate if memory should be set to 0 after alloc.

Typedef Documentation

◆ app_mem_shared_target_fxn

typedef uint64_t(* app_mem_shared_target_fxn) (const uint64_t sharedAddr)

Shared pointer to target pointer address translation callback function.

This callback function is used to translate the pointer shared across cores to a pointer that can be used by the core it is called from

Parameters
sharedAddr[IN] Shared address across remote cores
Returns
Corresponding target address

◆ app_mem_target_shared_fxn

typedef uint64_t(* app_mem_target_shared_fxn) (const uint64_t targetAddr)

target pointer to shared pointer address translation callback function.

This callback function is used to translate a pointer that is be used by the core to the shared pointer that can be used across cores

Parameters
targetAddr[IN] Target address of the calling core
Returns
Corresponding shared address

Function Documentation

◆ APP_MEM_ALIGNPTR()

static void * APP_MEM_ALIGNPTR ( void *  val,
uint32_t  align 
)
inlinestatic

Align ptr value to 'align' bytes.

◆ APP_MEM_ALIGN64()

static uint64_t APP_MEM_ALIGN64 ( uint64_t  val,
uint32_t  align 
)
inlinestatic

Align 64b value to 'align' bytes.

◆ APP_MEM_ALIGN32()

static uint32_t APP_MEM_ALIGN32 ( uint32_t  val,
uint32_t  align 
)
inlinestatic

Align 32b value to 'align' bytes.

◆ appMemEnableL1DandL2CacheWb()

static void appMemEnableL1DandL2CacheWb ( void  )
inlinestatic

Enable L1D and L2 Cache by performing a WB.

Applicable only for J784S4 SOC due to coherency difference on these SOC's

◆ appMemInitPrmSetDefault()

void appMemInitPrmSetDefault ( app_mem_init_prm_t prm)

Set defaults to app_mem_init_prm_t.

Recommend to be called before calling appMemInit() Override default with user specific parameters

By default all heaps are disabled.

Parameters
prm[out] Default initialized parameters

◆ appMemInit()

int32_t appMemInit ( app_mem_init_prm_t prm)

Init heaps for memory allocation.

Parameters
prm[in] Initialization parameters
Returns
0 on success else failure

◆ appMemDeInit()

int32_t appMemDeInit ( void  )

De-Init heaps for memory allocation.

Returns
0 on success else failure

◆ appMemAlloc()

void* appMemAlloc ( uint32_t  heap_id,
uint32_t  size,
uint32_t  align 
)

Alloc memory from specific heap.

Parameters
heap_id[in] See APP_MEM_HEAP_*
size[in] Size in bytes to allocate
align[in] Minimum alignment requested
Returns
pointer to memory or NULL in case of failure

◆ appMemRegionQuery()

bool appMemRegionQuery ( uint32_t  heap_id)

Check is specified memory region is enabled.

Parameters
heap_id[in] See APP_MEM_HEAP_*
Returns
true if region is enabled, false otherwise

◆ tivxMemRegionTranslate()

int32_t tivxMemRegionTranslate ( uint32_t  mem_heap_region,
uint32_t *  heap_id 
)

Convert tivx enumerated memory regions to app memory regions.

Parameters
mem_heap_region[in] See TIVX_MEM_*
heap_id[in] See APP_MEM_HEAP_*
Returns
0 on success else failure

◆ appMemResetScratchHeap()

int32_t appMemResetScratchHeap ( uint32_t  heap_id)

Reset scratch memory.

Parameters
heap_id[in] See APP_MEM_HEAP_*
Returns
0 on success else failure

◆ appMemFree()

int32_t appMemFree ( uint32_t  heap_id,
void *  ptr,
uint32_t  size 
)

Free memory that was previously allocated.

Parameters
heap_id[in] See APP_MEM_HEAP_*
ptr[in] pointer to allocated memory
size[in] size of allocated memory
Returns
0 on success else failure

◆ appMemGetDmaBufFd()

uint32_t appMemGetDmaBufFd ( void *  virPtr,
volatile uint32_t *  dmaBufFdOffset 
)

Return dmaBufFd of a CMEM buffer pointer.

Parameters
virPtr[in] pointer to allocated memory
dmaBufFdOffset[out] offset within the DMA BUF FD where the data is located
Returns
dmaBufFd or NULL in case of failure

◆ appMemTranslateDmaBufFd()

int32_t appMemTranslateDmaBufFd ( uint32_t  dmaBufFd,
uint32_t  size,
uint64_t *  virtPtr,
uint64_t *  phyPtr 
)

Translates a given 'dmaBufFd' to virtual and physical addresses.

Parameters
dmaBufFd[in] DMA Buffer Id to translate.
size[in] Size of buffer in bytes
virtPtr[out] Virtual address of the memory represented by 'dmaBufFd'.
phyPtr[out] Physical address of the memory represented by 'dmaBufFd'.
Returns
0, if the translation is successful or < 0 otherwise.

◆ appMemCloseDmaBufFd()

void appMemCloseDmaBufFd ( uint32_t  dmaBufFd)

Close the dmaBufFd of a CMEM buffer pointer.

Parameters
dmaBufFd[in] value of dmaBufFd

◆ appMemGetVirt2PhyBufPtr()

uint64_t appMemGetVirt2PhyBufPtr ( uint64_t  virtPtr,
uint32_t  heap_id 
)

Return physical ptr corresponds to a CMEM virtual ptr.

Parameters
virtPtr[in] pointer to allocated memory
heap_id[in] heap ID
Returns
Physical pointer to memory or NULL in case of failure

◆ appMemStats()

int32_t appMemStats ( uint32_t  heap_id,
app_mem_stats_t stats 
)

Return heap statistics and information.

Parameters
heap_id[in] See APP_MEM_HEAP_*
stats[in] Heap statistics and information
Returns
0 on success else failure

◆ appMemCacheInv()

void appMemCacheInv ( void *  ptr,
uint32_t  size 
)

Invalidate a section of memory from cache.

Parameters
ptr[in] Memory address for cache ops
size[in] Size of memory to perform the cache ops

◆ appMemCacheWb()

void appMemCacheWb ( void *  ptr,
uint32_t  size 
)

Write back a section of memory from cache.

Parameters
ptr[in] Memory address for cache ops
size[in] Size of memory to perform the cache ops

◆ appMemCacheWbInv()

void appMemCacheWbInv ( void *  ptr,
uint32_t  size 
)

Write back and invalidate a section of memory from cache.

Parameters
ptr[in] Memory address for cache ops
size[in] Size of memory to perform the cache ops

◆ appMemPrintMemAllocInfo()

void appMemPrintMemAllocInfo ( void  )

Print memory allocation from shared memory region.

◆ appMemShared2PhysPtr()

uint64_t appMemShared2PhysPtr ( uint64_t  shared_ptr,
uint32_t  heap_id 
)

Return the physical pointer from a shared pointer.

Parameters
shared_ptr[in] pointer to allocated memory
heap_id[in] heap ID
Returns
physical pointer to memory

◆ appMemShared2TargetPtr()

uint64_t appMemShared2TargetPtr ( uint64_t  shared_ptr)

Return the physical pointer from a shared pointer.

Parameters
physPtr[in] pointer to physical memory
Returns
pointer which can be used on target

◆ appMemMap()

void* appMemMap ( void *  phys_addr,
uint32_t  size 
)

Map the provided physical memory to a virtual address.

Parameters
phys_addr[in] pointer to physical memory
size[in] size of physical memory
Returns
pointer to mapped virtual memory

◆ appMemUnMap()

int32_t appMemUnMap ( void *  virt_addr,
uint32_t  size 
)

Unmap the provided virtual memory.

Parameters
virt_addr[in] pointer to virtual memory
size[in] size of virtual memory
Returns
status of the operation

◆ appMemGetNumAllocs()

uint32_t appMemGetNumAllocs ( void  )

Get number of memory allocation.