-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgc.cpp
796 lines (681 loc) · 27 KB
/
gc.cpp
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
// ****************************************************************************
// gc.cpp ELFE project
// ****************************************************************************
//
// File Description:
//
// Garbage collector and memory management
//
// Garbage collection in ELFE is based on reference counting.
// The GCPtr class does the reference counting.
// The rule is that as soon as you assign an object to a GCPtr,
// it becomes "tracked". Objects created during a cycle and not assigned
// to a GCPtr by the next cycle are an error, which is flagged
// in debug mode.
//
//
//
// ****************************************************************************
// This document is released under the GNU General Public License, with the
// following clarification and exception.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules,
// and to copy and distribute the resulting executable under terms of your
// choice, provided that you also meet, for each linked independent module,
// the terms and conditions of the license of that module. An independent
// module is a module which is not derived from or based on this library.
// If you modify this library, you may extend this exception to your version
// of the library, but you are not obliged to do so. If you do not wish to
// do so, delete this exception statement from your version.
//
// See http://www.gnu.org/copyleft/gpl.html and Matthew 25:22 for details
// (C) 1992-2010 Christophe de Dinechin <[email protected]>
// (C) 2010 Taodyne SAS
// ****************************************************************************
#include "gc.h"
#include "options.h"
#include "recorder.h"
#include "valgrind/memcheck.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
// Windows/MinGW (ancient): When getting in the way becomes an art form...
#if !defined(HAVE_POSIX_MEMALIGN) && defined(HAVE_MINGW_ALIGNED_MALLOC)
#include <malloc.h>
#endif // HAVE_POSIX_MEMALIGN
ELFE_BEGIN
// ============================================================================
//
// Allocator Base class
//
// ============================================================================
void *TypeAllocator::lowestAddress = (void *) ~0;
void *TypeAllocator::highestAddress = (void *) 0;
void *TypeAllocator::lowestAllocatorAddress = (void *) ~0;
void *TypeAllocator::highestAllocatorAddress = (void *) 0;
Atomic<uint> TypeAllocator::finalizing = 0;
// Identifier of the thread currently collecting if any
#define PTHREAD_NULL ((pthread_t) 0)
static pthread_t collecting = PTHREAD_NULL;
RECORDER_DEFINE(memory, 64, "Memory allocation and garbage collector");
TypeAllocator::TypeAllocator(kstring tn, uint os)
// ----------------------------------------------------------------------------
// Setup an empty allocator
// ----------------------------------------------------------------------------
: gc(NULL), name(tn), locked(0), lowestInUse(~0UL), highestInUse(0),
chunks(), freeList(NULL), toDelete(NULL),
available(0), freedCount(0),
chunkSize(1022), objectSize(os), alignedSize(os),
allocatedCount(0), scannedCount(0), collectedCount(0), totalCount(0)
{
record(memory, "New type allocator %p name '%s' object size %u",
this, tn, os);
// Make sure we align everything on Chunk boundaries
if ((alignedSize + sizeof (Chunk)) & CHUNKALIGN_MASK)
{
// Align total size up to 8-bytes boundaries
uint totalSize = alignedSize + sizeof(Chunk);
totalSize = (totalSize + CHUNKALIGN_MASK) & ~CHUNKALIGN_MASK;
alignedSize = totalSize - sizeof(Chunk);
}
// Use the address of the garbage collector as signature
gc = GarbageCollector::CreateSingleton();
// Register the allocator with the garbage collector
gc->Register(this);
// Make sure that we have the correct alignment
ELFE_ASSERT(this == ValidPointer(this));
// Update allocator addresses
if (lowestAllocatorAddress > this)
lowestAllocatorAddress = (void *) this;
char *highMark = (char *) this + sizeof(TypeAllocator);
if (highestAllocatorAddress < (void *) highMark)
highestAllocatorAddress = (void *) highMark;
VALGRIND_CREATE_MEMPOOL(this, 0, 0);
}
TypeAllocator::~TypeAllocator()
// ----------------------------------------------------------------------------
// Delete all the chunks we allocated
// ----------------------------------------------------------------------------
{
record(memory, "Destroy type allocator %p '%s'", this, this->name);
VALGRIND_DESTROY_MEMPOOL(this);
for (Chunks::iterator c = chunks.begin(); c != chunks.end(); c++)
free((void *) *c);
}
void *TypeAllocator::Allocate()
// ----------------------------------------------------------------------------
// Allocate a chunk of the given size
// ----------------------------------------------------------------------------
{
record(memory, "Allocate in '%s', free list %p",
this->name, freeList.Get());
Chunk_vp result;
do
{
result = freeList;
while (!result)
{
// Make sure only one thread allocates chunks
uint wasLocked = locked++;
if (wasLocked)
{
locked--;
result = freeList;
continue;
}
// Nothing free: allocate a big enough chunk
size_t itemSize = alignedSize + sizeof(Chunk);
size_t allocSize = (chunkSize + 1) * itemSize;
void *allocated = malloc(allocSize);
(void)VALGRIND_MAKE_MEM_NOACCESS(allocated, allocSize);
record(memory, "New chunk %p in '%s'", allocated, this->name);
char *chunkBase = (char *) allocated + alignedSize;
Chunk_vp last = (Chunk_vp) chunkBase;
Chunk_vp free = result;
for (uint i = 0; i < chunkSize; i++)
{
Chunk_vp ptr = (Chunk_vp) (chunkBase + i * itemSize);
VALGRIND_MAKE_MEM_UNDEFINED(&ptr->next,sizeof(ptr->next));
ptr->next = free;
free = ptr;
}
// Update the chunks list
chunks.push_back((Chunk *) allocated);
available += chunkSize;
if (lowestAddress > allocated)
lowestAddress = allocated;
char *highMark = (char *) allocated + (chunkSize+1) * itemSize;
if (highestAddress < (void *) highMark)
highestAddress = highMark;
// Update the freelist
while (!freeList.SetQ(result, free))
{
result = freeList;
last->next = result;
}
// Unlock the chunks
--locked;
// Read back the free list
result = freeList;
}
}
while (!freeList.SetQ(result, result->next));
VALGRIND_MAKE_MEM_UNDEFINED(result, sizeof(Chunk));
result->allocator = this;
result->bits |= IN_USE; // Mark it as in use for current collection
result->count = 0;
UpdateInUseRange(result);
allocatedCount++;
if (--available < chunkSize * 0.9)
gc->MustRun();
void *ret = (void *) &result[1];
VALGRIND_MEMPOOL_ALLOC(this, ret, objectSize);
record(memory, "Allocated %p from %s", ret, name);
return ret;
}
void TypeAllocator::Delete(void *ptr)
// ----------------------------------------------------------------------------
// Free a chunk of the given size
// ----------------------------------------------------------------------------
{
record(memory, "Delete %p in '%s'", ptr, this->name);
if (!ptr)
return;
Chunk_vp chunk = (Chunk_vp) ptr - 1;
ELFE_ASSERT(IsGarbageCollected(ptr) &&
"Deleted pointer not managed by GC");
ELFE_ASSERT(IsAllocated(ptr) &&
"Deleted GC pointer that was already freed");
ELFE_ASSERT(!chunk->count &&
"Deleted pointer has live references");
// Put the pointer back on the free list
do
{
chunk->next = freeList;
}
while (!freeList.SetQ(chunk->next, chunk));
available++;
freedCount++;
#ifdef DEBUG
// Scrub all the pointers
uint32 *base = (uint32 *) ptr;
uint32 *last = (uint32 *) (((char *) ptr) + alignedSize);
VALGRIND_MAKE_MEM_UNDEFINED(ptr, alignedSize);
for (uint *p = base; p < last; p++)
*p = 0xDeadBeef;
#endif
VALGRIND_MEMPOOL_FREE(this, ptr);
}
void TypeAllocator::Finalize(void *ptr)
// ----------------------------------------------------------------------------
// We should never reach this one
// ----------------------------------------------------------------------------
{
std::cerr << "No finalizer installed for " << ptr << "\n";
ELFE_ASSERT(!"No finalizer installed");
}
void TypeAllocator::ScheduleDelete(TypeAllocator::Chunk_vp ptr)
// ----------------------------------------------------------------------------
// Delete now if possible, or record that we will need to delete it later
// ----------------------------------------------------------------------------
{
if (ptr->bits & IN_USE)
{
UpdateInUseRange(ptr);
}
else
{
ELFE_ASSERT(ptr->count == 0 && "Deleting referenced object");
TypeAllocator *allocator = ValidPointer(ptr->allocator);
if (allocator->finalizing)
{
// Put it on the to-delete list to avoid deep recursion
LinkedListInsert(allocator->toDelete, ptr);
}
else
{
// Delete current object immediately
allocator->Finalize((void *) (ptr + 1));
// Delete the children put on the toDelete list
GarbageCollector::Sweep();
}
}
}
bool TypeAllocator::CheckLeakedPointers()
// ----------------------------------------------------------------------------
// Check if any pointers were allocated and not captured between safe points
// ----------------------------------------------------------------------------
{
record(memory, "CheckLeaks in '%s'", name);
char *lo = (char *) lowestInUse.Get();
char *hi = (char *) highestInUse.Get();
lowestInUse.Set((uintptr_t) lo, ~0UL);
highestInUse.Set((uintptr_t) hi, 0UL);
uint collected = 0;
totalCount = 0;
for (Chunks::iterator chk = chunks.begin(); chk != chunks.end(); chk++)
{
char *chunkBase = (char *) *chk + alignedSize;
size_t itemSize = alignedSize + sizeof(Chunk);
char *chunkEnd = chunkBase + itemSize * chunkSize;
totalCount += chunkSize;
if (chunkBase <= hi && chunkEnd >= lo)
{
char *start = (char *) chunkBase;
char *end = (char *) chunkEnd;
if (start < lo)
start = lo;
if (end > hi)
end = hi;
scannedCount += (end - start) / itemSize;
for (char *addr = start; addr < end; addr += itemSize)
{
Chunk_vp ptr = (Chunk_vp) addr;
if (AllocatorPointer(ptr->allocator) == this)
{
Atomic<uintptr_t>::And(ptr->bits, ~(uintptr_t) IN_USE);
if (!ptr->count)
{
// It is dead, Jim
Finalize((void *) (ptr+1));
collected++;
}
}
}
}
}
collectedCount += collected;
record(memory, "CheckLeaks in '%s' done, scanned %u, collected %u",
name, scannedCount, collected);
return collected;
}
bool TypeAllocator::Sweep()
// ----------------------------------------------------------------------------
// Remove all the things that we have pushed on the toDelete list
// ----------------------------------------------------------------------------
{
record(memory, "Sweep '%s'", name);
bool result = false;
while (toDelete)
{
Chunk_vp next = LinkedListPopFront(toDelete);
next->allocator = this;
Finalize((void *) (next+1));
result = true;
}
record(memory, "Swept '%s' %s objects deleted",
name, result ? "with" : "without");
return result;
}
void TypeAllocator::ResetStatistics()
// ----------------------------------------------------------------------------
// Reset the statistics counters
// ----------------------------------------------------------------------------
{
freedCount -= freedCount;
allocatedCount = 0;
scannedCount = 0;
collectedCount = 0;
totalCount = 0;
}
void *TypeAllocator::operator new(size_t size)
// ----------------------------------------------------------------------------
// Force 16-byte alignment not guaranteed by regular operator new
// ----------------------------------------------------------------------------
{
void *result = NULL;
#if defined(HAVE_POSIX_MEMALIGN)
// Real operating systems
if (posix_memalign(&result, PTR_MASK+1, size))
throw std::bad_alloc();
#elif defined(HAVE_MINGW_ALIGNED_MALLOC)
// Ancient versions of MinGW
result = __mingw_aligned_malloc(size, PTR_MASK+1);
if (!result)
throw std::bad_alloc();
#else // don't align
#warning "Unknown platfom - No alignment"
result = malloc(size);
if (!result)
throw std::bad_alloc();
#endif //
return result;
}
void TypeAllocator::operator delete(void *ptr)
// ----------------------------------------------------------------------------
// Matching deallocation
// ----------------------------------------------------------------------------
{
#if defined(HAVE_POSIX_MEMALIGN)
// Normal system
free(ptr);
#elif defined(HAVE_MINGW_ALIGNED_MALLOC)
// Brain damaged OS?
__mingw_aligned_free(ptr);
#else
// Assume limited brain-damage
free(ptr);
#endif // WINDOWS vs. rest of the world
}
bool TypeAllocator::CanDelete(void *obj)
// ----------------------------------------------------------------------------
// Ask all the listeners if it's OK to delete the object
// ----------------------------------------------------------------------------
{
bool result = true;
Listeners::iterator i;
for (i = listeners.begin(); i != listeners.end(); i++)
if (!(*i)->CanDelete(obj))
result = false;
record(memory, "%s delete %p in '%s'",
result ? "Can" : "Cannot", obj, name);
return result;
}
// ============================================================================
//
// Garbage Collector class
//
// ============================================================================
GarbageCollector::GarbageCollector()
// ----------------------------------------------------------------------------
// Create the garbage collector
// ----------------------------------------------------------------------------
: mustRun(false), running(false)
{}
GarbageCollector::~GarbageCollector()
// ----------------------------------------------------------------------------
// Destroy the garbage collector
// ----------------------------------------------------------------------------
{
MustRun();
Collect();
Collect();
Allocators::iterator i;
for (i = allocators.begin(); i != allocators.end(); i++)
delete *i;
// Make sure that destructors down the line won't try something silly
TypeAllocator::lowestAddress = (void *) ~0;
TypeAllocator::highestAddress = (void *) 0;
TypeAllocator::lowestAllocatorAddress = (void *) ~0;
TypeAllocator::highestAllocatorAddress = (void *) 0;
}
void GarbageCollector::Register(TypeAllocator *allocator)
// ----------------------------------------------------------------------------
// Record each individual allocator
// ----------------------------------------------------------------------------
{
allocators.push_back(allocator);
}
bool GarbageCollector::Sweep()
// ----------------------------------------------------------------------------
// Cleanup all the pending deletions
// ----------------------------------------------------------------------------
{
bool purging = false;
Allocators &allocators = gc->allocators;
for (Allocators::iterator a=allocators.begin(); a!=allocators.end(); a++)
purging |= (*a)->Sweep();
return purging;
}
bool GarbageCollector::Collect()
// ----------------------------------------------------------------------------
// Run garbage collection on all the allocators we own
// ----------------------------------------------------------------------------
{
pthread_t self = pthread_self();
// If we get here, we are at a safe point.
// Only one thread enters collecting, the others spin and wait
if (Atomic<pthread_t>::SetQ(collecting, PTHREAD_NULL, self))
{
record(memory, "Garbage collection in thread %p", self);
Allocators::iterator a;
Listeners listeners;
Listeners::iterator l;
// Build the listeners from all allocators
for (a = allocators.begin(); a != allocators.end(); a++)
for (l = (*a)->listeners.begin(); l != (*a)->listeners.end(); l++)
listeners.insert(*l);
// Notify all the listeners that we begin a collection
for (l = listeners.begin(); l != listeners.end(); l++)
(*l)->BeginCollection();
// Cleanup pending purges to maximize the effect of garbage collection
bool sweeping = true;
while (sweeping)
{
// Check if any object was allocated and not captured at this stage
for (a = allocators.begin(); a != allocators.end(); a++)
(*a)->CheckLeakedPointers();
sweeping = Sweep();
}
// Notify all the listeners that we completed the collection
for (l = listeners.begin(); l != listeners.end(); l++)
(*l)->EndCollection();
// Print statistics (inside lock, to increase race pressure)
IFTRACE(memory)
PrintStatistics();
// We are done, mark it so
mustRun &= 0U;
if (!Atomic<pthread_t>::SetQ(collecting, self, PTHREAD_NULL))
{
ELFE_ASSERT(!"Someone else stole the collection lock?");
}
record(memory, "Finished garbage collection in thread %p", self);
return true;
}
record(memory, "Garbage collection for thread %p was blocked", self);
return false;
}
void GarbageCollector::PrintStatistics()
// ----------------------------------------------------------------------------
// Print statistics about collection
// ----------------------------------------------------------------------------
{
uint tot = 0, alloc = 0, avail = 0, freed = 0, scan = 0, collect = 0;
printf("%24s %8s %8s %8s %8s %8s %8s\n",
"NAME", "TOTAL", "AVAIL", "ALLOC", "FREED", "SCANNED", "COLLECT");
Allocators::iterator a;
for (a = allocators.begin(); a != allocators.end(); a++)
{
TypeAllocator *ta = *a;
printf("%24s %8u %8u %8u %8u %8u %8u\n",
ta->name, ta->totalCount,
ta->available.Get(), ta->allocatedCount,
ta->freedCount.Get(), ta->scannedCount, ta->collectedCount);
tot += ta->totalCount * ta->alignedSize;
alloc += ta->allocatedCount * ta->alignedSize;
avail += ta->available * ta->alignedSize;
freed += ta->freedCount * ta->alignedSize;
scan += ta->scannedCount * ta->alignedSize;
collect += ta->collectedCount * ta->alignedSize;
ta->ResetStatistics();
}
printf("%24s %8s %8s %8s %8s %8s %8s\n",
"=====", "=====", "=====", "=====", "=====", "=====", "=====");
printf("%24s %7uK %7uK %7uK %7uK %7uK %7uK\n",
"Kilobytes",
tot >> 10, avail >> 10, alloc >> 10,
freed >> 10, scan >> 10, collect >> 10);
}
void GarbageCollector::Statistics(uint &total,
uint &allocated, uint &available,
uint &freed, uint &scanned, uint &collected)
// ----------------------------------------------------------------------------
// Get statistics about garbage collections
// ----------------------------------------------------------------------------
{
uint tot = 0, alloc = 0, avail = 0, free = 0, scan = 0, collect = 0;
std::vector<TypeAllocator *>::iterator a;
for (a = allocators.begin(); a != allocators.end(); a++)
{
TypeAllocator *ta = *a;
tot += ta->totalCount * ta->alignedSize;
alloc += ta->allocatedCount * ta->alignedSize;
avail += ta->available * ta->alignedSize;
free += ta->freedCount * ta->alignedSize;
scan += ta->scannedCount * ta->alignedSize;
collect += ta->collectedCount * ta->alignedSize;
ta->ResetStatistics();
}
total = tot;
allocated = alloc;
available = avail;
freed = free;
collected = collect;
}
GarbageCollector *GarbageCollector::gc = NULL;
GarbageCollector *GarbageCollector::CreateSingleton()
// ----------------------------------------------------------------------------
// Return the garbage collector
// ----------------------------------------------------------------------------
{
if (!gc)
gc = new GarbageCollector;
return gc;
}
void GarbageCollector::Delete()
// ----------------------------------------------------------------------------
// Delete the garbage collector
// ----------------------------------------------------------------------------
{
if (gc)
{
delete gc;
gc = NULL;
}
}
ELFE_END
void debuggc(void *ptr)
// ----------------------------------------------------------------------------
// Show allocation information about the given pointer
// ----------------------------------------------------------------------------
{
using namespace ELFE;
if (TypeAllocator::IsGarbageCollected(ptr))
{
typedef TypeAllocator::Chunk Chunk;
typedef TypeAllocator::Chunks Chunks;
typedef TypeAllocator::Chunk_vp Chunk_vp;
typedef TypeAllocator TA;
Chunk_vp chunk = (Chunk_vp) ptr - 1;
if ((uintptr_t) (void *) chunk & TA::CHUNKALIGN_MASK)
{
std::cerr << "WARNING: Pointer " << ptr << " is not aligned\n";
chunk = (Chunk_vp)
(((uintptr_t) (void *) chunk) & ~TA::CHUNKALIGN_MASK);
std::cerr << " Using " << chunk << " as chunk\n";
}
uintptr_t bits = chunk->bits;
uintptr_t aligned = bits & ~TA::PTR_MASK;
std::cerr << "Allocator bits: " << std::hex << bits << std::dec
<< " count=" << chunk->count << "\n";
GarbageCollector *gc = GarbageCollector::GC();
TA *alloc = (TA *) aligned;
bool allocated = alloc->gc == gc;
if (allocated)
{
std::cerr << "Allocated in " << alloc
<< " (" << alloc->name << ")"
<< " free=" << alloc->available
<< " chunks=" << alloc->chunks.size()
<< " size=" << alloc->chunkSize
<< " item=" << alloc->objectSize
<< " (" << alloc->alignedSize << ")"
<< "\n";
}
// Need to walk the GC to see where we belong
std::vector<TA *>::iterator a;
uint found = 0;
for (a = gc->allocators.begin(); a != gc->allocators.end(); a++)
{
Chunks::iterator c;
alloc = *a;
uint itemBytes = alloc->alignedSize + sizeof(Chunk);
uint chunkBytes = (alloc->chunkSize+1) * itemBytes;
uint chunkIndex = 0;
for (c = alloc->chunks.begin(); c != alloc->chunks.end(); c++)
{
char *start = (char *) *c;
char *end = start + chunkBytes;
char *base = start + alloc->alignedSize + sizeof(Chunk);
chunkIndex++;
if (ptr >= start && ptr <= end)
{
if (!allocated)
std::cerr << "Free item in "
<< alloc << " (" << alloc->name << ") "
<< "chunk #" << chunkIndex << " ";
uint freeIndex = 0;
Chunk_vp prev = NULL;
for (Chunk_vp f = alloc->freeList; f; f = f->next)
{
freeIndex++;
if (f == chunk)
{
std::cerr << " freelist #" << freeIndex
<< " after " << prev << " ";
found++;
}
prev = f;
}
freeIndex = 0;
prev = NULL;
for (Chunk_vp f = alloc->toDelete; f; f = f->next)
{
freeIndex++;
if (f == chunk)
{
std::cerr << " to-delete #" << freeIndex
<< " after " << prev << " ";
found++;
}
prev = f;
}
if (!allocated || found)
std::cerr << "\n";
}
for (char *addr = start; addr < end; addr += sizeof (void *))
{
void **ref = (void **) addr;
if (*ref == ptr)
{
uint diff = addr-base;
uint index = diff / itemBytes;
char *obj = base + index * itemBytes;
uint offset = addr - obj;
std::cerr << "Referenced from " << ref
<< " at offset " << offset
<< " in item #" << index
<< " at addr " << (void *) obj
<< "\n";
}
}
}
}
// Check how many times we found the item
if (allocated)
{
if (found)
std::cerr << "*** Allocated item found " << found
<< " time(s) in free list (DOUBLE PLUS UNGOOD)\n";
}
else if (found != 1)
{
if (!found)
std::cerr << "*** Pointer probably not allocated by us\n";
else
std::cerr << "*** Damaged free list, item found " << found
<< " times (MOSTLY UNFORTUNATE)\n";
}
}
else
{
std::cerr << "Pointer " << ptr << " is not dynamically allocated\n";
}
}