-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGC.cpp
executable file
·719 lines (630 loc) · 22.1 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
/******************************************************************************
File: GC.cpp
Description:
Object Memory management class garbage collection routines
N.B. Some functions are inlined in this module as they are auto-inlined by
the compiler anyway, and prepending the qualifier tells the compiler that
it does not need an out-of-line copy for calls from outside the module (there
aren't any) thus saving a little code space for these private functions.
******************************************************************************/
#include "Ist.h"
#pragma code_seg(GC_SEG)
#include "ObjMem.h"
#include "Interprt.h"
// Smalltalk classes
#include "STBehavior.h" // We need to check class flags such as indexability, etc,
#include "STArray.h" // VMPointers (roots) are stored in an Array
#include "STProcess.h"
#define _CRTBLD
#include "winheap.h"
#undef _CRTBLD
// The pointers in const space
extern VMPointers _Pointers;
#ifdef _DEBUG
#define VERBOSEGC
static bool ignoreRefCountErrors = true; // JGFoster edited to get past error
#endif
#ifdef VERBOSEGC
#pragma warning (disable : 4786)
#include <yvals.h>
#undef _HAS_EXCEPTIONS
#include <map>
typedef std::map<BehaviorOTE*, int> MAPCLASSOTE2INT;
#endif
enum { NoWeakMask = 0, GCNoWeakness = 1 };
BYTE ObjectMemory::WeaknessMask = static_cast<BYTE>(OTEFlags::WeakMask);
void ObjectMemory::ClearGCInfo()
{
}
///////////////////////////////////////////////////////////////////////////////
inline Oop ObjectMemory::corpsePointer()
{
return _Pointers.Corpse;
}
void ObjectMemory::MarkObjectsAccessibleFromRoot(OTE* rootOTE)
{
BYTE curMark = *reinterpret_cast<BYTE*>(&m_spaceOTEBits[OTEFlags::NormalSpace]);
if ((rootOTE->m_ubFlags ^ curMark) & OTEFlags::MarkMask) // Already accessible from roots of world?
markObjectsAccessibleFrom(rootOTE);
}
void ObjectMemory::markObjectsAccessibleFrom(OTE* ote)
{
HARDASSERT(!isIntegerObject(ote));
//HARDASSERT(!hasCurrentMark(ote));
// First toggle the mark bit to the new mark
markObject(ote);
BYTE curMark = *reinterpret_cast<BYTE*>(&m_spaceOTEBits[OTEFlags::NormalSpace]);
// The class is always visited, but is now in the OTE which means we may not need
// to visit the object body at all
BehaviorOTE* oteClass = ote->m_oteClass;
if ((oteClass->m_ubFlags ^ curMark) & OTEFlags::MarkMask) // Already accessible from roots of world?
markObjectsAccessibleFrom(reinterpret_cast<POTE>(oteClass));
const MWORD lastPointer = lastStrongPointerOf(ote);
Oop* pFields = reinterpret_cast<Oop*>(ote->m_location);
for (MWORD i = ObjectHeaderSize; i < lastPointer; i++)
{
// This will get nicely optimised by the Compiler
Oop fieldPointer = pFields[i];
// Perform tests to see if marking necessary to save a call
// We don't need to visit SmallIntegers and objects we've already visited
if (!isIntegerObject(fieldPointer))
{
OTE* oteField = reinterpret_cast<OTE*>(fieldPointer);
// By Xoring current mark mask with existing one we should only get > 1 if they
// don't actually match, and therefore we haven't visited here yet.
if ((oteField->m_ubFlags ^ curMark) & OTEFlags::MarkMask) // Already accessible from roots of world?
markObjectsAccessibleFrom(oteField);
}
}
}
OTEFlags ObjectMemory::nextMark()
{
OTEFlags oldMark = m_spaceOTEBits[OTEFlags::NormalSpace];
// Toggle the "visited" mark - all objects will then have previous mark
BOOL newMark = oldMark.m_mark ? FALSE : TRUE;
for (unsigned i=0;i<OTEFlags::NumSpaces;i++)
m_spaceOTEBits[i].m_mark = newMark;
return oldMark;
}
void ObjectMemory::asyncGC(DWORD gcFlags, Oop* const sp)
{
EmptyZct(sp);
reclaimInaccessibleObjects(gcFlags);
PopulateZct(sp);
Interpreter::scheduleFinalization();
}
void ObjectMemory::reclaimInaccessibleObjects(DWORD gcFlags)
{
// Assign flags to static, as we use some deeply recursive routines
// and we don't want to pass down to the depths. When we want to turn off
// weakness we mask with the free bit, which obviously can't be set on any
// live object so the test will always fail
WeaknessMask = static_cast<BYTE>(gcFlags & GCNoWeakness ? 0 : OTEFlags::WeakMask);
// Get the Oop to use for corpses from the interpreter (it's a global)
Oop corpse = corpsePointer();
HARDASSERT(!isIntegerObject(corpse));
if (corpse == Oop(_Pointers.Nil))
{
tracelock lock(TRACESTREAM);
TRACESTREAM<< L"GC: WARNING, attempted GC before Corpse registered." << std::endl;
return; // Refuse to garbage collect if the corpse is invalid
}
#ifdef _DEBUG
checkReferences();
#endif
#ifdef VERBOSEGC
MAPCLASSOTE2INT lossMap;
#endif
// Move to the "next" GC mark (really a toggle). We'll need the old mark to rescue objects
OTEFlags oldMark = nextMark();
// Starting from the roots of the world, recursively visit all objects which are still reachable
// along a chain of strong references. We may later need to 'rescue' some unmarked objects
// reachable from dying objects which are queued for finalization. Should these rescued objects
// also be finalizable, then this will delay their finalization until their parent has disappeared.
markObjectsAccessibleFrom(pointerFromIndex(0));
Interpreter::MarkRoots();
// Every object reachable from the roots of the world will now have the current mark bit,
// any objects with the old mark bit can be discarded.
// Now locate all the unmarked objects, and visit any object referenced from finalizable
// unmarked objects. Also nil the corpses of any weak objects, and queue them for finalization
unsigned nMaxUnmarked = 0, nUnmarked = 0;
OTE** pUnmarked = 0;
const OTE* pEnd = m_pOT+m_nOTSize; // Loop invariant
const BYTE curMark = *reinterpret_cast<BYTE*>(&m_spaceOTEBits[OTEFlags::NormalSpace]);
for (OTE* ote=m_pOT+OTBase; ote < pEnd; ote++)
{
BYTE oteFlags = ote->m_ubFlags;
if (!(oteFlags & OTEFlags::FreeMask)) // Already free'd?
{
// By Xoring current mark mask with existing one we should only get > 1 if they
// don't actually match
if ((oteFlags ^ curMark) & OTEFlags::MarkMask) // Accessible from roots of world?
{
// Inaccessible object found, if finalizable, then we need to rescue it by
// visiting all the objects it references
if (nUnmarked == nMaxUnmarked)
{
if (nMaxUnmarked == 0)
nMaxUnmarked = 512;
else
nMaxUnmarked = nMaxUnmarked << 1;
pUnmarked = static_cast<OTE**>(realloc(pUnmarked, nMaxUnmarked*sizeof(OTE*)));
}
pUnmarked[nUnmarked++] = ote;
// If the object is finalizable, rescue it by visiting all objects accessible from it
if (oteFlags & OTEFlags::FinalizeMask)
{
markObjectsAccessibleFrom(ote);
// We must ensure that if a finalizable object is circularly referenced, directly
// or indirectly, that we don't prevent it ever being finalized.
ote->setMark(oldMark.m_mark);
}
}
}
}
// Another scan to nil out weak references. This has to be a separate scan from the finalization
// candidate scan so that we don't end up nilling out weak references to objects that are accessible
// from finalizable objects
unsigned queuedForBereavement=0;
if (WeaknessMask != 0)
{
for (OTE* ote = m_pOT + OTBase; ote < pEnd; ote++)
{
const BYTE oteFlags = ote->m_ubFlags;
// Is it a non-free'd, weak pointer object, and does it either have the current mark or is finalizable?
// If so it's losses are replaced with references to the corpse object, and it may be sent a loss notification
if (((oteFlags & (OTEFlags::WeakMask | OTEFlags::FreeMask)) == OTEFlags::WeakMask)
&& (((oteFlags ^ curMark) & (OTEFlags::MarkMask | OTEFlags::FinalizeMask)) != OTEFlags::MarkMask))
{
SMALLINTEGER losses = 0;
PointersOTE* otePointers = reinterpret_cast<PointersOTE*>(ote);
const MWORD size = otePointers->pointersSize();
VariantObject* weakObj = otePointers->m_location;
const Behavior* weakObjClass = ote->m_oteClass->m_location;
const MWORD fixedFields = weakObjClass->fixedFields();
for (MWORD j = fixedFields; j < size; j++)
{
Oop fieldPointer = weakObj->m_fields[j];
if (!ObjectMemoryIsIntegerObject(fieldPointer))
{
OTE* fieldOTE = reinterpret_cast<OTE*>(fieldPointer);
const BYTE fieldFlags = fieldOTE->m_ubFlags;
if (fieldFlags & OTEFlags::FreeMask)
{
#if defined(_DEBUG) && 0
TRACESTREAM<< L"Weakling " << ote<< L" loses reference to freed object " <<
(UINT)fieldOTE<< L"/" << indexOfObject(fieldOTE) << std::endl;
#endif
weakObj->m_fields[j] = corpse;
losses++;
}
else if ((fieldFlags ^ curMark) & OTEFlags::MarkMask)
{
HARDASSERT(!ObjectMemory::hasCurrentMark(fieldOTE));
// We must correctly maintain ref. count of dying object,
// just in case it is in (or will be in) the finalization queue
#if defined(_DEBUG) && 0
TRACESTREAM<< L"Weakling " << ote<< L" loses reference to " <<
fieldOTE<< L"(" << (UINT)fieldOTE<< L"/" << indexOfObject(fieldOTE)<< L" refs " <<
int(ote->m_flags.m_count)<< L")" << std::endl;
#endif
fieldOTE->decRefs();
weakObj->m_fields[j] = corpse;
losses++;
}
}
}
// If any bereavements were suffered, then inform the weak object
if (losses && weakObjClass->isMourner())
{
queuedForBereavement++;
Interpreter::queueForBereavementOf(ote, integerObjectOf(losses));
#ifdef _DEBUG
{
tracelock lock(TRACESTREAM);
TRACESTREAM<< L"Weakling: " << ote<< L" (" << std::hex << UINT(ote)<< L") lost " << std::dec << losses << L" elements" << std::endl;
}
#endif
// We must also ensure that it and its referenced objects are marked since we're
// rescuing it.
markObjectsAccessibleFrom(ote);
}
}
}
}
#ifdef _DEBUG
{
// Ensure the permanent objects have the current mark too
const OTE* pEndPerm = m_pOT + NumPermanent;
for (OTE* ote = m_pOT; ote < pEndPerm; ote++)
markObject(ote);
}
#endif
// Now sweep through the unmarked objects, and finalize/deallocate any objects which are STILL
// unmarked
unsigned deletions=0;
unsigned queuedForFinalize=0;
const unsigned loopEnd = nUnmarked;
for (unsigned i=0;i<loopEnd;i++)
{
OTE* ote = pUnmarked[i];
const BYTE oteFlags = ote->m_ubFlags;
HARDASSERT(!(oteFlags & OTEFlags::FreeMask));
if ((oteFlags ^ curMark) & OTEFlags::MarkMask) // Still unmarked?
{
// Object still unmarked, so either deallocate it OR queue it for finalization
HARDASSERT(!ObjectMemory::hasCurrentMark(ote));
// We found a dying object, finalize it if necessary
if (!(oteFlags & OTEFlags::FinalizeMask))
{
// It doesn't want finalizing, so we can free it
// Countdown all refs from objects which are to be
// deallocated - but not recursively, since marking
// has already identified ALL objects which need to
// be freed - thus maintaining correct reference counts
// on objects which survive the garbage collect
// First we remove the reference to the class
BehaviorOTE* classPointer = ote->m_oteClass;
#ifdef VERBOSEGC
lossMap[classPointer]++;
#endif
if (classPointer->isFree())
{
#ifdef _DEBUG
{
tracelock lock(TRACESTREAM);
TRACESTREAM<< L"GC WARNING: " << LPVOID(ote) << L'/' << i
<< L" (size " << ote->getSize()<< L") has freed class "
<< LPVOID(classPointer) << L'/' << classPointer->getIndex() << std::endl;
}
#endif
}
else
{
classPointer->decRefs();
}
// If not a pointer object, then nothing further to do
if (ote->isPointers())
{
PointersOTE* otePointers = reinterpret_cast<PointersOTE*>(ote);
const MWORD lastPointer = otePointers->pointersSize();
VariantObject* varObj = otePointers->m_location;
for (unsigned f = 0; f < lastPointer; f++)
{
Oop fieldPointer = varObj->m_fields[f];
if (!isIntegerObject(fieldPointer))
{
OTE* fieldOTE = reinterpret_cast<OTE*>(fieldPointer);
// Could have been previously deleted during GC
if (!fieldOTE->isFree())
fieldOTE->decRefs();
}
}
}
// We must ensure count really zero as some deallocation routines may not do this
// (normally objects are only deallocated when the count hits zero)
ote->m_count = 0;
deallocate(ote);
deletions++;
}
else
{
#if 0//def _DEBUG
TRACESTREAM<< L"Finalizing " << ote << std::endl;
#endif
Interpreter::basicQueueForFinalization(ote);
// Prevent a second finalization
ote->beUnfinalizable();
// We must ensure the object has the current mark so that it doesn't cock up the
// next GC in case it survives that long
markObject(ote);
queuedForFinalize++;
}
}
}
free(pUnmarked);
#ifdef VERBOSEGC
{
for (MAPCLASSOTE2INT::iterator it=lossMap.begin(); it != lossMap.end(); it++)
{
BehaviorOTE* classPointer = (*it).first;
int val = (*it).second;
{
tracelock lock(TRACESTREAM);
if (classPointer->isFree())
TRACESTREAM<< L"GC: " << val<< L" objects of a free'd class ("
<< LPVOID(classPointer) << L'(' << classPointer->getIndex()<< L") were deallocated" << std::endl;
else
TRACESTREAM<< L"GC: " << std::dec << val << L' ' << classPointer<< L"'s were deallocated" << std::endl;
}
}
lossMap.clear();
}
#endif
__sbh_heapmin();
#ifdef _DEBUG
checkReferences();
#endif
#if defined(VERBOSEGC)
if (deletions > 0)
{
tracelock lock(TRACESTREAM);
TRACESTREAM<< L"GC: Completed, " << deletions<< L" objects reclaimed, "
<< queuedForFinalize<< L" queued for finalization, "
<< queuedForBereavement<< L" weak lose elements" << std::endl;
}
#endif
}
void ObjectMemory::addVMRefs()
{
// Deliberately max out ref. counts of VM ref'd objects so that ref. counting ops
// not needed
Array* globalPointers = (Array*)&_Pointers;
const unsigned loopEnd = NumPointers;
for (unsigned i=0;i<loopEnd;i++)
{
Oop obj = globalPointers->m_elements[i];
if (!isIntegerObject(obj))
reinterpret_cast<OTE*>(obj)->beSticky();
}
}
#ifdef _DEBUG
void ObjectMemory::checkPools()
{
const unsigned loopEnd = m_nOTSize;
for (unsigned i=OTBase;i<loopEnd;i++)
{
OTE& ote = m_pOT[i];
if (!ote.isFree())
{
OTEFlags::Spaces space = ote.heapSpace();
if (space == OTEFlags::PoolSpace)
{
unsigned size = ote.sizeOf();
if (size > MaxSizeOfPoolObject)
{
if (size <= MaxSmallObjectSize)
HARDASSERT(__sbh_find_block(ote.m_location))
else
{
tracelock lock(TRACESTREAM);
TRACESTREAM<< L"Found large object (size = " << size
<< L") in space " << (int)space
<< L": " << &ote << std::endl;
}
}
else
if (size != 0)
HARDASSERT(spacePoolForSize(size).isMyChunk(ote.m_location))
}
}
}
for (int j=0;j<NumPools;j++)
HARDASSERT(m_pools[j].isValid());
}
int ObjectMemory::CountFreeOTEs()
{
OTE* p = m_pFreePointerList;
int count = 0;
OTE* offEnd= m_pOT + m_nOTSize;
while (p < offEnd)
{
count++;
p = reinterpret_cast<OTE*>(p->m_location);
}
return count;
}
void ObjectMemory::checkStackRefs(Oop* const sp)
{
int zeroCountNotInZct = 0;
Process* pProcess = Interpreter::m_registers.m_pActiveProcess;
for (Oop* pOop = pProcess->m_stack;pOop <= sp;pOop++)
{
Oop oop = *pOop;
if (!isIntegerObject(oop))
{
OTE* ote = reinterpret_cast<OTE*>(oop);
if (ote->m_count == 0 && !IsInZct(ote))
{
TRACESTREAM<< L"WARNING: Zero count Oop not in Zct: " << ote << std::endl;
zeroCountNotInZct++;
}
}
}
HARDASSERT(zeroCountNotInZct == 0);
}
void ObjectMemory::checkReferences()
{
// If this assertion fires, then something has written off the end
// of an object, and corrupted the heap. Possibilities to consider are:
// 1) Modification of a class (e.g. addition/removal of instance
// variables). The Behavior code in Snailtalk is still
// not very safe, and frequently fails to modify information
// about the 'shape' of classes in a running system. The solution
// is probably to reboot.
// 2) An object passed to some external API as an 'lpvoid' has the
// wrong size, or some error caused writing off either of its ends
// 3) Stack overflow (probably due to a recursive loop going too deep),
// though it this occurs it will likely cause a failure somewhat
// earlier (mostly, it seems, in either activateNewMethod
// or returnValueTo) in BYTEASM.ASM).
//
#ifdef PRIVATE_HEAP
if (Interpreter::executionTrace > 3)
HARDASSERT(::HeapValidate(m_hHeap, 0, 0));
#endif
// HARDASSERT(_CrtCheckMemory());
HARDASSERT(__sbh_heap_check() >= 0);
//checkPools();
HARDASSERT(m_nFreeOTEs == CountFreeOTEs());
Interpreter::GrabAsyncProtect();
Oop* const sp = Interpreter::m_registers.m_stackPointer;
// Now adjust for the current active process, depending on whether the ZCT has been reconciled or not
if (!IsReconcilingZct())
{
checkStackRefs(sp);
Interpreter::IncStackRefs(sp);
}
int errors=0;
BYTE* currentRefs = new BYTE[m_nOTSize];
{
const unsigned loopEnd = m_nOTSize;
for (unsigned i=OTBase; i < loopEnd; i++)
{
// Count and free bit should both be zero, or both non-zero
/*if (m_pOT[i].m_flags.m_free ^ (m_pOT[i].m_flags.m_count == 0))
{
TRACESTREAM<< L"WARNING: ";
Oop oop = pointerFromIndex(i);
Interpreter::printObject(oop, TRACESTREAM);
TRACESTREAM<< L" (Oop " << oop<< L"/" << i<< L") has refs " <<
m_pOT[i].m_flags.m_count << std::endl;
//errors++;
}*/
OTE* ote = &m_pOT[i];
if (!ote->isFree() && ote->heapSpace() == OTEFlags::PoolSpace)
HARDASSERT(ote->sizeOf() <= MaxSmallObjectSize);
currentRefs[i] = ote->m_count;
ote->m_count = 0;
}
}
// Recalc the references
const OTE* pEnd = m_pOT+m_nOTSize;
int nFree = 0;
for (OTE* ote=m_pOT; ote < pEnd; ote++)
{
if (!ote->isFree())
addRefsFrom(ote);
else
nFree++;
}
POTE poteFree = m_pFreePointerList;
int cFreeList = 0;
while (poteFree < pEnd)
{
++cFreeList;
poteFree = reinterpret_cast<OTE*>(poteFree->m_location);
}
//TRACESTREAM << nFree<< L" free slots found in OT, " << cFreeList<< L" on the free list (" << nFree-cFreeList<< L")" <<endl;
Interpreter::ReincrementVMReferences();
int refCountTooSmall = 0;
const unsigned loopEnd = m_nOTSize;
for (unsigned i=OTBase; i < loopEnd; i++)
{
OTE* ote = &m_pOT[i];
if (currentRefs[i] < OTE::MAXCOUNT)
{
if (currentRefs[i] != ote->m_count)
{
bool tooSmall = currentRefs[i] < ote->m_count;
tracelock lock(TRACESTREAM);
if (tooSmall)
{
refCountTooSmall++;
TRACESTREAM<< L"ERROR: ";
}
else
TRACESTREAM<< L"WARNING: ";
TRACESTREAM << ote<< L" (Oop " << LPVOID(ote)<< L"/" << i<< L") had refs " << std::dec << (int)currentRefs[i]
<< L" should be " << int(ote->m_count) << std::endl;
errors++;
if (tooSmall)
{
if (!Interpreter::m_bAsyncGCDisabled)
{
TRACESTREAM<< L" Referenced From:" << std::endl;
ArrayOTE* oteRefs = ObjectMemory::referencesTo(reinterpret_cast<Oop>(ote), true);
Array* refs = oteRefs->m_location;
for (unsigned i=0;i<oteRefs->pointersSize();i++)
TRACESTREAM<< L" " << reinterpret_cast<OTE*>(refs->m_elements[i]) << std::endl;
deallocate(reinterpret_cast<OTE*>(oteRefs));
}
}
if (Interpreter::m_bAsyncGCDisabled)
{
// If compiler is running then async GCs are disabled, and we should leave the ref. counts
// that are too high unaffected
ote->m_count = currentRefs[i];
}
} else if (currentRefs[i] == 0 && !ote->isFree() && !IsInZct(ote))
{
// Shouldn't be zero count objects around that are not in the Zct
TRACESTREAM << ote<< L" (Oop " << LPVOID(ote)<< L"/" << i<< L") had zero refs" << std::endl;
errors++;
}
}
else
{
// Never modify the ref. count of a sticky object - let the GC collect these
ote->beSticky();
}
if (!ote->isFree())
{
// Perform some basic checks that the object is valid
// Is the class pointer valid?
HARDASSERT(isBehavior(Oop(ote->m_oteClass)));
// Are the remaining pointers valid Oops
if (ote->isPointers())
{
VariantObject* obj = reinterpret_cast<PointersOTE*>(ote)->m_location;
int size = ote->pointersSize();
for (int i = 0; i < size; i++)
{
HARDASSERT(isValidOop(obj->m_fields[i]));
}
}
}
}
// Now remove refs from the current active process that we added before checking refs
if (!IsReconcilingZct())
{
// We have to be careful not to cause more entries to be placed in the Zct, so we need to inline this
// operation and just count down the refs and not act when they drop to zero
Process* pProcess = Interpreter::m_registers.m_pActiveProcess;
for (Oop* pOop = pProcess->m_stack;pOop <= sp;pOop++)
ObjectMemory::decRefs(*pOop);
checkStackRefs(sp);
}
Interpreter::RelinquishAsyncProtect();
HARDASSERT(Interpreter::m_bAsyncGCDisabled || !refCountTooSmall);
delete[] currentRefs;
if (errors)
{
HARDASSERT(Interpreter::m_bAsyncGCDisabled || ignoreRefCountErrors || ((errors - refCountTooSmall) == 0));
// If we don't do this, the proc. may have wrong size for GC
Interpreter::resizeActiveProcess();
}
}
void ObjectMemory::addRefsFrom(OTE* ote)
{
HARDASSERT(ote >= m_pOT);
// We must also inc. ref. count on the class here
ote->m_oteClass->countUp();
if (ote->isPointers())
{
PointersOTE* otePointers = reinterpret_cast<PointersOTE*>(ote);
VariantObject* varObj = otePointers->m_location;
const MWORD lastPointer = otePointers->pointersSize();
for (MWORD i = 0; i < lastPointer; i++)
{
Oop fieldPointer = varObj->m_fields[i];
// The reason we don't use an ASSERT here is that, ASSERT throws
// up a message box which causes a callback into Smalltalk to process
// the window messages, which is not permissible during the execution
// of a GC
if (!isIntegerObject(fieldPointer))
{
OTE* fieldOTE = reinterpret_cast<OTE*>(fieldPointer);
if (fieldOTE < m_pOT
|| fieldOTE > m_pOT+m_nOTSize-1
|| fieldOTE->isFree())
{
HARDASSERT(FALSE); // Fires if fieldPointer bad
varObj->m_fields[i] = Oop(_Pointers.Nil); // Repair the damage
}
}
countUp(fieldPointer);
}
}
}
#endif // Debug