1
0

sysheap.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * sysheap.c
  3. *
  4. * This is a replacement of the POSIX memory management system
  5. *
  6. * It uses sbrk() to obtain memory chunks for use by the lub_heap
  7. * component
  8. *
  9. * We undefine the public functions
  10. * (just in case they've been MACRO overriden
  11. */
  12. #undef calloc
  13. #undef cfree
  14. #undef free
  15. #undef malloc
  16. #undef memalign
  17. #undef realloc
  18. #undef valloc
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/mman.h>
  22. #include <pthread.h>
  23. #include <string.h>
  24. #define __USE_XOPEN_EXTENDED /* needed for sbrk() */
  25. #include <unistd.h>
  26. #include "lub/partition/posix/private.h"
  27. #define VX_PAGE_SIZE 4096
  28. /* partition used for the system heap */
  29. static lub_posix_partition_t sysMemPartition;
  30. static lub_partition_t *sys_partition = &sysMemPartition.m_base;
  31. static lub_heap_show_e show_mode = LUB_HEAP_SHOW_LEAKS;
  32. static const char *leak_filter = 0;
  33. /* the partition is extended in 128K chunks as needed */
  34. #define DEFAULT_CHUNK_SIZE (128 * 1024)
  35. /*-------------------------------------------------------- */
  36. static void *
  37. sysheap_segment_alloc(size_t required)
  38. {
  39. return sbrk(required);
  40. }
  41. /*-------------------------------------------------------- */
  42. void
  43. sysheap_atexit(void)
  44. {
  45. /* dump the memory leak status */
  46. lub_heap_leak_report(show_mode,leak_filter);
  47. }
  48. /*-------------------------------------------------------- */
  49. static void
  50. sysheap_init_memory(size_t required)
  51. {
  52. static bool_t initialised;
  53. if(BOOL_FALSE == initialised)
  54. {
  55. lub_partition_spec_t spec =
  56. {
  57. BOOL_TRUE, /* use_local_heap */
  58. 8192, /* max_local_block_size */
  59. 8, /* num_local_max_blocks */
  60. 1 * 1024 * 1024, /* min_segment_size */
  61. 0, /* memory_limit */
  62. sysheap_segment_alloc /* sysalloc */
  63. };
  64. initialised = BOOL_TRUE;
  65. lub_posix_partition_init(&sysMemPartition,&spec);
  66. /* lub_heap_init("");
  67. */
  68. atexit(sysheap_atexit);
  69. }
  70. }
  71. /*-------------------------------------------------------- */
  72. static void
  73. sysheap_check_status(lub_heap_status_t status,
  74. const char *where,
  75. void *block,
  76. size_t size)
  77. {
  78. if(LUB_HEAP_OK != status)
  79. {
  80. switch(status)
  81. {
  82. /*------------------------------------------------- */
  83. case LUB_HEAP_CORRUPTED:
  84. {
  85. fprintf(stderr,"%s: Heap corrupted\n",where);
  86. break;
  87. }
  88. /*------------------------------------------------- */
  89. case LUB_HEAP_DOUBLE_FREE:
  90. {
  91. fprintf(stderr,"%s: Double free of 0x%p\n",where,block);
  92. break;
  93. }
  94. /*------------------------------------------------- */
  95. case LUB_HEAP_INVALID_POINTER:
  96. {
  97. fprintf(stderr,"%s: Invalid Pointer 0x%p\n",where,block);
  98. break;
  99. }
  100. /*------------------------------------------------- */
  101. case LUB_HEAP_FAILED:
  102. {
  103. fprintf(stderr,"%s: allocation of %lu bytes failed\n",
  104. where,size);
  105. break;
  106. }
  107. /*------------------------------------------------- */
  108. case LUB_HEAP_OK:
  109. {
  110. break;
  111. }
  112. /*------------------------------------------------- */
  113. }
  114. }
  115. }
  116. /*-------------------------------------------------------- */
  117. void *
  118. calloc(size_t nmemb,
  119. size_t size)
  120. {
  121. char *ptr = malloc(nmemb*size);
  122. if(NULL != ptr)
  123. {
  124. memset(ptr,0,size);
  125. }
  126. return ptr;
  127. }
  128. /*-------------------------------------------------------- */
  129. void
  130. cfree(void *ptr)
  131. {
  132. free(ptr);
  133. }
  134. /*-------------------------------------------------------- */
  135. void
  136. free(void *ptr)
  137. {
  138. char *pBlock = ptr;
  139. lub_heap_status_t status;
  140. sysheap_init_memory(0);
  141. status = lub_partition_realloc(sys_partition,
  142. &pBlock,
  143. 0,
  144. LUB_HEAP_ALIGN_NATIVE);
  145. sysheap_check_status(status,"free",pBlock,0);
  146. }
  147. /*-------------------------------------------------------- */
  148. void *
  149. malloc(size_t nBytes)
  150. {
  151. char *pBlock = NULL;
  152. lub_heap_status_t status;
  153. sysheap_init_memory(nBytes);
  154. status = lub_partition_realloc(sys_partition,
  155. &pBlock,
  156. nBytes,
  157. LUB_HEAP_ALIGN_NATIVE);
  158. sysheap_check_status(status,"malloc",NULL,nBytes);
  159. return pBlock;
  160. }
  161. /*-------------------------------------------------------- */
  162. void *
  163. memalign(unsigned alignment,
  164. unsigned nBytes)
  165. {
  166. char *pBlock = NULL;
  167. lub_heap_status_t status = LUB_HEAP_OK;
  168. lub_heap_align_t align;
  169. switch(alignment)
  170. {
  171. case 4: align = LUB_HEAP_ALIGN_2_POWER_2; break;
  172. case 8: align = LUB_HEAP_ALIGN_2_POWER_3; break;
  173. case 16: align = LUB_HEAP_ALIGN_2_POWER_4; break;
  174. case 32: align = LUB_HEAP_ALIGN_2_POWER_5; break;
  175. case 64: align = LUB_HEAP_ALIGN_2_POWER_6; break;
  176. case 128: align = LUB_HEAP_ALIGN_2_POWER_7; break;
  177. case 256: align = LUB_HEAP_ALIGN_2_POWER_8; break;
  178. case 512: align = LUB_HEAP_ALIGN_2_POWER_9; break;
  179. case 1024: align = LUB_HEAP_ALIGN_2_POWER_10; break;
  180. case 2048: align = LUB_HEAP_ALIGN_2_POWER_11; break;
  181. case 4096: align = LUB_HEAP_ALIGN_2_POWER_12; break;
  182. case 8192: align = LUB_HEAP_ALIGN_2_POWER_13; break;
  183. case 16384: align = LUB_HEAP_ALIGN_2_POWER_14; break;
  184. case 32768: align = LUB_HEAP_ALIGN_2_POWER_15; break;
  185. case 65536: align = LUB_HEAP_ALIGN_2_POWER_16; break;
  186. case 131072: align = LUB_HEAP_ALIGN_2_POWER_17; break;
  187. case 262144: align = LUB_HEAP_ALIGN_2_POWER_18; break;
  188. case 524288: align = LUB_HEAP_ALIGN_2_POWER_19; break;
  189. case 1048576: align = LUB_HEAP_ALIGN_2_POWER_20; break;
  190. case 2097152: align = LUB_HEAP_ALIGN_2_POWER_21; break;
  191. case 4194304: align = LUB_HEAP_ALIGN_2_POWER_22; break;
  192. case 8388608: align = LUB_HEAP_ALIGN_2_POWER_23; break;
  193. case 16777216: align = LUB_HEAP_ALIGN_2_POWER_24; break;
  194. case 33554432: align = LUB_HEAP_ALIGN_2_POWER_25; break;
  195. case 67108864: align = LUB_HEAP_ALIGN_2_POWER_26; break;
  196. case 134217728: align = LUB_HEAP_ALIGN_2_POWER_27; break;
  197. default: status = LUB_HEAP_FAILED; break;
  198. }
  199. if(LUB_HEAP_OK == status)
  200. {
  201. status = lub_partition_realloc(sys_partition,
  202. &pBlock,
  203. nBytes,
  204. align);
  205. }
  206. sysheap_check_status(status,"memalign",pBlock,nBytes);
  207. return (LUB_HEAP_OK == status) ? pBlock : NULL;
  208. }
  209. /*-------------------------------------------------------- */
  210. void *
  211. realloc(void *old_ptr,
  212. size_t nBytes)
  213. {
  214. char *pBlock = old_ptr;
  215. lub_heap_status_t status;
  216. sysheap_init_memory(nBytes);
  217. status = lub_partition_realloc(sys_partition,
  218. &pBlock,
  219. nBytes,
  220. LUB_HEAP_ALIGN_NATIVE);
  221. sysheap_check_status(status,"realloc",pBlock,nBytes);
  222. return (LUB_HEAP_OK == status) ? pBlock : NULL;
  223. }
  224. /*-------------------------------------------------------- */
  225. void *
  226. valloc(unsigned size)
  227. {
  228. return memalign(VX_PAGE_SIZE,size);
  229. }
  230. /*-------------------------------------------------------- */
  231. void
  232. sysheap_suppress_leak_detection(void)
  233. {
  234. lub_partition_disable_leak_detection(sys_partition);
  235. }
  236. /*-------------------------------------------------------- */
  237. void
  238. sysheap_restore_leak_detection(void)
  239. {
  240. lub_partition_enable_leak_detection(sys_partition);
  241. }
  242. /*-------------------------------------------------------- */