memmgr.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * memmgr.h
  17. *
  18. * This file contains implementation of fix-sized memory allocator.
  19. */
  20. #ifndef MEMMGR_H
  21. #define MEMMGR_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. #ifndef OBJ_TYPE
  37. #error "OBJ_TYPE not defined"
  38. #endif
  39. typedef struct mem_obj {
  40. union {
  41. struct list_head __list;
  42. OBJ_TYPE obj;
  43. };
  44. } MEM_OBJ_TYPE, * MEM_OBJ;
  45. typedef struct mem_area {
  46. struct list_head __list;
  47. uint64_t size;
  48. MEM_OBJ_TYPE objs[];
  49. } MEM_AREA_TYPE, * MEM_AREA;
  50. typedef struct mem_mgr {
  51. struct list_head area_list;
  52. struct list_head free_list;
  53. unsigned int size;
  54. } MEM_MGR_TYPE, * MEM_MGR;
  55. #define __SUM_OBJ_SIZE(size) (sizeof(MEM_OBJ_TYPE) * (size))
  56. #define __MIN_MEM_SIZE() (sizeof(MEM_MGR_TYPE) + sizeof(MEM_AREA_TYPE))
  57. #define __MAX_MEM_SIZE(size) (__MIN_MEM_SIZE() + __SUM_OBJ_SIZE(size))
  58. #ifdef PAGE_SIZE
  59. static inline int size_align_down(int size)
  60. {
  61. size_t s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  62. size_t p = s - (s & ~(PAGE_SIZE - 1));
  63. size_t o = __SUM_OBJ_SIZE(1);
  64. return size - p / o - (p % o ? 1 : 0);
  65. }
  66. static inline int size_align_up(int size)
  67. {
  68. size_t s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  69. size_t p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  70. size_t o = __SUM_OBJ_SIZE(1);
  71. return size + p / o;
  72. }
  73. static inline int init_align_down(int size)
  74. {
  75. size_t s = __MAX_MEM_SIZE(size);
  76. size_t p = s - (s & ~(PAGE_SIZE - 1));
  77. size_t o = __SUM_OBJ_SIZE(1);
  78. return size - p / o - (p % o ? 1 : 0);
  79. }
  80. static inline int init_align_up(int size)
  81. {
  82. size_t s = __MAX_MEM_SIZE(size);
  83. size_t p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  84. size_t o = __SUM_OBJ_SIZE(1);
  85. return size + p / o;
  86. }
  87. #endif
  88. static inline void __set_free_mem_area (MEM_AREA area, MEM_MGR mgr, int size)
  89. {
  90. int i;
  91. for (i = 0 ; i < size ; i++) {
  92. INIT_LIST_HEAD(&area->objs[i].__list);
  93. list_add_tail(&area->objs[i].__list, &mgr->free_list);
  94. }
  95. mgr->size += size;
  96. }
  97. static inline MEM_MGR create_mem_mgr (unsigned int size)
  98. {
  99. unsigned long mem = (unsigned long) system_malloc(__MAX_MEM_SIZE(size));
  100. MEM_AREA area;
  101. MEM_MGR mgr;
  102. if (mem <= 0)
  103. return NULL;
  104. mgr = (MEM_MGR) mem;
  105. area = (MEM_AREA) (mem + sizeof(MEM_MGR_TYPE));
  106. #if DEBUG_MEMMGR == 1
  107. area->size = size;
  108. INIT_LIST_HEAD(&area->__list);
  109. INIT_LIST_HEAD(&mgr->area_list);
  110. list_add(&area->__list, &mgr->area_list);
  111. #endif
  112. INIT_LIST_HEAD(&mgr->free_list);
  113. mgr->size = 0;
  114. __set_free_mem_area(area, mgr, size);
  115. return mgr;
  116. }
  117. static inline MEM_MGR enlarge_mem_mgr (MEM_MGR mgr, unsigned int size)
  118. {
  119. MEM_AREA area;
  120. area = (MEM_AREA) system_malloc(sizeof(MEM_AREA_TYPE) +
  121. __SUM_OBJ_SIZE(size));
  122. if (area <= 0)
  123. return NULL;
  124. system_lock();
  125. #if DEBUG_MEMMGR == 1
  126. area->size = size;
  127. INIT_LIST_HEAD(&area->__list);
  128. list_add(&area->__list, &mgr->area_list);
  129. #endif
  130. __set_free_mem_area(area, mgr, size);
  131. system_unlock();
  132. return mgr;
  133. }
  134. static inline void destroy_mem_mgr (MEM_MGR mgr)
  135. {
  136. MEM_AREA tmp, n, first = NULL;
  137. first = tmp = list_first_entry(&mgr->area_list, MEM_AREA_TYPE, __list);
  138. if (!first)
  139. return;
  140. list_for_each_entry_safe_continue(tmp, n, &mgr->area_list, __list) {
  141. list_del(&tmp->__list);
  142. system_free(tmp, sizeof(MEM_AREA_TYPE) + __SUM_OBJ_SIZE(tmp->size));
  143. }
  144. system_free(mgr, __MAX_MEM_SIZE(first->size));
  145. }
  146. static inline OBJ_TYPE * get_mem_obj_from_mgr (MEM_MGR mgr)
  147. {
  148. MEM_OBJ mobj;
  149. system_lock();
  150. if (list_empty(&mgr->free_list)) {
  151. system_unlock();
  152. return NULL;
  153. }
  154. mobj = list_first_entry(&mgr->free_list, MEM_OBJ_TYPE, __list);
  155. list_del(&mobj->__list);
  156. system_unlock();
  157. return &mobj->obj;
  158. }
  159. static inline OBJ_TYPE * get_mem_obj_from_mgr_enlarge (MEM_MGR mgr,
  160. unsigned int size)
  161. {
  162. MEM_OBJ mobj;
  163. system_lock();
  164. if (list_empty(&mgr->free_list)) {
  165. system_unlock();
  166. if (!size)
  167. return NULL;
  168. MEM_AREA area;
  169. area = (MEM_AREA) system_malloc(sizeof(MEM_AREA_TYPE) +
  170. __SUM_OBJ_SIZE(size));
  171. if (!area)
  172. return NULL;
  173. system_lock();
  174. #if DEBUG_MEMMGR == 1
  175. area->size = size;
  176. INIT_LIST_HEAD(&area->__list);
  177. list_add(&area->__list, &mgr->area_list);
  178. #endif
  179. __set_free_mem_area(area, mgr, size);
  180. }
  181. mobj = list_first_entry(&mgr->free_list, MEM_OBJ_TYPE, __list);
  182. list_del_init(&mobj->__list);
  183. check_list_head(&mgr->free_list);
  184. system_unlock();
  185. return &mobj->obj;
  186. }
  187. static inline void free_mem_obj_to_mgr (MEM_MGR mgr, OBJ_TYPE * obj)
  188. {
  189. MEM_OBJ mobj = container_of(obj, MEM_OBJ_TYPE, obj);
  190. system_lock();
  191. #if DEBUG_MEMMGR == 1
  192. MEM_AREA area, found = NULL;
  193. list_for_each_entry(area, &mgr->area_list, __list)
  194. if (mobj >= area->objs && mobj < area->objs + area->size) {
  195. found = area;
  196. break;
  197. }
  198. #else
  199. int found = 1;
  200. #endif /* DEBUG_MEMMGR != 1 */
  201. if (found) {
  202. INIT_LIST_HEAD(&mobj->__list);
  203. list_add_tail(&mobj->__list, &mgr->free_list);
  204. check_list_head(&mgr->free_list);
  205. }
  206. system_unlock();
  207. }
  208. #endif /* MEMMGR_H */