ACPI AML decompiler w/ CFG recovery and structured pseudocode
29

Configure Feed

Select the types of activity you want to include in your feed.

tobi / src / mem.c
1.6 kB 84 lines
1#include "mem.h" 2 3#include <stdint.h> 4#include <stdio.h> 5#include <stdlib.h> 6#include <string.h> 7 8#ifdef TOBI_SAN_BUILD 9const char *__asan_default_options(void) { 10 return "detect_leaks=0:abort_on_error=1"; 11} 12#endif 13 14static void oom(void) { 15 fputs("tobi: out of memory\n", stderr); 16 abort(); 17} 18 19void *tobi_xmalloc(size_t n) { 20 void *p = malloc(n == 0 ? 1 : n); 21 if (!p) { 22 oom(); 23 } 24 return p; 25} 26 27void *tobi_xcalloc(size_t count, size_t size) { 28 if (size != 0 && count > SIZE_MAX / size) { 29 oom(); 30 } 31 void *p = calloc(count == 0 ? 1 : count, size == 0 ? 1 : size); 32 if (!p) { 33 oom(); 34 } 35 return p; 36} 37 38void *tobi_xrealloc(void *ptr, size_t n) { 39 void *p = realloc(ptr, n == 0 ? 1 : n); 40 if (!p) { 41 oom(); 42 } 43 return p; 44} 45 46size_t tobi_xadd_size(size_t a, size_t b) { 47 if (a > SIZE_MAX - b) { 48 oom(); 49 } 50 return a + b; 51} 52 53size_t tobi_xmul_size(size_t a, size_t b) { 54 if (b != 0 && a > SIZE_MAX / b) { 55 oom(); 56 } 57 return a * b; 58} 59 60size_t tobi_xgrow_cap(size_t current, size_t required, size_t initial) { 61 size_t cap = current ? current : (initial ? initial : 1u); 62 while (cap < required) { 63 if (cap > SIZE_MAX / 2u) { 64 cap = required; 65 break; 66 } 67 cap *= 2u; 68 } 69 return cap; 70} 71 72char *tobi_xstrdup(const char *s) { 73 size_t n = strlen(s); 74 char *out = tobi_xmalloc(n + 1); 75 memcpy(out, s, n + 1); 76 return out; 77} 78 79char *tobi_xstrndup(const char *s, size_t n) { 80 char *out = tobi_xmalloc(n + 1); 81 memcpy(out, s, n); 82 out[n] = '\0'; 83 return out; 84}