Added flags
This commit is contained in:
parent
c439741f9e
commit
e0cd93f6e0
@ -4,25 +4,40 @@
|
||||
#include "except.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
||||
|
19
src/mem.c
19
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.
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user