File: cmalloc.c

package info (click to toggle)
siag 3.1.22-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,024 kB
  • ctags: 8,635
  • sloc: ansic: 66,498; lisp: 4,499; sh: 568; makefile: 550
file content (207 lines) | stat: -rw-r--r-- 3,936 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
    Copyright (C) 1996-1998  Ulric Eriksson <ulric@edu.stockholm.se>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston,
    MA 02111-1307, USA.
*/

/* ---
cmalloc.c

A set of functions to allocate and deallocate memory with a little
record keeping and error checking.
--- */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmalloc.h"

/* ---
Allocate memory and check if anything was actually allocated.
If not, print an error message or do something else
*/

static void std_alloc_fail(void)
{
	fprintf(stderr, "Out of memory\n");
	exit(EXIT_FAILURE);
}

static void (*alloc_fail)(void) = std_alloc_fail;

/* Also does a few checks that the library functions should already do */

static int paranoia = 1;

typedef struct malloc_node {
	void *p;			/* allocated memory */
	struct malloc_node *next;	/* next in chain */
} malloc_node;

malloc_node *nodes = NULL;

static void remove_node(void *p)
{
	malloc_node *m, *n;

	if (!p) return;		/* deallocating NULL is fine */

	n = nodes;
	if (!n) {
		if (paranoia == 1) return;
		fprintf(stderr,
			"Deallocating %p when nothing allocated\n", p);
		if (paranoia == 2) return;
		alloc_fail();
	}

	if (n->p == p) {
		nodes = n->next;
		free(n);
		return;
	}

	for (m = n->next; m; m = m->next) {
		if (m->p == p) {
			n->next = m->next;
			free(m);
			return;
		}
		n = m;
	}

	if (paranoia == 1) return;
	fprintf(stderr, "Deallocating %p which was not allocated\n", p);
	if (paranoia == 2) return;
	alloc_fail();
}

static void insert_node(void *p)
{
	malloc_node *n;

	if (!p) return;		/* we don't need to remember NULL */

	n = malloc(sizeof(malloc_node));
	if (n == NULL) alloc_fail();
	n->p = p;
	n->next = nodes;
	nodes = n;
}

/* ---
Set up a new failure handler and paranoia level.

	0 = don't check at all
	1 = try to fix errors without telling
	2 = warn about errors and try to fix them
	3 = warn about errors and fail
*/

void cmalloc_init(void (*new_fail)(void), int level)
{
	if (new_fail) alloc_fail = new_fail;
	else alloc_fail = std_alloc_fail;
	paranoia = level;
}

/* ---
Check for memory leaks.
*/

void cmalloc_exit(void)
{
	malloc_node *n;

	if (paranoia == 0) return;

	for (n = nodes; n; n = n->next) {
		if (paranoia >= 2)
			fprintf(stderr, "Didn't deallocate %p\n", n->p);
		if (paranoia == 3) alloc_fail();
		cfree(n->p);
	}
}

/* ---
Allocate memory with a bit of record keeping and error checking.
*/

void *cmalloc(size_t size)
{
	void *p;

	p = malloc(size);
	if (p == NULL) alloc_fail();
	if (paranoia) insert_node(p);
	return p;
}

/* ---
Reallocate memory.
*/

void *crealloc(void *ptr, size_t size)
{
	void *p;

	if (paranoia) remove_node(ptr);
	p = realloc(ptr, size);
	if (p == NULL) alloc_fail();
	if (paranoia) insert_node(p);
	return p;
}

/* ---
Allocate memory and initialize it to all zero.
*/

void *ccalloc(size_t nmemb, size_t size)
{
	void *p;

	p = calloc(nmemb, size);
	if (p == NULL) alloc_fail();
	if (paranoia) insert_node(p);
	return p;
}

/* ---
Allocate a duplicate of a string.
*/

char *cstrdup(const char *s)
{
	char *p;

	if (s) p = malloc(strlen(s)+1);
	else p = NULL;
	if (p == NULL) alloc_fail();
	else strcpy(p, s);
	if (paranoia) insert_node(p);
	return p;
}

/* ---
Free a block of memory.
*/

void cfree(void *ptr)
{
	if (paranoia) remove_node(ptr);
	free(ptr);
}