From 6a5543bb8f1f535ca49737bcd7be0cafca3b2a08 Mon Sep 17 00:00:00 2001 From: hachem Date: Mon, 12 Oct 2026 23:08:48 +0200 Subject: feat: lib sea --- user/membrk.c | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) (limited to 'user/membrk.c') diff --git a/user/membrk.c b/user/membrk.c index f1949cc..7d73adf 100644 --- a/user/membrk.c +++ b/user/membrk.c @@ -1,31 +1,26 @@ -#include "user.h" +#include +#include +#include static void check(const char* name, int ok) { - put(ok ? "ok " : "FAIL "); - put(name); - put("\n"); + printf("%s%s\n", ok ? "ok " : "FAIL ", name); } -void _start(const char* arg, unsigned long arglen) +int main(void) { - (void)arg; - (void)arglen; - - long start = (long)sys_sbrk(0); - put("heap base: "); - put_int(start); - put("\n"); + long start = (long)sbrk(0); + printf("heap base: %ld\n", start); // obese alloc test char* run[80]; for (int i = 0; i < 80; i++) - run[i] = (char*)malloc(4000); - long run_after = (long)sys_sbrk(0); + run[i] = malloc(4000); + long run_after = (long)sbrk(0); for (int i = 0; i < 80; i++) free(run[i]); - char* merged = (char*)malloc(80 * 4000 - 8000); - long merge_after = (long)sys_sbrk(0); + char* merged = malloc(80 * 4000 - 8000); + long merge_after = (long)sbrk(0); check("freed neighbours coalesce", merged != 0 && merge_after <= run_after); free(merged); @@ -33,7 +28,7 @@ void _start(const char* arg, unsigned long arglen) char* blocks[16]; for (int i = 0; i < 16; i++) { - blocks[i] = (char*)malloc(64); + blocks[i] = malloc(64); for (int j = 0; j < 64; j++) blocks[i][j] = (char)(i * 7 + j); } @@ -46,7 +41,7 @@ void _start(const char* arg, unsigned long arglen) // a big block spanning several pages written edge to edge unsigned long big = 20000; - unsigned char* buffer = (unsigned char*)malloc(big); + unsigned char* buffer = malloc(big); for (unsigned long k = 0; k < big; k++) buffer[k] = (unsigned char)(k & 0xff); int big_ok = 1; @@ -55,18 +50,18 @@ void _start(const char* arg, unsigned long arglen) big_ok = 0; check("big multi page alloc", big_ok); - // testing that the the heap must doesnt grow across the loop - long churn_before = (long)sys_sbrk(0); + // testing that the heap doesnt grow across the loop + long churn_before = (long)sbrk(0); for (int i = 0; i < 2000; i++) { void* pointer = malloc(500); free(pointer); } - long churn_after = (long)sys_sbrk(0); + long churn_after = (long)sbrk(0); check("alloc/free churn reclaims (no growth)", churn_before == churn_after); // calloc dirty test - unsigned char* zeroed = (unsigned char*)calloc(200, 1); + unsigned char* zeroed = calloc(200, 1); int zero_ok = 1; for (int i = 0; i < 200; i++) if (zeroed[i] != 0) @@ -74,20 +69,18 @@ void _start(const char* arg, unsigned long arglen) check("calloc is zeroed", zero_ok); // realloc test - char* grown = (char*)malloc(32); + char* grown = malloc(32); for (int i = 0; i < 32; i++) grown[i] = (char)(i + 1); - grown = (char*)realloc(grown, 500); + grown = realloc(grown, 500); int grow_ok = 1; for (int i = 0; i < 32; i++) if (grown[i] != (char)(i + 1)) grow_ok = 0; check("realloc grows and preserves data", grow_ok); - long end = (long)sys_sbrk(0); - put("heap high water: "); - put_int(end - start); - put(" bytes\n"); + long end = (long)sbrk(0); + printf("heap high water: %ld bytes\n", end - start); - sys_exit(0); + return 0; } -- cgit v1.3