From af512c99b6e5cff5052e7b94704d5ac1ef7e0013 Mon Sep 17 00:00:00 2001 From: karutoh Date: Sun, 11 Feb 2024 00:25:42 -0800 Subject: [PATCH] Removed sbrk and brk and only used the brk system call. --- main.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index ccfe0b0..9207e66 100644 --- a/main.c +++ b/main.c @@ -1,10 +1,11 @@ #include #include #include +#include #define ALIGN4(s) (((((s)-1)>>2)<<2)+4) #define BLOCK_SIZE sizeof(struct block) -#define MINIMUM_BLOCK_SIZE 16 +#define MINIMUM_BLOCK_SIZE 4 /// The memory block's header. struct block @@ -27,9 +28,9 @@ struct block* extend_heap(size_t s) if (s < MINIMUM_BLOCK_SIZE) s = MINIMUM_BLOCK_SIZE; - struct block* b = sbrk(0); + struct block* b = (struct block*)syscall(SYS_brk, NULL); - if (sbrk(BLOCK_SIZE + s) == (void*)-1) + if ((void*)syscall(SYS_brk, (char*)(b + 1) + s) == (void*)-1) return NULL; b->size = s; @@ -150,7 +151,7 @@ void free(void* ptr) } if (!b->next) - brk(b); + syscall(SYS_brk, b); } /// Retrieves the total heap usage in bytes. @@ -165,8 +166,8 @@ size_t get_heap_size() int main() { - int* a = (int*)malloc(sizeof(int)); - int* b = (int*)malloc(sizeof(int)); + int* a = (int*)malloc(1); + int* b = (int*)malloc(1); *a = 5; *b = 12;