1 /*
2 * xmalloc.c
3 *
4 * Copyright (C) 2001, 2006, 2012 Staf Wagemakers Belgie/Belgium
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22 #include "xmalloc.h"
23
24 #ifndef HAVE_MALLOC
25
26 #undef malloc
27
28 #include <sys/types.h>
29
30 void *malloc ();
31
32 /* Allocate an N-byte block of memory from the heap.
33 If N is zero, allocate a 1-byte block. */
34
35 void *
rpl_malloc(size_t n)36 rpl_malloc (size_t n)
37 {
38 if (n == 0)
39 n = 1;
40 return malloc (n);
41 }
42
43 #endif
44
45 #ifndef HAVE_REALLOC
46
47 #undef realloc
48
49 #include <sys/types.h>
50
51 void *realloc ();
52
53 /* Allocate an N-byte block of memory from the heap.
54 If N is zero, allocate a 1-byte block. */
55
56 void *
rpl_realloc(void * p,size_t n)57 rpl_realloc (void *p,size_t n)
58 {
59 if (n == 0)
60 n = 1;
61 return realloc (p,n);
62 }
63
64 #endif
65
66
67
xmalloc_out_of_memory()68 void xmalloc_out_of_memory() {
69 fprintf(stderr,"Out of memory\n");
70 exit(1);
71 }
set_memerr(void (* s)())72 void (*set_memerr(void (*s)()))()
73 {
74 static void (*err)()=&xmalloc_out_of_memory;
75 if (s!=NULL) err=s;
76 return err;
77 }
xmalloc(size_t size)78 void *xmalloc (size_t size)
79 {
80 register void *value=(void *) malloc(size);
81 void (*errfunction)();
82 if (value==NULL) {
83 errfunction=set_memerr(NULL);
84 errfunction();
85 }
86 return (value);
87 }
xrealloc(void * ptr,size_t size)88 void *xrealloc (void *ptr, size_t size)
89 {
90 register void *value=(void *) realloc(ptr,size);
91 void (*errfunction)();
92 if (value==NULL) {
93 errfunction=set_memerr(NULL);
94 errfunction();
95 }
96 return (value);
97 }
98
xcalloc(size_t n,size_t size)99 void *xcalloc (size_t n, size_t size)
100 {
101 register void *value=(void *) calloc(n,size);
102 void (*errfunction)();
103 if (value==NULL) {
104 errfunction=set_memerr(NULL);
105 errfunction();
106 }
107 return (value);
108 }
109
xfree(void * ptr)110 void xfree(void *ptr)
111 {
112 if(ptr!=NULL) free(ptr);
113 }
114