-
Notifications
You must be signed in to change notification settings - Fork 28
/
misc.c
46 lines (35 loc) · 816 Bytes
/
misc.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
/*
* This file is part of hat-trie.
*
* Copyright (c) 2011 by Daniel C. Jones <[email protected]>
*
*/
#include "misc.h"
#include <stdlib.h>
void* malloc_or_die(size_t n)
{
void* p = malloc(n);
if (p == NULL && n != 0) {
fprintf(stderr, "Cannot allocate %zu bytes.\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void* realloc_or_die(void* ptr, size_t n)
{
void* p = realloc(ptr, n);
if (p == NULL && n != 0) {
fprintf(stderr, "Cannot allocate %zu bytes.\n", n);
exit(EXIT_FAILURE);
}
return p;
}
FILE* fopen_or_die(const char* path, const char* mode)
{
FILE* f = fopen(path, mode);
if (f == NULL) {
fprintf(stderr, "Cannot open file %s with mode %s.\n", path, mode);
exit(EXIT_FAILURE);
}
return f;
}