mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-12-22 01:33:11 -05:00
malloc: remove sbrk
This commit is contained in:
parent
26ef0bc3ad
commit
15e07e2c9a
2 changed files with 7 additions and 63 deletions
|
@ -3,24 +3,17 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// Metadata for allocated memory - Headers are stored in a linked list to keep
|
// Metadata for allocated memory
|
||||||
// track of alloc's and free's
|
struct gc_block_header {
|
||||||
struct gcHeader {
|
|
||||||
size_t size;
|
size_t size;
|
||||||
int free;
|
int free;
|
||||||
struct gcHeader *next;
|
struct gc_block_header *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Iterates over the galloc linked list attempting to find free space
|
// Performs allocations of SIZE bytes
|
||||||
struct gcHeader *findFree(struct gcHeader **prev, size_t size);
|
void *gc_malloc(size_t size);
|
||||||
|
|
||||||
// Requests more memory from the kernel
|
// Frees blocks of memory allocated by 'gc_alloc'
|
||||||
struct gcHeader *requestMem(struct gcHeader *prev, size_t size);
|
void gc_free(void *ptr);
|
||||||
|
|
||||||
// Performs allocations
|
|
||||||
void *galloc(size_t size);
|
|
||||||
|
|
||||||
// Frees memory allocated with galloc()
|
|
||||||
void gfree(void *ptr);
|
|
||||||
|
|
||||||
#endif /* GC_ALLOC_H */
|
#endif /* GC_ALLOC_H */
|
||||||
|
|
|
@ -1,51 +1,2 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "gc_alloc.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;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue