Browse Source

Added util.c, fixed a couple of problems

Thomas Johnson 1 year ago
parent
commit
6e71e179a6
4 changed files with 28 additions and 9 deletions
  1. 4
    1
      Makefile
  2. 8
    8
      allocator_internal.h
  3. 3
    0
      tree_alloc.c
  4. 13
    0
      util.c

+ 4
- 1
Makefile View File

@@ -21,5 +21,8 @@ clean:
21 21
 main.o: main.c
22 22
 	$(CC) $(CFLAGS) -c main.c -o main.o
23 23
 
24
-tree_alloc.o: tree_alloc.c allocator_interal.h
24
+tree_alloc.o: tree_alloc.c allocator_internal.h
25 25
 	$(CC) $(CFLAGS) -c tree_alloc.c -o tree_alloc.o
26
+
27
+util.o: util.c
28
+	$(CC) $(CFLAGS) -c util.c

+ 8
- 8
allocator_internal.h View File

@@ -2,7 +2,6 @@
2 2
 #define ALLOCATOR_INTERNAL_H
3 3
 
4 4
 #include <stdint.h>
5
-#include <sys/types.h>
6 5
 
7 6
 // No enum because these need to be 1 byte
8 7
 const char RT_FREESPACE = 0;
@@ -12,7 +11,7 @@ const char RT_WATERMARK = 2;
12 11
 struct TopLevel {
13 12
   struct FreeSpace *root_freespace;
14 13
   struct TreeAlloc *root_treealloc;
15
-}
14
+};
16 15
 
17 16
 // All three of these types should be memory-compatible with each other
18 17
 
@@ -21,27 +20,28 @@ typedef struct TreeAlloc {
21 20
   struct TreeAlloc *parent;
22 21
   struct TreeAlloc *left;
23 22
   struct TreeAlloc *right;
24
-  size_t size;
25
-} TreeAlloc
23
+  uintptr_t size;
24
+} TreeAlloc;
26 25
 
27 26
 typedef struct FreeSpace {
28 27
   char type;  // Should be RT_FREESPACE
29 28
   struct FreeSpace *parent;
30 29
   struct FreeSpace *left;
31 30
   struct FreeSpace *right;
32
-  size_t size;
33
-} FreeSpace
31
+  uintptr_t size;
32
+} FreeSpace;
34 33
 
35 34
 typedef struct WatermarkAlloc {
36 35
   char type;  // Should be RT_WATERMARK
37 36
   struct TreeAlloc *parent;
38 37
   struct TreeAlloc *left;
39 38
   struct TreeAlloc *right;
40
-  size_t size;
39
+  uintptr_t size;
41 40
   int num_allocs;
42 41
   void *next_alloc;
43
-} WatermarkAlloc
42
+} WatermarkAlloc;
44 43
 
44
+void* align_after(void* address, int align);
45 45
 TreeAlloc *insert_node_at(void *address, int padding, int align, int size);
46 46
 TreeAlloc *search_by_address(TreeAlloc *root, void *address);
47 47
 TreeAlloc *search_by_size(TreeAlloc *root, int padding, int align, int size);

+ 3
- 0
tree_alloc.c View File

@@ -1,3 +1,6 @@
1
+
2
+#include <stddef.h> 
3
+
1 4
 #include "allocator_internal.h"
2 5
 
3 6
 TreeAlloc *insert_node_at(void *address, int padding, int align, int size) {

+ 13
- 0
util.c View File

@@ -0,0 +1,13 @@
1
+
2
+#include <stdint.h>
3
+
4
+void* align_after(void* address, int align) {
5
+  uintptr_t addr = (uintptr_t) address;
6
+  uintptr_t offset = addr % align;
7
+  if offset == 0 {
8
+    return address;
9
+  } else {
10
+    return (void*) (addr + align - align % offset);
11
+  }
12
+}
13
+

Loading…
Cancel
Save