-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathallocate.c
360 lines (251 loc) · 8.43 KB
/
allocate.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (C) 2006 B.A.T.M.A.N. contributors:
* Thomas Lopatic, Corinna 'Elektra' Aichele, Axel Neumann, Marek Lindner
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "bmx.h"
#ifdef DEBUG_MALLOC
#define MAGIC_NUMBER_HEADER 0xB2B2B2B2
#define MAGIC_NUMBER_TRAILOR 0xB2
struct chunkHeader *chunkList = NULL;
struct chunkHeader
{
struct chunkHeader *next;
uint32_t length;
int32_t tag;
uint32_t magicNumberHeader;
};
typedef unsigned char MAGIC_TRAILER_T;
#ifdef MEMORY_USAGE
struct memoryUsage *memoryList = NULL;
struct memoryUsage
{
struct memoryUsage *next;
uint32_t length;
uint32_t counter;
int32_t tag;
};
void addMemory(uint32_t length, int32_t tag)
{
struct memoryUsage *walker;
for ( walker = memoryList; walker != NULL; walker = walker->next ) {
if ( walker->tag == tag ) {
walker->counter++;
break;
}
}
if ( walker == NULL ) {
walker = malloc( sizeof(struct memoryUsage) );
walker->length = length;
walker->tag = tag;
walker->counter = 1;
walker->next = memoryList;
memoryList = walker;
}
}
void removeMemory(int32_t tag, int32_t freetag)
{
struct memoryUsage *walker;
for ( walker = memoryList; walker != NULL; walker = walker->next ) {
if ( walker->tag == tag ) {
if ( walker->counter == 0 ) {
dbg_sys(DBGT_ERR, "Freeing more memory than was allocated: malloc tag = %d, free tag = %d",
tag, freetag );
cleanup_all( -500069 );
}
walker->counter--;
break;
}
}
if ( walker == NULL ) {
dbg_sys(DBGT_ERR, "Freeing memory that was never allocated: malloc tag = %d, free tag = %d",
tag, freetag );
cleanup_all( -500070 );
}
}
void debugMemory(struct ctrl_node *cn)
{
struct memoryUsage *memoryWalker;
dbg_printf( cn, "\nMemory usage information:\n" );
for ( memoryWalker = memoryList; memoryWalker != NULL; memoryWalker = memoryWalker->next ) {
if ( memoryWalker->counter != 0 )
dbg_printf( cn, " tag: %4i, num malloc: %4i, bytes per malloc: %4i, total: %6i\n",
memoryWalker->tag, memoryWalker->counter, memoryWalker->length,
memoryWalker->counter * memoryWalker->length );
}
dbg_printf( cn, "\n" );
}
#endif //#ifdef MEMORY_USAGE
void checkIntegrity(void)
{
struct chunkHeader *walker;
MAGIC_TRAILER_T *chunkTrailer;
unsigned char *memory;
// dbgf_all(DBGT_INFO, " ");
for (walker = chunkList; walker != NULL; walker = walker->next)
{
if (walker->magicNumberHeader != MAGIC_NUMBER_HEADER)
{
dbgf_sys(DBGT_ERR, "invalid magic number in header: %08x, malloc tag = %d",
walker->magicNumberHeader, walker->tag );
cleanup_all( -500073 );
}
memory = (unsigned char *)walker;
chunkTrailer = (MAGIC_TRAILER_T*)(memory + sizeof(struct chunkHeader) + walker->length);
if (*chunkTrailer != MAGIC_NUMBER_TRAILOR)
{
dbgf_sys(DBGT_ERR, "invalid magic number in trailer: %08x, malloc tag = %d",
*chunkTrailer, walker->tag );
cleanup_all( -500075 );
}
}
}
void checkLeak(void)
{
struct chunkHeader *walker;
if (chunkList != NULL) {
openlog( "bmx6", LOG_PID, LOG_DAEMON );
for (walker = chunkList; walker != NULL; walker = walker->next) {
syslog( LOG_ERR, "Memory leak detected, malloc tag = %d\n", walker->tag );
fprintf( stderr, "Memory leak detected, malloc tag = %d \n", walker->tag );
}
closelog();
}
}
void *_debugMalloc(uint32_t length, int32_t tag, uint8_t reset)
{
unsigned char *memory;
struct chunkHeader *chunkHeader;
MAGIC_TRAILER_T *chunkTrailer;
unsigned char *chunk;
if (!length)
return NULL;
if (reset) {
memory = malloc(length + sizeof(struct chunkHeader) + sizeof(MAGIC_TRAILER_T));
memset(memory, 0, length + sizeof(struct chunkHeader) + sizeof(MAGIC_TRAILER_T));
} else {
memory = malloc(length + sizeof(struct chunkHeader) + sizeof(MAGIC_TRAILER_T));
}
if (memory == NULL)
{
dbg_sys(DBGT_ERR, "Cannot allocate %u bytes, malloc tag = %d",
(unsigned int)(length + sizeof(struct chunkHeader) + sizeof(MAGIC_TRAILER_T)), tag );
cleanup_all( -500076 );
}
chunkHeader = (struct chunkHeader *)memory;
chunk = memory + sizeof(struct chunkHeader);
chunkTrailer = (MAGIC_TRAILER_T*)(memory + sizeof(struct chunkHeader) + length);
chunkHeader->length = length;
chunkHeader->tag = tag;
chunkHeader->magicNumberHeader = MAGIC_NUMBER_HEADER;
*chunkTrailer = MAGIC_NUMBER_TRAILOR;
chunkHeader->next = chunkList;
chunkList = chunkHeader;
#ifdef MEMORY_USAGE
addMemory( length, tag );
#endif //#ifdef MEMORY_USAGE
return chunk;
}
void *_debugRealloc(void *memoryParameter, uint32_t length, int32_t tag)
{
unsigned char *result = _debugMalloc(length, tag, 0);
uint32_t copyLength = 0;
if (memoryParameter) { /* if memoryParameter==NULL, realloc() should work like malloc() !! */
struct chunkHeader *chunkHeader =
(struct chunkHeader *) (((unsigned char *) memoryParameter) - sizeof (struct chunkHeader));
MAGIC_TRAILER_T * chunkTrailer =
(MAGIC_TRAILER_T *) (((unsigned char *) memoryParameter) + chunkHeader->length);
if (chunkHeader->magicNumberHeader != MAGIC_NUMBER_HEADER) {
dbgf_sys(DBGT_ERR, "invalid magic number in header: %08x, malloc tag = %d",
chunkHeader->magicNumberHeader, chunkHeader->tag );
cleanup_all( -500078 );
}
if (*chunkTrailer != MAGIC_NUMBER_TRAILOR) {
dbgf_sys(DBGT_ERR, "invalid magic number in trailer: %08x, malloc tag = %d",
*chunkTrailer, chunkHeader->tag );
cleanup_all( -500079 );
}
copyLength = (length < chunkHeader->length) ? length : chunkHeader->length;
if (copyLength)
memcpy(result, memoryParameter, copyLength);
debugFree(memoryParameter, -300280);
}
return result;
}
void _debugFree(void *memoryParameter, int tag)
{
MAGIC_TRAILER_T *chunkTrailer;
struct chunkHeader *walker;
struct chunkHeader *previous;
struct chunkHeader *chunkHeader =
(struct chunkHeader *) (((unsigned char *) memoryParameter) - sizeof (struct chunkHeader));
if (chunkHeader->magicNumberHeader != MAGIC_NUMBER_HEADER)
{
dbgf_sys(DBGT_ERR,
"invalid magic number in header: %08x, malloc tag = %d, free tag = %d, malloc size = %d",
chunkHeader->magicNumberHeader, chunkHeader->tag, tag, chunkHeader->length);
cleanup_all( -500080 );
}
previous = NULL;
for (walker = chunkList; walker != NULL; walker = walker->next)
{
if (walker == chunkHeader)
break;
previous = walker;
}
if (walker == NULL)
{
dbg_sys(DBGT_ERR, "Double free detected, malloc tag = %d, free tag = %d malloc size = %d",
chunkHeader->tag, tag, chunkHeader->length );
cleanup_all( -500081 );
}
if (previous == NULL)
chunkList = walker->next;
else
previous->next = walker->next;
chunkTrailer = (MAGIC_TRAILER_T *) (((unsigned char *) memoryParameter) + chunkHeader->length);
if (*chunkTrailer != MAGIC_NUMBER_TRAILOR) {
dbgf_sys(DBGT_ERR, "invalid magic number in trailer: %08x, malloc tag = %d, free tag = %d, malloc size = %d",
*chunkTrailer, chunkHeader->tag, tag, chunkHeader->length);
cleanup_all( -500082 );
}
#ifdef MEMORY_USAGE
removeMemory( chunkHeader->tag, tag );
#endif //#ifdef MEMORY_USAGE
if (!terminating)
free(chunkHeader);
}
#else
void * _malloc( size_t length ) {
return malloc( length );
}
void * _calloc( size_t length ) {
void *mem = malloc( length );
memset( mem, 0, length);
return mem;
}
void * _realloc( void *mem, size_t length ) {
return realloc( mem, length );
}
void _free( void *mem ) {
if (!terminating)
free( mem );
}
#endif