-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartalloc.c
More file actions
246 lines (208 loc) · 6.39 KB
/
Copy pathsmartalloc.c
File metadata and controls
246 lines (208 loc) · 6.39 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Smartalloc.c Copyright Clinton Staley 1991
*
* Smartalloc provides an malloc version which checks for several possible
* errors:
*
* 1. Failure of malloc or calloc call for any reason.
* 2. Attempt to free memory not allocated by malloc/calloc, or already freed.
* 3. Writing past the end or before the beginning of allocated memory.
* 4. Failure to free memory by some point in the program.
* 5. Use of data after it has been freed.
* 6. Assumption that data returned by malloc is set to 0.
*
* Use smartalloc by including smartalloc.h in any file that calls malloc,
* calloc or free. Also, compile smartalloc.c along with your other .c files.
* If you make any of errors 1-3 above, smartalloc will report the error
* and the file/line on which it occured. To find out if you have left
* memory unfreed, call report_space(). If any unfreed memory is
* outstanding, report_space will return the number of bytes of unfreed
* memory. If no memory is unfreed, report_space returns 0.
* Use of freed data is "detected" by writing garbage into the freed data,
* which will usually generate a runtime fault if the data is later used.
*
* All rights to this package are reserved by its author. Duplication of
* source or object code is permitted only with the author's permission.
*/
/*
* Changes by John Bellardo to merge C and C++ versions into unified
* source.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#define PATTERN 0xA
#define MARGIN 4
#define HASH_SIZE 113
#define smartalloc_hash(x) ( ((unsigned long)(x)) % HASH_SIZE )
#ifdef __cplusplus
extern "C" {
#endif
static pthread_mutex_t mutt = PTHREAD_MUTEX_INITIALIZER;
typedef struct track_t {
char *data;
unsigned long space;
unsigned char needs_free;
unsigned short margin;
struct track_t *next;
} track_t, *track_t_ptr;
static track_t_ptr track_hash[HASH_SIZE];
static unsigned long allocated = 0;
static int inited = 0;
static void completion_function(void)
{
if (allocated == 0)
return;
fprintf(stderr, "%lu bytes left unfreed at end of program!\n", allocated);
}
void smartalloc_track(char *data, unsigned long space, unsigned char needs_free, unsigned short margin)
{
track_t *temp;
if (inited == 0) {
atexit(completion_function);
memset(track_hash, 0, sizeof(track_hash));
inited = 1;
}
if ((temp = (track_t *) malloc(sizeof(track_t))) == NULL)
{
fprintf(stderr, "Malloc failure in in smartalloc\n");
exit(1);
}
pthread_mutex_lock(&mutt);
allocated += space;
temp->needs_free = needs_free;
temp->data = data;
temp->space = space;
temp->margin = margin;
temp->next = track_hash[smartalloc_hash(data)];
track_hash[smartalloc_hash(data)] = temp;
pthread_mutex_unlock(&mutt);
}
void *smartalloc(unsigned long bytes, char *file, int line, char fill)
{
char *data;
if ( (data = (char *)malloc(bytes + 2*MARGIN)) == NULL) {
fprintf(stderr, "Malloc failure in file %s on line %d\n", file, line);
exit(1);
}
data += MARGIN;
memset(data, fill, bytes);
memset(data-MARGIN, PATTERN, MARGIN);
memset(data+bytes, PATTERN, MARGIN);
smartalloc_track(data, bytes, 1, MARGIN);
return data;
}
void *smartvalloc(unsigned long bytes, char *file, int line, char fill)
{
char *data;
if ( (data = (char *)valloc(bytes + 2*MARGIN)) == NULL) {
fprintf(stderr, "Malloc failure in file %s on line %d\n", file, line);
exit(1);
}
data += MARGIN;
memset(data, fill, bytes);
memset(data-MARGIN, PATTERN, MARGIN);
memset(data+bytes, PATTERN, MARGIN);
smartalloc_track(data, bytes, 1, MARGIN);
return data;
}
track_t *removeTrackNode(void *address)
{
track_t *temp, *to_free;
if (track_hash[smartalloc_hash(address)] == NULL)
return NULL;
pthread_mutex_lock(&mutt);
if (track_hash[smartalloc_hash(address)]->data == address) {
to_free = track_hash[smartalloc_hash(address)];
track_hash[smartalloc_hash(address)] = track_hash[smartalloc_hash(address)]->next;
}
else {
for (temp = track_hash[smartalloc_hash(address)]; temp->next != NULL && temp->next->data != address;)
temp = temp->next;
if (temp->next == NULL) {
pthread_mutex_unlock(&mutt);
return NULL;
}
to_free = temp->next;
temp->next = to_free->next;
}
allocated -= to_free->space;
pthread_mutex_unlock(&mutt);
return to_free;
}
void freechecks(track_t *check, char *file, int line)
{
int i;
for (i = 0; i < check->margin; i++)
if (check->data[check->space + i] != PATTERN ||
check->data[-check->margin + i] != PATTERN) {
fprintf(stderr,
"Space freed in file %s at line %d has data written past bounds.\n",
file, line);
break;
}
memset(check->data, PATTERN, check->space);
}
void smartfree(void *address, char *file, int line)
{
track_t *to_free;
to_free = removeTrackNode(address);
if (NULL == to_free) {
fprintf(stderr,
"Attempt to free non-malloced space in file %s at line %d\n",
file, line);
return;
}
freechecks(to_free, file, line);
if (to_free->needs_free)
free(to_free->data - to_free->margin);
free(to_free);
}
void* smartrealloc(void* ptr, unsigned long newSize, int freeOnFailure,
char *file, int line, char fill)
{
track_t *to_free;
int limit;
void *newMem;
char *s, *d;
if (!ptr)
return smartalloc(newSize, file, line, fill);
to_free = removeTrackNode(ptr);
if (NULL == to_free) {
fprintf(stderr,
"Attempt to free non-malloced space in file %s at line %d\n",
file, line);
return NULL;
}
newMem = smartalloc(newSize, file, line, fill);
if (NULL == newMem) {
if (freeOnFailure) {
freechecks(to_free, file, line);
if (to_free->needs_free)
free(to_free->data - to_free->margin);
}
else
smartalloc_track(ptr, to_free->space, to_free->needs_free, MARGIN);
free(to_free);
return NULL;
}
limit = newSize < to_free->space ? newSize : to_free->space;
s = (char*)ptr;
d = (char*)newMem;
while (limit-- > 0)
*d++ = *s++;
freechecks(to_free, file, line);
if (to_free->needs_free)
free(to_free->data - to_free->margin);
free(to_free);
return newMem;
}
unsigned long report_space()
{
return allocated;
}
#ifdef __cplusplus
}
#endif