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

结构体

struct  LOS_MEM_POOL_STATUS
 

函数

UINT32 LOS_MemDeInit (VOID *pool)
 Deinitialize dynamic memory. 更多...
 
UINT32 LOS_MemPoolList (VOID)
 Print infomation about all pools. 更多...
 
UINT32 LOS_MemInit (VOID *pool, UINT32 size)
 Initialize dynamic memory. 更多...
 
VOID LOS_MemExpandEnable (VOID *pool)
 Enable memory pool to support dynamic expansion. 更多...
 
VOID * LOS_MemAlloc (VOID *pool, UINT32 size)
 Allocate dynamic memory. 更多...
 
UINT32 LOS_MemFree (VOID *pool, VOID *ptr)
 Free dynamic memory. 更多...
 
VOID * LOS_MemRealloc (VOID *pool, VOID *ptr, UINT32 size)
 Re-allocate a memory block. 更多...
 
VOID * LOS_MemAllocAlign (VOID *pool, UINT32 size, UINT32 boundary)
 Allocate aligned memory. 更多...
 
UINT32 LOS_MemPoolSizeGet (const VOID *pool)
 Get the size of memory pool's size. 更多...
 
UINT32 LOS_MemTotalUsedGet (VOID *pool)
 Get the size of memory totally used. 更多...
 
UINT32 LOS_MemInfoGet (VOID *pool, LOS_MEM_POOL_STATUS *poolStatus)
 Get the infomation of memory pool. 更多...
 
UINT32 LOS_MemFreeNodeShow (VOID *pool)
 Get the number of free node in every size. 更多...
 
UINT32 LOS_MemIntegrityCheck (const VOID *pool)
 Check the memory pool integrity. 更多...
 

变量

UINT8m_aucSysMem0
 异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。 更多...
 
UINT8m_aucSysMem1
 系统动态内存池地址的起始地址 @note_thinking 能否不要用 0,1来命名核心变量 ??? 更多...
 

详细描述

函数说明

◆ LOS_MemAlloc()

VOID * LOS_MemAlloc ( VOID *  pool,
UINT32  size 
)

Allocate dynamic memory.

Description:
  • This API is used to allocate a memory block of which the size is specified.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
  • The size of the input parameter size can not be greater than the memory pool size that specified at the second input parameter of LOS_MemInit.
  • The size of the input parameter size must be four byte-aligned.
参数
pool[IN] Pointer to the memory pool that contains the memory block to be allocated.
size[IN] Size of the memory block to be allocated (unit: byte).
返回值
#NULLThe memory fails to be allocated.
#VOID*The memory is successfully allocated with the starting address of the allocated memory block returned.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
LOS_MemRealloc | LOS_MemAllocAlign | LOS_MemFree

Allocate dynamic memory.

在文件 los_memory.c1123 行定义.

1124{
1125 if ((pool == NULL) || (size == 0)) {//没提供内存池时
1126 return (size > 0) ? OsVmBootMemAlloc(size) : NULL;
1127 }
1128
1129 if (size < OS_MEM_MIN_ALLOC_SIZE) {
1130 size = OS_MEM_MIN_ALLOC_SIZE;
1131 }
1132
1133 struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
1134 VOID *ptr = NULL;
1135 UINT32 intSave;
1136
1137 do {
1138 if (OS_MEM_NODE_GET_USED_FLAG(size) || OS_MEM_NODE_GET_ALIGNED_FLAG(size)) {
1139 break;
1140 }
1141 MEM_LOCK(poolHead, intSave);
1142 ptr = OsMemAlloc(poolHead, size, intSave);//真正的分配内存函数,详细查看 鸿蒙内核源码分析(内存池篇)
1143 MEM_UNLOCK(poolHead, intSave);
1144 } while (0);
1145
1146 OsHookCall(LOS_HOOK_TYPE_MEM_ALLOC, pool, ptr, size);//打印日志,到此一游
1147 return ptr;
1148}
STATIC INLINE VOID * OsMemAlloc(struct OsMemPoolHead *pool, UINT32 size, UINT32 intSave)
从指定动态内存池中申请size长度的内存
Definition: los_memory.c:1075
unsigned int UINT32
Definition: los_typedef.h:57
VOID * OsVmBootMemAlloc(size_t len)
Definition: los_vm_boot.c:50
内存池头信息
Definition: los_memory.c:204
函数调用图:

◆ LOS_MemAllocAlign()

VOID * LOS_MemAllocAlign ( VOID *  pool,
UINT32  size,
UINT32  boundary 
)

Allocate aligned memory.

Description:
  • This API is used to allocate memory blocks of specified size and of which the starting addresses are aligned on a specified boundary.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
  • The size of the input parameter size can not be greater than the memory pool size that specified at the second input parameter of LOS_MemInit.
  • The alignment parameter value must be a power of 2 with the minimum value being 4.
参数
pool[IN] Pointer to the memory pool that contains the memory blocks to be allocated.
size[IN] Size of the memory to be allocated.
boundary[IN] Boundary on which the memory is aligned.
返回值
#NULLThe memory fails to be allocated.
#VOID*The memory is successfully allocated with the starting address of the allocated memory returned.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
LOS_MemAlloc | LOS_MemRealloc | LOS_MemFree

Allocate aligned memory.

在文件 los_memory.c1150 行定义.

1151{
1152 UINT32 gapSize;
1153
1154 if ((pool == NULL) || (size == 0) || (boundary == 0) || !OS_MEM_IS_POW_TWO(boundary) ||
1155 !OS_MEM_IS_ALIGNED(boundary, sizeof(VOID *))) {
1156 return NULL;
1157 }
1158
1159 if (size < OS_MEM_MIN_ALLOC_SIZE) {
1160 size = OS_MEM_MIN_ALLOC_SIZE;
1161 }
1162
1163 /*
1164 * sizeof(gapSize) bytes stores offset between alignedPtr and ptr,
1165 * the ptr has been OS_MEM_ALIGN_SIZE(4 or 8) aligned, so maximum
1166 * offset between alignedPtr and ptr is boundary - OS_MEM_ALIGN_SIZE
1167 */
1168 if ((boundary - sizeof(gapSize)) > ((UINT32)(-1) - size)) {
1169 return NULL;
1170 }
1171
1172 UINT32 useSize = (size + boundary) - sizeof(gapSize);
1173 if (OS_MEM_NODE_GET_USED_FLAG(useSize) || OS_MEM_NODE_GET_ALIGNED_FLAG(useSize)) {
1174 return NULL;
1175 }
1176
1177 struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
1178 UINT32 intSave;
1179 VOID *ptr = NULL;
1180 VOID *alignedPtr = NULL;
1181
1182 do {
1183 MEM_LOCK(poolHead, intSave);
1184 ptr = OsMemAlloc(pool, useSize, intSave);
1185 MEM_UNLOCK(poolHead, intSave);
1186 alignedPtr = (VOID *)OS_MEM_ALIGN(ptr, boundary);
1187 if (ptr == alignedPtr) {
1188#ifdef LOSCFG_KERNEL_LMS
1189 OsLmsAllocAlignMark(ptr, alignedPtr, size);
1190#endif
1191 break;
1192 }
1193
1194 /* store gapSize in address (ptr - 4), it will be checked while free */
1195 gapSize = (UINT32)((UINTPTR)alignedPtr - (UINTPTR)ptr);
1196 struct OsMemUsedNodeHead *allocNode = (struct OsMemUsedNodeHead *)ptr - 1;
1197 OS_MEM_NODE_SET_ALIGNED_FLAG(allocNode->header.sizeAndFlag);
1198 OS_MEM_NODE_SET_ALIGNED_FLAG(gapSize);
1199 *(UINT32 *)((UINTPTR)alignedPtr - sizeof(gapSize)) = gapSize;
1200#ifdef LOSCFG_KERNEL_LMS
1201 OsLmsAllocAlignMark(ptr, alignedPtr, size);
1202#endif
1203 ptr = alignedPtr;
1204 } while (0);
1205
1206 OsHookCall(LOS_HOOK_TYPE_MEM_ALLOCALIGN, pool, ptr, size, boundary);//打印对齐日志,表示程序曾临幸过此处
1207 return ptr;
1208}
STATIC INLINE VOID OsLmsAllocAlignMark(VOID *ptr, VOID *alignedPtr, UINT32 size)
Definition: los_memory.c:525
unsigned long UINTPTR
Definition: los_typedef.h:68
UINT32 sizeAndFlag
节点总大小+标签
Definition: los_memory.c:178
已使用内存池节点
Definition: los_memory.c:181
struct OsMemNodeHead header
已被使用节点
Definition: los_memory.c:182
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemDeInit()

UINT32 LOS_MemDeInit ( VOID *  pool)

Deinitialize dynamic memory.

Description:
  • This API is used to deinitialize the dynamic memory of a doubly linked list.
参数
pool[IN] Starting address of memory.
返回值
#OS_ERRORThe dynamic memory fails to be deinitialized.
#LOS_OKThe dynamic memory is successfully deinitialized.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Deinitialize dynamic memory.

在文件 los_memory.c1045 行定义.

1046{
1047 if (pool == NULL) {
1048 return OS_ERROR;
1049 }
1050
1051 if (OsMemPoolDelete(pool)) {
1052 return OS_ERROR;
1053 }
1054
1055 OsMemPoolDeinit(pool);
1056
1057 OsHookCall(LOS_HOOK_TYPE_MEM_DEINIT, pool);
1058 return LOS_OK;
1059}
STATIC UINT32 OsMemPoolDelete(VOID *pool)
删除内存池
Definition: los_memory.c:982
STATIC VOID OsMemPoolDeinit(VOID *pool)
Definition: los_memory.c:949
函数调用图:

◆ LOS_MemExpandEnable()

VOID LOS_MemExpandEnable ( VOID *  pool)

Enable memory pool to support dynamic expansion.

Description:
  • This API is used to enable the dynamic memory to expand size dynamically.
注意
  • The memory pool is default diabled dynamic expansion.
参数
pool[IN] Starting address of memory.
返回值
node.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

在文件 los_memory.c500 行定义.

501{
502 if (pool == NULL) {
503 return;
504 }
505
506 ((struct OsMemPoolHead *)pool)->info.attr |= OS_MEM_POOL_EXPAND_ENABLE;
507}
这是这个函数的调用关系图:

◆ LOS_MemFree()

UINT32 LOS_MemFree ( VOID *  pool,
VOID *  ptr 
)

Free dynamic memory.

Description:
This API is used to free specified dynamic memory that has been allocated.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
  • The input ptr parameter must be allocated by LOS_MemAlloc or LOS_MemAllocAlign or LOS_MemRealloc.
参数
pool[IN] Pointer to the memory pool that contains the dynamic memory block to be freed.
ptr[IN] Starting address of the memory block to be freed.
返回值
#LOS_NOKThe memory block fails to be freed because the starting address of the memory block is invalid, or the memory overwriting occurs.
#LOS_OKThe memory block is successfully freed.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
LOS_MemAlloc | LOS_MemRealloc | LOS_MemAllocAlign

Free dynamic memory.

在文件 los_memory.c1369 行定义.

1370{
1371 UINT32 intSave;
1372 UINT32 ret = LOS_NOK;
1373
1374 if ((pool == NULL) || (ptr == NULL) || !OS_MEM_IS_ALIGNED(pool, sizeof(VOID *)) ||
1375 !OS_MEM_IS_ALIGNED(ptr, sizeof(VOID *))) {
1376 return ret;
1377 }
1378 OsHookCall(LOS_HOOK_TYPE_MEM_FREE, pool, ptr);
1379
1380 struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
1381 struct OsMemNodeHead *node = NULL;
1382
1383 do {
1384 UINT32 gapSize = *(UINT32 *)((UINTPTR)ptr - sizeof(UINT32));//获取节点大小和标签 即: sizeAndFlag
1385 if (OS_MEM_NODE_GET_ALIGNED_FLAG(gapSize) && OS_MEM_NODE_GET_USED_FLAG(gapSize)) {
1386 PRINT_ERR("[%s:%d]gapSize:0x%x error\n", __FUNCTION__, __LINE__, gapSize);
1387 break;
1388 }
1389
1390 node = (struct OsMemNodeHead *)((UINTPTR)ptr - OS_MEM_NODE_HEAD_SIZE);//定位到节点开始位置
1391
1392 if (OS_MEM_NODE_GET_ALIGNED_FLAG(gapSize)) {
1393 gapSize = OS_MEM_NODE_GET_ALIGNED_GAPSIZE(gapSize);
1394 if ((gapSize & (OS_MEM_ALIGN_SIZE - 1)) || (gapSize > ((UINTPTR)ptr - OS_MEM_NODE_HEAD_SIZE))) {
1395 PRINT_ERR("illegal gapSize: 0x%x\n", gapSize);
1396 break;
1397 }
1398 node = (struct OsMemNodeHead *)((UINTPTR)ptr - gapSize - OS_MEM_NODE_HEAD_SIZE);
1399 }
1400 MEM_LOCK(poolHead, intSave);
1401 ret = OsMemFree(poolHead, node);
1402 MEM_UNLOCK(poolHead, intSave);
1403 } while (0);
1404
1405 return ret;
1406}
STATIC INLINE UINT32 OsMemFree(struct OsMemPoolHead *pool, struct OsMemNodeHead *node)
释放内存
Definition: los_memory.c:1314
内存池节点
Definition: los_memory.c:169
union OsMemNodeHead::@5 ptr
函数调用图:

◆ LOS_MemFreeNodeShow()

UINT32 LOS_MemFreeNodeShow ( VOID *  pool)

Get the number of free node in every size.

Description:
  • This API is used to get the number of free node in every size.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
参数
pool[IN] A pointer pointed to the memory pool.
返回值
#LOS_NOKThe incoming parameter pool is NULL.
UINT32The address of the last used node that casts to UINT32.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Get the number of free node in every size.

在文件 los_memory.c2048 行定义.

2049{
2050 struct OsMemPoolHead *poolInfo = (struct OsMemPoolHead *)pool;
2051
2052 if ((poolInfo == NULL) || ((UINTPTR)pool != (UINTPTR)poolInfo->info.pool)) {
2053 PRINT_ERR("wrong mem pool addr: %#x, line:%d\n", poolInfo, __LINE__);
2054 return LOS_NOK;
2055 }
2056
2057 struct OsMemFreeNodeHead *node = NULL;
2058 UINT32 countNum[OS_MEM_FREE_LIST_COUNT] = {0};
2059 UINT32 index;
2060 UINT32 intSave;
2061
2062 MEM_LOCK(poolInfo, intSave);
2063 for (index = 0; index < OS_MEM_FREE_LIST_COUNT; index++) {
2064 node = poolInfo->freeList[index];
2065 while (node) {
2066 node = node->next;
2067 countNum[index]++;
2068 }
2069 }
2070 MEM_UNLOCK(poolInfo, intSave);
2071
2072 PRINTK("\n ************************ left free node number**********************\n");
2073 for (index = 0; index < OS_MEM_FREE_LIST_COUNT; index++) {
2074 if (countNum[index] == 0) {
2075 continue;
2076 }
2077
2078 PRINTK("free index: %03u, ", index);
2079 if (index < OS_MEM_SMALL_BUCKET_COUNT) {
2080 PRINTK("size: [%#x], num: %u\n", (index + 1) << 2, countNum[index]); /* 2: setup is 4. */
2081 } else {
2082 UINT32 val = 1 << (((index - OS_MEM_SMALL_BUCKET_COUNT) >> OS_MEM_SLI) + OS_MEM_LARGE_START_BUCKET);
2083 UINT32 offset = val >> OS_MEM_SLI;
2084 PRINTK("size: [%#x, %#x], num: %u\n",
2085 (offset * ((index - OS_MEM_SMALL_BUCKET_COUNT) % (1 << OS_MEM_SLI))) + val,
2086 ((offset * (((index - OS_MEM_SMALL_BUCKET_COUNT) % (1 << OS_MEM_SLI)) + 1)) + val - 1),
2087 countNum[index]);
2088 }
2089 }
2090 PRINTK("\n ********************************************************************\n\n");
2091
2092 return LOS_OK;
2093}
内存池空闲节点
Definition: los_memory.c:188
struct OsMemFreeNodeHead * next
后一个空闲后继节点
Definition: los_memory.c:191
struct OsMemFreeNodeHead * freeList[OS_MEM_FREE_LIST_COUNT]
空闲节点链表 32 + 24 * 8 = 224
Definition: los_memory.c:207
struct OsMemPoolInfo info
记录内存池的信息
Definition: los_memory.c:205
VOID * pool
指向内存块基地址,仅做记录而已,真正的分配内存跟它没啥关系
Definition: los_memory.c:195

◆ LOS_MemInfoGet()

UINT32 LOS_MemInfoGet ( VOID *  pool,
LOS_MEM_POOL_STATUS poolStatus 
)

Get the infomation of memory pool.

Description:
  • This API is used to get the infomation of memory pool.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
参数
pool[IN] A pointer pointed to the memory pool.
poolStatus[IN] A pointer for storage the pool status
返回值
#LOS_NOKThe incoming parameter pool is NULL or invalid.
#LOS_OKSuccess to get memory infomation.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Get the infomation of memory pool.

参数
pool
poolStatus
返回
参见

在文件 los_memory.c1965 行定义.

1966{//内存碎片率计算:同样调用LOS_MemInfoGet接口,可以获取内存池的剩余内存大小和最大空闲内存块大小,然后根据公式(fragment=100-最大空闲内存块大小/剩余内存大小)得出此时的动态内存池碎片率。
1967 struct OsMemPoolHead *poolInfo = pool;
1968
1969 if (poolStatus == NULL) {
1970 PRINT_ERR("can't use NULL addr to save info\n");
1971 return LOS_NOK;
1972 }
1973
1974 if ((pool == NULL) || (poolInfo->info.pool != pool)) {
1975 PRINT_ERR("wrong mem pool addr: %#x, line:%d\n", poolInfo, __LINE__);
1976 return LOS_NOK;
1977 }
1978
1979 (VOID)memset(poolStatus, 0, sizeof(LOS_MEM_POOL_STATUS));
1980
1981 struct OsMemNodeHead *tmpNode = NULL;
1982 struct OsMemNodeHead *endNode = NULL;
1983 UINT32 intSave;
1984
1985 MEM_LOCK(poolInfo, intSave);
1986 endNode = OS_MEM_END_NODE(pool, poolInfo->info.totalSize);
1987#if OS_MEM_EXPAND_ENABLE
1988 UINT32 size;
1989 for (tmpNode = OS_MEM_FIRST_NODE(pool); tmpNode <= endNode; tmpNode = OS_MEM_NEXT_NODE(tmpNode)) {
1990 if (tmpNode == endNode) {
1991 poolStatus->totalUsedSize += OS_MEM_NODE_HEAD_SIZE;
1992 poolStatus->usedNodeNum++;
1993 if (OsMemIsLastSentinelNode(endNode) == FALSE) {
1994 size = OS_MEM_NODE_GET_SIZE(endNode->sizeAndFlag);
1995 tmpNode = OsMemSentinelNodeGet(endNode);
1996 endNode = OS_MEM_END_NODE(tmpNode, size);
1997 continue;
1998 } else {
1999 break;
2000 }
2001 } else {
2002 OsMemInfoGet(poolInfo, tmpNode, poolStatus);
2003 }
2004 }
2005#else
2006 for (tmpNode = OS_MEM_FIRST_NODE(pool); tmpNode < endNode; tmpNode = OS_MEM_NEXT_NODE(tmpNode)) {
2007 OsMemInfoGet(poolInfo, tmpNode, poolStatus);
2008 }
2009#endif
2010#ifdef LOSCFG_MEM_WATERLINE
2011 poolStatus->usageWaterLine = poolInfo->info.waterLine;
2012#endif
2013 MEM_UNLOCK(poolInfo, intSave);
2014
2015 return LOS_OK;
2016}
void * memset(void *addr, int c, size_t len)
Definition: lms_libc.c:36
STATIC INLINE VOID OsMemInfoGet(struct OsMemPoolHead *poolInfo, struct OsMemNodeHead *node, LOS_MEM_POOL_STATUS *poolStatus)
Definition: los_memory.c:1926
STATIC INLINE BOOL OsMemIsLastSentinelNode(struct OsMemNodeHead *sentinelNode)
是否为最后一个哨兵节点
Definition: los_memory.c:327
STATIC INLINE VOID * OsMemSentinelNodeGet(struct OsMemNodeHead *node)
Definition: los_memory.c:354
UINT32 totalSize
总大小,确定了内存池的边界
Definition: los_memory.c:196
UINT32 waterLine
Definition: los_memory.c:199
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemInit()

UINT32 LOS_MemInit ( VOID *  pool,
UINT32  size 
)

Initialize dynamic memory.

Description:
  • This API is used to initialize the dynamic memory of a doubly linked list.
注意
  • The size 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 OS_MEM_MIN_POOL_SIZE.
  • Call this API when dynamic memory needs to be initialized during the startup of Huawei LiteOS.
  • The parameter input must be four byte-aligned.
  • The init area [pool, pool + size] should not conflict with other pools.
参数
pool[IN] Starting address of memory.
size[IN] Memory size.
返回值
#OS_ERRORThe dynamic memory fails to be initialized.
#LOS_OKThe dynamic memory is successfully initialized.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Initialize dynamic memory.

参数
pool
size
返回
注意
EndNode作为内存池末尾的节点,size为0。
参见

在文件 los_memory.c1021 行定义.

1022{
1023 if ((pool == NULL) || (size <= OS_MEM_MIN_POOL_SIZE)) {
1024 return OS_ERROR;
1025 }
1026
1027 size = OS_MEM_ALIGN(size, OS_MEM_ALIGN_SIZE);//4个字节对齐
1028 if (OsMemPoolInit(pool, size)) {
1029 return OS_ERROR;
1030 }
1031
1032#ifdef LOSCFG_MEM_MUL_POOL //多内存池开关
1033 if (OsMemPoolAdd(pool, size)) {
1034 (VOID)OsMemPoolDeinit(pool);
1035 return OS_ERROR;
1036 }
1037#endif
1038
1039 OsHookCall(LOS_HOOK_TYPE_MEM_INIT, pool, size);//打印日志
1040 return LOS_OK;
1041}
STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
OsMemPoolInit 内存池初始化 内存池节点部分包含3种类型节点:未使用空闲内存节点(OsMemFreeNodeHead),已使用内存节点(OsMemUsedNodeHead) 和 尾节点(Os...
Definition: los_memory.c:894
STATIC UINT32 OsMemPoolAdd(VOID *pool, UINT32 size)
新增内存池
Definition: los_memory.c:954
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemIntegrityCheck()

UINT32 LOS_MemIntegrityCheck ( const VOID *  pool)

Check the memory pool integrity.

Description:
  • This API is used to check the memory pool integrity.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
  • LOS_MemIntegrityCheck will be called by malloc function when the macro of LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK is defined in LiteOS.
  • LOS_MemIntegrityCheck function can be called by user anytime.
参数
pool[IN] A pointer pointed to the memory pool.
返回值
#LOS_NOKThe memory pool (pool) is impaired.
#LOS_OKThe memory pool (pool) is integrated.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Check the memory pool integrity.

在文件 los_memory.c1903 行定义.

1904{
1905 if (pool == NULL) {
1906 return LOS_NOK;
1907 }
1908
1909 struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
1910 struct OsMemNodeHead *tmpNode = NULL;
1911 struct OsMemNodeHead *preNode = NULL;
1912 UINT32 intSave = 0;
1913
1914 MEM_LOCK(poolHead, intSave);
1915 if (OsMemIntegrityCheck(poolHead, &tmpNode, &preNode)) {
1916 goto ERROR_OUT;
1917 }
1918 MEM_UNLOCK(poolHead, intSave);
1919 return LOS_OK;
1920
1921ERROR_OUT:
1922 OsMemIntegrityCheckError(poolHead, tmpNode, preNode, intSave);
1923 return LOS_NOK;
1924}
STATIC UINT32 OsMemIntegrityCheck(const struct OsMemPoolHead *pool, struct OsMemNodeHead **tmpNode, struct OsMemNodeHead **preNode)
Definition: los_memory.c:1778
STATIC VOID OsMemIntegrityCheckError(struct OsMemPoolHead *pool, const struct OsMemNodeHead *tmpNode, const struct OsMemNodeHead *preNode, UINT32 intSave)
Definition: los_memory.c:1853
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemPoolList()

UINT32 LOS_MemPoolList ( VOID  )

Print infomation about all pools.

Description:
  • This API is used to print infomation about all pools.
返回值
UINT32The pool number.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Print infomation about all pools.

在文件 los_memory.c1061 行定义.

1062{
1063 VOID *nextPool = g_poolHead;
1064 UINT32 index = 0;
1065 while (nextPool != NULL) {
1066 PRINTK("pool%u :\n", index);
1067 index++;
1068 OsMemInfoPrint(nextPool);
1069 nextPool = ((struct OsMemPoolHead *)nextPool)->nextPool;
1070 }
1071 return index;
1072}
VOID * g_poolHead
Definition: los_memory.c:111
STATIC VOID OsMemInfoPrint(VOID *pool)
Definition: los_memory.c:2018
VOID * nextPool
指向下一个内存池 OsMemPoolHead 类型
Definition: los_memory.c:210
函数调用图:

◆ LOS_MemPoolSizeGet()

UINT32 LOS_MemPoolSizeGet ( const VOID *  pool)

Get the size of memory pool's size.

Description:
  • This API is used to get the size of memory pool' total size.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
参数
pool[IN] A pointer pointed to the memory pool.
返回值
#LOS_NOKThe incoming parameter pool is NULL.
UINT32The size of the memory pool.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Get the size of memory pool's size.

在文件 los_memory.c1603 行定义.

1604{
1605 UINT32 count = 0;
1606
1607 if (pool == NULL) {
1608 return LOS_NOK;
1609 }
1610
1611 count += ((struct OsMemPoolHead *)pool)->info.totalSize; // 这里的 += 好像没必要吧?, = 就可以了, @note_thinking
1612
1613#if OS_MEM_EXPAND_ENABLE //支持扩展
1614 UINT32 size;
1615 struct OsMemNodeHead *node = NULL;
1616 struct OsMemNodeHead *sentinel = OS_MEM_END_NODE(pool, count);//获取哨兵节点
1617
1618 while (OsMemIsLastSentinelNode(sentinel) == FALSE) {//不是最后一个节点
1619 size = OS_MEM_NODE_GET_SIZE(sentinel->sizeAndFlag);//数据域大小
1620 node = OsMemSentinelNodeGet(sentinel);//再获取哨兵节点
1621 sentinel = OS_MEM_END_NODE(node, size);//获取尾节点
1622 count += size; //内存池大小变大
1623 }
1624#endif
1625 return count;
1626}
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemRealloc()

VOID * LOS_MemRealloc ( VOID *  pool,
VOID *  ptr,
UINT32  size 
)

Re-allocate a memory block.

Description:
  • This API is used to allocate a new memory block of which the size is specified by size if the original memory block size is insufficient. The new memory block will copy the data in the original memory block of which the address is specified by ptr. The size of the new memory block determines the maximum size of data to be copied. After the new memory block is created, the original one is freed.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
  • The input ptr parameter must be allocated by LOS_MemAlloc or LOS_MemAllocAlign.
  • The size of the input parameter size can not be greater than the memory pool size that specified at the second input parameter of LOS_MemInit.
  • The size of the input parameter size must be aligned as follows: 1) if the ptr is allocated by LOS_MemAlloc, it must be four byte-aligned; 2) if the ptr is allocated by LOS_MemAllocAlign, it must be aligned with the size of the input parameter boundary of LOS_MemAllocAlign.
参数
pool[IN] Pointer to the memory pool that contains the original and new memory blocks.
ptr[IN] Address of the original memory block.
size[IN] Size of the new memory block.
返回值
#NULLThe memory fails to be re-allocated.
#VOID*The memory is successfully re-allocated with the starting address of the new memory block returned.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
LOS_MemAlloc | LOS_MemAllocAlign | LOS_MemFree

Re-allocate a memory block.

在文件 los_memory.c1510 行定义.

1511{
1512 if ((pool == NULL) || OS_MEM_NODE_GET_USED_FLAG(size) || OS_MEM_NODE_GET_ALIGNED_FLAG(size)) {
1513 return NULL;
1514 }
1515 OsHookCall(LOS_HOOK_TYPE_MEM_REALLOC, pool, ptr, size);
1516 if (size < OS_MEM_MIN_ALLOC_SIZE) {
1517 size = OS_MEM_MIN_ALLOC_SIZE;
1518 }
1519
1520 if (ptr == NULL) {
1521 return LOS_MemAlloc(pool, size);
1522 }
1523
1524 if (size == 0) {
1525 (VOID)LOS_MemFree(pool, ptr);
1526 return NULL;
1527 }
1528
1529 struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
1530 struct OsMemNodeHead *node = NULL;
1531 VOID *newPtr = NULL;
1532 UINT32 intSave;
1533
1534 MEM_LOCK(poolHead, intSave);
1535 do {
1536 ptr = OsGetRealPtr(pool, ptr);
1537 if (ptr == NULL) {
1538 break;
1539 }
1540
1541 node = (struct OsMemNodeHead *)((UINTPTR)ptr - OS_MEM_NODE_HEAD_SIZE);
1542 if (OsMemCheckUsedNode(pool, node) != LOS_OK) {
1543 break;
1544 }
1545
1546 newPtr = OsMemRealloc(pool, ptr, node, size, intSave);
1547 } while (0);
1548 MEM_UNLOCK(poolHead, intSave);
1549
1550 return newPtr;
1551}
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
STATIC UINT32 OsMemCheckUsedNode(const struct OsMemPoolHead *pool, const struct OsMemNodeHead *node)
Definition: los_memory.c:1291
STATIC INLINE VOID * OsGetRealPtr(const VOID *pool, VOID *ptr)
Definition: los_memory.c:1456
STATIC INLINE VOID * OsMemRealloc(struct OsMemPoolHead *pool, const VOID *ptr, struct OsMemNodeHead *node, UINT32 size, UINT32 intSave)
Definition: los_memory.c:1477
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MemTotalUsedGet()

UINT32 LOS_MemTotalUsedGet ( VOID *  pool)

Get the size of memory totally used.

Description:
  • This API is used to get the size of memory totally used in memory pool.
注意
  • The input pool parameter must be initialized via func LOS_MemInit.
参数
pool[IN] A pointer pointed to the memory pool.
返回值
#LOS_NOKThe incoming parameter pool is NULL.
UINT32The size of the memory pool used.
Dependency:
  • los_memory.h: the header file that contains the API declaration.
参见
None.

Get the size of memory totally used.

在文件 los_memory.c1628 行定义.

1629{
1630 struct OsMemNodeHead *tmpNode = NULL;
1631 struct OsMemPoolHead *poolInfo = (struct OsMemPoolHead *)pool;
1632 struct OsMemNodeHead *endNode = NULL;
1633 UINT32 memUsed = 0;
1634 UINT32 intSave;
1635
1636 if (pool == NULL) {
1637 return LOS_NOK;
1638 }
1639
1640 MEM_LOCK(poolInfo, intSave);
1641 endNode = OS_MEM_END_NODE(pool, poolInfo->info.totalSize);
1642#if OS_MEM_EXPAND_ENABLE
1643 UINT32 size;
1644 for (tmpNode = OS_MEM_FIRST_NODE(pool); tmpNode <= endNode;) {
1645 if (tmpNode == endNode) {
1646 memUsed += OS_MEM_NODE_HEAD_SIZE;
1647 if (OsMemIsLastSentinelNode(endNode) == FALSE) {
1648 size = OS_MEM_NODE_GET_SIZE(endNode->sizeAndFlag);
1649 tmpNode = OsMemSentinelNodeGet(endNode);
1650 endNode = OS_MEM_END_NODE(tmpNode, size);
1651 continue;
1652 } else {
1653 break;
1654 }
1655 } else {
1656 if (OS_MEM_NODE_GET_USED_FLAG(tmpNode->sizeAndFlag)) {
1657 memUsed += OS_MEM_NODE_GET_SIZE(tmpNode->sizeAndFlag);
1658 }
1659 tmpNode = OS_MEM_NEXT_NODE(tmpNode);
1660 }
1661 }
1662#else
1663 for (tmpNode = OS_MEM_FIRST_NODE(pool); tmpNode < endNode;) {
1664 if (OS_MEM_NODE_GET_USED_FLAG(tmpNode->sizeAndFlag)) {
1665 memUsed += OS_MEM_NODE_GET_SIZE(tmpNode->sizeAndFlag);
1666 }
1667 tmpNode = OS_MEM_NEXT_NODE(tmpNode);
1668 }
1669#endif
1670 MEM_UNLOCK(poolInfo, intSave);
1671
1672 return memUsed;
1673}
函数调用图:
这是这个函数的调用关系图:

变量说明

◆ m_aucSysMem0

UINT8* m_aucSysMem0
extern

异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。

The start address of exc interaction dynamic memory pool address, when the exc interaction feature not support, m_aucSysMem0 equals to m_aucSysMem1.

在文件 los_memory.c107 行定义.

◆ m_aucSysMem1

UINT8* m_aucSysMem1
extern

系统动态内存池地址的起始地址 @note_thinking 能否不要用 0,1来命名核心变量 ???

The start address of system dynamic memory pool address.

在文件 los_memory.c108 行定义.