diff --git a/include/gc_alloc.h b/include/gc_alloc.h index 44858df..01634e4 100644 --- a/include/gc_alloc.h +++ b/include/gc_alloc.h @@ -3,24 +3,17 @@ #include -// Metadata for allocated memory - Headers are stored in a linked list to keep -// track of alloc's and free's -struct gcHeader { +// Metadata for allocated memory +struct gc_block_header { size_t size; int free; - struct gcHeader *next; + struct gc_block_header *next; }; -// Iterates over the galloc linked list attempting to find free space -struct gcHeader *findFree(struct gcHeader **prev, size_t size); +// Performs allocations of SIZE bytes +void *gc_malloc(size_t size); -// Requests more memory from the kernel -struct gcHeader *requestMem(struct gcHeader *prev, size_t size); - -// Performs allocations -void *galloc(size_t size); - -// Frees memory allocated with galloc() -void gfree(void *ptr); +// Frees blocks of memory allocated by 'gc_alloc' +void gc_free(void *ptr); #endif /* GC_ALLOC_H */ diff --git a/src/gc_alloc.c b/src/gc_alloc.c index 640ceb4..42689e7 100644 --- a/src/gc_alloc.c +++ b/src/gc_alloc.c @@ -1,51 +1,2 @@ -#include -#include - #include "gc_alloc.h" -static void *allocHead = NULL; - -struct gcHeader *findFree(struct gcHeader **prev, size_t size) -{ - struct gcHeader *curr = allocHead; - - while (curr && !(curr->free && curr->size >= size)) { - *prev = curr; - curr = curr->next; - } - - return curr; -} - -struct gcHeader *requestMem(struct gcHeader *prev, size_t size) -{ - struct gcHeader *block; - void *request; - - block = sbrk(0); - if (block == (void *)-1) { - perror("sbrk block"); - return NULL; - } - - request = sbrk(size + sizeof(struct gcHeader)); - if (request == (void *)-1) { - perror("sbrk request"); - return NULL; - } - - if ((void *)block != request) { - fprintf(stderr, "gcHeader: block != request"); - return NULL; - } - - if (prev) { - prev->next = block; - } - - block->size = size; - block->next = NULL; - block->free = 0; - - return block; -}