slabmgr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * slabmgr.h
  17. *
  18. * This file contains implementation of SLAB (variable-size) memory allocator.
  19. */
  20. #ifndef SLABMGR_H
  21. #define SLABMGR_H
  22. #include "linux_list.h"
  23. #include <sys/mman.h>
  24. #ifndef system_malloc
  25. #error "macro \"void * system_malloc(int size)\" not declared"
  26. #endif
  27. #ifndef system_free
  28. #error "macro \"void * system_free(void * ptr, int size)\" not declared"
  29. #endif
  30. #ifndef system_lock
  31. #define system_lock() ({})
  32. #endif
  33. #ifndef system_unlock
  34. #define system_unlock() ({})
  35. #endif
  36. #define SLAB_PADDING 7
  37. typedef struct __attribute__((packed)) slab_obj {
  38. unsigned char level;
  39. unsigned char padding[SLAB_PADDING];
  40. union {
  41. struct list_head __list;
  42. unsigned char *raw;
  43. };
  44. } SLAB_OBJ_TYPE, * SLAB_OBJ;
  45. typedef struct __attribute__((packed)) slab_area {
  46. struct list_head __list;
  47. unsigned int size;
  48. unsigned char raw[];
  49. } SLAB_AREA_TYPE, * SLAB_AREA;
  50. #ifdef SLAB_DEBUG
  51. struct slab_debug {
  52. struct {
  53. const char * file;
  54. int line;
  55. } alloc, free;
  56. };
  57. # define SLAB_DEBUG_SIZE sizeof(struct slab_debug)
  58. #else
  59. # define SLAB_DEBUG_SIZE 0
  60. #endif
  61. #ifdef SLAB_CANARY
  62. # define SLAB_CANARY_STRING 0xDEADBEEF
  63. # define SLAB_CANARY_SIZE sizeof(unsigned long)
  64. #else
  65. # define SLAB_CANARY_SIZE 0
  66. #endif
  67. #define SLAB_HDR_SIZE (sizeof(SLAB_OBJ_TYPE) - sizeof(struct list_head) + \
  68. SLAB_DEBUG_SIZE + SLAB_CANARY_SIZE)
  69. #ifndef SLAB_LEVEL
  70. #define SLAB_LEVEL 8
  71. #endif
  72. #ifndef SLAB_LEVEL_SIZES
  73. # define SLAB_LEVEL_SIZES 16, 32, 64, \
  74. 128 - SLAB_HDR_SIZE, \
  75. 256 - SLAB_HDR_SIZE, \
  76. 512 - SLAB_HDR_SIZE, \
  77. 1024 - SLAB_HDR_SIZE, \
  78. 2048 - SLAB_HDR_SIZE
  79. # define SLAB_LEVELS_SUM (4080 - SLAB_HDR_SIZE * 5)
  80. #else
  81. # ifndef SLAB_LEVELS_SUM
  82. # error "SALB_LEVELS_SUM not defined"
  83. # endif
  84. #endif
  85. static int slab_levels[SLAB_LEVEL] = { SLAB_LEVEL_SIZES };
  86. typedef struct slab_mgr {
  87. struct list_head area_list[SLAB_LEVEL];
  88. struct list_head free_list[SLAB_LEVEL];
  89. unsigned int size[SLAB_LEVEL];
  90. } SLAB_MGR_TYPE, * SLAB_MGR;
  91. typedef struct __attribute__((packed)) large_mem_obj {
  92. unsigned long size;
  93. unsigned char level;
  94. unsigned char padding[SLAB_PADDING];
  95. unsigned char raw[];
  96. } LARGE_MEM_OBJ_TYPE, * LARGE_MEM_OBJ;
  97. #define OBJ_LEVEL(obj) ((obj)->level)
  98. #define OBJ_RAW(obj) (&(obj)->raw)
  99. #define RAW_TO_LEVEL(raw_ptr) \
  100. (*((unsigned char *) (raw_ptr) - SLAB_PADDING - 1))
  101. #define RAW_TO_OBJ(raw_ptr, type) container_of((raw_ptr), type, raw)
  102. #define __SUM_OBJ_SIZE(slab_size, size) \
  103. (((slab_size) + SLAB_HDR_SIZE) * (size))
  104. #define __MIN_MEM_SIZE() (sizeof(SLAB_AREA_TYPE))
  105. #define __MAX_MEM_SIZE(slab_size, size) \
  106. (__MIN_MEM_SIZE() + __SUM_OBJ_SIZE((slab_size), (size)))
  107. #define __INIT_SUM_OBJ_SIZE(size) \
  108. ((SLAB_LEVELS_SUM + SLAB_HDR_SIZE * SLAB_LEVEL) * (size))
  109. #define __INIT_MIN_MEM_SIZE() \
  110. (sizeof(SLAB_MGR_TYPE) + sizeof(SLAB_AREA_TYPE) * SLAB_LEVEL)
  111. #define __INIT_MAX_MEM_SIZE(size) \
  112. (__INIT_MIN_MEM_SIZE() + __INIT_SUM_OBJ_SIZE((size)))
  113. #ifdef PAGE_SIZE
  114. static inline int size_align_down(int slab_size, int size)
  115. {
  116. size_t s = __MAX_MEM_SIZE(slab_size, size);
  117. size_t p = s - (s & ~(PAGE_SIZE - 1));
  118. size_t o = __SUM_OBJ_SIZE(slab_size, 1);
  119. return size - p / o - (p % o ? 1 : 0);
  120. }
  121. static inline int size_align_up(int slab_size, int size)
  122. {
  123. size_t s = __MAX_MEM_SIZE(slab_size, size);
  124. size_t p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  125. size_t o = __SUM_OBJ_SIZE(slab_size, 1);
  126. return size + p / o;
  127. }
  128. static inline int init_align_down(int size)
  129. {
  130. size_t s = __INIT_MAX_MEM_SIZE(size);
  131. size_t p = s - (s & ~(PAGE_SIZE - 1));
  132. size_t o = __INIT_SUM_OBJ_SIZE(1);
  133. return size - p /o - (p % o ? 1 : 0);
  134. }
  135. static inline int init_size_align_up(int size)
  136. {
  137. size_t s = __INIT_MAX_MEM_SIZE(size);
  138. size_t p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  139. size_t o = __INIT_SUM_OBJ_SIZE(1);
  140. return size + p / o;
  141. }
  142. #endif /* PAGE_SIZE */
  143. #ifndef STARTUP_SIZE
  144. # define STARTUP_SIZE 16
  145. #endif
  146. static inline void __set_free_slab_area (SLAB_AREA area, SLAB_MGR mgr,
  147. unsigned int level)
  148. {
  149. int i, size = area->size;
  150. int slab_size = slab_levels[level] + SLAB_HDR_SIZE;
  151. void * addr = (void *) area->raw;
  152. for (i = 0 ; i < size ; i++, addr += slab_size) {
  153. SLAB_OBJ obj = (SLAB_OBJ) addr;
  154. OBJ_LEVEL(obj) = level;
  155. #ifdef SLAB_DEBUG
  156. struct slab_debug * debug =
  157. (struct slab_debug *) (addr + slab_size) - 1;
  158. debug->alloc.file = NULL;
  159. debug->alloc.line = 0;
  160. debug->free.file = NULL;
  161. debug->free.line = 0;
  162. #endif
  163. INIT_LIST_HEAD(&obj->__list);
  164. list_add_tail(&obj->__list, &mgr->free_list[level]);
  165. }
  166. mgr->size[level] += size;
  167. }
  168. static inline SLAB_MGR create_slab_mgr (void)
  169. {
  170. #ifdef PAGE_SIZE
  171. int size = init_size_align_up(STARTUP_SIZE);
  172. #else
  173. int size = STARTUP_SIZE;
  174. #endif
  175. unsigned long mem;
  176. SLAB_AREA area;
  177. SLAB_MGR mgr;
  178. mem = (unsigned long) system_malloc(__INIT_MAX_MEM_SIZE(size));
  179. if (mem <= 0)
  180. return NULL;
  181. mgr = (SLAB_MGR) mem;
  182. void * addr = (void *) mgr + sizeof(SLAB_MGR_TYPE);
  183. int i;
  184. for (i = 0 ; i < SLAB_LEVEL ; i++) {
  185. area = (SLAB_AREA) addr;
  186. area->size = STARTUP_SIZE;
  187. INIT_LIST_HEAD(&area->__list);
  188. INIT_LIST_HEAD(&mgr->area_list[i]);
  189. list_add_tail(&area->__list, &mgr->area_list[i]);
  190. INIT_LIST_HEAD(&mgr->free_list[i]);
  191. mgr->size[i] = 0;
  192. __set_free_slab_area(area, mgr, i);
  193. addr += __MAX_MEM_SIZE(slab_levels[i], STARTUP_SIZE);
  194. }
  195. return mgr;
  196. }
  197. static inline void destroy_slab_mgr (SLAB_MGR mgr)
  198. {
  199. void * addr = (void *) mgr + sizeof(SLAB_MGR_TYPE);
  200. SLAB_AREA area, tmp, n;
  201. int i;
  202. for (i = 0 ; i < SLAB_LEVEL; i++) {
  203. area = (SLAB_AREA) addr;
  204. list_for_each_entry_safe(tmp, n, &mgr->area_list[i], __list) {
  205. if (tmp != area)
  206. system_free(area,
  207. __MAX_MEM_SIZE(slab_levels[i], area->size));
  208. }
  209. addr += __MAX_MEM_SIZE(slab_levels[i], STARTUP_SIZE);
  210. }
  211. system_free(mgr, addr - (void *) mgr);
  212. }
  213. static inline SLAB_MGR enlarge_slab_mgr (SLAB_MGR mgr, unsigned int level)
  214. {
  215. SLAB_AREA area;
  216. int size;
  217. if (level >= SLAB_LEVEL) {
  218. system_lock();
  219. goto out;
  220. }
  221. size = mgr->size[level];
  222. area = (SLAB_AREA) system_malloc(__MAX_MEM_SIZE(slab_levels[level], size));
  223. if (area <= 0)
  224. return NULL;
  225. system_lock();
  226. area->size = size;
  227. INIT_LIST_HEAD(&area->__list);
  228. list_add(&area->__list, &mgr->area_list[level]);
  229. __set_free_slab_area(area, mgr, level);
  230. system_unlock();
  231. out:
  232. return mgr;
  233. }
  234. static inline void * slab_alloc (SLAB_MGR mgr, int size)
  235. {
  236. SLAB_OBJ mobj;
  237. int i;
  238. int level = -1;
  239. for (i = 0 ; i < SLAB_LEVEL ; i++)
  240. if (size < slab_levels[i]) {
  241. level = i;
  242. break;
  243. }
  244. if (level == -1) {
  245. LARGE_MEM_OBJ mem = (LARGE_MEM_OBJ)
  246. system_malloc(sizeof(LARGE_MEM_OBJ_TYPE) + size);
  247. if (!mem)
  248. return NULL;
  249. mem->size = size;
  250. OBJ_LEVEL(mem) = (unsigned char) -1;
  251. return OBJ_RAW(mem);
  252. }
  253. system_lock();
  254. if (list_empty(&mgr->free_list[level])) {
  255. system_unlock();
  256. enlarge_slab_mgr(mgr, level);
  257. system_lock();
  258. }
  259. mobj = list_first_entry(&mgr->free_list[level], SLAB_OBJ_TYPE, __list);
  260. list_del(&mobj->__list);
  261. system_unlock();
  262. #ifdef SLAB_CANARY
  263. unsigned long * m =
  264. (unsigned long *) ((void *) OBJ_RAW(mobj) + slab_levels[level]);
  265. *m = SLAB_CANARY_STRING;
  266. #endif
  267. return OBJ_RAW(mobj);
  268. }
  269. #ifdef SLAB_DEBUG
  270. static inline void * slab_alloc_debug (SLAB_MGR mgr, int size,
  271. const char * file, int line)
  272. {
  273. void * mem = slab_alloc(mgr, size);
  274. int i;
  275. int level = -1;
  276. for (i = 0 ; i < SLAB_LEVEL ; i++)
  277. if (size < slab_levels[i]) {
  278. level = i;
  279. break;
  280. }
  281. if (level != -1) {
  282. struct slab_debug * debug =
  283. (struct slab_debug *) (mem + slab_levels[level] +
  284. SLAB_CANARY_SIZE);
  285. debug->alloc.file = file;
  286. debug->alloc.line = line;
  287. }
  288. return mem;
  289. }
  290. #endif
  291. static inline void slab_free (SLAB_MGR mgr, void * obj)
  292. {
  293. unsigned char level = RAW_TO_LEVEL(obj);
  294. if (level == (unsigned char) -1) {
  295. LARGE_MEM_OBJ mem = RAW_TO_OBJ(obj, LARGE_MEM_OBJ_TYPE);
  296. system_free(mem, mem->size + sizeof(LARGE_MEM_OBJ_TYPE));
  297. return;
  298. }
  299. if (level >= SLAB_LEVEL)
  300. return;
  301. #ifdef SLAB_CANARY
  302. unsigned long * m = (unsigned long *) (obj + slab_levels[level]);
  303. assert((*m) == SLAB_CANARY_STRING);
  304. #endif
  305. SLAB_OBJ mobj = RAW_TO_OBJ(obj, SLAB_OBJ_TYPE);
  306. system_lock();
  307. INIT_LIST_HEAD(&mobj->__list);
  308. list_add_tail(&mobj->__list, &mgr->free_list[level]);
  309. system_unlock();
  310. }
  311. #ifdef SLAB_DEBUG
  312. static inline void slab_free_debug (SLAB_MGR mgr, void * obj,
  313. const char * file, int line)
  314. {
  315. unsigned char level = RAW_TO_LEVEL(obj);
  316. if (level < SLAB_LEVEL) {
  317. struct slab_debug * debug =
  318. (struct slab_debug *) (obj + slab_levels[level] +
  319. SLAB_CANARY_SIZE);
  320. debug->free.file = file;
  321. debug->free.line = line;
  322. }
  323. slab_free(mgr, obj);
  324. }
  325. #endif
  326. #endif /* SLABMGR_H */