No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree_alloc.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #include <stddef.h>
  2. #include <stdalign.h>
  3. #include "allocator_internal.h"
  4. #ifdef DEBUG
  5. #include <stdio.h>
  6. int debug_tree_black_height(TreeAlloc *node) {
  7. if (node == NULL) {
  8. return 1;
  9. }
  10. return ((node->color == COLOR_BLACK) ? 1 : 0) + debug_tree_black_height(node->left);
  11. }
  12. void debug_print_tree(int indent, void *p) {
  13. TreeAlloc *node = (TreeAlloc*) p;
  14. if (node != NULL) {
  15. int bad = debug_tree_black_height(node->left) != debug_tree_black_height(node->right);
  16. bad |= node->color == COLOR_RED && ((node->left != NULL && node->left->color == COLOR_RED) ||
  17. (node->right != NULL && node->right->color == COLOR_RED) || (node->parent != NULL &&
  18. node->parent->color == COLOR_RED));
  19. debug_print_tree(indent + 1, node->left);
  20. for (int ii = 0; ii < indent; ii++) { printf(" "); }
  21. if (node->color == COLOR_RED) { printf("\e[31m"); }
  22. else if (bad) { printf("\e[30m]"); }
  23. if (bad) { printf("\e[43m"); }
  24. printf("%p %lu\n", node, node->size);
  25. printf("\e[37m");
  26. if (bad) { printf("\e[40m"); }
  27. debug_print_tree(indent + 1, node->right);
  28. }
  29. }
  30. #endif
  31. TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
  32. return NULL;
  33. }
  34. /*
  35. * Search for the node whose allocated region contains an address.
  36. */
  37. TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
  38. TreeAlloc *head = root;
  39. while (1) {
  40. if (head > (TreeAlloc*) address) {
  41. if (head->left == NULL) {
  42. return NULL;
  43. } else {
  44. head = head->left;
  45. }
  46. } else {
  47. if (head->right == NULL || head->right > (TreeAlloc*) address) {
  48. return head;
  49. } else {
  50. head = head->right;
  51. }
  52. }
  53. }
  54. }
  55. static uintptr_t effective_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  56. return head->size - (align_after(head + padding, align) - (void*) head);
  57. }
  58. /*
  59. * This is the most optimistic estimate of size that we can use which also preserves the ordering over
  60. * the tree. I had planned to use effective_size before I realized that it would break the tree
  61. * ordering.
  62. */
  63. static uintptr_t pessimistic_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  64. return head->size - padding - align + 1;
  65. }
  66. TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size) {
  67. TreeAlloc *head = root;
  68. while (1) {
  69. uintptr_t esize = pessimistic_size(head, padding, align);
  70. if (esize < size) {
  71. if (head->right == NULL) {
  72. return NULL;
  73. } else {
  74. head = head->right;
  75. }
  76. } else {
  77. if (head->left == NULL || pessimistic_size(head->left, padding, align) < size) {
  78. return head;
  79. } else {
  80. head = head->left;
  81. }
  82. }
  83. }
  84. }
  85. TreeAlloc *succ(TreeAlloc *el) {
  86. if (el->right != NULL) {
  87. el = el->right;
  88. while (el->left != NULL) {
  89. el = el->left;
  90. }
  91. return el;
  92. }
  93. while (el->parent != NULL && el == el->parent->right) {
  94. el = el->parent;
  95. }
  96. return el->parent;
  97. }
  98. TreeAlloc *pred(TreeAlloc *el) {
  99. if (el->left != NULL) {
  100. el = el->left;
  101. while (el->right != NULL) {
  102. el = el->right;
  103. }
  104. return el;
  105. }
  106. while (el->parent != NULL && el == el->parent->left) {
  107. el = el->parent;
  108. }
  109. return el->parent;
  110. }
  111. TreeAlloc *get_sibling(TreeAlloc *ta) {
  112. TreeAlloc *p = ta->parent;
  113. if (!p)
  114. return NULL;
  115. else if (p->left == ta)
  116. return p->right;
  117. else
  118. return p->left;
  119. }
  120. void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta) {
  121. TreeAlloc *parent, *tmp;
  122. tmp = ta->right;
  123. parent = ta->parent;
  124. if (tmp) {
  125. ta->right = tmp->left;
  126. tmp->left = ta;
  127. tmp->parent = parent;
  128. }
  129. if (ta->right) ta->right->parent = ta;
  130. ta->parent = tmp;
  131. if (!parent) {
  132. *root_ptr = tmp;
  133. } else if (ta == parent->left) {
  134. parent->left = tmp;
  135. } else {
  136. parent->right = tmp;
  137. }
  138. }
  139. void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
  140. TreeAlloc *parent, *tmp;
  141. tmp = ta->left;
  142. parent = ta->parent;
  143. if (tmp) {
  144. ta->left = tmp->right;
  145. tmp->right = ta;
  146. tmp->parent = parent;
  147. }
  148. if (ta->left) ta->left->parent = ta;
  149. ta->parent = tmp;
  150. if (!parent) {
  151. *root_ptr = tmp;
  152. } else if (ta == parent->left) {
  153. parent->left = tmp;
  154. } else {
  155. parent->right = tmp;
  156. }
  157. }
  158. #define IS_BLACK_NODE(n) (n == NULL || n->color == COLOR_BLACK)
  159. #define IS_RED_NODE(n) (n != NULL && n->color == COLOR_RED)
  160. void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
  161. TreeAlloc *parent = ta->parent;
  162. if (ta == *root_ptr) {
  163. ta->color = COLOR_BLACK;
  164. } else if (IS_BLACK_NODE(parent)) {
  165. return;
  166. } else {
  167. TreeAlloc *uncle = get_sibling(parent);
  168. TreeAlloc *grandparent = parent->parent;
  169. if (IS_RED_NODE(uncle)) {
  170. parent->color = COLOR_BLACK;
  171. uncle->color = COLOR_BLACK;
  172. grandparent->color = COLOR_RED;
  173. repair_tree_after_insert(root_ptr, grandparent);
  174. } else {
  175. if (parent->left == ta) {
  176. if (grandparent->left == parent) {
  177. rotate_right(root_ptr, grandparent);
  178. grandparent->color = COLOR_RED;
  179. parent->color = COLOR_BLACK;
  180. } else {
  181. rotate_right(root_ptr, parent);
  182. rotate_left(root_ptr, grandparent);
  183. grandparent->color = COLOR_RED;
  184. ta->color = COLOR_BLACK;
  185. }
  186. } else {
  187. if (grandparent->left == parent) {
  188. rotate_left(root_ptr, parent);
  189. rotate_right(root_ptr, grandparent);
  190. grandparent->color = COLOR_RED;
  191. ta->color = COLOR_BLACK;
  192. } else {
  193. rotate_left(root_ptr, grandparent);
  194. grandparent->color = COLOR_RED;
  195. parent->color = COLOR_BLACK;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. // Inserts a node into an empty tree.
  202. void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
  203. #ifdef DEBUG
  204. printf("= PRE-INSERT-SINGLETON =\n");
  205. printf("===== CURRENT TREE =====\n");
  206. debug_print_tree(0, *root_ptr);
  207. printf("===== END OF TREES =====\n");
  208. #endif
  209. *root_ptr = to_insert;
  210. to_insert->parent = NULL;
  211. to_insert->color = COLOR_BLACK;
  212. #ifdef DEBUG
  213. printf("= POST-INSERT-SINGLETON =\n");
  214. printf("===== CURRENT TREE =====\n");
  215. debug_print_tree(0, *root_ptr);
  216. printf("===== END OF TREES =====\n");
  217. #endif
  218. }
  219. void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
  220. #ifdef DEBUG
  221. printf("=== PRE-INSERT-RIGHT ===\n");
  222. printf("===== INSERTING =====\n");
  223. debug_print_tree(0, to_insert);
  224. printf("===== CURRENT TREE =====\n");
  225. debug_print_tree(0, *root_ptr);
  226. printf("===== END OF TREES =====\n");
  227. #endif
  228. if (after->right != NULL) {
  229. after = after->right;
  230. while (after->left != NULL) {
  231. after = after->left;
  232. }
  233. after->left = to_insert;
  234. to_insert->parent = after;
  235. } else {
  236. after->right = to_insert;
  237. to_insert->parent = after;
  238. }
  239. to_insert->color = COLOR_RED;
  240. repair_tree_after_insert(root_ptr, to_insert);
  241. #ifdef DEBUG
  242. printf("== POST-INSERT-FIXUP ===\n");
  243. printf("===== CURRENT TREE =====\n");
  244. debug_print_tree(0, *root_ptr);
  245. printf("===== END OF TREES =====\n");
  246. #endif
  247. }
  248. void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
  249. #ifdef DEBUG
  250. printf("=== PRE-INSERT-LEFT ====\n");
  251. printf("===== INSERTING =====\n");
  252. debug_print_tree(0, to_insert);
  253. printf("===== CURRENT TREE =====\n");
  254. debug_print_tree(0, *root_ptr);
  255. printf("===== END OF TREES =====\n");
  256. #endif
  257. if (before->left != NULL) {
  258. before = before->left;
  259. while (before->right != NULL) {
  260. before = before->right;
  261. }
  262. before->right = to_insert;
  263. to_insert->parent = before;
  264. } else {
  265. before->left = to_insert;
  266. to_insert->parent = before;
  267. }
  268. to_insert->color = COLOR_RED;
  269. repair_tree_after_insert(root_ptr, to_insert);
  270. #ifdef DEBUG
  271. printf("== POST-INSERT-FIXUP ===\n");
  272. printf("===== CURRENT TREE =====\n");
  273. debug_print_tree(0, *root_ptr);
  274. printf("===== END OF TREES =====\n");
  275. #endif
  276. }
  277. void replace_node(TreeAlloc **root_ptr, TreeAlloc *node, TreeAlloc *replace) {
  278. if (!node->parent) {
  279. *root_ptr = replace;
  280. } else {
  281. if (node == node->parent->left)
  282. node->parent->left = replace;
  283. else
  284. node->parent->right = replace;
  285. }
  286. if (replace) replace->parent = node->parent;
  287. }
  288. void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
  289. if (IS_RED_NODE(node)) {
  290. node->color = COLOR_BLACK;
  291. } else {
  292. TreeAlloc *sibling = get_sibling(node);
  293. if (IS_RED_NODE(sibling)) {
  294. if (node->parent->left == node) {
  295. rotate_left(root_ptr, node->parent);
  296. } else {
  297. rotate_right(root_ptr, node->parent);
  298. }
  299. node->parent->parent->color = node->parent->color = COLOR_BLACK;
  300. }
  301. if (IS_BLACK_NODE(sibling->left) && IS_BLACK_NODE(sibling->right)) {
  302. node->color = COLOR_BLACK;
  303. sibling->color = COLOR_RED;
  304. repair_after_remove(root_ptr, node->parent);
  305. } else {
  306. if (node->parent->left == node && IS_BLACK_NODE(sibling)) {
  307. rotate_right(root_ptr, sibling);
  308. sibling = get_sibling(node);
  309. sibling->color = COLOR_RED;
  310. sibling->right->color = COLOR_RED;
  311. rotate_left(root_ptr, node->parent);
  312. node->color = get_sibling(node->parent)->color = COLOR_BLACK;
  313. } else if (node->parent->right == node && IS_BLACK_NODE(sibling)) {
  314. rotate_left(root_ptr, sibling);
  315. sibling = get_sibling(node);
  316. sibling->color = COLOR_RED;
  317. sibling->left->color = COLOR_RED;
  318. rotate_right(root_ptr, sibling);
  319. node->color = get_sibling(node->parent)->color = COLOR_BLACK;
  320. }
  321. node->parent->color ^= node->parent->parent->color;
  322. node->parent->parent->color ^= node->parent->color;
  323. node->parent->color ^= node->parent->parent->color;
  324. }
  325. }
  326. }
  327. void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
  328. char do_repair = 0;
  329. #ifdef DEBUG
  330. printf("====== PRE-REMOVE ======\n");
  331. printf("===== REMOVING =====\n");
  332. debug_print_tree(0, to_remove);
  333. printf("===== CURRENT TREE =====\n");
  334. debug_print_tree(0, *root_ptr);
  335. printf("===== END OF TREES =====\n");
  336. #endif
  337. TreeAlloc *replace;
  338. TreeAlloc *parent = to_remove->parent;
  339. if (!to_remove->left) {
  340. replace = to_remove->right;
  341. do_repair = to_remove->color == COLOR_BLACK;
  342. replace_node(root_ptr, to_remove, replace);
  343. } else if (!to_remove->right) {
  344. replace = to_remove->left;
  345. do_repair = to_remove->color == COLOR_BLACK;
  346. replace_node(root_ptr, to_remove, replace);
  347. } else {
  348. TreeAlloc *tmp = to_remove->right;
  349. while (tmp->left) tmp = tmp->left;
  350. replace = tmp->right;
  351. do_repair = tmp->color == COLOR_BLACK;
  352. if (tmp != to_remove->right) {
  353. replace_node(root_ptr, tmp, replace);
  354. tmp->right = to_remove->right;
  355. to_remove->right->parent = tmp;
  356. }
  357. replace_node(root_ptr, to_remove, tmp);
  358. tmp->color = to_remove->color;
  359. tmp->left = to_remove->left;
  360. to_remove->left->parent = tmp;
  361. }
  362. // Make sure that it doesn't have any tree pointers it shouldn't have.
  363. to_remove->parent = to_remove->left = to_remove->right = NULL;
  364. if (replace && replace->parent == NULL) {
  365. replace->color = COLOR_BLACK;
  366. } else if (do_repair && replace) {
  367. repair_after_remove(root_ptr, replace);
  368. }
  369. #ifdef DEBUG
  370. printf("=== POST-REMOVE ===\n");
  371. printf("===== CURRENT TREE =====\n");
  372. debug_print_tree(0, *root_ptr);
  373. printf("===== END OF TREES =====\n");
  374. #endif
  375. }
  376. int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {
  377. uintptr_t realsize = size + align + alignof(WatermarkAlloc) + padding - 1;
  378. if (realsize < MIN_NEW_MEM_SIZE) {
  379. realsize = MIN_NEW_MEM_SIZE;
  380. }
  381. FreeSpace *reg = (FreeSpace*) arena->get_new_region(realsize);
  382. if (reg == NULL) {
  383. arena->error("can't allocate a new memory region!");
  384. return 0;
  385. }
  386. FreeSpace *newreg = align_after(reg, alignof(WatermarkAlloc));
  387. newreg->left = NULL;
  388. newreg->right = NULL;
  389. realsize -= (void*) newreg - (void*) reg;
  390. realsize -= realsize % alignof(WatermarkAlloc);
  391. newreg->size = realsize;
  392. if (arena->root_freespace == NULL) {
  393. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg);
  394. } else {
  395. FreeSpace *head = arena->root_freespace;
  396. while (head->right != NULL) {
  397. head = head->right;
  398. }
  399. insert_right((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg, (TreeAlloc*) head);
  400. }
  401. #ifdef DEBUG
  402. printf("= POST-REGION-CREATION =\n");
  403. printf("==== FREESPACE TREE ====\n");
  404. debug_print_tree(0, arena->root_freespace);
  405. printf("==== TREEALLOC TREE ====\n");
  406. debug_print_tree(0, arena->root_treealloc);
  407. printf("===== END OF TREES =====\n");
  408. #endif
  409. return 1;
  410. }
  411. void unalloc(Arena *arena, void *addr) {
  412. #ifdef DEBUG
  413. printf("==== UNALLOCATING ====\n");
  414. printf("=== FREESPACE TREE ===\n");
  415. debug_print_tree(0, arena->root_freespace);
  416. printf("=== TREEALLOC TREE ===\n");
  417. debug_print_tree(0, arena->root_treealloc);
  418. printf("==== END OF TREES ====\n");
  419. #endif
  420. if (arena->root_treealloc == NULL) {
  421. arena->error("attempt to unallocate when there are no allocations!");
  422. return;
  423. }
  424. // Find the node this address belongs to
  425. TreeAlloc *node = search_by_address(arena->root_treealloc, addr);
  426. if (node == NULL) {
  427. arena->error("attempt to free memory outside any allocations!");
  428. return;
  429. }
  430. // Handle the watermark allocator in this region
  431. if (node->type == RT_WATERMARK) {
  432. // TODO: handle watermark deallocation
  433. return;
  434. }
  435. // Get rid of it
  436. remove_node(&arena->root_treealloc, node);
  437. // If there's free space on either side of it, merge it with the free space into a bigger chunk of
  438. // free space.
  439. uintptr_t size = node->size;
  440. FreeSpace *start = (FreeSpace*) node;
  441. if (node->before != NULL && node->before->type == RT_FREESPACE) {
  442. start = (FreeSpace*) node->before;
  443. size += node->before->size;
  444. remove_node((TreeAlloc**) &arena->root_freespace, node->before);
  445. }
  446. if (node->after != NULL && node->after->type == RT_FREESPACE) {
  447. size += node->after->size;
  448. remove_node((TreeAlloc**) &arena->root_freespace, node->after);
  449. }
  450. start->type = RT_FREESPACE;
  451. start->size = size;
  452. // And finally, insert the resulting free space.
  453. if (arena->root_freespace == NULL) {
  454. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start);
  455. } else {
  456. TreeAlloc *insert_point = search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, size);
  457. if (insert_point == NULL) {
  458. TreeAlloc *head = (TreeAlloc*) arena->root_freespace;
  459. while (head->right != NULL) {
  460. head = head->right;
  461. }
  462. insert_right((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, head);
  463. } else {
  464. insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, insert_point);
  465. }
  466. }
  467. }
  468. void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
  469. uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
  470. #ifdef DEBUG
  471. printf("==== ALLOCATING =====\n");
  472. printf("=== FREESPACE TREE ===\n");
  473. debug_print_tree(0, arena->root_freespace);
  474. printf("=== TREEALLOC TREE ===\n");
  475. debug_print_tree(0, arena->root_treealloc);
  476. printf("==== END OF TREES ====\n");
  477. #endif
  478. if (arena->root_freespace == NULL) {
  479. // Handle being out of freespace.
  480. #ifdef DEBUG
  481. printf("Out of freespace nodes; getting more\n");
  482. #endif
  483. if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
  484. return NULL;
  485. }
  486. return alloc(arena, size, align);
  487. } else {
  488. TreeAlloc *region = search_by_size((TreeAlloc*) arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
  489. if (region == NULL) {
  490. // Handle insufficient freespace or fragmentation.
  491. #ifdef DEBUG
  492. printf("Out of sufficiently large freespace nodes; getting more\n");
  493. #endif
  494. if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
  495. return NULL;
  496. }
  497. return alloc(arena, size, align);
  498. }
  499. remove_node((TreeAlloc**) &arena->root_freespace, region);
  500. void *true_end = align_after(align_after(((void*) region) + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
  501. // The size of the new allocation (adjusted for region header and alignment
  502. uintptr_t new_size = true_end - (void*) region;
  503. // The size of the free space region following the new allocation
  504. uintptr_t new_free_size = region->size - new_size;
  505. region->right = NULL;
  506. region->left = NULL;
  507. region->type = RT_TREE_NODE;
  508. #ifdef DEBUG
  509. printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
  510. printf("size: %lu -> %lu\n", size, new_size);
  511. #endif
  512. if (arena->root_treealloc == NULL) {
  513. insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
  514. } else {
  515. #ifdef DEBUG
  516. printf("searching for an insert point\n");
  517. #endif
  518. TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
  519. if (insert_point == NULL) {
  520. TreeAlloc *head = arena->root_treealloc;
  521. while (head->left != NULL) {
  522. head = head->left;
  523. }
  524. #ifdef DEBUG
  525. printf("none found; inserting before %p\n", head);
  526. #endif
  527. insert_left(&arena->root_treealloc, region, head);
  528. } else {
  529. #ifdef DEBUG
  530. printf("found one: %p\n", insert_point);
  531. #endif
  532. insert_right(&arena->root_treealloc, region, insert_point);
  533. }
  534. }
  535. if (new_free_size >= sizeof(FreeSpace)) {
  536. // If there's enough free space after the allocation, use it!
  537. region->size = new_size; // Safe because the allocated region tree is not sorted by size.
  538. FreeSpace *new_free = (FreeSpace*) ((void*) region + new_size);
  539. new_free->left = NULL;
  540. new_free->right = NULL;
  541. new_free->type = RT_FREESPACE;
  542. new_free->size = new_free_size;
  543. if (arena->root_freespace == NULL) {
  544. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free);
  545. } else {
  546. FreeSpace *insert_point = (FreeSpace*) search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, new_free_size);
  547. insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free, (TreeAlloc*) insert_point);
  548. }
  549. // Set the region following this one to be the new free space
  550. region->after = (TreeAlloc*) new_free;
  551. } else {
  552. // There isn't a free space after this one, so put the `next` pointer at the next allocated
  553. // region.
  554. region->after = succ(region);
  555. }
  556. // I seem to have forgotten about the fact that memory may not be contiguous
  557. if (region->after != NULL && region->after != (void*) region + region->size) {
  558. region->after = NULL;
  559. }
  560. // Also make sure the `before` pointer is correct.
  561. TreeAlloc *before_alloc = pred(region);
  562. if (before_alloc == NULL || ((void*) before_alloc) + before_alloc->size < (void*) region) {
  563. region->before = search_by_address((TreeAlloc*) arena->root_freespace, region);
  564. } else {
  565. region->before = before_alloc;
  566. }
  567. // I seem to have forgotten about the fact that memory may not be contiguous
  568. if (region->before != NULL && region->before != (void*) region->before + region->before->size) {
  569. region->before = NULL;
  570. }
  571. #ifdef DEBUG
  572. printf("region is still at %p\n", region);
  573. #endif
  574. return align_after((void*) region + sizeof(TreeAlloc), actual_align);
  575. }
  576. }
  577. void *alloc_growable(Arena *arena, uintptr_t size, uintptr_t align) {
  578. // TODO: Basically the same as above, but put the allocated region in the center of the largest free
  579. // space. Due to alignment and whatnot, the code will be gory.
  580. return NULL;
  581. }