liteos memory (three)

2020-07-31 17:10:13 0 Comment 1054 views
abstract

1. Overview 1.1 Basic Concepts The memory management module manages the memory resources of the system. It is one of the core modules of the operating

1. Overview

1.1 Basic Concepts

The memory management module manages the memory resources of the system. It is one of the core modules of the operating system. It mainly includes the initialization, allocation and release of memory.

During the system operation, the memory management module manages the use of memory by users and the OS through memory application/release operations, so as to optimize the utilization and efficiency of the memory and maximize Solve the problem of system memory fragmentation.

Huawei LiteOS memory management is divided into static memory management and dynamic memory management, providing functions such as memory initialization, allocation, and release.

  • Dynamic memory: allocate memory blocks of user-specified size in the dynamic memory pool.
  • Pros: Allocate on demand.
  • Disadvantage: Fragmentation may occur in the memory pool.
  • Static memory: Allocate a memory block with a preset (fixed) size during user initialization in the static memory pool.
  • Advantages: High allocation and release efficiency, no fragmentation in the static memory pool.
  • Disadvantages: Only the memory block of the initial preset size can be applied for, and cannot be applied on demand.

1.2 Dynamic memory operation mechanism

Dynamic memory management, that is, a relatively large continuous memory (memory pool) configured from the system when memory resources are sufficient , According to user needs, allocate memory blocks of any size. When the user does not need the contentWhen the block is saved, it can be released back to the system for the next use.

Compared with static memory, dynamic memory management has the advantage of on-demand allocation, but the disadvantage is that it is prone to fragmentation in the memory pool.

The system dynamic memory management structure is shown in Figure 1:

liteos memory (three)

Part 1: Heap memory (also called memory pool) The starting address and the total size of the heap area

The second part: itself is an array, each elementElement is a doubly linked list, and the control heads of all free nodes will be classified and hung in the doubly linked list of this array.

Assuming that the smallest node allowed by the memory is 2min bytes, the first doubly linked list of the array stores all sizes of 2minmin +1 free node, the second doubly linked list stores all the free nodes with size 2min+1min+2, and so on, the nth doubly linked list stores all A free node whose size is 2min+n-1

The third part: occupies a large part of the memory pool space, which is used to store the actual area of ​​each node. The following is the LOS_MEM_DYN_NODE node structure declaration and brief introduction:

typedef struct tag LOS_MEM_DYN_NODE
{
LOS_DL_LIST stFreeNodeInfo;
struct tagLOS_MEM_DYN_NODE *pstPreNode;
UINT32 uwSizeAndFlag;
} LOS_MEM_DYN_NODE;

liteos memory (three)

1.3 Static memory operation mechanism

Static memory is essentially a static array. The block size in the static memory pool is set during initialization, and the block size cannot be changed after initialization.

The static memory pool consists of a control block and several memory blocks of the same size. The control block is located at the head of the memory pool and is used for memory block management. The application and release of memory blocks are based on the block size.

p>

Figure 3-4 Schematic diagram of static memory

liteos memory (three)

2. Dynamic memory

2.1 Development guide

2.1.1 Usage scenarios

The main job of memory management is to dynamically divide and manage the memory interval allocated by users.

Dynamic memory management is mainly used in scenarios where users need to use memory blocks of varying sizes.

When the user needs to allocate memory, you canUse the dynamic memory application function of the operating system to request a memory block of a specified size. Once used, the occupied memory is returned through the dynamic memory release function so that it can be reused.

2.1.2 Functions

The dynamic memory management module in the Huawei LiteOS system provides users with the following functions. Please refer to the interface manual for specific APIs.

liteos memory (three)

2.1.3 Development process

  1. Configuration:
  • OS_SYS_MEM_ADDR: the starting address of the system dynamic memory pool, generally do not need to be modified
  • OS_SYS_MEM_SIZE: The system dynamic memory pool size, in bytes, the system allocates unused space after DDR by default
  • LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK: Memory cross-border detection switch, closed by default. After opening, the dynamic memory block out-of-bounds check is performed every time a dynamic memory is requested; each time the static memory is released, the static memory block out-of-bounds check is performed.
  1. Initialize LOS_MemInit.

As shown in the figure after initializing a memory pool, An EndNode is generated, and the remaining memory is all marked as FreeNode nodes. Note: EndNode is the node at the end of the memory pool, and the size is 0.

liteos memory (three)

  1. Apply for dynamic memory LOS_MemAlloc of any size.

Judging whether there is a space of the size of the application in the dynamic memory pool,Get a block of memory and return it as a pointer. If it does not exist, return NULL.

Call the LOS_MemAlloc function three times to create three nodes, assuming the names are UsedA, UsedB, and UsedC, and the sizes are sizeA, sizeB, and sizeC. Because there was only one large FreeNode when the memory pool was first initialized, these memory blocks were cut out from this FreeNode.

liteos memory (three)

When there are multiple FreeNodes in the memory pool, malloc will adapt the FreeNode of the most suitable size. It is used to create new memory blocks and reduce memory fragmentation. If the newly created memory block is not equal to the size of the used FreeNode, after the newly created memory block, the excess memory will be marked as a new FreeNode.

  1. Release the dynamic memory LOS_MemFree.

Reclaim the memory block for the next use.

Assuming that LOS_MemFree is called to release the memory block UsedB, the memory block UsedB will be reclaimed and marked as FreeNode

p>

liteos memory (three)

2.1.4 Platform differences

None

2.2 Programming example

2.2.1 Example description

Huawei LiteOS operation During this period, users need to use memory resources frequently, and the memory resources are limited, so it is necessary to ensure that the limited memory resources are allocated to urgently needed programs and at the same time release unused memory.

The memory management module of Huawei LiteOS can ensure highEffective and correct application and release of memory.

This example performs the following steps:

  1. Initialize a dynamic memory pool.
  2. Apply for a memory block in the dynamic memory pool.
  3. Use this memory block to store a piece of data.
  4. Print out the data stored in the memory block.
  5. Release this memory.

2.2.2 Programming example

VOID los_memory_test() {
UINT32 *p_num = NULL;
UINT32 uwRet;
uwRet = LOS_MemInit( m_aucSysMem0, 32);
if (LOS_OK== uwRet) {
dprintf("The memory pool is initialized successfully! ");
}
else {
dprintf("Memory pool initialization failed! ");
return;
}
/*Allocate memory*/
p_num = (int*)LOS_MemAlloc(m_aucSysMem0, 4);
if (NULL == p_num) {
dprintf("Memory allocation failed! ");
return;
}
dprintf("Memory allocation succeeded ");
/*Assignment*/
*p_num = 828;
dprintf("*p_num = %d ", *p_num);
/*Release memory*/
uwRet = LOS_MemFree(m_aucSysMem0, p_num);
if (LOS_OK == uwRet) {
dprintf("Memory release succeeded! ");
}
else {
dprintf("Memory release failed! ");
}
return;
}

2.2.3 Result verification

Figure 3-5 Results display

liteos memory (three)

3. Static memory

3.1 Development guidance

When users need to use fixed-length memory, they can use static memory allocation to obtain memory. Once used, the occupied memory is returned through the static memory release function so that it can be reused .

3.2 Functions

The static memory management of Huawei LiteOS mainly provides users with the following functions.

liteos memory (three)

3.3 development process

This section introduces the typical scene development process using static memory.

  1. Plan a memory area as a static memory pool.
  2. Call the LOS_MemboxInit interface.

The static memory pool will be initialized inside the system. The memory area specified by the input is divided into N blocks (the value of N depends on the total size of the static memory and the block size), and all memory blocks are linked to Free linked list, place the controller at the beginning of the memory制头。 System head.

  1. Call the LOS_MemboxAlloc interface.

The system will obtain the first free block from the free list and return the user space address of the block.

  1. Call the LOS_MemboxFree interface.

Add this block of memory to the free block list.

  1. Call the LOS_MemboxClr interface.

Clear the static memory block inside the system and clear the memory block corresponding to the input parameter address

3.3 Programming example

During the running of Huawei LiteOS , The user needs to use memory resources frequently, and the memory resources are limited. It is necessary to ensure that the limited memory resources are allocated to the urgently needed programs, and the unusedRAM.

The memory management module can ensure the correct and efficient application to release memory.

This example performs the following steps:

  1. Initialize a static memory pool.
  2. Apply for a piece of static memory from the static memory pool.
  3. Use this memory block to store a piece of data.
  4. Print out the data stored in the memory block.
  5. Clear the data in the memory block.
  6. Release this memory;
VOID los_membox_test(void) {
UINT32 *p_num = NULL;
UINT32 uwBlkSize = 10, uwBoxSize = 100 ;
UINT32 uwRet;
UINT32 pBoxMem[1000];
uwRet = LOS_MemboxInit(&pBoxMem[0], uwBoxSize, uwBlkSize);
if (uwRet != LOS_OK)
{
dprintf("Memory Pool initialization failed! ");
return;
}
else {
dprintf("The memory pool is initialized successfully! ");
}
/*Apply for memory block*/
p_num = (int*)LOS_MemboxAlloc(pBoxMem);
if (NULL == p_num) {
dprintf("Memory allocation failed! ");
return;
}
dprintf("Memory allocation succeeded ");
/*Assignment*/
*p_num = 828;
dprintf("*p_num = %d ", *p_num);
/*Clear memory content*/
LOS_MemboxClr(pBoxMem, p_num);
dprintf("Clear memory content successfully *p_num = %d ", *p_num);
/*Release memory*/
uwRet = LOS_MemboxFree(pBoxMem, p_num);
if(LOS_OK == uwRet) {
dprintf("Memory release successfully! ");
}
else{
dprintf("Memory release failed! ");
}
return;

3.4 Result verification

Figure 3-6 Results display

liteos memory (three)