RainbowBS Manual: Chapter4 Dynamic Memory Managment

RainbowBS

RainbowBS Manual  v0.1.0
Written by QWQ([email protected])
Chapter4 Dynamic Memory Managment

Dynamic memory management is an optional component for memory usage. It offers both memory block and memory pool mechanism.

DMM Features

  • memory block offers fixed size memory management.
  • memory pool offers variable size memory management.
  • both mechanisms support register,byte-alignment,usage check and thread-safety.

Memory Block


Memory Pool


Usage

In an embedded system,there are some memory types with different size such as internal SRAM integrated in micro-controller,external SDRAM,SRAM,etc... and those memory can be byte-addressed in the same address space.Some of them are used for stacks,while some used for global data and some used for dynamic memory allocation.So the specific usage of those memory are decided by the application.

Unlike traditional memory management like malloc in C library,DMM can manage more than one continuous byte-addressed memory for dynamic memory allocation and can be sensitive to memory usage.

As an example,a micro-controller has 8K-bytes size internal RAM in and external 2M-bytes SDRAM.You want to use 2K-bytes internal RAM and 1M-bytes external SDRAM space for dynamic memory allocation.First of all,you just define two byte array,one for the 2K internal RAM and the other for the 1M-bytes external SDRAM,and the use the register interface to define the two memory space.After register,you can allocate memory from them by the DMM handle returned from register.

Allocated memory will return a memory handle and then you can use the memory handle to use,not use or free the allocated memory.

Using RBS_DMM_UseHBlock() or RBS_DMM_UseHMem() to refer to the allocated memory space.

Using RBS_DMM_UnuseHBlock() or BS_DMM_UnuseHMem() to notify the DMM to end the last memory referance.

Using RBS_DMM_FreeBlock() or RBS_DMM_FreeMem() to notify the DMM to release the memory.

RBS_DMM_UseHBloc() and BRS_DMM_UnuseHBlock() are used in pair,while RBS_DMM_UseHMem() and RBS_DMM_UnuseHMem() are in pair.The reason is that each memory handle has a referance counter in internal,and the counter is increased by 1 per usage and decrease by 1 per no usage.So if the counter is not initial value,it will not be released successfully by RBS_DMM_FreeBlock() or RBS_DMM_FreeMem().This is called usage check.Developers should keep in mind that after each reference to the allocated memory by RBS_DMM_UseHBlock() or RBS_DMM_UseHMem(),an RBS_DMM_UnuseHBlock() or RBS_DMM_UnuseHMem() should be called and if he wants to refer to the memory again,then RBS_DMM_UseHBlock() or BS_DMM_UseHMem() should be called again.With usage check,developers will find memory usage conflicts and bugs earlier.For example,there are two running tasks A and B that both using an allocated memory.If task A is using the memory without calling RBS_DMM_UnuseHBlock() or RBS_DMM_UnuseHMem() and task B call RBS_DMM_FreeBlock() or RBS_DMM_FreeMem(),then an error information will be printed by debug component,telling that it can not release memory handle because it is using.

Remember that usage check is only effective if RBS debug component is enabled,otherwise usage check is disabled.
Following is the example code for DMM usage.

1 /*define array*/
2 U8 aInternalRAM[2048] __attribute__((at(0x20000000)));//2K-bytes internal RAM starting from 0x20000000
3 U8 aExternalRAM[1024*1024] __attribute__((at(0x08011000)));//1M-bytes external SDRAM starting from 0x08011000
4 /*register*/
5 hDMM hBlockDMM = RBS_DMM_RegisterBlock("Block DMM",aInternalRAM,sizeof(aInternalRAM),60);//register a memory block with 60 bytes per block.
6 hDMM hPoolDMM = RBS_DMM_RegisterPool("Pool DMM",aExternalRAM,sizeof(aExternalRAM),FALSE,200);//register a memory pool with 200 handle available.
7 /*allocate*/
8 hBLOCK hBlock = RBS_DMM_AllocBlock(hBlockDMM);//allocate a block from hBlockDMM
9 hMEM hMem = RBS_DMM_AllocMem(hPoolDMM,200);//allocate 200 bytes from hPoolDMM
10 /*reference*/
11 void *pBlock = RBS_DMM_UseHBlock(hBlock);//using hBlock
12 void *pMem = RBS_DMM_UseHMem(hMem);//using hMem
13 ...//using memory
14 RBS_DMM_UnuseHBlock(hBlock);//not use hBlock
15 RBS_DMM_UnuseHMem(hMem);//unusing hMem
16 /*free*/
17 RBS_DMM_FreeBlock(hBlock);//release hBlock
18 RBS_DMM_FreeMem(hMem);//release hMem

Configuration

RBS_CFG_DMM_ALIGN is defined for define byte-alignment.3 means 8 bytes,2 means 4 bytes,1 means 2 bytes,0 means 1 bytes.

Allocated memory started address and size are both alignment.So if RBS_CFG_DMM_ALIGN is 2 and you allocated 25 bytes,then you get 28 bytes actually.

Generated by   doxygen 1.8.9.1