Skip to content

Commit 2523e3a

Browse files
author
Patrick Soquet
committed
XS: organic vs forced garbage collection
1 parent 99e893b commit 2523e3a

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

xs/platforms/gecko/xsHost.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ txSlot* fxAllocateSlots(txMachine* the, txSize theCount)
314314
result = (txSlot *)mc_xs_slot_allocator(the, theCount * sizeof(txSlot));
315315
if (!result) {
316316
fxReport(the, "# Slot allocation: failed. trying to make room...\n");
317-
fxCollect(the, 1); /* expecting memory from the chunk pool */
317+
fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG); /* expecting memory from the chunk pool */
318318
if (the->firstBlock != C_NULL && the->firstBlock->limit == mc_xs_chunk_allocator(the, 0)) { /* sanity check just in case */
319319
fxReport(the, "# Slot allocation: %d bytes returned\n", the->firstBlock->limit - the->firstBlock->current);
320320
the->maximumChunksSize -= the->firstBlock->limit - the->firstBlock->current;

xs/platforms/mc/xsHosts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ txSlot *fxAllocateSlots(txMachine* the, txSize theCount)
147147
#ifdef mxDebug
148148
fxReport(the, "# Slot allocation: failed. trying to make room...\n");
149149
#endif
150-
fxCollect(the, 1); /* expecting memory from the chunk pool */
150+
fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG); /* expecting memory from the chunk pool */
151151
#ifdef mxDebug
152152
fxReport(the, "# Slot allocation: %d bytes returned\n", the->firstBlock->limit - the->firstBlock->current);
153153
#endif

xs/platforms/qca4020/xsHost.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ txSlot* fxAllocateSlots(txMachine* the, txSize theCount)
447447
result = (txSlot *)mc_xs_slot_allocator(the, theCount * sizeof(txSlot));
448448
if (!result) {
449449
fxReport(the, "# Slot allocation: failed. trying to make room...\n");
450-
fxCollect(the, 1); /* expecting memory from the chunk pool */
450+
fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG); /* expecting memory from the chunk pool */
451451
if (the->firstBlock != C_NULL && the->firstBlock->limit == mc_xs_chunk_allocator(the, 0)) { /* sanity check just in case */
452452
fxReport(the, "# Slot allocation: %d bytes returned\n", the->firstBlock->limit - the->firstBlock->current);
453453
the->maximumChunksSize -= the->firstBlock->limit - the->firstBlock->current;

xs/sources/xsAPI.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@ void fxShareMachine(txMachine* the)
17891789

17901790
void fxCollectGarbage(txMachine* the)
17911791
{
1792-
fxCollect(the, 1);
1792+
fxCollect(the, XS_COMPACT_FLAG);
17931793
}
17941794

17951795
void fxEnableGarbageCollection(txMachine* the, txBoolean enableIt)

xs/sources/xsAll.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ extern txBoolean fxIsSameValue(txMachine* the, txSlot* a, txSlot* b, txBoolean z
780780
extern txSize fxAddChunkSizes(txMachine* the, txSize a, txSize b);
781781
extern void fxCheckCStack(txMachine* the);
782782
extern void fxAllocate(txMachine* the, txCreation* theCreation);
783-
extern void fxCollect(txMachine* the, txBoolean theFlag);
783+
extern void fxCollect(txMachine* the, txFlag theFlag);
784784
mxExport txSlot* fxDuplicateSlot(txMachine* the, txSlot* theSlot);
785785
extern void fxFree(txMachine* the);
786786
extern void fxGrowKeys(txMachine* the, txID theCount);
@@ -1909,9 +1909,11 @@ enum {
19091909
XS_GET_ONLY = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG,
19101910

19111911
/* collect flags */
1912-
XS_COLLECTING_FLAG = 1,
1913-
XS_TRASHING_FLAG = 2,
1914-
XS_SKIPPED_COLLECT_FLAG = 4,
1912+
XS_COMPACT_FLAG = 1,
1913+
XS_ORGANIC_FLAG = 2,
1914+
XS_COLLECTING_FLAG = 4,
1915+
XS_TRASHING_FLAG = 8,
1916+
XS_SKIPPED_COLLECT_FLAG = 16,
19151917
XS_HOST_CHUNK_FLAG = 32,
19161918
XS_HOST_HOOKS_FLAG = 64,
19171919

xs/sources/xsMemory.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ void fxCheckCStack(txMachine* the)
263263
}
264264
}
265265

266-
void fxCollect(txMachine* the, txBoolean theFlag)
266+
void fxCollect(txMachine* the, txFlag theFlag)
267267
{
268268
txSize aCount;
269269
txSlot* freeSlot;
@@ -275,8 +275,9 @@ void fxCollect(txMachine* the, txBoolean theFlag)
275275
the->collectFlag |= XS_SKIPPED_COLLECT_FLAG;
276276
return;
277277
}
278+
the->collectFlag |= theFlag & XS_ORGANIC_FLAG;
278279

279-
if (theFlag) {
280+
if (theFlag & XS_COMPACT_FLAG) {
280281
fxMark(the, fxMarkValue);
281282
fxMarkWeakStuff(the);
282283
fxSweep(the);
@@ -347,6 +348,7 @@ void fxCollect(txMachine* the, txBoolean theFlag)
347348
else
348349
the->collectFlag &= ~XS_TRASHING_FLAG;
349350
}
351+
the->collectFlag &= ~XS_ORGANIC_FLAG;
350352

351353
#if mxReport
352354
if (theFlag)
@@ -391,7 +393,7 @@ void* fxFindChunk(txMachine* the, txSize size, txBoolean *once)
391393
#if mxStress
392394
if (fxShouldStress()) {
393395
if (*once) {
394-
fxCollect(the, 1);
396+
fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
395397
*once = 0;
396398
}
397399
}
@@ -415,7 +417,7 @@ void* fxFindChunk(txMachine* the, txSize size, txBoolean *once)
415417
block = block->nextBlock;
416418
}
417419
if (*once) {
418-
fxCollect(the, 1);
420+
fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
419421
*once = 0;
420422
goto again;
421423
}
@@ -1008,9 +1010,20 @@ void fxMarkReference(txMachine* the, txSlot* theSlot)
10081010
}
10091011
break;
10101012
case XS_WEAK_REF_KIND:
1011-
if (theSlot->value.weakRef.target) {
1013+
aSlot = theSlot->value.weakRef.target;
1014+
if (aSlot) {
1015+
#ifdef mxSnapshot
1016+
if (the->collectFlag & XS_ORGANIC_FLAG) {
1017+
fxMarkReference(the, aSlot);
1018+
}
1019+
else {
1020+
theSlot->value.weakRef.link = the->firstWeakRefLink;
1021+
the->firstWeakRefLink = theSlot;
1022+
}
1023+
#else
10121024
theSlot->value.weakRef.link = the->firstWeakRefLink;
10131025
the->firstWeakRefLink = theSlot;
1026+
#endif
10141027
}
10151028
break;
10161029
case XS_FINALIZATION_REGISTRY_KIND:
@@ -1250,9 +1263,20 @@ void fxMarkValue(txMachine* the, txSlot* theSlot)
12501263
}
12511264
break;
12521265
case XS_WEAK_REF_KIND:
1253-
if (theSlot->value.weakRef.target) {
1266+
aSlot = theSlot->value.weakRef.target;
1267+
if (aSlot) {
1268+
#ifdef mxSnapshot
1269+
if (the->collectFlag & XS_ORGANIC_FLAG) {
1270+
fxMarkValue(the, aSlot);
1271+
}
1272+
else {
1273+
theSlot->value.weakRef.link = the->firstWeakRefLink;
1274+
the->firstWeakRefLink = theSlot;
1275+
}
1276+
#else
12541277
theSlot->value.weakRef.link = the->firstWeakRefLink;
12551278
the->firstWeakRefLink = theSlot;
1279+
#endif
12561280
}
12571281
break;
12581282
case XS_FINALIZATION_REGISTRY_KIND:
@@ -1406,7 +1430,7 @@ txSlot* fxNewSlot(txMachine* the)
14061430

14071431
#if mxStress
14081432
if (fxShouldStress()) {
1409-
fxCollect(the, 1);
1433+
fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
14101434
once = 0;
14111435
}
14121436
#endif
@@ -1435,7 +1459,7 @@ txSlot* fxNewSlot(txMachine* the)
14351459
if (once) {
14361460
txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_FLAG) != 0), isThrashing;
14371461

1438-
fxCollect(the, 0);
1462+
fxCollect(the, XS_ORGANIC_FLAG);
14391463

14401464
isThrashing = ((the->collectFlag & XS_TRASHING_FLAG) != 0);
14411465
allocate = wasThrashing && isThrashing;

0 commit comments

Comments
 (0)