hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/user/membrk.c
diff options
context:
space:
mode:
Diffstat (limited to 'user/membrk.c')
-rw-r--r--user/membrk.c51
1 files changed, 22 insertions, 29 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
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;
}