summaryrefslogtreecommitdiff
path: root/lib/xalloc.c
blob: 08739bc62c6acb2c520b93a7237429ea0fda51e5 (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
#include "xalloc.h"

#include <stdlib.h>
#include <string.h>

#include "logger.h"

static void die() __attribute__((noreturn));

void *xcalloc(size_t count, size_t size) {
    void *res = calloc(count, size);
    if (res == NULL) {
        die();
    }

    return res;
}

void xfree(void *ptr) { free(ptr); }

void *xmalloc(size_t size) {
    void *res = malloc(size);
    if (res == NULL) {
        die();
    }
    memset(res, 0, size);

    return res;
}

void *xrealloc(void *ptr, size_t size) {
    void *res = realloc(ptr, size);
    if (res == NULL) {
        die();
    }

    return res;
}

void die() { log_fatal("xmap", "Out of memory"); }