memheap.c 32 KB

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