Compare commits

...

6 Commits

Author SHA1 Message Date
Cameron Weinfurt c3c6ead55d Handled merge conflicts 2 years ago
Cameron Weinfurt ba4a82fbf2 Tied tree allocator into waterfall allocator 2 years ago
Cameron Weinfurt e7e90fe2bf Finished doing the rest of the merge 2 years ago
Cameron Weinfurt ee9ad41808 Pulled in tree_alloc branch 2 years ago
Cameron Weinfurt 4eaf9d6ef2 Implemented some functionality for WM allocs 2 years ago
Cameron Weinfurt bd82791771 Init watermark_alloc branch 2 years ago
  1. 5
      Makefile
  2. 3
      alloc_api.h
  3. 17
      allocator_internal.h
  4. 1
      main.c
  5. 46
      watermark_alloc.c

5
Makefile

@ -1,4 +1,4 @@
objs = main.o tree_alloc.o util.o
objs = main.o tree_alloc.o util.o watermark_alloc.o
CC = clang
COMMON_FLAGS = -std=gnu11
@ -22,6 +22,9 @@ clean:
main.o: main.c alloc_api.h
$(CC) $(CFLAGS) -c main.c -o main.o
watermark_alloc.o: watermark_alloc.c allocator_internal.h
$(CC) $(CFLAGS) -c watermark_alloc.c -o watermark_alloc.o
tree_alloc.o: tree_alloc.c allocator_internal.h
$(CC) $(CFLAGS) -c tree_alloc.c -o tree_alloc.o

3
alloc_api.h

@ -4,10 +4,11 @@
struct Arena {
void *root_freespace;
void *root_treealloc;
void *head_watermark;
void *(*get_new_region)(uintptr_t);
void (*error)(char*);
};
void unalloc(struct Arena *arena, void *addr);
void *alloc(struct Arena *arena, uintptr_t size, uintptr_t align);
void *alloc_fixed(struct Arena *arena, uintptr_t size, uintptr_t align);

17
allocator_internal.h

@ -4,18 +4,19 @@
#include <stdint.h>
// No enum because these need to be 1 byte
const char RT_FREESPACE = 0;
const char RT_TREE_NODE = 1;
const char RT_WATERMARK = 2;
#define RT_FREESPACE 0
#define RT_TREE_NODE 1
#define RT_WATERMARK 2
const char COLOR_RED = 0;
const char COLOR_BLACK = 1;
#define COLOR_RED 0
#define COLOR_BLACK 1
const uintptr_t MIN_NEW_MEM_SIZE = 4096;
#define MIN_NEW_MEM_SIZE 4096
typedef struct Arena {
struct FreeSpace *root_freespace;
struct TreeAlloc *root_treealloc;
struct WatermarkAlloc *head_watermark;
void *(*get_new_region)(uintptr_t);
void (*error)(char*);
} Arena;
@ -57,7 +58,7 @@ typedef struct WatermarkAlloc {
uintptr_t size;
struct TreeAlloc *before;
struct TreeAlloc *after;
int num_allocs;
unsigned int num_allocs;
void *next_alloc;
} WatermarkAlloc;
@ -76,5 +77,7 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after);
void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before);
void unalloc(Arena *arena, void *addr);
void *alloc(Arena *arena, uintptr_t size, uintptr_t align);
void *alloc_fixed(struct Arena *arena, uintptr_t size, uintptr_t align);
#endif

1
main.c

@ -114,6 +114,7 @@ int act(struct AllocationRecord *dummy, struct Arena *arena) {
int main() {
srand(1);
struct Arena arena = {
NULL,
NULL,
NULL,
get_new_region,

46
watermark_alloc.c

@ -0,0 +1,46 @@
#include <stddef.h>
#include "allocator_internal.h"
/*
* Attempt to allocate space within a watermark region. It will try to
* allocate more space if
*/
void *alloc_fixed(struct Arena *arena, uintptr_t size, uintptr_t align)
{
// Large allocations will never fit inside this allocator.
if (size > MIN_NEW_MEM_SIZE) return NULL;
WatermarkAlloc *wma = arena->head_watermark;
int space_left;
if (!wma) {
// No watermark allocators are currently active, so a new one
// needs to be created to act as the head.
wma = alloc(arena, MIN_NEW_MEM_SIZE + sizeof(WatermarkAlloc), align);
arena->head_watermark = wma;
wma->before = wma->after = NULL;
} else {
// Scan through the current allocators until one with enough space is
// found or the end of the list is reached. In the latter case, alloc
// a new allocator.
while(wma) {
if(wma->size <= size) {
// There is enough room for the allocation in this allocator,
// so use it.
break;
} else if (!wma->after) {
wma->after = alloc(arena, MIN_NEW_MEM_SIZE + sizeof(WatermarkAlloc), align);
wma->after->before = (TreeAlloc *) wma;
wma = (WatermarkAlloc *) wma->after;
break;
}
wma = (WatermarkAlloc *) wma->after;
}
}
void *allocation = wma->next_alloc;
wma->next_alloc += size;
wma->num_allocs += 1;
return allocation;
}
Loading…
Cancel
Save