mem/include/mem.h
2025-07-03 19:04:47 -05:00

25 lines
971 B
C

#ifndef MEM_INCLUDED
#define MEM_INCLUDED
#include "except.h"
#include <stddef.h>
#define SIZEOF(x) (ptrdiff_t)sizeof(x)
#define NELEMS(a) (sizeof(a) / sizeof(*(a)))
#define LEN(s) (NELEMS(s) - 1)
extern const Exception out_of_memory; // OOM Exception
extern void *mem_alloc (size_t nbytes,const char *file, int line);
extern void *mem_calloc(size_t count, size_t nbytes,const char *file, int line);
extern void mem_free(void *ptr, const char *file, int line);
extern void *mem_realloc(void *ptr, size_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((size_t)sizeof *(p)))
#define NEW0(p) ((p) = CALLOC(1, (size_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__))
#endif