From e0cd93f6e05477c5de7f7ea04204855c46f811ff Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Sat, 19 Jul 2025 13:46:13 -0500 Subject: [PATCH] Added flags --- include/mem.h | 43 +++++++++++++++++++++++++++++-------------- src/mem.c | 19 +++++++++++++++---- tests/01_alloc.c | 3 +-- tests/02_new.c | 3 +-- tests/03_calloc.c | 3 +-- tests/04_new0.c | 5 ++--- 6 files changed, 49 insertions(+), 27 deletions(-) diff --git a/include/mem.h b/include/mem.h index 2979f15..5271a6e 100644 --- a/include/mem.h +++ b/include/mem.h @@ -4,25 +4,40 @@ #include "except.h" #include -#define SIZEOF(x) (ptrdiff_t)sizeof(x) +#define sizeof(x) (ptrdiff_t)sizeof(x) #define NELEMS(a) (sizeof(a) / sizeof(*(a))) #define LEN(s) (NELEMS(s) - 1) -#define KB(x) ((size_t)(x) << 10) // 1 KB = 1024 bytes -#define MB(x) ((size_t)(x) << 20) // 1 MB = 1024 * 1024 bytes -#define GB(x) ((size_t)(x) << 30) // 1 GB = 1024 * 1024 * 1024 bytes +#define KB(x) ((ptrdiff_t)(x) << 10) // 1 KB = 1024 bytes +#define MB(x) ((ptrdiff_t)(x) << 20) // 1 MB = 1024 * 1024 bytes +#define GB(x) ((ptrdiff_t)(x) << 30) // 1 GB = 1024 * 1024 * 1024 bytes +typedef enum { + SOFTFAIL = 0x1, +} MemFlags; extern const Exception out_of_memory; // OOM Exception -extern void *mem_alloc (ptrdiff_t nbytes,const char *file, int line); -extern void *mem_calloc(ptrdiff_t count, ptrdiff_t nbytes,const char *file, int line); -extern void mem_free(void *ptr, const char *file, int line); -extern void *mem_realloc(void *ptr, ptrdiff_t nbytes, const char *file, int line); -#define ALLOC(nbytes) mem_alloc((nbytes), __FILE__, __LINE__) -#define CALLOC(count, nbytes) mem_calloc((count), (nbytes), __FILE__, __LINE__) -#define NEW(p) ((p) = ALLOC((ptrdiff_t)sizeof *(p))) -#define NEW0(p) ((p) = CALLOC(1, (ptrdiff_t)sizeof *(p))) -#define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0)) -#define REALLOC(ptr, nbytes) ((ptr) = mem_realloc((ptr), (nbytes), __FILE__, __LINE__)) +extern void *mem_alloc (ptrdiff_t nbytes, const char *file, int line, int flags); +extern void *mem_calloc(ptrdiff_t count, ptrdiff_t nbytes, const char *file, int line, int flags); +extern void mem_free(void *ptr, const char *file, int line); +extern void *mem_realloc(void *ptr, ptrdiff_t nbytes, const char *file, int line, int flags); + +#define ALLOC(nbytes,flags) \ + mem_alloc((nbytes), __FILE__, __LINE__, flags) + +#define CALLOC(count, nbytes, flags) \ + mem_calloc((count), (nbytes), __FILE__, __LINE__, flags) + +#define NEW(p,flags) \ + ((p) = ALLOC((ptrdiff_t)sizeof *(p), flags)) + +#define NEW0(p,flags) \ + ((p) = CALLOC(1, (ptrdiff_t)sizeof *(p), flags)) + +#define FREE(ptr) \ + ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0)) + +#define REALLOC(ptr, nbytes, flags) \ + ((ptr) = mem_realloc((ptr), (nbytes), __FILE__, __LINE__, flags)) #endif diff --git a/src/mem.c b/src/mem.c index eadafeb..ec3031f 100644 --- a/src/mem.c +++ b/src/mem.c @@ -4,11 +4,13 @@ const Exception out_of_memory = { "Out Of Memory" }; // OOM Exception. -void *mem_alloc(ptrdiff_t nbytes, const char *file, int line){ +void *mem_alloc(ptrdiff_t nbytes, const char *file, int line, int flags) +{ void *ptr; // Memory to allocate. ASSERTED(nbytes > 0); ptr = malloc(nbytes); // Attempt allocation. if (ptr == NULL){ // Out of memory, raise exception . + if(flags & SOFTFAIL) return NULL; if (file == NULL) // Wasn't called by macro. RAISE(out_of_memory); else // Called by macro. @@ -17,12 +19,15 @@ void *mem_alloc(ptrdiff_t nbytes, const char *file, int line){ // Success, return allocated memory. return ptr; } -void *mem_calloc(ptrdiff_t count, ptrdiff_t nbytes,const char *file, int line) { + +void *mem_calloc(ptrdiff_t count, ptrdiff_t nbytes,const char *file, int line, int flags) +{ void *ptr; // Memory to allocate. ASSERTED(count > 0); ASSERTED(nbytes > 0); ptr = calloc(count, nbytes);// Attempt allocation and zero out memory. if (ptr == NULL){ // Out of memory raise exception. + if(flags & SOFTFAIL) return NULL; if (file == NULL) // Wasn't called by macro. RAISE(out_of_memory); else // Was called by macro. @@ -31,7 +36,9 @@ void *mem_calloc(ptrdiff_t count, ptrdiff_t nbytes,const char *file, int line) { // Success, return allocated and zeroed memory. return ptr; } -void mem_free(void *ptr, const char *file, int line) { + +void mem_free(void *ptr, const char *file, int line) +{ (void) file; (void) line; if (ptr){// If we have valid pointer, free and null. @@ -39,11 +46,15 @@ void mem_free(void *ptr, const char *file, int line) { ptr = NULL; } } -void *mem_realloc(void *ptr, ptrdiff_t nbytes, const char *file, int line) { + +void *mem_realloc(void *ptr, ptrdiff_t nbytes, const char *file, int line, int flags) +{ ASSERTED(ptr); ASSERTED(nbytes > 0); ptr = realloc(ptr, nbytes); // Attempt reallocation. if (ptr == NULL){ // Out of memory, raise exception. + if(flags & SOFTFAIL) return NULL; + if (file == NULL) // Wasn't called by macro. RAISE(out_of_memory); else // Called by macro. diff --git a/tests/01_alloc.c b/tests/01_alloc.c index 61f64ce..2829d6a 100644 --- a/tests/01_alloc.c +++ b/tests/01_alloc.c @@ -20,10 +20,9 @@ static bool isZeroed(void *memory, unsigned int size) int main(void){ - char *buf = ALLOC(MB(20)); + char *buf = ALLOC(MB(20), 0); ASSERTED(buf != NULL); ASSERTED(isZeroed(buf, BUFSZ) == false); FREE(buf); - ASSERTED(buf == NULL); return EXIT_SUCCESS; } diff --git a/tests/02_new.c b/tests/02_new.c index 5e630a0..6e295eb 100644 --- a/tests/02_new.c +++ b/tests/02_new.c @@ -20,10 +20,9 @@ static bool isZeroed(void *memory, unsigned int size) int main(void){ - char *buf = NEW(buf); + char *buf = NEW(buf, 0); ASSERTED(buf != NULL); ASSERTED(isZeroed(buf, BUFSZ) == false); FREE(buf); - ASSERTED(buf == NULL); return EXIT_SUCCESS; } diff --git a/tests/03_calloc.c b/tests/03_calloc.c index 177c06d..effb102 100644 --- a/tests/03_calloc.c +++ b/tests/03_calloc.c @@ -20,10 +20,9 @@ static bool isZeroed(void *memory, unsigned int size) int main(void){ - char *buf = CALLOC(BUFSZ, SIZEOF(char *)); + char *buf = CALLOC(BUFSZ, sizeof(char *), 0); ASSERTED(buf != NULL); ASSERTED(isZeroed(buf, BUFSZ) == true); FREE(buf); - ASSERTED(buf == NULL); return EXIT_SUCCESS; } diff --git a/tests/04_new0.c b/tests/04_new0.c index cff82f7..33484bd 100644 --- a/tests/04_new0.c +++ b/tests/04_new0.c @@ -20,10 +20,9 @@ static bool isZeroed(void *memory, unsigned int size) int main(void){ - char *buf = NEW0(buf); + char *buf = NEW0(buf, 0); ASSERTED(buf != NULL); - ASSERTED(isZeroed(buf,SIZEOF(char)) == true); + ASSERTED(isZeroed(buf,sizeof(char)) == true); FREE(buf); - ASSERTED(buf == NULL); return EXIT_SUCCESS; }