37 KiB
Development Guidelines
Usage Scenarios
The main task of memory management is to dynamically partition and manage user allocated memory intervals.
Dynamic memory management is used when users have different demands on memory blocks. When a user needs memory, the system calls the LOS_MemAlloc API to allocate the requested amount of memory. When the user no longer needs the memory, the system calls the LOS_MemFree API to free it up.
Functions
The dynamic memory management module of Huawei LiteOS provides the following functions. For details about the APIs, see the API reference.
- Debugging functions are provided for dynamic memory. For details, see Memory Debugging.
- For the bestfit_little algorithm, only Multi-Memory Pool Mechanism and Memory Validity Check are supported. Other memory debugging functions are not supported.
- Among the preceding APIs, the ones controlled through macro are all related to memory debugging.
- After the LOS_MemRealloc or LOS_MemMrealloc operation is performed on the memory applied using LOS_MemAllocAlign or LOS_MemMallocAlign, the new start address of memory may become unaligned.
- For the bestfit_little algorithm, the LOS_MemRealloc operation cannot be performed on the memory applied using LOS_MemAllocAlign. Otherwise, failure will be returned.
Development Process
This section describes the development process of dynamic memory in typical scenarios.
-
Configure the start address and size of a dynamic memory pool in the los_config.h file.
Size (in bytes) of the system dynamic memory pool (DDR adaptive).
From the end of the BSS segment to the end of the system DDR
- OS_SYS_MEM_ADDR: In most cases, retain the default value.
- OS_SYS_MEM_SIZE: In most cases, retain the default value.
-
Run the make menuconfig command and choose Kernel > Memory Management to configure the dynamic memory management module.
-
Call the LOS_MemInit API to initialize a memory pool.
As shown in the figure below, an EndNode is generated after the initialization, and all the remaining memory is marked as a FreeNode. EndNode is the last node in the memory pool, and its size is 0.
-
Call the LOS_MemAlloc API to apply for dynamic memory of any size.
The system determines whether the dynamic memory pool has sufficient memory. If yes, it takes the required size out of the memory pool and returns a pointer. If no, it returns "NULL" instead.
The LOS_MemAlloc function can be called three times to create three nodes, namely, UsedA, UsedB, and UsedC with sizeA, sizeB, and sizeC, respectively. There is only one large FreeNode when the memory pool is initialized, so these memory blocks are divided from the FreeNode.
If there are multiple FreeNodes in the memory pool, the one of the most appropriate size will be used to create a memory block during malloc, which can reduce memory fragments. If the created memory block is not the size of the selected FreeNode, the remaining memory will be marked as a new FreeNode.
-
Call the LOS_MemFree API to free up dynamic memory.
The reclaimed memory block can be used next time.
Assume that LOS_MemFree is called to free up UsedB. The reclaimed UsedB block will be marked as a FreeNode. When memory blocks are reclaimed, adjacent FreeNodes are automatically merged.
Platform Differences
None