marCsweep/gc_alloc.c

49 lines
887 B
C

#include <stdio.h>
#include <unistd.h>
#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;
}