开源鸿蒙 Static Memory
Static Memory
Working Principles
The static memory is a static array. The block size in the static memory pool is set during initialization and cannot be changed after initialization.
The static memory pool consists of a control block LOS_MEMBOX_INFO and several memory blocks LOS_MEMBOX_NODE of the same size. The control block is located at the head of the memory pool and used for memory block management. It contains the memory block size (uwBlkSize), number of memory blocks (uwBlkNum), number of allocated memory blocks (uwBlkCnt), and free list (stFreeList). Memory is allocated and released by block size. Each memory block contains the pointer pstNext that points to the next memory block.
Figure 1 Static memory
Development Guidelines
When to Use
Use static memory allocation to obtain memory blocks of the fixed size. When the memory is no longer required, release the static memory.
Available APIs
The following table describes APIs available for OpenHarmony LiteOS-M static memory management. For more details about the APIs, see the API reference.
Table 1 APIs of the static memory module
NOTE
The number of memory blocks in the memory pool after initialization is not equal to the total memory size divided by the memory block size. The reason is the control block of the memory pool and the control header of each memory block have memory overheads. When setting the total memory size, you need to consider these factors.
How to Develop
The typical development process of static memory is as follows:
Plan a memory space as the static memory pool.
Call the LOS_MemboxInit API to initialize the static memory pool.
During initialization, the memory space specified by the input parameter is divided into multiple blocks (the number of blocks depends on the total static memory size and the block size). Insert all memory blocks to the free list, and place the control header at the beginning of the memory.
Call the LOS_MemboxAlloc API to allocate static memory.
The system allocates the first free memory block from the free list and returns the start address of this memory block.
Call the LOS_MemboxClr API.
Clear the memory block corresponding to the address contained in the input parameter.
Call the LOS_MemboxFree API.
Add the memory block to the free list.
Development Example
This example implements the following:
Initialize a static memory pool.
Allocate a memory block from the static memory pool.
Store a piece of data in a memory block.
Print the data in the memory block.
Clear the data in the memory block.
Release the memory block.
The sample code is as follows:
#include "los_membox.h"
VOID Example_StaticMem(VOID)
{
UINT32 *mem = NULL;
UINT32 blkSize = 10;
UINT32 boxSize = 100;
UINT32 boxMem[1000];
UINT32 ret;
/* Initialize the memory pool.*/
ret = LOS_MemboxInit(&boxMem[0], boxSize, blkSize);
if(ret != LOS_OK) {
printf("Membox init failed!\n");
return;
} else {
printf("Membox init success!\n");
}
/* Request a memory block.*/
mem = (UINT32 *)LOS_MemboxAlloc(boxMem);
if (NULL == mem) {
printf("Mem alloc failed!\n");
return;
}
printf("Mem alloc success!\n");
/* Assign a value.*/
*mem = 828;
printf("*mem = %d\n", *mem);
/* Clear data in the memory block. */
LOS_MemboxClr(boxMem, mem);
printf("Mem clear success \n *mem = %d\n", *mem);
/* Release the memory.*/
ret = LOS_MemboxFree(boxMem, mem);
if (LOS_OK == ret) {
printf("Mem free success!\n");
} else {
printf("Mem free failed!\n");
}
return;
}
Verification
The output is as follows:
Membox init success!
Mem alloc success!
*mem = 828
Mem clear success
*mem = 0
Mem free success!
你可能感兴趣的文章
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦