mem/include/mem.h

44 lines
1.3 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>
2025-07-19 18:46:13 +00:00
#define sizeof(x) (ptrdiff_t)sizeof(x)
2025-07-04 00:04:47 +00:00
#define NELEMS(a) (sizeof(a) / sizeof(*(a)))
#define LEN(s) (NELEMS(s) - 1)
2025-07-19 18:46:13 +00:00
#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
2025-07-05 16:40:16 +00:00
2025-07-19 18:46:13 +00:00
typedef enum {
SOFTFAIL = 0x1,
} MemFlags;
2025-07-04 00:04:47 +00:00
extern const Exception out_of_memory; // OOM Exception
2025-07-19 18:46:13 +00:00
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);
2025-07-04 00:04:47 +00:00
extern void mem_free(void *ptr, const char *file, int line);
2025-07-19 18:46:13 +00:00
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))
2025-07-04 00:04:47 +00:00
#endif