shim_malloc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_malloc.c
  17. *
  18. * This file contains codes for SLAB memory allocator of library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_profile.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_vma.h>
  25. #include <pal.h>
  26. #include <pal_debug.h>
  27. static LOCKTYPE slab_mgr_lock;
  28. #define system_lock() lock(slab_mgr_lock)
  29. #define system_unlock() unlock(slab_mgr_lock)
  30. #define PAGE_SIZE allocsize
  31. #ifdef SLAB_DEBUG_TRACE
  32. # define SLAB_DEBUG
  33. #endif
  34. #define SLAB_CANARY
  35. #define STARTUP_SIZE 4
  36. #include <slabmgr.h>
  37. static SLAB_MGR slab_mgr = NULL;
  38. #define MIN_SHIM_HEAP_PAGES 64
  39. #define MAX_SHIM_HEAP_AREAS 32
  40. #define INIT_SHIM_HEAP 256 * allocsize
  41. static struct shim_heap {
  42. void * start;
  43. void * current;
  44. void * end;
  45. } shim_heap_areas[MAX_SHIM_HEAP_AREAS];
  46. static LOCKTYPE shim_heap_lock;
  47. DEFINE_PROFILE_CATAGORY(memory, );
  48. static struct shim_heap * __alloc_enough_heap (size_t size)
  49. {
  50. struct shim_heap * heap = NULL, * first_empty = NULL, * smallest = NULL;
  51. size_t smallest_size = 0;
  52. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  53. if (shim_heap_areas[i].start) {
  54. if (shim_heap_areas[i].end >= shim_heap_areas[i].current + size)
  55. return &shim_heap_areas[i];
  56. if (!smallest ||
  57. shim_heap_areas[i].end <=
  58. shim_heap_areas[i].current + smallest_size) {
  59. smallest = &shim_heap_areas[i];
  60. smallest_size = shim_heap_areas[i].end -
  61. shim_heap_areas[i].current;
  62. }
  63. } else {
  64. if (!first_empty)
  65. first_empty = &shim_heap_areas[i];
  66. }
  67. if (!heap) {
  68. size_t heap_size = MIN_SHIM_HEAP_PAGES * allocsize;
  69. void * start = NULL;
  70. heap = first_empty ? : smallest;
  71. assert(heap);
  72. while (size > heap_size)
  73. heap_size *= 2;
  74. if (!(start = (void *) DkVirtualMemoryAlloc(NULL, heap_size, 0,
  75. PAL_PROT_WRITE|PAL_PROT_READ)))
  76. return NULL;
  77. debug("allocate internal heap at %p - %p\n", start, start + heap_size);
  78. if (heap == smallest && heap->current != heap->end) {
  79. DkVirtualMemoryFree(heap->current, heap->end - heap->current);
  80. int flags = VMA_INTERNAL;
  81. bkeep_munmap(heap->current, heap->end - heap->current, &flags);
  82. }
  83. heap->start = heap->current = start;
  84. heap->end = start + heap_size;
  85. unlock(shim_heap_lock);
  86. bkeep_mmap(start, heap_size, PROT_READ|PROT_WRITE,
  87. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  88. lock(shim_heap_lock);
  89. }
  90. return heap;
  91. }
  92. void * __system_malloc (size_t size)
  93. {
  94. size_t alloc_size = ALIGN_UP(size);
  95. lock(shim_heap_lock);
  96. struct shim_heap * heap = __alloc_enough_heap(alloc_size);
  97. if (!heap) {
  98. unlock(shim_heap_lock);
  99. return NULL;
  100. }
  101. void * addr = heap->current;
  102. heap->current += alloc_size;
  103. unlock(shim_heap_lock);
  104. return addr;
  105. }
  106. void __system_free (void * addr, size_t size)
  107. {
  108. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  109. int flags = VMA_INTERNAL;
  110. bkeep_munmap(addr, ALIGN_UP(size), &flags);
  111. }
  112. int init_heap (void)
  113. {
  114. create_lock(shim_heap_lock);
  115. void * start = (void *) DkVirtualMemoryAlloc(NULL, INIT_SHIM_HEAP, 0,
  116. PAL_PROT_WRITE|PAL_PROT_READ);
  117. if (!start)
  118. return -ENOMEM;
  119. debug("allocate internal heap at %p - %p\n", start,
  120. start + INIT_SHIM_HEAP);
  121. shim_heap_areas[0].start = shim_heap_areas[0].current = start;
  122. shim_heap_areas[0].end = start + INIT_SHIM_HEAP;
  123. return 0;
  124. }
  125. int bkeep_shim_heap (void)
  126. {
  127. lock(shim_heap_lock);
  128. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  129. if (shim_heap_areas[i].start)
  130. bkeep_mmap(shim_heap_areas[i].start,
  131. shim_heap_areas[i].end - shim_heap_areas[i].start,
  132. PROT_READ|PROT_WRITE,
  133. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  134. unlock(shim_heap_lock);
  135. return 0;
  136. }
  137. int init_slab (void)
  138. {
  139. create_lock(slab_mgr_lock);
  140. slab_mgr = create_slab_mgr();
  141. return 0;
  142. }
  143. extern_alias(init_slab);
  144. int reinit_slab (void)
  145. {
  146. if (slab_mgr) {
  147. destroy_slab_mgr(slab_mgr);
  148. slab_mgr = NULL;
  149. }
  150. return 0;
  151. }
  152. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  153. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  154. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  155. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  156. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  157. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  158. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  159. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  160. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  161. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  162. void * __malloc_debug (size_t size, const char * file, int line)
  163. #else
  164. void * malloc (size_t size)
  165. #endif
  166. {
  167. #ifdef PROFILE
  168. int i;
  169. int level = -1;
  170. for (i = 0 ; i < SLAB_LEVEL ; i++)
  171. if (size < slab_levels[i]) {
  172. level = i;
  173. break;
  174. }
  175. switch(level) {
  176. case 0:
  177. INC_PROFILE_OCCURENCE(malloc_0);
  178. break;
  179. case 1:
  180. INC_PROFILE_OCCURENCE(malloc_1);
  181. break;
  182. case 2:
  183. INC_PROFILE_OCCURENCE(malloc_2);
  184. break;
  185. case 3:
  186. INC_PROFILE_OCCURENCE(malloc_3);
  187. break;
  188. case 4:
  189. INC_PROFILE_OCCURENCE(malloc_4);
  190. break;
  191. case 5:
  192. INC_PROFILE_OCCURENCE(malloc_5);
  193. break;
  194. case 6:
  195. INC_PROFILE_OCCURENCE(malloc_6);
  196. break;
  197. case 7:
  198. INC_PROFILE_OCCURENCE(malloc_7);
  199. break;
  200. case -1:
  201. INC_PROFILE_OCCURENCE(malloc_big);
  202. break;
  203. }
  204. #endif
  205. #ifdef SLAB_DEBUG_TRACE
  206. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  207. #else
  208. void * mem = slab_alloc(slab_mgr, size);
  209. #endif
  210. #ifdef SLAB_DEBUG_PRINT
  211. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  212. #endif
  213. return mem;
  214. }
  215. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  216. extern_alias(malloc);
  217. #endif
  218. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  219. void * __remalloc_debug (const void * mem, size_t size,
  220. const char * file, int line)
  221. #else
  222. void * remalloc (const void * mem, size_t size)
  223. #endif
  224. {
  225. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  226. void * buff = __malloc_debug(size, file, line);
  227. #else
  228. void * buff = malloc(size);
  229. #endif
  230. if (buff)
  231. memcpy(buff, mem, size);
  232. return buff;
  233. }
  234. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  235. extern_alias(remalloc);
  236. #endif
  237. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  238. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  239. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  240. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  241. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  242. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  243. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  244. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  245. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  246. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  247. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  248. void __free_debug (void * mem, const char * file, int line)
  249. #else
  250. void free (void * mem)
  251. #endif
  252. {
  253. if (MEMORY_MIGRATED(mem)) {
  254. INC_PROFILE_OCCURENCE(free_migrated);
  255. return;
  256. }
  257. #ifdef PROFILE
  258. int level = RAW_TO_LEVEL(mem);
  259. switch(level) {
  260. case 0:
  261. INC_PROFILE_OCCURENCE(free_0);
  262. break;
  263. case 1:
  264. INC_PROFILE_OCCURENCE(free_1);
  265. break;
  266. case 2:
  267. INC_PROFILE_OCCURENCE(free_2);
  268. break;
  269. case 3:
  270. INC_PROFILE_OCCURENCE(free_3);
  271. break;
  272. case 4:
  273. INC_PROFILE_OCCURENCE(free_4);
  274. break;
  275. case 5:
  276. INC_PROFILE_OCCURENCE(free_5);
  277. break;
  278. case 6:
  279. INC_PROFILE_OCCURENCE(free_6);
  280. break;
  281. case 7:
  282. INC_PROFILE_OCCURENCE(free_7);
  283. break;
  284. case -1:
  285. case 255:
  286. INC_PROFILE_OCCURENCE(free_big);
  287. break;
  288. }
  289. #endif
  290. #ifdef SLAB_DEBUG_PRINT
  291. debug("free(%p) (%s:%d)\n", mem, file, line);
  292. #endif
  293. #ifdef SLAB_DEBUG_TRACE
  294. slab_free_debug(slab_mgr, mem, file, line);
  295. #else
  296. slab_free(slab_mgr, mem);
  297. #endif
  298. }
  299. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  300. extern_alias(free);
  301. #endif