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

#include <stddef.h>
#include "allocator_internal.h"
TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
return NULL;
}
// Search for the node whose allocated region contains an address.
TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
TreeAlloc *head = root;
while (1) {
if (head > (TreeAlloc*) address) {
if (head->left == NULL) {
return NULL;
} else {
head = head->left;
}
} else {
if (head->right == NULL || head->right > (TreeAlloc*) address) {
return head;
} else {
head = head->right;
}
}
}
}
static uintptr_t effective_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
return head->size - (align_after(head + padding, align) - (void*) head);
}
// This is the most optimistic estimate of size that we can use which also preserves the ordering over
// the tree. I had planned to use effective_size before I realized that it would break the tree
// ordering.
static uintptr_t pessimistic_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
return head->size - padding - align + 1;
}
TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size) {
TreeAlloc *head = root;
while (1) {
uintptr_t esize = pessimistic_size(head, padding, align);
if (esize < size) {
if (head->right == NULL) {
return NULL;
} else {
head = head->right;
}
} else {
if (head->left == NULL || pessimistic_size(head->left, padding, align) < size) {
return head;
} else {
head = head->left;
}
}
}
}
// TODO: Rewrite for self-balancing tree.
void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
TreeAlloc *replace = NULL;
if (node->left == NULL) {
replace = node->right;
} else if (node->right != NULL) {
replace = node->right;
TreeAlloc *head = node->left;
while (head->right != NULL) {
head = head->right;
}
head->right = head->parent->right;
head->right->parent = head;
}
if (node->parent == NULL) {
replace->parent = NULL;
*root_ptr = replace;
} else {
if (node == node->parent->left) {
node->parent->left = replace;
} else {
node->parent->right = replace;
}
}
}
// TODO: Rewrite for self-balancing tree.
void insert_after(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
if (after->right != NULL) {
after->right->parent = to_insert;
to_insert->right = after->right;
}
after->right = to_insert;
to_insert->parent = after;
}
// TODO: Rewrite for self-balancing tree.
void insert_before(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
if (before->left != NULL) {
before->left->parent = to_insert;
to_insert->left = before->left;
}
before->left = to_insert;
to_insert->parent = before;
}