Initial commit
This commit is contained in:
31
include/mem.h
Normal file
31
include/mem.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef MEM_INCLUDED
|
||||
#define MEM_INCLUDED
|
||||
|
||||
#include "except.h" // Exceptions
|
||||
#include <stddef.h> // size_t
|
||||
|
||||
/* General Macros*/
|
||||
#define MEM_KB(x) ((size_t)(x) * 1024ULL)
|
||||
#define MEM_MB(x) ((size_t)(x) * 1024ULL * 1024ULL)
|
||||
#define MEM_GB(x) ((size_t)(x) * 1024ULL * 1024ULL * 1024ULL)
|
||||
#define MEM_SIZE(x) (ptrdiff_t)sizeof(x)
|
||||
#define MEM_COUNT(a) (MEM_SIZE(a) / MEM_SIZE(*(a)))
|
||||
#define MEM_LEN(s) (MEM_COUNT(s) - 1)
|
||||
|
||||
|
||||
extern const Exception oom; // Out of memory
|
||||
|
||||
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_resize(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 RESIZE(ptr, nbytes) ((ptr) = mem_resize((ptr), (nbytes), __FILE__, __LINE__))
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user