mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Copyright (c) 2006-2024, 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_uintptr_t pool_ptr; /**< small memory object addr */
  57. rt_size_t next; /**< next free item */
  58. rt_size_t prev; /**< prev free item */
  59. #ifdef RT_USING_MEMTRACE
  60. #ifdef ARCH_CPU_64BIT
  61. rt_uint8_t thread[8]; /**< thread name */
  62. #else
  63. rt_uint8_t thread[4]; /**< thread name */
  64. #endif /* ARCH_CPU_64BIT */
  65. #endif /* RT_USING_MEMTRACE */
  66. };
  67. /**
  68. * Base structure of small memory object
  69. */
  70. struct rt_small_mem
  71. {
  72. struct rt_memory parent; /**< inherit from rt_memory */
  73. rt_uint8_t *heap_ptr; /**< pointer to the heap */
  74. struct rt_small_mem_item *heap_end;
  75. struct rt_small_mem_item *lfree;
  76. rt_size_t mem_size_aligned; /**< aligned memory size */
  77. };
  78. #define MIN_SIZE (sizeof(rt_uintptr_t) + sizeof(rt_size_t) + sizeof(rt_size_t))
  79. #define MEM_MASK ((~(rt_size_t)0) - 1)
  80. #define MEM_USED(_mem) ((((rt_uintptr_t)(_mem)) & MEM_MASK) | 0x1)
  81. #define MEM_FREED(_mem) ((((rt_uintptr_t)(_mem)) & MEM_MASK) | 0x0)
  82. #define MEM_ISUSED(_mem) \
  83. (((rt_uintptr_t)(((struct rt_small_mem_item *)(_mem))->pool_ptr)) & (~MEM_MASK))
  84. #define MEM_POOL(_mem) \
  85. ((struct rt_small_mem *)(((rt_uintptr_t)(((struct rt_small_mem_item *)(_mem))->pool_ptr)) & (MEM_MASK)))
  86. #define MEM_SIZE(_heap, _mem) \
  87. (((struct rt_small_mem_item *)(_mem))->next - ((rt_uintptr_t)(_mem) - \
  88. (rt_uintptr_t)((_heap)->heap_ptr)) - RT_ALIGN(sizeof(struct rt_small_mem_item), RT_ALIGN_SIZE))
  89. #define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE)
  90. #define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct rt_small_mem_item), RT_ALIGN_SIZE)
  91. #ifdef RT_USING_MEMTRACE
  92. rt_inline void rt_smem_setname(struct rt_small_mem_item *mem, const char *name)
  93. {
  94. int index;
  95. for (index = 0; index < sizeof(mem->thread); index ++)
  96. {
  97. if (name[index] == '\0') break;
  98. mem->thread[index] = name[index];
  99. }
  100. for (; index < sizeof(mem->thread); index ++)
  101. {
  102. mem->thread[index] = ' ';
  103. }
  104. }
  105. #endif /* RT_USING_MEMTRACE */
  106. static void plug_holes(struct rt_small_mem *m, struct rt_small_mem_item *mem)
  107. {
  108. struct rt_small_mem_item *nmem;
  109. struct rt_small_mem_item *pmem;
  110. RT_ASSERT((rt_uint8_t *)mem >= m->heap_ptr);
  111. RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)m->heap_end);
  112. /* plug hole forward */
  113. nmem = (struct rt_small_mem_item *)&m->heap_ptr[mem->next];
  114. if (mem != nmem && !MEM_ISUSED(nmem) &&
  115. (rt_uint8_t *)nmem != (rt_uint8_t *)m->heap_end)
  116. {
  117. /* if mem->next is unused and not end of m->heap_ptr,
  118. * combine mem and mem->next
  119. */
  120. if (m->lfree == nmem)
  121. {
  122. m->lfree = mem;
  123. }
  124. nmem->pool_ptr = 0;
  125. mem->next = nmem->next;
  126. ((struct rt_small_mem_item *)&m->heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - m->heap_ptr;
  127. }
  128. /* plug hole backward */
  129. pmem = (struct rt_small_mem_item *)&m->heap_ptr[mem->prev];
  130. if (pmem != mem && !MEM_ISUSED(pmem))
  131. {
  132. /* if mem->prev is unused, combine mem and mem->prev */
  133. if (m->lfree == mem)
  134. {
  135. m->lfree = pmem;
  136. }
  137. mem->pool_ptr = 0;
  138. pmem->next = mem->next;
  139. ((struct rt_small_mem_item *)&m->heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - m->heap_ptr;
  140. }
  141. }
  142. /**
  143. * @brief This function will initialize small memory management algorithm.
  144. *
  145. * @param name is the name of the small memory management object.
  146. *
  147. * @param begin_addr the beginning address of memory.
  148. *
  149. * @param size is the size of the memory.
  150. *
  151. * @return Return a pointer to the memory object. When the return value is RT_NULL, it means the init failed.
  152. */
  153. rt_smem_t rt_smem_init(const char *name,
  154. void *begin_addr,
  155. rt_size_t size)
  156. {
  157. struct rt_small_mem_item *mem;
  158. struct rt_small_mem *small_mem;
  159. rt_uintptr_t start_addr, begin_align, end_align, mem_size;
  160. small_mem = (struct rt_small_mem *)RT_ALIGN((rt_uintptr_t)begin_addr, RT_ALIGN_SIZE);
  161. start_addr = (rt_uintptr_t)small_mem + sizeof(*small_mem);
  162. begin_align = RT_ALIGN((rt_uintptr_t)start_addr, RT_ALIGN_SIZE);
  163. end_align = RT_ALIGN_DOWN((rt_uintptr_t)begin_addr + size, RT_ALIGN_SIZE);
  164. /* alignment addr */
  165. if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
  166. ((end_align - 2 * SIZEOF_STRUCT_MEM) >= start_addr))
  167. {
  168. /* calculate the aligned memory size */
  169. mem_size = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
  170. }
  171. else
  172. {
  173. rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
  174. (rt_uintptr_t)begin_addr, (rt_uintptr_t)begin_addr + size);
  175. return RT_NULL;
  176. }
  177. rt_memset(small_mem, 0, sizeof(*small_mem));
  178. /* initialize small memory object */
  179. rt_object_init(&(small_mem->parent.parent), RT_Object_Class_Memory, name);
  180. small_mem->parent.algorithm = "small";
  181. small_mem->parent.address = begin_align;
  182. small_mem->parent.total = mem_size;
  183. small_mem->mem_size_aligned = mem_size;
  184. /* point to begin address of heap */
  185. small_mem->heap_ptr = (rt_uint8_t *)begin_align;
  186. LOG_D("mem init, heap begin address 0x%x, size %d",
  187. (rt_uintptr_t)small_mem->heap_ptr, small_mem->mem_size_aligned);
  188. /* initialize the start of the heap */
  189. mem = (struct rt_small_mem_item *)small_mem->heap_ptr;
  190. mem->pool_ptr = MEM_FREED(small_mem);
  191. mem->next = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM;
  192. mem->prev = 0;
  193. #ifdef RT_USING_MEMTRACE
  194. rt_smem_setname(mem, "INIT");
  195. #endif /* RT_USING_MEMTRACE */
  196. /* initialize the end of the heap */
  197. small_mem->heap_end = (struct rt_small_mem_item *)&small_mem->heap_ptr[mem->next];
  198. small_mem->heap_end->pool_ptr = MEM_USED(small_mem);
  199. small_mem->heap_end->next = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM;
  200. small_mem->heap_end->prev = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM;
  201. #ifdef RT_USING_MEMTRACE
  202. rt_smem_setname(small_mem->heap_end, "INIT");
  203. #endif /* RT_USING_MEMTRACE */
  204. /* initialize the lowest-free pointer to the start of the heap */
  205. small_mem->lfree = (struct rt_small_mem_item *)small_mem->heap_ptr;
  206. return &small_mem->parent;
  207. }
  208. RTM_EXPORT(rt_smem_init);
  209. /**
  210. * @brief This function will remove a small mem from the system.
  211. *
  212. * @param m the small memory management object.
  213. *
  214. * @return RT_EOK
  215. */
  216. rt_err_t rt_smem_detach(rt_smem_t m)
  217. {
  218. RT_ASSERT(m != RT_NULL);
  219. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  220. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  221. rt_object_detach(&(m->parent));
  222. return RT_EOK;
  223. }
  224. RTM_EXPORT(rt_smem_detach);
  225. /**
  226. * @addtogroup group_memory_management
  227. */
  228. /**@{*/
  229. /**
  230. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  231. *
  232. * @param m the small memory management object.
  233. *
  234. * @param size is the minimum size of the requested block in bytes.
  235. *
  236. * @return the pointer to allocated memory or NULL if no free memory was found.
  237. */
  238. void *rt_smem_alloc(rt_smem_t m, rt_size_t size)
  239. {
  240. rt_size_t ptr, ptr2;
  241. struct rt_small_mem_item *mem, *mem2;
  242. struct rt_small_mem *small_mem;
  243. if (size == 0)
  244. return RT_NULL;
  245. RT_ASSERT(m != RT_NULL);
  246. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  247. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  248. small_mem = (struct rt_small_mem *)m;
  249. /* alignment size */
  250. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  251. /* every data block must be at least MIN_SIZE_ALIGNED long */
  252. if (size < MIN_SIZE_ALIGNED)
  253. size = MIN_SIZE_ALIGNED;
  254. if (size > small_mem->mem_size_aligned)
  255. {
  256. LOG_D("no memory");
  257. return RT_NULL;
  258. }
  259. for (ptr = (rt_uint8_t *)small_mem->lfree - small_mem->heap_ptr;
  260. ptr <= small_mem->mem_size_aligned - size;
  261. ptr = ((struct rt_small_mem_item *)&small_mem->heap_ptr[ptr])->next)
  262. {
  263. mem = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr];
  264. if ((!MEM_ISUSED(mem)) && (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 rt_small_mem_item (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 rt_small_mem_item 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 rt_small_mem_item *)&small_mem->heap_ptr[ptr2];
  284. mem2->pool_ptr = MEM_FREED(small_mem);
  285. mem2->next = mem->next;
  286. mem2->prev = ptr;
  287. #ifdef RT_USING_MEMTRACE
  288. rt_smem_setname(mem2, " ");
  289. #endif /* RT_USING_MEMTRACE */
  290. /* and insert it between mem and mem->next */
  291. mem->next = ptr2;
  292. if (mem2->next != small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM)
  293. {
  294. ((struct rt_small_mem_item *)&small_mem->heap_ptr[mem2->next])->prev = ptr2;
  295. }
  296. small_mem->parent.used += (size + SIZEOF_STRUCT_MEM);
  297. if (small_mem->parent.max < small_mem->parent.used)
  298. small_mem->parent.max = small_mem->parent.used;
  299. }
  300. else
  301. {
  302. /* (a mem2 struct does no fit into the user data space of mem and mem->next will always
  303. * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
  304. * take care of this).
  305. * -> near fit or excact fit: do not split, no mem2 creation
  306. * also can't move mem->next directly behind mem, since mem->next
  307. * will always be used at this point!
  308. */
  309. small_mem->parent.used += mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr);
  310. if (small_mem->parent.max < small_mem->parent.used)
  311. small_mem->parent.max = small_mem->parent.used;
  312. }
  313. /* set small memory object */
  314. mem->pool_ptr = MEM_USED(small_mem);
  315. #ifdef RT_USING_MEMTRACE
  316. if (rt_thread_self())
  317. rt_smem_setname(mem, rt_thread_self()->parent.name);
  318. else
  319. rt_smem_setname(mem, "NONE");
  320. #endif /* RT_USING_MEMTRACE */
  321. if (mem == small_mem->lfree)
  322. {
  323. /* Find next free block after mem and update lowest free pointer */
  324. while (MEM_ISUSED(small_mem->lfree) && small_mem->lfree != small_mem->heap_end)
  325. small_mem->lfree = (struct rt_small_mem_item *)&small_mem->heap_ptr[small_mem->lfree->next];
  326. RT_ASSERT(((small_mem->lfree == small_mem->heap_end) || (!MEM_ISUSED(small_mem->lfree))));
  327. }
  328. RT_ASSERT((rt_uintptr_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uintptr_t)small_mem->heap_end);
  329. RT_ASSERT((rt_uintptr_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
  330. RT_ASSERT((((rt_uintptr_t)mem) & (RT_ALIGN_SIZE - 1)) == 0);
  331. LOG_D("allocate memory at 0x%x, size: %d",
  332. (rt_uintptr_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
  333. (rt_uintptr_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr)));
  334. /* return the memory data except mem struct */
  335. return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
  336. }
  337. }
  338. return RT_NULL;
  339. }
  340. RTM_EXPORT(rt_smem_alloc);
  341. /**
  342. * @brief This function will change the size of previously allocated memory block.
  343. *
  344. * @param m the small memory management object.
  345. *
  346. * @param rmem is the pointer to memory allocated by rt_mem_alloc.
  347. *
  348. * @param newsize is the required new size.
  349. *
  350. * @return the changed memory block address.
  351. */
  352. void *rt_smem_realloc(rt_smem_t m, void *rmem, rt_size_t newsize)
  353. {
  354. rt_size_t size;
  355. rt_size_t ptr, ptr2;
  356. struct rt_small_mem_item *mem, *mem2;
  357. struct rt_small_mem *small_mem;
  358. void *nmem;
  359. RT_ASSERT(m != RT_NULL);
  360. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  361. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  362. small_mem = (struct rt_small_mem *)m;
  363. /* alignment size */
  364. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  365. if (newsize > small_mem->mem_size_aligned)
  366. {
  367. LOG_D("realloc: out of memory");
  368. return RT_NULL;
  369. }
  370. else if (newsize == 0)
  371. {
  372. rt_smem_free(rmem);
  373. return RT_NULL;
  374. }
  375. /* allocate a new memory block */
  376. if (rmem == RT_NULL)
  377. return rt_smem_alloc(&small_mem->parent, newsize);
  378. RT_ASSERT((((rt_uintptr_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  379. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr);
  380. RT_ASSERT((rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
  381. mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  382. /* current memory block size */
  383. ptr = (rt_uint8_t *)mem - small_mem->heap_ptr;
  384. size = mem->next - ptr - SIZEOF_STRUCT_MEM;
  385. if (size == newsize)
  386. {
  387. /* the size is the same as */
  388. return rmem;
  389. }
  390. if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
  391. {
  392. /* split memory block */
  393. small_mem->parent.used -= (size - newsize);
  394. ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
  395. mem2 = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr2];
  396. mem2->pool_ptr = MEM_FREED(small_mem);
  397. mem2->next = mem->next;
  398. mem2->prev = ptr;
  399. #ifdef RT_USING_MEMTRACE
  400. rt_smem_setname(mem2, " ");
  401. #endif /* RT_USING_MEMTRACE */
  402. mem->next = ptr2;
  403. if (mem2->next != small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM)
  404. {
  405. ((struct rt_small_mem_item *)&small_mem->heap_ptr[mem2->next])->prev = ptr2;
  406. }
  407. if (mem2 < small_mem->lfree)
  408. {
  409. /* the splited struct is now the lowest */
  410. small_mem->lfree = mem2;
  411. }
  412. plug_holes(small_mem, mem2);
  413. return rmem;
  414. }
  415. /* expand memory */
  416. nmem = rt_smem_alloc(&small_mem->parent, newsize);
  417. if (nmem != RT_NULL) /* check memory */
  418. {
  419. rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
  420. rt_smem_free(rmem);
  421. }
  422. return nmem;
  423. }
  424. RTM_EXPORT(rt_smem_realloc);
  425. /**
  426. * @brief This function will release the previously allocated memory block by
  427. * rt_mem_alloc. The released memory block is taken back to system heap.
  428. *
  429. * @param rmem the address of memory which will be released.
  430. */
  431. void rt_smem_free(void *rmem)
  432. {
  433. struct rt_small_mem_item *mem;
  434. struct rt_small_mem *small_mem;
  435. if (rmem == RT_NULL)
  436. return;
  437. RT_ASSERT((((rt_uintptr_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  438. /* Get the corresponding struct rt_small_mem_item ... */
  439. mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  440. /* ... which has to be in a used state ... */
  441. small_mem = MEM_POOL(mem);
  442. RT_ASSERT(small_mem != RT_NULL);
  443. RT_ASSERT(MEM_ISUSED(mem));
  444. RT_ASSERT(rt_object_get_type(&small_mem->parent.parent) == RT_Object_Class_Memory);
  445. RT_ASSERT(rt_object_is_systemobject(&small_mem->parent.parent));
  446. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr &&
  447. (rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
  448. RT_ASSERT(MEM_POOL(&small_mem->heap_ptr[mem->next]) == small_mem);
  449. LOG_D("release memory 0x%x, size: %d",
  450. (rt_uintptr_t)rmem,
  451. (rt_uintptr_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr)));
  452. /* ... and is now unused. */
  453. mem->pool_ptr = MEM_FREED(small_mem);
  454. #ifdef RT_USING_MEMTRACE
  455. rt_smem_setname(mem, " ");
  456. #endif /* RT_USING_MEMTRACE */
  457. if (mem < small_mem->lfree)
  458. {
  459. /* the newly freed struct is now the lowest */
  460. small_mem->lfree = mem;
  461. }
  462. small_mem->parent.used -= (mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr));
  463. /* finally, see if prev or next are free also */
  464. plug_holes(small_mem, mem);
  465. }
  466. RTM_EXPORT(rt_smem_free);
  467. #ifdef RT_USING_FINSH
  468. #include <finsh.h>
  469. #ifdef RT_USING_MEMTRACE
  470. static int memcheck(int argc, char *argv[])
  471. {
  472. int position;
  473. rt_base_t level;
  474. struct rt_small_mem_item *mem;
  475. struct rt_small_mem *m;
  476. struct rt_object_information *information;
  477. struct rt_list_node *node;
  478. struct rt_object *object;
  479. char *name;
  480. name = argc > 1 ? argv[1] : RT_NULL;
  481. level = rt_hw_interrupt_disable();
  482. /* get mem object */
  483. information = rt_object_get_information(RT_Object_Class_Memory);
  484. for (node = information->object_list.next;
  485. node != &(information->object_list);
  486. node = node->next)
  487. {
  488. object = rt_list_entry(node, struct rt_object, list);
  489. /* find the specified object */
  490. if (name != RT_NULL && rt_strncmp(name, object->name, RT_NAME_MAX) != 0)
  491. {
  492. continue;
  493. }
  494. /* mem object */
  495. m = (struct rt_small_mem *)object;
  496. if(rt_strncmp(m->parent.algorithm, "small", RT_NAME_MAX) != 0)
  497. {
  498. continue;
  499. }
  500. /* check mem */
  501. 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])
  502. {
  503. position = (rt_uintptr_t)mem - (rt_uintptr_t)m->heap_ptr;
  504. if (position < 0) goto __exit;
  505. if (position > (int)m->mem_size_aligned) goto __exit;
  506. if (MEM_POOL(mem) != m) goto __exit;
  507. }
  508. }
  509. rt_hw_interrupt_enable(level);
  510. return 0;
  511. __exit:
  512. rt_kprintf("Memory block wrong:\n");
  513. rt_kprintf(" name: %s\n", m->parent.parent.name);
  514. rt_kprintf("address: 0x%08x\n", mem);
  515. rt_kprintf(" pool: 0x%04x\n", mem->pool_ptr);
  516. rt_kprintf(" size: %d\n", mem->next - position - SIZEOF_STRUCT_MEM);
  517. rt_hw_interrupt_enable(level);
  518. return 0;
  519. }
  520. MSH_CMD_EXPORT(memcheck, check memory data);
  521. static int memtrace(int argc, char **argv)
  522. {
  523. struct rt_small_mem_item *mem;
  524. struct rt_small_mem *m;
  525. struct rt_object_information *information;
  526. struct rt_list_node *node;
  527. struct rt_object *object;
  528. char *name;
  529. name = argc > 1 ? argv[1] : RT_NULL;
  530. /* get mem object */
  531. information = rt_object_get_information(RT_Object_Class_Memory);
  532. for (node = information->object_list.next;
  533. node != &(information->object_list);
  534. node = node->next)
  535. {
  536. object = rt_list_entry(node, struct rt_object, list);
  537. /* find the specified object */
  538. if (name != RT_NULL && rt_strncmp(name, object->name, RT_NAME_MAX) != 0)
  539. {
  540. continue;
  541. }
  542. /* mem object */
  543. m = (struct rt_small_mem *)object;
  544. if(rt_strncmp(m->parent.algorithm, "small", RT_NAME_MAX) != 0)
  545. {
  546. continue;
  547. }
  548. /* show memory information */
  549. rt_kprintf("\nmemory heap address:\n");
  550. rt_kprintf("name : %s\n", m->parent.parent.name);
  551. rt_kprintf("total : %d\n", m->parent.total);
  552. rt_kprintf("used : %d\n", m->parent.used);
  553. rt_kprintf("max_used: %d\n", m->parent.max);
  554. rt_kprintf("heap_ptr: 0x%08x\n", m->heap_ptr);
  555. rt_kprintf("lfree : 0x%08x\n", m->lfree);
  556. rt_kprintf("heap_end: 0x%08x\n", m->heap_end);
  557. rt_kprintf("\n--memory item information --\n");
  558. 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])
  559. {
  560. int size = MEM_SIZE(m, mem);
  561. rt_kprintf("[0x%08x - ", mem);
  562. if (size < 1024)
  563. rt_kprintf("%5d", size);
  564. else if (size < 1024 * 1024)
  565. rt_kprintf("%4dK", size / 1024);
  566. else
  567. rt_kprintf("%4dM", size / (1024 * 1024));
  568. rt_kprintf("] %c%c%c%c", mem->thread[0], mem->thread[1], mem->thread[2], mem->thread[3]);
  569. if (MEM_POOL(mem) != m)
  570. rt_kprintf(": ***\n");
  571. else
  572. rt_kprintf("\n");
  573. }
  574. }
  575. return 0;
  576. }
  577. MSH_CMD_EXPORT(memtrace, dump memory trace information);
  578. #endif /* RT_USING_MEMTRACE */
  579. #endif /* RT_USING_FINSH */
  580. #endif /* defined (RT_USING_SMALL_MEM) */
  581. /**@}*/