1 /*
2  *	aprsc
3  *
4  *	(c) Heikki Hannikainen, OH7LZB <hessu@hes.iki.fi>
5  *
6  *	This program is licensed under the BSD license, which can be found
7  *	in the file LICENSE.
8  *
9  */
10 
11 /*
12  *	Replacements for malloc, realloc and free, which never fail,
13  *	and might keep statistics on memory allocation...
14  */
15 
16 #include <pthread.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <stdio.h>
20 
21 #include "hmalloc.h"
22 
23 int mem_panic = 0;
24 
hmalloc(size_t size)25 void *hmalloc(size_t size)
26 {
27 	void *p;
28 
29 	if (!(p = malloc(size))) {
30 		if (mem_panic)
31 			exit(1);	/* To prevent a deadlock */
32 		mem_panic = 1;
33 		fprintf(stderr, "hmalloc: Out of memory! Could not allocate %d bytes.", (int)size);
34 		exit(1);
35 	}
36 
37 	return p;
38 }
39 
hrealloc(void * ptr,size_t size)40 void *hrealloc(void *ptr, size_t size)
41 {
42 	void *p;
43 
44 	if (!(p = realloc(ptr, size))) {
45 		if (mem_panic)
46 			exit(1);
47 		mem_panic = 1;
48 		fprintf(stderr, "hrealloc: Out of memory! Could not reallocate %d bytes.", (int)size);
49 		exit(1);
50 	}
51 
52 	return p;
53 }
54 
hfree(void * ptr)55 void hfree(void *ptr)
56 {
57 	if (ptr)
58 		free(ptr);
59 }
60 
hstrdup(const char * s)61 char *hstrdup(const char *s)
62 {
63 	char *p;
64 
65 	p = hmalloc(strlen(s)+1);
66 	strcpy(p, s);
67 
68 	return p;
69 }
70 
71