Files
ci2/include/ci2_mem.h

54 lines
2.4 KiB
C
Raw Normal View History

2026-05-12 07:50:58 -05:00
/* - | Copyright | --------------------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* --------------------------------------------------------------------------*/
#ifndef CI2_MEM_H
#define CI2_MEM_H
#include "ci2_exception.h" // Exceptions
#include <stddef.h> // size_t
/* General Macros*/
#define CI2_KB(x) ((size_t)(x) * 1024ULL)
#define CI2_MB(x) ((size_t)(x) * 1024ULL * 1024ULL)
#define CI2_GB(x) ((size_t)(x) * 1024ULL * 1024ULL * 1024ULL)
#define CI2_SIZE(x) (ptrdiff_t)sizeof(x)
#define CI2_COUNT(a) (CI2_SIZE(a) / CI2_SIZE(*(a)))
#define CI2_LEN(s) (CI2_COUNT(s) - 1)
extern const CI2_Exception oom; // Out of memory
extern void *ci2_alloc (size_t nbytes,const char *file, int line);
extern void *ci2_calloc(size_t count, size_t nbytes, const char *file, int line);
extern void ci2_free(void *ptr, const char *file, int line);
extern void *ci2_resize(void *ptr, size_t nbytes, const char *file, int line);
#define CI2_ALLOC(nbytes) ci2_alloc((nbytes), __FILE__, __LINE__)
#define CI2_CALLOC(count, nbytes) ci2_calloc((count), (nbytes), __FILE__, __LINE__)
#define CI2_NEW(p) ((p) = CI2_ALLOC((size_t)sizeof *(p)))
#define CI2_NEW0(p) ((p) = CI2_CALLOC(1, (size_t)sizeof *(p)))
#define CI2_FREE(ptr) ((void)(ci2_free((ptr), __FILE__, __LINE__), (ptr) = 0))
#define CI2_RESIZE(ptr, nbytes) ((ptr) = ci2_resize((ptr), (nbytes), __FILE__, __LINE__))
#endif // ci2_mem.h