memory_manager.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "memory_manager.h"
  2. /* 内存块大小不能 小于 MemoryMINIMUM_BLOCK_SIZE */
  3. #define MemoryMINIMUM_BLOCK_SIZE ((size_t)(pMemoryStructSize << 1))
  4. /* 内存使用标记 最高位标志 当最高位置位时表示已使用 */
  5. /* pBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * MemoryBITS_PER_BYTE ) - 1 ); pBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * 8 ) - 1 ); */
  6. #define MemoryBITS_PER_BYTE ((size_t)8)
  7. #define BYTE_ALIGNMENT (uint8_t)8UL
  8. #define BYTE_ALIGNMENT_MASK (uint8_t)(BYTE_ALIGNMENT - 1)
  9. //__align(BYTE_ALIGNMENT) static volatile uint8_t AXISRAM[ AXISRAMSIZE ] __attribute__((at(0x24000000)));
  10. //__align(BYTE_ALIGNMENT) static volatile uint8_t SDRAMEX[ SDRAMSIZE ] __attribute__((at(0xC0400000)));
  11. MemoryStruct SRAM0 = {
  12. .pMemory = (uint8_t *)0x24000000,
  13. AXISRAMSIZE,
  14. 0,
  15. 0,
  16. 0,
  17. 0,
  18. };
  19. MemoryStruct SRAM1 = {
  20. // 可以通过AXI 访问
  21. .pMemory = (uint8_t *)0x10000000,
  22. SRAM1SIZE,
  23. 0,
  24. 0,
  25. 0,
  26. 0,
  27. };
  28. MemoryStruct SRAM2 = {
  29. // 可以通过AXI->AHB 访问
  30. .pMemory = (uint8_t *)0x30020000,
  31. SRAM2SIZE,
  32. 0,
  33. 0,
  34. 0,
  35. 0,
  36. };
  37. MemoryStruct SDRAM = {
  38. .pMemory = (uint8_t *)0xC0400000,
  39. SDRAMSIZE,
  40. 0,
  41. 0,
  42. 0,
  43. 0,
  44. };
  45. /*合并内存*/
  46. static void pInsertBlockIntoFreeList(MemoryStruct *mem, BlockLink_t *pxBlockToInsert);
  47. /*-----------------------------------------------------------*/
  48. /* 在每个分配的内存的开始处放置的结构的大小块必须通过正确的字节对齐 */
  49. static const size_t pMemoryStructSize = (sizeof(BlockLink_t) + ((size_t)(BYTE_ALIGNMENT - 1))) & ~((size_t)BYTE_ALIGNMENT_MASK);
  50. /* 内存使用标志 BLOCK 的最高位 | pBlockAllocatedBit 表示已使用 */
  51. static size_t pBlockAllocatedBit = 0;
  52. /*-----------------------------------------------------------*/
  53. void *pMemoryMalloc(MemoryStruct *mem, size_t xWantedSize)
  54. {
  55. BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
  56. void *pvReturn = NULL;
  57. // __set_FAULTMASK(1); // 关闭所有中断();
  58. /* 申请内存大小不能大于 pBlockAllocatedBit */
  59. if ((xWantedSize & pBlockAllocatedBit) == 0)
  60. {
  61. if (xWantedSize > 0)
  62. {
  63. /* xWantedSize 实际大小 需要加 pMemoryStructSize */
  64. xWantedSize += pMemoryStructSize;
  65. /* 判断 xWantedSize 是否 字节对齐 */
  66. if ((xWantedSize & BYTE_ALIGNMENT_MASK) != 0x00)
  67. {
  68. /* 字节对齐处理 */
  69. xWantedSize += (BYTE_ALIGNMENT - (xWantedSize & BYTE_ALIGNMENT_MASK));
  70. }
  71. }
  72. /* 申请大小大于零 并且 小于当前剩余量 */
  73. if ((xWantedSize > 0) && (xWantedSize <= mem->Free))
  74. {
  75. /* 寻找足够大小的内存块 */
  76. pxPreviousBlock = &mem->pStart;
  77. pxBlock = mem->pStart.pxNextFreeBlock;
  78. while ((pxBlock->xBlockSize < xWantedSize) && (pxBlock->pxNextFreeBlock != NULL))
  79. {
  80. pxPreviousBlock = pxBlock; //保存当前 BLOCK
  81. pxBlock = pxBlock->pxNextFreeBlock; //指向下一个 BLOCK
  82. }
  83. /* 是否有满足条件的内存块 */
  84. if (pxBlock != mem->pEnd) // 如果 pxBlock = = pEnd[mem] 则找到了最后没找到
  85. {
  86. /* 内存地址为 pxNextFreeBlock + pMemoryStructSize 为起始 */
  87. pvReturn = (void *)(((uint8_t *)pxPreviousBlock->pxNextFreeBlock) + pMemoryStructSize);
  88. /* 改变当前 BLOCK 的 pxNextFreeBlock 的指向地址 指向下一个 BLOCK 的地址 */
  89. pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
  90. /* 如果 pxBlock 剩余内存 大于 最小的 BLOCK 大小 */
  91. if ((pxBlock->xBlockSize - xWantedSize) > MemoryMINIMUM_BLOCK_SIZE)
  92. {
  93. /* 创建新的 BLOCKLink 地址为 pxBlock + xWantedSize */
  94. pxNewBlockLink = (void *)(((uint8_t *)pxBlock) + xWantedSize);
  95. /* 创建新的 BLOCKLink 内存大小为 xBlockSize - 申请值 */
  96. pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
  97. /* 当前 BLOCK 的大小 */
  98. pxBlock->xBlockSize = xWantedSize;
  99. /* 插入新的LIST 到剩余 内存中 */
  100. pInsertBlockIntoFreeList(mem, pxNewBlockLink);
  101. }
  102. /* 记录当前内存剩余 */
  103. mem->Free -= pxBlock->xBlockSize;
  104. if (mem->Free < mem->MinFree)
  105. {
  106. /* 记录历史内存最小剩余 */
  107. mem->MinFree = mem->Free;
  108. }
  109. /* 当前BLock 已使用标志置位 */
  110. pxBlock->xBlockSize |= pBlockAllocatedBit;
  111. pxBlock->pxNextFreeBlock = NULL;
  112. }
  113. }
  114. }
  115. // __set_FAULTMASK(0);
  116. return pvReturn;
  117. }
  118. /*-----------------------------------------------------------*/
  119. void *pMemoryReMalloc(MemoryStruct *mem1, MemoryStruct *mem2, void *ptr, size_t size)
  120. {
  121. size_t *offset = NULL;
  122. offset = pMemoryMalloc(mem1, size);
  123. if (offset == NULL)
  124. {
  125. return NULL;
  126. }
  127. else
  128. {
  129. memcpy(offset, ptr, size);
  130. pMemoryFree(mem2, ptr);
  131. }
  132. return offset;
  133. }
  134. /*-----------------------------------------------------------*/
  135. void pMemoryFree(MemoryStruct *mem, void *pv)
  136. {
  137. uint8_t *puc = (uint8_t *)pv;
  138. BlockLink_t *pxLink;
  139. if (pv == NULL)
  140. return;
  141. /* 内存BLOCK list 地址为内存地址 - pMemoryStructSize 在申请的时候会 + pMemoryStructSize */
  142. puc -= pMemoryStructSize;
  143. /* 使用 puc 避免编译器报警告 */
  144. pxLink = (void *)puc;
  145. /* 使用 xBlockSize 是否使用 */
  146. if ((pxLink->xBlockSize & pBlockAllocatedBit) != 0)
  147. {
  148. if (pxLink->pxNextFreeBlock == NULL)
  149. {
  150. /* 去掉使用标志 */
  151. pxLink->xBlockSize &= ~pBlockAllocatedBit;
  152. // __set_FAULTMASK(1); // 关闭所有中断();
  153. /* 把这个BLOCK 添加到剩余内存中 */
  154. mem->Free += pxLink->xBlockSize;
  155. pInsertBlockIntoFreeList(mem, ((BlockLink_t *)pxLink));
  156. // __set_FAULTMASK(0);
  157. }
  158. }
  159. }
  160. /*-----------------------------------------------------------*/
  161. size_t pMemoryGetFreeMemorySize(MemoryStruct *mem)
  162. {
  163. return mem->Free;
  164. }
  165. /*-----------------------------------------------------------*/
  166. size_t pMemoryGetMinimumEverFreeMemorySize(MemoryStruct *mem)
  167. {
  168. return mem->MinFree;
  169. }
  170. /*-----------------------------------------------------------*/
  171. void pMemoryInit(MemoryStruct *mem)
  172. {
  173. BlockLink_t *pxFirstFreeBlock;
  174. uint8_t *pucAlignedHeap;
  175. size_t uxAddress;
  176. size_t xTotalHeapSize = mem->size;
  177. memset((void *)mem->pMemory, 0, mem->size);
  178. /* 内存地址赋值 */
  179. uxAddress = (size_t)mem->pMemory;
  180. /* 判断是否内存对齐 */
  181. if ((uxAddress & BYTE_ALIGNMENT_MASK) != 0)
  182. {
  183. uxAddress += (BYTE_ALIGNMENT - 1);
  184. uxAddress &= ~((size_t)BYTE_ALIGNMENT_MASK);
  185. xTotalHeapSize -= uxAddress - (size_t)mem->pMemory;
  186. }
  187. pucAlignedHeap = (uint8_t *)uxAddress;
  188. /* 第一个 BLOCK 初始化 */
  189. mem->pStart.pxNextFreeBlock = (void *)pucAlignedHeap;
  190. mem->pStart.xBlockSize = (size_t)0;
  191. /* 内存结尾赋值 */
  192. uxAddress = ((size_t)pucAlignedHeap) + xTotalHeapSize;
  193. uxAddress -= pMemoryStructSize;
  194. uxAddress &= ~((size_t)BYTE_ALIGNMENT_MASK);
  195. mem->pEnd = (void *)uxAddress;
  196. mem->pEnd->xBlockSize = 0;
  197. mem->pEnd->pxNextFreeBlock = NULL;
  198. /* 首先,有一个单独的自由块,它的大小可以用来处理整个堆空间,减去pxEnd所占用的空间 */
  199. pxFirstFreeBlock = (void *)pucAlignedHeap;
  200. pxFirstFreeBlock->xBlockSize = uxAddress - (size_t)pxFirstFreeBlock;
  201. pxFirstFreeBlock->pxNextFreeBlock = mem->pEnd;
  202. /* 内存使用剩余量 */
  203. mem->Free = pxFirstFreeBlock->xBlockSize;
  204. mem->MinFree = pxFirstFreeBlock->xBlockSize;
  205. /* 最高位标志 当最高位置位时表示已使用 */
  206. pBlockAllocatedBit = ((size_t)1) << ((sizeof(size_t) * MemoryBITS_PER_BYTE) - 1);
  207. }
  208. /*-----------------------------------------------------------*/
  209. static void pInsertBlockIntoFreeList(MemoryStruct *mem, BlockLink_t *pxBlockToInsert)
  210. {
  211. BlockLink_t *pxIterator;
  212. uint8_t *puc;
  213. /* 寻找插入内存地址 找到当前 插入的 BLOCK 位置 pxIterator 下一个内存地址 >= pxBlockToInsert */
  214. for (pxIterator = &mem->pStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock)
  215. {
  216. }
  217. /* 判断前一个 BLOCK(pxIterator) 是否 指向的是当前 BLOCK(pxBlockToInsert) 内存块*/
  218. puc = (uint8_t *)pxIterator;
  219. if ((puc + pxIterator->xBlockSize) == (uint8_t *)pxBlockToInsert)
  220. {
  221. pxIterator->xBlockSize += pxBlockToInsert->xBlockSize; //合并两个 BLOCK
  222. pxBlockToInsert = pxIterator;
  223. }
  224. /* 插入的块,以及它之前插入的块创建一个连续的内存块?*/
  225. puc = (uint8_t *)pxBlockToInsert;
  226. if ((puc + pxBlockToInsert->xBlockSize) == (uint8_t *)pxIterator->pxNextFreeBlock)
  227. {
  228. if (pxIterator->pxNextFreeBlock != mem->pEnd)
  229. {
  230. /* 合并两个内存块 */
  231. pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
  232. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
  233. }
  234. else
  235. {
  236. pxBlockToInsert->pxNextFreeBlock = mem->pEnd;
  237. }
  238. }
  239. else
  240. {
  241. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
  242. }
  243. /* 如果插入的块插入一个gab,那么就与block合并在此之前和之后,它的pxNextFreeBlock指针将会有已经设置好了,
  244. 不应该在这里设置因为这将使它指向本身。 */
  245. if (pxIterator != pxBlockToInsert)
  246. {
  247. pxIterator->pxNextFreeBlock = pxBlockToInsert;
  248. }
  249. }