|
|
@ -3,42 +3,57 @@ |
|
|
|
|
|
|
|
#include "allocator_internal.h" |
|
|
|
|
|
|
|
TreeAlloc *insert_node_at(void *address, int padding, int align, int size) { |
|
|
|
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) { |
|
|
|
while (1) { |
|
|
|
if (root < address) { |
|
|
|
if (root->left) |
|
|
|
root = root->left; |
|
|
|
else |
|
|
|
return root; |
|
|
|
} else if (root > address) { |
|
|
|
if (root->right) |
|
|
|
root = root->right; |
|
|
|
else |
|
|
|
return root; |
|
|
|
} else { |
|
|
|
return root; |
|
|
|
} |
|
|
|
} |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
TreeAlloc *search_by_size(TreeAlloc *root, int padding, int align, int size) { |
|
|
|
while (1) { |
|
|
|
if (root->size < size) { |
|
|
|
if (root->left) |
|
|
|
root = root->left; |
|
|
|
else |
|
|
|
return root; |
|
|
|
} else if (root->size > address) { |
|
|
|
if (root->right) |
|
|
|
root = root->right; |
|
|
|
else |
|
|
|
return root; |
|
|
|
} else { |
|
|
|
return root; |
|
|
|
} |
|
|
|
} |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |