mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. /**
  52. * memory item on the small mem
  53. */
  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 0xfffffffe
  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 m the small memory management object.
  154. *
  155. * @param name is the name of the small memory management object.
  156. *
  157. * @param begin_addr the beginning address of memory.
  158. *
  159. * @param size is the size of the memory.
  160. *
  161. * @return Return a pointer to the memory object. When the return value is RT_NULL, it means the init failed.
  162. */
  163. rt_smem_t rt_smem_init(const char *name,
  164. void *begin_addr,
  165. rt_size_t size)
  166. {
  167. struct rt_small_mem_item *mem;
  168. struct rt_small_mem *small_mem;
  169. rt_ubase_t start_addr, begin_align, end_align, mem_size;
  170. small_mem = (struct rt_small_mem *)RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
  171. start_addr = (rt_ubase_t)small_mem + sizeof(*small_mem);
  172. begin_align = RT_ALIGN((rt_ubase_t)start_addr, RT_ALIGN_SIZE);
  173. end_align = RT_ALIGN_DOWN((rt_ubase_t)begin_addr + size, RT_ALIGN_SIZE);
  174. /* alignment addr */
  175. if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
  176. ((end_align - 2 * SIZEOF_STRUCT_MEM) >= start_addr))
  177. {
  178. /* calculate the aligned memory size */
  179. mem_size = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
  180. }
  181. else
  182. {
  183. rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
  184. (rt_ubase_t)begin_addr, (rt_ubase_t)begin_addr + size);
  185. return RT_NULL;
  186. }
  187. rt_memset(small_mem, 0, sizeof(*small_mem));
  188. /* initialize small memory object */
  189. rt_object_init(&(small_mem->parent.parent), RT_Object_Class_Memory, name);
  190. small_mem->parent.algorithm = "small";
  191. small_mem->parent.address = begin_align;
  192. small_mem->parent.total = mem_size;
  193. small_mem->mem_size_aligned = mem_size;
  194. /* point to begin address of heap */
  195. small_mem->heap_ptr = (rt_uint8_t *)begin_align;
  196. RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
  197. (rt_ubase_t)small_mem->heap_ptr, small_mem->mem_size_aligned));
  198. /* initialize the start of the heap */
  199. mem = (struct rt_small_mem_item *)small_mem->heap_ptr;
  200. mem->pool_ptr = MEM_FREED();
  201. mem->next = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM;
  202. mem->prev = 0;
  203. #ifdef RT_USING_MEMTRACE
  204. rt_smem_setname(mem, "INIT");
  205. #endif /* RT_USING_MEMTRACE */
  206. /* initialize the end of the heap */
  207. small_mem->heap_end = (struct rt_small_mem_item *)&small_mem->heap_ptr[mem->next];
  208. small_mem->heap_end->pool_ptr = MEM_USED();
  209. small_mem->heap_end->next = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM;
  210. small_mem->heap_end->prev = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM;
  211. #ifdef RT_USING_MEMTRACE
  212. rt_smem_setname(small_mem->heap_end, "INIT");
  213. #endif /* RT_USING_MEMTRACE */
  214. /* initialize the lowest-free pointer to the start of the heap */
  215. small_mem->lfree = (struct rt_small_mem_item *)small_mem->heap_ptr;
  216. return &small_mem->parent;
  217. }
  218. RTM_EXPORT(rt_smem_init);
  219. /**
  220. * @brief This function will remove a small mem from the system.
  221. *
  222. * @param m the small memory management object.
  223. *
  224. * @return RT_EOK
  225. */
  226. rt_err_t rt_smem_detach(rt_smem_t m)
  227. {
  228. RT_ASSERT(m != RT_NULL);
  229. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  230. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  231. rt_object_detach(&(m->parent));
  232. return RT_EOK;
  233. }
  234. RTM_EXPORT(rt_smem_detach);
  235. /**
  236. * @addtogroup MM
  237. */
  238. /**@{*/
  239. /**
  240. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  241. *
  242. * @param m the small memory management object.
  243. *
  244. * @param size is the minimum size of the requested block in bytes.
  245. *
  246. * @return the pointer to allocated memory or NULL if no free memory was found.
  247. */
  248. void *rt_smem_alloc(rt_smem_t m, rt_size_t size)
  249. {
  250. rt_size_t ptr, ptr2;
  251. struct rt_small_mem_item *mem, *mem2;
  252. struct rt_small_mem *small_mem;
  253. if (size == 0)
  254. return RT_NULL;
  255. RT_ASSERT(m != RT_NULL);
  256. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  257. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  258. if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
  259. {
  260. RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
  261. size, RT_ALIGN(size, RT_ALIGN_SIZE)));
  262. }
  263. else
  264. {
  265. RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));
  266. }
  267. small_mem = (struct rt_small_mem *)m;
  268. /* alignment size */
  269. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  270. /* every data block must be at least MIN_SIZE_ALIGNED long */
  271. if (size < MIN_SIZE_ALIGNED)
  272. size = MIN_SIZE_ALIGNED;
  273. if (size > small_mem->mem_size_aligned)
  274. {
  275. RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
  276. return RT_NULL;
  277. }
  278. for (ptr = (rt_uint8_t *)small_mem->lfree - small_mem->heap_ptr;
  279. ptr <= small_mem->mem_size_aligned - size;
  280. ptr = ((struct rt_small_mem_item *)&small_mem->heap_ptr[ptr])->next)
  281. {
  282. mem = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr];
  283. if ((!MEM_ISUSED(mem)) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
  284. {
  285. /* mem is not used and at least perfect fit is possible:
  286. * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
  287. if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
  288. (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
  289. {
  290. /* (in addition to the above, we test if another struct rt_small_mem_item (SIZEOF_STRUCT_MEM) containing
  291. * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
  292. * -> split large block, create empty remainder,
  293. * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
  294. * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
  295. * struct rt_small_mem_item would fit in but no data between mem2 and mem2->next
  296. * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
  297. * region that couldn't hold data, but when mem->next gets freed,
  298. * the 2 regions would be combined, resulting in more free memory
  299. */
  300. ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
  301. /* create mem2 struct */
  302. mem2 = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr2];
  303. mem2->pool_ptr = MEM_FREED();
  304. mem2->next = mem->next;
  305. mem2->prev = ptr;
  306. #ifdef RT_USING_MEMTRACE
  307. rt_smem_setname(mem2, " ");
  308. #endif /* RT_USING_MEMTRACE */
  309. /* and insert it between mem and mem->next */
  310. mem->next = ptr2;
  311. if (mem2->next != small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM)
  312. {
  313. ((struct rt_small_mem_item *)&small_mem->heap_ptr[mem2->next])->prev = ptr2;
  314. }
  315. small_mem->parent.used += (size + SIZEOF_STRUCT_MEM);
  316. if (small_mem->parent.max < small_mem->parent.used)
  317. small_mem->parent.max = small_mem->parent.used;
  318. }
  319. else
  320. {
  321. /* (a mem2 struct does no fit into the user data space of mem and mem->next will always
  322. * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
  323. * take care of this).
  324. * -> near fit or excact fit: do not split, no mem2 creation
  325. * also can't move mem->next directly behind mem, since mem->next
  326. * will always be used at this point!
  327. */
  328. small_mem->parent.used += mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr);
  329. if (small_mem->parent.max < small_mem->parent.used)
  330. small_mem->parent.max = small_mem->parent.used;
  331. }
  332. /* set small memory object */
  333. mem->pool_ptr = MEM_USED();
  334. #ifdef RT_USING_MEMTRACE
  335. if (rt_thread_self())
  336. rt_smem_setname(mem, rt_thread_self()->name);
  337. else
  338. rt_smem_setname(mem, "NONE");
  339. #endif /* RT_USING_MEMTRACE */
  340. if (mem == small_mem->lfree)
  341. {
  342. /* Find next free block after mem and update lowest free pointer */
  343. while (MEM_ISUSED(small_mem->lfree) && small_mem->lfree != small_mem->heap_end)
  344. small_mem->lfree = (struct rt_small_mem_item *)&small_mem->heap_ptr[small_mem->lfree->next];
  345. RT_ASSERT(((small_mem->lfree == small_mem->heap_end) || (!MEM_ISUSED(small_mem->lfree))));
  346. }
  347. RT_ASSERT((rt_ubase_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_ubase_t)small_mem->heap_end);
  348. RT_ASSERT((rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
  349. RT_ASSERT((((rt_ubase_t)mem) & (RT_ALIGN_SIZE - 1)) == 0);
  350. RT_DEBUG_LOG(RT_DEBUG_MEM,
  351. ("allocate memory at 0x%x, size: %d\n",
  352. (rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
  353. (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr))));
  354. /* return the memory data except mem struct */
  355. return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
  356. }
  357. }
  358. return RT_NULL;
  359. }
  360. RTM_EXPORT(rt_smem_alloc);
  361. /**
  362. * @brief This function will change the size of previously allocated memory block.
  363. *
  364. * @param m the small memory management object.
  365. *
  366. * @param rmem is the pointer to memory allocated by rt_mem_alloc.
  367. *
  368. * @param newsize is the required new size.
  369. *
  370. * @return the changed memory block address.
  371. */
  372. void *rt_smem_realloc(rt_smem_t m, void *rmem, rt_size_t newsize)
  373. {
  374. rt_size_t size;
  375. rt_size_t ptr, ptr2;
  376. struct rt_small_mem_item *mem, *mem2;
  377. struct rt_small_mem *small_mem;
  378. void *nmem;
  379. RT_ASSERT(m != RT_NULL);
  380. RT_ASSERT(rt_object_get_type(&m->parent) == RT_Object_Class_Memory);
  381. RT_ASSERT(rt_object_is_systemobject(&m->parent));
  382. small_mem = (struct rt_small_mem *)m;
  383. /* alignment size */
  384. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  385. if (newsize > small_mem->mem_size_aligned)
  386. {
  387. RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
  388. return RT_NULL;
  389. }
  390. else if (newsize == 0)
  391. {
  392. rt_smem_free(rmem);
  393. return RT_NULL;
  394. }
  395. /* allocate a new memory block */
  396. if (rmem == RT_NULL)
  397. return rt_smem_alloc(&small_mem->parent, newsize);
  398. RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  399. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr);
  400. RT_ASSERT((rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
  401. mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  402. /* current memory block size */
  403. ptr = (rt_uint8_t *)mem - small_mem->heap_ptr;
  404. size = mem->next - ptr - SIZEOF_STRUCT_MEM;
  405. if (size == newsize)
  406. {
  407. /* the size is the same as */
  408. return rmem;
  409. }
  410. if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
  411. {
  412. /* split memory block */
  413. small_mem->parent.used -= (size - newsize);
  414. ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
  415. mem2 = (struct rt_small_mem_item *)&small_mem->heap_ptr[ptr2];
  416. mem2->pool_ptr = MEM_FREED();
  417. mem2->next = mem->next;
  418. mem2->prev = ptr;
  419. #ifdef RT_USING_MEMTRACE
  420. rt_smem_setname(mem2, " ");
  421. #endif /* RT_USING_MEMTRACE */
  422. mem->next = ptr2;
  423. if (mem2->next != small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM)
  424. {
  425. ((struct rt_small_mem_item *)&small_mem->heap_ptr[mem2->next])->prev = ptr2;
  426. }
  427. if (mem2 < small_mem->lfree)
  428. {
  429. /* the splited struct is now the lowest */
  430. small_mem->lfree = mem2;
  431. }
  432. plug_holes(small_mem, mem2);
  433. return rmem;
  434. }
  435. /* expand memory */
  436. nmem = rt_smem_alloc(&small_mem->parent, newsize);
  437. if (nmem != RT_NULL) /* check memory */
  438. {
  439. rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
  440. rt_smem_free(rmem);
  441. }
  442. return nmem;
  443. }
  444. RTM_EXPORT(rt_smem_realloc);
  445. /**
  446. * @brief This function will release the previously allocated memory block by
  447. * rt_mem_alloc. The released memory block is taken back to system heap.
  448. *
  449. * @param rmem the address of memory which will be released.
  450. */
  451. void rt_smem_free(void *rmem)
  452. {
  453. struct rt_small_mem_item *mem;
  454. struct rt_small_mem *small_mem;
  455. if (rmem == RT_NULL)
  456. return;
  457. RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
  458. /* Get the corresponding struct rt_small_mem_item ... */
  459. mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  460. RT_DEBUG_LOG(RT_DEBUG_MEM,
  461. ("release memory 0x%x, size: %d\n",
  462. (rt_ubase_t)rmem,
  463. (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr))));
  464. /* ... which has to be in a used state ... */
  465. small_mem = MEM_POOL(mem);
  466. RT_ASSERT(small_mem != RT_NULL);
  467. RT_ASSERT(MEM_ISUSED(mem));
  468. RT_ASSERT(rt_object_get_type(&small_mem->parent.parent) == RT_Object_Class_Memory);
  469. RT_ASSERT(rt_object_is_systemobject(&small_mem->parent.parent));
  470. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr &&
  471. (rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
  472. RT_ASSERT(MEM_POOL(&small_mem->heap_ptr[mem->next]) == small_mem);
  473. /* ... and is now unused. */
  474. mem->pool_ptr = MEM_FREED();
  475. #ifdef RT_USING_MEMTRACE
  476. rt_smem_setname(mem, " ");
  477. #endif /* RT_USING_MEMTRACE */
  478. if (mem < small_mem->lfree)
  479. {
  480. /* the newly freed struct is now the lowest */
  481. small_mem->lfree = mem;
  482. }
  483. small_mem->parent.used -= (mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr));
  484. /* finally, see if prev or next are free also */
  485. plug_holes(small_mem, mem);
  486. }
  487. RTM_EXPORT(rt_smem_free);
  488. #ifdef RT_USING_FINSH
  489. #include <finsh.h>
  490. #ifdef RT_USING_MEMTRACE
  491. int memcheck(int argc, char *argv[])
  492. {
  493. int position;
  494. rt_base_t level;
  495. struct rt_small_mem_item *mem;
  496. struct rt_small_mem *m;
  497. struct rt_object_information *information;
  498. struct rt_list_node *node;
  499. struct rt_object *object;
  500. char *name;
  501. name = argc > 1 ? argv[1] : RT_NULL;
  502. level = rt_hw_interrupt_disable();
  503. /* get mem object */
  504. information = rt_object_get_information(RT_Object_Class_Memory);
  505. for (node = information->object_list.next;
  506. node != &(information->object_list);
  507. node = node->next)
  508. {
  509. object = rt_list_entry(node, struct rt_object, list);
  510. /* find the specified object */
  511. if (name != RT_NULL && rt_strncmp(name, object->name, RT_NAME_MAX) != 0)
  512. continue;
  513. /* mem object */
  514. m = (struct rt_small_mem *)object;
  515. /* check mem */
  516. 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])
  517. {
  518. position = (rt_ubase_t)mem - (rt_ubase_t)m->heap_ptr;
  519. if (position < 0) goto __exit;
  520. if (position > (int)m->mem_size_aligned) goto __exit;
  521. if (MEM_POOL(mem) != m) goto __exit;
  522. }
  523. }
  524. rt_hw_interrupt_enable(level);
  525. return 0;
  526. __exit:
  527. rt_kprintf("Memory block wrong:\n");
  528. rt_kprintf(" name: %s\n", m->parent.parent.name);
  529. rt_kprintf("address: 0x%08x\n", mem);
  530. rt_kprintf(" pool: 0x%04x\n", mem->pool_ptr);
  531. rt_kprintf(" size: %d\n", mem->next - position - SIZEOF_STRUCT_MEM);
  532. rt_hw_interrupt_enable(level);
  533. return 0;
  534. }
  535. MSH_CMD_EXPORT(memcheck, check memory data);
  536. int memtrace(int argc, char **argv)
  537. {
  538. struct rt_small_mem_item *mem;
  539. struct rt_small_mem *m;
  540. struct rt_object_information *information;
  541. struct rt_list_node *node;
  542. struct rt_object *object;
  543. char *name;
  544. name = argc > 1 ? argv[1] : RT_NULL;
  545. /* get mem object */
  546. information = rt_object_get_information(RT_Object_Class_Memory);
  547. for (node = information->object_list.next;
  548. node != &(information->object_list);
  549. node = node->next)
  550. {
  551. object = rt_list_entry(node, struct rt_object, list);
  552. /* find the specified object */
  553. if (name != RT_NULL && rt_strncmp(name, object->name, RT_NAME_MAX) != 0)
  554. continue;
  555. /* mem object */
  556. m = (struct rt_small_mem *)object;
  557. /* show memory information */
  558. rt_kprintf("\nmemory heap address:\n");
  559. rt_kprintf("name : %s\n", m->parent.parent.name);
  560. rt_kprintf("total : 0x%d\n", m->parent.total);
  561. rt_kprintf("used : 0x%d\n", m->parent.used);
  562. rt_kprintf("max_used: 0x%d\n", m->parent.max);
  563. rt_kprintf("heap_ptr: 0x%08x\n", m->heap_ptr);
  564. rt_kprintf("lfree : 0x%08x\n", m->lfree);
  565. rt_kprintf("heap_end: 0x%08x\n", m->heap_end);
  566. rt_kprintf("\n--memory item information --\n");
  567. 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])
  568. {
  569. int size = MEM_SIZE(m, mem);
  570. rt_kprintf("[0x%08x - ", mem);
  571. if (size < 1024)
  572. rt_kprintf("%5d", size);
  573. else if (size < 1024 * 1024)
  574. rt_kprintf("%4dK", size / 1024);
  575. else
  576. rt_kprintf("%4dM", size / (1024 * 1024));
  577. rt_kprintf("] %c%c%c%c", mem->thread[0], mem->thread[1], mem->thread[2], mem->thread[3]);
  578. if (MEM_POOL(mem) != m)
  579. rt_kprintf(": ***\n");
  580. else
  581. rt_kprintf("\n");
  582. }
  583. }
  584. return 0;
  585. }
  586. MSH_CMD_EXPORT(memtrace, dump memory trace information);
  587. #endif /* RT_USING_MEMTRACE */
  588. #endif /* RT_USING_FINSH */
  589. #endif /* defined (RT_USING_SMALL_MEM) */
  590. /**@}*/