Browse Source

Added util.c, fixed a couple of problems

master
Thomas Johnson 1 year ago
parent
commit
6e71e179a6
  1. 5
      Makefile
  2. 16
      allocator_internal.h
  3. 3
      tree_alloc.c
  4. 13
      util.c

5
Makefile

@ -21,5 +21,8 @@ clean:
main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o
tree_alloc.o: tree_alloc.c allocator_interal.h
tree_alloc.o: tree_alloc.c allocator_internal.h
$(CC) $(CFLAGS) -c tree_alloc.c -o tree_alloc.o
util.o: util.c
$(CC) $(CFLAGS) -c util.c

16
allocator_internal.h

@ -2,7 +2,6 @@
#define ALLOCATOR_INTERNAL_H
#include <stdint.h>
#include <sys/types.h>
// No enum because these need to be 1 byte
const char RT_FREESPACE = 0;
@ -12,7 +11,7 @@ const char RT_WATERMARK = 2;
struct TopLevel {
struct FreeSpace *root_freespace;
struct TreeAlloc *root_treealloc;
}
};
// All three of these types should be memory-compatible with each other
@ -21,27 +20,28 @@ typedef struct TreeAlloc {
struct TreeAlloc *parent;
struct TreeAlloc *left;
struct TreeAlloc *right;
size_t size;
} TreeAlloc
uintptr_t size;
} TreeAlloc;
typedef struct FreeSpace {
char type; // Should be RT_FREESPACE
struct FreeSpace *parent;
struct FreeSpace *left;
struct FreeSpace *right;
size_t size;
} FreeSpace
uintptr_t size;
} FreeSpace;
typedef struct WatermarkAlloc {
char type; // Should be RT_WATERMARK
struct TreeAlloc *parent;
struct TreeAlloc *left;
struct TreeAlloc *right;
size_t size;
uintptr_t size;
int num_allocs;
void *next_alloc;
} WatermarkAlloc
} WatermarkAlloc;
void* align_after(void* address, int align);
TreeAlloc *insert_node_at(void *address, int padding, int align, int size);
TreeAlloc *search_by_address(TreeAlloc *root, void *address);
TreeAlloc *search_by_size(TreeAlloc *root, int padding, int align, int size);

3
tree_alloc.c

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

13
util.c

@ -0,0 +1,13 @@
#include <stdint.h>
void* align_after(void* address, int align) {
uintptr_t addr = (uintptr_t) address;
uintptr_t offset = addr % align;
if offset == 0 {
return address;
} else {
return (void*) (addr + align - align % offset);
}
}
Loading…
Cancel
Save