diff --git a/include/ci2.h b/include/ci2.h index dfa52b1..daef847 100644 --- a/include/ci2.h +++ b/include/ci2.h @@ -25,4 +25,8 @@ SOFTWARE. #include "./platform/ci2_platform.h" #include "./platform/error/ci2_exception.h" + +#include "./platform/mem/ci2_arena.h" +#include "./platform/mem/ci2_mem.h" + #endif // ci2.h diff --git a/include/platform/mem/ci2_arena.h b/include/platform/mem/ci2_arena.h new file mode 100644 index 0000000..0c7ba97 --- /dev/null +++ b/include/platform/mem/ci2_arena.h @@ -0,0 +1,62 @@ +/* - | 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_ARENA_H +#define CI2_ARENA_H + +#include "../ci2_platform.h" +#include + +struct CI2_Arena +{ + char* beg; + char* end; +}; +typedef struct CI2_Arena CI2_Arena; + +/* Initialize an Arena with an existing buffer */ +CI2_DEF void +ci2_arena_back(struct CI2_Arena* a, char* buf, ptrdiff_t len); + +/* Allocate a new Arena */ +CI2_DEF struct CI2_Arena +ci2_arena_new(ptrdiff_t cap); + +/* Get the current capacity of an Arena */ +CI2_DEF ptrdiff_t +ci2_arena_capacity(struct CI2_Arena a); + +/* Make an allocation from an Arena and do not zero.*/ +CI2_DEF void* +ci2_arena_malloc(struct CI2_Arena* a, + ptrdiff_t size, + ptrdiff_t align, + ptrdiff_t count); + +/* Make an allocation from an Arena and zero.*/ +CI2_DEF void* +ci2_arena_calloc(struct CI2_Arena* a, + ptrdiff_t size, + ptrdiff_t align, + ptrdiff_t count); + +#endif // ci2_arena.h diff --git a/include/platform/mem/ci2_mem.h b/include/platform/mem/ci2_mem.h new file mode 100644 index 0000000..6347fca --- /dev/null +++ b/include/platform/mem/ci2_mem.h @@ -0,0 +1,71 @@ +/* - | 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 "../error/ci2_exception.h" +#include + +/* Out of memory exception forward declaration. */ +CI2_DEF const struct Exception oom; + +/* Allocate an object and do not zero. May throw exception. */ +CI2_DEF void* +ci2_try_malloc(size_t nbytes, const char* file, const char* func, int line); + +/* Allocate an object and zero ci2ory. May throw exception. */ +CI2_DEF void* +ci2_try_calloc(size_t count, + size_t nbytes, + const char* file, + const char* func, + int line); + +/* Free and null a pointer */ +CI2_DEF void +ci2_free(void** ptr, const char* file, const char* func, int line); + +/* Resize a non-null pointer */ +CI2_DEF void* +ci2_try_resize(void* ptr, + size_t nbytes, + const char* file, + const char* func, + int line); + +#define CI2_MALLOC(nbytes) \ + ci2_try_malloc((nbytes), __FILE__, CI2_FUNC, __LINE__) + +#define CI2_CALLOC(count, nbytes) \ + ci2_try_calloc((count), (nbytes), __FILE__, CI2_FUNC, __LINE__) + +#define CI2_NEW(p) ((p) = CI2_MALLOC((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__, CI2_FUNC, __LINE__), (ptr) = 0)) + +#define CI2_RESIZE(ptr, nbytes) \ + ((ptr) = ci2_try_resize((ptr), (nbytes), __FILE__, CI2_FUNC, __LINE__)) + +#endif // ci2_mem.h diff --git a/src/ci2_arena.c b/src/ci2_arena.c new file mode 100644 index 0000000..8ac6cfa --- /dev/null +++ b/src/ci2_arena.c @@ -0,0 +1,89 @@ +/* - | 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. + + * --------------------------------------------------------------------------*/ +#include "../include/platform/mem/ci2_arena.h" +#include +#include + +/* Initialize an Arena with an existing buffer */ +void +ci2_arena_back(struct CI2_Arena* a, char* buf, ptrdiff_t len) +{ + CI2_ASSERT(a != NULL); + CI2_ASSERT(buf != NULL); + a->beg = buf; + a->end = buf + len; +} + +/* Allocate a new Arena */ +struct CI2_Arena +ci2_arena_new(ptrdiff_t cap) +{ + struct CI2_Arena a = { 0 }; + a.beg = malloc(cap); + a.end = a.beg ? a.beg + cap : 0; + return a; +} + +/* Get the current capacity of an Arena */ +ptrdiff_t +ci2_arena_capacity(struct CI2_Arena a) +{ + ptrdiff_t res = a.end - a.beg; + return res; +} + +/* Make an allocation from an Arena and do not zero.*/ +void* +ci2_arena_calloc(struct CI2_Arena* a, + ptrdiff_t size, + ptrdiff_t align, + ptrdiff_t count) +{ + ptrdiff_t padding = -(uintptr_t)a->beg & (align - 1); + ptrdiff_t available = a->end - a->beg - padding; + if (available < 0 || count > available / size) { + return NULL; + } + ptrdiff_t total = padding + count * size; + void* p = a->beg + total; + a->beg += total; + return memset(p, 0, total); +} + +/* Make an allocation from an Arena and zero.*/ +void* +ci2_arena_malloc(struct CI2_Arena* a, + ptrdiff_t size, + ptrdiff_t align, + ptrdiff_t count) +{ + ptrdiff_t padding = -(uintptr_t)a->beg & (align - 1); + ptrdiff_t available = a->end - a->beg - padding; + if (available < 0 || count > available / size) { + return NULL; + } + ptrdiff_t total = padding + count * size; + void* p = a->beg + total; + a->beg += total; + return p; +} diff --git a/src/ci2_mem.c b/src/ci2_mem.c new file mode 100644 index 0000000..4106ac4 --- /dev/null +++ b/src/ci2_mem.c @@ -0,0 +1,94 @@ +/* - | 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. + + * --------------------------------------------------------------------------*/ +#include "../include/platform/mem/ci2_mem.h" + +#include + +const struct Exception oom = { "Out of Memory!" }; + +void* +ci2_try_malloc(size_t nbytes, const char* file, const char* func, int line) +{ + void* ptr; + CI2_ASSERT(nbytes > 0); + ptr = malloc(nbytes); + if (ptr == NULL) { + if (file == NULL) + CI2_RAISE(oom); + else + ci2_raise_exception(&oom, file, func, line); + } + return ptr; +} + +void* +ci2_try_calloc(size_t count, + size_t nbytes, + const char* file, + const char* func, + int line) +{ + void* ptr; + CI2_ASSERT(count > 0); + CI2_ASSERT(nbytes > 0); + ptr = calloc(count, nbytes); + if (ptr == NULL) { + if (file == NULL) + CI2_RAISE(oom); + else + ci2_raise_exception(&oom, file, func, line); + } + return ptr; +} +void +ci2_free(void** ptr, const char* file, const char* func, int line) +{ + UNUSED(file); + UNUSED(func); + UNUSED(line); + if (ptr && *ptr) { + free(*ptr); + *ptr = NULL; + } +} + +void* +ci2_try_resize(void* ptr, + size_t nbytes, + const char* file, + const char* func, + int line) +{ + CI2_ASSERT(ptr != NULL); + CI2_ASSERT(nbytes > 0); + void* temp = realloc(ptr, nbytes); + if (temp == NULL) { + if (file == NULL) + CI2_RAISE(oom); + else + ci2_raise_exception(&oom, file, func, line); + } + + ptr = temp; + return ptr; +}