lwp_memheap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * File : lwp_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_lwp_memheap_realloc function.
  27. * 2013-05-24 Bernard fix the rt_lwp_memheap_realloc issue.
  28. * 2013-07-11 Grissiom fix the memory block splitting issue.
  29. * 2013-07-15 Grissiom optimize rt_lwp_memheap_realloc
  30. */
  31. #include <rthw.h>
  32. #include <rtthread.h>
  33. #include <lwp.h>
  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_lwp_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_lwp_memheap_init(struct rt_lwp_memheap *memheap,
  55. const char *name,
  56. void *start_addr,
  57. rt_uint32_t size)
  58. {
  59. struct rt_lwp_memheap_item *item;
  60. RT_ASSERT(memheap != RT_NULL);
  61. /* initialize pool object */
  62. memheap->start_addr = start_addr;
  63. memheap->pool_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  64. memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE);
  65. memheap->max_used_size = memheap->pool_size - memheap->available_size;
  66. /* initialize the free list header */
  67. item = &(memheap->free_header);
  68. item->magic = RT_MEMHEAP_MAGIC;
  69. item->pool_ptr = memheap;
  70. item->next = RT_NULL;
  71. item->prev = RT_NULL;
  72. item->next_free = item;
  73. item->prev_free = item;
  74. /* set the free list to free list header */
  75. memheap->free_list = item;
  76. /* initialize the first big memory block */
  77. item = (struct rt_lwp_memheap_item *)start_addr;
  78. item->magic = RT_MEMHEAP_MAGIC;
  79. item->pool_ptr = memheap;
  80. item->next = RT_NULL;
  81. item->prev = RT_NULL;
  82. item->next_free = item;
  83. item->prev_free = item;
  84. item->next = (struct rt_lwp_memheap_item *)
  85. ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
  86. item->prev = item->next;
  87. /* block list header */
  88. memheap->block_list = item;
  89. /* place the big memory block to free list */
  90. item->next_free = memheap->free_list->next_free;
  91. item->prev_free = memheap->free_list;
  92. memheap->free_list->next_free->prev_free = item;
  93. memheap->free_list->next_free = item;
  94. /* move to the end of memory pool to build a small tailer block,
  95. * which prevents block merging
  96. */
  97. item = item->next;
  98. /* it's a used memory block */
  99. item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
  100. item->pool_ptr = memheap;
  101. item->next = (struct rt_lwp_memheap_item *)start_addr;
  102. item->prev = (struct rt_lwp_memheap_item *)start_addr;
  103. /* not in free list */
  104. item->next_free = item->prev_free = RT_NULL;
  105. /* initialize semaphore lock */
  106. rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_FIFO);
  107. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  108. ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
  109. start_addr, size, &(memheap->free_header)));
  110. return RT_EOK;
  111. }
  112. void *rt_lwp_memheap_alloc(struct rt_lwp_memheap *heap, rt_uint32_t size)
  113. {
  114. rt_err_t result;
  115. rt_uint32_t free_size;
  116. struct rt_lwp_memheap_item *header_ptr;
  117. RT_ASSERT(heap != RT_NULL);
  118. /* align allocated size */
  119. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  120. if (size < RT_MEMHEAP_MINIALLOC)
  121. size = RT_MEMHEAP_MINIALLOC;
  122. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
  123. size, RT_NAME_MAX, heap->parent.name));
  124. if (size < heap->available_size)
  125. {
  126. /* search on free list */
  127. free_size = 0;
  128. /* lock memheap */
  129. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  130. if (result != RT_EOK)
  131. {
  132. rt_set_errno(result);
  133. return RT_NULL;
  134. }
  135. /* get the first free memory block */
  136. header_ptr = heap->free_list->next_free;
  137. while (header_ptr != heap->free_list && free_size < size)
  138. {
  139. /* get current freed memory block size */
  140. free_size = MEMITEM_SIZE(header_ptr);
  141. if (free_size < size)
  142. {
  143. /* move to next free memory block */
  144. header_ptr = header_ptr->next_free;
  145. }
  146. }
  147. /* determine if the memory is available. */
  148. if (free_size >= size)
  149. {
  150. /* a block that satisfies the request has been found. */
  151. /* determine if the block needs to be split. */
  152. if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
  153. {
  154. struct rt_lwp_memheap_item *new_ptr;
  155. /* split the block. */
  156. new_ptr = (struct rt_lwp_memheap_item *)
  157. (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
  158. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  159. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  160. header_ptr,
  161. header_ptr->next,
  162. header_ptr->prev,
  163. new_ptr));
  164. /* mark the new block as a memory block and freed. */
  165. new_ptr->magic = RT_MEMHEAP_MAGIC;
  166. /* put the pool pointer into the new block. */
  167. new_ptr->pool_ptr = heap;
  168. /* break down the block list */
  169. new_ptr->prev = header_ptr;
  170. new_ptr->next = header_ptr->next;
  171. header_ptr->next->prev = new_ptr;
  172. header_ptr->next = new_ptr;
  173. /* remove header ptr from free list */
  174. header_ptr->next_free->prev_free = header_ptr->prev_free;
  175. header_ptr->prev_free->next_free = header_ptr->next_free;
  176. header_ptr->next_free = RT_NULL;
  177. header_ptr->prev_free = RT_NULL;
  178. /* insert new_ptr to free list */
  179. new_ptr->next_free = heap->free_list->next_free;
  180. new_ptr->prev_free = heap->free_list;
  181. heap->free_list->next_free->prev_free = new_ptr;
  182. heap->free_list->next_free = new_ptr;
  183. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
  184. new_ptr->next_free,
  185. new_ptr->prev_free));
  186. /* decrement the available byte count. */
  187. heap->available_size = heap->available_size -
  188. size -
  189. RT_MEMHEAP_SIZE;
  190. if (heap->pool_size - heap->available_size > heap->max_used_size)
  191. heap->max_used_size = heap->pool_size - heap->available_size;
  192. }
  193. else
  194. {
  195. /* decrement the entire free size from the available bytes count. */
  196. heap->available_size = heap->available_size - free_size;
  197. if (heap->pool_size - heap->available_size > heap->max_used_size)
  198. heap->max_used_size = heap->pool_size - heap->available_size;
  199. /* remove header_ptr from free list */
  200. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  201. ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
  202. header_ptr,
  203. header_ptr->next_free,
  204. header_ptr->prev_free));
  205. header_ptr->next_free->prev_free = header_ptr->prev_free;
  206. header_ptr->prev_free->next_free = header_ptr->next_free;
  207. header_ptr->next_free = RT_NULL;
  208. header_ptr->prev_free = RT_NULL;
  209. }
  210. /* Mark the allocated block as not available. */
  211. header_ptr->magic |= RT_MEMHEAP_USED;
  212. /* release lock */
  213. rt_sem_release(&(heap->lock));
  214. /* Return a memory address to the caller. */
  215. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  216. ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
  217. (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
  218. header_ptr,
  219. size));
  220. return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
  221. }
  222. /* release lock */
  223. rt_sem_release(&(heap->lock));
  224. }
  225. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
  226. /* Return the completion status. */
  227. return RT_NULL;
  228. }
  229. void *rt_lwp_memheap_realloc(struct rt_lwp_memheap *heap, void *ptr, rt_size_t newsize)
  230. {
  231. rt_err_t result;
  232. rt_size_t oldsize;
  233. struct rt_lwp_memheap_item *header_ptr;
  234. struct rt_lwp_memheap_item *new_ptr;
  235. if (newsize == 0)
  236. {
  237. rt_lwp_memheap_free(ptr);
  238. return RT_NULL;
  239. }
  240. /* align allocated size */
  241. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  242. if (newsize < RT_MEMHEAP_MINIALLOC)
  243. newsize = RT_MEMHEAP_MINIALLOC;
  244. if (ptr == RT_NULL)
  245. {
  246. return rt_lwp_memheap_alloc(heap, newsize);
  247. }
  248. /* get memory block header and get the size of memory block */
  249. header_ptr = (struct rt_lwp_memheap_item *)
  250. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  251. oldsize = MEMITEM_SIZE(header_ptr);
  252. /* re-allocate memory */
  253. if (newsize > oldsize)
  254. {
  255. void *new_ptr;
  256. struct rt_lwp_memheap_item *next_ptr;
  257. /* lock memheap */
  258. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  259. if (result != RT_EOK)
  260. {
  261. rt_set_errno(result);
  262. return RT_NULL;
  263. }
  264. next_ptr = header_ptr->next;
  265. /* header_ptr should not be the tail */
  266. RT_ASSERT(next_ptr > header_ptr);
  267. /* check whether the following free space is enough to expand */
  268. if (!RT_MEMHEAP_IS_USED(next_ptr))
  269. {
  270. rt_int32_t nextsize;
  271. nextsize = MEMITEM_SIZE(next_ptr);
  272. RT_ASSERT(next_ptr > 0);
  273. /* Here is the ASCII art of the situation that we can make use of
  274. * the next free node without alloc/memcpy, |*| is the control
  275. * block:
  276. *
  277. * oldsize free node
  278. * |*|-----------|*|----------------------|*|
  279. * newsize >= minialloc
  280. * |*|----------------|*|-----------------|*|
  281. */
  282. if (nextsize + oldsize > newsize + RT_MEMHEAP_MINIALLOC)
  283. {
  284. /* decrement the entire free size from the available bytes count. */
  285. heap->available_size = heap->available_size - (newsize - oldsize);
  286. if (heap->pool_size - heap->available_size > heap->max_used_size)
  287. heap->max_used_size = heap->pool_size - heap->available_size;
  288. /* remove next_ptr from free list */
  289. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  290. ("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
  291. next_ptr,
  292. next_ptr->next_free,
  293. next_ptr->prev_free));
  294. next_ptr->next_free->prev_free = next_ptr->prev_free;
  295. next_ptr->prev_free->next_free = next_ptr->next_free;
  296. next_ptr->next->prev = next_ptr->prev;
  297. next_ptr->prev->next = next_ptr->next;
  298. /* build a new one on the right place */
  299. next_ptr = (struct rt_lwp_memheap_item *)((char *)ptr + newsize);
  300. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  301. ("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
  302. next_ptr,
  303. next_ptr->next,
  304. next_ptr->prev));
  305. /* mark the new block as a memory block and freed. */
  306. next_ptr->magic = RT_MEMHEAP_MAGIC;
  307. /* put the pool pointer into the new block. */
  308. next_ptr->pool_ptr = heap;
  309. next_ptr->prev = header_ptr;
  310. next_ptr->next = header_ptr->next;
  311. header_ptr->next->prev = next_ptr;
  312. header_ptr->next = next_ptr;
  313. /* insert next_ptr to free list */
  314. next_ptr->next_free = heap->free_list->next_free;
  315. next_ptr->prev_free = heap->free_list;
  316. heap->free_list->next_free->prev_free = next_ptr;
  317. heap->free_list->next_free = next_ptr;
  318. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
  319. next_ptr->next_free,
  320. next_ptr->prev_free));
  321. /* release lock */
  322. rt_sem_release(&(heap->lock));
  323. return ptr;
  324. }
  325. }
  326. /* release lock */
  327. rt_sem_release(&(heap->lock));
  328. /* re-allocate a memory block */
  329. new_ptr = (void *)rt_lwp_memheap_alloc(heap, newsize);
  330. if (new_ptr != RT_NULL)
  331. {
  332. rt_memcpy(new_ptr, ptr, oldsize < newsize ? oldsize : newsize);
  333. rt_lwp_memheap_free(ptr);
  334. }
  335. return new_ptr;
  336. }
  337. /* don't split when there is less than one node space left */
  338. if (newsize + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC >= oldsize)
  339. return ptr;
  340. /* lock memheap */
  341. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  342. if (result != RT_EOK)
  343. {
  344. rt_set_errno(result);
  345. return RT_NULL;
  346. }
  347. /* split the block. */
  348. new_ptr = (struct rt_lwp_memheap_item *)
  349. (((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);
  350. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  351. ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
  352. header_ptr,
  353. header_ptr->next,
  354. header_ptr->prev,
  355. new_ptr));
  356. /* mark the new block as a memory block and freed. */
  357. new_ptr->magic = RT_MEMHEAP_MAGIC;
  358. /* put the pool pointer into the new block. */
  359. new_ptr->pool_ptr = heap;
  360. /* break down the block list */
  361. new_ptr->prev = header_ptr;
  362. new_ptr->next = header_ptr->next;
  363. header_ptr->next->prev = new_ptr;
  364. header_ptr->next = new_ptr;
  365. /* determine if the block can be merged with the next neighbor. */
  366. if (!RT_MEMHEAP_IS_USED(new_ptr->next))
  367. {
  368. struct rt_lwp_memheap_item *free_ptr;
  369. /* merge block with next neighbor. */
  370. free_ptr = new_ptr->next;
  371. heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);
  372. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  373. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  374. header_ptr, header_ptr->next_free, header_ptr->prev_free));
  375. free_ptr->next->prev = new_ptr;
  376. new_ptr->next = free_ptr->next;
  377. /* remove free ptr from free list */
  378. free_ptr->next_free->prev_free = free_ptr->prev_free;
  379. free_ptr->prev_free->next_free = free_ptr->next_free;
  380. }
  381. /* insert the split block to free list */
  382. new_ptr->next_free = heap->free_list->next_free;
  383. new_ptr->prev_free = heap->free_list;
  384. heap->free_list->next_free->prev_free = new_ptr;
  385. heap->free_list->next_free = new_ptr;
  386. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
  387. new_ptr->next_free,
  388. new_ptr->prev_free));
  389. /* increment the available byte count. */
  390. heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
  391. /* release lock */
  392. rt_sem_release(&(heap->lock));
  393. /* return the old memory block */
  394. return ptr;
  395. }
  396. void rt_lwp_memheap_free(void *ptr)
  397. {
  398. rt_err_t result;
  399. struct rt_lwp_memheap *heap;
  400. struct rt_lwp_memheap_item *header_ptr, *new_ptr;
  401. rt_uint32_t insert_header;
  402. /* NULL check */
  403. if (ptr == RT_NULL) return;
  404. /* set initial status as OK */
  405. insert_header = 1;
  406. new_ptr = RT_NULL;
  407. header_ptr = (struct rt_lwp_memheap_item *)
  408. ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
  409. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
  410. ptr, header_ptr));
  411. /* check magic */
  412. RT_ASSERT((header_ptr->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
  413. RT_ASSERT(header_ptr->magic & RT_MEMHEAP_USED);
  414. /* check whether this block of memory has been over-written. */
  415. RT_ASSERT((header_ptr->next->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
  416. /* get pool ptr */
  417. heap = header_ptr->pool_ptr;
  418. /* lock memheap */
  419. result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
  420. if (result != RT_EOK)
  421. {
  422. rt_set_errno(result);
  423. return ;
  424. }
  425. /* Mark the memory as available. */
  426. header_ptr->magic &= ~RT_MEMHEAP_USED;
  427. /* Adjust the available number of bytes. */
  428. heap->available_size = heap->available_size + MEMITEM_SIZE(header_ptr);
  429. /* Determine if the block can be merged with the previous neighbor. */
  430. if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
  431. {
  432. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
  433. header_ptr->prev));
  434. /* adjust the available number of bytes. */
  435. heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
  436. /* yes, merge block with previous neighbor. */
  437. (header_ptr->prev)->next = header_ptr->next;
  438. (header_ptr->next)->prev = header_ptr->prev;
  439. /* move header pointer to previous. */
  440. header_ptr = header_ptr->prev;
  441. /* don't insert header to free list */
  442. insert_header = 0;
  443. }
  444. /* determine if the block can be merged with the next neighbor. */
  445. if (!RT_MEMHEAP_IS_USED(header_ptr->next))
  446. {
  447. /* adjust the available number of bytes. */
  448. heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
  449. /* merge block with next neighbor. */
  450. new_ptr = header_ptr->next;
  451. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  452. ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
  453. new_ptr, new_ptr->next_free, new_ptr->prev_free));
  454. new_ptr->next->prev = header_ptr;
  455. header_ptr->next = new_ptr->next;
  456. /* remove new ptr from free list */
  457. new_ptr->next_free->prev_free = new_ptr->prev_free;
  458. new_ptr->prev_free->next_free = new_ptr->next_free;
  459. }
  460. if (insert_header)
  461. {
  462. /* no left merge, insert to free list */
  463. header_ptr->next_free = heap->free_list->next_free;
  464. header_ptr->prev_free = heap->free_list;
  465. heap->free_list->next_free->prev_free = header_ptr;
  466. heap->free_list->next_free = header_ptr;
  467. RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
  468. ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
  469. header_ptr->next_free, header_ptr->prev_free));
  470. }
  471. /* release lock */
  472. rt_sem_release(&(heap->lock));
  473. }
  474. rt_bool_t rt_lwp_memheap_is_empty(struct rt_lwp_memheap *memheap)
  475. {
  476. RT_ASSERT(memheap != RT_NULL);
  477. return (memheap->available_size + 2 * sizeof(struct rt_lwp_memheap_item)) == memheap->pool_size;
  478. }
  479. rt_bool_t rt_lwp_memheap_unavailable_size_get(void)
  480. {
  481. return 2 * RT_MEMHEAP_SIZE + 3;
  482. }