memmgr.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 OBJ_TYPE
  25. #error "OBJ_TYPE not defined"
  26. #endif
  27. #ifndef system_malloc
  28. #error "macro \"void * system_malloc(int size)\" not declared"
  29. #endif
  30. #ifndef system_free
  31. #error "macro \"void * system_free(void * ptr, int 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. 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. unsigned int 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. MEM_OBJ_TYPE * obj, * obj_top;
  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. int s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  62. int p = s - (s & ~(PAGE_SIZE - 1));
  63. int 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. int s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  69. int p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  70. int o = __SUM_OBJ_SIZE(1);
  71. return size + p / o;
  72. }
  73. static inline int init_align_down (int size)
  74. {
  75. int s = __MAX_MEM_SIZE(size);
  76. int p = s - (s & ~(PAGE_SIZE - 1));
  77. int 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. int s = __MAX_MEM_SIZE(size);
  83. int p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  84. int 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. mgr->obj = area->objs;
  91. mgr->obj_top = area->objs + area->size;
  92. }
  93. static inline MEM_MGR create_mem_mgr (unsigned int size)
  94. {
  95. unsigned long mem = (unsigned long) system_malloc(__MAX_MEM_SIZE(size));
  96. MEM_AREA area;
  97. MEM_MGR mgr;
  98. if (mem <= 0)
  99. return NULL;
  100. mgr = (MEM_MGR) mem;
  101. area = (MEM_AREA) (mem + sizeof(MEM_MGR_TYPE));
  102. area->size = size;
  103. INIT_LIST_HEAD(&area->__list);
  104. INIT_LIST_HEAD(&mgr->area_list);
  105. list_add(&area->__list, &mgr->area_list);
  106. INIT_LIST_HEAD(&mgr->free_list);
  107. __set_free_mem_area(area, mgr, size);
  108. return mgr;
  109. }
  110. static inline MEM_MGR enlarge_mem_mgr (MEM_MGR mgr, unsigned int size)
  111. {
  112. MEM_AREA area;
  113. area = (MEM_AREA) system_malloc(sizeof(MEM_AREA_TYPE) +
  114. __SUM_OBJ_SIZE(size));
  115. if (area <= 0)
  116. return NULL;
  117. system_lock();
  118. area->size = size;
  119. INIT_LIST_HEAD(&area->__list);
  120. list_add(&area->__list, &mgr->area_list);
  121. __set_free_mem_area(area, mgr, size);
  122. system_unlock();
  123. return mgr;
  124. }
  125. static inline void destroy_mem_mgr (MEM_MGR mgr)
  126. {
  127. MEM_AREA tmp, n, first = NULL;
  128. first = tmp = list_first_entry(&mgr->area_list, MEM_AREA_TYPE, __list);
  129. if (!first)
  130. return;
  131. list_for_each_entry_safe_continue(tmp, n, &mgr->area_list, __list) {
  132. list_del(&tmp->__list);
  133. system_free(tmp, sizeof(MEM_AREA_TYPE) + __SUM_OBJ_SIZE(tmp->size));
  134. }
  135. system_free(mgr, __MAX_MEM_SIZE(first->size));
  136. }
  137. static inline OBJ_TYPE * get_mem_obj_from_mgr (MEM_MGR mgr)
  138. {
  139. MEM_OBJ mobj;
  140. system_lock();
  141. if (mgr->obj == mgr->obj_top && list_empty(&mgr->free_list)) {
  142. system_unlock();
  143. return NULL;
  144. }
  145. if (!list_empty(&mgr->free_list)) {
  146. mobj = list_first_entry(&mgr->free_list, MEM_OBJ_TYPE, __list);
  147. list_del_init(&mobj->__list);
  148. check_list_head(&mgr->free_list);
  149. } else {
  150. mobj = mgr->obj++;
  151. }
  152. system_unlock();
  153. return &mobj->obj;
  154. }
  155. static inline OBJ_TYPE * get_mem_obj_from_mgr_enlarge (MEM_MGR mgr,
  156. unsigned int size)
  157. {
  158. MEM_OBJ mobj;
  159. system_lock();
  160. if (mgr->obj == mgr->obj_top && list_empty(&mgr->free_list)) {
  161. system_unlock();
  162. if (!size)
  163. return NULL;
  164. MEM_AREA area;
  165. area = (MEM_AREA) system_malloc(sizeof(MEM_AREA_TYPE) +
  166. __SUM_OBJ_SIZE(size));
  167. if (!area)
  168. return NULL;
  169. system_lock();
  170. area->size = size;
  171. INIT_LIST_HEAD(&area->__list);
  172. list_add(&area->__list, &mgr->area_list);
  173. __set_free_mem_area(area, mgr, size);
  174. }
  175. if (!list_empty(&mgr->free_list)) {
  176. mobj = list_first_entry(&mgr->free_list, MEM_OBJ_TYPE, __list);
  177. list_del_init(&mobj->__list);
  178. check_list_head(&mgr->free_list);
  179. } else {
  180. mobj = mgr->obj++;
  181. }
  182. system_unlock();
  183. return &mobj->obj;
  184. }
  185. static inline void free_mem_obj_to_mgr (MEM_MGR mgr, OBJ_TYPE * obj)
  186. {
  187. MEM_OBJ mobj = container_of(obj, MEM_OBJ_TYPE, obj);
  188. system_lock();
  189. MEM_AREA area, found = NULL;
  190. list_for_each_entry(area, &mgr->area_list, __list)
  191. if (mobj >= area->objs && mobj < area->objs + area->size) {
  192. found = area;
  193. break;
  194. }
  195. if (found) {
  196. INIT_LIST_HEAD(&mobj->__list);
  197. list_add_tail(&mobj->__list, &mgr->free_list);
  198. check_list_head(&mgr->free_list);
  199. }
  200. system_unlock();
  201. }
  202. #endif /* MEMMGR_H */