memmgr.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * memmgr.h
  15. *
  16. * This file contains implementation of fixed-size memory allocator.
  17. */
  18. #ifndef MEMMGR_H
  19. #define MEMMGR_H
  20. #include <sys/mman.h>
  21. #include "api.h"
  22. #include "assert.h"
  23. #include "list.h"
  24. #ifndef OBJ_TYPE
  25. #error "OBJ_TYPE not defined"
  26. #endif
  27. #ifndef system_malloc
  28. #error "macro \"void * system_malloc (size_t size)\" not declared"
  29. #endif
  30. #ifndef system_free
  31. #error "macro \"void * system_free (void * ptr, size_t size)\" not declared"
  32. #endif
  33. #ifndef SYSTEM_LOCK
  34. #define SYSTEM_LOCK() ({})
  35. #endif
  36. #ifndef SYSTEM_UNLOCK
  37. #define SYSTEM_UNLOCK() ({})
  38. #endif
  39. #ifndef SYSTEM_LOCKED
  40. #define SYSTEM_LOCKED() true
  41. #endif
  42. DEFINE_LIST(mem_obj);
  43. typedef struct mem_obj {
  44. union {
  45. LIST_TYPE(mem_obj) __list;
  46. OBJ_TYPE obj;
  47. };
  48. } MEM_OBJ_TYPE, *MEM_OBJ;
  49. DEFINE_LIST(mem_area);
  50. typedef struct mem_area {
  51. LIST_TYPE(mem_area) __list;
  52. unsigned int size;
  53. MEM_OBJ_TYPE objs[];
  54. } MEM_AREA_TYPE, *MEM_AREA;
  55. DEFINE_LISTP(mem_area);
  56. DEFINE_LISTP(mem_obj);
  57. typedef struct mem_mgr {
  58. LISTP_TYPE(mem_area) area_list;
  59. LISTP_TYPE(mem_obj) free_list;
  60. size_t size;
  61. MEM_OBJ_TYPE* obj;
  62. MEM_OBJ_TYPE* obj_top;
  63. MEM_AREA active_area;
  64. } MEM_MGR_TYPE, *MEM_MGR;
  65. #define __SUM_OBJ_SIZE(size) (sizeof(MEM_OBJ_TYPE) * (size))
  66. #define __MIN_MEM_SIZE() (sizeof(MEM_MGR_TYPE) + sizeof(MEM_AREA_TYPE))
  67. #define __MAX_MEM_SIZE(size) (__MIN_MEM_SIZE() + __SUM_OBJ_SIZE(size))
  68. #ifdef ALLOC_ALIGNMENT
  69. static inline int size_align_down(int size) {
  70. assert(IS_POWER_OF_2(ALLOC_ALIGNMENT));
  71. int s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  72. int p = s - ALIGN_DOWN_POW2(s, ALLOC_ALIGNMENT);
  73. int o = __SUM_OBJ_SIZE(1);
  74. return size - p / o - (p % o ? 1 : 0);
  75. }
  76. static inline int size_align_up(int size) {
  77. assert(IS_POWER_OF_2(ALLOC_ALIGNMENT));
  78. int s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  79. int p = ALIGN_UP_POW2(s, ALLOC_ALIGNMENT) - s;
  80. int o = __SUM_OBJ_SIZE(1);
  81. return size + p / o;
  82. }
  83. static inline int init_align_down(int size) {
  84. assert(IS_POWER_OF_2(ALLOC_ALIGNMENT));
  85. int s = __MAX_MEM_SIZE(size);
  86. int p = s - ALIGN_DOWN_POW2(s, ALLOC_ALIGNMENT);
  87. int o = __SUM_OBJ_SIZE(1);
  88. return size - p / o - (p % o ? 1 : 0);
  89. }
  90. static inline int init_align_up(int size) {
  91. assert(IS_POWER_OF_2(ALLOC_ALIGNMENT));
  92. int s = __MAX_MEM_SIZE(size);
  93. int p = ALIGN_UP_POW2(s, ALLOC_ALIGNMENT) - s;
  94. int o = __SUM_OBJ_SIZE(1);
  95. return size + p / o;
  96. }
  97. #endif
  98. static inline void __set_free_mem_area(MEM_AREA area, MEM_MGR mgr) {
  99. assert(SYSTEM_LOCKED());
  100. mgr->size += area->size;
  101. mgr->obj = area->objs;
  102. mgr->obj_top = area->objs + area->size;
  103. mgr->active_area = area;
  104. }
  105. static inline MEM_MGR create_mem_mgr(unsigned int size) {
  106. void* mem = system_malloc(__MAX_MEM_SIZE(size));
  107. MEM_AREA area;
  108. MEM_MGR mgr;
  109. if (!mem)
  110. return NULL;
  111. mgr = (MEM_MGR)mem;
  112. mgr->size = 0;
  113. area = (MEM_AREA)(mem + sizeof(MEM_MGR_TYPE));
  114. area->size = size;
  115. INIT_LIST_HEAD(area, __list);
  116. INIT_LISTP(&mgr->area_list);
  117. LISTP_ADD(area, &mgr->area_list, __list);
  118. INIT_LISTP(&mgr->free_list);
  119. __set_free_mem_area(area, mgr);
  120. return mgr;
  121. }
  122. static inline MEM_MGR enlarge_mem_mgr(MEM_MGR mgr, unsigned int size) {
  123. MEM_AREA area;
  124. area = (MEM_AREA)system_malloc(sizeof(MEM_AREA_TYPE) + __SUM_OBJ_SIZE(size));
  125. if (!area)
  126. return NULL;
  127. SYSTEM_LOCK();
  128. area->size = size;
  129. INIT_LIST_HEAD(area, __list);
  130. LISTP_ADD(area, &mgr->area_list, __list);
  131. __set_free_mem_area(area, mgr);
  132. SYSTEM_UNLOCK();
  133. return mgr;
  134. }
  135. static inline void destroy_mem_mgr(MEM_MGR mgr) {
  136. MEM_AREA tmp, n, first = NULL;
  137. first = tmp = LISTP_FIRST_ENTRY(&mgr->area_list, MEM_AREA_TYPE, __list);
  138. if (!first)
  139. goto free_mgr;
  140. LISTP_FOR_EACH_ENTRY_SAFE_CONTINUE(tmp, n, &mgr->area_list, __list) {
  141. LISTP_DEL(tmp, &mgr->area_list, __list);
  142. system_free(tmp, sizeof(MEM_AREA_TYPE) + __SUM_OBJ_SIZE(tmp->size));
  143. }
  144. free_mgr:
  145. system_free(mgr, __MAX_MEM_SIZE(first->size));
  146. }
  147. static inline OBJ_TYPE* get_mem_obj_from_mgr(MEM_MGR mgr) {
  148. MEM_OBJ mobj;
  149. SYSTEM_LOCK();
  150. if (mgr->obj == mgr->obj_top && LISTP_EMPTY(&mgr->free_list)) {
  151. SYSTEM_UNLOCK();
  152. return NULL;
  153. }
  154. if (!LISTP_EMPTY(&mgr->free_list)) {
  155. mobj = LISTP_FIRST_ENTRY(&mgr->free_list, MEM_OBJ_TYPE, __list);
  156. LISTP_DEL_INIT(mobj, &mgr->free_list, __list);
  157. CHECK_LIST_HEAD(MEM_OBJ, &mgr->free_list, __list);
  158. } else {
  159. mobj = mgr->obj++;
  160. }
  161. SYSTEM_UNLOCK();
  162. return &mobj->obj;
  163. }
  164. static inline OBJ_TYPE* get_mem_obj_from_mgr_enlarge(MEM_MGR mgr, unsigned int size) {
  165. MEM_OBJ mobj;
  166. SYSTEM_LOCK();
  167. if (mgr->obj == mgr->obj_top && LISTP_EMPTY(&mgr->free_list)) {
  168. size_t mgr_size = mgr->size;
  169. MEM_AREA area;
  170. /* If there is a previously allocated area, just activate it. */
  171. area = LISTP_PREV_ENTRY(mgr->active_area, &mgr->area_list, __list);
  172. if (area) {
  173. __set_free_mem_area(area, mgr);
  174. goto alloc;
  175. }
  176. SYSTEM_UNLOCK();
  177. if (!size)
  178. return NULL;
  179. /* There can be concurrent attempt to try to enlarge the
  180. allocator, but we prevent deadlocks or crashes. */
  181. area = (MEM_AREA)system_malloc(sizeof(MEM_AREA_TYPE) + __SUM_OBJ_SIZE(size));
  182. if (!area)
  183. return NULL;
  184. SYSTEM_LOCK();
  185. area->size = size;
  186. INIT_LIST_HEAD(area, __list);
  187. /* There can be concurrent operations to extend the manager. In case
  188. * someone has already enlarged the space, we just add the new area to
  189. * the list for later use. */
  190. LISTP_ADD(area, &mgr->area_list, __list);
  191. if (mgr_size == mgr->size) /* check if the size has changed */
  192. __set_free_mem_area(area, mgr);
  193. }
  194. alloc:
  195. if (!LISTP_EMPTY(&mgr->free_list)) {
  196. mobj = LISTP_FIRST_ENTRY(&mgr->free_list, MEM_OBJ_TYPE, __list);
  197. LISTP_DEL_INIT(mobj, &mgr->free_list, __list);
  198. CHECK_LIST_HEAD(MEM_OBJ, &mgr->free_list, __list);
  199. } else {
  200. mobj = mgr->obj++;
  201. }
  202. assert(mgr->obj <= mgr->obj_top);
  203. SYSTEM_UNLOCK();
  204. return &mobj->obj;
  205. }
  206. static inline void free_mem_obj_to_mgr(MEM_MGR mgr, OBJ_TYPE* obj) {
  207. MEM_OBJ mobj = container_of(obj, MEM_OBJ_TYPE, obj);
  208. SYSTEM_LOCK();
  209. MEM_AREA area, found = NULL;
  210. LISTP_FOR_EACH_ENTRY(area, &mgr->area_list, __list) {
  211. if (mobj >= area->objs && mobj < area->objs + area->size) {
  212. found = area;
  213. break;
  214. }
  215. }
  216. if (found) {
  217. INIT_LIST_HEAD(mobj, __list);
  218. LISTP_ADD_TAIL(mobj, &mgr->free_list, __list);
  219. CHECK_LIST_HEAD(MEM_OBJ, &mgr->free_list, __list);
  220. }
  221. SYSTEM_UNLOCK();
  222. }
  223. #endif /* MEMMGR_H */