memheap.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * File : memheap.c
  8. *
  9. * Change Logs:
  10. * Date Author Notes
  11. * 2012-04-10 Bernard first implementation
  12. * 2012-10-16 Bernard add the mutex lock for heap object.
  13. * 2012-12-29 Bernard memheap can be used as system heap.
  14. * change mutex lock to semaphore lock.
  15. * 2013-04-10 Bernard add rt_memheap_realloc function.
  16. * 2013-05-24 Bernard fix the rt_memheap_realloc issue.
  17. * 2013-07-11 Grissiom fix the memory block splitting issue.
  18. * 2013-07-15 Grissiom optimize rt_memheap_realloc
  19. * 2021-06-03 Flybreak Fix the crash problem after opening Oz optimization on ac6.
  20. */
  21. #include <rthw.h>
  22. #include <rtthread.h>
  23. #ifdef RT_USING_MEMHEAP
  24. /* dynamic pool magic and mask */
  25. #define RT_MEMHEAP_MAGIC 0x1ea01ea0
  26. #define RT_MEMHEAP_MASK 0xfffffffe
  27. #define RT_MEMHEAP_USED 0x01
  28. #define RT_MEMHEAP_FREED 0x00
  29. #define RT_MEMHEAP_IS_USED(i) ((i)->magic & RT_MEMHEAP_USED)
  30. #define RT_MEMHEAP_MINIALLOC 12
  31. #define RT_MEMHEAP_SIZE RT_ALIGN(sizeof(struct rt_memheap_item), RT_ALIGN_SIZE)
  32. #define MEMITEM_SIZE(item) ((rt_ubase_t)item->next - (rt_ubase_t)item - RT_MEMHEAP_SIZE)
  33. #define MEMITEM(ptr) (struct rt_memheap_item*)((rt_uint8_t*)ptr - RT_MEMHEAP_SIZE)
  34. #ifdef RT_USING_MEMTRACE
  35. /**
  36. * @brief This function will set a new name for memheap.
  37. *
  38. * @param item is a pointer point to a memheap object.
  39. *
  40. * @param name is the new name to be set.
  41. */
  42. rt_inline void rt_memheap_setname(struct rt_memheap_item *item, const char *name)
  43. {
  44. int index;
  45. rt_uint8_t *ptr;
  46. ptr = (rt_uint8_t *) & (item->next_free);
  47. for (index = 0; index < sizeof(void *); index ++)
  48. {
  49. if (name[index] == '\0') break;
  50. ptr[index] = name[index];
  51. }
  52. if (name[index] == '\0') ptr[index] = '\0';
  53. else
  54. {
  55. ptr = (rt_uint8_t *) & (item->prev_free);
  56. for (index = 0; index < sizeof(void *) && (index + sizeof(void *)) < RT_NAME_MAX; index ++)
  57. {
  58. if (name[sizeof(void *) + index] == '\0') break;
  59. ptr[index] = name[sizeof(void *) + index];
  60. }
  61. if (name[sizeof(void *) + index] == '\0') ptr[index] = '\0';
  62. }
  63. }
  64. /**
  65. * @brief This function will set a new name for memheap.
  66. *
  67. * @param ptr is a pointer point to a memheap object.
  68. *
  69. * @param name is the new name to be set.
  70. */
  71. void rt_mem_set_tag(void *ptr, const char *name)
  72. {
  73. struct rt_memheap_item *item;
  74. if (ptr && name)
  75. {
  76. item = MEMITEM(ptr);
  77. rt_memheap_setname(item, name);
  78. }
  79. }
  80. #endif /* RT_USING_MEMTRACE */
  81. /**
  82. * @brief This function initializes a piece of memory called memheap.
  83. *
  84. * @note The initialized memory pool will be:
  85. * +-----------------------------------+--------------------------+
  86. * | whole freed memory block | Used Memory Block Tailer |
  87. * +-----------------------------------+--------------------------+
  88. *
  89. * block_list --> whole freed memory block
  90. *
  91. * The length of Used Memory Block Tailer is 0,
  92. * which is prevents block merging across list
  93. *
  94. * @param memheap is a pointer of the memheap object.
  95. *
  96. * @param name is the name of the memheap.
  97. *
  98. * @param start_addr is the start address of the memheap.
  99. *
  100. * @param size is the size of the memheap.
  101. *
  102. * @return RT_EOK
  103. */
  104. rt_err_t rt_memheap_init(struct rt_memheap *memheap,
  105. const char *name,
  106. void *start_addr,
  107. rt_size_t size)
  108. {
  109. struct rt_memheap_item *item;
  110. RT_ASSERT(memheap != RT_NULL);
  111. /* initialize pool object */
  112. rt_object_init(&(memheap->parent), RT_Object_Class_MemHeap, name);
  113. memheap->start_addr = start_addr;
  114. memheap->pool_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  115. memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE);
  116. memheap->max_used_size = memheap->pool_size - memheap->available_size;
  117. /* initialize the free list header */
  118. item = &(memheap->free_header);
  119. item->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
  120. item->pool_ptr = memheap;
  121. item->next = RT_NULL;
  122. item->prev = RT_NULL;
  123. item->next_free = item;
  124. item->prev_free = item;
  125. /* set the free list to free list header */
  126. memheap->free_list = item;
  127. /* initialize the first big memory block */
  128. item = (struct rt_memheap_item *)start_addr;
  129. item->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
  130. item->pool_ptr = memheap;
  131. item->next = RT_NULL;
  132. item->prev = RT_NULL;
  133. item->next_free = item;
  134. item->prev_free = item;
  135. #ifdef RT_USING_MEMTRACE
  136. rt_memset(item->owner_thread_name, ' ', sizeof(item->owner_thread_name));
  137. #endif /* RT_USING_MEMTRACE */
  138. item->next = (struct rt_memheap_item *)
  139. ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
  140. item->prev = item->next;
  141. /* block list header */
  142. memheap->block_list = item;
  143. /* place the big memory block to free list */
  144. item->next_free = memheap->free_list->next_free;
  145. item->prev_free = memheap->free_list;
  146. memheap->free_list->next_free->prev_free = item;
  147. memheap->free_list->next_free = item;
  148. /* move to the end of memory pool to build a small tailer block,
  149. * which prevents block merging
  150. */
  151. item = item->next;
  152. /* it's a used memory block */
  153. item->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED);
  154. item->pool_ptr = memheap;
  155. item->next = (struct rt_memheap_item *)start_addr;
  156. item->prev = (struct rt_memheap_item *)start_addr;
  157. /* not in free list */
  158. item->next_free = item->prev_free = RT_NULL;
  159. /* initialize semaphore lock */
  160. rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_PRIO);
  161. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  162. ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
  163. start_addr, size, &(memheap->free_header)));
  164. return RT_EOK;
  165. }
  166. RTM_EXPORT(rt_memheap_init);
  167. /**
  168. * @brief This function will remove a memheap from the system.
  169. *
  170. * @param heap is a pointer of memheap object.
  171. *
  172. * @return RT_EOK
  173. */
  174. rt_err_t rt_memheap_detach(struct rt_memheap *heap)
  175. {
  176. RT_ASSERT(heap);
  177. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  178. RT_ASSERT(rt_object_is_systemobject(&heap->parent));
  179. rt_sem_detach(&heap->lock);
  180. rt_object_detach(&(heap->parent));
  181. /* Return a successful completion. */
  182. return RT_EOK;
  183. }
  184. RTM_EXPORT(rt_memheap_detach);
  185. /**
  186. * @brief Allocate a block of memory with a minimum of 'size' bytes on memheap.
  187. *
  188. * @param heap is a pointer for memheap object.
  189. *
  190. * @param size is the minimum size of the requested block in bytes.
  191. *
  192. * @return the pointer to allocated memory or NULL if no free memory was found.
  193. */
  194. void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
  195. {
  196. rt_err_t result;
  197. rt_uint32_t free_size;
  198. struct rt_memheap_item *header_ptr;
  199. RT_ASSERT(heap != RT_NULL);
  200. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  201. /* align allocated size */
  202. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  203. if (size < RT_MEMHEAP_MINIALLOC)
  204. size = RT_MEMHEAP_MINIALLOC;
  205. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
  206. size, RT_NAME_MAX, heap->parent.name));
  207. if (size < heap->available_size)
  208. {
  209. /* search on free list */
  210. free_size = 0;
  211. /* lock memheap */
  212. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  213. if (result != RT_EOK)
  214. {
  215. rt_set_errno(result);
  216. return RT_NULL;
  217. }
  218. /* get the first free memory block */
  219. header_ptr = heap->free_list->next_free;
  220. while (header_ptr != heap->free_list && free_size < size)
  221. {
  222. /* get current freed memory block size */
  223. free_size = MEMITEM_SIZE(header_ptr);
  224. if (free_size < size)
  225. {
  226. /* move to next free memory block */
  227. header_ptr = header_ptr->next_free;
  228. }
  229. }
  230. /* determine if the memory is available. */
  231. if (free_size >= size)
  232. {
  233. /* a block that satisfies the request has been found. */
  234. /* determine if the block needs to be split. */
  235. if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
  236. {
  237. struct rt_memheap_item *new_ptr;
  238. /* split the block. */
  239. new_ptr = (struct rt_memheap_item *)
  240. (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
  241. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  242. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  243. header_ptr,
  244. header_ptr->next,
  245. header_ptr->prev,
  246. new_ptr));
  247. /* mark the new block as a memory block and freed. */
  248. new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
  249. /* put the pool pointer into the new block. */
  250. new_ptr->pool_ptr = heap;
  251. #ifdef RT_USING_MEMTRACE
  252. rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name));
  253. #endif /* RT_USING_MEMTRACE */
  254. /* break down the block list */
  255. new_ptr->prev = header_ptr;
  256. new_ptr->next = header_ptr->next;
  257. header_ptr->next->prev = new_ptr;
  258. header_ptr->next = new_ptr;
  259. /* remove header ptr from free list */
  260. header_ptr->next_free->prev_free = header_ptr->prev_free;
  261. header_ptr->prev_free->next_free = header_ptr->next_free;
  262. header_ptr->next_free = RT_NULL;
  263. header_ptr->prev_free = RT_NULL;
  264. /* insert new_ptr to free list */
  265. new_ptr->next_free = heap->free_list->next_free;
  266. new_ptr->prev_free = heap->free_list;
  267. heap->free_list->next_free->prev_free = new_ptr;
  268. heap->free_list->next_free = new_ptr;
  269. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
  270. new_ptr->next_free,
  271. new_ptr->prev_free));
  272. /* decrement the available byte count. */
  273. heap->available_size = heap->available_size -
  274. size -
  275. RT_MEMHEAP_SIZE;
  276. if (heap->pool_size - heap->available_size > heap->max_used_size)
  277. heap->max_used_size = heap->pool_size - heap->available_size;
  278. }
  279. else
  280. {
  281. /* decrement the entire free size from the available bytes count. */
  282. heap->available_size = heap->available_size - free_size;
  283. if (heap->pool_size - heap->available_size > heap->max_used_size)
  284. heap->max_used_size = heap->pool_size - heap->available_size;
  285. /* remove header_ptr from free list */
  286. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  287. ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
  288. header_ptr,
  289. header_ptr->next_free,
  290. header_ptr->prev_free));
  291. header_ptr->next_free->prev_free = header_ptr->prev_free;
  292. header_ptr->prev_free->next_free = header_ptr->next_free;
  293. header_ptr->next_free = RT_NULL;
  294. header_ptr->prev_free = RT_NULL;
  295. }
  296. /* Mark the allocated block as not available. */
  297. header_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED);
  298. #ifdef RT_USING_MEMTRACE
  299. if (rt_thread_self())
  300. rt_memcpy(header_ptr->owner_thread_name, rt_thread_self()->name, sizeof(header_ptr->owner_thread_name));
  301. else
  302. rt_memcpy(header_ptr->owner_thread_name, "NONE", sizeof(header_ptr->owner_thread_name));
  303. #endif /* RT_USING_MEMTRACE */
  304. /* release lock */
  305. rt_sem_release(&(heap->lock));
  306. /* Return a memory address to the caller. */
  307. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  308. ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
  309. (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
  310. header_ptr,
  311. size));
  312. return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
  313. }
  314. /* release lock */
  315. rt_sem_release(&(heap->lock));
  316. }
  317. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
  318. /* Return the completion status. */
  319. return RT_NULL;
  320. }
  321. RTM_EXPORT(rt_memheap_alloc);
  322. /**
  323. * @brief This function will change the size of previously allocated memory block.
  324. *
  325. * @param heap is a pointer to the memheap object, which will reallocate
  326. * memory from the block
  327. *
  328. * @param ptr is a pointer to start address of memory.
  329. *
  330. * @param newsize is the required new size.
  331. *
  332. * @return the changed memory block address.
  333. */
  334. void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
  335. {
  336. rt_err_t result;
  337. rt_size_t oldsize;
  338. struct rt_memheap_item *header_ptr;
  339. struct rt_memheap_item *new_ptr;
  340. RT_ASSERT(heap);
  341. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  342. if (newsize == 0)
  343. {
  344. rt_memheap_free(ptr);
  345. return RT_NULL;
  346. }
  347. /* align allocated size */
  348. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  349. if (newsize < RT_MEMHEAP_MINIALLOC)
  350. newsize = RT_MEMHEAP_MINIALLOC;
  351. if (ptr == RT_NULL)
  352. {
  353. return rt_memheap_alloc(heap, newsize);
  354. }
  355. /* get memory block header and get the size of memory block */
  356. header_ptr = (struct rt_memheap_item *)
  357. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  358. oldsize = MEMITEM_SIZE(header_ptr);
  359. /* re-allocate memory */
  360. if (newsize > oldsize)
  361. {
  362. void *new_ptr;
  363. /* Fix the crash problem after opening Oz optimization on ac6 */
  364. volatile struct rt_memheap_item *next_ptr;
  365. /* lock memheap */
  366. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  367. if (result != RT_EOK)
  368. {
  369. rt_set_errno(result);
  370. return RT_NULL;
  371. }
  372. next_ptr = header_ptr->next;
  373. /* header_ptr should not be the tail */
  374. RT_ASSERT(next_ptr > header_ptr);
  375. /* check whether the following free space is enough to expand */
  376. if (!RT_MEMHEAP_IS_USED(next_ptr))
  377. {
  378. rt_int32_t nextsize;
  379. nextsize = MEMITEM_SIZE(next_ptr);
  380. RT_ASSERT(next_ptr > 0);
  381. /* Here is the ASCII art of the situation that we can make use of
  382. * the next free node without alloc/memcpy, |*| is the control
  383. * block:
  384. *
  385. * oldsize free node
  386. * |*|-----------|*|----------------------|*|
  387. * newsize >= minialloc
  388. * |*|----------------|*|-----------------|*|
  389. */
  390. if (nextsize + oldsize > newsize + RT_MEMHEAP_MINIALLOC)
  391. {
  392. /* decrement the entire free size from the available bytes count. */
  393. heap->available_size = heap->available_size - (newsize - oldsize);
  394. if (heap->pool_size - heap->available_size > heap->max_used_size)
  395. heap->max_used_size = heap->pool_size - heap->available_size;
  396. /* remove next_ptr from free list */
  397. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  398. ("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
  399. next_ptr,
  400. next_ptr->next_free,
  401. next_ptr->prev_free));
  402. next_ptr->next_free->prev_free = next_ptr->prev_free;
  403. next_ptr->prev_free->next_free = next_ptr->next_free;
  404. next_ptr->next->prev = next_ptr->prev;
  405. next_ptr->prev->next = next_ptr->next;
  406. /* build a new one on the right place */
  407. next_ptr = (struct rt_memheap_item *)((char *)ptr + newsize);
  408. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  409. ("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
  410. next_ptr,
  411. next_ptr->next,
  412. next_ptr->prev));
  413. /* mark the new block as a memory block and freed. */
  414. next_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
  415. /* put the pool pointer into the new block. */
  416. next_ptr->pool_ptr = heap;
  417. #ifdef RT_USING_MEMTRACE
  418. rt_memset((void *)next_ptr->owner_thread_name, ' ', sizeof(next_ptr->owner_thread_name));
  419. #endif /* RT_USING_MEMTRACE */
  420. next_ptr->prev = header_ptr;
  421. next_ptr->next = header_ptr->next;
  422. header_ptr->next->prev = (struct rt_memheap_item *)next_ptr;
  423. header_ptr->next = (struct rt_memheap_item *)next_ptr;
  424. /* insert next_ptr to free list */
  425. next_ptr->next_free = heap->free_list->next_free;
  426. next_ptr->prev_free = heap->free_list;
  427. heap->free_list->next_free->prev_free = (struct rt_memheap_item *)next_ptr;
  428. heap->free_list->next_free = (struct rt_memheap_item *)next_ptr;
  429. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
  430. next_ptr->next_free,
  431. next_ptr->prev_free));
  432. /* release lock */
  433. rt_sem_release(&(heap->lock));
  434. return ptr;
  435. }
  436. }
  437. /* release lock */
  438. rt_sem_release(&(heap->lock));
  439. /* re-allocate a memory block */
  440. new_ptr = (void *)rt_memheap_alloc(heap, newsize);
  441. if (new_ptr != RT_NULL)
  442. {
  443. rt_memcpy(new_ptr, ptr, oldsize < newsize ? oldsize : newsize);
  444. rt_memheap_free(ptr);
  445. }
  446. return new_ptr;
  447. }
  448. /* don't split when there is less than one node space left */
  449. if (newsize + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC >= oldsize)
  450. return ptr;
  451. /* lock memheap */
  452. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  453. if (result != RT_EOK)
  454. {
  455. rt_set_errno(result);
  456. return RT_NULL;
  457. }
  458. /* split the block. */
  459. new_ptr = (struct rt_memheap_item *)
  460. (((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);
  461. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  462. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  463. header_ptr,
  464. header_ptr->next,
  465. header_ptr->prev,
  466. new_ptr));
  467. /* mark the new block as a memory block and freed. */
  468. new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
  469. /* put the pool pointer into the new block. */
  470. new_ptr->pool_ptr = heap;
  471. #ifdef RT_USING_MEMTRACE
  472. rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name));
  473. #endif /* RT_USING_MEMTRACE */
  474. /* break down the block list */
  475. new_ptr->prev = header_ptr;
  476. new_ptr->next = header_ptr->next;
  477. header_ptr->next->prev = new_ptr;
  478. header_ptr->next = new_ptr;
  479. /* determine if the block can be merged with the next neighbor. */
  480. if (!RT_MEMHEAP_IS_USED(new_ptr->next))
  481. {
  482. struct rt_memheap_item *free_ptr;
  483. /* merge block with next neighbor. */
  484. free_ptr = new_ptr->next;
  485. heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);
  486. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  487. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  488. header_ptr, header_ptr->next_free, header_ptr->prev_free));
  489. free_ptr->next->prev = new_ptr;
  490. new_ptr->next = free_ptr->next;
  491. /* remove free ptr from free list */
  492. free_ptr->next_free->prev_free = free_ptr->prev_free;
  493. free_ptr->prev_free->next_free = free_ptr->next_free;
  494. }
  495. /* insert the split block to free list */
  496. new_ptr->next_free = heap->free_list->next_free;
  497. new_ptr->prev_free = heap->free_list;
  498. heap->free_list->next_free->prev_free = new_ptr;
  499. heap->free_list->next_free = new_ptr;
  500. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
  501. new_ptr->next_free,
  502. new_ptr->prev_free));
  503. /* increment the available byte count. */
  504. heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
  505. /* release lock */
  506. rt_sem_release(&(heap->lock));
  507. /* return the old memory block */
  508. return ptr;
  509. }
  510. RTM_EXPORT(rt_memheap_realloc);
  511. /**
  512. * @brief This function will release the allocated memory block by
  513. * rt_malloc. The released memory block is taken back to system heap.
  514. *
  515. * @param ptr the address of memory which will be released.
  516. */
  517. void rt_memheap_free(void *ptr)
  518. {
  519. rt_err_t result;
  520. struct rt_memheap *heap;
  521. struct rt_memheap_item *header_ptr, *new_ptr;
  522. rt_uint32_t insert_header;
  523. /* NULL check */
  524. if (ptr == RT_NULL) return;
  525. /* set initial status as OK */
  526. insert_header = 1;
  527. new_ptr = RT_NULL;
  528. header_ptr = (struct rt_memheap_item *)
  529. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  530. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
  531. ptr, header_ptr));
  532. /* check magic */
  533. if (header_ptr->magic != (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED))
  534. {
  535. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("bad magic:0x%08x @ memheap\n",
  536. header_ptr->magic));
  537. }
  538. RT_ASSERT(header_ptr->magic == (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED));
  539. /* check whether this block of memory has been over-written. */
  540. RT_ASSERT((header_ptr->next->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
  541. /* get pool ptr */
  542. heap = header_ptr->pool_ptr;
  543. RT_ASSERT(heap);
  544. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  545. /* lock memheap */
  546. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  547. if (result != RT_EOK)
  548. {
  549. rt_set_errno(result);
  550. return ;
  551. }
  552. /* Mark the memory as available. */
  553. header_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
  554. /* Adjust the available number of bytes. */
  555. heap->available_size += MEMITEM_SIZE(header_ptr);
  556. /* Determine if the block can be merged with the previous neighbor. */
  557. if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
  558. {
  559. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
  560. header_ptr->prev));
  561. /* adjust the available number of bytes. */
  562. heap->available_size += RT_MEMHEAP_SIZE;
  563. /* yes, merge block with previous neighbor. */
  564. (header_ptr->prev)->next = header_ptr->next;
  565. (header_ptr->next)->prev = header_ptr->prev;
  566. /* move header pointer to previous. */
  567. header_ptr = header_ptr->prev;
  568. /* don't insert header to free list */
  569. insert_header = 0;
  570. }
  571. /* determine if the block can be merged with the next neighbor. */
  572. if (!RT_MEMHEAP_IS_USED(header_ptr->next))
  573. {
  574. /* adjust the available number of bytes. */
  575. heap->available_size += RT_MEMHEAP_SIZE;
  576. /* merge block with next neighbor. */
  577. new_ptr = header_ptr->next;
  578. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  579. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  580. new_ptr, new_ptr->next_free, new_ptr->prev_free));
  581. new_ptr->next->prev = header_ptr;
  582. header_ptr->next = new_ptr->next;
  583. /* remove new ptr from free list */
  584. new_ptr->next_free->prev_free = new_ptr->prev_free;
  585. new_ptr->prev_free->next_free = new_ptr->next_free;
  586. }
  587. if (insert_header)
  588. {
  589. /* no left merge, insert to free list */
  590. header_ptr->next_free = heap->free_list->next_free;
  591. header_ptr->prev_free = heap->free_list;
  592. heap->free_list->next_free->prev_free = header_ptr;
  593. heap->free_list->next_free = header_ptr;
  594. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  595. ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
  596. header_ptr->next_free, header_ptr->prev_free));
  597. }
  598. #ifdef RT_USING_MEMTRACE
  599. rt_memset(header_ptr->owner_thread_name, ' ', sizeof(header_ptr->owner_thread_name));
  600. #endif /* RT_USING_MEMTRACE */
  601. /* release lock */
  602. rt_sem_release(&(heap->lock));
  603. }
  604. RTM_EXPORT(rt_memheap_free);
  605. #ifdef RT_USING_FINSH
  606. static void _memheap_dump_tag(struct rt_memheap_item *item)
  607. {
  608. rt_uint8_t name[2 * sizeof(void *)];
  609. rt_uint8_t *ptr;
  610. ptr = (rt_uint8_t *) & (item->next_free);
  611. rt_memcpy(name, ptr, sizeof(void *));
  612. ptr = (rt_uint8_t *) & (item->prev_free);
  613. rt_memcpy(&name[sizeof(void *)], ptr, sizeof(void *));
  614. rt_kprintf("%.*s", 2 * sizeof(void *), name);
  615. }
  616. /**
  617. * @brief This function will print the memheap infomation.
  618. *
  619. * @param heap is the pointer to the memheap to get information.
  620. *
  621. * @return 0
  622. */
  623. int rt_memheap_dump(struct rt_memheap *heap)
  624. {
  625. struct rt_memheap_item *item, *end;
  626. if (heap == RT_NULL) return 0;
  627. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  628. rt_kprintf("\n[%.*s] [0x%08x - 0x%08x]->\n", RT_NAME_MAX, heap->parent.name,
  629. (rt_ubase_t)heap->start_addr, (rt_ubase_t)heap->start_addr + heap->pool_size);
  630. rt_kprintf("------------------------------\n");
  631. /* lock memheap */
  632. rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  633. item = heap->block_list;
  634. end = (struct rt_memheap_item *)((rt_uint8_t *)heap->start_addr + heap->pool_size - RT_MEMHEAP_SIZE);
  635. /* for each memory block */
  636. while ((rt_ubase_t)item < ((rt_ubase_t)end))
  637. {
  638. if (RT_MEMHEAP_IS_USED(item) && ((item->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC))
  639. rt_kprintf("0x%08x", item + 1);
  640. if (item->magic == (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED))
  641. {
  642. rt_kprintf("0x%08x: %-8d ", item + 1, MEMITEM_SIZE(item));
  643. _memheap_dump_tag(item);
  644. rt_kprintf("\n");
  645. }
  646. else
  647. {
  648. rt_kprintf("0x%08x: %-8d <F>\n", item + 1, MEMITEM_SIZE(item));
  649. }
  650. item = item->next;
  651. }
  652. rt_sem_release(&(heap->lock));
  653. return 0;
  654. }
  655. int memheaptrace(void)
  656. {
  657. int count = rt_object_get_length(RT_Object_Class_MemHeap);
  658. struct rt_memheap **heaps;
  659. if (count > 0)
  660. {
  661. int index;
  662. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  663. extern int list_memheap(void);
  664. #endif
  665. heaps = (struct rt_memheap **)rt_malloc(sizeof(struct rt_memheap *) * count);
  666. if (heaps == RT_NULL) return 0;
  667. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  668. list_memheap();
  669. #endif
  670. rt_kprintf("memheap header size: %d\n", RT_MEMHEAP_SIZE);
  671. count = rt_object_get_pointers(RT_Object_Class_MemHeap, (rt_object_t *)heaps, count);
  672. for (index = 0; index < count; index++)
  673. {
  674. rt_memheap_dump(heaps[index]);
  675. }
  676. rt_free(heaps);
  677. }
  678. return 0;
  679. }
  680. MSH_CMD_EXPORT(memheaptrace, dump memory trace information);
  681. #endif /* RT_USING_FINSH */
  682. #ifdef RT_USING_MEMHEAP_AS_HEAP
  683. static struct rt_memheap _heap;
  684. /**
  685. * @brief This function initializes a heap for system.
  686. *
  687. * @param begin_addr is the start address of the memory.
  688. *
  689. * @param end_addr is the end address of the memory.
  690. */
  691. void rt_system_heap_init(void *begin_addr, void *end_addr)
  692. {
  693. RT_ASSERT((rt_uint32_t)end_addr > (rt_uint32_t)begin_addr);
  694. /* initialize a default heap in the system */
  695. rt_memheap_init(&_heap,
  696. "heap",
  697. begin_addr,
  698. (rt_uint32_t)end_addr - (rt_uint32_t)begin_addr);
  699. }
  700. /**
  701. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  702. *
  703. * @param size is the minimum size of the requested block in bytes.
  704. */
  705. void *rt_malloc(rt_size_t size)
  706. {
  707. void *ptr;
  708. /* try to allocate in system heap */
  709. ptr = rt_memheap_alloc(&_heap, size);
  710. if (ptr == RT_NULL)
  711. {
  712. struct rt_object *object;
  713. struct rt_list_node *node;
  714. struct rt_memheap *heap;
  715. struct rt_object_information *information;
  716. /* try to allocate on other memory heap */
  717. information = rt_object_get_information(RT_Object_Class_MemHeap);
  718. RT_ASSERT(information != RT_NULL);
  719. for (node = information->object_list.next;
  720. node != &(information->object_list);
  721. node = node->next)
  722. {
  723. object = rt_list_entry(node, struct rt_object, list);
  724. heap = (struct rt_memheap *)object;
  725. RT_ASSERT(heap);
  726. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  727. /* not allocate in the default system heap */
  728. if (heap == &_heap)
  729. continue;
  730. ptr = rt_memheap_alloc(heap, size);
  731. if (ptr != RT_NULL)
  732. break;
  733. }
  734. }
  735. #ifdef RT_USING_MEMTRACE
  736. if (ptr == RT_NULL)
  737. {
  738. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("malloc[%d] => NULL", size));
  739. }
  740. else
  741. {
  742. struct rt_memheap_item *item = MEMITEM(ptr);
  743. if (rt_thread_self())
  744. rt_memheap_setname(item, rt_thread_self()->name);
  745. else
  746. rt_memheap_setname(item, "<null>");
  747. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("malloc => 0x%08x : %d", ptr, size));
  748. }
  749. #endif /* RT_USING_MEMTRACE */
  750. return ptr;
  751. }
  752. RTM_EXPORT(rt_malloc);
  753. /**
  754. * @brief This function will release the previously allocated memory block by
  755. * rt_malloc. The released memory block is taken back to system heap.
  756. *
  757. * @param rmem the address of memory which will be released.
  758. */
  759. void rt_free(void *rmem)
  760. {
  761. rt_memheap_free(rmem);
  762. }
  763. RTM_EXPORT(rt_free);
  764. /**
  765. * @brief This function will change the size of previously allocated memory block.
  766. *
  767. * @param rmem is the pointer to memory allocated by rt_malloc.
  768. *
  769. * @param newsize is the required new size.
  770. *
  771. * @return the changed memory block address.
  772. */
  773. void *rt_realloc(void *rmem, rt_size_t newsize)
  774. {
  775. void *new_ptr;
  776. struct rt_memheap_item *header_ptr;
  777. if (rmem == RT_NULL)
  778. return rt_malloc(newsize);
  779. if (newsize == 0)
  780. {
  781. rt_free(rmem);
  782. return RT_NULL;
  783. }
  784. /* get old memory item */
  785. header_ptr = (struct rt_memheap_item *)
  786. ((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);
  787. new_ptr = rt_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
  788. if (new_ptr == RT_NULL && newsize != 0)
  789. {
  790. /* allocate memory block from other memheap */
  791. new_ptr = rt_malloc(newsize);
  792. if (new_ptr != RT_NULL && rmem != RT_NULL)
  793. {
  794. rt_size_t oldsize;
  795. /* get the size of old memory block */
  796. oldsize = MEMITEM_SIZE(header_ptr);
  797. if (newsize > oldsize)
  798. rt_memcpy(new_ptr, rmem, oldsize);
  799. else
  800. rt_memcpy(new_ptr, rmem, newsize);
  801. rt_free(rmem);
  802. }
  803. }
  804. #ifdef RT_USING_MEMTRACE
  805. if (new_ptr == RT_NULL)
  806. {
  807. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("realloc[%d] => NULL", newsize));
  808. }
  809. else
  810. {
  811. struct rt_memheap_item *item = MEMITEM(new_ptr);
  812. if (rt_thread_self())
  813. rt_memheap_setname(item, rt_thread_self()->name);
  814. else
  815. rt_memheap_setname(item, "<null>");
  816. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("realloc => 0x%08x : %d",
  817. new_ptr, newsize));
  818. }
  819. #endif /* RT_USING_MEMTRACE */
  820. return new_ptr;
  821. }
  822. RTM_EXPORT(rt_realloc);
  823. /**
  824. * @brief This function will contiguously allocate enough space for count objects
  825. * that are size bytes of memory each and returns a pointer to the allocated
  826. * memory.
  827. *
  828. * @note The allocated memory is filled with bytes of value zero.
  829. *
  830. * @param count is the number of objects to allocate.
  831. *
  832. * @param size is the size of one object to allocate.
  833. *
  834. * @return pointer to allocated memory pointer.
  835. */
  836. void *rt_calloc(rt_size_t count, rt_size_t size)
  837. {
  838. void *ptr;
  839. rt_size_t total_size;
  840. total_size = count * size;
  841. ptr = rt_malloc(total_size);
  842. if (ptr != RT_NULL)
  843. {
  844. /* clean memory */
  845. rt_memset(ptr, 0, total_size);
  846. }
  847. #ifdef RT_USING_MEMTRACE
  848. if (ptr == RT_NULL)
  849. {
  850. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("calloc[%d x %d] => NULL",
  851. count, size));
  852. }
  853. else
  854. {
  855. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("calloc => 0x%08x : %d",
  856. ptr, count * size));
  857. }
  858. #endif /* RT_USING_MEMTRACE */
  859. return ptr;
  860. }
  861. RTM_EXPORT(rt_calloc);
  862. /**
  863. * @brief This function will caculate the total memory, the used memory, and
  864. * the max used memory.
  865. *
  866. * @param total is a pointer to get the total size of the memory.
  867. *
  868. * @param used is a pointer to get the size of memory used.
  869. *
  870. * @param max_used is a pointer to get the maximum memory used.
  871. */
  872. void rt_memory_info(rt_uint32_t *total,
  873. rt_uint32_t *used,
  874. rt_uint32_t *max_used)
  875. {
  876. if (total != RT_NULL)
  877. *total = _heap.pool_size;
  878. if (used != RT_NULL)
  879. *used = _heap.pool_size - _heap.available_size;
  880. if (max_used != RT_NULL)
  881. *max_used = _heap.max_used_size;
  882. }
  883. #endif /* RT_USING_MEMHEAP_AS_HEAP */
  884. #ifdef RT_USING_MEMTRACE
  885. /**
  886. * @brief This function will print the used memheap infomation.
  887. *
  888. * @param mh is a pointer of the memheap object.
  889. */
  890. void dump_used_memheap(struct rt_memheap *mh)
  891. {
  892. struct rt_memheap_item *header_ptr;
  893. rt_uint32_t block_size;
  894. rt_kprintf("\nmemory heap address:\n");
  895. rt_kprintf("heap_ptr: 0x%08x\n", mh->start_addr);
  896. rt_kprintf("free : 0x%08x\n", mh->available_size);
  897. rt_kprintf("max_used: 0x%08x\n", mh->max_used_size);
  898. rt_kprintf("size : 0x%08x\n", mh->pool_size);
  899. rt_kprintf("\n--memory used information --\n");
  900. header_ptr = mh->block_list;
  901. while (header_ptr->next != mh->block_list)
  902. {
  903. if ((header_ptr->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC)
  904. {
  905. rt_kprintf("[0x%08x - incorrect magic: 0x%08x\n", header_ptr, header_ptr->magic);
  906. break;
  907. }
  908. /* get current memory block size */
  909. block_size = MEMITEM_SIZE(header_ptr);
  910. if (block_size < 0)
  911. break;
  912. if (RT_MEMHEAP_IS_USED(header_ptr))
  913. {
  914. /* dump information */
  915. rt_kprintf("[0x%08x - %d - %c%c%c%c] used\n", header_ptr, block_size,
  916. header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
  917. header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
  918. }
  919. else
  920. {
  921. /* dump information */
  922. rt_kprintf("[0x%08x - %d - %c%c%c%c] free\n", header_ptr, block_size,
  923. header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
  924. header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
  925. }
  926. /* move to next used memory block */
  927. header_ptr = header_ptr->next;
  928. }
  929. }
  930. void memtrace_heap()
  931. {
  932. struct rt_object_information *info;
  933. struct rt_list_node *list;
  934. struct rt_memheap *mh;
  935. struct rt_list_node *node;
  936. info = rt_object_get_information(RT_Object_Class_MemHeap);
  937. list = &info->object_list;
  938. for (node = list->next; node != list; node = node->next)
  939. {
  940. mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);
  941. dump_used_memheap(mh);
  942. }
  943. }
  944. #ifdef RT_USING_FINSH
  945. #include <finsh.h>
  946. MSH_CMD_EXPORT(memtrace_heap, dump memory trace for heap);
  947. #endif /* RT_USING_FINSH */
  948. #endif /* RT_USING_MEMTRACE */
  949. #endif /* RT_USING_MEMHEAP */