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.

131 lines
3.5 KiB

  1. #include <stddef.h>
  2. #include "allocator_internal.h"
  3. TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
  4. return NULL;
  5. }
  6. // Search for the node whose allocated region contains an address.
  7. TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
  8. TreeAlloc *head = root;
  9. while (1) {
  10. if (head > (TreeAlloc*) address) {
  11. if (head->left == NULL) {
  12. return NULL;
  13. } else {
  14. head = head->left;
  15. }
  16. } else {
  17. if (head->right == NULL || head->right > (TreeAlloc*) address) {
  18. return head;
  19. } else {
  20. head = head->right;
  21. }
  22. }
  23. }
  24. }
  25. static uintptr_t effective_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  26. return head->size - (align_after(head + padding, align) - (void*) head);
  27. }
  28. // This is the most optimistic estimate of size that we can use which also preserves the ordering over
  29. // the tree. I had planned to use effective_size before I realized that it would break the tree
  30. // ordering.
  31. static uintptr_t pessimistic_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
  32. return head->size - padding - align + 1;
  33. }
  34. TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size) {
  35. TreeAlloc *head = root;
  36. while (1) {
  37. uintptr_t esize = pessimistic_size(head, padding, align);
  38. if (esize < size) {
  39. if (head->right == NULL) {
  40. return NULL;
  41. } else {
  42. head = head->right;
  43. }
  44. } else {
  45. if (head->left == NULL || pessimistic_size(head->left, padding, align) < size) {
  46. return head;
  47. } else {
  48. head = head->left;
  49. }
  50. }
  51. }
  52. }
  53. // TODO: Rewrite for self-balancing tree.
  54. void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
  55. TreeAlloc *replace = NULL;
  56. if (node->left == NULL) {
  57. replace = node->right;
  58. } else if (node->right != NULL) {
  59. replace = node->right;
  60. TreeAlloc *head = node->left;
  61. while (head->right != NULL) {
  62. head = head->right;
  63. }
  64. head->right = head->parent->right;
  65. head->right->parent = head;
  66. }
  67. if (node->parent == NULL) {
  68. replace->parent = NULL;
  69. *root_ptr = replace;
  70. } else {
  71. if (node == node->parent->left) {
  72. node->parent->left = replace;
  73. } else {
  74. node->parent->right = replace;
  75. }
  76. }
  77. }
  78. // Inserts a node into an empty tree.
  79. // TODO: Rewrite for self-balancing tree.
  80. void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
  81. *root_ptr = to_insert;
  82. to_insert->parent = NULL;
  83. }
  84. // TODO: Rewrite for self-balancing tree.
  85. void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
  86. if (after->right != NULL) {
  87. after->right->parent = to_insert;
  88. to_insert->right = after->right;
  89. }
  90. after->right = to_insert;
  91. to_insert->parent = after;
  92. }
  93. // TODO: Rewrite for self-balancing tree.
  94. void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
  95. if (before->left != NULL) {
  96. before->left->parent = to_insert;
  97. to_insert->left = before->left;
  98. }
  99. before->left = to_insert;
  100. to_insert->parent = before;
  101. }
  102. void *unalloc(Arena *arena, void *addr) {
  103. if (arena->root_treealloc == NULL) {
  104. arena->error("attempt to unallocate when there are no allocations!");
  105. }
  106. TreeAlloc *node = search_by_address(arena->root_treealloc, addr);
  107. // TODO
  108. }
  109. void *alloc(Arena arena, uintptr_t size, uintptr_t align) {
  110. // TODO
  111. return NULL;
  112. }
  113. void *alloc_growable(Arena arena, uintptr_t size, uintptr_t align) {
  114. // TODO
  115. return NULL;
  116. }