malloc: remove sbrk

This commit is contained in:
Andrew Scott 2024-09-16 17:25:07 -04:00
parent 26ef0bc3ad
commit 15e07e2c9a
Signed by: a
GPG key ID: 7CD5A5977E4931C1
2 changed files with 7 additions and 63 deletions

View file

@ -3,24 +3,17 @@
#include <stdio.h>
// 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 */

View file

@ -1,51 +1,2 @@
#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;
}