暫無描述
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

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