summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/OSLib/MemUtils.c
blob: fcacd810570feedbcc32073f489203df6f23d490 (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
45
46
47
48
49
50
51
52
53
54
#include "oslib.h"

void *xmalloc(const char *what, int size) {
    void *ret;

    ret = malloc(size ? size : 1);
    if (!ret) {
        fprintf(
                stderr,
                "*** Out of memory when allocating %d bytes%s%s",
                size,
                what ? " for " : "",
                what ? what : "");
        exit(-23);
        return 0;
    }

    return ret;
}

void *xcalloc(const char *what, int size) {
    void *ret;

    ret = xmalloc(what, size);
    memset(ret, 0, size);
    return ret;
}

void *xrealloc(const char *what, void *old, int size) {
    void *ret;

    ret = realloc(old, size ? size : 1);
    if (!ret) {
        fprintf(
                stderr,
                "*** Out of memory when resizing buffer to %d bytes%s%s",
                size,
                what ? " for " : "",
                what ? what : "");
        exit(-23);
        return 0;
    }

    return ret;
}

char *xstrdup(const char *str) {
    return strcpy(xmalloc(0, strlen(str) + 1), str);
}

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