Removed sbrk and brk and only used the brk system call.

This commit is contained in:
Arron David Nelson 2024-02-11 00:25:42 -08:00
parent 7ebc328b5a
commit af512c99b6

13
main.c
View File

@ -1,10 +1,11 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/syscall.h>
#define ALIGN4(s) (((((s)-1)>>2)<<2)+4) #define ALIGN4(s) (((((s)-1)>>2)<<2)+4)
#define BLOCK_SIZE sizeof(struct block) #define BLOCK_SIZE sizeof(struct block)
#define MINIMUM_BLOCK_SIZE 16 #define MINIMUM_BLOCK_SIZE 4
/// The memory block's header. /// The memory block's header.
struct block struct block
@ -27,9 +28,9 @@ struct block* extend_heap(size_t s)
if (s < MINIMUM_BLOCK_SIZE) if (s < MINIMUM_BLOCK_SIZE)
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; return NULL;
b->size = s; b->size = s;
@ -150,7 +151,7 @@ void free(void* ptr)
} }
if (!b->next) if (!b->next)
brk(b); syscall(SYS_brk, b);
} }
/// Retrieves the total heap usage in bytes. /// Retrieves the total heap usage in bytes.
@ -165,8 +166,8 @@ size_t get_heap_size()
int main() int main()
{ {
int* a = (int*)malloc(sizeof(int)); int* a = (int*)malloc(1);
int* b = (int*)malloc(sizeof(int)); int* b = (int*)malloc(1);
*a = 5; *a = 5;
*b = 12; *b = 12;