memmgr.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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 "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. DEFINE_LIST(mem_obj);
  40. typedef struct mem_obj {
  41. union {
  42. LIST_TYPE(mem_obj) __list;
  43. OBJ_TYPE obj;
  44. };
  45. } MEM_OBJ_TYPE, * MEM_OBJ;
  46. DEFINE_LIST(mem_area);
  47. typedef struct mem_area {
  48. LIST_TYPE(mem_area) __list;
  49. unsigned int size;
  50. MEM_OBJ_TYPE objs[];
  51. } MEM_AREA_TYPE, * MEM_AREA;
  52. DEFINE_LISTP(mem_area);
  53. DEFINE_LISTP(mem_obj);
  54. typedef struct mem_mgr {
  55. LISTP_TYPE(mem_area) area_list;
  56. LISTP_TYPE(mem_obj) free_list;
  57. MEM_OBJ_TYPE * obj, * obj_top;
  58. } MEM_MGR_TYPE, * MEM_MGR;
  59. #define __SUM_OBJ_SIZE(size) (sizeof(MEM_OBJ_TYPE) * (size))
  60. #define __MIN_MEM_SIZE() (sizeof(MEM_MGR_TYPE) + sizeof(MEM_AREA_TYPE))
  61. #define __MAX_MEM_SIZE(size) (__MIN_MEM_SIZE() + __SUM_OBJ_SIZE(size))
  62. #ifdef PAGE_SIZE
  63. static inline int size_align_down (int size)
  64. {
  65. int s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  66. int p = s - (s & ~(PAGE_SIZE - 1));
  67. int o = __SUM_OBJ_SIZE(1);
  68. return size - p / o - (p % o ? 1 : 0);
  69. }
  70. static inline int size_align_up (int size)
  71. {
  72. int s = __MAX_MEM_SIZE(size) - sizeof(MEM_MGR_TYPE);
  73. int p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  74. int o = __SUM_OBJ_SIZE(1);
  75. return size + p / o;
  76. }
  77. static inline int init_align_down (int size)
  78. {
  79. int s = __MAX_MEM_SIZE(size);
  80. int p = s - (s & ~(PAGE_SIZE - 1));
  81. int o = __SUM_OBJ_SIZE(1);
  82. return size - p / o - (p % o ? 1 : 0);
  83. }
  84. static inline int init_align_up (int size)
  85. {
  86. int s = __MAX_MEM_SIZE(size);
  87. int p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  88. int o = __SUM_OBJ_SIZE(1);
  89. return size + p / o;
  90. }
  91. #endif
  92. static inline void __set_free_mem_area (MEM_AREA area, MEM_MGR mgr, int size)
  93. {
  94. mgr->obj = area->objs;
  95. mgr->obj_top = area->objs + area->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. area->size = size;
  107. INIT_LIST_HEAD(area, __list);
  108. INIT_LISTP(&mgr->area_list);
  109. listp_add(area, &mgr->area_list, __list);
  110. INIT_LISTP(&mgr->free_list);
  111. __set_free_mem_area(area, mgr, size);
  112. return mgr;
  113. }
  114. static inline MEM_MGR enlarge_mem_mgr (MEM_MGR mgr, unsigned int size)
  115. {
  116. MEM_AREA area;
  117. area = (MEM_AREA) system_malloc(sizeof(MEM_AREA_TYPE) +
  118. __SUM_OBJ_SIZE(size));
  119. if (area <= 0)
  120. return NULL;
  121. system_lock();
  122. area->size = size;
  123. INIT_LIST_HEAD(area, __list);
  124. listp_add(area, &mgr->area_list, __list);
  125. __set_free_mem_area(area, mgr, size);
  126. system_unlock();
  127. return mgr;
  128. }
  129. static inline void destroy_mem_mgr (MEM_MGR mgr)
  130. {
  131. MEM_AREA tmp, n, first = NULL;
  132. first = tmp = listp_first_entry(&mgr->area_list, MEM_AREA_TYPE, __list);
  133. if (!first)
  134. return;
  135. listp_for_each_entry_safe_continue(tmp, n, &mgr->area_list, __list) {
  136. listp_del(tmp, &mgr->area_list, __list);
  137. system_free(tmp, sizeof(MEM_AREA_TYPE) + __SUM_OBJ_SIZE(tmp->size));
  138. }
  139. system_free(mgr, __MAX_MEM_SIZE(first->size));
  140. }
  141. static inline OBJ_TYPE * get_mem_obj_from_mgr (MEM_MGR mgr)
  142. {
  143. MEM_OBJ mobj;
  144. system_lock();
  145. if (mgr->obj == mgr->obj_top && listp_empty(&mgr->free_list)) {
  146. system_unlock();
  147. return NULL;
  148. }
  149. if (!listp_empty(&mgr->free_list)) {
  150. mobj = listp_first_entry(&mgr->free_list, MEM_OBJ_TYPE, __list);
  151. listp_del_init(mobj, &mgr->free_list, __list);
  152. check_list_head(MEM_OBJ, &mgr->free_list, __list);
  153. } else {
  154. mobj = mgr->obj++;
  155. }
  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 (mgr->obj == mgr->obj_top && listp_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. area->size = size;
  175. INIT_LIST_HEAD(area, __list);
  176. listp_add(area, &mgr->area_list, __list);
  177. __set_free_mem_area(area, mgr, size);
  178. }
  179. if (!listp_empty(&mgr->free_list)) {
  180. mobj = listp_first_entry(&mgr->free_list, MEM_OBJ_TYPE, __list);
  181. listp_del_init(mobj, &mgr->free_list, __list);
  182. check_list_head(MEM_OBJ, &mgr->free_list, __list);
  183. } else {
  184. mobj = mgr->obj++;
  185. }
  186. system_unlock();
  187. return &mobj->obj;
  188. }
  189. static inline void free_mem_obj_to_mgr (MEM_MGR mgr, OBJ_TYPE * obj)
  190. {
  191. MEM_OBJ mobj = container_of(obj, MEM_OBJ_TYPE, obj);
  192. system_lock();
  193. MEM_AREA area, found = NULL;
  194. listp_for_each_entry(area, &mgr->area_list, __list)
  195. if (mobj >= area->objs && mobj < area->objs + area->size) {
  196. found = area;
  197. break;
  198. }
  199. if (found) {
  200. INIT_LIST_HEAD(mobj, __list);
  201. listp_add_tail(mobj, &mgr->free_list, __list);
  202. check_list_head(MEM_OBJ, &mgr->free_list, __list);
  203. }
  204. system_unlock();
  205. }
  206. #endif /* MEMMGR_H */