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.

417 lines
13 KiB

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