From 103277bf638a6d4265b8c074e64dabc3e9410ad5 Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 8 Feb 2024 17:47:44 -0800 Subject: [PATCH 1/5] Added ralloc function. --- main.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.c b/main.c index 9510dc7..f88eb07 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #define ALIGN4(s) (((((s)-1)>>2)<<2)+4) #define BLOCK_SIZE sizeof(struct block) +#define MINIMUM_BLOCK_SIZE 16 /// The memory block's header. struct block @@ -22,6 +23,10 @@ struct block* last = NULL; /// @returns The new memory block. struct block* extend_heap(size_t s) { + // Ensure the allocated size is at least the minimum block size + if (s < MINIMUM_BLOCK_SIZE) + s = MINIMUM_BLOCK_SIZE; + struct block* b = sbrk(0); if (sbrk(BLOCK_SIZE + s) == (void*)-1) From 9594b76202881f893d2c5ecda7f2075ae6fdb06e Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 8 Feb 2024 17:51:03 -0800 Subject: [PATCH 2/5] Added ralloc function. --- .idea/vcs.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..8d3e42f 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,5 +1,10 @@ + + + From 0e2633db9dd97b0ea02c60de2163d433947cacdf Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 8 Feb 2024 18:50:58 -0800 Subject: [PATCH 3/5] Added get_heap_size function. --- main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/main.c b/main.c index f88eb07..dc86001 100644 --- a/main.c +++ b/main.c @@ -153,6 +153,13 @@ void free(void* ptr) brk(b); } +/// Retrieves the total heap usage in bytes. +/// @returns Usage in bytes. +size_t get_heap_size() +{ + return ((char*)last + last->size) - (char*)first; +} + int main() { int* a = (int*)malloc(sizeof(int)); @@ -163,6 +170,7 @@ int main() printf("Test 1: %i\n", *a); printf("Test 2: %i\n", *b); + printf("Heap Size: %zu Bytes\n", get_heap_size()); free(a); free(b); From 7ebc328b5ac9158dc52cc7809d4c19d2d9a33594 Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 8 Feb 2024 18:51:44 -0800 Subject: [PATCH 4/5] fixed get_heap_size function by adding null pointer checks. --- main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.c b/main.c index dc86001..ccfe0b0 100644 --- a/main.c +++ b/main.c @@ -157,6 +157,9 @@ void free(void* ptr) /// @returns Usage in bytes. size_t get_heap_size() { + if (!first || !last) + return 0; + return ((char*)last + last->size) - (char*)first; } From af512c99b6e5cff5052e7b94704d5ac1ef7e0013 Mon Sep 17 00:00:00 2001 From: karutoh Date: Sun, 11 Feb 2024 00:25:42 -0800 Subject: [PATCH 5/5] 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;