2026-06-25 20:04:47 -05:00
|
|
|
/* - | Copyright / About | ----------------------------------------------------
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
This is the ci2_mem.h implementation. It's 'just wrappers for the general
|
|
|
|
|
stdlib allocators. If they fail they raise an exception with macro magic. All
|
|
|
|
|
other functions that may raise an exception are in include/platform/try. It's
|
|
|
|
|
not recommended to use these functions, as they are hard to debug and it's not
|
|
|
|
|
a robust way to handle errors.
|
|
|
|
|
* --------------------------------------------------------------------------*/
|
|
|
|
|
#include "../include/platform/mem/ci2_mem.h"
|
|
|
|
|
#include <stdlib.h> // malloc calloc free
|
|
|
|
|
|
|
|
|
|
const struct Exception oom = { "Out of memory!" };
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
|
ci2_alloc(size_t nbytes, const char* file, int line)
|
|
|
|
|
{
|
|
|
|
|
void* ptr;
|
2026-06-25 20:48:45 -05:00
|
|
|
CI2_ASSERT(nbytes > 0);
|
2026-06-25 20:04:47 -05:00
|
|
|
ptr = malloc(nbytes);
|
|
|
|
|
if (ptr == NULL) {
|
|
|
|
|
if (file == NULL)
|
|
|
|
|
CI2_RAISE(oom);
|
|
|
|
|
else
|
|
|
|
|
ci2_raise_exception(&oom, file, CI2_FUNC, line);
|
|
|
|
|
}
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
|
ci2_calloc(size_t count, size_t nbytes, const char* file, int line)
|
|
|
|
|
{
|
|
|
|
|
void* ptr;
|
2026-06-25 20:48:45 -05:00
|
|
|
CI2_ASSERT(count > 0);
|
|
|
|
|
CI2_ASSERT(nbytes > 0);
|
2026-06-25 20:04:47 -05:00
|
|
|
ptr = calloc(count, nbytes);
|
|
|
|
|
if (ptr == NULL) {
|
|
|
|
|
if (file == NULL)
|
|
|
|
|
CI2_RAISE(oom);
|
|
|
|
|
else
|
|
|
|
|
ci2_raise_exception(&oom, file, CI2_FUNC, line);
|
|
|
|
|
}
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ci2_free(void* ptr, const char* file, int line)
|
|
|
|
|
{
|
|
|
|
|
(void)file;
|
|
|
|
|
(void)line;
|
|
|
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
|
free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
|
ci2_resize(void* ptr, size_t nbytes, const char* file, int line)
|
|
|
|
|
{
|
2026-06-25 20:48:45 -05:00
|
|
|
// CI2_ASSERT(ptr); // ? Maybe
|
|
|
|
|
// CI2_ASSERT(nbytes > 0);// ? Maybe
|
2026-06-25 20:04:47 -05:00
|
|
|
void* tmp = realloc(ptr, nbytes);
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
|
if (file == NULL)
|
|
|
|
|
CI2_RAISE(oom);
|
|
|
|
|
else
|
|
|
|
|
ci2_raise_exception(&oom, file, CI2_FUNC, line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ptr = tmp;
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|