-
Notifications
You must be signed in to change notification settings - Fork 16
/
stdlib_free.c
596 lines (463 loc) · 12 KB
/
stdlib_free.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* $Id: stdlib_free.c,v 1.13 2006-01-08 12:04:25 obarthel Exp $
*
* :ts=4
*
* Portable ISO 'C' (1994) runtime library for the Amiga computer
* Copyright (c) 2002-2015 by Olaf Barthel <obarthel (at) gmx.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*#define DEBUG*/
#ifndef _STDLIB_HEADERS_H
#include "stdlib_headers.h"
#endif /* _STDLIB_HEADERS_H */
/****************************************************************************/
#ifndef _STDLIB_MEMORY_H
#include "stdlib_memory.h"
#endif /* _STDLIB_MEMORY_H */
/****************************************************************************/
#undef free
#undef __free
/****************************************************************************/
#ifdef __MEM_DEBUG
/****************************************************************************/
STATIC char
get_hex_char(int n)
{
char result;
if(0 <= n && n <= 9)
result = n + '0';
else
result = n + 'A' - 10;
return(result);
}
STATIC VOID
int_to_hex(unsigned long v,char * buffer,int min_digits)
{
int i,j,c;
i = 0;
do
{
c = (v % 16);
v = v / 16;
buffer[i++] = get_hex_char(c);
}
while(v > 0);
while(i < min_digits)
buffer[i++] = '0';
for(j = 0 ; j < i / 2 ; j++)
{
c = buffer[i - 1 - j];
buffer[i - 1 - j] = buffer[j];
buffer[j] = c;
}
buffer[i] = '\0';
}
STATIC VOID
dump_memory(unsigned char * m,int size,int ignore)
{
const int mod = 20;
int position,i,c;
char buffer[120];
char hex[10];
buffer[0] = '\0';
for(i = 0 ; i < size ; i++)
{
position = i % mod;
if(position == 0)
{
if(buffer[0] != '\0')
{
int len = sizeof(buffer)-1;
while(len > 0 && buffer[len-1] == ' ')
len--;
buffer[len] = '\0';
kprintf("[%s] %s\n",__program_name,buffer);
}
memset(buffer,' ',sizeof(buffer)-1);
int_to_hex((unsigned long)&m[i],hex,8);
memmove(buffer,hex,8);
hex[9] = ':';
}
if(m[i] != ignore)
{
buffer[10 + 2 * position + 0] = get_hex_char(m[i] >> 4);
buffer[10 + 2 * position + 1] = get_hex_char(m[i] & 15);
c = m[i];
if(c < ' ' || (c >= 127 && c <= 160))
c = '.';
buffer[10 + 2 * mod + 1 + position] = c;
}
else
{
buffer[10 + 2 * position + 0] = '_';
buffer[10 + 2 * position + 1] = '_';
}
}
if(buffer[0] != '\0')
{
int len = sizeof(buffer)-1;
while(len > 0 && buffer[len-1] == ' ')
len--;
buffer[len] = '\0';
kprintf("[%s] %s\n",__program_name,buffer);
}
}
/****************************************************************************/
STATIC VOID
check_memory_node(struct MemoryNode * mn,const char * file,int line)
{
ULONG size = GET_MN_SIZE(mn);
unsigned char * head = (unsigned char *)(mn + 1);
unsigned char * body = head + MALLOC_HEAD_SIZE;
unsigned char * tail = body + size;
int max_head_damage = 0;
int max_tail_damage = 0;
int max_body_damage = 0;
int i;
for(i = 1 ; i <= MALLOC_HEAD_SIZE ; i++)
{
if(head[MALLOC_HEAD_SIZE - i] != MALLOC_HEAD_FILL)
max_head_damage = i;
}
if(max_head_damage > 0)
{
kprintf("[%s] ",__program_name);
if(file != NULL)
kprintf("%s:%ld:",file,line);
kprintf("At least %ld bytes were damaged in front of allocation 0x%08lx..0x%08lx, size = %ld",
max_head_damage,
body,body + size - 1,size);
if(mn->mn_File != NULL)
kprintf(", allocated at %s:%ld",mn->mn_File,mn->mn_Line);
kprintf("\n");
dump_memory(head,MALLOC_HEAD_SIZE,MALLOC_HEAD_FILL);
}
for(i = 0 ; i < MALLOC_TAIL_SIZE ; i++)
{
if(tail[i] != MALLOC_TAIL_FILL)
max_tail_damage = i+1;
}
if(max_tail_damage > 0)
{
kprintf("[%s] ",__program_name);
if(file != NULL)
kprintf("%s:%ld:",file,line);
kprintf("At least %ld bytes were damaged behind allocation 0x%08lx..0x%08lx, size = %ld (with damage = %ld)",
max_tail_damage,
body,body + size - 1,
size,size+max_tail_damage);
if(mn->mn_File != NULL)
kprintf(", allocated at %s:%ld",mn->mn_File,mn->mn_Line);
kprintf("\n");
dump_memory(tail,MALLOC_TAIL_SIZE,MALLOC_TAIL_FILL);
}
if(mn->mn_AlreadyFree)
{
ULONG j;
for(j = 0 ; j < size ; j++)
{
if(body[j] != MALLOC_FREE_FILL)
max_body_damage = j+1;
}
if(max_body_damage > 0)
{
kprintf("[%s] ",__program_name);
if(file != NULL)
kprintf("%s:%ld:",file,line);
kprintf("At least %ld bytes were damaged in freed allocation 0x%08lx..0x%08lx, size = %ld",
max_body_damage,
body,body + size - 1,size);
if(mn->mn_File != NULL)
kprintf(", allocated at %s:%ld",mn->mn_File,mn->mn_Line);
kprintf("\n");
dump_memory(body,size,MALLOC_FREE_FILL);
}
}
}
/****************************************************************************/
void
__check_memory_allocations(const char * file,int line)
{
struct MemoryNode * mn;
__memory_lock();
for(mn = (struct MemoryNode *)__memory_list.mlh_Head ;
mn->mn_MinNode.mln_Succ != NULL ;
mn = (struct MemoryNode *)mn->mn_MinNode.mln_Succ)
{
check_memory_node(mn,file,line);
}
__memory_unlock();
}
/****************************************************************************/
struct MemoryNode *
__find_memory_node(void * address)
{
struct MemoryNode * result;
assert( address != NULL );
__memory_lock();
#if defined(__USE_MEM_TREES)
{
result = __red_black_tree_find(&__memory_tree,address);
}
#else
{
struct MemoryNode * mn;
result = NULL;
for(mn = (struct MemoryNode *)__memory_list.mlh_Head ;
mn->mn_MinNode.mln_Succ != NULL ;
mn = (struct MemoryNode *)mn->mn_MinNode.mln_Succ)
{
if(address == mn->mn_Allocation)
{
result = mn;
break;
}
}
}
#endif /* __USE_MEM_TREES */
__memory_unlock();
return(result);
}
/****************************************************************************/
#else
/****************************************************************************/
struct MemoryNode *
__find_memory_node(void * address)
{
struct MemoryNode * result;
assert( address != NULL );
result = &((struct MemoryNode *)address)[-1];
return(result);
}
/****************************************************************************/
#endif /* __MEM_DEBUG */
/****************************************************************************/
STATIC VOID
remove_and_free_memory_node(struct MemoryNode * mn)
{
size_t allocation_size;
assert( mn != NULL );
__memory_lock();
#if defined(__MEM_DEBUG)
{
Remove((struct Node *)mn);
#if defined(__USE_MEM_TREES)
{
__red_black_tree_remove(&__memory_tree,mn);
}
#endif /* __USE_MEM_TREES */
allocation_size = sizeof(*mn) + MALLOC_HEAD_SIZE + GET_MN_SIZE(mn) + MALLOC_TAIL_SIZE;
assert( allocation_size == mn->mn_AllocationSize );
memset(mn,MALLOC_FREE_FILL,allocation_size);
}
#else
{
allocation_size = sizeof(*mn) + GET_MN_SIZE(mn);
}
#endif /* __MEM_DEBUG */
#if defined(__USE_SLAB_ALLOCATOR)
{
/* Are we using the slab allocator? */
if(__slab_data.sd_InUse)
{
__slab_free(mn,allocation_size);
}
else
{
if(__memory_pool != NULL)
{
PROFILE_OFF();
FreePooled(__memory_pool,mn,allocation_size);
PROFILE_ON();
}
else
{
#if defined(__MEM_DEBUG)
{
PROFILE_OFF();
FreeMem(mn,allocation_size);
PROFILE_ON();
}
#else
{
struct MinNode * mln = (struct MinNode *)mn;
mln--;
Remove((struct Node *)mln);
PROFILE_OFF();
FreeMem(mln,sizeof(*mln) + allocation_size);
PROFILE_ON();
}
#endif /* __MEM_DEBUG */
}
}
}
#else
{
if(__memory_pool != NULL)
{
PROFILE_OFF();
FreePooled(__memory_pool,mn,allocation_size);
PROFILE_ON();
}
else
{
#if defined(__MEM_DEBUG)
{
PROFILE_OFF();
FreeMem(mn,allocation_size);
PROFILE_ON();
}
#else
{
struct MinNode * mln = (struct MinNode *)mn;
mln--;
Remove((struct Node *)mln);
PROFILE_OFF();
FreeMem(mln,sizeof(*mln) + allocation_size);
PROFILE_ON();
}
#endif /* __MEM_DEBUG */
}
}
#endif /* __USE_SLAB_ALLOCATOR */
__current_memory_allocated -= allocation_size;
__current_num_memory_chunks_allocated--;
__memory_unlock();
}
/****************************************************************************/
void
__free_memory_node(struct MemoryNode * mn,const char * UNUSED file,int UNUSED line)
{
assert(mn != NULL);
#ifdef __MEM_DEBUG
{
ULONG size = GET_MN_SIZE(mn);
check_memory_node(mn,file,line);
if(NOT mn->mn_AlreadyFree)
{
#ifdef __MEM_DEBUG_LOG
{
kprintf("[%s] - %10ld 0x%08lx [",__program_name,size,mn->mn_Allocation);
if(mn->mn_File != NULL)
kprintf("allocated at %s:%ld, ",mn->mn_File,mn->mn_Line);
kprintf("freed at %s:%ld]\n",file,line);
}
#endif /* __MEM_DEBUG_LOG */
if(__never_free)
{
mn->mn_AlreadyFree = TRUE;
mn->mn_FreeFile = (char *)file;
mn->mn_FreeLine = line;
memset(mn->mn_Allocation,MALLOC_FREE_FILL,size);
}
else
{
remove_and_free_memory_node(mn);
}
}
else
{
#ifdef __MEM_DEBUG_LOG
{
kprintf("[%s] - %10ld 0x%08lx [",__program_name,size,mn->mn_Allocation);
kprintf("FAILED]\n");
}
#endif /* __MEM_DEBUG_LOG */
kprintf("[%s] %s:%ld:Allocation at address 0x%08lx, size %ld",
__program_name,file,line,mn->mn_Allocation,size);
if(mn->mn_File != NULL)
kprintf(", allocated at %s:%ld",mn->mn_File,mn->mn_Line);
kprintf(", has already been freed at %s:%ld.\n",mn->mn_FreeFile,mn->mn_FreeLine);
}
}
#else
{
remove_and_free_memory_node(mn);
}
#endif /* __MEM_DEBUG */
}
/****************************************************************************/
VOID
__free_memory(void * ptr,BOOL force,const char * file,int line)
{
struct MemoryNode * mn;
assert(ptr != NULL);
SHOWPOINTER(ptr);
SHOWVALUE(force);
#ifdef __MEM_DEBUG
{
/*if((rand() % 16) == 0)
__check_memory_allocations(file,line);*/
}
#endif /* __MEM_DEBUG */
mn = __find_memory_node(ptr);
#ifdef __MEM_DEBUG
{
if(mn != NULL)
{
if(force || FLAG_IS_CLEAR(mn->mn_Size, MN_SIZE_NEVERFREE))
__free_memory_node(mn,file,line);
}
else
{
#ifdef __MEM_DEBUG_LOG
{
kprintf("[%s] - %10ld 0x%08lx [",__program_name,0,ptr);
kprintf("FAILED]\n");
}
#endif /* __MEM_DEBUG_LOG */
kprintf("[%s] %s:%ld:Address for free(0x%08lx) not known.\n",__program_name,file,line,ptr);
D(("memory allocation at 0x%08lx could not be freed",ptr));
}
}
#else
{
assert( mn != NULL );
SHOWVALUE(mn->mn_Size);
if(mn != NULL && (force || FLAG_IS_CLEAR(mn->mn_Size, MN_SIZE_NEVERFREE)))
__free_memory_node(mn,file,line);
}
#endif /* __MEM_DEBUG */
}
/****************************************************************************/
__static void
__free(void * ptr,const char * file,int line)
{
__memory_lock();
/* Try to get rid of now unused memory. */
if(__alloca_cleanup != NULL)
(*__alloca_cleanup)(file,line);
__memory_unlock();
if(ptr != NULL)
__free_memory(ptr,FALSE,file,line);
}
/****************************************************************************/
void
free(void * ptr)
{
__free(ptr,NULL,0);
}