memheap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  20. #include <rthw.h>
  21. #include <rtthread.h>
  22. #ifdef RT_USING_MEMHEAP
  23. /* dynamic pool magic and mask */
  24. #define RT_MEMHEAP_MAGIC 0x1ea01ea0
  25. #define RT_MEMHEAP_MASK 0xfffffffe
  26. #define RT_MEMHEAP_USED 0x01
  27. #define RT_MEMHEAP_FREED 0x00
  28. #define RT_MEMHEAP_IS_USED(i) ((i)->magic & RT_MEMHEAP_USED)
  29. #define RT_MEMHEAP_MINIALLOC 12
  30. #define RT_MEMHEAP_SIZE RT_ALIGN(sizeof(struct rt_memheap_item), RT_ALIGN_SIZE)
  31. #define MEMITEM_SIZE(item) ((rt_ubase_t)item->next - (rt_ubase_t)item - RT_MEMHEAP_SIZE)
  32. /*
  33. * The initialized memory pool will be:
  34. * +-----------------------------------+--------------------------+
  35. * | whole freed memory block | Used Memory Block Tailer |
  36. * +-----------------------------------+--------------------------+
  37. *
  38. * block_list --> whole freed memory block
  39. *
  40. * The length of Used Memory Block Tailer is 0,
  41. * which is prevents block merging across list
  42. */
  43. rt_err_t rt_memheap_init(struct rt_memheap *memheap,
  44. const char *name,
  45. void *start_addr,
  46. rt_size_t size)
  47. {
  48. struct rt_memheap_item *item;
  49. RT_ASSERT(memheap != RT_NULL);
  50. /* initialize pool object */
  51. rt_object_init(&(memheap->parent), RT_Object_Class_MemHeap, name);
  52. memheap->start_addr = start_addr;
  53. memheap->pool_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  54. memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE);
  55. memheap->max_used_size = memheap->pool_size - memheap->available_size;
  56. /* initialize the free list header */
  57. item = &(memheap->free_header);
  58. item->magic = RT_MEMHEAP_MAGIC;
  59. item->pool_ptr = memheap;
  60. item->next = RT_NULL;
  61. item->prev = RT_NULL;
  62. item->next_free = item;
  63. item->prev_free = item;
  64. /* set the free list to free list header */
  65. memheap->free_list = item;
  66. /* initialize the first big memory block */
  67. item = (struct rt_memheap_item *)start_addr;
  68. item->magic = RT_MEMHEAP_MAGIC;
  69. item->pool_ptr = memheap;
  70. item->next = RT_NULL;
  71. item->prev = RT_NULL;
  72. item->next_free = item;
  73. item->prev_free = item;
  74. #ifdef RT_USING_MEMTRACE
  75. rt_memset(item->owner_thread_name, ' ', sizeof(item->owner_thread_name));
  76. #endif
  77. item->next = (struct rt_memheap_item *)
  78. ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
  79. item->prev = item->next;
  80. /* block list header */
  81. memheap->block_list = item;
  82. /* place the big memory block to free list */
  83. item->next_free = memheap->free_list->next_free;
  84. item->prev_free = memheap->free_list;
  85. memheap->free_list->next_free->prev_free = item;
  86. memheap->free_list->next_free = item;
  87. /* move to the end of memory pool to build a small tailer block,
  88. * which prevents block merging
  89. */
  90. item = item->next;
  91. /* it's a used memory block */
  92. item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
  93. item->pool_ptr = memheap;
  94. item->next = (struct rt_memheap_item *)start_addr;
  95. item->prev = (struct rt_memheap_item *)start_addr;
  96. /* not in free list */
  97. item->next_free = item->prev_free = RT_NULL;
  98. /* initialize semaphore lock */
  99. rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_FIFO);
  100. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  101. ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
  102. start_addr, size, &(memheap->free_header)));
  103. return RT_EOK;
  104. }
  105. RTM_EXPORT(rt_memheap_init);
  106. rt_err_t rt_memheap_detach(struct rt_memheap *heap)
  107. {
  108. RT_ASSERT(heap);
  109. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  110. RT_ASSERT(rt_object_is_systemobject(&heap->parent));
  111. rt_sem_detach(&heap->lock);
  112. rt_object_detach(&(heap->parent));
  113. /* Return a successful completion. */
  114. return RT_EOK;
  115. }
  116. RTM_EXPORT(rt_memheap_detach);
  117. void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
  118. {
  119. rt_err_t result;
  120. rt_uint32_t free_size;
  121. struct rt_memheap_item *header_ptr;
  122. RT_ASSERT(heap != RT_NULL);
  123. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  124. /* align allocated size */
  125. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  126. if (size < RT_MEMHEAP_MINIALLOC)
  127. size = RT_MEMHEAP_MINIALLOC;
  128. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
  129. size, RT_NAME_MAX, heap->parent.name));
  130. if (size < heap->available_size)
  131. {
  132. /* search on free list */
  133. free_size = 0;
  134. /* lock memheap */
  135. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  136. if (result != RT_EOK)
  137. {
  138. rt_set_errno(result);
  139. return RT_NULL;
  140. }
  141. /* get the first free memory block */
  142. header_ptr = heap->free_list->next_free;
  143. while (header_ptr != heap->free_list && free_size < size)
  144. {
  145. /* get current freed memory block size */
  146. free_size = MEMITEM_SIZE(header_ptr);
  147. if (free_size < size)
  148. {
  149. /* move to next free memory block */
  150. header_ptr = header_ptr->next_free;
  151. }
  152. }
  153. /* determine if the memory is available. */
  154. if (free_size >= size)
  155. {
  156. /* a block that satisfies the request has been found. */
  157. /* determine if the block needs to be split. */
  158. if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
  159. {
  160. struct rt_memheap_item *new_ptr;
  161. /* split the block. */
  162. new_ptr = (struct rt_memheap_item *)
  163. (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
  164. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  165. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  166. header_ptr,
  167. header_ptr->next,
  168. header_ptr->prev,
  169. new_ptr));
  170. /* mark the new block as a memory block and freed. */
  171. new_ptr->magic = RT_MEMHEAP_MAGIC;
  172. /* put the pool pointer into the new block. */
  173. new_ptr->pool_ptr = heap;
  174. #ifdef RT_USING_MEMTRACE
  175. rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name));
  176. #endif
  177. /* break down the block list */
  178. new_ptr->prev = header_ptr;
  179. new_ptr->next = header_ptr->next;
  180. header_ptr->next->prev = new_ptr;
  181. header_ptr->next = new_ptr;
  182. /* remove header ptr from free list */
  183. header_ptr->next_free->prev_free = header_ptr->prev_free;
  184. header_ptr->prev_free->next_free = header_ptr->next_free;
  185. header_ptr->next_free = RT_NULL;
  186. header_ptr->prev_free = RT_NULL;
  187. /* insert new_ptr to free list */
  188. new_ptr->next_free = heap->free_list->next_free;
  189. new_ptr->prev_free = heap->free_list;
  190. heap->free_list->next_free->prev_free = new_ptr;
  191. heap->free_list->next_free = new_ptr;
  192. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
  193. new_ptr->next_free,
  194. new_ptr->prev_free));
  195. /* decrement the available byte count. */
  196. heap->available_size = heap->available_size -
  197. size -
  198. RT_MEMHEAP_SIZE;
  199. if (heap->pool_size - heap->available_size > heap->max_used_size)
  200. heap->max_used_size = heap->pool_size - heap->available_size;
  201. }
  202. else
  203. {
  204. /* decrement the entire free size from the available bytes count. */
  205. heap->available_size = heap->available_size - free_size;
  206. if (heap->pool_size - heap->available_size > heap->max_used_size)
  207. heap->max_used_size = heap->pool_size - heap->available_size;
  208. /* remove header_ptr from free list */
  209. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  210. ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
  211. header_ptr,
  212. header_ptr->next_free,
  213. header_ptr->prev_free));
  214. header_ptr->next_free->prev_free = header_ptr->prev_free;
  215. header_ptr->prev_free->next_free = header_ptr->next_free;
  216. header_ptr->next_free = RT_NULL;
  217. header_ptr->prev_free = RT_NULL;
  218. }
  219. /* Mark the allocated block as not available. */
  220. header_ptr->magic |= RT_MEMHEAP_USED;
  221. #ifdef RT_USING_MEMTRACE
  222. if (rt_thread_self())
  223. rt_memcpy(header_ptr->owner_thread_name, rt_thread_self()->name, sizeof(header_ptr->owner_thread_name));
  224. else
  225. rt_memcpy(header_ptr->owner_thread_name, "NONE", sizeof(header_ptr->owner_thread_name));
  226. #endif
  227. /* release lock */
  228. rt_sem_release(&(heap->lock));
  229. /* Return a memory address to the caller. */
  230. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  231. ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
  232. (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
  233. header_ptr,
  234. size));
  235. return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
  236. }
  237. /* release lock */
  238. rt_sem_release(&(heap->lock));
  239. }
  240. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
  241. /* Return the completion status. */
  242. return RT_NULL;
  243. }
  244. RTM_EXPORT(rt_memheap_alloc);
  245. void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
  246. {
  247. rt_err_t result;
  248. rt_size_t oldsize;
  249. struct rt_memheap_item *header_ptr;
  250. struct rt_memheap_item *new_ptr;
  251. RT_ASSERT(heap);
  252. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  253. if (newsize == 0)
  254. {
  255. rt_memheap_free(ptr);
  256. return RT_NULL;
  257. }
  258. /* align allocated size */
  259. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  260. if (newsize < RT_MEMHEAP_MINIALLOC)
  261. newsize = RT_MEMHEAP_MINIALLOC;
  262. if (ptr == RT_NULL)
  263. {
  264. return rt_memheap_alloc(heap, newsize);
  265. }
  266. /* get memory block header and get the size of memory block */
  267. header_ptr = (struct rt_memheap_item *)
  268. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  269. oldsize = MEMITEM_SIZE(header_ptr);
  270. /* re-allocate memory */
  271. if (newsize > oldsize)
  272. {
  273. void *new_ptr;
  274. struct rt_memheap_item *next_ptr;
  275. /* lock memheap */
  276. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  277. if (result != RT_EOK)
  278. {
  279. rt_set_errno(result);
  280. return RT_NULL;
  281. }
  282. next_ptr = header_ptr->next;
  283. /* header_ptr should not be the tail */
  284. RT_ASSERT(next_ptr > header_ptr);
  285. /* check whether the following free space is enough to expand */
  286. if (!RT_MEMHEAP_IS_USED(next_ptr))
  287. {
  288. rt_int32_t nextsize;
  289. nextsize = MEMITEM_SIZE(next_ptr);
  290. RT_ASSERT(next_ptr > 0);
  291. /* Here is the ASCII art of the situation that we can make use of
  292. * the next free node without alloc/memcpy, |*| is the control
  293. * block:
  294. *
  295. * oldsize free node
  296. * |*|-----------|*|----------------------|*|
  297. * newsize >= minialloc
  298. * |*|----------------|*|-----------------|*|
  299. */
  300. if (nextsize + oldsize > newsize + RT_MEMHEAP_MINIALLOC)
  301. {
  302. /* decrement the entire free size from the available bytes count. */
  303. heap->available_size = heap->available_size - (newsize - oldsize);
  304. if (heap->pool_size - heap->available_size > heap->max_used_size)
  305. heap->max_used_size = heap->pool_size - heap->available_size;
  306. /* remove next_ptr from free list */
  307. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  308. ("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
  309. next_ptr,
  310. next_ptr->next_free,
  311. next_ptr->prev_free));
  312. next_ptr->next_free->prev_free = next_ptr->prev_free;
  313. next_ptr->prev_free->next_free = next_ptr->next_free;
  314. next_ptr->next->prev = next_ptr->prev;
  315. next_ptr->prev->next = next_ptr->next;
  316. /* build a new one on the right place */
  317. next_ptr = (struct rt_memheap_item *)((char *)ptr + newsize);
  318. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  319. ("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
  320. next_ptr,
  321. next_ptr->next,
  322. next_ptr->prev));
  323. /* mark the new block as a memory block and freed. */
  324. next_ptr->magic = RT_MEMHEAP_MAGIC;
  325. /* put the pool pointer into the new block. */
  326. next_ptr->pool_ptr = heap;
  327. #ifdef RT_USING_MEMTRACE
  328. rt_memset(next_ptr->owner_thread_name, ' ', sizeof(next_ptr->owner_thread_name));
  329. #endif
  330. next_ptr->prev = header_ptr;
  331. next_ptr->next = header_ptr->next;
  332. header_ptr->next->prev = next_ptr;
  333. header_ptr->next = next_ptr;
  334. /* insert next_ptr to free list */
  335. next_ptr->next_free = heap->free_list->next_free;
  336. next_ptr->prev_free = heap->free_list;
  337. heap->free_list->next_free->prev_free = next_ptr;
  338. heap->free_list->next_free = next_ptr;
  339. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
  340. next_ptr->next_free,
  341. next_ptr->prev_free));
  342. /* release lock */
  343. rt_sem_release(&(heap->lock));
  344. return ptr;
  345. }
  346. }
  347. /* release lock */
  348. rt_sem_release(&(heap->lock));
  349. /* re-allocate a memory block */
  350. new_ptr = (void *)rt_memheap_alloc(heap, newsize);
  351. if (new_ptr != RT_NULL)
  352. {
  353. rt_memcpy(new_ptr, ptr, oldsize < newsize ? oldsize : newsize);
  354. rt_memheap_free(ptr);
  355. }
  356. return new_ptr;
  357. }
  358. /* don't split when there is less than one node space left */
  359. if (newsize + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC >= oldsize)
  360. return ptr;
  361. /* lock memheap */
  362. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  363. if (result != RT_EOK)
  364. {
  365. rt_set_errno(result);
  366. return RT_NULL;
  367. }
  368. /* split the block. */
  369. new_ptr = (struct rt_memheap_item *)
  370. (((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);
  371. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  372. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  373. header_ptr,
  374. header_ptr->next,
  375. header_ptr->prev,
  376. new_ptr));
  377. /* mark the new block as a memory block and freed. */
  378. new_ptr->magic = RT_MEMHEAP_MAGIC;
  379. /* put the pool pointer into the new block. */
  380. new_ptr->pool_ptr = heap;
  381. #ifdef RT_USING_MEMTRACE
  382. rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name));
  383. #endif
  384. /* break down the block list */
  385. new_ptr->prev = header_ptr;
  386. new_ptr->next = header_ptr->next;
  387. header_ptr->next->prev = new_ptr;
  388. header_ptr->next = new_ptr;
  389. /* determine if the block can be merged with the next neighbor. */
  390. if (!RT_MEMHEAP_IS_USED(new_ptr->next))
  391. {
  392. struct rt_memheap_item *free_ptr;
  393. /* merge block with next neighbor. */
  394. free_ptr = new_ptr->next;
  395. heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);
  396. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  397. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  398. header_ptr, header_ptr->next_free, header_ptr->prev_free));
  399. free_ptr->next->prev = new_ptr;
  400. new_ptr->next = free_ptr->next;
  401. /* remove free ptr from free list */
  402. free_ptr->next_free->prev_free = free_ptr->prev_free;
  403. free_ptr->prev_free->next_free = free_ptr->next_free;
  404. }
  405. /* insert the split block to free list */
  406. new_ptr->next_free = heap->free_list->next_free;
  407. new_ptr->prev_free = heap->free_list;
  408. heap->free_list->next_free->prev_free = new_ptr;
  409. heap->free_list->next_free = new_ptr;
  410. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
  411. new_ptr->next_free,
  412. new_ptr->prev_free));
  413. /* increment the available byte count. */
  414. heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
  415. /* release lock */
  416. rt_sem_release(&(heap->lock));
  417. /* return the old memory block */
  418. return ptr;
  419. }
  420. RTM_EXPORT(rt_memheap_realloc);
  421. void rt_memheap_free(void *ptr)
  422. {
  423. rt_err_t result;
  424. struct rt_memheap *heap;
  425. struct rt_memheap_item *header_ptr, *new_ptr;
  426. rt_uint32_t insert_header;
  427. /* NULL check */
  428. if (ptr == RT_NULL) return;
  429. /* set initial status as OK */
  430. insert_header = 1;
  431. new_ptr = RT_NULL;
  432. header_ptr = (struct rt_memheap_item *)
  433. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  434. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
  435. ptr, header_ptr));
  436. /* check magic */
  437. RT_ASSERT((header_ptr->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
  438. RT_ASSERT(header_ptr->magic & RT_MEMHEAP_USED);
  439. /* check whether this block of memory has been over-written. */
  440. RT_ASSERT((header_ptr->next->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
  441. /* get pool ptr */
  442. heap = header_ptr->pool_ptr;
  443. RT_ASSERT(heap);
  444. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  445. /* lock memheap */
  446. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  447. if (result != RT_EOK)
  448. {
  449. rt_set_errno(result);
  450. return ;
  451. }
  452. /* Mark the memory as available. */
  453. header_ptr->magic &= ~RT_MEMHEAP_USED;
  454. /* Adjust the available number of bytes. */
  455. heap->available_size = heap->available_size + MEMITEM_SIZE(header_ptr);
  456. /* Determine if the block can be merged with the previous neighbor. */
  457. if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
  458. {
  459. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
  460. header_ptr->prev));
  461. /* adjust the available number of bytes. */
  462. heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
  463. /* yes, merge block with previous neighbor. */
  464. (header_ptr->prev)->next = header_ptr->next;
  465. (header_ptr->next)->prev = header_ptr->prev;
  466. /* move header pointer to previous. */
  467. header_ptr = header_ptr->prev;
  468. /* don't insert header to free list */
  469. insert_header = 0;
  470. }
  471. /* determine if the block can be merged with the next neighbor. */
  472. if (!RT_MEMHEAP_IS_USED(header_ptr->next))
  473. {
  474. /* adjust the available number of bytes. */
  475. heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
  476. /* merge block with next neighbor. */
  477. new_ptr = header_ptr->next;
  478. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  479. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  480. new_ptr, new_ptr->next_free, new_ptr->prev_free));
  481. new_ptr->next->prev = header_ptr;
  482. header_ptr->next = new_ptr->next;
  483. /* remove new ptr from free list */
  484. new_ptr->next_free->prev_free = new_ptr->prev_free;
  485. new_ptr->prev_free->next_free = new_ptr->next_free;
  486. }
  487. if (insert_header)
  488. {
  489. /* no left merge, insert to free list */
  490. header_ptr->next_free = heap->free_list->next_free;
  491. header_ptr->prev_free = heap->free_list;
  492. heap->free_list->next_free->prev_free = header_ptr;
  493. heap->free_list->next_free = header_ptr;
  494. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  495. ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
  496. header_ptr->next_free, header_ptr->prev_free));
  497. }
  498. #ifdef RT_USING_MEMTRACE
  499. rt_memset(header_ptr->owner_thread_name, ' ', sizeof(header_ptr->owner_thread_name));
  500. #endif
  501. /* release lock */
  502. rt_sem_release(&(heap->lock));
  503. }
  504. RTM_EXPORT(rt_memheap_free);
  505. #ifdef RT_USING_MEMHEAP_AS_HEAP
  506. static struct rt_memheap _heap;
  507. void rt_system_heap_init(void *begin_addr, void *end_addr)
  508. {
  509. /* initialize a default heap in the system */
  510. rt_memheap_init(&_heap,
  511. "heap",
  512. begin_addr,
  513. (rt_uint32_t)end_addr - (rt_uint32_t)begin_addr);
  514. }
  515. void *rt_malloc(rt_size_t size)
  516. {
  517. void *ptr;
  518. /* try to allocate in system heap */
  519. ptr = rt_memheap_alloc(&_heap, size);
  520. if (ptr == RT_NULL)
  521. {
  522. struct rt_object *object;
  523. struct rt_list_node *node;
  524. struct rt_memheap *heap;
  525. struct rt_object_information *information;
  526. /* try to allocate on other memory heap */
  527. information = rt_object_get_information(RT_Object_Class_MemHeap);
  528. RT_ASSERT(information != RT_NULL);
  529. for (node = information->object_list.next;
  530. node != &(information->object_list);
  531. node = node->next)
  532. {
  533. object = rt_list_entry(node, struct rt_object, list);
  534. heap = (struct rt_memheap *)object;
  535. RT_ASSERT(heap);
  536. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  537. /* not allocate in the default system heap */
  538. if (heap == &_heap)
  539. continue;
  540. ptr = rt_memheap_alloc(heap, size);
  541. if (ptr != RT_NULL)
  542. break;
  543. }
  544. }
  545. return ptr;
  546. }
  547. RTM_EXPORT(rt_malloc);
  548. void rt_free(void *rmem)
  549. {
  550. rt_memheap_free(rmem);
  551. }
  552. RTM_EXPORT(rt_free);
  553. void *rt_realloc(void *rmem, rt_size_t newsize)
  554. {
  555. void *new_ptr;
  556. struct rt_memheap_item *header_ptr;
  557. if (rmem == RT_NULL)
  558. return rt_malloc(newsize);
  559. if (newsize == 0)
  560. {
  561. rt_free(rmem);
  562. return RT_NULL;
  563. }
  564. /* get old memory item */
  565. header_ptr = (struct rt_memheap_item *)
  566. ((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);
  567. new_ptr = rt_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
  568. if (new_ptr == RT_NULL && newsize != 0)
  569. {
  570. /* allocate memory block from other memheap */
  571. new_ptr = rt_malloc(newsize);
  572. if (new_ptr != RT_NULL && rmem != RT_NULL)
  573. {
  574. rt_size_t oldsize;
  575. /* get the size of old memory block */
  576. oldsize = MEMITEM_SIZE(header_ptr);
  577. if (newsize > oldsize)
  578. rt_memcpy(new_ptr, rmem, oldsize);
  579. else
  580. rt_memcpy(new_ptr, rmem, newsize);
  581. rt_free(rmem);
  582. }
  583. }
  584. return new_ptr;
  585. }
  586. RTM_EXPORT(rt_realloc);
  587. void *rt_calloc(rt_size_t count, rt_size_t size)
  588. {
  589. void *ptr;
  590. rt_size_t total_size;
  591. total_size = count * size;
  592. ptr = rt_malloc(total_size);
  593. if (ptr != RT_NULL)
  594. {
  595. /* clean memory */
  596. rt_memset(ptr, 0, total_size);
  597. }
  598. return ptr;
  599. }
  600. RTM_EXPORT(rt_calloc);
  601. void rt_memory_info(rt_uint32_t *total,
  602. rt_uint32_t *used,
  603. rt_uint32_t *max_used)
  604. {
  605. if (total != RT_NULL)
  606. *total = _heap.pool_size;
  607. if (used != RT_NULL)
  608. *used = _heap.pool_size - _heap.available_size;
  609. if (max_used != RT_NULL)
  610. *max_used = _heap.max_used_size;
  611. }
  612. #endif
  613. #ifdef RT_USING_MEMTRACE
  614. void dump_used_memheap(struct rt_memheap *mh)
  615. {
  616. struct rt_memheap_item *header_ptr;
  617. rt_uint32_t block_size;
  618. rt_kprintf("\nmemory heap address:\n");
  619. rt_kprintf("heap_ptr: 0x%08x\n", mh->start_addr);
  620. rt_kprintf("free : 0x%08x\n", mh->available_size);
  621. rt_kprintf("max_used: 0x%08x\n", mh->max_used_size);
  622. rt_kprintf("size : 0x%08x\n", mh->pool_size);
  623. rt_kprintf("\n--memory used information --\n");
  624. header_ptr = mh->block_list;
  625. while (header_ptr->next != mh->block_list)
  626. {
  627. if ((header_ptr->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC)
  628. {
  629. rt_kprintf("[0x%08x - incorrect magic: 0x%08x\n", header_ptr, header_ptr->magic);
  630. break;
  631. }
  632. /* get current memory block size */
  633. block_size = MEMITEM_SIZE(header_ptr);
  634. if (block_size < 0)
  635. break;
  636. if (RT_MEMHEAP_IS_USED(header_ptr))
  637. {
  638. /* dump information */
  639. rt_kprintf("[0x%08x - %d - %c%c%c%c] used\n", header_ptr, block_size,
  640. header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
  641. header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
  642. }
  643. else
  644. {
  645. /* dump information */
  646. rt_kprintf("[0x%08x - %d - %c%c%c%c] free\n", header_ptr, block_size,
  647. header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
  648. header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
  649. }
  650. /* move to next used memory block */
  651. header_ptr = header_ptr->next;
  652. }
  653. }
  654. void memtrace_heap()
  655. {
  656. struct rt_object_information *info;
  657. struct rt_list_node *list;
  658. struct rt_memheap *mh;
  659. struct rt_list_node *node;
  660. info = rt_object_get_information(RT_Object_Class_MemHeap);
  661. list = &info->object_list;
  662. for (node = list->next; node != list; node = node->next)
  663. {
  664. mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);
  665. dump_used_memheap(mh);
  666. }
  667. }
  668. #ifdef RT_USING_FINSH
  669. #include <finsh.h>
  670. MSH_CMD_EXPORT(memtrace_heap, dump memory trace for heap);
  671. #endif /* end of RT_USING_FINSH */
  672. #endif /* end of RT_USING_MEMTRACE */
  673. #endif /* end of RT_USING_MEMHEAP */