memheap.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. #ifdef RT_USING_MEMHEAP_AUTO_BINDING
  711. if (ptr == RT_NULL)
  712. {
  713. struct rt_object *object;
  714. struct rt_list_node *node;
  715. struct rt_memheap *heap;
  716. struct rt_object_information *information;
  717. /* try to allocate on other memory heap */
  718. information = rt_object_get_information(RT_Object_Class_MemHeap);
  719. RT_ASSERT(information != RT_NULL);
  720. for (node = information->object_list.next;
  721. node != &(information->object_list);
  722. node = node->next)
  723. {
  724. object = rt_list_entry(node, struct rt_object, list);
  725. heap = (struct rt_memheap *)object;
  726. RT_ASSERT(heap);
  727. RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
  728. /* not allocate in the default system heap */
  729. if (heap == &_heap)
  730. continue;
  731. ptr = rt_memheap_alloc(heap, size);
  732. if (ptr != RT_NULL)
  733. break;
  734. }
  735. }
  736. #endif /* RT_USING_MEMHEAP_AUTO_BINDING */
  737. #ifdef RT_USING_MEMTRACE
  738. if (ptr == RT_NULL)
  739. {
  740. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("malloc[%d] => NULL", size));
  741. }
  742. else
  743. {
  744. struct rt_memheap_item *item = MEMITEM(ptr);
  745. if (rt_thread_self())
  746. rt_memheap_setname(item, rt_thread_self()->name);
  747. else
  748. rt_memheap_setname(item, "<null>");
  749. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("malloc => 0x%08x : %d", ptr, size));
  750. }
  751. #endif /* RT_USING_MEMTRACE */
  752. return ptr;
  753. }
  754. RTM_EXPORT(rt_malloc);
  755. /**
  756. * @brief This function will release the previously allocated memory block by
  757. * rt_malloc. The released memory block is taken back to system heap.
  758. *
  759. * @param rmem the address of memory which will be released.
  760. */
  761. void rt_free(void *rmem)
  762. {
  763. rt_memheap_free(rmem);
  764. }
  765. RTM_EXPORT(rt_free);
  766. /**
  767. * @brief This function will change the size of previously allocated memory block.
  768. *
  769. * @param rmem is the pointer to memory allocated by rt_malloc.
  770. *
  771. * @param newsize is the required new size.
  772. *
  773. * @return the changed memory block address.
  774. */
  775. void *rt_realloc(void *rmem, rt_size_t newsize)
  776. {
  777. void *new_ptr;
  778. struct rt_memheap_item *header_ptr;
  779. if (rmem == RT_NULL)
  780. return rt_malloc(newsize);
  781. if (newsize == 0)
  782. {
  783. rt_free(rmem);
  784. return RT_NULL;
  785. }
  786. /* get old memory item */
  787. header_ptr = (struct rt_memheap_item *)
  788. ((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);
  789. new_ptr = rt_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
  790. if (new_ptr == RT_NULL && newsize != 0)
  791. {
  792. /* allocate memory block from other memheap */
  793. new_ptr = rt_malloc(newsize);
  794. if (new_ptr != RT_NULL && rmem != RT_NULL)
  795. {
  796. rt_size_t oldsize;
  797. /* get the size of old memory block */
  798. oldsize = MEMITEM_SIZE(header_ptr);
  799. if (newsize > oldsize)
  800. rt_memcpy(new_ptr, rmem, oldsize);
  801. else
  802. rt_memcpy(new_ptr, rmem, newsize);
  803. rt_free(rmem);
  804. }
  805. }
  806. #ifdef RT_USING_MEMTRACE
  807. if (new_ptr == RT_NULL)
  808. {
  809. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("realloc[%d] => NULL", newsize));
  810. }
  811. else
  812. {
  813. struct rt_memheap_item *item = MEMITEM(new_ptr);
  814. if (rt_thread_self())
  815. rt_memheap_setname(item, rt_thread_self()->name);
  816. else
  817. rt_memheap_setname(item, "<null>");
  818. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("realloc => 0x%08x : %d",
  819. new_ptr, newsize));
  820. }
  821. #endif /* RT_USING_MEMTRACE */
  822. return new_ptr;
  823. }
  824. RTM_EXPORT(rt_realloc);
  825. /**
  826. * @brief This function will contiguously allocate enough space for count objects
  827. * that are size bytes of memory each and returns a pointer to the allocated
  828. * memory.
  829. *
  830. * @note The allocated memory is filled with bytes of value zero.
  831. *
  832. * @param count is the number of objects to allocate.
  833. *
  834. * @param size is the size of one object to allocate.
  835. *
  836. * @return pointer to allocated memory pointer.
  837. */
  838. void *rt_calloc(rt_size_t count, rt_size_t size)
  839. {
  840. void *ptr;
  841. rt_size_t total_size;
  842. total_size = count * size;
  843. ptr = rt_malloc(total_size);
  844. if (ptr != RT_NULL)
  845. {
  846. /* clean memory */
  847. rt_memset(ptr, 0, total_size);
  848. }
  849. #ifdef RT_USING_MEMTRACE
  850. if (ptr == RT_NULL)
  851. {
  852. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("calloc[%d x %d] => NULL",
  853. count, size));
  854. }
  855. else
  856. {
  857. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("calloc => 0x%08x : %d",
  858. ptr, count * size));
  859. }
  860. #endif /* RT_USING_MEMTRACE */
  861. return ptr;
  862. }
  863. RTM_EXPORT(rt_calloc);
  864. /**
  865. * @brief This function will caculate the total memory, the used memory, and
  866. * the max used memory.
  867. *
  868. * @param total is a pointer to get the total size of the memory.
  869. *
  870. * @param used is a pointer to get the size of memory used.
  871. *
  872. * @param max_used is a pointer to get the maximum memory used.
  873. */
  874. void rt_memory_info(rt_uint32_t *total,
  875. rt_uint32_t *used,
  876. rt_uint32_t *max_used)
  877. {
  878. if (total != RT_NULL)
  879. *total = _heap.pool_size;
  880. if (used != RT_NULL)
  881. *used = _heap.pool_size - _heap.available_size;
  882. if (max_used != RT_NULL)
  883. *max_used = _heap.max_used_size;
  884. }
  885. #endif /* RT_USING_MEMHEAP_AS_HEAP */
  886. #ifdef RT_USING_MEMTRACE
  887. /**
  888. * @brief This function will print the used memheap infomation.
  889. *
  890. * @param mh is a pointer of the memheap object.
  891. */
  892. void dump_used_memheap(struct rt_memheap *mh)
  893. {
  894. struct rt_memheap_item *header_ptr;
  895. rt_uint32_t block_size;
  896. rt_kprintf("\nmemory heap address:\n");
  897. rt_kprintf("heap_ptr: 0x%08x\n", mh->start_addr);
  898. rt_kprintf("free : 0x%08x\n", mh->available_size);
  899. rt_kprintf("max_used: 0x%08x\n", mh->max_used_size);
  900. rt_kprintf("size : 0x%08x\n", mh->pool_size);
  901. rt_kprintf("\n--memory used information --\n");
  902. header_ptr = mh->block_list;
  903. while (header_ptr->next != mh->block_list)
  904. {
  905. if ((header_ptr->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC)
  906. {
  907. rt_kprintf("[0x%08x - incorrect magic: 0x%08x\n", header_ptr, header_ptr->magic);
  908. break;
  909. }
  910. /* get current memory block size */
  911. block_size = MEMITEM_SIZE(header_ptr);
  912. if (block_size < 0)
  913. break;
  914. if (RT_MEMHEAP_IS_USED(header_ptr))
  915. {
  916. /* dump information */
  917. rt_kprintf("[0x%08x - %d - %c%c%c%c] used\n", header_ptr, block_size,
  918. header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
  919. header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
  920. }
  921. else
  922. {
  923. /* dump information */
  924. rt_kprintf("[0x%08x - %d - %c%c%c%c] free\n", header_ptr, block_size,
  925. header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
  926. header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
  927. }
  928. /* move to next used memory block */
  929. header_ptr = header_ptr->next;
  930. }
  931. }
  932. void memtrace_heap()
  933. {
  934. struct rt_object_information *info;
  935. struct rt_list_node *list;
  936. struct rt_memheap *mh;
  937. struct rt_list_node *node;
  938. info = rt_object_get_information(RT_Object_Class_MemHeap);
  939. list = &info->object_list;
  940. for (node = list->next; node != list; node = node->next)
  941. {
  942. mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);
  943. dump_used_memheap(mh);
  944. }
  945. }
  946. #ifdef RT_USING_FINSH
  947. #include <finsh.h>
  948. MSH_CMD_EXPORT(memtrace_heap, dump memory trace for heap);
  949. #endif /* RT_USING_FINSH */
  950. #endif /* RT_USING_MEMTRACE */
  951. #endif /* RT_USING_MEMHEAP */