forked from mati75/evilwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmalloc.c
53 lines (42 loc) · 758 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
48
49
50
51
52
53
/*
Memory allocation with checking
Copyright 2014-2018 Ciaran Anscomb
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "xalloc.h"
void *xmalloc(size_t s) {
void *mem = malloc(s);
if (!mem) {
perror(NULL);
exit(EXIT_FAILURE);
}
return mem;
}
void *xzalloc(size_t s) {
void *mem = xmalloc(s);
memset(mem, 0, s);
return mem;
}
void *xrealloc(void *p, size_t s) {
void *mem = realloc(p, s);
if (!mem && s != 0) {
perror(NULL);
exit(EXIT_FAILURE);
}
return mem;
}
void *xmemdup(const void *p, size_t s) {
if (!p)
return NULL;
void *mem = xmalloc(s);
memcpy(mem, p, s);
return mem;
}
char *xstrdup(const char *str) {
return xmemdup(str, strlen(str) + 1);
}