mem/include/mem.h

29 lines
1.1 KiB
C
Raw Normal View History

2025-07-04 00:04:47 +00:00
#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)
2025-07-05 16:40:16 +00:00
#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
2025-07-04 00:04:47 +00:00
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