#include "mem.h" #include #include #include #include #ifdef TOBI_SAN_BUILD const char *__asan_default_options(void) { return "detect_leaks=0:abort_on_error=1"; } #endif static void oom(void) { fputs("tobi: out of memory\n", stderr); abort(); } void *tobi_xmalloc(size_t n) { void *p = malloc(n == 0 ? 1 : n); if (!p) { oom(); } return p; } void *tobi_xcalloc(size_t count, size_t size) { if (size != 0 && count > SIZE_MAX / size) { oom(); } void *p = calloc(count == 0 ? 1 : count, size == 0 ? 1 : size); if (!p) { oom(); } return p; } void *tobi_xrealloc(void *ptr, size_t n) { void *p = realloc(ptr, n == 0 ? 1 : n); if (!p) { oom(); } return p; } char *tobi_xstrdup(const char *s) { size_t n = strlen(s); char *out = tobi_xmalloc(n + 1); memcpy(out, s, n + 1); return out; } char *tobi_xstrndup(const char *s, size_t n) { char *out = tobi_xmalloc(n + 1); memcpy(out, s, n); out[n] = '\0'; return out; }