-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmalloc.c
executable file
·1482 lines (1152 loc) · 29.7 KB
/
mmalloc.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mmalloc - dynamic memory checker
*
* Copyright (C) 2002,2003,2004 Mika Kuoppala <[email protected]>
*
* 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.
*
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <execinfo.h>
#include <sys/time.h>
#include <stdint.h>
#define MMALLOC_LIB
#include "mmalloc.h"
#include "mconfig.h"
#define MIN_BLOCK_SIZE_B 3
#define MIN_BLOCK_SIZE (1 << (MIN_BLOCK_SIZE_B))
#define MAX_BLOCK_SIZE_B 30
#define MAX_BLOCK_SIZE (1 << (MAX_BLOCK_SIZE_B))
#define HEAD_MAGIC 0xBBADF00D
#define TAIL_MAGIC 0xF00DBADD
/* Time resolution */
#define ONE_MICROSECOND 1
#define ONE_MILLISECOND (ONE_MICROSECOND * 1000)
#define ONE_SECOND (ONE_MILLISECOND * 1000)
#define get_block_head(ptr) (struct block_head*) ((unsigned char *)(ptr) - sizeof(struct block_head))
#define get_block_ptr(head) (unsigned char*) ((unsigned char *)(head) + sizeof(struct block_head))
#define get_head_from_region(number, reg) \
(struct block_head *) ((unsigned char *)reg->blocks + \
( (number) * (sizeof(struct block_head) + \
(reg)->block_size + \
sizeof(struct block_tail))))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
struct region {
struct region* next;
struct region* free_next;
size_t block_size;
unsigned long free_count;
unsigned char blocks[];
};
struct block_head {
struct region* myreg;
size_t mysize;
#if TRACE_DEPTH > 0
void *alloc_trace[TRACE_DEPTH];
void *free_trace[TRACE_DEPTH];
#endif
unsigned long state_counter;
unsigned long magic;
};
struct block_tail {
unsigned long magic;
unsigned long padding;
};
#if PERIOD_PRINT_STATISTICS > 0
struct region_chain_stats {
unsigned long total_blocks;
unsigned long blocks_alloc;
unsigned long block_size;
unsigned long bytes_alloced;
};
struct region_stats {
unsigned long long bytes;
unsigned long long total_alloc_count;
unsigned long current_alloc_count;
};
static struct region_stats region_stats[MAX_BLOCK_SIZE_B] = { { 0, 0, 0}, };
static unsigned long mmallocBytesAllocated = 0;
static unsigned long applicationBytesAllocated = 0;
static unsigned long lastApplicationBytesAllocated = 0;
static unsigned long lastAllocCount = 0;
static unsigned long lastCurrentAllocs = 0;
static unsigned long maxApplicationBytesAllocated = 0;
#endif
static struct region* all_regions[MAX_BLOCK_SIZE_B] = { 0, };
static struct region* free_regions[MAX_BLOCK_SIZE_B] = { 0, };
static unsigned long memPool1_Size = 0;
static unsigned char* memPool1 = 0;
static unsigned long memPool1_Allocated = 0;
static unsigned long memPool2_Size = 0;
static unsigned char* memPool2 = 0;
static unsigned long memPool2_Allocated = 0;
static unsigned long freq_check_all = 0;
static unsigned long freq_statistics = 0;
static unsigned long freq_check_region = 0;
static unsigned long mmalloc_disabled = 0;
static unsigned long alloc_count = 0;
static unsigned long check_count = 0;
static unsigned long not_mmalloc_alloc_count = 0;
static unsigned long stop_on_error = 0;
static uint8_t tailguard_data = 0;
static void merror(const struct block_head* head, const char *fmt, ...);
static void minfo(const char *fmt, ...);
static void print_block_data(const unsigned char *start, const unsigned char *end);
static unsigned long check_region(struct region *reg, int report_still_alloced, const unsigned long start_stamp);
static unsigned long mcheck_regions(const int report_still_alloced, const unsigned long start_count);
static void mmalloc_init(void);
static int mmalloc_close_fd(void);
static const char mmalloc_version_str[] = "0.24";
static unsigned long error_count = 0;
static FILE* output_fd = 0;
static struct timeval print_stats_timestamp;
void mmalloc_disable(void)
{
if(mmalloc_disabled == 0)
mmalloc_disabled = 1;
else
merror(NULL, "Disabling mmalloc while already disabled\n");
}
void mmalloc_enable(void)
{
if(mmalloc_disabled == 1)
mmalloc_disabled = 0;
else
merror(NULL, "Enabling mmalloc while already enabled\n");
}
#if PERIOD_PRINT_STATISTICS > 0
static void mdump_mmalloc_state(void);
static void report_stats(void)
{
int i;
unsigned long long totalAllocations = 0;
unsigned long long totalBytes = 0;
unsigned long long timeDiffInMicroseconds = 0;
unsigned long current_allocs = 0;
long bytesPerSec = 0;
long allocDeltaPerSec = 0;
unsigned long allocsPerSec = 0;
struct timeval current_time;
mdump_mmalloc_state();
current_allocs = mcheck_regions(0,0);
for(i = MIN_BLOCK_SIZE_B; i < MAX_BLOCK_SIZE_B; i++)
{
if(region_stats[i].total_alloc_count)
{
#ifdef PRINT_REGION_STATISTICS
minfo("Block(%d-%d) %llu allocations with %llu bytes, average %llu\n",
(1 << (i-1)), (1 << i),
region_stats[i].total_alloc_count, region_stats[i].bytes,
region_stats[i].bytes / region_stats[i].total_alloc_count);
#endif
totalAllocations += region_stats[i].total_alloc_count;
totalBytes += region_stats[i].bytes;
}
}
if(totalAllocations)
{
minfo("Current allocs %lu, total allocations %llu with %llu total bytes, average %llu\n",
current_allocs, totalAllocations, totalBytes, totalBytes/totalAllocations);
if(not_mmalloc_alloc_count)
minfo("Not mmalloc handler blocks %lu\n", not_mmalloc_alloc_count);
minfo("Allocated %lu, overhead %lu, total %lu, %3.2f%% free %3.2f%% eff\n",
applicationBytesAllocated, (mmallocBytesAllocated - applicationBytesAllocated),
memPool1_Allocated, 100.0 - (double)(memPool1_Allocated * 100.0)/(double)memPool1_Size,
(double)(applicationBytesAllocated * 100.0) /
((double)(memPool1_Allocated) + (double)memPool2_Allocated) );
if(memPool2_Size != 0)
minfo("Second pool in use %lu, %3.2f%% free\n",
memPool2_Allocated, 100.0 - (double)(memPool2_Allocated * 100.0)/(double)memPool2_Size);
}
gettimeofday(¤t_time, NULL);
timeDiffInMicroseconds = (current_time.tv_sec * ONE_SECOND + current_time.tv_usec) -
(print_stats_timestamp.tv_sec * ONE_SECOND + print_stats_timestamp.tv_usec);
bytesPerSec = (long)(applicationBytesAllocated - lastApplicationBytesAllocated) * (long long)(ONE_SECOND) /
(long long) timeDiffInMicroseconds;
allocsPerSec = (alloc_count - lastAllocCount) * (unsigned long long)(ONE_SECOND) / timeDiffInMicroseconds;
allocDeltaPerSec = (long)(current_allocs - lastCurrentAllocs) *
(long long)ONE_SECOND / (long long)timeDiffInMicroseconds;
minfo("Usage delta: %ld allocs, %ld bytes (%ld allocs/s %ld bytes/s) rate: %lu allocs/s\n",
current_allocs - lastCurrentAllocs,
applicationBytesAllocated - lastApplicationBytesAllocated,
allocDeltaPerSec,
bytesPerSec,
allocsPerSec);
minfo("Peak usage %ld application bytes, %ld total bytes\n",
maxApplicationBytesAllocated,
(memPool1_Allocated + memPool2_Allocated));
lastApplicationBytesAllocated = applicationBytesAllocated;
lastAllocCount = alloc_count;
lastCurrentAllocs = current_allocs;
print_stats_timestamp.tv_sec = current_time.tv_sec;
print_stats_timestamp.tv_usec = current_time.tv_usec;
}
#endif
static inline void mmalloc_check_state(struct region* reg)
{
if(freq_check_all && ((check_count % freq_check_all) == 0) )
{
mcheck_regions(0,0);
}
else if( reg && freq_check_region && ((check_count % freq_check_all) == 0) )
{
check_region(reg, 0, 0);
}
check_count++;
#if PERIOD_PRINT_STATISTICS > 0
if( freq_statistics && ((check_count % freq_statistics) == 0) )
report_stats();
#endif
}
static int is_initialized(void)
{
if(memPool1_Size)
return 1;
return 0;
}
static void* alloc_from_pool(unsigned long size)
{
void *p;
if( memPool1_Size == 0 && memPool2_Size == 0)
{
mmalloc_init();
}
if( (memPool1_Allocated + size) > memPool1_Size )
{
if( (memPool2_Allocated + size) > memPool2_Size )
{
merror(0, "Out of memory");
return 0;
}
p = (void *) ((unsigned char *)memPool2 + memPool2_Allocated);
memPool2_Allocated += size;
return p;
}
p = (void *) ((unsigned char*)memPool1 + memPool1_Allocated);
memPool1_Allocated += size;
return p;
}
static void print_stack_trace( void* const *a, const int size, FILE* out )
{
int i;
int stop = size;
char **strings = 0;
#ifdef BACKTRACE_SYMBOLS
strings = backtrace_symbols(a, size);
#endif
for(i = 0; i < size; i++)
{
void* addr = a[i];
if(addr == 0)
{
stop = i;
break;
}
}
for(i = stop-1; i >= 0; i--)
{
void* addr = a[i];
fprintf(out, "#%-3d [0x%lx]: ", (stop) - i, (unsigned long)addr);
if (strings)
fprintf(out, "%s", strings[i]);
fprintf(out, "\n");
}
}
static void print_traces( const struct block_head* head, FILE* out )
{
#if (TRACE_DEPTH > 0)
if( head->alloc_trace[0] )
{
fprintf(out, "\nAllocation Trace: \n");
print_stack_trace( head->alloc_trace, TRACE_DEPTH, output_fd);
}
if( head->free_trace[0] )
{
fprintf(out, "\nFree Trace: \n");
print_stack_trace( head->free_trace, TRACE_DEPTH, output_fd );
}
#endif
}
static int get_stack_trace(void **array, int size)
{
static void* trace[50];
int trace_len;
int i;
int store_len;
trace_len = backtrace(trace,
size + TRACE_START_OFFSET + TRACE_END_OFFSET );
store_len = trace_len - TRACE_START_OFFSET - TRACE_END_OFFSET;
if(store_len < 1)
return 0;
for(i = 0; (i < store_len) && (i < size); i++)
array[i] = trace[TRACE_START_OFFSET + i];
return i;
}
#ifdef I386_FIND_HIGH_BIT
static inline long find_high_bit(const unsigned long n)
{
register long ret;
asm ("bsr %1,%0" : "=g" (ret) : "r" (n));
return ret + 1;
}
#else
static inline long find_high_bit(unsigned long n)
{
unsigned long mask = 1UL << 31;
long bit = 32;
while (mask) {
if (n & mask)
return bit;
bit--;
mask >>= 1;
}
return 0;
}
#endif
#ifdef DEBUG_FREE_CHAINS
static void print_free_chain(const int size)
{
struct region* reg;
int entries = 0;
reg = free_regions[size];
while(reg)
{
printf("0x%x(%d free) -> ", (int)reg, reg->free_count);
fflush(stdout);
entries++;
assert( reg->free_next != reg );
reg = reg->free_next;
}
if(entries)
printf( " NULL (freechain with %d entries)\n", entries);
}
static void print_region_chain(struct region* first)
{
struct region* reg;
int entries = 0;
reg = first;
while(reg)
{
printf("0x%x(%d free) -> ", (int)reg, reg->free_count);
fflush(stdout);
entries++;
assert( reg->next != reg );
reg = reg->next;
}
if(entries)
printf( " NULL (with %d entries)\n", entries);
}
#endif
#ifdef CHECK_FREED_BLOCK_DATA
static inline void insert_freed_block_data(struct block_head* head)
{
memset(get_block_ptr(head), FREED_DATA_MAGIC, head->myreg->block_size);
}
#else
#define insert_freed_block_data(x)
#endif
static struct region* allocate_new_region(const size_t size, const unsigned int block_count)
{
struct region* region;
struct block_head* head;
struct block_tail* tail;
unsigned char* block_ptr;
unsigned long i;
const unsigned long block_size =
sizeof(struct block_head) + size + sizeof(struct block_tail);
region = (struct region *)alloc_from_pool(sizeof(struct region) + (block_size * block_count));
if(region == 0)
return 0;
region->next = 0;
region->free_next = 0;
region->block_size = size;
region->free_count = block_count;
for(i = 0; i < block_count; i++)
{
head = get_head_from_region(i, region);
head->mysize = (size_t)-1;
head->myreg = region;
head->magic = HEAD_MAGIC;
#if (TRACE_DEPTH > 0)
head->alloc_trace[0] = 0;
head->free_trace[0] = 0;
#endif
block_ptr = get_block_ptr(head);
tail = (struct block_tail *) ((unsigned char *)(block_ptr) + size);
assert(((unsigned char *)tail - (unsigned char *)block_ptr) == (int)size);
insert_freed_block_data(head);
tail->padding = TAIL_MAGIC;
tail->magic = TAIL_MAGIC;
}
return region;
}
#if TAILGUARD_BYTES > 0
static inline void insert_tailguard(const struct block_head* head)
{
uint8_t *s = (uint8_t *)(get_block_ptr(head) + head->mysize);
const uint8_t * const e = s + TAILGUARD_BYTES;
while (s < e)
*s++ = tailguard_data;
}
#else
#define insert_tailguard(x)
#endif
#ifdef CHECK_FREED_BLOCK_DATA
static inline void check_freed_block_integrity(struct region* reg, struct block_head* head)
{
const unsigned int block_size = reg->block_size;
const unsigned char* ptr = get_block_ptr(head);
unsigned int i;
for(i = 0; i < block_size; i++)
{
if(ptr[i] != FREED_DATA_MAGIC)
{
merror(head, "FREED BLOCK USED");
}
}
}
#else
#define check_freed_block_integrity(x, y)
#endif
#if TAILGUARD_BYTES > 0
static void print_tailguard_corruption(struct block_head* head);
static inline void check_tailguard(struct block_head* head)
{
const uint8_t *s = (uint8_t *)(get_block_ptr(head) + head->mysize);
const uint8_t * const e = s + TAILGUARD_BYTES;
while (s < e) {
if(*s++ != tailguard_data)
print_tailguard_corruption(head);
}
}
static void print_tailguard_corruption(struct block_head* head)
{
const unsigned char* start = 0;
const unsigned char* end = 0;
const unsigned char* tailstart = (unsigned char *)(get_block_ptr(head) + head->mysize);
int j = 0;
while(j < TAILGUARD_BYTES)
{
if(tailstart[j] != TAILGUARD_DATA)
{
if(start == 0)
start = &tailstart[j];
}
else
{
if(start)
{
if(end == 0)
end = &tailstart[j];
}
}
j++;
}
if(start)
{
if(end == 0)
end = tailstart + TAILGUARD_BYTES;
#ifdef DUMP_BLOCK_DATA
minfo("Block 0x%x (%d bytes) dump:\n", (unsigned long)get_block_ptr(head), head->mysize);
print_block_data(get_block_ptr(head), get_block_ptr(head) + head->mysize);
#endif
minfo("Block tail, 0x%x is valid tailguard:\n", TAILGUARD_DATA);
print_block_data(get_block_ptr(head) + head->mysize, get_block_ptr(head) + head->mysize + TAILGUARD_BYTES);
merror(head, "BLOCK TAIL OVERWRITE at offset %d, len %d",
start - tailstart, end - start);
}
}
#else
#define check_tailguard(x)
#endif
static int check_block(struct region* orgreg, struct block_head* head)
{
struct block_tail *tail = NULL;
unsigned char* ptr = get_block_ptr(head);
struct region* reg;
reg = head->myreg;
if(reg != orgreg)
merror(head, "BLOCK %p REGION CORRUPTED", ptr);
if(head->magic != HEAD_MAGIC)
merror(head, "BLOCK HEADMAGIC CORRUPTED" );
if(head->mysize == (size_t)-1)
{
check_freed_block_integrity(reg, head);
return -1;
}
if(head->mysize > reg->block_size)
merror(head, "Block has bigger size than region size");
check_tailguard(head);
insert_tailguard(head);
tail = (struct block_tail *) ((unsigned char *)(ptr) + reg->block_size);
if(tail->magic != TAIL_MAGIC)
merror(head, "BLOCK TAILMAGIC CORRUPTED");
return head->mysize;
}
static unsigned long check_region(struct region* reg, int report_still_alloced, const unsigned long start_stamp)
{
struct block_head* head;
unsigned long freecount = 0;
unsigned int i = 0;
for(i = 0; i < BLOCKS_IN_REGION; i++)
{
head = get_head_from_region(i, reg);
if(check_block(reg, head) == -1)
freecount++;
else if(report_still_alloced && (head->state_counter >= start_stamp))
{
merror(head, "BLOCK NOT FREED");
}
}
return BLOCKS_IN_REGION - freecount;
}
static unsigned long check_region_chain(struct region* reg, const int report_still_alloced, const int start_stamp)
{
unsigned long currently_allocated = 0;
if(reg == 0)
return 0;
while(reg)
{
currently_allocated += check_region(reg, report_still_alloced, start_stamp);
reg = reg->next;
}
return currently_allocated;
}
static inline struct block_head* find_free_block(struct region* reg)
{
unsigned int i = 0;
struct block_head* head;
while(i < BLOCKS_IN_REGION)
{
head = get_head_from_region(i, reg);
if(head->mysize == (size_t)-1)
{
return head;
}
i++;
}
return 0;
}
#if (TRACE_DEPTH > 0)
static inline void store_alloc_trace(struct block_head* head)
{
int stored;
stored = get_stack_trace( head->alloc_trace, TRACE_DEPTH);
if(stored < TRACE_DEPTH)
head->alloc_trace[stored] = 0;
head->free_trace[0] = 0;
}
#else
#define store_alloc_trace(x)
#endif
#if (TRACE_DEPTH > 0)
static inline void store_free_trace(struct block_head* head)
{
int stored;
stored = get_stack_trace( head->free_trace, TRACE_DEPTH);
if(stored < TRACE_DEPTH)
head->free_trace[stored] = 0;
}
#else
#define store_free_trace(x)
#endif
static inline struct block_head *
alloc_block_from_region(struct region* reg, const size_t size)
{
struct block_head *head;
head = find_free_block(reg);
if(head)
{
#if PERIOD_PRINT_STATISTICS > 0
struct region_stats *stats;
int sizeb;
#endif
#if TRACE_DEPTH > 0
store_alloc_trace(head);
#endif
#if PERIOD_PRINT_STATISTICS > 0
if( size < MIN_BLOCK_SIZE )
sizeb = MIN_BLOCK_SIZE_B;
else
sizeb = find_high_bit( size );
stats = ®ion_stats[sizeb];
stats->current_alloc_count++;
stats->total_alloc_count++;
stats->bytes += size;
applicationBytesAllocated += size;
if(applicationBytesAllocated > maxApplicationBytesAllocated)
{
maxApplicationBytesAllocated = applicationBytesAllocated;
}
mmallocBytesAllocated += reg->block_size;
#endif
head->state_counter = alloc_count++;
head->mysize = size;
insert_tailguard(head);
}
else
{
merror(0, "Couln't get free block from region %p\n", reg);
}
return head;
}
static inline void free_block(struct block_head* head)
{
struct region* reg;
reg = head->myreg;
check_block(reg, head);
if(head->mysize == (size_t)-1)
{
merror(head, "FREE ON ALREADY FREED BLOCK");
}
else
{
reg->free_count++;
if(reg->free_count > BLOCKS_IN_REGION)
{
merror(head, "Internal error, block has %d free blocks in %d blocks",
reg->free_count, BLOCKS_IN_REGION);
}
#if PERIOD_PRINT_STATISTICS > 0
applicationBytesAllocated -= head->mysize;
mmallocBytesAllocated -= reg->block_size;
#endif
if(reg->free_count == 1)
{
struct region** free_list;
const int bsize = find_high_bit((reg->block_size-1));
free_list = &free_regions[bsize];
/* assert(reg->free_next == 0); */
if(*free_list)
{
reg->free_next = *free_list;
}
*free_list = reg;
}
head->mysize = (size_t)-1;
insert_freed_block_data(head);
}
#if TRACE_DEPTH > 0
store_free_trace(head);
#endif
}
static inline struct region* find_block_region(const struct block_head* head)
{
if(head->magic != HEAD_MAGIC)
{
return 0;
}
return head->myreg;
}
static struct region* get_free_region(const size_t size)
{
struct region** free_list;
struct region* reg;
int sizeb;
if( size < MIN_BLOCK_SIZE )
sizeb = MIN_BLOCK_SIZE_B;
else
sizeb = find_high_bit( size );
free_list = &free_regions[sizeb];
if(*free_list == 0)
{
struct region** all_list;
reg = allocate_new_region( (1 << sizeb), BLOCKS_IN_REGION );
if(reg == 0)
return 0;
all_list = &all_regions[sizeb];
/* Insert first to all list */
if(*all_list)
{
reg->next = *all_list;
}
reg->free_next = 0;
*all_list = reg;
/* Insert first to free list */
*free_list = reg;
}
reg = *free_list;
assert( reg->free_count != 0 );
reg->free_count--;
if(reg->free_count == 0)
{
*free_list = (*free_list)->free_next;
reg->free_next = 0;
}
return reg;
}
#if (PERIOD_PRINT_STATISTICS > 0)
static int dump_region_chain(struct region_chain_stats *data,
struct region *reg)
{
struct block_head *head;
int region_count;
int i;
region_count = 0;
data->total_blocks = 0;
data->blocks_alloc = 0;
data->block_size = 0;
data->bytes_alloced = 0;
if(!reg)
return 0;
data->block_size = reg->block_size;
while(reg)
{
region_count++;
data->total_blocks += BLOCKS_IN_REGION;
for(i = 0; i < BLOCKS_IN_REGION; i++)
{
head = get_head_from_region(i, reg);
if(head->mysize == (size_t)-1)
continue;
data->blocks_alloc++;
data->bytes_alloced += head->mysize;
}
reg = reg->next;
}
return region_count;
}
#ifdef PRINT_REGION_STATISTICS
static void print_region_data(region_chain_stats_t* data, FILE* out)
{
fprintf(out, " Blocks: (%lu, allocated: %lu, free %lu)",
data->total_blocks, data->blocks_alloc, (data->total_blocks - data->blocks_alloc));
fprintf(out, " Bytes: (alloced %lu, free %lu) ",
data->bytes_alloced, (data->total_blocks - data->blocks_alloc) * data->block_size);
}
#endif
static void mdump_mmalloc_state(void)
{
struct region_chain_stats region_data;
int total_bytes = 0;
int total_allocs = 0;
int count;
int i;
minfo("\n *** MMALLOC STATISTICS ***\n");
for(i = MIN_BLOCK_SIZE_B; i < MAX_BLOCK_SIZE_B; i++) {
count = dump_region_chain(®ion_data, all_regions[i]);
if(count) {
total_bytes += region_data.bytes_alloced;
total_allocs += region_data.blocks_alloc;
#ifdef PRINT_REGION_STATISTICS
fprintf(out, "[%6d]: %-5d regions ", (1 << i), count);
print_region_data(®ion_data, out);
fprintf(out, "\n");
#endif
}
}
minfo(" MMALLOC has alloced %d bytes in %d allocs\n", total_bytes, total_allocs);
}
#endif
int mmalloc_set_output_file(const char* const filename)
{
FILE* f;
f = fopen(filename, "a");
if (!f)
return 0;
return mmalloc_set_output_fd(f);
}
int mmalloc_set_output_fd(FILE* fd)
{
int r;
r = mmalloc_close_fd();
output_fd = fd;
return r;
}
static int mmalloc_close_fd(void)
{
if(output_fd != stderr)
{
int r;
r = fclose(output_fd);
output_fd = 0;
return r;
}
return 0;
}