-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmalloc.c
52 lines (50 loc) · 1.7 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
/**************************************************************************************
* Filename: xmalloc.c
*
* Disclaimer: This file is based on chapter 7 of the book
*
* Programming Projects in C for Students of
* Engineering, Science and Mathematics
* Rouben Rostamian
* SIAM
* Computational Science & Engineering (2014)
* ISBN 978-1-611973-49-5
* https://userpages.umbc.edu/~rostamia/cbook/
*
* License: This library is free software; you can redistribute it and/or
* modify it under the terms of either:
*
* 1 the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* or
*
* 2 the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* See https://www.gnu.org/licenses/
***************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
void *malloc_or_exit(size_t nbytes, const char *file, int line)
{
void *x;
if ((x = calloc(nbytes,1)) == NULL) {
fprintf(stderr, "%s:line %d: calloc() of %zu bytes failed\n",file, line, nbytes);
exit(EXIT_FAILURE);
} else {
return x;
}
}
void *realloc_or_exit(void *v,size_t nbytes, const char *file, int line)
{
void *x;
if ((x = realloc(v,nbytes)) == NULL) {
fprintf(stderr, "%s:line %d: realloc() of %zu bytes failed\n",file, line, nbytes);
exit(EXIT_FAILURE);
} else {
return x;
}
}