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.

108 lines
2.9 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. // TODO: Rewrite for self-balancing tree.
  79. void insert_after(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
  80. if (after->right != NULL) {
  81. after->right->parent = to_insert;
  82. to_insert->right = after->right;
  83. }
  84. after->right = to_insert;
  85. to_insert->parent = after;
  86. }
  87. // TODO: Rewrite for self-balancing tree.
  88. void insert_before(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
  89. if (before->left != NULL) {
  90. before->left->parent = to_insert;
  91. to_insert->left = before->left;
  92. }
  93. before->left = to_insert;
  94. to_insert->parent = before;
  95. }