mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
  257. {
  258. LOG_D("malloc size %d, but align to %d",
  259. size, RT_ALIGN(size, RT_ALIGN_SIZE));
  260. }
  261. else
  262. {
  263. LOG_D("malloc size %d", size);
  264. }
  265. small_mem = (struct rt_small_mem *)m;
  266. /* alignment size */
  267. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  268. /* every data block must be at least MIN_SIZE_ALIGNED long */
  269. if (size < MIN_SIZE_ALIGNED)
  270. size = MIN_SIZE_ALIGNED;
  271. if (size > small_mem->mem_size_aligned)
  272. {
  273. LOG_D("no memory");
  274. return RT_NULL;
  275. }
  276. for (ptr = (rt_uint8_t *)small_mem->lfree - small_mem->heap_ptr;
  277. ptr <= small_mem->mem_size_aligned - size;
  278. ptr = ((struct rt_small_mem_item *)&small_mem->heap_ptr[ptr])->next)
  279. {
  280. mem = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr];
  281. if ((!MEM_ISUSED(mem)) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
  282. {
  283. /* mem is not used and at least perfect fit is possible:
  284. * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
  285. if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
  286. (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
  287. {
  288. /* (in addition to the above, we test if another struct rt_small_mem_item (SIZEOF_STRUCT_MEM) containing
  289. * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
  290. * -> split large block, create empty remainder,
  291. * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
  292. * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
  293. * struct rt_small_mem_item would fit in but no data between mem2 and mem2->next
  294. * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
  295. * region that couldn't hold data, but when mem->next gets freed,
  296. * the 2 regions would be combined, resulting in more free memory
  297. */
  298. ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
  299. /* create mem2 struct */
  300. mem2 = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr2];
  301. mem2->pool_ptr = MEM_FREED();
  302. mem2->next = mem->next;
  303. mem2->prev = ptr;
  304. #ifdef RT_USING_MEMTRACE
  305. rt_smem_setname(mem2, " ");
  306. #endif /* RT_USING_MEMTRACE */
  307. /* and insert it between mem and mem->next */
  308. mem->next = ptr2;
  309. if (mem2->next != small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM)
  310. {
  311. ((struct rt_small_mem_item *)&small_mem->heap_ptr[mem2->next])->prev = ptr2;
  312. }
  313. small_mem->parent.used += (size + SIZEOF_STRUCT_MEM);
  314. if (small_mem->parent.max < small_mem->parent.used)
  315. small_mem->parent.max = small_mem->parent.used;
  316. }
  317. else
  318. {
  319. /* (a mem2 struct does no fit into the user data space of mem and mem->next will always
  320. * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
  321. * take care of this).
  322. * -> near fit or excact fit: do not split, no mem2 creation
  323. * also can't move mem->next directly behind mem, since mem->next
  324. * will always be used at this point!
  325. */
  326. small_mem->parent.used += mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr);
  327. if (small_mem->parent.max < small_mem->parent.used)
  328. small_mem->parent.max = small_mem->parent.used;
  329. }
  330. /* set small memory object */
  331. mem->pool_ptr = MEM_USED();
  332. #ifdef RT_USING_MEMTRACE
  333. if (rt_thread_self())
  334. rt_smem_setname(mem, rt_thread_self()->parent.name);
  335. else
  336. rt_smem_setname(mem, "NONE");
  337. #endif /* RT_USING_MEMTRACE */
  338. if (mem == small_mem->lfree)
  339. {
  340. /* Find next free block after mem and update lowest free pointer */
  341. while (MEM_ISUSED(small_mem->lfree) && small_mem->lfree != small_mem->heap_end)
  342. small_mem->lfree = (struct rt_small_mem_item *)&small_mem->heap_ptr[small_mem->lfree->next];
  343. RT_ASSERT(((small_mem->lfree == small_mem->heap_end) || (!MEM_ISUSED(small_mem->lfree))));
  344. }
  345. RT_ASSERT((rt_ubase_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_ubase_t)small_mem->heap_end);
  346. RT_ASSERT((rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
  347. RT_ASSERT((((rt_ubase_t)mem) & (RT_ALIGN_SIZE - 1)) == 0);
  348. LOG_D("allocate memory at 0x%x, size: %d",
  349. (rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
  350. (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr)));
  351. /* return the memory data except mem struct */
  352. return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
  353. }
  354. }
  355. return RT_NULL;
  356. }
  357. RTM_EXPORT(rt_smem_alloc);
  358. /**
  359. * @brief This function will change the size of previously allocated memory block.
  360. *
  361. * @param m the small memory management object.
  362. *
  363. * @param rmem is the pointer to memory allocated by rt_mem_alloc.
  364. *
  365. * @param newsize is the required new size.
  366. *
  367. * @return the changed memory block address.
  368. */
  369. void *rt_smem_realloc(rt_smem_t m, void *rmem, rt_size_t newsize)
  370. {
  371. rt_size_t size;
  372. rt_size_t ptr, ptr2;
  373. struct rt_small_mem_item *mem, *mem2;
  374. struct rt_small_mem *small_mem;
  375. void *nmem;
  376. RT_ASSERT(m != RT_NULL);
  377. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  378. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  379. small_mem = (struct rt_small_mem *)m;
  380. /* alignment size */
  381. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  382. if (newsize > small_mem->mem_size_aligned)
  383. {
  384. LOG_D("realloc: out of memory");
  385. return RT_NULL;
  386. }
  387. else if (newsize == 0)
  388. {
  389. rt_smem_free(rmem);
  390. return RT_NULL;
  391. }
  392. /* allocate a new memory block */
  393. if (rmem == RT_NULL)
  394. return rt_smem_alloc(&small_mem->parent, newsize);
  395. RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  396. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr);
  397. RT_ASSERT((rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
  398. mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  399. /* current memory block size */
  400. ptr = (rt_uint8_t *)mem - small_mem->heap_ptr;
  401. size = mem->next - ptr - SIZEOF_STRUCT_MEM;
  402. if (size == newsize)
  403. {
  404. /* the size is the same as */
  405. return rmem;
  406. }
  407. if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
  408. {
  409. /* split memory block */
  410. small_mem->parent.used -= (size - newsize);
  411. ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
  412. mem2 = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr2];
  413. mem2->pool_ptr = MEM_FREED();
  414. mem2->next = mem->next;
  415. mem2->prev = ptr;
  416. #ifdef RT_USING_MEMTRACE
  417. rt_smem_setname(mem2, " ");
  418. #endif /* RT_USING_MEMTRACE */
  419. mem->next = ptr2;
  420. if (mem2->next != small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM)
  421. {
  422. ((struct rt_small_mem_item *)&small_mem->heap_ptr[mem2->next])->prev = ptr2;
  423. }
  424. if (mem2 < small_mem->lfree)
  425. {
  426. /* the splited struct is now the lowest */
  427. small_mem->lfree = mem2;
  428. }
  429. plug_holes(small_mem, mem2);
  430. return rmem;
  431. }
  432. /* expand memory */
  433. nmem = rt_smem_alloc(&small_mem->parent, newsize);
  434. if (nmem != RT_NULL) /* check memory */
  435. {
  436. rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
  437. rt_smem_free(rmem);
  438. }
  439. return nmem;
  440. }
  441. RTM_EXPORT(rt_smem_realloc);
  442. /**
  443. * @brief This function will release the previously allocated memory block by
  444. * rt_mem_alloc. The released memory block is taken back to system heap.
  445. *
  446. * @param rmem the address of memory which will be released.
  447. */
  448. void rt_smem_free(void *rmem)
  449. {
  450. struct rt_small_mem_item *mem;
  451. struct rt_small_mem *small_mem;
  452. if (rmem == RT_NULL)
  453. return;
  454. RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  455. /* Get the corresponding struct rt_small_mem_item ... */
  456. mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  457. /* ... which has to be in a used state ... */
  458. small_mem = MEM_POOL(mem);
  459. RT_ASSERT(small_mem != RT_NULL);
  460. RT_ASSERT(MEM_ISUSED(mem));
  461. RT_ASSERT(rt_object_get_type(&small_mem->parent.parent) == RT_Object_Class_Memory);
  462. RT_ASSERT(rt_object_is_systemobject(&small_mem->parent.parent));
  463. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr &&
  464. (rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
  465. RT_ASSERT(MEM_POOL(&small_mem->heap_ptr[mem->next]) == small_mem);
  466. LOG_D("release memory 0x%x, size: %d",
  467. (rt_ubase_t)rmem,
  468. (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr)));
  469. /* ... and is now unused. */
  470. mem->pool_ptr = MEM_FREED();
  471. #ifdef RT_USING_MEMTRACE
  472. rt_smem_setname(mem, " ");
  473. #endif /* RT_USING_MEMTRACE */
  474. if (mem < small_mem->lfree)
  475. {
  476. /* the newly freed struct is now the lowest */
  477. small_mem->lfree = mem;
  478. }
  479. small_mem->parent.used -= (mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr));
  480. /* finally, see if prev or next are free also */
  481. plug_holes(small_mem, mem);
  482. }
  483. RTM_EXPORT(rt_smem_free);
  484. #ifdef RT_USING_FINSH
  485. #include <finsh.h>
  486. #ifdef RT_USING_MEMTRACE
  487. int memcheck(int argc, char *argv[])
  488. {
  489. int position;
  490. rt_base_t level;
  491. struct rt_small_mem_item *mem;
  492. struct rt_small_mem *m;
  493. struct rt_object_information *information;
  494. struct rt_list_node *node;
  495. struct rt_object *object;
  496. char *name;
  497. name = argc > 1 ? argv[1] : RT_NULL;
  498. level = rt_hw_interrupt_disable();
  499. /* get mem object */
  500. information = rt_object_get_information(RT_Object_Class_Memory);
  501. for (node = information->object_list.next;
  502. node != &(information->object_list);
  503. node = node->next)
  504. {
  505. object = rt_list_entry(node, struct rt_object, list);
  506. /* find the specified object */
  507. if (name != RT_NULL && rt_strncmp(name, object->name, RT_NAME_MAX) != 0)
  508. continue;
  509. /* mem object */
  510. m = (struct rt_small_mem *)object;
  511. /* check mem */
  512. 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])
  513. {
  514. position = (rt_ubase_t)mem - (rt_ubase_t)m->heap_ptr;
  515. if (position < 0) goto __exit;
  516. if (position > (int)m->mem_size_aligned) goto __exit;
  517. if (MEM_POOL(mem) != m) goto __exit;
  518. }
  519. }
  520. rt_hw_interrupt_enable(level);
  521. return 0;
  522. __exit:
  523. rt_kprintf("Memory block wrong:\n");
  524. rt_kprintf(" name: %s\n", m->parent.parent.name);
  525. rt_kprintf("address: 0x%08x\n", mem);
  526. rt_kprintf(" pool: 0x%04x\n", mem->pool_ptr);
  527. rt_kprintf(" size: %d\n", mem->next - position - SIZEOF_STRUCT_MEM);
  528. rt_hw_interrupt_enable(level);
  529. return 0;
  530. }
  531. MSH_CMD_EXPORT(memcheck, check memory data);
  532. int memtrace(int argc, char **argv)
  533. {
  534. struct rt_small_mem_item *mem;
  535. struct rt_small_mem *m;
  536. struct rt_object_information *information;
  537. struct rt_list_node *node;
  538. struct rt_object *object;
  539. char *name;
  540. name = argc > 1 ? argv[1] : RT_NULL;
  541. /* get mem object */
  542. information = rt_object_get_information(RT_Object_Class_Memory);
  543. for (node = information->object_list.next;
  544. node != &(information->object_list);
  545. node = node->next)
  546. {
  547. object = rt_list_entry(node, struct rt_object, list);
  548. /* find the specified object */
  549. if (name != RT_NULL && rt_strncmp(name, object->name, RT_NAME_MAX) != 0)
  550. continue;
  551. /* mem object */
  552. m = (struct rt_small_mem *)object;
  553. /* show memory information */
  554. rt_kprintf("\nmemory heap address:\n");
  555. rt_kprintf("name : %s\n", m->parent.parent.name);
  556. rt_kprintf("total : 0x%d\n", m->parent.total);
  557. rt_kprintf("used : 0x%d\n", m->parent.used);
  558. rt_kprintf("max_used: 0x%d\n", m->parent.max);
  559. rt_kprintf("heap_ptr: 0x%08x\n", m->heap_ptr);
  560. rt_kprintf("lfree : 0x%08x\n", m->lfree);
  561. rt_kprintf("heap_end: 0x%08x\n", m->heap_end);
  562. rt_kprintf("\n--memory item information --\n");
  563. 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])
  564. {
  565. int size = MEM_SIZE(m, mem);
  566. rt_kprintf("[0x%08x - ", mem);
  567. if (size < 1024)
  568. rt_kprintf("%5d", size);
  569. else if (size < 1024 * 1024)
  570. rt_kprintf("%4dK", size / 1024);
  571. else
  572. rt_kprintf("%4dM", size / (1024 * 1024));
  573. rt_kprintf("] %c%c%c%c", mem->thread[0], mem->thread[1], mem->thread[2], mem->thread[3]);
  574. if (MEM_POOL(mem) != m)
  575. rt_kprintf(": ***\n");
  576. else
  577. rt_kprintf("\n");
  578. }
  579. }
  580. return 0;
  581. }
  582. MSH_CMD_EXPORT(memtrace, dump memory trace information);
  583. #endif /* RT_USING_MEMTRACE */
  584. #endif /* RT_USING_FINSH */
  585. #endif /* defined (RT_USING_SMALL_MEM) */
  586. /**@}*/