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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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, int safe) {
  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. bad &= !safe;
  20. debug_print_tree(indent + 1, node->left, safe);
  21. for (int ii = 0; ii < indent; ii++) { printf(" "); }
  22. if (node->color == COLOR_RED) { printf("\e[31m"); }
  23. else if (bad) { printf("\e[30m]"); }
  24. if (bad) { printf("\e[43m"); }
  25. printf("%p %lu\n", node, node->size);
  26. printf("\e[37m");
  27. if (bad) { printf("\e[40m"); }
  28. debug_print_tree(indent + 1, node->right, safe);
  29. }
  30. }
  31. #endif
  32. TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
  33. return NULL;
  34. }
  35. /*
  36. * Search for the node whose allocated region contains an address.
  37. */
  38. TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
  39. TreeAlloc *head = root;
  40. while (1) {
  41. if (head > (TreeAlloc*) address) {
  42. if (head->left == NULL) {
  43. return NULL;
  44. } else {
  45. head = head->left;
  46. }
  47. } else {
  48. if (head->right == NULL || head->right > (TreeAlloc*) address) {
  49. return head;
  50. } else {
  51. head = head->right;
  52. }
  53. }
  54. }
  55. }
  56. static uintptr_t effective_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  57. return head->size - (align_after(head + padding, align) - (void*) head);
  58. }
  59. /*
  60. * This is the most optimistic estimate of size that we can use which also preserves the ordering over
  61. * the tree. I had planned to use effective_size before I realized that it would break the tree
  62. * ordering.
  63. */
  64. static uintptr_t pessimistic_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  65. return head->size - padding - align + 1;
  66. }
  67. TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size) {
  68. TreeAlloc *head = root;
  69. while (1) {
  70. uintptr_t esize = pessimistic_size(head, padding, align);
  71. if (esize < size) {
  72. if (head->right == NULL) {
  73. return NULL;
  74. } else {
  75. head = head->right;
  76. }
  77. } else {
  78. if (head->left == NULL || pessimistic_size(head->left, padding, align) < size) {
  79. return head;
  80. } else {
  81. head = head->left;
  82. }
  83. }
  84. }
  85. }
  86. TreeAlloc *succ(TreeAlloc *el) {
  87. if (el->right != NULL) {
  88. el = el->right;
  89. while (el->left != NULL) {
  90. el = el->left;
  91. }
  92. return el;
  93. }
  94. while (el->parent != NULL && el == el->parent->right) {
  95. el = el->parent;
  96. }
  97. return el->parent;
  98. }
  99. TreeAlloc *pred(TreeAlloc *el) {
  100. if (el->left != NULL) {
  101. el = el->left;
  102. while (el->right != NULL) {
  103. el = el->right;
  104. }
  105. return el;
  106. }
  107. while (el->parent != NULL && el == el->parent->left) {
  108. el = el->parent;
  109. }
  110. return el->parent;
  111. }
  112. TreeAlloc *get_sibling(TreeAlloc *p, TreeAlloc *ta) {
  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->parent, 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, 0);
  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, 0);
  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, 0);
  224. printf("===== CURRENT TREE =====\n");
  225. debug_print_tree(0, *root_ptr, 0);
  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, 0);
  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, 0);
  253. printf("===== CURRENT TREE =====\n");
  254. debug_print_tree(0, *root_ptr, 0);
  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, 0);
  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 *parent, TreeAlloc *node) {
  289. #ifdef DEBUG
  290. printf("delete fixup at %p -> %p\n", parent, node);
  291. #endif
  292. // In theory, the last two conditions should be the same ...
  293. if (IS_RED_NODE(node) || (node != NULL && node == *root_ptr)) {
  294. node->color = COLOR_BLACK;
  295. } else {
  296. TreeAlloc *sibling = get_sibling(parent, node);
  297. if (IS_RED_NODE(sibling)) {
  298. if (parent->left == node) {
  299. rotate_left(root_ptr, parent);
  300. } else {
  301. rotate_right(root_ptr, parent);
  302. }
  303. // The rotate shouldn't touch the parent relationship of `node`
  304. parent->parent->color = COLOR_BLACK;
  305. parent->color = COLOR_RED;
  306. sibling = get_sibling(parent, node);
  307. }
  308. if (IS_BLACK_NODE(sibling->left) && IS_BLACK_NODE(sibling->right)) {
  309. if (node != NULL)
  310. node->color = COLOR_BLACK;
  311. sibling->color = COLOR_RED;
  312. repair_after_remove(root_ptr, parent->parent, parent);
  313. } else {
  314. if (parent->left == node && IS_BLACK_NODE(sibling->right)) {
  315. rotate_right(root_ptr, sibling);
  316. sibling->color = COLOR_RED;
  317. sibling = parent->right;
  318. sibling->color = COLOR_BLACK;
  319. }
  320. if (parent->right == node && IS_BLACK_NODE(sibling->left)) {
  321. rotate_left(root_ptr, sibling);
  322. sibling->color = COLOR_RED;
  323. sibling = parent->left;
  324. sibling->color = COLOR_BLACK;
  325. }
  326. if (parent->left == node) {
  327. rotate_left(root_ptr, parent);
  328. } else {
  329. rotate_right(root_ptr, parent);
  330. }
  331. if (node != NULL)
  332. node->color = COLOR_BLACK;
  333. TreeAlloc *uncle = get_sibling(parent->parent, parent);
  334. if (uncle != NULL)
  335. uncle->color = COLOR_BLACK;
  336. char swap = parent->color;
  337. parent->color = parent->parent->color;
  338. parent->parent->color = swap;
  339. }
  340. }
  341. }
  342. void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
  343. char do_repair = 0;
  344. char old_color;
  345. #ifdef DEBUG
  346. printf("====== PRE-REMOVE ======\n");
  347. printf("======= REMOVING =======\n");
  348. debug_print_tree(0, to_remove, 0);
  349. printf("===== CURRENT TREE =====\n");
  350. debug_print_tree(0, *root_ptr, 0);
  351. printf("===== END OF TREES =====\n");
  352. #endif
  353. TreeAlloc *replace, *parent_of_replace;
  354. TreeAlloc *parent = to_remove->parent;
  355. if (!to_remove->left) {
  356. #ifdef DEBUG
  357. printf("code path 1l\n");
  358. #endif
  359. replace = to_remove->right;
  360. parent_of_replace = to_remove->parent;
  361. do_repair = to_remove->color == COLOR_BLACK;
  362. replace_node(root_ptr, to_remove, replace);
  363. } else if (!to_remove->right) {
  364. #ifdef DEBUG
  365. printf("code path 1r\n");
  366. #endif
  367. replace = to_remove->left;
  368. parent_of_replace = to_remove->parent;
  369. do_repair = to_remove->color == COLOR_BLACK;
  370. replace_node(root_ptr, to_remove, replace);
  371. } else {
  372. #ifdef DEBUG
  373. printf("code path 2\n");
  374. #endif
  375. TreeAlloc *tmp = succ(to_remove);
  376. replace = tmp->right;
  377. do_repair = tmp->color == COLOR_BLACK;
  378. if (tmp != to_remove->right) {
  379. replace_node(root_ptr, tmp, replace);
  380. tmp->right = to_remove->right;
  381. to_remove->right->parent = tmp;
  382. parent_of_replace = tmp->parent;
  383. } else {
  384. parent_of_replace = tmp;
  385. }
  386. replace_node(root_ptr, to_remove, tmp);
  387. tmp->color = to_remove->color;
  388. tmp->left = to_remove->left;
  389. to_remove->left->parent = tmp;
  390. }
  391. // Make sure that it doesn't have any tree pointers it shouldn't have.
  392. to_remove->parent = to_remove->left = to_remove->right = NULL;
  393. #ifdef DEBUG
  394. printf("==== PRE-REMOVE-FIXUP ===\n");
  395. printf("===== CURRENT TREE =====\n");
  396. debug_print_tree(0, *root_ptr, 1);
  397. printf("===== END OF TREES =====\n");
  398. printf("considering fixing up %p -> %p\n", parent_of_replace, replace);
  399. #endif
  400. if (replace && parent_of_replace == NULL) {
  401. replace->color = COLOR_BLACK;
  402. } else if (parent_of_replace != NULL && do_repair) {
  403. repair_after_remove(root_ptr, parent_of_replace, replace);
  404. }
  405. #ifdef DEBUG
  406. printf("=== POST-REMOVE ===\n");
  407. printf("===== CURRENT TREE =====\n");
  408. debug_print_tree(0, *root_ptr, 0);
  409. printf("===== END OF TREES =====\n");
  410. #endif
  411. }
  412. int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {
  413. uintptr_t realsize = size + align + alignof(WatermarkAlloc) + padding - 1;
  414. if (realsize < MIN_NEW_MEM_SIZE) {
  415. realsize = MIN_NEW_MEM_SIZE;
  416. }
  417. FreeSpace *reg = (FreeSpace*) arena->get_new_region(realsize);
  418. if (reg == NULL) {
  419. arena->error("can't allocate a new memory region!");
  420. return 0;
  421. }
  422. FreeSpace *newreg = align_after(reg, alignof(WatermarkAlloc));
  423. newreg->left = NULL;
  424. newreg->right = NULL;
  425. realsize -= (void*) newreg - (void*) reg;
  426. realsize -= realsize % alignof(WatermarkAlloc);
  427. newreg->size = realsize;
  428. if (arena->root_freespace == NULL) {
  429. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg);
  430. } else {
  431. FreeSpace *head = arena->root_freespace;
  432. while (head->right != NULL) {
  433. head = head->right;
  434. }
  435. insert_right((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg, (TreeAlloc*) head);
  436. }
  437. #ifdef DEBUG
  438. printf("= POST-REGION-CREATION =\n");
  439. printf("==== FREESPACE TREE ====\n");
  440. debug_print_tree(0, arena->root_freespace, 0);
  441. printf("==== TREEALLOC TREE ====\n");
  442. debug_print_tree(0, arena->root_treealloc, 0);
  443. printf("===== END OF TREES =====\n");
  444. #endif
  445. return 1;
  446. }
  447. void unalloc(Arena *arena, void *addr) {
  448. #ifdef DEBUG
  449. printf("==== UNALLOCATING ====\n");
  450. printf("=== FREESPACE TREE ===\n");
  451. debug_print_tree(0, arena->root_freespace, 0);
  452. printf("=== TREEALLOC TREE ===\n");
  453. debug_print_tree(0, arena->root_treealloc, 0);
  454. printf("==== END OF TREES ====\n");
  455. #endif
  456. if (arena->root_treealloc == NULL) {
  457. arena->error("attempt to unallocate when there are no allocations!");
  458. return;
  459. }
  460. // Find the node this address belongs to
  461. TreeAlloc *node = search_by_address(arena->root_treealloc, addr);
  462. if (node == NULL) {
  463. arena->error("attempt to free memory outside any allocations!");
  464. return;
  465. }
  466. // Handle the watermark allocator in this region
  467. if (node->type == RT_WATERMARK) {
  468. // TODO: handle watermark deallocation
  469. return;
  470. }
  471. // Get rid of it
  472. remove_node(&arena->root_treealloc, node);
  473. // If there's free space on either side of it, merge it with the free space into a bigger chunk of
  474. // free space.
  475. uintptr_t size = node->size;
  476. FreeSpace *start = (FreeSpace*) node;
  477. if (node->before != NULL && node->before->type == RT_FREESPACE) {
  478. start = (FreeSpace*) node->before;
  479. size += node->before->size;
  480. remove_node((TreeAlloc**) &arena->root_freespace, node->before);
  481. }
  482. if (node->after != NULL && node->after->type == RT_FREESPACE) {
  483. size += node->after->size;
  484. remove_node((TreeAlloc**) &arena->root_freespace, node->after);
  485. }
  486. start->type = RT_FREESPACE;
  487. start->size = size;
  488. // And finally, insert the resulting free space.
  489. if (arena->root_freespace == NULL) {
  490. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start);
  491. } else {
  492. TreeAlloc *insert_point = search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, size);
  493. if (insert_point == NULL) {
  494. TreeAlloc *head = (TreeAlloc*) arena->root_freespace;
  495. while (head->right != NULL) {
  496. head = head->right;
  497. }
  498. insert_right((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, head);
  499. } else {
  500. insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, insert_point);
  501. }
  502. }
  503. }
  504. void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
  505. uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
  506. #ifdef DEBUG
  507. printf("==== ALLOCATING =====\n");
  508. printf("=== FREESPACE TREE ===\n");
  509. debug_print_tree(0, arena->root_freespace, 0);
  510. printf("=== TREEALLOC TREE ===\n");
  511. debug_print_tree(0, arena->root_treealloc, 0);
  512. printf("==== END OF TREES ====\n");
  513. #endif
  514. if (arena->root_freespace == NULL) {
  515. // Handle being out of freespace.
  516. #ifdef DEBUG
  517. printf("Out of freespace nodes; getting more\n");
  518. #endif
  519. if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
  520. return NULL;
  521. }
  522. return alloc(arena, size, align);
  523. } else {
  524. TreeAlloc *region = search_by_size((TreeAlloc*) arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
  525. if (region == NULL) {
  526. // Handle insufficient freespace or fragmentation.
  527. #ifdef DEBUG
  528. printf("Out of sufficiently large freespace nodes; getting more\n");
  529. #endif
  530. if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
  531. return NULL;
  532. }
  533. return alloc(arena, size, align);
  534. }
  535. remove_node((TreeAlloc**) &arena->root_freespace, region);
  536. void *true_end = align_after(align_after(((void*) region) + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
  537. // The size of the new allocation (adjusted for region header and alignment
  538. uintptr_t new_size = true_end - (void*) region;
  539. // The size of the free space region following the new allocation
  540. uintptr_t new_free_size = region->size - new_size;
  541. region->right = NULL;
  542. region->left = NULL;
  543. region->type = RT_TREE_NODE;
  544. #ifdef DEBUG
  545. printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
  546. printf("size: %lu -> %lu\n", size, new_size);
  547. #endif
  548. if (arena->root_treealloc == NULL) {
  549. insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
  550. } else {
  551. #ifdef DEBUG
  552. printf("searching for an insert point\n");
  553. #endif
  554. TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
  555. if (insert_point == NULL) {
  556. TreeAlloc *head = arena->root_treealloc;
  557. while (head->left != NULL) {
  558. head = head->left;
  559. }
  560. #ifdef DEBUG
  561. printf("none found; inserting before %p\n", head);
  562. #endif
  563. insert_left(&arena->root_treealloc, region, head);
  564. } else {
  565. #ifdef DEBUG
  566. printf("found one: %p\n", insert_point);
  567. #endif
  568. insert_right(&arena->root_treealloc, region, insert_point);
  569. }
  570. }
  571. if (new_free_size >= sizeof(FreeSpace)) {
  572. // If there's enough free space after the allocation, use it!
  573. region->size = new_size; // Safe because the allocated region tree is not sorted by size.
  574. FreeSpace *new_free = (FreeSpace*) ((void*) region + new_size);
  575. new_free->left = NULL;
  576. new_free->right = NULL;
  577. new_free->type = RT_FREESPACE;
  578. new_free->size = new_free_size;
  579. if (arena->root_freespace == NULL) {
  580. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free);
  581. } else {
  582. FreeSpace *insert_point = (FreeSpace*) search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, new_free_size);
  583. insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free, (TreeAlloc*) insert_point);
  584. }
  585. // Set the region following this one to be the new free space
  586. region->after = (TreeAlloc*) new_free;
  587. } else {
  588. // There isn't a free space after this one, so put the `next` pointer at the next allocated
  589. // region.
  590. region->after = succ(region);
  591. }
  592. // I seem to have forgotten about the fact that memory may not be contiguous
  593. if (region->after != NULL && region->after != (void*) region + region->size) {
  594. region->after = NULL;
  595. }
  596. // Also make sure the `before` pointer is correct.
  597. TreeAlloc *before_alloc = pred(region);
  598. if (before_alloc == NULL || ((void*) before_alloc) + before_alloc->size < (void*) region) {
  599. region->before = search_by_address((TreeAlloc*) arena->root_freespace, region);
  600. } else {
  601. region->before = before_alloc;
  602. }
  603. // I seem to have forgotten about the fact that memory may not be contiguous
  604. if (region->before != NULL && region->before != (void*) region->before + region->before->size) {
  605. region->before = NULL;
  606. }
  607. #ifdef DEBUG
  608. printf("region is still at %p\n", region);
  609. #endif
  610. return align_after((void*) region + sizeof(TreeAlloc), actual_align);
  611. }
  612. }
  613. void *alloc_growable(Arena *arena, uintptr_t size, uintptr_t align) {
  614. // TODO: Basically the same as above, but put the allocated region in the center of the largest free
  615. // space. Due to alignment and whatnot, the code will be gory.
  616. return NULL;
  617. }