nbtvis/lib/lib.c

52 lines
886 B
C
Raw Normal View History

2023-12-14 23:08:16 +00:00
#include "lib.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
2023-12-16 16:47:59 +00:00
#include <string.h>
2023-12-14 23:08:16 +00:00
__attribute__((__noreturn__))
static void die() {
exit(1);
}
void error_and_die(char *format, ...) {
va_list list;
va_start(list, format);
vfprintf(stderr, format, list);
die();
}
__attribute__((__noreturn__, format(printf, 1, 2)))
void perror_and_die(char *format, ...) {
va_list list;
va_start(list, format);
vfprintf(stderr, format, list);
perror(": ");
die();
}
void *xalloc(size_t amount) {
void *res = malloc(amount);
if (res == NULL)
error_and_die("failed to allocate memory");
return res;
}
2023-12-16 16:47:59 +00:00
void *xzalloc(size_t amount) {
void *res = xalloc(amount);
memset(res, 0, sizeof(amount));
return res;
}
2023-12-14 23:08:16 +00:00
void *xrealloc(void *ptr, size_t amount) {
void *res = realloc(ptr, amount);
if (res == NULL)
error_and_die("failed to allocate memory");
return res;
}