mem.c 21 KB

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