-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmalloc.c
47 lines (41 loc) · 847 Bytes
/
xmalloc.c
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
#ifndef _XMALLOC_C
#define _XMALLOC_C
#include <stdlib.h>
#include "fail.c"
static void *xmalloc(size_t size)
{
#ifndef NDEBUG
{
double sm = size / (0x100000 * 1.0);
if (sm > 1000)
fprintf(stderr, "WARNING: large malloc"
" %zu bytes (%gMB)\n", size, sm);
}
#endif
if (size == 0)
fail("xmalloc: zero size");
void *new = malloc(size);
if (!new)
{
double sm = size / (0x100000 * 1.0);
fail("xmalloc: out of memory when requesting "
"%zu bytes (%gMB)",//:\"%s\"",
size, sm);//, strerror(errno));
}
return new;
}
inline // to avoid unused warnings
static void *xrealloc(void *p, size_t s)
{
void *r = realloc(p, s);
if (!r) fail("realloc failed");
return r;
}
inline // to avoid unused warnings
static void xfree(void *p)
{
if (!p)
fail("thou shalt not free a null pointer!");
free(p);
}
#endif//_XMALLOC_C