diff --git a/Makefile b/Makefile index 95a2a59..3b8792f 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/include/mem.h b/include/mem.h index 6d4115e..b70421b 100644 --- a/include/mem.h +++ b/include/mem.h @@ -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 diff --git a/src/mem.c b/src/mem.c index a12d2d5..f9a2c99 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,10 +1,12 @@ #include +#include #include #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); +} + diff --git a/tests/01_malloc.c b/tests/01_malloc.c new file mode 100644 index 0000000..ead898f --- /dev/null +++ b/tests/01_malloc.c @@ -0,0 +1,11 @@ +#define DEBUG +#include +#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; +} diff --git a/tests/02_calloc.c b/tests/02_calloc.c new file mode 100644 index 0000000..6aa0f16 --- /dev/null +++ b/tests/02_calloc.c @@ -0,0 +1,11 @@ +#define DEBUG +#include +#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; +} diff --git a/tests/03_malloc_softfail.c b/tests/03_malloc_softfail.c new file mode 100644 index 0000000..3e78c5a --- /dev/null +++ b/tests/03_malloc_softfail.c @@ -0,0 +1,11 @@ +#define DEBUG +#include +#include +#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; +} diff --git a/tests/04_malloc_exception.c b/tests/04_malloc_exception.c new file mode 100644 index 0000000..7d0bb71 --- /dev/null +++ b/tests/04_malloc_exception.c @@ -0,0 +1,28 @@ +#define DEBUG +#include +#include +#include +#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 */ + }