-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmalloc.c
68 lines (59 loc) · 1.17 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void*
xmalloc(size_t size)
{
void *p = malloc(size);
if (p == NULL) {
fprintf(stderr, "malloc() failed, aborting!\n");
exit(1);
}
return p;
}
void*
xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (p == NULL) {
fprintf(stderr, "realloc() failed, aborting!\n");
exit(1);
}
return p;
}
char*
xstrdup(char *src)
{
char *p = strdup(src);
if (p == NULL) {
fprintf(stderr, "strdup() failed, aborting!\n");
exit(1);
}
return p;
}
char*
xstrndup(char *src, size_t n)
{
char *p = strndup(src, n);
if (p == NULL) {
fprintf(stderr, "strndup() failed, aborting!\n");
exit(1);
}
return p;
}
/*
int xvasprintf(char **ret, const char *format, va_list ap) {
int len = vasprintf(ret, format, ap);
if (ret == NULL || len == -1) {
fprintf(stderr, "vasprintf() failed, aborting!\n");
}
return len;
}
int xasprintf(char **ret, const char *format, ...) {
va_list va;
va_start(va, format);
int len = xvasprintf(ret, format, va);
va_end(va);
return len;
}
*/