page_alloc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: David Frese
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include "utils.h"
  9. #include "page_constants.h"
  10. #include "page_alloc.h"
  11. #include "memory.h"
  12. /* A doubly linked list of free areas. (Not to be confused with Areas!) */
  13. typedef struct FreeArea {
  14. s48_address start; /* first page */
  15. unsigned long size; /* number of pages */
  16. struct FreeArea* next;
  17. struct FreeArea* prev;
  18. } FreeArea;
  19. static FreeArea* freelist;
  20. static unsigned long free_area_count = 0;
  21. static unsigned long free_page_count = 0;
  22. static FreeArea* make_free_area(s48_address start, unsigned long size) {
  23. FreeArea* new = (FreeArea*)malloc(sizeof(FreeArea));
  24. if (new == NULL) s48_gc_error("make_free_area: out of memory");
  25. new->start = start;
  26. new->size = size;
  27. new->next = NULL;
  28. new->prev = NULL;
  29. free_area_count = free_area_count + 1;
  30. return new;
  31. }
  32. inline static void deallocate_free_area(FreeArea* area) {
  33. free(area);
  34. free_area_count = free_area_count - 1;
  35. }
  36. inline static void connect(FreeArea* first, FreeArea* second) {
  37. second->prev = first;
  38. first->next = second;
  39. }
  40. inline static s48_address address_after_free_area(FreeArea* area) {
  41. /* Presumed safe because AREA->SIZE is the size in pages of an
  42. allocated block of memory. */
  43. return area->start + PAGES_TO_BYTES_I_KNOW_THIS_CAN_OVERFLOW(area->size);
  44. }
  45. inline static void adjust_free_area(FreeArea* area, unsigned long pages) {
  46. area->size = area->size + pages;
  47. }
  48. void s48_initialize_page_allocation() {
  49. freelist = make_free_area(0, 0);
  50. connect(freelist, freelist);
  51. free_area_count = 0;
  52. free_page_count = 0;
  53. }
  54. static void check_freelist() {
  55. FreeArea* area = freelist->next;
  56. unsigned int areas = 0, pages = 0;
  57. while (1) {
  58. s48_address end = address_after_free_area(area);
  59. if (area == freelist) {
  60. /* now we've seen the whole list */
  61. if ((areas != free_area_count) || (pages != free_page_count))
  62. s48_gc_error("bad page freelist (1)");
  63. else return; /* the list is OK */
  64. } else if ((end < area->start) ||
  65. ((freelist != area->prev) &&
  66. (area->start < address_after_free_area(area->prev)))) {
  67. s48_gc_error("bad page freelist (2)");
  68. } else {
  69. char dummy; unsigned long addr;
  70. /* check if area has correct addresses */
  71. addr = (unsigned long)(area->start);
  72. dummy = *(area->start);
  73. if ((addr % 4) != 0) {
  74. s48_gc_error("bad page start address");
  75. }
  76. areas = areas + 1;
  77. pages = pages + area->size;
  78. area = area->next;
  79. /* LOOP */
  80. }
  81. }
  82. /* Never reached */
  83. }
  84. /* Add SIZE pages starting from START to the set of free pages. We
  85. walk down the list of free areas to find where START goes and then
  86. either merge with an existing area or create a new one. */
  87. void s48_free_pagesB(s48_address start, unsigned long size) {
  88. s48_address end = start + PAGES_TO_BYTES_I_KNOW_THIS_CAN_OVERFLOW(size);
  89. FreeArea* before = freelist;
  90. int done;
  91. if (PAGES_TO_BYTES_LOSES_P(size)) {
  92. s48_gc_error("s48_free_pagesB: integer overflow detected too late to avoid"
  93. " crash (%li pages requested)", size);
  94. };
  95. free_page_count = free_page_count + size;
  96. do {
  97. FreeArea* after = before->next;
  98. done = 1; /* true */
  99. if (after == freelist) {
  100. /* we're last */
  101. if ( (start == address_after_free_area(before)) &&
  102. (before != freelist))
  103. adjust_free_area(before, size);
  104. else {
  105. FreeArea* new = make_free_area(start, size);
  106. connect(before, new);
  107. connect(new, after);
  108. }
  109. } else {
  110. s48_address end_of_previous = address_after_free_area(before);
  111. assert(end_of_previous <= start);
  112. if (after->start < start) {
  113. /* we're after AFTER */
  114. before = after;
  115. done = 0; /* LOOP */
  116. } else {
  117. assert(end <= after->start);
  118. if ((start == end_of_previous) &&
  119. (before != freelist)) {
  120. /* merge us with BEFORE */
  121. adjust_free_area(before, size);
  122. if (end == after->start) {
  123. /* and with AFTER, deleting AFTER */
  124. adjust_free_area(before, after->size);
  125. connect(before, after->next);
  126. deallocate_free_area(after);
  127. }
  128. } else if (end == after->start) {
  129. /* merge us with AFTER */
  130. after->start = start;
  131. adjust_free_area(after, size);
  132. } else {
  133. /* nothing doing, we're on our own */
  134. FreeArea* new = make_free_area(start, size);
  135. connect(before, new);
  136. connect(new, after);
  137. }
  138. }
  139. }
  140. } while (!done);
  141. check_freelist();
  142. }
  143. /* Getting more memory from the OS */
  144. /* Grab at least a quarter-megabyte (2**18) at a time. */
  145. /* minimum_allocation_quantum = 64 Pages (= 64 * 4KB = 256 KB) */
  146. static unsigned long minimum_allocation_quantum = BYTES_TO_PAGES(2 << 18);
  147. #define generic_max(a, b) ((a < b) ? b : a)
  148. /* We grab the memory and then cut it down to even page boundaries. */
  149. /* Returns 0 on failure, non-zero on success. */
  150. static int get_more_memory(unsigned long minimum) {
  151. unsigned long ask_for = generic_max(minimum, minimum_allocation_quantum) + 1;
  152. /* may lose up to one full page on the ends */
  153. unsigned long size = PAGES_TO_BYTES_I_KNOW_THIS_CAN_OVERFLOW(ask_for);
  154. s48_address memory;
  155. if (PAGES_TO_BYTES_LOSES_P(ask_for)) {
  156. s48_gc_error("get_more_memory: integer overflow detected too late to avoid"
  157. " crash (%li pages requested)", minimum);
  158. };
  159. memory = (s48_address)malloc(size);
  160. if (memory == NULL)
  161. return 0;
  162. else {
  163. s48_address start = PAGE_START_ADDRESS(memory);
  164. if (start == memory)
  165. s48_free_pagesB(start, ask_for);
  166. else
  167. s48_free_pagesB(ADD_PAGES_I_KNOW_THIS_CAN_OVERFLOW(start, 1),
  168. ask_for - 1);
  169. return 1;
  170. }
  171. }
  172. /* Do a first-fit search of the free list to find a free section of
  173. between MINIMUM and MAXIMUM pages, inclusive. */
  174. /* Returns the number of pages allocated (between MINIMUM and MAXIMUM,
  175. or 0 on failure). */
  176. unsigned long s48_allocate_pages(unsigned long minimum,
  177. unsigned long maximum,
  178. s48_address* start) {
  179. FreeArea* area = freelist->next;
  180. if (PAGES_TO_BYTES_LOSES_P(minimum) || PAGES_TO_BYTES_LOSES_P(maximum)) {
  181. s48_gc_error("s48_allocate_pages: integer overflow detected too late to"
  182. " avoid crash (%li..%li pages requested)", minimum, maximum);
  183. };
  184. while (1) {
  185. if (area == freelist) {
  186. if (!get_more_memory(minimum)) {
  187. return 0;
  188. };
  189. area = freelist->next; /* LOOP */
  190. } else if (area->size < minimum) {
  191. area = area->next; /* LOOP */
  192. } else if (maximum < area->size) {
  193. *start = area->start;
  194. area->start = ADD_PAGES_I_KNOW_THIS_CAN_OVERFLOW(area->start, minimum);
  195. /* This integer overflow seems to be harmless. */
  196. adjust_free_area(area, - ((long) minimum));
  197. free_page_count = free_page_count - minimum;
  198. check_freelist();
  199. return minimum;
  200. } else {
  201. unsigned long size = area->size;
  202. *start = area->start;
  203. connect(area->prev, area->next);
  204. deallocate_free_area(area);
  205. free_page_count = free_page_count - size;
  206. check_freelist();
  207. #if (BIBOP_LOG)
  208. s48_bibop_log("s48_allocate_pages: minimum %li < area->size %li < maximum %li",
  209. minimum, size, maximum);
  210. #endif
  211. return size;
  212. }
  213. }
  214. /* Never reached */
  215. }