36 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			36 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | #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)
 | ||
|  | 
 | ||
|  | /* Default behavior is to throw exceptions and zero out memory*/ | ||
|  | enum MemFlags { | ||
|  |     NOZERO       = 1 << 0,  /* 0001 */ | ||
|  |     SOFT_FAIL    = 1 << 1,  /* 0010 */ | ||
|  |     HARD_FAIL    = 1 << 2,  /* 0100 */ | ||
|  | }; | ||
|  | 
 | ||
|  | extern const Exception OOM; // Out of memory
 | ||
|  | 
 | ||
|  | extern void *mem_alloc (int flags, size_t nbytes, const char *file, int line); | ||
|  | extern void mem_free(void *ptr, const char *file, int line); | ||
|  | extern void *mem_resize(int flags, void *ptr, size_t nbytes, const char *file, int line); | ||
|  | extern int mem_is_zero(const void *ptr, size_t nbytes); | ||
|  | 
 | ||
|  | 
 | ||
|  | #define ALLOC(flags, nbytes)  mem_alloc((flags), (nbytes), __FILE__, __LINE__)
 | ||
|  | #define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0))
 | ||
|  | #define RESIZE(flags, ptr, nbytes) 	((ptr) = mem_resize((flags), (ptr), \
 | ||
|  | 	(nbytes), __FILE__, __LINE__)) | ||
|  | 
 | ||
|  | #endif
 |