更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Static memory
Static memory 的协作图:

结构体

struct  tagMEMBOX_NODE
 
struct  LOS_MEMBOX_INFO
 

类型定义

typedef struct tagMEMBOX_NODE LOS_MEMBOX_NODE
 

函数

UINT32 LOS_MemboxInit (VOID *pool, UINT32 poolSize, UINT32 blkSize)
 Initialize a memory pool. 更多...
 
VOID * LOS_MemboxAlloc (VOID *pool)
 Request a memory block. 更多...
 
UINT32 LOS_MemboxFree (VOID *pool, VOID *box)
 Free a memory block. 更多...
 
VOID LOS_MemboxClr (VOID *pool, VOID *box)
 Clear a memory block. 更多...
 
VOID LOS_ShowBox (VOID *pool)
 show membox info. 更多...
 
UINT32 LOS_MemboxStatisticsGet (const VOID *boxMem, UINT32 *maxBlk, UINT32 *blkCnt, UINT32 *blkSize)
 calculate membox information. 更多...
 

详细描述

类型定义说明

◆ LOS_MEMBOX_NODE

Structure of a free node in a memory pool

函数说明

◆ LOS_MemboxAlloc()

VOID * LOS_MemboxAlloc ( VOID *  pool)

Request a memory block.

Description:
  • This API is used to request a memory block.
注意
  • The input pool parameter must be initialized via func LOS_MemboxInit.
参数
pool[IN] Memory pool address.
返回值
#VOID*The request is accepted, and return a memory block address.
#NULLThe request fails.
Dependency:
  • los_membox.h: the header file that contains the API declaration.
参见
LOS_MemboxFree

Request a memory block.

在文件 los_membox.c150 行定义.

151{
152 LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
153 LOS_MEMBOX_NODE *node = NULL;
154 LOS_MEMBOX_NODE *nodeTmp = NULL;
155 UINT32 intSave;
156
157 if (pool == NULL) {
158 return NULL;
159 }
160
161 MEMBOX_LOCK(intSave);
162 node = &(boxInfo->stFreeList);//拿到空闲单链表
163 if (node->pstNext != NULL) {//不需要遍历链表,因为这是空闲链表
164 nodeTmp = node->pstNext;//先记录要使用的节点
165 node->pstNext = nodeTmp->pstNext;//不再空闲了,把节点摘出去了.
166 OS_MEMBOX_SET_MAGIC(nodeTmp);//为已使用的节块设置魔法数字
167 boxInfo->uwBlkCnt++;//已使用块数增加
168 }
169 MEMBOX_UNLOCK(intSave);
170
171 return (nodeTmp == NULL) ? NULL : OS_MEMBOX_USER_ADDR(nodeTmp);//返回可用的虚拟地址
172}
unsigned int UINT32
Definition: los_typedef.h:57
UINT32 uwBlkCnt
Definition: los_membox.h:67
LOS_MEMBOX_NODE stFreeList
Definition: los_membox.h:68
struct tagMEMBOX_NODE * pstNext
Definition: los_membox.h:57
这是这个函数的调用关系图:

◆ LOS_MemboxClr()

VOID LOS_MemboxClr ( VOID *  pool,
VOID *  box 
)

Clear a memory block.

Description:
  • This API is used to set the memory block value to be 0.
注意
  • The input pool parameter must be initialized via func LOS_MemboxInit.
  • The input box parameter must be allocated by LOS_MemboxAlloc.
参数
pool[IN] Memory pool address.
box[IN] Memory block address.
返回值
VOID
Dependency:
  • los_membox.h: the header file that contains the API declaration.
参见
None.

Clear a memory block.

在文件 los_membox.c201 行定义.

202{
203 LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
204
205 if ((pool == NULL) || (box == NULL)) {
206 return;
207 }
208 //将魔法数字一并清除了.
209 (VOID)memset_s(box, (boxInfo->uwBlkSize - OS_MEMBOX_NODE_HEAD_SIZE), 0,
210 (boxInfo->uwBlkSize - OS_MEMBOX_NODE_HEAD_SIZE));
211}
UINT32 uwBlkSize
Definition: los_membox.h:65

◆ LOS_MemboxFree()

UINT32 LOS_MemboxFree ( VOID *  pool,
VOID *  box 
)

Free a memory block.

Description:
  • This API is used to free a memory block.
注意
  • The input pool parameter must be initialized via func LOS_MemboxInit.
  • The input box parameter must be allocated by LOS_MemboxAlloc.
参数
pool[IN] Memory pool address.
box[IN] Memory block address.
返回值
#LOS_NOKThis memory block fails to be freed.
#LOS_OKThis memory block is successfully freed.
Dependency:
  • los_membox.h: the header file that contains the API declaration.
参见
LOS_MemboxAlloc

Free a memory block.

在文件 los_membox.c174 行定义.

175{
176 LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
177 UINT32 ret = LOS_NOK;
178 UINT32 intSave;
179
180 if ((pool == NULL) || (box == NULL)) {
181 return LOS_NOK;
182 }
183
184 MEMBOX_LOCK(intSave);
185 do {
186 LOS_MEMBOX_NODE *node = OS_MEMBOX_NODE_ADDR(box);//通过节体获取节块首地址
187 if (OsCheckBoxMem(boxInfo, node) != LOS_OK) {
188 break;
189 }
190
191 node->pstNext = boxInfo->stFreeList.pstNext;//节块指向空闲链表表头
192 boxInfo->stFreeList.pstNext = node;//空闲链表表头反指向它,意味节块排到第一,下次申请将首个分配它
193 boxInfo->uwBlkCnt--;//已经使用的内存块减一
194 ret = LOS_OK;
195 } while (0);//将被编译时优化
196 MEMBOX_UNLOCK(intSave);
197
198 return ret;
199}
STATIC INLINE UINT32 OsCheckBoxMem(const LOS_MEMBOX_INFO *boxInfo, const VOID *node)
检查静态内存块
Definition: los_membox.c:86
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemboxInit()

UINT32 LOS_MemboxInit ( VOID *  pool,
UINT32  poolSize,
UINT32  blkSize 
)

Initialize a memory pool.

Description:
  • This API is used to initialize a memory pool.
注意
  • The poolSize parameter value should match the following two conditions : 1) Be less than or equal to the Memory pool size; 2) Be greater than the size of LOS_MEMBOX_INFO.
参数
pool[IN] Memory pool address.
poolSize[IN] Memory pool size.
blkSize[IN] Memory block size.
返回值
#LOS_NOKThe memory pool fails to be initialized.
#LOS_OKThe memory pool is successfully initialized.
Dependency:
  • los_membox.h: the header file that contains the API declaration.
参见
None.

Initialize a memory pool.

在文件 los_membox.c106 行定义.

107{
108 LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;//在内存起始处安置池头
109 LOS_MEMBOX_NODE *node = NULL;
110 UINT32 index;
111 UINT32 intSave;
112
113 if (pool == NULL) {
114 return LOS_NOK;
115 }
116
117 if (blkSize == 0) {
118 return LOS_NOK;
119 }
120
121 if (poolSize < sizeof(LOS_MEMBOX_INFO)) {
122 return LOS_NOK;
123 }
124
125 MEMBOX_LOCK(intSave);
126 boxInfo->uwBlkSize = LOS_MEMBOX_ALIGNED(blkSize + OS_MEMBOX_NODE_HEAD_SIZE); //节块总大小(节头+节体)
127 boxInfo->uwBlkNum = (poolSize - sizeof(LOS_MEMBOX_INFO)) / boxInfo->uwBlkSize;//总节块数量
128 boxInfo->uwBlkCnt = 0; //已分配的数量
129 if (boxInfo->uwBlkNum == 0) {//只有0块的情况
130 MEMBOX_UNLOCK(intSave);
131 return LOS_NOK;
132 }
133
134 node = (LOS_MEMBOX_NODE *)(boxInfo + 1);//去除池头,找到第一个节块位置
135
136 boxInfo->stFreeList.pstNext = node;//池头空闲链表指向第一个节块
137
138 for (index = 0; index < boxInfo->uwBlkNum - 1; ++index) {//切割节块,挂入空闲链表
139 node->pstNext = OS_MEMBOX_NEXT(node, boxInfo->uwBlkSize);//按块大小切割好,统一由pstNext指向
140 node = node->pstNext;//node存储了下一个节点的地址信息
141 }
142
143 node->pstNext = NULL;//最后一个为null
144
145 MEMBOX_UNLOCK(intSave);
146
147 return LOS_OK;
148}
UINT32 uwBlkNum
Definition: los_membox.h:66
这是这个函数的调用关系图:

◆ LOS_MemboxStatisticsGet()

UINT32 LOS_MemboxStatisticsGet ( const VOID *  boxMem,
UINT32 maxBlk,
UINT32 blkCnt,
UINT32 blkSize 
)

calculate membox information.

Description:
  • This API is used to calculate membox information.
注意
  • One parameter of this interface is a pointer, it should be a correct value, otherwise, the system may be abnormal.
参数
boxMem[IN] Type #VOID* Pointer to the calculate membox.
maxBlk[OUT] Type UINT32* Record membox max block.
blkCnt[OUT] Type UINT32* Record membox block count alreay allocated.
blkSize[OUT] Type UINT32* Record membox block size.
返回值
#LOS_OKThe heap status calculate success.
#LOS_NOKThe membox status calculate with some error.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
LOS_MemAlloc | LOS_MemRealloc | LOS_MemFree

calculate membox information.

在文件 los_membox.c241 行定义.

243{
244 if ((boxMem == NULL) || (maxBlk == NULL) || (blkCnt == NULL) || (blkSize == NULL)) {
245 return LOS_NOK;
246 }
247
248 *maxBlk = ((OS_MEMBOX_S *)boxMem)->uwBlkNum;
249 *blkCnt = ((OS_MEMBOX_S *)boxMem)->uwBlkCnt;
250 *blkSize = ((OS_MEMBOX_S *)boxMem)->uwBlkSize;
251
252 return LOS_OK;
253}

◆ LOS_ShowBox()

VOID LOS_ShowBox ( VOID *  pool)

show membox info.

Description:
  • This API is used to show the memory pool info.
注意
  • The input pool parameter must be initialized via func LOS_MemboxInit.
参数
pool[IN] Memory pool address.
返回值
VOID
Dependency:
  • los_membox.h: the header file that contains the API declaration.
参见
None.

打印指定静态内存池所有节点信息(打印等级是LOS_INFO_LEVEL),包括内存池起始地址、 内存块大小、总内存块数量、每个空闲内存块的起始地址、所有内存块的起始地址

在文件 los_membox.c214 行定义.

215{
216 UINT32 index;
217 UINT32 intSave;
218 LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
219 LOS_MEMBOX_NODE *node = NULL;
220
221 if (pool == NULL) {
222 return;
223 }
224 MEMBOX_LOCK(intSave);
225 PRINT_INFO("membox(%p,0x%x,0x%x):\r\n", pool, boxInfo->uwBlkSize, boxInfo->uwBlkNum);
226 PRINT_INFO("free node list:\r\n");
227
228 for (node = boxInfo->stFreeList.pstNext, index = 0; node != NULL;
229 node = node->pstNext, ++index) {
230 PRINT_INFO("(%u,%p)\r\n", index, node);
231 }
232
233 PRINT_INFO("all node list:\r\n");
234 node = (LOS_MEMBOX_NODE *)(boxInfo + 1);
235 for (index = 0; index < boxInfo->uwBlkNum; ++index, node = OS_MEMBOX_NEXT(node, boxInfo->uwBlkSize)) {
236 PRINT_INFO("(%u,%p,%p)\r\n", index, node, node->pstNext);
237 }
238 MEMBOX_UNLOCK(intSave);
239}