diff options
author | Preston Pan <ret2pop@gmail.com> | 2024-12-24 21:11:06 -0800 |
---|---|---|
committer | Preston Pan <ret2pop@gmail.com> | 2024-12-24 21:11:06 -0800 |
commit | e75d6f6b8f4512a5bbfecbfa8c17f0bb687e3d55 (patch) | |
tree | 7a96002751a1ca3b173762a13b181ebea4123817 /src/helpers.c |
first commit
Diffstat (limited to 'src/helpers.c')
-rw-r--r-- | src/helpers.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/helpers.c b/src/helpers.c new file mode 100644 index 0000000..ad7228e --- /dev/null +++ b/src/helpers.c @@ -0,0 +1,28 @@ +#include <helpers.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void die(const char *msg) { + fprintf(stderr, "panic: "); + perror(msg); + exit(EXIT_FAILURE); +} + +void *safe_calloc(unsigned int i, size_t size) { + void *x = calloc(i, size); + if (x == NULL) { + die("abort: calloc()"); + } + return x; +} + +void *safe_realloc(void *x, size_t size) { + void *p = realloc(x, size); + if (x == NULL) { + die("abort: realloc()"); + } + return p; +} + +void alloc_zero(void *ptr, size_t size) { memset(ptr, 0, size); } |