mem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * File : mem.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 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. * 2008-7-12 Bernard the first version
  23. * 2010-06-09 Bernard fix the end stub of heap
  24. * fix memory check in rt_realloc function
  25. * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
  26. * 2010-10-14 Bernard fix rt_realloc issue when realloc a NULL pointer.
  27. */
  28. /*
  29. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  30. * All rights reserved.
  31. *
  32. * Redistribution and use in source and binary forms, with or without modification,
  33. * are permitted provided that the following conditions are met:
  34. *
  35. * 1. Redistributions of source code must retain the above copyright notice,
  36. * this list of conditions and the following disclaimer.
  37. * 2. Redistributions in binary form must reproduce the above copyright notice,
  38. * this list of conditions and the following disclaimer in the documentation
  39. * and/or other materials provided with the distribution.
  40. * 3. The name of the author may not be used to endorse or promote products
  41. * derived from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  44. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  45. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  46. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  47. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  48. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  49. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  50. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  51. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  52. * OF SUCH DAMAGE.
  53. *
  54. * This file is part of the lwIP TCP/IP stack.
  55. *
  56. * Author: Adam Dunkels <adam@sics.se>
  57. * Simon Goldschmidt
  58. *
  59. */
  60. #include <rthw.h>
  61. #include <rtthread.h>
  62. #ifndef RT_USING_MEMHEAP_AS_HEAP
  63. /* #define RT_MEM_DEBUG */
  64. #define RT_MEM_STATS
  65. #if defined (RT_USING_HEAP) && defined (RT_USING_SMALL_MEM)
  66. #ifdef RT_USING_HOOK
  67. static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
  68. static void (*rt_free_hook)(void *ptr);
  69. /**
  70. * @addtogroup Hook
  71. */
  72. /*@{*/
  73. /**
  74. * This function will set a hook function, which will be invoked when a memory
  75. * block is allocated from heap memory.
  76. *
  77. * @param hook the hook function
  78. */
  79. void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
  80. {
  81. rt_malloc_hook = hook;
  82. }
  83. /**
  84. * This function will set a hook function, which will be invoked when a memory
  85. * block is released to heap memory.
  86. *
  87. * @param hook the hook function
  88. */
  89. void rt_free_sethook(void (*hook)(void *ptr))
  90. {
  91. rt_free_hook = hook;
  92. }
  93. /*@}*/
  94. #endif
  95. #define HEAP_MAGIC 0x1ea0
  96. struct heap_mem
  97. {
  98. /* magic and used flag */
  99. rt_uint16_t magic;
  100. rt_uint16_t used;
  101. rt_size_t next, prev;
  102. };
  103. /** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
  104. static rt_uint8_t *heap_ptr;
  105. /** the last entry, always unused! */
  106. static struct heap_mem *heap_end;
  107. #define MIN_SIZE 12
  108. #define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE)
  109. #define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE)
  110. static struct heap_mem *lfree; /* pointer to the lowest free block */
  111. static struct rt_semaphore heap_sem;
  112. static rt_size_t mem_size_aligned;
  113. #ifdef RT_MEM_STATS
  114. static rt_size_t used_mem, max_mem;
  115. #endif
  116. static void plug_holes(struct heap_mem *mem)
  117. {
  118. struct heap_mem *nmem;
  119. struct heap_mem *pmem;
  120. RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);
  121. RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
  122. RT_ASSERT(mem->used == 0);
  123. /* plug hole forward */
  124. nmem = (struct heap_mem *)&heap_ptr[mem->next];
  125. if (mem != nmem &&
  126. nmem->used == 0 &&
  127. (rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
  128. {
  129. /* if mem->next is unused and not end of heap_ptr,
  130. * combine mem and mem->next
  131. */
  132. if (lfree == nmem)
  133. {
  134. lfree = mem;
  135. }
  136. mem->next = nmem->next;
  137. ((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
  138. }
  139. /* plug hole backward */
  140. pmem = (struct heap_mem *)&heap_ptr[mem->prev];
  141. if (pmem != mem && pmem->used == 0)
  142. {
  143. /* if mem->prev is unused, combine mem and mem->prev */
  144. if (lfree == mem)
  145. {
  146. lfree = pmem;
  147. }
  148. pmem->next = mem->next;
  149. ((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
  150. }
  151. }
  152. /**
  153. * @ingroup SystemInit
  154. *
  155. * This function will initialize system heap memory.
  156. *
  157. * @param begin_addr the beginning address of system heap memory.
  158. * @param end_addr the end address of system heap memory.
  159. */
  160. void rt_system_heap_init(void *begin_addr, void *end_addr)
  161. {
  162. struct heap_mem *mem;
  163. rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
  164. rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);
  165. RT_DEBUG_NOT_IN_INTERRUPT;
  166. /* alignment addr */
  167. if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
  168. ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
  169. {
  170. /* calculate the aligned memory size */
  171. mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
  172. }
  173. else
  174. {
  175. rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
  176. (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
  177. return;
  178. }
  179. /* point to begin address of heap */
  180. heap_ptr = (rt_uint8_t *)begin_align;
  181. RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
  182. (rt_uint32_t)heap_ptr, mem_size_aligned));
  183. /* initialize the start of the heap */
  184. mem = (struct heap_mem *)heap_ptr;
  185. mem->magic = HEAP_MAGIC;
  186. mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
  187. mem->prev = 0;
  188. mem->used = 0;
  189. /* initialize the end of the heap */
  190. heap_end = (struct heap_mem *)&heap_ptr[mem->next];
  191. heap_end->magic = HEAP_MAGIC;
  192. heap_end->used = 1;
  193. heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
  194. heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
  195. rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
  196. /* initialize the lowest-free pointer to the start of the heap */
  197. lfree = (struct heap_mem *)heap_ptr;
  198. }
  199. /**
  200. * @addtogroup MM
  201. */
  202. /*@{*/
  203. /**
  204. * Allocate a block of memory with a minimum of 'size' bytes.
  205. *
  206. * @param size is the minimum size of the requested block in bytes.
  207. *
  208. * @return pointer to allocated memory or NULL if no free memory was found.
  209. */
  210. void *rt_malloc(rt_size_t size)
  211. {
  212. rt_size_t ptr, ptr2;
  213. struct heap_mem *mem, *mem2;
  214. RT_DEBUG_NOT_IN_INTERRUPT;
  215. if (size == 0)
  216. return RT_NULL;
  217. if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
  218. RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
  219. size, RT_ALIGN(size, RT_ALIGN_SIZE)));
  220. else
  221. RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));
  222. /* alignment size */
  223. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  224. if (size > mem_size_aligned)
  225. {
  226. RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
  227. return RT_NULL;
  228. }
  229. /* every data block must be at least MIN_SIZE_ALIGNED long */
  230. if (size < MIN_SIZE_ALIGNED)
  231. size = MIN_SIZE_ALIGNED;
  232. /* take memory semaphore */
  233. rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
  234. for (ptr = (rt_uint8_t *)lfree - heap_ptr;
  235. ptr < mem_size_aligned - size;
  236. ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
  237. {
  238. mem = (struct heap_mem *)&heap_ptr[ptr];
  239. if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
  240. {
  241. /* mem is not used and at least perfect fit is possible:
  242. * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
  243. if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
  244. (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
  245. {
  246. /* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
  247. * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
  248. * -> split large block, create empty remainder,
  249. * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
  250. * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
  251. * struct heap_mem would fit in but no data between mem2 and mem2->next
  252. * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
  253. * region that couldn't hold data, but when mem->next gets freed,
  254. * the 2 regions would be combined, resulting in more free memory
  255. */
  256. ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
  257. /* create mem2 struct */
  258. mem2 = (struct heap_mem *)&heap_ptr[ptr2];
  259. mem2->used = 0;
  260. mem2->next = mem->next;
  261. mem2->prev = ptr;
  262. /* and insert it between mem and mem->next */
  263. mem->next = ptr2;
  264. mem->used = 1;
  265. if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
  266. {
  267. ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
  268. }
  269. #ifdef RT_MEM_STATS
  270. used_mem += (size + SIZEOF_STRUCT_MEM);
  271. if (max_mem < used_mem)
  272. max_mem = used_mem;
  273. #endif
  274. }
  275. else
  276. {
  277. /* (a mem2 struct does no fit into the user data space of mem and mem->next will always
  278. * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
  279. * take care of this).
  280. * -> near fit or excact fit: do not split, no mem2 creation
  281. * also can't move mem->next directly behind mem, since mem->next
  282. * will always be used at this point!
  283. */
  284. mem->used = 1;
  285. #ifdef RT_MEM_STATS
  286. used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
  287. if (max_mem < used_mem)
  288. max_mem = used_mem;
  289. #endif
  290. }
  291. /* set memory block magic */
  292. mem->magic = HEAP_MAGIC;
  293. if (mem == lfree)
  294. {
  295. /* Find next free block after mem and update lowest free pointer */
  296. while (lfree->used && lfree != heap_end)
  297. lfree = (struct heap_mem *)&heap_ptr[lfree->next];
  298. RT_ASSERT(((lfree == heap_end) || (!lfree->used)));
  299. }
  300. rt_sem_release(&heap_sem);
  301. RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
  302. RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
  303. RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);
  304. RT_DEBUG_LOG(RT_DEBUG_MEM,
  305. ("allocate memory at 0x%x, size: %d\n",
  306. (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
  307. (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
  308. RT_OBJECT_HOOK_CALL(rt_malloc_hook,
  309. (((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
  310. /* return the memory data except mem struct */
  311. return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
  312. }
  313. }
  314. rt_sem_release(&heap_sem);
  315. return RT_NULL;
  316. }
  317. RTM_EXPORT(rt_malloc);
  318. /**
  319. * This function will change the previously allocated memory block.
  320. *
  321. * @param rmem pointer to memory allocated by rt_malloc
  322. * @param newsize the required new size
  323. *
  324. * @return the changed memory block address
  325. */
  326. void *rt_realloc(void *rmem, rt_size_t newsize)
  327. {
  328. rt_size_t size;
  329. rt_size_t ptr, ptr2;
  330. struct heap_mem *mem, *mem2;
  331. void *nmem;
  332. RT_DEBUG_NOT_IN_INTERRUPT;
  333. /* alignment size */
  334. newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
  335. if (newsize > mem_size_aligned)
  336. {
  337. RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
  338. return RT_NULL;
  339. }
  340. /* allocate a new memory block */
  341. if (rmem == RT_NULL)
  342. return rt_malloc(newsize);
  343. rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
  344. if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
  345. (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
  346. {
  347. /* illegal memory */
  348. rt_sem_release(&heap_sem);
  349. return rmem;
  350. }
  351. mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  352. ptr = (rt_uint8_t *)mem - heap_ptr;
  353. size = mem->next - ptr - SIZEOF_STRUCT_MEM;
  354. if (size == newsize)
  355. {
  356. /* the size is the same as */
  357. rt_sem_release(&heap_sem);
  358. return rmem;
  359. }
  360. if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
  361. {
  362. /* split memory block */
  363. #ifdef RT_MEM_STATS
  364. used_mem -= (size - newsize);
  365. #endif
  366. ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
  367. mem2 = (struct heap_mem *)&heap_ptr[ptr2];
  368. mem2->magic= HEAP_MAGIC;
  369. mem2->used = 0;
  370. mem2->next = mem->next;
  371. mem2->prev = ptr;
  372. mem->next = ptr2;
  373. if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
  374. {
  375. ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
  376. }
  377. plug_holes(mem2);
  378. rt_sem_release(&heap_sem);
  379. return rmem;
  380. }
  381. rt_sem_release(&heap_sem);
  382. /* expand memory */
  383. nmem = rt_malloc(newsize);
  384. if (nmem != RT_NULL) /* check memory */
  385. {
  386. rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
  387. rt_free(rmem);
  388. }
  389. return nmem;
  390. }
  391. RTM_EXPORT(rt_realloc);
  392. /**
  393. * This function will contiguously allocate enough space for count objects
  394. * that are size bytes of memory each and returns a pointer to the allocated
  395. * memory.
  396. *
  397. * The allocated memory is filled with bytes of value zero.
  398. *
  399. * @param count number of objects to allocate
  400. * @param size size of the objects to allocate
  401. *
  402. * @return pointer to allocated memory / NULL pointer if there is an error
  403. */
  404. void *rt_calloc(rt_size_t count, rt_size_t size)
  405. {
  406. void *p;
  407. RT_DEBUG_NOT_IN_INTERRUPT;
  408. /* allocate 'count' objects of size 'size' */
  409. p = rt_malloc(count * size);
  410. /* zero the memory */
  411. if (p)
  412. rt_memset(p, 0, count * size);
  413. return p;
  414. }
  415. RTM_EXPORT(rt_calloc);
  416. /**
  417. * This function will release the previously allocated memory block by
  418. * rt_malloc. The released memory block is taken back to system heap.
  419. *
  420. * @param rmem the address of memory which will be released
  421. */
  422. void rt_free(void *rmem)
  423. {
  424. struct heap_mem *mem;
  425. RT_DEBUG_NOT_IN_INTERRUPT;
  426. if (rmem == RT_NULL)
  427. return;
  428. RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0);
  429. RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr &&
  430. (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);
  431. RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
  432. if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
  433. (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
  434. {
  435. RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));
  436. return;
  437. }
  438. /* Get the corresponding struct heap_mem ... */
  439. mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
  440. RT_DEBUG_LOG(RT_DEBUG_MEM,
  441. ("release memory 0x%x, size: %d\n",
  442. (rt_uint32_t)rmem,
  443. (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
  444. /* protect the heap from concurrent access */
  445. rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
  446. /* ... which has to be in a used state ... */
  447. RT_ASSERT(mem->used);
  448. RT_ASSERT(mem->magic == HEAP_MAGIC);
  449. /* ... and is now unused. */
  450. mem->used = 0;
  451. mem->magic = 0;
  452. if (mem < lfree)
  453. {
  454. /* the newly freed struct is now the lowest */
  455. lfree = mem;
  456. }
  457. #ifdef RT_MEM_STATS
  458. used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));
  459. #endif
  460. /* finally, see if prev or next are free also */
  461. plug_holes(mem);
  462. rt_sem_release(&heap_sem);
  463. }
  464. RTM_EXPORT(rt_free);
  465. #ifdef RT_MEM_STATS
  466. void rt_memory_info(rt_uint32_t *total,
  467. rt_uint32_t *used,
  468. rt_uint32_t *max_used)
  469. {
  470. if (total != RT_NULL)
  471. *total = mem_size_aligned;
  472. if (used != RT_NULL)
  473. *used = used_mem;
  474. if (max_used != RT_NULL)
  475. *max_used = max_mem;
  476. }
  477. #ifdef RT_USING_FINSH
  478. #include <finsh.h>
  479. void list_mem(void)
  480. {
  481. rt_kprintf("total memory: %d\n", mem_size_aligned);
  482. rt_kprintf("used memory : %d\n", used_mem);
  483. rt_kprintf("maximum allocated memory: %d\n", max_mem);
  484. }
  485. FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
  486. #endif
  487. #endif
  488. /*@}*/
  489. #endif /* end of RT_USING_HEAP */
  490. #endif /* end of RT_USING_MEMHEAP_AS_HEAP */