summaryrefslogtreecommitdiff
path: root/tools/npl/xmem.h
blob: 173a88fd91b7770d3f15d5f9fe47377d53c28ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _X_MEM_H
#define _X_MEM_H

#include <stdlib.h>

static void oom_killer(void) {
	fprintf(stderr, "\n\n !!! Out of memory !!!\n\n");
	exit(1);
}

static inline void *xmalloc(size_t s) { void *ptr = malloc(s); if (!ptr) oom_killer(); return memset(ptr, 0x00, s); }
static inline void *xrealloc(void *p, size_t s) { void *ptr = realloc(p, s); if (!ptr) oom_killer(); return ptr; }

static inline void *xmemdup(void *p, size_t s) { void *ptr = malloc(s); if (!ptr) oom_killer(); return memcpy(ptr, p, s); }
static inline char *xstrdup(const char *s) { void *ptr = strdup(s); if (!ptr) oom_killer(); return ptr; }

#define xnew(x) (x *) xmalloc(sizeof(x))
#define xdup(x, y) (x *) xmemdup(y, sizeof(x))

#endif