memheap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * File : memheap.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-04-10 Bernard first implementation
  23. * 2012-10-16 Bernard add the mutex lock for heap object.
  24. * 2012-12-29 Bernard memheap can be used as system heap.
  25. * change mutex lock to semaphore lock.
  26. * 2013-04-10 Bernard add rt_memheap_realloc function.
  27. * 2013-05-24 Bernard fix the rt_memheap_realloc issue.
  28. * 2013-07-11 Grissiom fix the memory block splitting issue.
  29. * 2013-07-15 Grissiom optimize rt_memheap_realloc
  30. */
  31. #include <rthw.h>
  32. #include <rtthread.h>
  33. #ifdef RT_USING_MEMHEAP
  34. /* dynamic pool magic and mask */
  35. #define RT_MEMHEAP_MAGIC 0x1ea01ea0
  36. #define RT_MEMHEAP_MASK 0xfffffffe
  37. #define RT_MEMHEAP_USED 0x01
  38. #define RT_MEMHEAP_FREED 0x00
  39. #define RT_MEMHEAP_IS_USED(i) ((i)->magic & RT_MEMHEAP_USED)
  40. #define RT_MEMHEAP_MINIALLOC 12
  41. #define RT_MEMHEAP_SIZE RT_ALIGN(sizeof(struct rt_memheap_item), RT_ALIGN_SIZE)
  42. #define MEMITEM_SIZE(item) ((rt_uint32_t)item->next - (rt_uint32_t)item - RT_MEMHEAP_SIZE)
  43. /*
  44. * The initialized memory pool will be:
  45. * +-----------------------------------+--------------------------+
  46. * | whole freed memory block | Used Memory Block Tailer |
  47. * +-----------------------------------+--------------------------+
  48. *
  49. * block_list --> whole freed memory block
  50. *
  51. * The length of Used Memory Block Tailer is 0,
  52. * which is prevents block merging across list
  53. */
  54. rt_err_t rt_memheap_init(struct rt_memheap *memheap,
  55. const char *name,
  56. void *start_addr,
  57. rt_uint32_t size)
  58. {
  59. struct rt_memheap_item *item;
  60. RT_ASSERT(memheap != RT_NULL);
  61. /* initialize pool object */
  62. rt_object_init(&(memheap->parent), RT_Object_Class_MemHeap, name);
  63. memheap->start_addr = start_addr;
  64. memheap->pool_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  65. memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE);
  66. memheap->max_used_size = memheap->pool_size - memheap->available_size;
  67. /* initialize the free list header */
  68. item = &(memheap->free_header);
  69. item->magic = RT_MEMHEAP_MAGIC;
  70. item->pool_ptr = memheap;
  71. item->next = RT_NULL;
  72. item->prev = RT_NULL;
  73. item->next_free = item;
  74. item->prev_free = item;
  75. /* set the free list to free list header */
  76. memheap->free_list = item;
  77. /* initialize the first big memory block */
  78. item = (struct rt_memheap_item *)start_addr;
  79. item->magic = RT_MEMHEAP_MAGIC;
  80. item->pool_ptr = memheap;
  81. item->next = RT_NULL;
  82. item->prev = RT_NULL;
  83. item->next_free = item;
  84. item->prev_free = item;
  85. item->next = (struct rt_memheap_item *)
  86. ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
  87. item->prev = item->next;
  88. /* block list header */
  89. memheap->block_list = item;
  90. /* place the big memory block to free list */
  91. item->next_free = memheap->free_list->next_free;
  92. item->prev_free = memheap->free_list;
  93. memheap->free_list->next_free->prev_free = item;
  94. memheap->free_list->next_free = item;
  95. /* move to the end of memory pool to build a small tailer block,
  96. * which prevents block merging
  97. */
  98. item = item->next;
  99. /* it's a used memory block */
  100. item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
  101. item->pool_ptr = memheap;
  102. item->next = (struct rt_memheap_item *)start_addr;
  103. item->prev = (struct rt_memheap_item *)start_addr;
  104. /* not in free list */
  105. item->next_free = item->prev_free = RT_NULL;
  106. /* initialize semaphore lock */
  107. rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_FIFO);
  108. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  109. ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
  110. start_addr, size, &(memheap->free_header)));
  111. return RT_EOK;
  112. }
  113. RTM_EXPORT(rt_memheap_init);
  114. rt_err_t rt_memheap_detach(struct rt_memheap *heap)
  115. {
  116. RT_ASSERT(heap);
  117. rt_object_detach(&(heap->lock.parent.parent));
  118. rt_object_detach(&(heap->parent));
  119. /* Return a successful completion. */
  120. return RT_EOK;
  121. }
  122. RTM_EXPORT(rt_memheap_detach);
  123. void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size)
  124. {
  125. rt_err_t result;
  126. rt_uint32_t free_size;
  127. struct rt_memheap_item *header_ptr;
  128. RT_ASSERT(heap != RT_NULL);
  129. /* align allocated size */
  130. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  131. if (size < RT_MEMHEAP_MINIALLOC)
  132. size = RT_MEMHEAP_MINIALLOC;
  133. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
  134. size, RT_NAME_MAX, heap->parent.name));
  135. if (size < heap->available_size)
  136. {
  137. /* search on free list */
  138. free_size = 0;
  139. /* lock memheap */
  140. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  141. if (result != RT_EOK)
  142. {
  143. rt_set_errno(result);
  144. return RT_NULL;
  145. }
  146. /* get the first free memory block */
  147. header_ptr = heap->free_list->next_free;
  148. while (header_ptr != heap->free_list && free_size < size)
  149. {
  150. /* get current freed memory block size */
  151. free_size = MEMITEM_SIZE(header_ptr);
  152. if (free_size < size)
  153. {
  154. /* move to next free memory block */
  155. header_ptr = header_ptr->next_free;
  156. }
  157. }
  158. /* determine if the memory is available. */
  159. if (free_size >= size)
  160. {
  161. /* a block that satisfies the request has been found. */
  162. /* determine if the block needs to be split. */
  163. if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
  164. {
  165. struct rt_memheap_item *new_ptr;
  166. /* split the block. */
  167. new_ptr = (struct rt_memheap_item *)
  168. (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
  169. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  170. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  171. header_ptr,
  172. header_ptr->next,
  173. header_ptr->prev,
  174. new_ptr));
  175. /* mark the new block as a memory block and freed. */
  176. new_ptr->magic = RT_MEMHEAP_MAGIC;
  177. /* put the pool pointer into the new block. */
  178. new_ptr->pool_ptr = heap;
  179. /* break down the block list */
  180. new_ptr->prev = header_ptr;
  181. new_ptr->next = header_ptr->next;
  182. header_ptr->next->prev = new_ptr;
  183. header_ptr->next = new_ptr;
  184. /* remove header ptr from free list */
  185. header_ptr->next_free->prev_free = header_ptr->prev_free;
  186. header_ptr->prev_free->next_free = header_ptr->next_free;
  187. header_ptr->next_free = RT_NULL;
  188. header_ptr->prev_free = RT_NULL;
  189. /* insert new_ptr to free list */
  190. new_ptr->next_free = heap->free_list->next_free;
  191. new_ptr->prev_free = heap->free_list;
  192. heap->free_list->next_free->prev_free = new_ptr;
  193. heap->free_list->next_free = new_ptr;
  194. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
  195. new_ptr->next_free,
  196. new_ptr->prev_free));
  197. /* decrement the available byte count. */
  198. heap->available_size = heap->available_size -
  199. size -
  200. RT_MEMHEAP_SIZE;
  201. if (heap->pool_size - heap->available_size > heap->max_used_size)
  202. heap->max_used_size = heap->pool_size - heap->available_size;
  203. }
  204. else
  205. {
  206. /* decrement the entire free size from the available bytes count. */
  207. heap->available_size = heap->available_size - free_size;
  208. if (heap->pool_size - heap->available_size > heap->max_used_size)
  209. heap->max_used_size = heap->pool_size - heap->available_size;
  210. /* remove header_ptr from free list */
  211. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  212. ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
  213. header_ptr,
  214. header_ptr->next_free,
  215. header_ptr->prev_free));
  216. header_ptr->next_free->prev_free = header_ptr->prev_free;
  217. header_ptr->prev_free->next_free = header_ptr->next_free;
  218. header_ptr->next_free = RT_NULL;
  219. header_ptr->prev_free = RT_NULL;
  220. }
  221. /* Mark the allocated block as not available. */
  222. header_ptr->magic |= RT_MEMHEAP_USED;
  223. /* release lock */
  224. rt_sem_release(&(heap->lock));
  225. /* Return a memory address to the caller. */
  226. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  227. ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
  228. (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
  229. header_ptr,
  230. size));
  231. return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
  232. }
  233. /* release lock */
  234. rt_sem_release(&(heap->lock));
  235. }
  236. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
  237. /* Return the completion status. */
  238. return RT_NULL;
  239. }
  240. RTM_EXPORT(rt_memheap_alloc);
  241. void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
  242. {
  243. rt_err_t result;
  244. rt_size_t oldsize;
  245. struct rt_memheap_item *header_ptr;
  246. struct rt_memheap_item *new_ptr;
  247. if (newsize == 0)
  248. {
  249. rt_memheap_free(ptr);
  250. return RT_NULL;
  251. }
  252. /* align allocated size */
  253. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  254. if (newsize < RT_MEMHEAP_MINIALLOC)
  255. newsize = RT_MEMHEAP_MINIALLOC;
  256. if (ptr == RT_NULL)
  257. {
  258. return rt_memheap_alloc(heap, newsize);
  259. }
  260. /* get memory block header and get the size of memory block */
  261. header_ptr = (struct rt_memheap_item *)
  262. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  263. oldsize = MEMITEM_SIZE(header_ptr);
  264. /* re-allocate memory */
  265. if (newsize > oldsize)
  266. {
  267. void* new_ptr;
  268. struct rt_memheap_item *next_ptr;
  269. /* lock memheap */
  270. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  271. if (result != RT_EOK)
  272. {
  273. rt_set_errno(result);
  274. return RT_NULL;
  275. }
  276. next_ptr = header_ptr->next;
  277. /* header_ptr should not be the tail */
  278. RT_ASSERT(next_ptr > header_ptr);
  279. /* check whether the following free space is enough to expand */
  280. if (!RT_MEMHEAP_IS_USED(next_ptr))
  281. {
  282. rt_int32_t nextsize;
  283. nextsize = MEMITEM_SIZE(next_ptr);
  284. RT_ASSERT(next_ptr > 0);
  285. /* Here is the ASCII art of the situation that we can make use of
  286. * the next free node without alloc/memcpy, |*| is the control
  287. * block:
  288. *
  289. * oldsize free node
  290. * |*|-----------|*|----------------------|*|
  291. * newsize >= minialloc
  292. * |*|----------------|*|-----------------|*|
  293. */
  294. if (nextsize + oldsize > newsize + RT_MEMHEAP_MINIALLOC)
  295. {
  296. /* decrement the entire free size from the available bytes count. */
  297. heap->available_size = heap->available_size - (newsize - oldsize);
  298. if (heap->pool_size - heap->available_size > heap->max_used_size)
  299. heap->max_used_size = heap->pool_size - heap->available_size;
  300. /* remove next_ptr from free list */
  301. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  302. ("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
  303. next_ptr,
  304. next_ptr->next_free,
  305. next_ptr->prev_free));
  306. next_ptr->next_free->prev_free = next_ptr->prev_free;
  307. next_ptr->prev_free->next_free = next_ptr->next_free;
  308. next_ptr->next->prev = next_ptr->prev;
  309. next_ptr->prev->next = next_ptr->next;
  310. /* build a new one on the right place */
  311. next_ptr = (struct rt_memheap_item*)((char*)ptr + newsize);
  312. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  313. ("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
  314. next_ptr,
  315. next_ptr->next,
  316. next_ptr->prev));
  317. /* mark the new block as a memory block and freed. */
  318. next_ptr->magic = RT_MEMHEAP_MAGIC;
  319. /* put the pool pointer into the new block. */
  320. next_ptr->pool_ptr = heap;
  321. next_ptr->prev = header_ptr;
  322. next_ptr->next = header_ptr->next;
  323. header_ptr->next->prev = next_ptr;
  324. header_ptr->next = next_ptr;
  325. /* insert next_ptr to free list */
  326. next_ptr->next_free = heap->free_list->next_free;
  327. next_ptr->prev_free = heap->free_list;
  328. heap->free_list->next_free->prev_free = next_ptr;
  329. heap->free_list->next_free = next_ptr;
  330. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
  331. next_ptr->next_free,
  332. next_ptr->prev_free));
  333. /* release lock */
  334. rt_sem_release(&(heap->lock));
  335. return ptr;
  336. }
  337. }
  338. /* release lock */
  339. rt_sem_release(&(heap->lock));
  340. /* re-allocate a memory block */
  341. new_ptr = (void*)rt_memheap_alloc(heap, newsize);
  342. if (new_ptr != RT_NULL)
  343. {
  344. rt_memcpy(new_ptr, ptr, oldsize < newsize ? oldsize : newsize);
  345. rt_memheap_free(ptr);
  346. }
  347. return new_ptr;
  348. }
  349. /* don't split when there is less than one node space left */
  350. if (newsize + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC >= oldsize)
  351. return ptr;
  352. /* lock memheap */
  353. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  354. if (result != RT_EOK)
  355. {
  356. rt_set_errno(result);
  357. return RT_NULL;
  358. }
  359. /* split the block. */
  360. new_ptr = (struct rt_memheap_item *)
  361. (((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);
  362. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  363. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  364. header_ptr,
  365. header_ptr->next,
  366. header_ptr->prev,
  367. new_ptr));
  368. /* mark the new block as a memory block and freed. */
  369. new_ptr->magic = RT_MEMHEAP_MAGIC;
  370. /* put the pool pointer into the new block. */
  371. new_ptr->pool_ptr = heap;
  372. /* break down the block list */
  373. new_ptr->prev = header_ptr;
  374. new_ptr->next = header_ptr->next;
  375. header_ptr->next->prev = new_ptr;
  376. header_ptr->next = new_ptr;
  377. /* determine if the block can be merged with the next neighbor. */
  378. if (!RT_MEMHEAP_IS_USED(new_ptr->next))
  379. {
  380. struct rt_memheap_item *free_ptr;
  381. /* merge block with next neighbor. */
  382. free_ptr = new_ptr->next;
  383. heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);
  384. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  385. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  386. header_ptr, header_ptr->next_free, header_ptr->prev_free));
  387. free_ptr->next->prev = new_ptr;
  388. new_ptr->next = free_ptr->next;
  389. /* remove free ptr from free list */
  390. free_ptr->next_free->prev_free = free_ptr->prev_free;
  391. free_ptr->prev_free->next_free = free_ptr->next_free;
  392. }
  393. /* insert the split block to free list */
  394. new_ptr->next_free = heap->free_list->next_free;
  395. new_ptr->prev_free = heap->free_list;
  396. heap->free_list->next_free->prev_free = new_ptr;
  397. heap->free_list->next_free = new_ptr;
  398. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
  399. new_ptr->next_free,
  400. new_ptr->prev_free));
  401. /* increment the available byte count. */
  402. heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
  403. /* release lock */
  404. rt_sem_release(&(heap->lock));
  405. /* return the old memory block */
  406. return ptr;
  407. }
  408. RTM_EXPORT(rt_memheap_realloc);
  409. void rt_memheap_free(void *ptr)
  410. {
  411. rt_err_t result;
  412. struct rt_memheap *heap;
  413. struct rt_memheap_item *header_ptr, *new_ptr;
  414. rt_uint32_t insert_header;
  415. /* NULL check */
  416. if (ptr == RT_NULL) return;
  417. /* set initial status as OK */
  418. insert_header = 1;
  419. new_ptr = RT_NULL;
  420. header_ptr = (struct rt_memheap_item *)
  421. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  422. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
  423. ptr, header_ptr));
  424. /* check magic */
  425. RT_ASSERT((header_ptr->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
  426. /* get pool ptr */
  427. heap = header_ptr->pool_ptr;
  428. /* lock memheap */
  429. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  430. if (result != RT_EOK)
  431. {
  432. rt_set_errno(result);
  433. return ;
  434. }
  435. /* Mark the memory as available. */
  436. header_ptr->magic &= ~RT_MEMHEAP_USED;
  437. /* Adjust the available number of bytes. */
  438. heap->available_size = heap->available_size + MEMITEM_SIZE(header_ptr);
  439. /* Determine if the block can be merged with the previous neighbor. */
  440. if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
  441. {
  442. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
  443. header_ptr->prev));
  444. /* adjust the available number of bytes. */
  445. heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
  446. /* yes, merge block with previous neighbor. */
  447. (header_ptr->prev)->next = header_ptr->next;
  448. (header_ptr->next)->prev = header_ptr->prev;
  449. /* move header pointer to previous. */
  450. header_ptr = header_ptr->prev;
  451. /* don't insert header to free list */
  452. insert_header = 0;
  453. }
  454. /* determine if the block can be merged with the next neighbor. */
  455. if (!RT_MEMHEAP_IS_USED(header_ptr->next))
  456. {
  457. /* adjust the available number of bytes. */
  458. heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
  459. /* merge block with next neighbor. */
  460. new_ptr = header_ptr->next;
  461. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  462. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  463. new_ptr, new_ptr->next_free, new_ptr->prev_free));
  464. new_ptr->next->prev = header_ptr;
  465. header_ptr->next = new_ptr->next;
  466. /* remove new ptr from free list */
  467. new_ptr->next_free->prev_free = new_ptr->prev_free;
  468. new_ptr->prev_free->next_free = new_ptr->next_free;
  469. }
  470. if (insert_header)
  471. {
  472. /* no left merge, insert to free list */
  473. header_ptr->next_free = heap->free_list->next_free;
  474. header_ptr->prev_free = heap->free_list;
  475. heap->free_list->next_free->prev_free = header_ptr;
  476. heap->free_list->next_free = header_ptr;
  477. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  478. ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
  479. header_ptr->next_free, header_ptr->prev_free));
  480. }
  481. /* release lock */
  482. rt_sem_release(&(heap->lock));
  483. }
  484. RTM_EXPORT(rt_memheap_free);
  485. #ifdef RT_USING_MEMHEAP_AS_HEAP
  486. static struct rt_memheap _heap;
  487. void rt_system_heap_init(void *begin_addr, void *end_addr)
  488. {
  489. /* initialize a default heap in the system */
  490. rt_memheap_init(&_heap,
  491. "heap",
  492. begin_addr,
  493. (rt_uint32_t)end_addr - (rt_uint32_t)begin_addr);
  494. }
  495. void *rt_malloc(rt_size_t size)
  496. {
  497. void* ptr;
  498. /* try to allocate in system heap */
  499. ptr = rt_memheap_alloc(&_heap, size);
  500. if (ptr == RT_NULL)
  501. {
  502. struct rt_object *object;
  503. struct rt_list_node *node;
  504. struct rt_memheap *heap;
  505. struct rt_object_information *information;
  506. extern struct rt_object_information rt_object_container[];
  507. /* try to allocate on other memory heap */
  508. information = &rt_object_container[RT_Object_Class_MemHeap];
  509. for (node = information->object_list.next;
  510. node != &(information->object_list);
  511. node = node->next)
  512. {
  513. object = rt_list_entry(node, struct rt_object, list);
  514. heap = (struct rt_memheap *)object;
  515. /* not allocate in the default system heap */
  516. if (heap == &_heap)
  517. continue;
  518. ptr = rt_memheap_alloc(heap, size);
  519. if (ptr != RT_NULL)
  520. break;
  521. }
  522. }
  523. return ptr;
  524. }
  525. RTM_EXPORT(rt_malloc);
  526. void rt_free(void *rmem)
  527. {
  528. rt_memheap_free(rmem);
  529. }
  530. RTM_EXPORT(rt_free);
  531. void *rt_realloc(void *rmem, rt_size_t newsize)
  532. {
  533. void *new_ptr;
  534. struct rt_memheap_item *header_ptr;
  535. if (rmem == RT_NULL)
  536. return rt_malloc(newsize);
  537. /* get old memory item */
  538. header_ptr = (struct rt_memheap_item *)
  539. ((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);
  540. new_ptr = rt_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
  541. if (new_ptr == RT_NULL && newsize != 0)
  542. {
  543. /* allocate memory block from other memheap */
  544. new_ptr = rt_malloc(newsize);
  545. if (new_ptr != RT_NULL && rmem != RT_NULL)
  546. {
  547. rt_size_t oldsize;
  548. /* get the size of old memory block */
  549. oldsize = MEMITEM_SIZE(header_ptr);
  550. if (newsize > oldsize)
  551. rt_memcpy(new_ptr, rmem, oldsize);
  552. else
  553. rt_memcpy(new_ptr, rmem, newsize);
  554. }
  555. }
  556. return new_ptr;
  557. }
  558. RTM_EXPORT(rt_realloc);
  559. void *rt_calloc(rt_size_t count, rt_size_t size)
  560. {
  561. void *ptr;
  562. rt_size_t total_size;
  563. total_size = count * size;
  564. ptr = rt_malloc(total_size);
  565. if (ptr != RT_NULL)
  566. {
  567. /* clean memory */
  568. rt_memset(ptr, 0, total_size);
  569. }
  570. return ptr;
  571. }
  572. RTM_EXPORT(rt_calloc);
  573. #endif
  574. #endif