Browse Source

Added some tree functions

tree_alloc
Thomas Johnson 1 year ago
parent
commit
21dbaea68a
  1. 2
      Makefile
  2. 6
      allocator_internal.h
  3. 79
      tree_alloc.c
  4. 2
      util.c

2
Makefile

@ -1,4 +1,4 @@
objs = main.o tree_alloc.o
objs = main.o tree_alloc.o util.o
CC = clang
COMMON_FLAGS = -std=c11

6
allocator_internal.h

@ -41,9 +41,9 @@ typedef struct WatermarkAlloc {
void *next_alloc;
} WatermarkAlloc;
void* align_after(void* address, int align);
TreeAlloc *insert_node_at(void *address, int padding, int align, int size);
void* align_after(void* address, uintptr_t align);
TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size);
TreeAlloc *search_by_address(TreeAlloc *root, void *address);
TreeAlloc *search_by_size(TreeAlloc *root, int padding, int align, int size);
TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size);
#endif

79
tree_alloc.c

@ -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;
}
}
}
}

2
util.c

@ -4,7 +4,7 @@
void* align_after(void* address, int align) {
uintptr_t addr = (uintptr_t) address;
uintptr_t offset = addr % align;
if offset == 0 {
if (offset == 0) {
return address;
} else {
return (void*) (addr + align - align % offset);

Loading…
Cancel
Save