2013-09-13 18:29:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012-2013, Jakub Zawadzki <darkjames-ws@darkjames.pl>
|
|
|
|
*
|
2018-02-09 09:14:34 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2013-09-13 18:29:08 +00:00
|
|
|
*/
|
|
|
|
|
2013-03-03 11:59:14 +00:00
|
|
|
#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");
|
2013-03-17 15:32:07 +00:00
|
|
|
abort();
|
2013-03-03 11:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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); }
|
2013-03-09 15:53:30 +00:00
|
|
|
static inline char *xstrdup(const char *s) { void *ptr = strdup(s); if (!ptr) oom_killer(); return ptr; }
|
2013-03-03 11:59:14 +00:00
|
|
|
|
|
|
|
#define xnew(x) (x *) xmalloc(sizeof(x))
|
|
|
|
#define xdup(x, y) (x *) xmemdup(y, sizeof(x))
|
|
|
|
|
|
|
|
#endif
|