Browse Source

Added some tree functions

Thomas Johnson 1 year ago
parent
commit
21dbaea68a
4 changed files with 52 additions and 37 deletions
  1. 1
    1
      Makefile
  2. 3
    3
      allocator_internal.h
  3. 47
    32
      tree_alloc.c
  4. 1
    1
      util.c

+ 1
- 1
Makefile View File

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

+ 3
- 3
allocator_internal.h View File

@@ -41,9 +41,9 @@ typedef struct WatermarkAlloc {
41 41
   void *next_alloc;
42 42
 } WatermarkAlloc;
43 43
 
44
-void* align_after(void* address, int align);
45
-TreeAlloc *insert_node_at(void *address, int padding, int align, int size);
44
+void* align_after(void* address, uintptr_t align);
45
+TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size);
46 46
 TreeAlloc *search_by_address(TreeAlloc *root, void *address);
47
-TreeAlloc *search_by_size(TreeAlloc *root, int padding, int align, int size);
47
+TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size);
48 48
 
49 49
 #endif

+ 47
- 32
tree_alloc.c View File

@@ -3,42 +3,57 @@
3 3
 
4 4
 #include "allocator_internal.h"
5 5
 
6
-TreeAlloc *insert_node_at(void *address, int padding, int align, int size) {
6
+TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
7 7
 	return NULL;
8 8
 }
9 9
 
10
+// Search for the node whose allocated region contains an address.
10 11
 TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
11
-	while (1) {
12
-		if (root < address) {
13
-			if (root->left)
14
-				root = root->left;
15
-			else
16
-				return root;
17
-		} else if (root > address) {
18
-			if (root->right)
19
-				root = root->right;
20
-			else
21
-				return root;
22
-		} else {
23
-			return root;
24
-		}
25
-	}
12
+  TreeAlloc *head = root;
13
+  while (1) {
14
+    if (head > (TreeAlloc*) address) {
15
+      if (head->left == NULL) {
16
+        return NULL;
17
+      } else {
18
+        head = head->left;
19
+      }
20
+    } else {
21
+      if (head->right == NULL || head->right > (TreeAlloc*) address) {
22
+        return head;
23
+      } else {
24
+        head = head->right;
25
+      }
26
+    }
27
+  }
26 28
 }
27 29
 
28
-TreeAlloc *search_by_size(TreeAlloc *root, int padding, int align, int size) {
29
-	while (1) {
30
-		if (root->size < size) {
31
-			if (root->left)
32
-				root = root->left;
33
-			else
34
-				return root;
35
-		} else if (root->size > address) {
36
-			if (root->right)
37
-				root = root->right;
38
-			else
39
-				return root;
40
-		} else {
41
-			return root;
42
-		}
43
-	}
30
+static uintptr_t effective_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
31
+    return head->size - (align_after(head + padding, align) - (void*) head);
32
+}
33
+
34
+// This is the most optimistic estimate of size that we can use which also preserves the ordering over
35
+// the tree. I had planned to use effective_size before I realized that it would break the tree
36
+// ordering.
37
+static uintptr_t pessimistic_size(TreeAlloc *head, uintptr_t padding, uintptr_t align) {
38
+    return head->size - padding - align + 1;
39
+}
40
+
41
+TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size) {
42
+  TreeAlloc *head = root;
43
+  while (1) {
44
+    uintptr_t esize = pessimistic_size(head, padding, align);
45
+    if (esize < size) {
46
+      if (head->right == NULL) {
47
+        return NULL;
48
+      } else {
49
+        head = head->right;
50
+      }
51
+    } else {
52
+      if (head->left == NULL || pessimistic_size(head->left, padding, align) < size) {
53
+        return head;
54
+      } else {
55
+        head = head->left;
56
+      }
57
+    }
58
+  }
44 59
 }

+ 1
- 1
util.c View File

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

Loading…
Cancel
Save