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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include <stddef.h>
  2. #include <stdalign.h>
  3. #include "allocator_internal.h"
  4. #ifdef DEBUG
  5. #include <stdio.h>
  6. void debug_print_tree(int indent, void *p) {
  7. TreeAlloc *node = (TreeAlloc*) p;
  8. if (node != NULL) {
  9. debug_print_tree(indent + 1, node->left);
  10. for (int ii = 0; ii < indent; ii++) { printf(" "); }
  11. if (node->color == COLOR_RED) { printf("\e[31"); }
  12. printf("%p %lu\n", node, node->size);
  13. if (node->color == COLOR_RED) { printf("\e[37"); }
  14. debug_print_tree(indent + 1, node->right);
  15. }
  16. }
  17. #endif
  18. TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
  19. return NULL;
  20. }
  21. /*
  22. * Search for the node whose allocated region contains an address.
  23. */
  24. TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
  25. TreeAlloc *head = root;
  26. while (1) {
  27. if (head > (TreeAlloc*) address) {
  28. if (head->left == NULL) {
  29. return NULL;
  30. } else {
  31. head = head->left;
  32. }
  33. } else {
  34. if (head->right == NULL || head->right > (TreeAlloc*) address) {
  35. return head;
  36. } else {
  37. head = head->right;
  38. }
  39. }
  40. }
  41. }
  42. static uintptr_t effective_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  43. return head->size - (align_after(head + padding, align) - (void*) head);
  44. }
  45. /*
  46. * This is the most optimistic estimate of size that we can use which also preserves the ordering over
  47. * the tree. I had planned to use effective_size before I realized that it would break the tree
  48. * ordering.
  49. */
  50. static uintptr_t pessimistic_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  51. return head->size - padding - align + 1;
  52. }
  53. TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size) {
  54. TreeAlloc *head = root;
  55. while (1) {
  56. uintptr_t esize = pessimistic_size(head, padding, align);
  57. if (esize < size) {
  58. if (head->right == NULL) {
  59. return NULL;
  60. } else {
  61. head = head->right;
  62. }
  63. } else {
  64. if (head->left == NULL || pessimistic_size(head->left, padding, align) < size) {
  65. return head;
  66. } else {
  67. head = head->left;
  68. }
  69. }
  70. }
  71. }
  72. TreeAlloc *get_sibling(TreeAlloc *ta) {
  73. TreeAlloc *p = ta->parent;
  74. if (!p)
  75. return NULL;
  76. else if (p->left == ta)
  77. return p->right;
  78. else
  79. return p->left;
  80. }
  81. void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta) {
  82. TreeAlloc *parent, *tmp;
  83. parent = ta->parent;
  84. tmp = ta->right;
  85. if (!tmp) return;
  86. ta->right = tmp->left;
  87. tmp->left = ta;
  88. ta->parent = tmp;
  89. if (ta->right) ta->right->parent = ta;
  90. if (parent == NULL) {
  91. *root_ptr = tmp;
  92. } else {
  93. if (ta == parent->left)
  94. parent->left = tmp;
  95. else
  96. parent->right = tmp;
  97. }
  98. tmp->parent = parent;
  99. }
  100. void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
  101. TreeAlloc *parent, *tmp;
  102. parent = ta->parent;
  103. tmp = ta->left;
  104. if (!tmp) return;
  105. ta->left = tmp->right;
  106. tmp->right = ta;
  107. ta->parent = tmp;
  108. if (ta->left) ta->left->parent = ta;
  109. if (parent == NULL) {
  110. *root_ptr = tmp;
  111. } else {
  112. if (ta == parent->left)
  113. parent->left = tmp;
  114. else
  115. parent->right = tmp;
  116. }
  117. tmp->parent = parent;
  118. }
  119. void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
  120. TreeAlloc *parent = ta->parent;
  121. if (parent == NULL) {
  122. ta->color = COLOR_BLACK;
  123. return;
  124. }
  125. TreeAlloc *grandparent = parent->parent;
  126. TreeAlloc *uncle = get_sibling(parent);
  127. if (parent->color == COLOR_RED) {
  128. if (uncle != NULL && uncle->color == COLOR_RED) {
  129. parent->color = COLOR_BLACK;
  130. uncle->color = COLOR_BLACK;
  131. grandparent->color = COLOR_RED;
  132. repair_tree_after_insert(root_ptr, grandparent);
  133. } else {
  134. if (ta == parent->left && parent == grandparent->left) {
  135. rotate_left(root_ptr, parent);
  136. ta = ta->left;
  137. } else {
  138. rotate_right(root_ptr, parent);
  139. ta = ta->right;
  140. }
  141. parent = ta->parent;
  142. grandparent = parent->parent;
  143. if (ta == parent->left) {
  144. rotate_right(root_ptr, grandparent);
  145. } else {
  146. rotate_left(root_ptr, grandparent);
  147. }
  148. parent->color = COLOR_BLACK;
  149. grandparent->color = COLOR_RED;
  150. }
  151. }
  152. }
  153. void replace_node(TreeAlloc **root_ptr, TreeAlloc *node, TreeAlloc *replace) {
  154. if (!node->parent) {
  155. *root_ptr = replace;
  156. } else {
  157. if (node == node->parent->left)
  158. node->parent->left = replace;
  159. else
  160. node->parent->right = replace;
  161. }
  162. if (replace) replace->parent = node->parent;
  163. }
  164. void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
  165. if (node->color == COLOR_RED) {
  166. node->color = COLOR_BLACK;
  167. } else {
  168. TreeAlloc *sibling = get_sibling(node);
  169. if (sibling->color == COLOR_RED) {
  170. if (node->parent->left == node)
  171. rotate_left(root_ptr, node->parent);
  172. else
  173. rotate_right(root_ptr, node->parent);
  174. node->parent->parent->color = node->parent->color = COLOR_BLACK;
  175. }
  176. if (sibling->left->color == COLOR_BLACK && sibling->right->color == COLOR_BLACK) {
  177. node->color = COLOR_BLACK;
  178. sibling->color = COLOR_RED;
  179. repair_after_remove(root_ptr, node->parent);
  180. } else {
  181. if (node->parent->left == node && sibling->right->color == COLOR_BLACK) {
  182. rotate_right(root_ptr, sibling);
  183. sibling = get_sibling(node);
  184. sibling->color = COLOR_RED;
  185. sibling->right->color = COLOR_RED;
  186. rotate_left(root_ptr, node->parent);
  187. node->color = get_sibling(node->parent)->color = COLOR_BLACK;
  188. } else if (node->parent->right == node && sibling->left->color == COLOR_BLACK) {
  189. rotate_left(root_ptr, sibling);
  190. sibling = get_sibling(node);
  191. sibling->color = COLOR_RED;
  192. sibling->left->color = COLOR_RED;
  193. rotate_right(root_ptr, sibling);
  194. node->color = get_sibling(node->parent)->color = COLOR_BLACK;
  195. }
  196. node->parent->color ^= node->parent->parent->color;
  197. node->parent->parent->color ^= node->parent->color;
  198. node->parent->color ^= node->parent->parent->color;
  199. }
  200. }
  201. }
  202. void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
  203. char do_repair = 0;
  204. TreeAlloc *replace;
  205. TreeAlloc *parent = node->parent;
  206. if (!node->left) {
  207. replace = node->right;
  208. do_repair = node->color == COLOR_BLACK;
  209. replace_node(root_ptr, node, replace);
  210. } else if (!node->right) {
  211. replace = node->left;
  212. do_repair = node->color == COLOR_BLACK;
  213. replace_node(root_ptr, node, replace);
  214. } else {
  215. TreeAlloc *tmp = node->right;
  216. while (tmp->left) tmp = tmp->left;
  217. replace = tmp->right;
  218. do_repair = tmp->color == COLOR_BLACK;
  219. if (tmp != node->right) {
  220. replace_node(root_ptr, tmp, replace);
  221. tmp->right = node->right;
  222. node->right->parent = tmp;
  223. }
  224. replace_node(root_ptr, node, tmp);
  225. tmp->color = node->color;
  226. tmp->left = node->left;
  227. node->left->parent = tmp;
  228. }
  229. if (do_repair && replace) {
  230. repair_after_remove(root_ptr, replace);
  231. }
  232. }
  233. // Inserts a node into an empty tree.
  234. void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
  235. *root_ptr = to_insert;
  236. to_insert->parent = NULL;
  237. repair_tree_after_insert(root_ptr, to_insert);
  238. }
  239. void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
  240. if (after->right != NULL) {
  241. after->right->parent = to_insert;
  242. to_insert->right = after->right;
  243. }
  244. after->right = to_insert;
  245. to_insert->parent = after;
  246. repair_tree_after_insert(root_ptr, to_insert);
  247. }
  248. void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
  249. if (before->left != NULL) {
  250. before->left->parent = to_insert;
  251. to_insert->left = before->left;
  252. }
  253. before->left = to_insert;
  254. to_insert->parent = before;
  255. repair_tree_after_insert(root_ptr, to_insert);
  256. }
  257. int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {
  258. uintptr_t realsize = size + align + alignof(WatermarkAlloc) + padding - 1;
  259. if (realsize < MIN_NEW_MEM_SIZE) {
  260. realsize = MIN_NEW_MEM_SIZE;
  261. }
  262. FreeSpace *reg = (FreeSpace*) arena->get_new_region(realsize);
  263. if (reg == NULL) {
  264. arena->error("can't allocate a new memory region!");
  265. return 0;
  266. }
  267. FreeSpace *newreg = align_after(reg, alignof(WatermarkAlloc));
  268. newreg->left = NULL;
  269. newreg->right = NULL;
  270. realsize -= (void*) newreg - (void*) reg;
  271. realsize -= realsize % alignof(WatermarkAlloc);
  272. newreg->size = realsize;
  273. if (arena->root_freespace == NULL) {
  274. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg);
  275. } else {
  276. FreeSpace *head = arena->root_freespace;
  277. while (head->right != NULL) {
  278. head = head->right;
  279. }
  280. insert_right((TreeAlloc**) arena->root_freespace, (TreeAlloc*) newreg, (TreeAlloc*) newreg);
  281. }
  282. return 1;
  283. }
  284. void unalloc(Arena *arena, void *addr) {
  285. #ifdef DEBUG
  286. printf("==== UNALLOCATING ====\n");
  287. printf("=== FREESPACE TREE ===\n");
  288. debug_print_tree(0, arena->root_freespace);
  289. printf("=== TREEALLOC TREE ===\n");
  290. debug_print_tree(0, arena->root_treealloc);
  291. printf("=== END OF TREES ====\n");
  292. #endif
  293. if (arena->root_treealloc == NULL) {
  294. arena->error("attempt to unallocate when there are no allocations!");
  295. return;
  296. }
  297. // Find the node this address belongs to
  298. TreeAlloc *node = search_by_address(arena->root_treealloc, addr);
  299. if (node == NULL) {
  300. arena->error("attempt to free memory outside any allocations!");
  301. return;
  302. }
  303. // Handle the watermark allocator in this region
  304. if (node->type == RT_WATERMARK) {
  305. // TODO: handle watermark deallocation
  306. return;
  307. }
  308. // Get rid of it
  309. remove_node(&arena->root_treealloc, node);
  310. // If there's free space on either side of it, merge it with the free space into a bigger chunk of
  311. // free space.
  312. uintptr_t size = node->size;
  313. FreeSpace *start = (FreeSpace*) node;
  314. if (node->before != NULL && node->before->type == RT_FREESPACE) {
  315. start = (FreeSpace*) node->before;
  316. size += node->before->size;
  317. remove_node((TreeAlloc**) &arena->root_freespace, node->before);
  318. }
  319. if (node->after != NULL && node->after->type == RT_FREESPACE) {
  320. size += node->after->size;
  321. remove_node((TreeAlloc**) &arena->root_freespace, node->after);
  322. }
  323. start->type = RT_FREESPACE;
  324. start->size = size;
  325. // And finally, insert the resulting free space.
  326. if (arena->root_freespace == NULL) {
  327. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start);
  328. } else {
  329. TreeAlloc *insert_point = search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, size);
  330. if (insert_point == NULL) {
  331. TreeAlloc *head = (TreeAlloc*) arena->root_freespace;
  332. while (head->right != NULL) {
  333. head = head->right;
  334. }
  335. insert_right((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, head);
  336. } else {
  337. insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, insert_point);
  338. }
  339. }
  340. }
  341. void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
  342. uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
  343. #ifdef DEBUG
  344. printf("==== ALLOCATING =====\n");
  345. printf("=== FREESPACE TREE ===\n");
  346. debug_print_tree(0, arena->root_freespace);
  347. printf("=== TREEALLOC TREE ===\n");
  348. debug_print_tree(0, arena->root_treealloc);
  349. printf("=== END OF TREES ====\n");
  350. #endif
  351. if (arena->root_freespace == NULL) {
  352. // Handle being out of freespace.
  353. if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
  354. return NULL;
  355. }
  356. return alloc(arena, size, align);
  357. } else {
  358. TreeAlloc *region = search_by_size((TreeAlloc*) arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
  359. if (region == NULL) {
  360. // Handle insufficient freespace or fragmentation.
  361. if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
  362. return NULL;
  363. }
  364. return alloc(arena, size, align);
  365. }
  366. remove_node((TreeAlloc**) &arena->root_freespace, region);
  367. void *true_end = align_after(align_after(((void*) region) + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
  368. // The size of the new allocation (adjusted for region header and alignment
  369. uintptr_t new_size = true_end - (void*) region;
  370. // The size of the free space region following the new allocation
  371. uintptr_t new_free_size = region->size - new_size;
  372. region->right = NULL;
  373. region->left = NULL;
  374. region->type = RT_TREE_NODE;
  375. #ifdef DEBUG
  376. printf("sizeof(TreeAlloc): %lu\n", (uintptr_t) sizeof(TreeAlloc));
  377. printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
  378. printf("size: %lu -> %lu\n", size, new_size);
  379. #endif
  380. if (arena->root_treealloc == NULL) {
  381. insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
  382. } else {
  383. TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
  384. insert_right(&arena->root_treealloc, region, insert_point);
  385. }
  386. if (new_free_size >= sizeof(FreeSpace)) {
  387. // If there's enough free space after the allocation, use it!
  388. region->size = new_size; // Safe because the allocated region tree is not sorted by size.
  389. FreeSpace *new_free = (FreeSpace*) ((void*) region + new_size);
  390. new_free->left = NULL;
  391. new_free->right = NULL;
  392. new_free->type = RT_FREESPACE;
  393. new_free->size = new_free_size;
  394. if (arena->root_freespace == NULL) {
  395. insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free);
  396. } else {
  397. FreeSpace *insert_point = (FreeSpace*) search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, new_free_size);
  398. insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free, (TreeAlloc*) insert_point);
  399. }
  400. }
  401. return align_after(region + sizeof(TreeAlloc), actual_align);
  402. }
  403. }
  404. void *alloc_growable(Arena *arena, uintptr_t size, uintptr_t align) {
  405. // TODO: Basically the same as above, but put the allocated region in the center of the largest free
  406. // space. Due to alignment and whatnot, the code will be gory.
  407. return NULL;
  408. }