AN2345 Freescale Semiconductor / Motorola, AN2345 Datasheet - Page 18

no-image

AN2345

Manufacturer Part Number
AN2345
Description
Real-Time Memory Manager for StarCore DSPs
Manufacturer
Freescale Semiconductor / Motorola
Datasheet
Building Your Application with VSMM
4.4 Allocating Memory
18
total heap size (THS) = number of blocks * (ABS + VSMM_MEMBLK_HDR_SIZE)
For example, suppose we are creating heaps for an application that requires at most:
• four 16 KB blocks or twenty 774 byte blocks
• or three 16 KB blocks and fifteen 774 byte blocks
• or two 16 KB blocks and ten 774 byte blocks
We create an initial heap of four 16 KB blocks from a specified memory area and then create a second
heap from a memory block of the first heap. We want to fit 20
we recall that each memory block has an additional overhead of 8 bytes
(VSMM_MEMBLK_HDR_SIZE) plus any byte-alignment performed. We verify that we can fit 20
774 blocks within a single 16 KB block, as follows:
ABS = BALIGN(774) = 776-bytes
THS = 20 * (776+ VSMM_MEMBLK_HDR_SIZE) = 15,680 or approximately 15.31 KB
Having verified that heap 2 can indeed be created from the heap 1 memory block, we create our heap
memory area from which heap 1 is to be created. Notice that in this example we are not using the system
heap area but rather a specified area of memory within the data space.
// Define Heap 1’s Memory Block Size
#define HEAP1_BLOCK_SIZE (BALIGN(16384)) // ABS = 16384
// Define Heap 2’s Memory Block Size
#define HEAP2_BLOCK_SIZE (BALIGN(774))
// Heap1[# blocks] [Actual Block Size + 8-byte Overhead]
unsigned char ucHeap1[4] [(HEAP1_BLOCK_SIZE + VSMM_MEMBLK_HDR_SIZE)];
With the memory area defined we can now create our heaps, as follows:
// VSMMMemCreate(HeapMCB, # blocks, actual block size, errorCode)
pstHeap1Ptr = VSMMMemCreate(ucHeap1, 4, HEAP1_BLOCK_SIZE, &ucErrCode);
// VSMMMemAllocCreate(HeapMCB, # blocks, actual block size, errorCode)
pstHeap2Ptr = VSMMMemAllocCreate(pstHeap1Ptr, 20, HEAP2_BLOCK_SIZE, &ucErrCode);
For an actual application, we would typically not create heap 2 until we need it, and we would destroy it
when it is no longer needed. However, for purposes of illustration, we create it here at the same time we
create heap 1. Notice that 16384 is already 8-byte aligned, so the ABS for this block size is 16384.
After the heaps are created, we begin allocating memory to them. We call the VSMMMemAlloc routine,
passing it a pointer to the heap MCB from which the block is to be allocated and a storage area for the
returned error code. It is good practice to validate that the allocation succeeded.
Freescale Semiconductor, Inc.
For More Information On This Product,
Go to: www.freescale.com
// ABS = 776
774 blocks within this second heap, but

Related parts for AN2345