mem.c 23 KB

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