摘要:鴻蒙輕核心M核新增支援了多段非連續性記憶體區域,把多個非連續性記憶體邏輯上合一,使用者不感知底層的不同記憶體塊。

本文分享自華為雲社群《鴻蒙輕核心M核原始碼分析系列九 動態記憶體Dynamic Memory 補充》,作者:zhushy。

一些晶片片內RAM大小無法滿足要求,需要使用片外實體記憶體進行擴充。對於多段非連續性記憶體,需要記憶體管理模組統一管理,應用使用記憶體介面時不需要關注記憶體分配屬於哪塊實體記憶體,不感知多塊記憶體。

多段非連續性記憶體如下圖所示:

鴻蒙輕核心M核新增支援了多段非連續性記憶體區域,把多個非連續性記憶體邏輯上合一,使用者不感知底層的不同記憶體塊。本文來分析下動態記憶體模組的支援多段非連續記憶體的原始碼,幫助讀者掌握其使用。本文中所涉及的原始碼,以OpenHarmony LiteOS-M核心為例,均可以在開源站點https://gitee.com/openharmony/kernel_liteos_m 獲取。接下來,我們看下新增的結構體、巨集和對外介面的原始碼。

1、結構體定義和常用巨集定義

在檔案kernel/include/los_memory.h中新增了結構體LosMemRegion用於維護多個非連續的記憶體區域,包含各個記憶體區域的開始地址和大小。如下:

  1. typedef struct {
  2. VOID *startAddress; /* 記憶體區域的開始地址 */
  3. UINT32 length; /* 記憶體區域的長度 */
  4. } LosMemRegion;

需要注意這個結構體的定義需要開啟巨集LOSCFG_MEM_MUL_REGIONS的情況下才生效,這個巨集也是支援非連續記憶體區域的配置巨集,定義在檔案kernel/include/los_config.h中。

我們繼續看下新增的幾個巨集函式,定義在檔案kernel/src/mm/los_memory.c,程式碼下下文:

註釋講的比較明白,當開啟LOSCFG_MEM_MUL_REGIONS支援非連續記憶體特性時,會把兩個不連續記憶體區域之間的間隔Gap區域標記為虛擬的已使用記憶體節點。這個節點當然不能被釋放,在記憶體調測特性中也不能被統計。因為我們只是把它視為已使用記憶體節點,但其實不是。在動態記憶體演算法中每個記憶體節點都維護一個指向前序節點的指標,對於虛擬已使用節點,我們把該指標設定為魔術字,來標記它是個記憶體區域的間隔部分。

⑴處定義了一個魔術字OS_MEM_GAP_NODE_MAGIC,用於表示兩個不連續記憶體區域之前的間隔Gap區域。⑵和⑶處定義2個巨集,分別用於設定魔術字,驗證魔術字。

  1. #if (LOSCFG_MEM_MUL_REGIONS == 1)
  2. /**
  3. * When LOSCFG_MEM_MUL_REGIONS is enabled to support multiple non-continuous memory regions, the gap between two memory regions
  4. * is marked as a used OsMemNodeHead node. The gap node could not be freed, and would also be skipped in some DFX functions. The
  5. * 'ptr.prev' pointer of this node is set to OS_MEM_GAP_NODE_MAGIC to identify that this is a gap node.
  6. */
  7. #define OS_MEM_GAP_NODE_MAGIC 0xDCBAABCD
  8. #define OS_MEM_MARK_GAP_NODE(node) (((struct OsMemNodeHead *)(node))->ptr.prev = (struct OsMemNodeHead *)OS_MEM_GAP_NODE_MAGIC)
  9. #define OS_MEM_IS_GAP_NODE(node) (((struct OsMemNodeHead *)(node))->ptr.prev == (struct OsMemNodeHead *)OS_MEM_GAP_NODE_MAGIC)
  10. #else
  11. #define OS_MEM_MARK_GAP_NODE(node)
  12. #define OS_MEM_IS_GAP_NODE(node) FALSE
  13. #endif

2、動態記憶體常用操作

本節我們一起分析下非連續性記憶體的實現演算法,及介面實現程式碼。首先通過示意圖瞭解下演算法:

集合示意圖,我們瞭解下非連續性記憶體合併為一個記憶體池的步驟:

  • 1、把多段記憶體區域的第一塊記憶體區域呼叫LOS_MemInit進行初始化
  • 2、獲取下一個記憶體區域的開始地址和長度,計算該記憶體區域和上一塊記憶體區域的間隔大小gapSize。
  • 3、把記憶體塊間隔部分視為虛擬的已使用節點,使用上一記憶體塊的尾節點,設定其大小為gapSize+ OS_MEM_NODE_HEAD_SIZE。
  • 4、把當前記憶體區域劃分為一個空閒記憶體塊和一個尾節點,把空閒記憶體塊插入到空閒連結串列。並設定各個節點的前後連結關係。
  • 5、有更多的非連續記憶體塊,重複上述步驟2-4。

2.1 新增介面LOS_MemRegionsAdd

新增的介面的介面說明文件見下文,註釋比較詳細,總結如下:

  • LOSCFG_MEM_MUL_REGIONS=0:

不支援多段非連續記憶體,相關程式碼不使能。

  • LOSCFG_MEM_MUL_REGIONS=1:

支援多段非連續記憶體,相關程式碼使能。使用者配置多段記憶體區域,呼叫介面
LOS_MemRegionsAdd(VOID *pool, const LosMemRegion * const multipleMemRegions)進行記憶體池合一:

    • 如果pool為空,則合併到主記憶體堆m_aucSysMem0。
    • 如果不為空,則初始化一個新的記憶體池,合併多記憶體區域為一個從堆。
  1. /**
  2. * @ingroup los_memory
  3. * @brief Initialize multiple non-continuous memory regions.
  4. *
  5. * @par Description:
  6. * <ul>
  7. * <li>This API is used to initialize multiple non-continuous memory regions. If the starting address of a pool is specified,
  8. * the memory regions will be linked to the pool as free nodes. Otherwise, the first memory region will be initialized as a
  9. * new pool, and the rest regions will be linked as free nodes to the new pool.</li>
  10. * </ul>
  11. *
  12. * @attention
  13. * <ul>
  14. * <li>If the starting address of a memory pool is specified, the start address of the non-continuous memory regions should be
  15. * greater than the end address of the memory pool.</li>
  16. * <li>The multiple non-continuous memory regions shouldn't conflict with each other.</li>
  17. * </ul>
  18. *
  19. * @param pool [IN] The memory pool address. If NULL is specified, the start address of first memory region will be
  20. * initialized as the memory pool address. If not NULL, it should be a valid address of a memory pool.
  21. * @param memRegions [IN] The LosMemRegion array that contains multiple non-continuous memory regions. The start address
  22. * of the memory regions are placed in ascending order.
  23. * @param memRegionCount [IN] The count of non-continuous memory regions, and it should be the length of the LosMemRegion array.
  24. *
  25. * @retval #LOS_NOK The multiple non-continuous memory regions fails to be initialized.
  26. * @retval #LOS_OK The multiple non-continuous memory regions is initialized successfully.
  27. * @par Dependency:
  28. * <ul>
  29. * <li>los_memory.h: the header file that contains the API declaration.</li>
  30. * </ul>
  31. * @see None.
  32. */
  33. extern UINT32 LOS_MemRegionsAdd(VOID *pool, const LosMemRegion * const memRegions, UINT32 memRegionCount);

2.2 新增介面LOS_MemRegionsAdd實現

結合上文示意圖,加上註釋,實現比較清晰,直接閱讀下程式碼即可。

  1. #if (LOSCFG_MEM_MUL_REGIONS == 1)
  2. STATIC INLINE UINT32 OsMemMulRegionsParamCheck(VOID *pool, const LosMemRegion * const memRegions, UINT32 memRegionCount)
  3. {
  4. const LosMemRegion *memRegion = NULL;
  5. VOID *lastStartAddress = NULL;
  6. VOID *curStartAddress = NULL;
  7. UINT32 lastLength;
  8. UINT32 curLength;
  9. UINT32 regionCount;
  10.  
  11. if ((pool != NULL) && (((struct OsMemPoolHead *)pool)->info.pool != pool)) {
  12. PRINT_ERR("wrong mem pool addr: %p, func: %s, line: %d\n", pool, __FUNCTION__, __LINE__);
  13. return LOS_NOK;
  14. }
  15.  
  16. if (pool != NULL) {
  17. lastStartAddress = pool;
  18. lastLength = ((struct OsMemPoolHead *)pool)->info.totalSize;
  19. }
  20.  
  21. memRegion = memRegions;
  22. regionCount = 0;
  23. while (regionCount < memRegionCount) {
  24. curStartAddress = memRegion->startAddress;
  25. curLength = memRegion->length;
  26. if ((curStartAddress == NULL) || (curLength == 0)) {
  27. PRINT_ERR("Memory address or length configured wrongly:address:0x%x, the length:0x%x\n", (UINTPTR)curStartAddress, curLength);
  28. return LOS_NOK;
  29. }
  30. if (((UINTPTR)curStartAddress & (OS_MEM_ALIGN_SIZE - 1)) || (curLength & (OS_MEM_ALIGN_SIZE - 1))) {
  31. PRINT_ERR("Memory address or length configured not aligned:address:0x%x, the length:0x%x, alignsize:%d\n", \
  32. (UINTPTR)curStartAddress, curLength, OS_MEM_ALIGN_SIZE);
  33. return LOS_NOK;
  34. }
  35. if ((lastStartAddress != NULL) && (((UINT8 *)lastStartAddress + lastLength) >= (UINT8 *)curStartAddress)) {
  36. PRINT_ERR("Memory regions overlapped, the last start address:0x%x, the length:0x%x, the current start address:0x%x\n", \
  37. (UINTPTR)lastStartAddress, lastLength, (UINTPTR)curStartAddress);
  38. return LOS_NOK;
  39. }
  40. memRegion++;
  41. regionCount++;
  42. lastStartAddress = curStartAddress;
  43. lastLength = curLength;
  44. }
  45. return LOS_OK;
  46. }
  47.  
  48. STATIC INLINE VOID OsMemMulRegionsLink(struct OsMemPoolHead *poolHead, VOID *lastStartAddress, UINT32 lastLength, struct OsMemNodeHead *lastEndNode, const LosMemRegion *memRegion)
  49. {
  50. UINT32 curLength;
  51. UINT32 gapSize;
  52. struct OsMemNodeHead *curEndNode = NULL;
  53. struct OsMemNodeHead *curFreeNode = NULL;
  54. VOID *curStartAddress = NULL;
  55.  
  56. curStartAddress = memRegion->startAddress;
  57. curLength = memRegion->length;
  58.  
  59. // mark the gap between two regions as one used node
  60. gapSize = (UINT8 *)(curStartAddress) - ((UINT8 *)(lastStartAddress) + lastLength);
  61. lastEndNode->sizeAndFlag = gapSize + OS_MEM_NODE_HEAD_SIZE;
  62. OS_MEM_SET_MAGIC(lastEndNode);
  63. OS_MEM_NODE_SET_USED_FLAG(lastEndNode->sizeAndFlag);
  64.  
  65. // mark the gap node with magic number
  66. OS_MEM_MARK_GAP_NODE(lastEndNode);
  67.  
  68. poolHead->info.totalSize += (curLength + gapSize);
  69. poolHead->info.totalGapSize += gapSize;
  70.  
  71. curFreeNode = (struct OsMemNodeHead *)curStartAddress;
  72. curFreeNode->sizeAndFlag = curLength - OS_MEM_NODE_HEAD_SIZE;
  73. curFreeNode->ptr.prev = lastEndNode;
  74. OS_MEM_SET_MAGIC(curFreeNode);
  75. OsMemFreeNodeAdd(poolHead, (struct OsMemFreeNodeHead *)curFreeNode);
  76.  
  77. curEndNode = OS_MEM_END_NODE(curStartAddress, curLength);
  78. curEndNode->sizeAndFlag = 0;
  79. curEndNode->ptr.prev = curFreeNode;
  80. OS_MEM_SET_MAGIC(curEndNode);
  81. OS_MEM_NODE_SET_USED_FLAG(curEndNode->sizeAndFlag);
  82.  
  83. #if (LOSCFG_MEM_WATERLINE == 1)
  84. poolHead->info.curUsedSize += OS_MEM_NODE_HEAD_SIZE;
  85. poolHead->info.waterLine = poolHead->info.curUsedSize;
  86. #endif
  87. }
  88.  
  89. UINT32 LOS_MemRegionsAdd(VOID *pool, const LosMemRegion *const memRegions, UINT32 memRegionCount)
  90. {
  91. UINT32 ret;
  92. UINT32 lastLength;
  93. UINT32 curLength;
  94. UINT32 regionCount;
  95. struct OsMemPoolHead *poolHead = NULL;
  96. struct OsMemNodeHead *lastEndNode = NULL;
  97. struct OsMemNodeHead *firstFreeNode = NULL;
  98. const LosMemRegion *memRegion = NULL;
  99. VOID *lastStartAddress = NULL;
  100. VOID *curStartAddress = NULL;
  101.  
  102. ret = OsMemMulRegionsParamCheck(pool, memRegions, memRegionCount);
  103. if (ret != LOS_OK) {
  104. return ret;
  105. }
  106.  
  107. memRegion = memRegions;
  108. regionCount = 0;
  109. if (pool != NULL) { // add the memory regions to the specified memory pool
  110. poolHead = (struct OsMemPoolHead *)pool;
  111. lastStartAddress = pool;
  112. lastLength = poolHead->info.totalSize;
  113. } else { // initialize the memory pool with the first memory region
  114. lastStartAddress = memRegion->startAddress;
  115. lastLength = memRegion->length;
  116. poolHead = (struct OsMemPoolHead *)lastStartAddress;
  117. ret = LOS_MemInit(lastStartAddress, lastLength);
  118. if (ret != LOS_OK) {
  119. return ret;
  120. }
  121. memRegion++;
  122. regionCount++;
  123. }
  124.  
  125. firstFreeNode = OS_MEM_FIRST_NODE(lastStartAddress);
  126. lastEndNode = OS_MEM_END_NODE(lastStartAddress, lastLength);
  127. while (regionCount < memRegionCount) { // traverse the rest memory regions, and initialize them as free nodes and link together
  128. curStartAddress = memRegion->startAddress;
  129. curLength = memRegion->length;
  130.  
  131. OsMemMulRegionsLink(poolHead, lastStartAddress, lastLength, lastEndNode, memRegion);
  132. lastStartAddress = curStartAddress;
  133. lastLength = curLength;
  134. lastEndNode = OS_MEM_END_NODE(curStartAddress, curLength);
  135. memRegion++;
  136. regionCount++;
  137. }
  138.  
  139. firstFreeNode->ptr.prev = lastEndNode;
  140. return ret;
  141. }
  142. #endif

小結

本文帶領大家一起剖析了鴻蒙輕核心M核的動態記憶體如何支援多段非連續性記憶體,包含結構體、運作示意圖、新增介面等等。感謝閱讀,如有任何問題、建議,都可以留言評論,謝謝。

更多學習內容,請點選關注IoT物聯網社群新增華為雲IoT小助手微訊號(hwc-iot),獲取更多資訊

點選關注,第一時間瞭解華為雲新鮮技術~