summaryrefslogtreecommitdiff
path: root/src/lib.c
blob: 7f077ba98cbc98e7a57cff6cf2244c05d92f2f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "lib.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

__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;
}

void *xrealloc(void *ptr, size_t amount) {
	void *res = realloc(ptr, amount);
	if (res == NULL)
		error_and_die("failed to allocate memory");
	return res;
}