mem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2008-7-12 Bernard the first version
  9. * 2010-06-09 Bernard fix the end stub of heap
  10. * fix memory check in rt_realloc function
  11. * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
  12. * 2010-10-14 Bernard fix rt_realloc issue when realloc a NULL pointer.
  13. * 2017-07-14 armink fix rt_realloc issue when new size is 0
  14. */
  15. /*
  16. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without modification,
  20. * are permitted provided that the following conditions are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright notice,
  23. * this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright notice,
  25. * this list of conditions and the following disclaimer in the documentation
  26. * and/or other materials provided with the distribution.
  27. * 3. The name of the author may not be used to endorse or promote products
  28. * derived from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  31. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  32. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  33. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  35. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  38. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  39. * OF SUCH DAMAGE.
  40. *
  41. * This file is part of the lwIP TCP/IP stack.
  42. *
  43. * Author: Adam Dunkels <adam@sics.se>
  44. * Simon Goldschmidt
  45. *
  46. */
  47. #include <rthw.h>
  48. #include <rtthread.h>
  49. #ifndef RT_USING_MEMHEAP_AS_HEAP
  50. /* #define RT_MEM_DEBUG */
  51. #define RT_MEM_STATS
  52. #if defined (RT_USING_HEAP) && defined (RT_USING_SMALL_MEM)
  53. #ifdef RT_USING_HOOK
  54. static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
  55. static void (*rt_free_hook)(void *ptr);
  56. /**
  57. * @addtogroup Hook
  58. */
  59. /**@{*/
  60. /**
  61. * This function will set a hook function, which will be invoked when a memory
  62. * block is allocated from heap memory.
  63. *
  64. * @param hook the hook function
  65. */
  66. void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
  67. {
  68. rt_malloc_hook = hook;
  69. }
  70. /**
  71. * This function will set a hook function, which will be invoked when a memory
  72. * block is released to heap memory.
  73. *
  74. * @param hook the hook function
  75. */
  76. void rt_free_sethook(void (*hook)(void *ptr))
  77. {
  78. rt_free_hook = hook;
  79. }
  80. /**@}*/
  81. #endif
  82. #define HEAP_MAGIC 0x1ea0
  83. struct heap_mem
  84. {
  85. /* magic and used flag */
  86. rt_uint16_t magic;
  87. rt_uint16_t used;
  88. rt_size_t next, prev;
  89. #ifdef RT_USING_MEMTRACE
  90. rt_uint8_t thread[4]; /* thread name */
  91. #endif
  92. };
  93. /** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
  94. static rt_uint8_t *heap_ptr;
  95. /** the last entry, always unused! */
  96. static struct heap_mem *heap_end;
  97. #define MIN_SIZE 12
  98. #define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE)
  99. #define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE)
  100. static struct heap_mem *lfree; /* pointer to the lowest free block */
  101. static struct rt_semaphore heap_sem;
  102. static rt_size_t mem_size_aligned;
  103. #ifdef RT_MEM_STATS
  104. static rt_size_t used_mem, max_mem;
  105. #endif
  106. #ifdef RT_USING_MEMTRACE
  107. rt_inline void rt_mem_setname(struct heap_mem *mem, const char *name)
  108. {
  109. int index;
  110. for (index = 0; index < sizeof(mem->thread); index ++)
  111. {
  112. if (name[index] == '\0') break;
  113. mem->thread[index] = name[index];
  114. }
  115. for (; index < sizeof(mem->thread); index ++)
  116. {
  117. mem->thread[index] = ' ';
  118. }
  119. }
  120. #endif
  121. static void plug_holes(struct heap_mem *mem)
  122. {
  123. struct heap_mem *nmem;
  124. struct heap_mem *pmem;
  125. RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);
  126. RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
  127. RT_ASSERT(mem->used == 0);
  128. /* plug hole forward */
  129. nmem = (struct heap_mem *)&heap_ptr[mem->next];
  130. if (mem != nmem &&
  131. nmem->used == 0 &&
  132. (rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
  133. {
  134. /* if mem->next is unused and not end of heap_ptr,
  135. * combine mem and mem->next
  136. */
  137. if (lfree == nmem)
  138. {
  139. lfree = mem;
  140. }
  141. mem->next = nmem->next;
  142. ((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
  143. }
  144. /* plug hole backward */
  145. pmem = (struct heap_mem *)&heap_ptr[mem->prev];
  146. if (pmem != mem && pmem->used == 0)
  147. {
  148. /* if mem->prev is unused, combine mem and mem->prev */
  149. if (lfree == mem)
  150. {
  151. lfree = pmem;
  152. }
  153. pmem->next = mem->next;
  154. ((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
  155. }
  156. }
  157. /**
  158. * @ingroup SystemInit
  159. *
  160. * This function will initialize system heap memory.
  161. *
  162. * @param begin_addr the beginning address of system heap memory.
  163. * @param end_addr the end address of system heap memory.
  164. */
  165. void rt_system_heap_init(void *begin_addr, void *end_addr)
  166. {
  167. struct heap_mem *mem;
  168. rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
  169. rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);
  170. RT_DEBUG_NOT_IN_INTERRUPT;
  171. /* alignment addr */
  172. if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
  173. ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
  174. {
  175. /* calculate the aligned memory size */
  176. mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
  177. }
  178. else
  179. {
  180. rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
  181. (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
  182. return;
  183. }
  184. /* point to begin address of heap */
  185. heap_ptr = (rt_uint8_t *)begin_align;
  186. RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
  187. (rt_uint32_t)heap_ptr, mem_size_aligned));
  188. /* initialize the start of the heap */
  189. mem = (struct heap_mem *)heap_ptr;
  190. mem->magic = HEAP_MAGIC;
  191. mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
  192. mem->prev = 0;
  193. mem->used = 0;
  194. #ifdef RT_USING_MEMTRACE
  195. rt_mem_setname(mem, "INIT");
  196. #endif
  197. /* initialize the end of the heap */
  198. heap_end = (struct heap_mem *)&heap_ptr[mem->next];
  199. heap_end->magic = HEAP_MAGIC;
  200. heap_end->used = 1;
  201. heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
  202. heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
  203. #ifdef RT_USING_MEMTRACE
  204. rt_mem_setname(heap_end, "INIT");
  205. #endif
  206. rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
  207. /* initialize the lowest-free pointer to the start of the heap */
  208. lfree = (struct heap_mem *)heap_ptr;
  209. }
  210. /**
  211. * @addtogroup MM
  212. */
  213. /**@{*/
  214. /**
  215. * Allocate a block of memory with a minimum of 'size' bytes.
  216. *
  217. * @param size is the minimum size of the requested block in bytes.
  218. *
  219. * @return pointer to allocated memory or NULL if no free memory was found.
  220. */
  221. void *rt_malloc(rt_size_t size)
  222. {
  223. rt_size_t ptr, ptr2;
  224. struct heap_mem *mem, *mem2;
  225. if (size == 0)
  226. return RT_NULL;
  227. RT_DEBUG_NOT_IN_INTERRUPT;
  228. if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
  229. RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
  230. size, RT_ALIGN(size, RT_ALIGN_SIZE)));
  231. else
  232. RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));
  233. /* alignment size */
  234. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  235. if (size > mem_size_aligned)
  236. {
  237. RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
  238. return RT_NULL;
  239. }
  240. /* every data block must be at least MIN_SIZE_ALIGNED long */
  241. if (size < MIN_SIZE_ALIGNED)
  242. size = MIN_SIZE_ALIGNED;
  243. /* take memory semaphore */
  244. rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
  245. for (ptr = (rt_uint8_t *)lfree - heap_ptr;
  246. ptr < mem_size_aligned - size;
  247. ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
  248. {
  249. mem = (struct heap_mem *)&heap_ptr[ptr];
  250. if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
  251. {
  252. /* mem is not used and at least perfect fit is possible:
  253. * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
  254. if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
  255. (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
  256. {
  257. /* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
  258. * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
  259. * -> split large block, create empty remainder,
  260. * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
  261. * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
  262. * struct heap_mem would fit in but no data between mem2 and mem2->next
  263. * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
  264. * region that couldn't hold data, but when mem->next gets freed,
  265. * the 2 regions would be combined, resulting in more free memory
  266. */
  267. ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
  268. /* create mem2 struct */
  269. mem2 = (struct heap_mem *)&heap_ptr[ptr2];
  270. mem2->magic = HEAP_MAGIC;
  271. mem2->used = 0;
  272. mem2->next = mem->next;
  273. mem2->prev = ptr;
  274. #ifdef RT_USING_MEMTRACE
  275. rt_mem_setname(mem2, " ");
  276. #endif
  277. /* and insert it between mem and mem->next */
  278. mem->next = ptr2;
  279. mem->used = 1;
  280. if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
  281. {
  282. ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
  283. }
  284. #ifdef RT_MEM_STATS
  285. used_mem += (size + SIZEOF_STRUCT_MEM);
  286. if (max_mem < used_mem)
  287. max_mem = used_mem;
  288. #endif
  289. }
  290. else
  291. {
  292. /* (a mem2 struct does no fit into the user data space of mem and mem->next will always
  293. * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
  294. * take care of this).
  295. * -> near fit or excact fit: do not split, no mem2 creation
  296. * also can't move mem->next directly behind mem, since mem->next
  297. * will always be used at this point!
  298. */
  299. mem->used = 1;
  300. #ifdef RT_MEM_STATS
  301. used_mem += mem->next - ((rt_uint8_t *)mem - heap_ptr);
  302. if (max_mem < used_mem)
  303. max_mem = used_mem;
  304. #endif
  305. }
  306. /* set memory block magic */
  307. mem->magic = HEAP_MAGIC;
  308. #ifdef RT_USING_MEMTRACE
  309. if (rt_thread_self())
  310. rt_mem_setname(mem, rt_thread_self()->name);
  311. else
  312. rt_mem_setname(mem, "NONE");
  313. #endif
  314. if (mem == lfree)
  315. {
  316. /* Find next free block after mem and update lowest free pointer */
  317. while (lfree->used && lfree != heap_end)
  318. lfree = (struct heap_mem *)&heap_ptr[lfree->next];
  319. RT_ASSERT(((lfree == heap_end) || (!lfree->used)));
  320. }
  321. rt_sem_release(&heap_sem);
  322. RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
  323. RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
  324. RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE - 1)) == 0);
  325. RT_DEBUG_LOG(RT_DEBUG_MEM,
  326. ("allocate memory at 0x%x, size: %d\n",
  327. (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
  328. (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
  329. RT_OBJECT_HOOK_CALL(rt_malloc_hook,
  330. (((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
  331. /* return the memory data except mem struct */
  332. return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
  333. }
  334. }
  335. rt_sem_release(&heap_sem);
  336. return RT_NULL;
  337. }
  338. RTM_EXPORT(rt_malloc);
  339. /**
  340. * This function will change the previously allocated memory block.
  341. *
  342. * @param rmem pointer to memory allocated by rt_malloc
  343. * @param newsize the required new size
  344. *
  345. * @return the changed memory block address
  346. */
  347. void *rt_realloc(void *rmem, rt_size_t newsize)
  348. {
  349. rt_size_t size;
  350. rt_size_t ptr, ptr2;
  351. struct heap_mem *mem, *mem2;
  352. void *nmem;
  353. RT_DEBUG_NOT_IN_INTERRUPT;
  354. /* alignment size */
  355. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  356. if (newsize > mem_size_aligned)
  357. {
  358. RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
  359. return RT_NULL;
  360. }
  361. else if (newsize == 0)
  362. {
  363. rt_free(rmem);
  364. return RT_NULL;
  365. }
  366. /* allocate a new memory block */
  367. if (rmem == RT_NULL)
  368. return rt_malloc(newsize);
  369. rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
  370. if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
  371. (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
  372. {
  373. /* illegal memory */
  374. rt_sem_release(&heap_sem);
  375. return rmem;
  376. }
  377. mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  378. ptr = (rt_uint8_t *)mem - heap_ptr;
  379. size = mem->next - ptr - SIZEOF_STRUCT_MEM;
  380. if (size == newsize)
  381. {
  382. /* the size is the same as */
  383. rt_sem_release(&heap_sem);
  384. return rmem;
  385. }
  386. if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
  387. {
  388. /* split memory block */
  389. #ifdef RT_MEM_STATS
  390. used_mem -= (size - newsize);
  391. #endif
  392. ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
  393. mem2 = (struct heap_mem *)&heap_ptr[ptr2];
  394. mem2->magic = HEAP_MAGIC;
  395. mem2->used = 0;
  396. mem2->next = mem->next;
  397. mem2->prev = ptr;
  398. #ifdef RT_USING_MEMTRACE
  399. rt_mem_setname(mem2, " ");
  400. #endif
  401. mem->next = ptr2;
  402. if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
  403. {
  404. ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
  405. }
  406. plug_holes(mem2);
  407. rt_sem_release(&heap_sem);
  408. return rmem;
  409. }
  410. rt_sem_release(&heap_sem);
  411. /* expand memory */
  412. nmem = rt_malloc(newsize);
  413. if (nmem != RT_NULL) /* check memory */
  414. {
  415. rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
  416. rt_free(rmem);
  417. }
  418. return nmem;
  419. }
  420. RTM_EXPORT(rt_realloc);
  421. /**
  422. * This function will contiguously allocate enough space for count objects
  423. * that are size bytes of memory each and returns a pointer to the allocated
  424. * memory.
  425. *
  426. * The allocated memory is filled with bytes of value zero.
  427. *
  428. * @param count number of objects to allocate
  429. * @param size size of the objects to allocate
  430. *
  431. * @return pointer to allocated memory / NULL pointer if there is an error
  432. */
  433. void *rt_calloc(rt_size_t count, rt_size_t size)
  434. {
  435. void *p;
  436. /* allocate 'count' objects of size 'size' */
  437. p = rt_malloc(count * size);
  438. /* zero the memory */
  439. if (p)
  440. rt_memset(p, 0, count * size);
  441. return p;
  442. }
  443. RTM_EXPORT(rt_calloc);
  444. /**
  445. * This function will release the previously allocated memory block by
  446. * rt_malloc. The released memory block is taken back to system heap.
  447. *
  448. * @param rmem the address of memory which will be released
  449. */
  450. void rt_free(void *rmem)
  451. {
  452. struct heap_mem *mem;
  453. if (rmem == RT_NULL)
  454. return;
  455. RT_DEBUG_NOT_IN_INTERRUPT;
  456. RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  457. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr &&
  458. (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);
  459. RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
  460. if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
  461. (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
  462. {
  463. RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));
  464. return;
  465. }
  466. /* Get the corresponding struct heap_mem ... */
  467. mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  468. RT_DEBUG_LOG(RT_DEBUG_MEM,
  469. ("release memory 0x%x, size: %d\n",
  470. (rt_uint32_t)rmem,
  471. (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
  472. /* protect the heap from concurrent access */
  473. rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
  474. /* ... which has to be in a used state ... */
  475. if (!mem->used || mem->magic != HEAP_MAGIC)
  476. {
  477. rt_kprintf("to free a bad data block:\n");
  478. rt_kprintf("mem: 0x%08x, used flag: %d, magic code: 0x%04x\n", mem, mem->used, mem->magic);
  479. }
  480. RT_ASSERT(mem->used);
  481. RT_ASSERT(mem->magic == HEAP_MAGIC);
  482. /* ... and is now unused. */
  483. mem->used = 0;
  484. mem->magic = HEAP_MAGIC;
  485. #ifdef RT_USING_MEMTRACE
  486. rt_mem_setname(mem, " ");
  487. #endif
  488. if (mem < lfree)
  489. {
  490. /* the newly freed struct is now the lowest */
  491. lfree = mem;
  492. }
  493. #ifdef RT_MEM_STATS
  494. used_mem -= (mem->next - ((rt_uint8_t *)mem - heap_ptr));
  495. #endif
  496. /* finally, see if prev or next are free also */
  497. plug_holes(mem);
  498. rt_sem_release(&heap_sem);
  499. }
  500. RTM_EXPORT(rt_free);
  501. #ifdef RT_MEM_STATS
  502. void rt_memory_info(rt_uint32_t *total,
  503. rt_uint32_t *used,
  504. rt_uint32_t *max_used)
  505. {
  506. if (total != RT_NULL)
  507. *total = mem_size_aligned;
  508. if (used != RT_NULL)
  509. *used = used_mem;
  510. if (max_used != RT_NULL)
  511. *max_used = max_mem;
  512. }
  513. #ifdef RT_USING_FINSH
  514. #include <finsh.h>
  515. void list_mem(void)
  516. {
  517. rt_kprintf("total memory: %d\n", mem_size_aligned);
  518. rt_kprintf("used memory : %d\n", used_mem);
  519. rt_kprintf("maximum allocated memory: %d\n", max_mem);
  520. }
  521. FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
  522. #ifdef RT_USING_MEMTRACE
  523. int memcheck(void)
  524. {
  525. int position;
  526. rt_uint32_t level;
  527. struct heap_mem *mem;
  528. level = rt_hw_interrupt_disable();
  529. for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next])
  530. {
  531. position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr;
  532. if (position < 0) goto __exit;
  533. if (position > mem_size_aligned) goto __exit;
  534. if (mem->magic != HEAP_MAGIC) goto __exit;
  535. if (mem->used != 0 && mem->used != 1) goto __exit;
  536. }
  537. rt_hw_interrupt_enable(level);
  538. return 0;
  539. __exit:
  540. rt_kprintf("Memory block wrong:\n");
  541. rt_kprintf("address: 0x%08x\n", mem);
  542. rt_kprintf(" magic: 0x%04x\n", mem->magic);
  543. rt_kprintf(" used: %d\n", mem->used);
  544. rt_kprintf(" size: %d\n", mem->next - position - SIZEOF_STRUCT_MEM);
  545. rt_hw_interrupt_enable(level);
  546. return 0;
  547. }
  548. MSH_CMD_EXPORT(memcheck, check memory data);
  549. int memtrace(int argc, char **argv)
  550. {
  551. struct heap_mem *mem;
  552. list_mem();
  553. rt_kprintf("\nmemory heap address:\n");
  554. rt_kprintf("heap_ptr: 0x%08x\n", heap_ptr);
  555. rt_kprintf("lfree : 0x%08x\n", lfree);
  556. rt_kprintf("heap_end: 0x%08x\n", heap_end);
  557. rt_kprintf("\n--memory item information --\n");
  558. for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next])
  559. {
  560. int position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr;
  561. int size;
  562. rt_kprintf("[0x%08x - ", mem);
  563. size = mem->next - position - SIZEOF_STRUCT_MEM;
  564. if (size < 1024)
  565. rt_kprintf("%5d", size);
  566. else if (size < 1024 * 1024)
  567. rt_kprintf("%4dK", size / 1024);
  568. else
  569. rt_kprintf("%4dM", size / (1024 * 1024));
  570. rt_kprintf("] %c%c%c%c", mem->thread[0], mem->thread[1], mem->thread[2], mem->thread[3]);
  571. if (mem->magic != HEAP_MAGIC)
  572. rt_kprintf(": ***\n");
  573. else
  574. rt_kprintf("\n");
  575. }
  576. return 0;
  577. }
  578. MSH_CMD_EXPORT(memtrace, dump memory trace information);
  579. #endif /* end of RT_USING_MEMTRACE */
  580. #endif /* end of RT_USING_FINSH */
  581. #endif
  582. /**@}*/
  583. #endif /* end of RT_USING_HEAP */
  584. #endif /* end of RT_USING_MEMHEAP_AS_HEAP */