Initial commit

This commit is contained in:
2025-10-25 19:15:56 -05:00
commit e6a978d36b
13 changed files with 459 additions and 0 deletions

14
tests/01_arena_new.c Normal file
View File

@@ -0,0 +1,14 @@
#define DEBUG
#include <stdlib.h>
#include <assert.h>
#include "../include/arena.h"
#include "../include/mem.h"
int main(void){
size_t nbytes = 20;
Arena a = ARENA(0,nbytes);
assert(a.beg != NULL);
assert( mem_is_zero(a.beg, nbytes));
free(a.beg);
return EXIT_SUCCESS;
}

14
tests/02_arena_nozero.c Normal file
View File

@@ -0,0 +1,14 @@
#define DEBUG
#include <stdlib.h>
#include <assert.h>
#include "../include/arena.h"
#include "../include/mem.h"
int main(void){
size_t nbytes = 20;
Arena a = ARENA(NOZERO,nbytes);
assert(a.beg != NULL);
assert( !mem_is_zero(a.beg, nbytes));
free(a.beg);
return EXIT_SUCCESS;
}

12
tests/03_arena_softfail.c Normal file
View File

@@ -0,0 +1,12 @@
#define DEBUG
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "../include/arena.h"
#include "../include/mem.h"
int main(void){
size_t nbytes = MEM_GB(1024);
Arena a = ARENA(SOFT_FAIL, nbytes);
assert(a.beg == NULL);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,30 @@
#define DEBUG
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include "../include/arena.h"
#include "../include/mem.h"
int main(void){
size_t nbytes = MEM_GB(1024);
Arena a;
TRY {
a = ARENA(0,nbytes); /* try 1GB */
if (!a.beg)
RAISE(OOM);
free(a.beg);
}
EXCEPT(OOM) {
/* handle memory failure gracefully */
fprintf(stderr, "Caught: %s\n", OOM.reason);
return EXIT_SUCCESS; /* requested behavior */
}
FINALLY {
/* cleanup if needed, runs always */
if (a.beg) free(a.beg);
}
END_TRY;
return EXIT_FAILURE; /* shouldn't reach here for this example */
}