Added tests

This commit is contained in:
2025-10-18 11:22:09 -05:00
parent dd5764be5c
commit 952c2a668e
7 changed files with 131 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
# Compiler Flags
CC := gcc
CFLAGS := -g -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer
export ASAN_OPTIONS = allocator_may_return_null=1
# Directory variables
LIBDIR := lib
OBJ := obj
@@ -57,7 +57,7 @@ $(TEST)/bin:
test: $(LIB) $(TEST)/bin $(TESTBINS)
@SUCCESS_COUNT=0; FAILURE_COUNT=0; \
for test in $(TESTBINS); do \
./$$test; \
./$$test; \
EXIT_CODE=$$?; \
TEST_NAME=$(notdir $$test); \
if [ $$EXIT_CODE -eq 0 ]; then \

View File

@@ -19,17 +19,30 @@ enum MemFlags {
HARD_FAIL = 1 << 2, /* 0100 */
};
struct Arena{
unsigned char *beg;
unsigned char *end;
};
typedef struct Arena Arena;
extern const Exception OOM; // Out of memory
extern void *mem_alloc (int flags, size_t nbytes, const char *file, int line);
extern void mem_free(void *ptr, const char *file, int line);
extern void *mem_resize(int flags, void *ptr, size_t nbytes,
const char *file, int line);
extern int mem_is_zero(const void *ptr, size_t nbytes);
extern Arena mem_arena_new(int flags, size_t nbytes, const char *file, int line);
extern void *mem_arena_alloc(int flags, Arena *a,
size_t nbytes, size_t align, size_t count, const char *file, int line);
#define ALLOC(flags, nbytes) mem_alloc((flags), (nbytes), __FILE__, __LINE__)
#define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0))
#define RESIZE(flags, ptr, nbytes) ((ptr) = mem_resize((flags), (ptr), \
(nbytes), __FILE__, __LINE__))
#define ARENA(flags, nbytes)\
mem_arena_new((flags), (nbytes), __FILE__, __LINE__)
#define ARENA_ALLOC(flags, a, nbytes, align, count) \
mem_arena_alloc((flags), (a), (nbytes), (align), (count), __FILE__, __LINE__)
#endif

View File

@@ -1,10 +1,12 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "../include/except.h"
#include "../include/mem.h"
const Exception OOM = { "Out of Memory" };
#define ENOMEM 12 /* Out of memory */
const Exception OOM = { "Out of Memory" };/* Out of memory Exception */
#define MEM_ENOMEM 12 /* Out of memory errno */
#define MEM_ARENA_NULL ((Arena){ NULL, NULL }) /* Null arena error */
void *mem_alloc(int flags, size_t nbytes, const char *file, int line){
void *ptr;
@@ -12,7 +14,7 @@ void *mem_alloc(int flags, size_t nbytes, const char *file, int line){
ptr = malloc(nbytes);
if (ptr == NULL){
if (flags & SOFT_FAIL) return 0;
if (flags & HARD_FAIL) exit(ENOMEM);
if (flags & HARD_FAIL) exit(MEM_ENOMEM);
if (file == NULL) // Wasn't called by macro
RAISE(OOM); // Out of Memory Exception
@@ -36,7 +38,7 @@ void *mem_resize(int flags, void *ptr, size_t nbytes,const char *file, int line)
ptr = realloc(ptr, nbytes);
if (ptr == NULL){
if(flags & SOFT_FAIL) return 0;
if(flags & HARD_FAIL) exit(ENOMEM);
if(flags & HARD_FAIL) exit(MEM_ENOMEM);
if (file == NULL)
RAISE(OOM); // Out of memory exception
@@ -45,3 +47,51 @@ void *mem_resize(int flags, void *ptr, size_t nbytes,const char *file, int line)
}
return ptr;
}
int mem_is_zero(const void *ptr, size_t nbytes) {
static const unsigned char zero_block[1024] = {0}; /* reused */
while (nbytes >= sizeof(zero_block)) {
if (memcmp(ptr, zero_block, sizeof(zero_block)) != 0)
return 0;
ptr = (const unsigned char *)ptr + sizeof(zero_block);
nbytes -= sizeof(zero_block);
}
if (nbytes > 0 && memcmp(ptr, zero_block, nbytes) != 0)
return 0;
return 1;
}
Arena mem_arena_new(int flags, size_t nbytes, const char *file, int line){
Arena a = {0};
a.beg = malloc(nbytes);
if(a.beg == NULL){
if(flags & SOFT_FAIL) return MEM_ARENA_NULL;
if(flags & HARD_FAIL) exit(MEM_ENOMEM);
if (file == NULL)
RAISE(OOM); // Out of memory exception
else // Called by macro
except_raise(&OOM, file, line);
}
a.end = a.beg ? a.beg+nbytes : 0;
if( !flags & NOZERO ) memset(a.beg, 0, nbytes);
return a;
}
void* mem_arena_alloc(int flags, Arena *a, size_t nbytes, size_t align,
size_t count, const char *file, int line){
ptrdiff_t padding = -(uintptr_t)a->beg & (align - 1);
ptrdiff_t available = a->end - a->beg - padding;
if (available < 0 || count > available/nbytes) {
if(flags & SOFT_FAIL) return NULL;
if(flags & HARD_FAIL) exit(MEM_ENOMEM);
except_raise(&OOM, file, line);
}
void *p = a->beg + padding;
a->beg += padding + count * nbytes;
size_t total = padding + count * nbytes;
return flags&NOZERO ? p : memset(p, 0, total);
}

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 */
}