Initial commit

This commit is contained in:
2026-01-01 16:44:21 -06:00
commit 31d6f8bf94
12 changed files with 436 additions and 0 deletions

11
tests/01_malloc.c Normal file
View File

@@ -0,0 +1,11 @@
#define DEBUG
#include <stdlib.h>
#include "../include/mem.h"
int main(void){
size_t nbytes = 20;
void *ptr = ALLOC(0, nbytes);
ASSERTED(ptr != NULL);
ASSERTED( mem_is_zero(ptr, nbytes));
return EXIT_SUCCESS;
}

11
tests/02_calloc.c Normal file
View File

@@ -0,0 +1,11 @@
#define DEBUG
#include <stdlib.h>
#include "../include/mem.h"
int main(void){
size_t nbytes = 20;
void *ptr = ALLOC(NOZERO, nbytes);
ASSERTED(ptr != NULL);
ASSERTED( !mem_is_zero(ptr, nbytes));
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,11 @@
#define DEBUG
#include <stdlib.h>
#include <stdint.h>
#include "../include/mem.h"
int main(void){
size_t nbytes = MEM_GB(1024);
void *ptr = ALLOC(SOFT_FAIL, nbytes);
ASSERTED(ptr == NULL);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,28 @@
#define DEBUG
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "../include/mem.h"
int main(void){
size_t nbytes = MEM_GB(1024);
void *buf;
TRY {
buf = ALLOC(0,nbytes); /* try 1GB */
if (!buf)
RAISE(OOM);
free(buf);
}
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 (buf) free(buf);
}
END_TRY;
return EXIT_FAILURE; /* shouldn't reach here for this example */
}

39
tests/_ Normal file
View File

@@ -0,0 +1,39 @@
#define DEBUG
#include <stdlib.h>
#include <stdio.h>
#include "../include/mem.h"
#include <unistd.h>
int main(void){
size_t count = 20;
size_t nbytes = sizeof(int32_t) * count;
struct Arena_Type mem = ARENA_TYPE(HARD_FAIL, ARENA_INT_32, count);
ASSERTED(mem.buf.beg != NULL);
ASSERTED( mem_is_zero(mem.buf.beg, nbytes));
ASSERTED( (size_t)(mem.buf.end - mem.buf.beg) == nbytes);
size_t size = (size_t)(mem.buf.end - mem.buf.beg);
ASSERTED(mem.header.type != arena_id_to_header[ARENA_UINT_32]);
ASSERTED(mem.header.type == arena_id_to_header[ARENA_INT_32]);
ASSERTED(mem.was_malloc == 1);
(void) size;
/*
printf("Bytes: %ld\t Alignment: %ld\t Type_Size: %ld\t Type_ID: %d\t Type_Header:%c\n",
size, size/count, size/count, mem.header.type, mem.header.type);
*/
unsigned char *ptr = mem.buf.beg;
int32_t *arr = ARENA_ALLOC(0, &mem.buf, sizeof(int32_t), sizeof(int32_t), count);
for (int i = 0; i < 20; i++){
arr[i] = rand();
}
ASSERTED(ptr != NULL);
char template[] = "/tmp/intfileXXXXXX";
int fd;
fd = mkstemp(template);
write(fd, ptr, nbytes);
lseek(fd,0,SEEK_SET);
unsigned char buf[nbytes];
ssize_t bytesread = read(fd, buf, nbytes);
printf("Read %ld bytes\n",bytesread);
free(ptr);
return EXIT_SUCCESS;
}