shim_malloc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <slabmgr.h>
  36. static SLAB_MGR slab_mgr = NULL;
  37. #define MIN_SHIM_HEAP_PAGES 64
  38. #define MAX_SHIM_HEAP_AREAS 32
  39. #define INIT_SHIM_HEAP 256 * allocsize
  40. static struct shim_heap {
  41. void * start;
  42. void * current;
  43. void * end;
  44. } shim_heap_areas[MAX_SHIM_HEAP_AREAS];
  45. static LOCKTYPE shim_heap_lock;
  46. DEFINE_PROFILE_CATAGORY(memory, );
  47. static struct shim_heap * __alloc_enough_heap (size_t size)
  48. {
  49. struct shim_heap * heap = NULL, * first_empty = NULL, * smallest = NULL;
  50. size_t smallest_size = 0;
  51. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  52. if (shim_heap_areas[i].start) {
  53. if (shim_heap_areas[i].end >= shim_heap_areas[i].current + size)
  54. return &shim_heap_areas[i];
  55. if (!smallest ||
  56. shim_heap_areas[i].end <=
  57. shim_heap_areas[i].current + smallest_size) {
  58. smallest = &shim_heap_areas[i];
  59. smallest_size = shim_heap_areas[i].end -
  60. shim_heap_areas[i].current;
  61. }
  62. } else {
  63. if (!first_empty)
  64. first_empty = &shim_heap_areas[i];
  65. }
  66. if (!heap) {
  67. size_t heap_size = MIN_SHIM_HEAP_PAGES * allocsize;
  68. void * start = NULL;
  69. heap = first_empty ? : smallest;
  70. assert(heap);
  71. while (size > heap_size)
  72. heap_size *= 2;
  73. if (!(start = DkVirtualMemoryAlloc(NULL, heap_size, 0,
  74. PAL_PROT_WRITE|PAL_PROT_READ)))
  75. return NULL;
  76. debug("allocate internal heap at %p - %p\n", start, start + heap_size);
  77. if (heap == smallest && heap->current != heap->end) {
  78. DkVirtualMemoryFree(heap->current, heap->end - heap->current);
  79. int flags = VMA_INTERNAL;
  80. bkeep_munmap(heap->current, heap->end - heap->current, &flags);
  81. }
  82. heap->start = heap->current = start;
  83. heap->end = start + heap_size;
  84. unlock(shim_heap_lock);
  85. bkeep_mmap(start, heap_size, PROT_READ|PROT_WRITE,
  86. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  87. lock(shim_heap_lock);
  88. }
  89. return heap;
  90. }
  91. void * __system_malloc (size_t size)
  92. {
  93. size_t alloc_size = ALIGN_UP(size);
  94. lock(shim_heap_lock);
  95. struct shim_heap * heap = __alloc_enough_heap(alloc_size);
  96. if (!heap) {
  97. unlock(shim_heap_lock);
  98. return NULL;
  99. }
  100. void * addr = heap->current;
  101. heap->current += alloc_size;
  102. unlock(shim_heap_lock);
  103. return addr;
  104. }
  105. void __system_free (void * addr, size_t size)
  106. {
  107. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  108. int flags = VMA_INTERNAL;
  109. bkeep_munmap(addr, ALIGN_UP(size), &flags);
  110. }
  111. int init_heap (void)
  112. {
  113. create_lock(shim_heap_lock);
  114. void * start = DkVirtualMemoryAlloc(NULL, INIT_SHIM_HEAP, 0,
  115. PAL_PROT_WRITE|PAL_PROT_READ);
  116. if (!start)
  117. return -ENOMEM;
  118. debug("allocate internal heap at %p - %p\n", start,
  119. start + INIT_SHIM_HEAP);
  120. shim_heap_areas[0].start = shim_heap_areas[0].current = start;
  121. shim_heap_areas[0].end = start + INIT_SHIM_HEAP;
  122. return 0;
  123. }
  124. int bkeep_shim_heap (void)
  125. {
  126. lock(shim_heap_lock);
  127. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  128. if (shim_heap_areas[i].start)
  129. bkeep_mmap(shim_heap_areas[i].start,
  130. shim_heap_areas[i].end - shim_heap_areas[i].start,
  131. PROT_READ|PROT_WRITE,
  132. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  133. unlock(shim_heap_lock);
  134. return 0;
  135. }
  136. int init_slab (void)
  137. {
  138. create_lock(slab_mgr_lock);
  139. slab_mgr = create_slab_mgr();
  140. return 0;
  141. }
  142. extern_alias(init_slab);
  143. int reinit_slab (void)
  144. {
  145. if (slab_mgr) {
  146. destroy_slab_mgr(slab_mgr);
  147. slab_mgr = NULL;
  148. }
  149. return 0;
  150. }
  151. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  152. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  153. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  154. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  155. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  156. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  157. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  158. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  159. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  160. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  161. void * __malloc_debug (size_t size, const char * file, int line)
  162. #else
  163. void * malloc (size_t size)
  164. #endif
  165. {
  166. #ifdef PROFILE
  167. int i;
  168. int level = -1;
  169. for (i = 0 ; i < SLAB_LEVEL ; i++)
  170. if (size < slab_levels[i]) {
  171. level = i;
  172. break;
  173. }
  174. switch(level) {
  175. case 0:
  176. INC_PROFILE_OCCURENCE(malloc_0);
  177. break;
  178. case 1:
  179. INC_PROFILE_OCCURENCE(malloc_1);
  180. break;
  181. case 2:
  182. INC_PROFILE_OCCURENCE(malloc_2);
  183. break;
  184. case 3:
  185. INC_PROFILE_OCCURENCE(malloc_3);
  186. break;
  187. case 4:
  188. INC_PROFILE_OCCURENCE(malloc_4);
  189. break;
  190. case 5:
  191. INC_PROFILE_OCCURENCE(malloc_5);
  192. break;
  193. case 6:
  194. INC_PROFILE_OCCURENCE(malloc_6);
  195. break;
  196. case 7:
  197. INC_PROFILE_OCCURENCE(malloc_7);
  198. break;
  199. case -1:
  200. INC_PROFILE_OCCURENCE(malloc_big);
  201. break;
  202. }
  203. #endif
  204. #ifdef SLAB_DEBUG_TRACE
  205. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  206. #else
  207. void * mem = slab_alloc(slab_mgr, size);
  208. #endif
  209. #ifdef SLAB_DEBUG_PRINT
  210. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  211. #endif
  212. return mem;
  213. }
  214. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  215. extern_alias(malloc);
  216. #endif
  217. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  218. void * __remalloc_debug (const void * mem, size_t size,
  219. const char * file, int line)
  220. #else
  221. void * remalloc (const void * mem, size_t size)
  222. #endif
  223. {
  224. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  225. void * buff = __malloc_debug(size, file, line);
  226. #else
  227. void * buff = malloc(size);
  228. #endif
  229. if (buff)
  230. memcpy(buff, mem, size);
  231. return buff;
  232. }
  233. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  234. extern_alias(remalloc);
  235. #endif
  236. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  237. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  238. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  239. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  240. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  241. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  242. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  243. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  244. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  245. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  246. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  247. void __free_debug (void * mem, const char * file, int line)
  248. #else
  249. void free (void * mem)
  250. #endif
  251. {
  252. if (MEMORY_MIGRATED(mem)) {
  253. INC_PROFILE_OCCURENCE(free_migrated);
  254. return;
  255. }
  256. #ifdef PROFILE
  257. int level = RAW_TO_LEVEL(mem);
  258. switch(level) {
  259. case 0:
  260. INC_PROFILE_OCCURENCE(free_0);
  261. break;
  262. case 1:
  263. INC_PROFILE_OCCURENCE(free_1);
  264. break;
  265. case 2:
  266. INC_PROFILE_OCCURENCE(free_2);
  267. break;
  268. case 3:
  269. INC_PROFILE_OCCURENCE(free_3);
  270. break;
  271. case 4:
  272. INC_PROFILE_OCCURENCE(free_4);
  273. break;
  274. case 5:
  275. INC_PROFILE_OCCURENCE(free_5);
  276. break;
  277. case 6:
  278. INC_PROFILE_OCCURENCE(free_6);
  279. break;
  280. case 7:
  281. INC_PROFILE_OCCURENCE(free_7);
  282. break;
  283. case -1:
  284. case 255:
  285. INC_PROFILE_OCCURENCE(free_big);
  286. break;
  287. }
  288. #endif
  289. #ifdef SLAB_DEBUG_PRINT
  290. debug("free(%p) (%s:%d)\n", mem, file, line);
  291. #endif
  292. #ifdef SLAB_DEBUG_TRACE
  293. slab_free_debug(slab_mgr, mem, file, line);
  294. #else
  295. slab_free(slab_mgr, mem);
  296. #endif
  297. }
  298. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  299. extern_alias(free);
  300. #endif