Coverage Report

Created: 2025-11-24 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsMemory.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2025  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#include "xsAll.h"
39
40
#ifndef mxReport
41
#define mxReport 0
42
#endif
43
#ifndef mxStress
44
#define mxStress 0
45
#endif
46
#ifndef mxNoChunks
47
#define mxNoChunks 0
48
#endif
49
#ifndef mxPoisonSlots
50
#define mxPoisonSlots 0
51
#endif
52
53
#if mxPoisonSlots
54
#include <sanitizer/asan_interface.h>
55
#endif
56
57
#if mxStress
58
int gxStress = 0;
59
60
static int fxShouldStress()
61
1.12G
{
62
1.12G
  if (!gxStress)
63
1.12G
    return 0;
64
65
275k
  if (gxStress > 0)
66
0
    return 1;
67
68
275k
  gxStress += 1;
69
275k
  return 0 == gxStress;
70
275k
}
71
#endif
72
73
#if mxNoChunks
74
  #if FUZZING
75
    extern void *fxMemMalloc_noforcefail(size_t size);
76
    #define c_malloc_noforcefail(size) fxMemMalloc_noforcefail(size)
77
  #else
78
    #define c_malloc_noforcefail c_malloc
79
  #endif
80
#endif
81
82
1.66G
#define mxChunkFlag 0x80000000
83
84
static txSize fxAdjustChunkSize(txMachine* the, txSize size);
85
static void* fxCheckChunk(txMachine* the, txChunk* chunk, txSize size, txSize offset);
86
static void* fxFindChunk(txMachine* the, txSize size, txBoolean *once);
87
static void* fxGrowChunk(txMachine* the, txSize size);
88
static void* fxGrowChunks(txMachine* the, txSize theSize); 
89
/* static */ void fxGrowSlots(txMachine* the, txSize theCount); 
90
static void fxMark(txMachine* the, void (*theMarker)(txMachine*, txSlot*));
91
#if mxKeysGarbageCollection
92
static void fxMarkID(txMachine* the, txID id);
93
#endif
94
static void fxMarkFinalizationRegistry(txMachine* the, txSlot* registry);
95
static void fxMarkInstance(txMachine* the, txSlot* theCurrent, void (*theMarker)(txMachine*, txSlot*));
96
static void fxMarkReference(txMachine* the, txSlot* theSlot);
97
static void fxMarkValue(txMachine* the, txSlot* theSlot);
98
static void fxMarkWeakStuff(txMachine* the);
99
static void fxSweep(txMachine* the);
100
static void fxSweepValue(txMachine* the, txSlot* theSlot);
101
102
#ifdef mxNever
103
104
typedef struct sxSample txSample;
105
struct sxSample {
106
  struct timespec time;
107
  txNumber duration;
108
  long count;
109
  char* label;
110
};
111
112
void reportTime(txSample* theSample) 
113
{
114
  fprintf(stderr, " %s %ld %f", theSample->label, theSample->count, theSample->duration); 
115
}
116
117
void startTime(txSample* theSample) 
118
{
119
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(theSample->time));
120
}
121
122
void stopTime(txSample* theSample) 
123
{
124
  struct timespec time;
125
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
126
  theSample->duration += ((double)(time.tv_sec) - (double)(theSample->time.tv_sec))
127
      + (((double)(time.tv_nsec) - (double)(theSample->time.tv_nsec)) / 1000000000.0);
128
  theSample->count++;
129
}
130
131
txSample gxLifeTime = { { 0, 0 }, 0, 0, "life" };
132
txSample gxMarkTime = { { 0, 0 }, 0, 0, "mark" };
133
txSample gxSweepChunkTime = { { 0, 0 }, 0, 0, "sweep chunk" };
134
txSample gxSweepSlotTime = { { 0, 0 }, 0, 0, "sweep slot" };
135
txSample gxCompactChunkTime = { { 0, 0 }, 0, 0, "compact" };
136
txSample gxChunksGarbageCollectionTime = { { 0, 0 }, 0, 0, "chunks" };
137
txSample gxKeysGarbageCollectionTime = { { 0, 0 }, 0, 0, "keys" };
138
txSample gxSlotsGarbageCollectionTime = { { 0, 0 }, 0, 0, "slots" };
139
txSample gxForcedGarbageCollectionTime = { { 0, 0 }, 0, 0, "forced" };
140
#endif
141
142
txSize fxAddChunkSizes(txMachine* the, txSize a, txSize b)
143
563M
{
144
563M
  txSize c;
145
563M
#if __has_builtin(__builtin_add_overflow)
146
563M
  if (__builtin_add_overflow(a, b, &c)) {
147
#else
148
  c = a + b;
149
  if (((a ^ c) & (b ^ c)) < 0) {
150
#endif
151
7
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
152
7
  }
153
563M
  return c;
154
563M
}
155
156
txSize fxAdjustChunkSize(txMachine* the, txSize size)
157
413M
{
158
413M
  txSize adjust = sizeof(txChunk);
159
413M
  txSize modulo = size & (sizeof(size_t) - 1);
160
413M
  if (modulo)
161
282M
    adjust += sizeof(size_t) - modulo;
162
413M
  return fxAddChunkSizes(the, size, adjust);
163
413M
}
164
165
void fxAllocate(txMachine* the, txCreation* theCreation)
166
33.8k
{
167
#ifdef mxNever
168
  startTime(&gxLifeTime);
169
#endif
170
33.8k
#if mxStress
171
33.8k
  gxStress = 0;
172
33.8k
#endif
173
174
33.8k
  the->currentChunksSize = 0;
175
33.8k
  the->peakChunksSize = 0;
176
33.8k
  the->maximumChunksSize = 0;
177
33.8k
  the->minimumChunksSize = theCreation->incrementalChunkSize;
178
  
179
33.8k
  the->currentHeapCount = 0;
180
33.8k
  the->peakHeapCount = 0;
181
33.8k
  the->maximumHeapCount = 0;
182
33.8k
  the->minimumHeapCount = theCreation->incrementalHeapCount;
183
  
184
33.8k
  the->firstBlock = C_NULL;
185
33.8k
  the->firstHeap = C_NULL;
186
187
#if mxNoChunks
188
  the->maximumChunksSize = theCreation->initialChunkSize;
189
#else
190
33.8k
  fxGrowChunks(the, theCreation->initialChunkSize);
191
33.8k
#endif
192
193
33.8k
  the->stackBottom = fxAllocateSlots(the, theCreation->stackCount);
194
33.8k
  the->stackTop = the->stackBottom + theCreation->stackCount;
195
33.8k
  the->stackIntrinsics = the->stackTop;
196
33.8k
  the->stackPrototypes = the->stackTop - XS_INTRINSICS_COUNT;
197
33.8k
  the->stack = the->stackTop;
198
#ifdef mxInstrument
199
  the->stackPeak = the->stackTop;
200
#endif
201
202
33.8k
  fxGrowSlots(the, theCreation->initialHeapCount);
203
204
33.8k
  the->keyCount = (txID)theCreation->initialKeyCount;
205
33.8k
  the->keyDelta = (txID)theCreation->incrementalKeyCount;
206
33.8k
  the->keyIndex = 0;
207
33.8k
  the->keyArray = (txSlot **)c_malloc_uint32(theCreation->initialKeyCount * sizeof(txSlot*));
208
33.8k
  if (!the->keyArray)
209
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);   
210
  
211
33.8k
  the->nameModulo = theCreation->nameModulo;
212
33.8k
  the->nameTable = (txSlot **)c_malloc_uint32(theCreation->nameModulo * sizeof(txSlot*));
213
33.8k
  if (!the->nameTable)
214
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
215
216
33.8k
  the->symbolModulo = theCreation->symbolModulo;
217
33.8k
  the->symbolTable = (txSlot **)c_malloc_uint32(theCreation->symbolModulo * sizeof(txSlot*));
218
33.8k
  if (!the->symbolTable)
219
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
220
    
221
33.8k
  fxAllocateStringInfoCache(the);
222
223
33.8k
  the->stackLimit = fxCStackLimit();
224
225
33.8k
  the->cRoot = C_NULL;
226
33.8k
  the->parserBufferSize = theCreation->parserBufferSize;
227
33.8k
  the->parserTableModulo = theCreation->parserTableModulo;
228
  
229
33.8k
#ifdef mxDebug
230
33.8k
  the->pathCount = 256;
231
33.8k
  the->pathValue = c_malloc(the->pathCount);
232
33.8k
  if (!the->pathValue)
233
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
234
33.8k
#endif
235
33.8k
}
236
237
void* fxCheckChunk(txMachine* the, txChunk* chunk, txSize size, txSize offset)
238
348M
{
239
348M
  if (chunk) {
240
348M
    txByte* data = (txByte*)chunk;
241
#if mxNoChunks
242
    chunk->size = size;
243
    the->currentChunksSize += size;
244
#else
245
348M
    txSize capacity = (txSize)(chunk->temporary - data);
246
348M
  #ifdef mxSnapshot
247
348M
    #if INTPTR_MAX == INT64_MAX
248
348M
      chunk->dummy = 0;
249
348M
    #endif
250
  #ifdef mxSnapshotRandomInit
251
    arc4random_buf(data + sizeof(txChunk), offset);
252
  #endif    
253
348M
  #endif
254
348M
    offset += sizeof(txChunk);
255
348M
    c_memset(data + offset, 0, capacity - offset);
256
348M
    chunk->size = size;
257
348M
    the->currentChunksSize += capacity;
258
348M
#endif
259
348M
    if (the->peakChunksSize < the->currentChunksSize)
260
92.9M
      the->peakChunksSize = the->currentChunksSize;
261
348M
    return data + sizeof(txChunk);
262
348M
  }
263
7
  fxReport(the, "# Chunk allocation: failed for %ld bytes\n", (long)size);
264
7
  fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
265
7
  return C_NULL;
266
348M
}
267
268
#if defined(__clang__) || defined (__GNUC__)
269
  __attribute__((no_sanitize_address))
270
#endif
271
void fxCheckCStack(txMachine* the)
272
470M
{
273
470M
    char x;
274
470M
    char *stack = &x;
275
470M
  if (stack <= the->stackLimit) {
276
17
    fxAbort(the, XS_NATIVE_STACK_OVERFLOW_EXIT);
277
17
  }
278
470M
}
279
280
void fxCollect(txMachine* the, txFlag theFlag)
281
105k
{
282
105k
  txSize aCount;
283
105k
  txSlot* freeSlot;
284
105k
  txSlot* aSlot;
285
105k
  txSlot* bSlot;
286
105k
  txSlot* cSlot;
287
288
105k
  if ((the->collectFlag & XS_COLLECTING_FLAG) == 0) {
289
0
    the->collectFlag |= XS_SKIPPED_COLLECT_FLAG;
290
0
    return;
291
0
  }
292
105k
  the->collectFlag |= theFlag & (XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
293
294
#ifdef mxNever
295
  if (theFlag & XS_ORGANIC_FLAG) {
296
    if (theFlag & XS_COMPACT_FLAG)
297
      startTime(&gxChunksGarbageCollectionTime);
298
    else if (theFlag & XS_COLLECT_KEYS_FLAG)
299
      startTime(&gxKeysGarbageCollectionTime);
300
    else
301
      startTime(&gxSlotsGarbageCollectionTime);
302
  }
303
  else
304
    startTime(&gxForcedGarbageCollectionTime);
305
  startTime(&gxMarkTime);
306
#endif
307
105k
  if (theFlag & XS_COMPACT_FLAG) {
308
80.0k
    fxInvalidateStringInfoCache(the);
309
80.0k
    fxMark(the, fxMarkValue);
310
80.0k
    fxMarkWeakStuff(the);
311
  #ifdef mxNever
312
    stopTime(&gxMarkTime);
313
  #endif
314
80.0k
    fxSweep(the);
315
80.0k
  }
316
25.8k
  else {
317
25.8k
    fxMark(the, fxMarkReference);
318
25.8k
    fxMarkWeakStuff(the);
319
  #ifdef mxNever
320
    stopTime(&gxMarkTime);
321
    startTime(&gxSweepSlotTime);
322
  #endif
323
25.8k
    aCount = 0;
324
25.8k
    freeSlot = C_NULL;
325
25.8k
    aSlot = the->firstHeap;
326
88.7k
    while (aSlot) {
327
62.9k
      bSlot = aSlot + 1;
328
62.9k
      cSlot = aSlot->value.reference;
329
2.06G
      while (bSlot < cSlot) {
330
2.06G
        if (bSlot->flag & XS_MARK_FLAG) {
331
1.33G
          bSlot->flag &= ~XS_MARK_FLAG; 
332
          
333
1.33G
          if (bSlot->kind == XS_REFERENCE_KIND)
334
142M
            mxCheck(the, bSlot->value.reference->kind == XS_INSTANCE_KIND);
335
          
336
1.33G
          aCount++;
337
1.33G
        }
338
731M
        else {
339
731M
          if (bSlot->kind == XS_HOST_KIND) {
340
3.35k
            if (bSlot->flag & XS_HOST_HOOKS_FLAG) {
341
0
              if (bSlot->value.host.variant.hooks->destructor)
342
0
                (*(bSlot->value.host.variant.hooks->destructor))(bSlot->value.host.data);
343
0
            }
344
3.35k
            else if (bSlot->value.host.variant.destructor)
345
1.75k
              (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
346
3.35k
          }
347
        #if mxInstrument
348
          if ((bSlot->kind == XS_MODULE_KIND) && (bSlot->ID == XS_MODULE_BEHAVIOR))
349
            the->loadedModulesCount--;
350
        #endif
351
731M
          bSlot->kind = XS_UNDEFINED_KIND;
352
731M
          bSlot->next = freeSlot;
353
        #if mxPoisonSlots
354
          ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
355
        #endif
356
731M
          freeSlot = bSlot;
357
731M
        }
358
2.06G
        bSlot++;
359
2.06G
      }
360
62.9k
      aSlot = aSlot->next;
361
62.9k
    }
362
25.8k
    the->currentHeapCount = aCount;
363
25.8k
    the->freeHeap = freeSlot;
364
  #ifdef mxNever
365
    stopTime(&gxSweepSlotTime);
366
  #endif
367
25.8k
  }
368
  
369
105k
  aSlot = the->stack;
370
133M
  while (aSlot < the->stackTop) {
371
133M
    aSlot->flag &= ~XS_MARK_FLAG; 
372
133M
    aSlot++;
373
133M
  }
374
  
375
105k
  the->collectFlag &= ~(XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
376
  
377
105k
  if (theFlag & XS_COMPACT_FLAG) {
378
80.0k
    if ((the->maximumChunksSize - the->currentChunksSize) < the->minimumChunksSize)
379
23.3k
      the->collectFlag |= XS_TRASHING_CHUNKS_FLAG;
380
56.6k
    else
381
56.6k
      the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
382
80.0k
  }
383
  
384
105k
  if ((the->maximumHeapCount - the->currentHeapCount) < the->minimumHeapCount)
385
94.6k
    the->collectFlag |= XS_TRASHING_SLOTS_FLAG;
386
11.1k
  else
387
11.1k
    the->collectFlag &= ~XS_TRASHING_SLOTS_FLAG;
388
  
389
#ifdef mxNever
390
  if (theFlag & XS_ORGANIC_FLAG) {
391
    if (theFlag & XS_COMPACT_FLAG)
392
      stopTime(&gxChunksGarbageCollectionTime);
393
    else if (theFlag & XS_COLLECT_KEYS_FLAG)
394
      stopTime(&gxKeysGarbageCollectionTime);
395
    else
396
      stopTime(&gxSlotsGarbageCollectionTime);
397
  }
398
  else
399
    stopTime(&gxForcedGarbageCollectionTime);
400
#endif
401
402
#if mxReport
403
  if (theFlag)
404
    fxReport(the, "# Chunk collection: reserved %ld used %ld peak %ld bytes\n", 
405
      (long)the->maximumChunksSize, (long)the->currentChunksSize, (long)the->peakChunksSize);
406
  aCount = 0;
407
  aSlot = the->firstHeap;
408
  while (aSlot) {
409
    aCount++;
410
    aSlot = aSlot->next;
411
  }
412
  fxReport(the, "# Slot collection: reserved %ld used %ld peak %ld bytes %d\n",
413
    (long)((the->maximumHeapCount - aCount) * sizeof(txSlot)),
414
    (long)(the->currentHeapCount * sizeof(txSlot)),
415
    (long)(the->peakHeapCount * sizeof(txSlot)),
416
    the->collectFlag & XS_TRASHING_SLOTS_FLAG);
417
#endif
418
#ifdef mxInstrument
419
  the->garbageCollectionCount++;
420
#endif
421
105k
#if defined(mxInstrument) || defined(mxProfile)
422
105k
  fxCheckProfiler(the, C_NULL);
423
105k
#endif
424
105k
}
425
426
txSlot* fxDuplicateSlot(txMachine* the, txSlot* theSlot)
427
7.91k
{
428
7.91k
  txSlot* result;
429
  
430
7.91k
  result = fxNewSlot(the);
431
7.91k
  result->ID = theSlot->ID;
432
7.91k
  result->kind = theSlot->kind;
433
7.91k
  result->flag = theSlot->flag & ~XS_MARK_FLAG;
434
7.91k
  result->value = theSlot->value;
435
7.91k
  return result;
436
7.91k
}
437
438
void* fxFindChunk(txMachine* the, txSize size, txBoolean *once)
439
348M
{
440
348M
  txBlock* block;
441
348M
  txChunk* chunk;
442
348M
#if mxStress
443
348M
  if (fxShouldStress()) {
444
12
    if (*once) {
445
12
      fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
446
12
      *once = 0;
447
12
    }
448
12
  }
449
348M
#endif
450
#if mxNoChunks
451
  if ((the->currentChunksSize + size > the->maximumChunksSize)) {
452
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
453
    if (the->collectFlag & XS_TRASHING_CHUNKS_FLAG)
454
      the->maximumChunksSize += the->minimumChunksSize;
455
  }
456
  chunk = c_malloc_noforcefail(size);
457
  if (!chunk)
458
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
459
  chunk->size = size;
460
  chunk->temporary = (txByte*)the->firstBlock;
461
  the->firstBlock = (txBlock*)chunk;
462
  return chunk;
463
#endif
464
348M
again:
465
348M
  block = the->firstBlock;
466
480M
  while (block) {
467
480M
    if ((block->current + size) <= block->limit) {
468
348M
      chunk = (txChunk*)(block->current);
469
348M
      block->current += size;
470
348M
      chunk->temporary = block->current;
471
348M
      return chunk;
472
348M
    }
473
132M
    block = block->nextBlock;
474
132M
  }
475
65.5k
  if (*once) {
476
63.6k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0), isThrashing;
477
63.6k
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
478
63.6k
    isThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0);
479
63.6k
    *once = 0;
480
63.6k
    if (wasThrashing && isThrashing)
481
2.34k
      return C_NULL;
482
61.3k
    goto again;
483
63.6k
  }
484
1.91k
  return C_NULL;
485
65.5k
}
486
487
txSlot* fxFindKey(txMachine* the)
488
23.4M
{
489
23.4M
#if mxKeysGarbageCollection
490
23.4M
  txBoolean once = 1;
491
23.4M
#endif
492
23.4M
  txID id;
493
23.4M
  txSlot* result;
494
23.4M
more:
495
23.4M
  id = the->keyIndex;
496
23.4M
  if (id < the->keyCount) {
497
19.2M
    result = fxNewSlot(the);
498
19.2M
    result->ID = id;
499
19.2M
    the->keyArray[id - the->keyOffset] = result;
500
19.2M
    the->keyIndex++;
501
19.2M
    return result;
502
19.2M
  }
503
4.15M
#if mxKeysGarbageCollection
504
4.17M
again:
505
4.17M
  result = the->keyholeList;
506
4.17M
  if (result) {
507
4.15M
    the->keyholeCount--;
508
4.15M
    the->keyholeList = result->next;
509
4.15M
    result->next = C_NULL;
510
4.15M
    return result;
511
4.15M
  }
512
18.1k
  if (once) {
513
17.4k
    fxCollect(the, XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
514
17.4k
    once = 0;
515
17.4k
    goto again;
516
17.4k
  }
517
642
#endif
518
642
  else {
519
642
    fxGrowKeys(the, 1);
520
642
    goto more;
521
642
  }
522
0
  return C_NULL;
523
18.1k
}
524
525
void fxFree(txMachine* the) 
526
33.8k
{
527
33.8k
  txSlot* aHeap;
528
529
#if mxAliasInstance
530
  if (the->aliasArray)
531
    c_free_uint32(the->aliasArray);
532
  the->aliasArray = C_NULL;
533
#endif
534
535
33.8k
  fxFreeStringInfoCache(the);
536
537
33.8k
  if (the->symbolTable)
538
33.8k
    c_free_uint32(the->symbolTable);
539
33.8k
  the->symbolTable = C_NULL;
540
33.8k
  if (the->nameTable)
541
33.8k
    c_free_uint32(the->nameTable);
542
33.8k
  the->nameTable = C_NULL;
543
33.8k
  if (the->keyArray)
544
33.8k
    c_free_uint32(the->keyArray);
545
33.8k
  the->keyArray = C_NULL;
546
  
547
70.9k
  while (the->firstHeap) {
548
37.0k
    aHeap = the->firstHeap;
549
37.0k
    the->firstHeap = aHeap->next;
550
37.0k
    fxFreeSlots(the, aHeap);
551
37.0k
  }
552
33.8k
  the->firstHeap = C_NULL;
553
554
33.8k
  if (the->stackBottom)
555
33.8k
    fxFreeSlots(the, the->stackBottom);
556
33.8k
  the->stackBottom = C_NULL;
557
33.8k
  the->stackTop = C_NULL;
558
33.8k
  the->stackIntrinsics = C_NULL;
559
33.8k
  the->stackPrototypes = C_NULL;
560
33.8k
  the->stack = C_NULL;
561
  
562
#if mxNoChunks
563
  {
564
    txChunk** address;
565
    txChunk* chunk;
566
    address = (txChunk**)&(the->firstBlock);
567
    while ((chunk = *address)) {
568
      *address = (txChunk*)(chunk->temporary);
569
      c_free(chunk);
570
    }
571
  }
572
#else
573
33.8k
  {
574
33.8k
    txBlock* aBlock;
575
71.9k
    while (the->firstBlock) {
576
38.1k
      aBlock = the->firstBlock;
577
38.1k
      the->firstBlock = aBlock->nextBlock;
578
38.1k
      fxFreeChunks(the, aBlock);
579
38.1k
    }
580
33.8k
    the->firstBlock = C_NULL;
581
33.8k
  }
582
33.8k
#endif
583
584
33.8k
#ifdef mxDebug
585
33.8k
  if (the->pathValue)
586
33.8k
    c_free(the->pathValue);
587
33.8k
  the->pathValue = C_NULL;
588
33.8k
#endif
589
590
#ifdef mxNever
591
  stopTime(&gxLifeTime);
592
//  fprintf(stderr, "###");
593
//  reportTime(&gxLifeTime);
594
//  reportTime(&gxForcedGarbageCollectionTime);
595
//  reportTime(&gxChunksGarbageCollectionTime);
596
//  reportTime(&gxKeysGarbageCollectionTime);
597
//  reportTime(&gxSlotsGarbageCollectionTime);
598
//  reportTime(&gxMarkTime);
599
//  reportTime(&gxSweepChunkTime);
600
//  reportTime(&gxSweepSlotTime);
601
//  reportTime(&gxCompactChunkTime);
602
//  fprintf(stderr, "\n");
603
#endif
604
33.8k
}
605
606
void* fxGrowChunk(txMachine* the, txSize size) 
607
4.26k
{
608
4.26k
  txBlock* block = fxGrowChunks(the, size);
609
4.26k
  txChunk* chunk = C_NULL;
610
4.26k
  if (block) {
611
4.25k
    chunk = (txChunk*)(block->current);
612
4.25k
    block->current += size;
613
4.25k
    chunk->temporary = block->current;
614
4.25k
  }
615
4.26k
  return chunk;
616
4.26k
}
617
618
void* fxGrowChunks(txMachine* the, txSize size) 
619
38.1k
{
620
38.1k
  txByte* buffer;
621
38.1k
  txBlock* block = C_NULL;
622
623
38.1k
  if (!the->minimumChunksSize && the->firstBlock) {
624
0
    fxReport(the, "# Chunk allocation: %d bytes failed in fixed size heap\n", size);
625
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
626
0
  }
627
628
38.1k
  if ((the->firstBlock != C_NULL) && (!(the->collectFlag & XS_SKIPPED_COLLECT_FLAG))) {
629
4.26k
    txSize modulo = size % (the->minimumChunksSize ? the->minimumChunksSize : 16);
630
4.26k
    if (modulo)
631
4.25k
      size = fxAddChunkSizes(the, size, the->minimumChunksSize - modulo);
632
4.26k
  }
633
38.1k
  size = fxAddChunkSizes(the, size, sizeof(txBlock));
634
38.1k
  buffer = fxAllocateChunks(the, size);
635
38.1k
  if (buffer) {
636
38.1k
  #ifdef mxSnapshot
637
38.1k
    c_memset(buffer, 0, size);
638
38.1k
  #endif
639
38.1k
    if ((the->firstBlock != C_NULL) && (the->firstBlock->limit == buffer)) {
640
0
      the->firstBlock->limit += size;
641
0
      block = the->firstBlock;
642
0
    }
643
38.1k
    else {
644
38.1k
      block = (txBlock*)buffer;
645
38.1k
      block->nextBlock = the->firstBlock;
646
38.1k
      block->current = buffer + sizeof(txBlock);
647
38.1k
      block->limit = buffer + size;
648
38.1k
      block->temporary = C_NULL;
649
38.1k
      the->firstBlock = block;
650
38.1k
      size -= sizeof(txBlock);
651
38.1k
    }
652
38.1k
    the->maximumChunksSize = fxAddChunkSizes(the, the->maximumChunksSize, size);
653
  #if mxReport
654
    fxReport(the, "# Chunk allocation: reserved %ld used %ld peak %ld bytes\n", 
655
      (long)the->maximumChunksSize, (long)the->currentChunksSize, (long)the->peakChunksSize);
656
  #endif
657
38.1k
  }
658
38.1k
  the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
659
38.1k
  return block;
660
38.1k
}
661
662
void fxGrowKeys(txMachine* the, txID theCount) 
663
642
{
664
642
  if (the->keyDelta > 0) {
665
642
    txID keyDelta = (theCount > the->keyDelta) ? theCount : the->keyDelta;
666
642
    txID keyCount = (the->keyCount + keyDelta) - the->keyOffset;
667
642
    txSlot** keyArray = c_realloc(the->keyArray, keyCount * sizeof(txSlot*));
668
642
    if (keyArray == C_NULL)
669
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
670
642
    the->keyArray = keyArray;
671
642
    the->keyCount = keyCount + the->keyOffset;
672
642
  }
673
0
  else 
674
0
    fxAbort(the, XS_NO_MORE_KEYS_EXIT);
675
642
}
676
677
void fxGrowSlots(txMachine* the, txSize theCount) 
678
37.0k
{
679
37.0k
  txSlot* aHeap;
680
37.0k
  txSlot* aSlot;
681
682
37.0k
  aHeap = fxAllocateSlots(the, theCount);
683
37.0k
  if (!aHeap) {
684
0
    fxReport(the, "# Slot allocation: failed for %ld bytes\n", theCount * sizeof(txSlot));
685
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
686
0
  }
687
37.0k
  if ((void *)-1 == aHeap)
688
0
    return;
689
    
690
37.0k
  if (the->firstHeap && the->growHeapDirection) {
691
0
    if (the->growHeapDirection > 0) {
692
0
      the->firstHeap->value.reference = aHeap + theCount;
693
0
      the->maximumHeapCount += theCount;    
694
0
      theCount -= 1;
695
0
      aSlot = aHeap;
696
0
    }
697
0
    else {
698
0
      *aHeap = *(the->firstHeap);
699
0
      the->maximumHeapCount += theCount;
700
0
      theCount -= 1;
701
0
      the->firstHeap = aHeap;
702
0
      aSlot = aHeap + 1;
703
0
    }
704
0
  }
705
37.0k
  else {
706
37.0k
    the->maximumHeapCount += theCount - 1;
707
37.0k
    aHeap->next = the->firstHeap;
708
37.0k
    aHeap->ID = 0;
709
37.0k
    aHeap->flag = 0;
710
37.0k
    aHeap->kind = 0;
711
37.0k
    aHeap->value.reference = aHeap + theCount;
712
37.0k
    theCount -= 2;
713
37.0k
    the->firstHeap = aHeap;
714
37.0k
    aSlot = aHeap + 1;
715
37.0k
  }
716
1.21G
    while (theCount--) {
717
1.21G
    txSlot* next = aSlot + 1;
718
1.21G
    aSlot->next = next;
719
1.21G
    aSlot->flag = XS_NO_FLAG;
720
1.21G
    aSlot->kind = XS_UNDEFINED_KIND;
721
  #if mxPoisonSlots
722
    ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
723
  #endif
724
1.21G
        aSlot = next;
725
1.21G
    }
726
37.0k
  aSlot->next = the->freeHeap;
727
37.0k
  aSlot->flag = XS_NO_FLAG;
728
37.0k
  aSlot->kind = XS_UNDEFINED_KIND;
729
#if mxPoisonSlots
730
  ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
731
#endif
732
37.0k
  the->freeHeap = aHeap + 1;
733
37.0k
  the->collectFlag &= ~XS_TRASHING_SLOTS_FLAG;
734
#if mxReport
735
  fxReport(the, "# Slot allocation: reserved %ld used %ld peak %ld bytes\n", 
736
    (long)(the->maximumHeapCount * sizeof(txSlot)),
737
    (long)(the->currentHeapCount * sizeof(txSlot)),
738
    (long)(the->peakHeapCount * sizeof(txSlot)));
739
#endif
740
37.0k
}
741
742
void fxMark(txMachine* the, void (*theMarker)(txMachine*, txSlot*))
743
105k
{
744
105k
  txSlot** p;
745
105k
  txSlot** q;
746
105k
  txSlot* slot;
747
748
#if mxAliasInstance
749
  p = the->aliasArray;
750
  q = p + the->aliasCount;
751
  while (p < q) {
752
    if ((slot = *p)) {
753
      (*theMarker)(the, slot);
754
      slot->flag |= XS_MARK_FLAG;
755
    }
756
    p++;
757
  }
758
#endif
759
  
760
105k
  slot = the->stackTop;
761
133M
  while (slot > the->stack) {
762
133M
        slot--;
763
133M
    (*theMarker)(the, slot);
764
133M
  }
765
105k
  slot = the->cRoot;
766
105k
  while (slot) {
767
0
    (*theMarker)(the, slot);
768
0
    slot = slot->next;
769
0
  }
770
  
771
105k
#if mxKeysGarbageCollection
772
105k
  if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
773
33.8k
    txInteger deletions = 0;
774
33.8k
    p = the->keyArray;
775
33.8k
    q = p + the->keyIndex - the->keyOffset;
776
33.9M
    while (p < q) {
777
33.9M
      slot = *p++;
778
33.9M
      if (!(slot->flag & XS_MARK_FLAG)) {
779
13.7M
        if (slot->flag & XS_DONT_DELETE_FLAG)
780
4.66M
          slot->flag |= XS_MARK_FLAG;
781
9.08M
        else if (slot->flag & XS_DONT_ENUM_FLAG)
782
2.15M
          deletions++;
783
13.7M
      }
784
33.9M
    }
785
  
786
  //  fprintf(stderr, "\n### KEYS GC %d", deletions);
787
33.8k
    p = the->nameTable;
788
33.8k
    q = the->nameTable + the->nameModulo;
789
43.3M
    while ((p < q) && deletions) {
790
43.3M
      txSlot** address = p;
791
60.5M
      while (((slot = *address)) && deletions) {
792
17.1M
        if (slot->flag & XS_MARK_FLAG)
793
14.9M
          address = &(slot->next);
794
2.15M
        else {
795
2.15M
          *address = slot->next;
796
2.15M
          deletions--;
797
2.15M
        }
798
17.1M
      }
799
43.3M
      p++;
800
43.3M
    }
801
  //  fprintf(stderr, " => %d", deletions);
802
  
803
33.8k
    the->keyholeCount = 0;
804
33.8k
    the->keyholeList = C_NULL;
805
33.8k
    p = the->keyArray;
806
33.8k
    q = p + the->keyIndex - the->keyOffset;
807
33.9M
    while (p < q) {
808
33.9M
      slot = *p;
809
33.9M
      if (slot->flag & XS_MARK_FLAG)
810
24.8M
        (*theMarker)(the, slot);
811
9.08M
      else {
812
  //      if (slot->kind != XS_UNDEFINED_KIND) {  
813
  //        fxIDToString(the, slot->ID, the->nameBuffer, sizeof(the->nameBuffer));
814
  //        fprintf(stderr, "\n%p %d %s", slot, slot->ID, the->nameBuffer);
815
  //      }
816
9.08M
        slot->flag = XS_INTERNAL_FLAG | XS_MARK_FLAG;
817
9.08M
        slot->next = the->keyholeList;
818
9.08M
        slot->kind = XS_UNDEFINED_KIND;
819
9.08M
        the->keyholeCount++;
820
9.08M
        the->keyholeList = slot;
821
9.08M
      }
822
33.9M
      p++;
823
33.9M
    }
824
  //  fprintf(stderr, "\n");
825
33.8k
  }
826
72.0k
  else
827
72.0k
#endif
828
72.0k
  {
829
72.0k
    p = the->keyArray;
830
72.0k
    q = p + the->keyIndex - the->keyOffset;
831
41.1M
    while (p < q) {
832
41.0M
      slot = *p++;
833
41.0M
      slot->flag |= XS_MARK_FLAG;
834
41.0M
      (*theMarker)(the, slot);
835
41.0M
    }
836
72.0k
  }
837
105k
}
838
839
#if mxKeysGarbageCollection
840
void fxMarkID(txMachine* the, txID id)
841
161M
{
842
161M
  txSlot* slot;
843
161M
  if (id == XS_NO_ID)
844
63.1M
    return;
845
98.7M
  if (id < the->keyOffset)
846
0
    return;
847
98.7M
  id -= the->keyOffset;
848
98.7M
  slot = the->keyArray[id];
849
98.7M
  slot->flag |= XS_MARK_FLAG;
850
98.7M
}
851
#endif
852
853
void fxMarkFinalizationRegistry(txMachine* the, txSlot* registry) 
854
409k
{
855
409k
  txSlot* slot = registry->value.finalizationRegistry.callback->next;
856
409k
  txSlot* instance;
857
1.32M
  while (slot) {
858
916k
    slot = slot->next;
859
916k
    if (slot) {
860
916k
      instance = slot->value.finalizationCell.target;
861
916k
      if (instance && !(instance->flag & XS_MARK_FLAG)) {
862
64.7k
        slot->value.finalizationCell.target = C_NULL;
863
64.7k
        registry->value.finalizationRegistry.flags |= XS_FINALIZATION_REGISTRY_CHANGED;
864
64.7k
      }
865
916k
      instance = slot->value.finalizationCell.token;
866
916k
      if (instance && !(instance->flag & XS_MARK_FLAG))
867
17.9k
        slot->value.finalizationCell.token = C_NULL;
868
916k
      slot = slot->next;
869
916k
    }
870
916k
  }
871
409k
}
872
873
void fxMarkInstance(txMachine* the, txSlot* theCurrent, void (*theMarker)(txMachine*, txSlot*))
874
182M
{
875
182M
  txSlot* aProperty;
876
182M
  txSlot* aTemporary;
877
878
182M
  mxCheck(the, theCurrent->kind == XS_INSTANCE_KIND);
879
182M
  aProperty = theCurrent;
880
182M
  theCurrent->value.instance.garbage = C_NULL;
881
2.68G
  for (;;) {
882
2.68G
    if (aProperty) {
883
2.29G
      if (!(aProperty->flag & XS_MARK_FLAG)) {
884
2.29G
        aProperty->flag |= XS_MARK_FLAG;
885
2.29G
        switch (aProperty->kind) {
886
387M
        case XS_INSTANCE_KIND:
887
387M
          aTemporary = aProperty->value.instance.prototype;
888
387M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
889
950k
            aProperty->value.instance.prototype = theCurrent;
890
950k
            theCurrent = aTemporary;
891
950k
            theCurrent->value.instance.garbage = aProperty;
892
950k
            aProperty = theCurrent;
893
950k
          }
894
386M
          else
895
386M
            aProperty = aProperty->next;
896
387M
          break;
897
239M
        case XS_REFERENCE_KIND:
898
239M
        #if mxKeysGarbageCollection
899
239M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
900
25.6M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
901
24.8M
              fxMarkID(the, aProperty->ID);
902
25.6M
          }
903
239M
        #endif
904
239M
          aTemporary = aProperty->value.reference;
905
239M
          if (!(aTemporary->flag & XS_MARK_FLAG)) {
906
200M
            aProperty->value.reference = theCurrent;
907
200M
            theCurrent = aTemporary;
908
200M
            theCurrent->value.instance.garbage = aProperty;
909
200M
            aProperty = theCurrent;
910
200M
          }
911
38.3M
          else
912
38.3M
            aProperty = aProperty->next;
913
239M
          break;
914
          
915
1.08M
        case XS_PROXY_KIND:
916
1.08M
          aTemporary = aProperty->value.proxy.handler;
917
1.08M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
918
1.08M
            aProperty->flag |= XS_INSPECTOR_FLAG;
919
1.08M
            aProperty->value.proxy.handler = theCurrent;
920
1.08M
            theCurrent = aTemporary;
921
1.08M
            theCurrent->value.instance.garbage = aProperty;
922
1.08M
            aProperty = theCurrent;
923
1.08M
          }
924
2.35k
          else {
925
2.35k
            aTemporary = aProperty->value.proxy.target;
926
2.35k
            if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
927
6
              aProperty->value.proxy.target = theCurrent;
928
6
              theCurrent = aTemporary;
929
6
              theCurrent->value.instance.garbage = aProperty;
930
6
              aProperty = theCurrent;
931
6
            }
932
2.34k
            else
933
2.34k
              aProperty = aProperty->next;
934
2.35k
          }
935
1.08M
          break;
936
          
937
6.37M
        case XS_CLOSURE_KIND:
938
6.37M
        #if mxKeysGarbageCollection
939
6.37M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
940
262k
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
941
149k
              fxMarkID(the, aProperty->ID);
942
262k
          }
943
6.37M
        #endif
944
6.37M
          aTemporary = aProperty->value.closure;
945
6.37M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
946
2.54M
            aTemporary->flag |= XS_MARK_FLAG; 
947
2.54M
            if (aTemporary->kind == XS_REFERENCE_KIND) {
948
667k
              aTemporary = aTemporary->value.reference;
949
667k
              if (!(aTemporary->flag & XS_MARK_FLAG)) {
950
495k
                aProperty->value.closure->value.reference = theCurrent;
951
495k
                theCurrent = aTemporary;
952
495k
                theCurrent->value.instance.garbage = aProperty;
953
495k
                aProperty = theCurrent;
954
            
955
495k
              }
956
667k
            }
957
1.88M
            else {
958
1.88M
              (*theMarker)(the, aTemporary);
959
1.88M
              aProperty = aProperty->next;
960
1.88M
            }
961
2.54M
          }
962
3.82M
          else
963
3.82M
            aProperty = aProperty->next;
964
6.37M
          break;
965
          
966
87.5M
        case XS_CALLBACK_KIND:
967
92.8M
        case XS_CODE_KIND:
968
92.8M
        #if mxKeysGarbageCollection
969
92.8M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
970
20.5M
            fxMarkID(the, aProperty->ID);
971
92.8M
        #endif
972
92.8M
          (*theMarker)(the, aProperty);
973
92.8M
          aProperty = aProperty->next;
974
92.8M
          break;  
975
        
976
1.56G
        default:
977
1.56G
        #if mxKeysGarbageCollection
978
1.56G
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
979
119M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
980
81.1M
              fxMarkID(the, aProperty->ID);
981
119M
          }
982
1.56G
        #endif
983
1.56G
          (*theMarker)(the, aProperty);
984
1.56G
          aProperty = aProperty->next;
985
1.56G
          break; 
986
2.29G
        }
987
2.29G
      }
988
191k
      else
989
191k
        aProperty = aProperty->next;
990
2.29G
    }
991
387M
    else if (theCurrent->value.instance.garbage) {
992
204M
      aProperty = theCurrent->value.instance.garbage;
993
204M
      theCurrent->value.instance.garbage = C_NULL;
994
204M
      switch (aProperty->kind) {
995
950k
      case XS_INSTANCE_KIND:
996
950k
        aTemporary = aProperty->value.instance.prototype;
997
950k
        aProperty->value.instance.prototype = theCurrent;
998
950k
        theCurrent = aTemporary;
999
950k
        aProperty = aProperty->next;
1000
950k
        break;
1001
200M
      case XS_REFERENCE_KIND:
1002
200M
        aTemporary = aProperty->value.reference;
1003
200M
        aProperty->value.reference = theCurrent;
1004
200M
        theCurrent = aTemporary;
1005
200M
        aProperty = aProperty->next;
1006
200M
        break;
1007
2.17M
      case XS_PROXY_KIND:
1008
2.17M
        if (aProperty->flag & XS_INSPECTOR_FLAG) {
1009
1.08M
          aProperty->flag &= ~XS_INSPECTOR_FLAG;
1010
1.08M
          aTemporary = aProperty->value.proxy.handler;
1011
1.08M
          aProperty->value.proxy.handler = theCurrent;
1012
1.08M
          theCurrent = aTemporary;
1013
          
1014
1.08M
          aTemporary = aProperty->value.proxy.target;
1015
1.08M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
1016
1.08M
            aProperty->value.proxy.target = theCurrent;
1017
1.08M
            theCurrent = aTemporary;
1018
1.08M
            theCurrent->value.instance.garbage = aProperty;
1019
1.08M
            aProperty = theCurrent;
1020
1.08M
          }
1021
649
          else {
1022
649
            aProperty = aProperty->next;
1023
649
          }
1024
1.08M
        }
1025
1.08M
        else {
1026
1.08M
          aTemporary = aProperty->value.proxy.target;
1027
1.08M
          aProperty->value.proxy.target = theCurrent;
1028
1.08M
          theCurrent = aTemporary;
1029
1.08M
          aProperty = aProperty->next;
1030
1.08M
        }
1031
2.17M
        break;
1032
495k
      case XS_CLOSURE_KIND:
1033
495k
        aTemporary = aProperty->value.closure->value.reference;
1034
495k
        aProperty->value.closure->value.reference = theCurrent;
1035
495k
        theCurrent = aTemporary;
1036
495k
        aProperty = aProperty->next;
1037
495k
        break;
1038
204M
      }
1039
204M
    }
1040
182M
    else
1041
182M
      break;
1042
2.68G
  }
1043
182M
}
1044
1045
void fxMarkReference(txMachine* the, txSlot* theSlot)
1046
1.34G
{
1047
1.34G
  txSlot* aSlot;
1048
1.34G
  switch (theSlot->kind) {
1049
94.9M
  case XS_REFERENCE_KIND:
1050
94.9M
    aSlot = theSlot->value.reference;
1051
94.9M
    if (!(aSlot->flag & XS_MARK_FLAG))
1052
76.3M
      fxMarkInstance(the, aSlot, fxMarkReference);
1053
94.9M
    break;
1054
2.71M
  case XS_CLOSURE_KIND:
1055
2.71M
    aSlot = theSlot->value.closure;
1056
2.71M
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG))) {
1057
384k
      aSlot->flag |= XS_MARK_FLAG; 
1058
384k
      fxMarkReference(the, aSlot);
1059
384k
    }
1060
2.71M
    break;
1061
588k
  case XS_INSTANCE_KIND:
1062
588k
    if (!(theSlot->flag & XS_MARK_FLAG))
1063
588k
      fxMarkInstance(the, theSlot, fxMarkReference);
1064
588k
    break;
1065
2.30M
  case XS_ACCESSOR_KIND:
1066
2.30M
    aSlot = theSlot->value.accessor.getter;
1067
2.30M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1068
2.26M
      fxCheckCStack(the);
1069
2.26M
      fxMarkInstance(the, aSlot, fxMarkReference);
1070
2.26M
    }
1071
2.30M
    aSlot = theSlot->value.accessor.setter;
1072
2.30M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1073
994k
      fxCheckCStack(the);
1074
994k
      fxMarkInstance(the, aSlot, fxMarkReference);
1075
994k
    }
1076
2.30M
    break;
1077
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1078
0
  case XS_ARGUMENTS_STRICT_KIND:
1079
125M
  case XS_ARRAY_KIND:
1080
127M
  case XS_STACK_KIND:
1081
127M
    fxCheckCStack(the);
1082
127M
    if ((aSlot = theSlot->value.array.address)) {
1083
51.1M
      txIndex aLength = (((txChunk*)(((txByte*)aSlot) - sizeof(txChunk)))->size) / sizeof(txSlot);
1084
51.1M
      if (aLength > theSlot->value.array.length)
1085
32.1k
        aLength = theSlot->value.array.length;
1086
366M
      while (aLength) {
1087
315M
        fxMarkReference(the, aSlot);
1088
315M
        aSlot++;
1089
315M
        aLength--;
1090
315M
      }
1091
51.1M
    }
1092
127M
    break;
1093
31.6M
  case XS_CALLBACK_KIND:
1094
31.6M
    aSlot = theSlot->value.callback.closures;
1095
31.6M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1096
0
      fxCheckCStack(the);
1097
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1098
0
    }
1099
31.6M
    break;
1100
4.01M
  case XS_CODE_KIND:
1101
    // continue
1102
4.08M
  case XS_CODE_X_KIND:
1103
4.08M
    aSlot = theSlot->value.code.closures;
1104
4.08M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1105
1.92M
      fxCheckCStack(the);
1106
1.92M
      fxMarkInstance(the, aSlot, fxMarkReference);
1107
1.92M
    }
1108
4.08M
    break;
1109
35.7M
  case XS_HOME_KIND:
1110
35.7M
    aSlot = theSlot->value.home.object;
1111
35.7M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1112
10.6M
      fxCheckCStack(the);
1113
10.6M
      fxMarkInstance(the, aSlot, fxMarkReference);
1114
10.6M
    }
1115
35.7M
    aSlot = theSlot->value.home.module;
1116
35.7M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1117
2.07k
      fxCheckCStack(the);
1118
2.07k
      fxMarkInstance(the, aSlot, fxMarkReference);
1119
2.07k
    }
1120
35.7M
    break;
1121
36
  case XS_MODULE_KIND:
1122
25.8k
  case XS_PROGRAM_KIND:
1123
25.8k
#if mxKeysGarbageCollection
1124
25.8k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1125
17.5k
      fxMarkID(the, theSlot->value.module.id);
1126
25.8k
#endif
1127
25.8k
    aSlot = theSlot->value.module.realm;
1128
25.8k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1129
25.8k
      fxCheckCStack(the);
1130
25.8k
      fxMarkInstance(the, aSlot, fxMarkReference);
1131
25.8k
    }
1132
25.8k
    break;
1133
0
  case XS_EXPORT_KIND:
1134
0
    aSlot = theSlot->value.export.closure;
1135
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1136
0
      aSlot->flag |= XS_MARK_FLAG; 
1137
0
      fxMarkReference(the, aSlot);
1138
0
    }
1139
0
    aSlot = theSlot->value.export.module;
1140
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG))
1141
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1142
0
    break;
1143
34.1k
  case XS_HOST_KIND:
1144
34.1k
    if (theSlot->value.host.data) {
1145
8.36k
      if ((theSlot->flag & XS_HOST_HOOKS_FLAG) && (theSlot->value.host.variant.hooks->marker))
1146
0
        (*theSlot->value.host.variant.hooks->marker)(the, theSlot->value.host.data, fxMarkReference);
1147
8.36k
    }
1148
34.1k
    break;
1149
0
  case XS_PROXY_KIND:
1150
0
    aSlot = theSlot->value.proxy.handler;
1151
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG))
1152
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1153
0
    aSlot = theSlot->value.proxy.target;
1154
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG))
1155
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1156
0
    break;
1157
    
1158
0
  case XS_BREAKPOINT_KIND:
1159
0
    aSlot = theSlot->value.breakpoint.info;
1160
0
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1161
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1162
0
    break;
1163
533k
  case XS_ERROR_KIND:
1164
533k
    aSlot = theSlot->value.error.info;
1165
533k
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1166
533k
      fxMarkInstance(the, aSlot, fxMarkReference);
1167
533k
    break;
1168
113
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1169
165
  case XS_DISPOSABLE_STACK_KIND:
1170
119k
  case XS_LIST_KIND:
1171
119k
    fxCheckCStack(the);
1172
119k
    aSlot = theSlot->value.list.first;
1173
154k
    while (aSlot) {
1174
34.9k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1175
34.9k
        aSlot->flag |= XS_MARK_FLAG;
1176
34.9k
        fxMarkReference(the, aSlot);
1177
34.9k
      }
1178
34.9k
      aSlot = aSlot->next;
1179
34.9k
    }
1180
119k
    break;
1181
    
1182
0
  case XS_PRIVATE_KIND:
1183
0
    fxCheckCStack(the);
1184
0
    aSlot = theSlot->value.private.check;
1185
0
    if (!(aSlot->flag & XS_MARK_FLAG))
1186
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1187
0
    aSlot = theSlot->value.private.first;
1188
0
    while (aSlot) {
1189
0
      aSlot->flag |= XS_MARK_FLAG;
1190
0
      fxMarkReference(the, aSlot);
1191
0
      aSlot = aSlot->next;
1192
0
    }
1193
0
    break;
1194
1195
38
  case XS_MAP_KIND:
1196
14.2k
  case XS_SET_KIND:
1197
14.2k
    {
1198
14.2k
      txSlot** anAddress = theSlot->value.table.address;
1199
14.2k
      txInteger aLength = theSlot->value.table.length;
1200
82.6k
      while (aLength) {
1201
68.3k
        aSlot = *anAddress;
1202
103k
        while (aSlot) {
1203
34.8k
          aSlot->flag |= XS_MARK_FLAG; 
1204
34.8k
          aSlot = aSlot->next;
1205
34.8k
        }
1206
68.3k
        anAddress++;
1207
68.3k
        aLength--;
1208
68.3k
      }
1209
14.2k
    }
1210
14.2k
    break;
1211
275k
  case XS_WEAK_MAP_KIND:
1212
275k
  case XS_WEAK_SET_KIND:
1213
275k
    aSlot = theSlot->value.weakList.first;
1214
374k
    while (aSlot) {
1215
99.8k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1216
99.8k
        aSlot->flag |= XS_MARK_FLAG;
1217
99.8k
        fxMarkReference(the, aSlot);
1218
99.8k
      }
1219
99.8k
      aSlot = aSlot->next;
1220
99.8k
    }
1221
275k
    break;
1222
201k
  case XS_WEAK_ENTRY_KIND:
1223
201k
    aSlot = theSlot->value.weakEntry.check;
1224
201k
    if (aSlot->flag & XS_MARK_FLAG) {
1225
90.3k
      aSlot = theSlot->value.weakEntry.value;
1226
90.3k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1227
90.3k
        aSlot->flag |= XS_MARK_FLAG; 
1228
90.3k
        fxMarkReference(the, aSlot);
1229
90.3k
      }
1230
90.3k
    }
1231
201k
    break;
1232
587k
  case XS_WEAK_REF_KIND:
1233
587k
    aSlot = theSlot->value.weakRef.target;
1234
587k
    if (aSlot) {
1235
587k
  #ifdef mxSnapshot
1236
587k
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1237
587k
        fxMarkReference(the, aSlot);
1238
587k
      }
1239
0
      else {
1240
0
        theSlot->value.weakRef.link = the->firstWeakRefLink;
1241
0
        the->firstWeakRefLink = theSlot;
1242
0
      }
1243
  #else
1244
      theSlot->value.weakRef.link = the->firstWeakRefLink;
1245
      the->firstWeakRefLink = theSlot;
1246
  #endif
1247
587k
    }
1248
587k
    break;
1249
293k
  case XS_FINALIZATION_REGISTRY_KIND:
1250
293k
    aSlot = theSlot->value.finalizationRegistry.callback;
1251
293k
    if (aSlot) {
1252
293k
      fxCheckCStack(the);
1253
293k
      aSlot->flag |= XS_MARK_FLAG;
1254
293k
      fxMarkReference(the, aSlot);
1255
293k
      aSlot = aSlot->next;
1256
900k
      while (aSlot) {
1257
606k
        aSlot->flag |= XS_MARK_FLAG;
1258
606k
        fxMarkReference(the, aSlot); // holdings
1259
606k
        aSlot = aSlot->next;
1260
606k
        if (aSlot) {
1261
606k
          aSlot->flag |= XS_MARK_FLAG;
1262
          // weak target and token
1263
606k
          aSlot = aSlot->next;
1264
606k
        }
1265
606k
      }
1266
293k
    }
1267
293k
    break;
1268
    
1269
0
  case XS_HOST_INSPECTOR_KIND:
1270
0
    aSlot = theSlot->value.hostInspector.cache;
1271
0
    if (!(aSlot->flag & XS_MARK_FLAG))
1272
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1273
0
    break;  
1274
0
#if mxKeysGarbageCollection
1275
1.16M
  case XS_SYMBOL_KIND:
1276
1.16M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1277
683k
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1278
327k
        fxMarkID(the, theSlot->value.symbol);
1279
683k
    }
1280
1.16M
    break;  
1281
13.7M
  case XS_AT_KIND:
1282
13.7M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1283
8.92M
      fxMarkID(the, theSlot->value.at.id);
1284
13.7M
    break; 
1285
1.34G
#endif
1286
1.34G
  }
1287
1.34G
}
1288
1289
void fxMarkValue(txMachine* the, txSlot* theSlot)
1290
1.35G
{
1291
1.35G
#define mxMarkChunk(_THE_DATA) \
1292
1.35G
  ((txChunk*)(((txByte*)_THE_DATA) - sizeof(txChunk)))->size |= mxChunkFlag
1293
1294
1.35G
  txSlot* aSlot;
1295
1.35G
  switch (theSlot->kind) {
1296
341M
  case XS_STRING_KIND:
1297
341M
    mxMarkChunk(theSlot->value.string);
1298
341M
    break;
1299
17.8k
  case XS_BIGINT_KIND:
1300
17.8k
    mxMarkChunk(theSlot->value.bigint.data);
1301
17.8k
    break;
1302
145M
  case XS_REFERENCE_KIND:
1303
145M
    aSlot = theSlot->value.reference;
1304
145M
    if (!(aSlot->flag & XS_MARK_FLAG))
1305
74.1M
      fxMarkInstance(the, aSlot, fxMarkValue);
1306
145M
    break;
1307
2.65M
  case XS_CLOSURE_KIND:
1308
2.65M
    aSlot = theSlot->value.closure;
1309
2.65M
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG))) {
1310
260k
      aSlot->flag |= XS_MARK_FLAG; 
1311
260k
      fxMarkValue(the, aSlot);
1312
260k
    }
1313
2.65M
    break;
1314
2.38M
  case XS_INSTANCE_KIND:
1315
2.38M
    if (!(theSlot->flag & XS_MARK_FLAG))
1316
2.38M
      fxMarkInstance(the, theSlot, fxMarkValue);
1317
2.38M
    break;
1318
    
1319
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1320
0
  case XS_ARGUMENTS_STRICT_KIND:
1321
59.7M
  case XS_ARRAY_KIND:
1322
68.8M
  case XS_STACK_KIND:
1323
68.8M
    fxCheckCStack(the);
1324
68.8M
    if ((aSlot = theSlot->value.array.address)) {
1325
60.8M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
1326
60.8M
      if (!(chunk->size & mxChunkFlag)) {
1327
60.8M
        txIndex aLength = chunk->size / sizeof(txSlot);
1328
60.8M
        if (aLength > theSlot->value.array.length)
1329
17.8k
          aLength = theSlot->value.array.length;
1330
580M
        while (aLength) {
1331
519M
          fxMarkValue(the, aSlot);
1332
519M
          aSlot++;
1333
519M
          aLength--;
1334
519M
        }
1335
60.8M
        mxMarkChunk(theSlot->value.array.address);
1336
60.8M
      }
1337
60.8M
    }
1338
68.8M
    break;
1339
1.17M
  case XS_ARRAY_BUFFER_KIND:
1340
1.17M
    if (theSlot->value.arrayBuffer.address)
1341
1.17M
      mxMarkChunk(theSlot->value.arrayBuffer.address);
1342
1.17M
    break;
1343
55.8M
  case XS_CALLBACK_KIND:
1344
55.8M
    aSlot = theSlot->value.callback.closures;
1345
55.8M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1346
0
      fxCheckCStack(the);
1347
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1348
0
    }
1349
55.8M
    break;
1350
1.34M
  case XS_CODE_KIND:
1351
1.34M
    mxMarkChunk(theSlot->value.code.address);
1352
    /* continue */
1353
1.58M
  case XS_CODE_X_KIND:
1354
1.58M
    aSlot = theSlot->value.code.closures;
1355
1.58M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1356
509k
      fxCheckCStack(the);
1357
509k
      fxMarkInstance(the, aSlot, fxMarkValue);
1358
509k
    }
1359
1.58M
    break;
1360
80.0k
  case XS_GLOBAL_KIND:
1361
80.0k
    mxMarkChunk(theSlot->value.table.address);
1362
80.0k
    break;
1363
212k
  case XS_HOST_KIND:
1364
212k
    if (theSlot->value.host.data) {
1365
132k
      if ((theSlot->flag & XS_HOST_HOOKS_FLAG) && (theSlot->value.host.variant.hooks->marker))
1366
0
        (*theSlot->value.host.variant.hooks->marker)(the, theSlot->value.host.data, fxMarkValue);
1367
132k
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
1368
129
        mxMarkChunk(theSlot->value.host.data);
1369
132k
    }
1370
212k
    break;
1371
5
  case XS_IDS_KIND:
1372
5
    if (theSlot->value.IDs)
1373
5
      mxMarkChunk(theSlot->value.IDs);
1374
5
    break;
1375
0
  case XS_PROXY_KIND:
1376
0
    aSlot = theSlot->value.proxy.handler;
1377
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG))
1378
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1379
0
    aSlot = theSlot->value.proxy.target;
1380
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG))
1381
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1382
0
    break;
1383
28.0k
  case XS_REGEXP_KIND:
1384
28.0k
    if (theSlot->value.regexp.code)
1385
27.4k
      mxMarkChunk(theSlot->value.regexp.code);
1386
28.0k
    if (theSlot->value.regexp.data)
1387
27.9k
      mxMarkChunk(theSlot->value.regexp.data);
1388
28.0k
    break;
1389
    
1390
4.81M
  case XS_ACCESSOR_KIND:
1391
4.81M
    aSlot = theSlot->value.accessor.getter;
1392
4.81M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1393
4.73M
      fxCheckCStack(the);
1394
4.73M
      fxMarkInstance(the, aSlot, fxMarkValue);
1395
4.73M
    }
1396
4.81M
    aSlot = theSlot->value.accessor.setter;
1397
4.81M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1398
810k
      fxCheckCStack(the);
1399
810k
      fxMarkInstance(the, aSlot, fxMarkValue);
1400
810k
    }
1401
4.81M
    break;
1402
57.3M
  case XS_HOME_KIND:
1403
57.3M
    aSlot = theSlot->value.home.object;
1404
57.3M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1405
4.27M
      fxCheckCStack(the);
1406
4.27M
      fxMarkInstance(the, aSlot, fxMarkValue);
1407
4.27M
    }
1408
57.3M
    aSlot = theSlot->value.home.module;
1409
57.3M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1410
5.84k
      fxCheckCStack(the);
1411
5.84k
      fxMarkInstance(the, aSlot, fxMarkValue);
1412
5.84k
    }
1413
57.3M
    break;
1414
89
  case XS_MODULE_KIND:
1415
80.1k
  case XS_PROGRAM_KIND:
1416
80.1k
#if mxKeysGarbageCollection
1417
80.1k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1418
16.4k
      fxMarkID(the, theSlot->value.module.id);
1419
80.1k
#endif
1420
80.1k
    aSlot = theSlot->value.module.realm;
1421
80.1k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1422
80.0k
      fxCheckCStack(the);
1423
80.0k
      fxMarkInstance(the, aSlot, fxMarkValue);
1424
80.0k
    }
1425
80.1k
    break;
1426
0
  case XS_EXPORT_KIND:
1427
0
    aSlot = theSlot->value.export.closure;
1428
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1429
0
      aSlot->flag |= XS_MARK_FLAG; 
1430
0
      fxMarkValue(the, aSlot);
1431
0
    }
1432
0
    aSlot = theSlot->value.export.module;
1433
0
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG))
1434
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1435
0
    break;
1436
102M
  case XS_KEY_KIND:
1437
102M
    if (theSlot->value.key.string)
1438
102M
      mxMarkChunk(theSlot->value.key.string);
1439
102M
    break;
1440
    
1441
0
  case XS_BREAKPOINT_KIND:
1442
0
    aSlot = theSlot->value.breakpoint.info;
1443
0
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1444
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1445
0
    break;
1446
2.37M
  case XS_ERROR_KIND:
1447
2.37M
    aSlot = theSlot->value.error.info;
1448
2.37M
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1449
2.37M
      fxMarkInstance(the, aSlot, fxMarkValue);
1450
2.37M
    break;
1451
53
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1452
93
  case XS_DISPOSABLE_STACK_KIND:
1453
257k
  case XS_LIST_KIND:
1454
257k
    fxCheckCStack(the);
1455
257k
    aSlot = theSlot->value.list.first;
1456
280k
    while (aSlot) {
1457
23.3k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1458
23.3k
        aSlot->flag |= XS_MARK_FLAG;
1459
23.3k
        fxMarkValue(the, aSlot);
1460
23.3k
      }
1461
23.3k
      aSlot = aSlot->next;
1462
23.3k
    }
1463
257k
    break;
1464
    
1465
1
  case XS_PRIVATE_KIND:
1466
1
    fxCheckCStack(the);
1467
1
    aSlot = theSlot->value.private.check;
1468
1
    if (!(aSlot->flag & XS_MARK_FLAG))
1469
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1470
1
    aSlot = theSlot->value.private.first;
1471
2
    while (aSlot) {
1472
1
      aSlot->flag |= XS_MARK_FLAG;
1473
1
      fxMarkValue(the, aSlot);
1474
1
      aSlot = aSlot->next;
1475
1
    }
1476
1
    break;
1477
1478
12
  case XS_MAP_KIND:
1479
5.14k
  case XS_SET_KIND:
1480
5.14k
    {
1481
5.14k
      txSlot** anAddress = theSlot->value.table.address;
1482
5.14k
      txInteger aLength = theSlot->value.table.length;
1483
48.2k
      while (aLength) {
1484
43.0k
        aSlot = *anAddress;
1485
66.3k
        while (aSlot) {
1486
23.3k
          aSlot->flag |= XS_MARK_FLAG; 
1487
23.3k
          aSlot = aSlot->next;
1488
23.3k
        }
1489
43.0k
        anAddress++;
1490
43.0k
        aLength--;
1491
43.0k
      }
1492
5.14k
    }
1493
5.14k
    mxMarkChunk(theSlot->value.table.address);
1494
5.14k
    break;
1495
1496
124k
  case XS_WEAK_MAP_KIND:
1497
124k
  case XS_WEAK_SET_KIND:
1498
124k
    aSlot = theSlot->value.weakList.first;
1499
183k
    while (aSlot) {
1500
58.3k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1501
58.3k
        aSlot->flag |= XS_MARK_FLAG;
1502
58.3k
        fxMarkValue(the, aSlot);
1503
58.3k
      }
1504
58.3k
      aSlot = aSlot->next;
1505
58.3k
    }
1506
124k
    break;
1507
121k
  case XS_WEAK_ENTRY_KIND:
1508
121k
    aSlot = theSlot->value.weakEntry.check;
1509
121k
    if (aSlot->flag & XS_MARK_FLAG) {
1510
56.5k
      aSlot = theSlot->value.weakEntry.value;
1511
56.5k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1512
56.5k
        aSlot->flag |= XS_MARK_FLAG; 
1513
56.5k
        fxMarkValue(the, aSlot);
1514
56.5k
      }
1515
56.5k
    }
1516
121k
    break;
1517
2.38M
  case XS_WEAK_REF_KIND:
1518
2.38M
    aSlot = theSlot->value.weakRef.target;
1519
2.38M
    if (aSlot) {
1520
2.38M
  #ifdef mxSnapshot
1521
2.38M
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1522
2.38M
        fxMarkValue(the, aSlot);
1523
2.38M
      }
1524
131
      else {
1525
131
        theSlot->value.weakRef.link = the->firstWeakRefLink;
1526
131
        the->firstWeakRefLink = theSlot;
1527
131
      }
1528
  #else
1529
      theSlot->value.weakRef.link = the->firstWeakRefLink;
1530
      the->firstWeakRefLink = theSlot;
1531
  #endif
1532
2.38M
    }
1533
2.38M
    break;
1534
135k
  case XS_FINALIZATION_REGISTRY_KIND:
1535
135k
    aSlot = theSlot->value.finalizationRegistry.callback;
1536
135k
    if (aSlot) {
1537
135k
      aSlot->flag |= XS_MARK_FLAG;
1538
135k
      fxMarkValue(the, aSlot);
1539
135k
      aSlot = aSlot->next;
1540
445k
      while (aSlot) {
1541
310k
        aSlot->flag |= XS_MARK_FLAG;
1542
310k
                fxCheckCStack(the);
1543
310k
        fxMarkValue(the, aSlot); // holdings
1544
310k
        aSlot = aSlot->next;
1545
310k
        if (aSlot) {
1546
310k
          aSlot->flag |= XS_MARK_FLAG;
1547
          // weak target and token
1548
310k
          aSlot = aSlot->next;
1549
310k
        }
1550
310k
      }
1551
135k
    }
1552
135k
    break;
1553
    
1554
0
  case XS_HOST_INSPECTOR_KIND:
1555
0
    aSlot = theSlot->value.hostInspector.cache;
1556
0
    if (!(aSlot->flag & XS_MARK_FLAG))
1557
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1558
0
    break;  
1559
    
1560
0
#if mxKeysGarbageCollection
1561
2.71M
  case XS_SYMBOL_KIND:
1562
2.71M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1563
490k
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1564
245k
        fxMarkID(the, theSlot->value.symbol);
1565
490k
    }
1566
2.71M
    break;  
1567
197M
  case XS_AT_KIND:
1568
197M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1569
25.6M
      fxMarkID(the, theSlot->value.at.id);
1570
197M
    break; 
1571
1.35G
#endif
1572
1.35G
  }
1573
1.35G
}
1574
1575
void fxMarkWeakStuff(txMachine* the) 
1576
105k
{
1577
105k
  txSlot* slot;
1578
105k
  txSlot** address;
1579
1580
105k
  {
1581
105k
    txSlot* list;
1582
105k
    txSlot** listAddress = &the->firstWeakListLink;
1583
1.01M
    while ((list = *listAddress)) {
1584
908k
      if (list->flag & XS_MARK_FLAG) {
1585
399k
        txSlot* listEntry;
1586
399k
        txSlot** listEntryAddress = &list->value.weakList.first;
1587
558k
        while ((listEntry = *listEntryAddress)) {
1588
158k
          txSlot* value = listEntry->value.weakEntry.value;
1589
158k
          if ((value->flag & XS_MARK_FLAG) && (value->kind != XS_UNINITIALIZED_KIND)) {
1590
141k
            listEntryAddress = &listEntry->next;
1591
141k
          }
1592
16.9k
          else {
1593
16.9k
            listEntry->flag &= ~XS_MARK_FLAG;
1594
16.9k
            *listEntryAddress = listEntry->next;
1595
16.9k
          }
1596
158k
        }
1597
399k
        listAddress = &list->value.weakList.link;
1598
399k
      }
1599
508k
      else {
1600
508k
        txSlot* listEntry = list->value.weakList.first;
1601
532k
        while (listEntry) {
1602
24.2k
          txSlot* key = listEntry->value.weakEntry.check;
1603
24.2k
          if (key->flag & XS_MARK_FLAG) {
1604
24.2k
            txSlot* keyEntry;
1605
24.2k
            txSlot** keyEntryAddress = &key->next;
1606
24.2M
            while ((keyEntry = *keyEntryAddress)) {
1607
24.2M
              if (!(keyEntry->flag & XS_INTERNAL_FLAG))
1608
36
                break;
1609
24.2M
              if ((keyEntry->kind == XS_WEAK_ENTRY_KIND) && (keyEntry->value.weakEntry.check == list)) {
1610
24.2k
                keyEntry->flag &= ~XS_MARK_FLAG;
1611
24.2k
                *keyEntryAddress = keyEntry->next;
1612
24.2k
                break;
1613
24.2k
              }
1614
24.2M
              keyEntryAddress = &keyEntry->next;
1615
24.2M
            }
1616
24.2k
          }
1617
24.2k
          listEntry = listEntry->next;
1618
24.2k
        }
1619
508k
        *listAddress = list->value.weakList.link;
1620
508k
      }
1621
908k
    }
1622
105k
  }
1623
105k
  address = &the->firstWeakRefLink;
1624
106k
  while ((slot = *address)) {
1625
131
    if (!(slot->value.weakRef.target->flag & XS_MARK_FLAG))
1626
76
      slot->value.weakRef.target = C_NULL;
1627
131
    *address = C_NULL;
1628
131
    address = &(slot->value.weakRef.link);
1629
131
  }
1630
  
1631
105k
  if (mxFinalizationRegistries.kind == XS_REFERENCE_KIND) {
1632
105k
    slot = mxFinalizationRegistries.value.reference->next;
1633
514k
    while (slot) {
1634
409k
      fxMarkFinalizationRegistry(the, slot->value.closure);
1635
409k
      slot = slot->next;
1636
409k
    }
1637
105k
  }
1638
105k
}
1639
1640
txSize fxMultiplyChunkSizes(txMachine* the, txSize a, txSize b)
1641
91.1M
{
1642
91.1M
  txSize c;
1643
91.1M
#if __has_builtin(__builtin_mul_overflow)
1644
91.1M
  if (__builtin_mul_overflow(a, b, &c) || (c < 0)) {
1645
#else
1646
  txNumber C = (txNumber)a * (txNumber)b;
1647
  c = (txSize)C;
1648
  if ((C > (txNumber)0x7FFFFFFF) || (C < (txNumber)0)) {
1649
#endif
1650
3
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1651
3
  }
1652
91.1M
  return c;
1653
91.1M
}
1654
1655
void* fxNewChunk(txMachine* the, txSize size)
1656
342M
{
1657
342M
  txSize offset = size;
1658
342M
  txChunk* chunk;
1659
342M
  txBoolean once = 1;
1660
342M
  size = fxAdjustChunkSize(the, size);
1661
342M
  chunk = fxFindChunk(the, size, &once);
1662
342M
  if (!chunk) {
1663
4.15k
    chunk = fxGrowChunk(the, size);
1664
4.15k
  }
1665
342M
#ifdef mxMetering
1666
342M
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1667
342M
#endif
1668
342M
  return fxCheckChunk(the, chunk, size, offset);
1669
342M
}
1670
1671
void* fxNewGrowableChunk(txMachine* the, txSize size, txSize capacity)
1672
6.11M
{
1673
#if mxNoChunks
1674
  return fxNewChunk(the, size);
1675
#else
1676
6.11M
  txSize offset = size;
1677
6.11M
  txChunk* chunk;
1678
6.11M
  txBoolean once = 1;
1679
6.11M
  size = fxAdjustChunkSize(the, size);
1680
6.11M
  capacity = fxAdjustChunkSize(the, capacity);
1681
6.11M
  chunk = fxFindChunk(the, capacity, &once);
1682
6.11M
  if (!chunk) {
1683
110
    chunk = fxGrowChunk(the, capacity);
1684
110
    if (!chunk) {
1685
0
      chunk = fxFindChunk(the, size, &once);
1686
0
      if (!chunk) {
1687
0
        chunk = fxGrowChunk(the, size);
1688
0
      }
1689
0
    }
1690
110
  }
1691
6.11M
#ifdef mxMetering
1692
6.11M
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1693
6.11M
#endif
1694
6.11M
  return fxCheckChunk(the, chunk, size, offset);
1695
6.11M
#endif
1696
6.11M
}
1697
1698
txSlot* fxNewSlot(txMachine* the) 
1699
773M
{
1700
773M
  txSlot* aSlot;
1701
773M
  txBoolean once = 1, allocate;
1702
  
1703
773M
#if mxStress
1704
773M
  if (fxShouldStress()) {
1705
45
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
1706
45
    once = 0;
1707
45
  }
1708
773M
#endif
1709
773M
again:
1710
773M
  aSlot = the->freeHeap;
1711
773M
  if (aSlot) {
1712
773M
    the->freeHeap = aSlot->next;
1713
773M
    aSlot->next = C_NULL;
1714
773M
    aSlot->ID = XS_NO_ID;
1715
773M
    aSlot->flag = XS_NO_FLAG;
1716
773M
  #ifdef mxSnapshot
1717
773M
    #if mx32bitID
1718
773M
      aSlot->dummy = 0;
1719
    #elif INTPTR_MAX == INT64_MAX
1720
      aSlot->dummy = 0;
1721
    #endif
1722
773M
  #endif
1723
#if mxPoisonSlots
1724
    ASAN_UNPOISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
1725
#endif
1726
773M
    the->currentHeapCount++;
1727
773M
    if (the->peakHeapCount < the->currentHeapCount)
1728
305M
      the->peakHeapCount = the->currentHeapCount;
1729
773M
#ifdef mxMetering
1730
773M
    the->meterIndex += XS_SLOT_ALLOCATION_METERING;
1731
773M
#endif
1732
773M
    return aSlot;
1733
773M
  }
1734
8.98k
  if (once) {
1735
8.32k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0), isThrashing;
1736
1737
8.32k
    fxCollect(the, XS_ORGANIC_FLAG);
1738
1739
8.32k
    isThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0);
1740
8.32k
    allocate = wasThrashing && isThrashing;
1741
1742
8.32k
    once = 0;
1743
8.32k
  }
1744
659
  else
1745
659
    allocate = 1;
1746
8.98k
  if (allocate) {
1747
3.22k
    if (!the->minimumHeapCount) {
1748
0
      fxReport(the, "# Slot allocation: failed in fixed size heap\n");
1749
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1750
0
    }
1751
3.22k
    fxGrowSlots(the, !(the->collectFlag & XS_SKIPPED_COLLECT_FLAG) ? the->minimumHeapCount : 64);
1752
3.22k
  }
1753
8.98k
  goto again;
1754
0
  return C_NULL;
1755
773M
}
1756
1757
void* fxRenewChunk(txMachine* the, void* theData, txSize size)
1758
58.9M
{
1759
#if mxNoChunks
1760
  txByte* aData = ((txByte*)theData) - sizeof(txChunk);
1761
  txChunk* aChunk = (txChunk*)aData;
1762
  size = fxAdjustChunkSize(the, size);
1763
  if (size <= aChunk->size) {
1764
    aChunk->size = size;
1765
    return theData;
1766
  }
1767
  return C_NULL;
1768
#else
1769
58.9M
  txByte* aData = ((txByte*)theData) - sizeof(txChunk);
1770
58.9M
  txChunk* aChunk = (txChunk*)aData;
1771
58.9M
  txSize capacity = (txSize)(aChunk->temporary - aData);
1772
58.9M
  txBlock* aBlock = the->firstBlock;
1773
58.9M
  size = fxAdjustChunkSize(the, size);
1774
58.9M
  if (size <= capacity) {
1775
20.1M
    the->currentChunksSize += size - aChunk->size;
1776
20.1M
    if (the->peakChunksSize < the->currentChunksSize)
1777
1.11M
      the->peakChunksSize = the->currentChunksSize;
1778
20.1M
    aChunk->size = size;
1779
20.1M
  #ifdef mxMetering
1780
20.1M
    the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1781
20.1M
  #endif
1782
20.1M
    return theData;
1783
20.1M
  }
1784
95.6M
  while (aBlock) {
1785
82.6M
    if (aChunk->temporary == aBlock->current) {
1786
25.7M
      txSize delta = size - capacity;
1787
25.7M
      if (aBlock->current + delta <= aBlock->limit) {
1788
25.7M
        the->currentChunksSize += size - aChunk->size;
1789
25.7M
        if (the->peakChunksSize < the->currentChunksSize)
1790
1.59M
          the->peakChunksSize = the->currentChunksSize;
1791
25.7M
        aBlock->current += delta;
1792
25.7M
        aChunk->temporary = aBlock->current;
1793
25.7M
        aChunk->size = size;
1794
25.7M
      #ifdef mxSnapshot
1795
25.7M
        c_memset(aData + capacity, 0, delta);
1796
25.7M
      #endif
1797
25.7M
      #ifdef mxMetering
1798
25.7M
        the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1799
25.7M
      #endif
1800
25.7M
        return theData;
1801
25.7M
      }
1802
2.11k
      else {
1803
2.11k
        return C_NULL;
1804
2.11k
      }
1805
25.7M
    }
1806
56.9M
    aBlock = aBlock->nextBlock;
1807
56.9M
  }
1808
13.0M
  return C_NULL;
1809
38.7M
#endif
1810
38.7M
}
1811
1812
#if mxAliasInstance
1813
void fxShare(txMachine* the)
1814
{
1815
  txID aliasCount = 0;
1816
  txSlot *heap, *slot, *limit;
1817
1818
  heap = the->firstHeap;
1819
  while (heap) {
1820
    slot = heap + 1;
1821
    limit = heap->value.reference;
1822
    while (slot < limit) {
1823
      if (slot->kind == XS_INSTANCE_KIND) {
1824
        txBoolean frozen = (slot->flag & XS_DONT_PATCH_FLAG) ? 1 : 0;
1825
        if (frozen) {
1826
          txSlot *property = slot->next;
1827
          while (property) {
1828
            if (property->kind == XS_ARRAY_KIND) {
1829
              txSlot* item = property->value.array.address;
1830
              txInteger length = (txInteger)fxGetIndexSize(the, property);
1831
              while (length > 0) {
1832
                if (item->kind != XS_ACCESSOR_KIND) 
1833
                  if (!(item->flag & XS_DONT_SET_FLAG))
1834
                    frozen = 0;
1835
                if (!(item->flag & XS_DONT_DELETE_FLAG))
1836
                  frozen = 0;
1837
                item++;
1838
                length--;
1839
              }
1840
            }
1841
            else {
1842
              if (property->kind != XS_ACCESSOR_KIND) 
1843
                if (!(property->flag & XS_DONT_SET_FLAG))
1844
                  frozen = 0;
1845
              if (!(property->flag & XS_DONT_DELETE_FLAG))
1846
                frozen = 0;
1847
            }
1848
            property = property->next;
1849
          }
1850
        }
1851
        if (frozen)
1852
          slot->ID = XS_NO_ID;
1853
        else
1854
          slot->ID = aliasCount++;
1855
      }
1856
      else if (slot->kind == XS_CLOSURE_KIND) {
1857
        txSlot* closure = slot->value.closure;
1858
        if (closure->flag & XS_DONT_SET_FLAG)
1859
          closure->flag |= XS_DONT_DELETE_FLAG;
1860
        else {
1861
          if (closure->ID == XS_NO_ID)
1862
            closure->ID = aliasCount++;
1863
          slot->flag &= ~XS_DONT_SET_FLAG;
1864
        }
1865
      }
1866
      slot->flag |= XS_MARK_FLAG; 
1867
      slot++;
1868
    }
1869
    heap = heap->next;
1870
  }
1871
  the->aliasCount = aliasCount;
1872
  /*
1873
  fxReport(the, "# Share\n");
1874
  fxReport(the, "# \tSlots: %ld\n", the->currentHeapCount);
1875
  fxReport(the, "# \t\tSymbols: %ld\n", the->keyIndex);
1876
  fxReport(the, "# \t\tInstances: %ld\n", aliasCount);
1877
  fxReport(the, "# \tChunks: %ld bytes\n", the->currentChunksSize);
1878
  */
1879
}
1880
#endif
1881
1882
void fxSweep(txMachine* the)
1883
80.0k
{
1884
80.0k
  txSize aTotal;
1885
#if mxNoChunks
1886
  txChunk** address;
1887
  txChunk* chunk;
1888
#else
1889
80.0k
  txBlock* aBlock;
1890
80.0k
  txByte* limit;
1891
80.0k
  txByte* next;
1892
80.0k
#endif
1893
80.0k
  txByte* current;
1894
80.0k
  txByte* temporary;
1895
80.0k
  txSize aSize;
1896
80.0k
  txByte** aCodeAddress;
1897
80.0k
  txSlot* aSlot;
1898
80.0k
  txSlot* bSlot;
1899
80.0k
  txSlot* cSlot;
1900
80.0k
  txSlot* freeSlot;
1901
80.0k
  txJump* jump;
1902
1903
#ifdef mxNever
1904
  startTime(&gxSweepChunkTime);
1905
#endif
1906
1907
80.0k
  aTotal = 0;
1908
#if mxNoChunks
1909
  address = (txChunk**)&(the->firstBlock);
1910
  while ((chunk = *address)) {
1911
    aSize = chunk->size;
1912
    if (aSize & mxChunkFlag) {
1913
      aSize &= ~mxChunkFlag;
1914
      temporary = c_malloc_noforcefail(aSize);
1915
      if (!temporary)
1916
        fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);    // should never happen
1917
      c_memcpy(temporary, chunk, aSize);
1918
      ((txChunk*)temporary)->size = aSize;
1919
      chunk->temporary = temporary;
1920
      address = (txChunk**)&(((txChunk*)temporary)->temporary);
1921
      aTotal += aSize;
1922
    }
1923
    else {
1924
      *address = (txChunk*)(chunk->temporary);
1925
      c_free(chunk);
1926
    }
1927
  }
1928
#else
1929
80.0k
  aBlock = the->firstBlock;
1930
295k
  while (aBlock) {
1931
215k
    current = ((txByte*)aBlock) + sizeof(txBlock);
1932
215k
    limit = aBlock->current;
1933
215k
    temporary = current;
1934
687M
    while (current < limit) {
1935
687M
      aSize = ((txChunk*)current)->size;
1936
687M
      next = ((txChunk*)current)->temporary;
1937
687M
      if (aSize & mxChunkFlag) {
1938
411M
        aSize &= ~mxChunkFlag;
1939
411M
        ((txChunk*)current)->temporary = temporary;
1940
411M
        temporary += aSize;
1941
411M
        aTotal += aSize;
1942
411M
      }
1943
276M
      else {
1944
276M
        ((txChunk*)current)->temporary = C_NULL;
1945
276M
      }
1946
687M
      ((txChunk*)current)->size = (txSize)(next - current);
1947
687M
      current = next;
1948
687M
    }  
1949
215k
    aBlock->temporary = temporary;
1950
215k
    aBlock = aBlock->nextBlock;
1951
215k
  }
1952
80.0k
#endif
1953
80.0k
  the->currentChunksSize = aTotal;
1954
1955
80.0k
  aCodeAddress = &(the->code);
1956
80.0k
  aSlot = the->frame;
1957
3.13M
  while (aSlot) {
1958
3.05M
    mxCheck(the, aSlot->kind == XS_FRAME_KIND);
1959
3.05M
    if ((aSlot->flag & XS_C_FLAG) == 0) {
1960
2.34M
      bSlot = (aSlot + 3)->value.reference->next;
1961
2.34M
      if (bSlot->kind == XS_CODE_KIND) {
1962
2.26M
        current = bSlot->value.code.address;
1963
2.26M
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1964
2.26M
        if (temporary) {
1965
2.26M
          temporary += sizeof(txChunk);
1966
2.26M
          *aCodeAddress += temporary - current;
1967
2.26M
        }
1968
2.26M
      }
1969
2.34M
    }
1970
710k
    else {
1971
710k
      current = *aCodeAddress;
1972
710k
      if (current) {
1973
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1974
0
        if (temporary)
1975
0
          *aCodeAddress = temporary + sizeof(txChunk);
1976
0
      }
1977
710k
    }
1978
3.05M
    aCodeAddress = &(aSlot->value.frame.code);
1979
3.05M
    aSlot = aSlot->next;
1980
3.05M
  }
1981
  
1982
80.0k
  jump = the->firstJump;
1983
763k
  while (jump) {
1984
683k
    if (jump->flag) {
1985
103k
      aSlot = jump->frame;
1986
103k
      bSlot = (aSlot + 3)->value.reference->next;
1987
103k
      if (bSlot->kind == XS_CODE_KIND) {
1988
11.2k
        current = bSlot->value.code.address;
1989
11.2k
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1990
11.2k
        if (temporary) {
1991
11.2k
          temporary += sizeof(txChunk);
1992
11.2k
          jump->code += temporary - current;
1993
11.2k
        }
1994
11.2k
      }
1995
103k
    }
1996
579k
    else {
1997
579k
      current = jump->code;
1998
579k
      if (current) {
1999
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
2000
0
        if (temporary)
2001
0
          jump->code = temporary + sizeof(txChunk);
2002
0
      }
2003
579k
    }
2004
683k
    jump = jump->nextJump;
2005
683k
  }
2006
  
2007
80.0k
  aSlot = the->stack;
2008
68.7M
  while (aSlot < the->stackTop) {
2009
68.6M
    fxSweepValue(the, aSlot);
2010
68.6M
    aSlot++;
2011
68.6M
  }
2012
80.0k
  aSlot = the->cRoot;
2013
80.0k
  while (aSlot) {
2014
0
    fxSweepValue(the, aSlot);
2015
0
    aSlot = aSlot->next;
2016
0
  }
2017
2018
#ifdef mxNever
2019
  stopTime(&gxSweepChunkTime);
2020
  startTime(&gxSweepSlotTime);
2021
#endif
2022
  
2023
80.0k
  aTotal = 0;
2024
80.0k
  freeSlot = C_NULL;
2025
80.0k
  aSlot = the->firstHeap;
2026
180k
  while (aSlot) {
2027
100k
    bSlot = aSlot + 1;
2028
100k
    cSlot = aSlot->value.reference;
2029
3.30G
    while (bSlot < cSlot) {
2030
3.30G
      if (bSlot->flag & XS_MARK_FLAG) {
2031
1.04G
        bSlot->flag &= ~XS_MARK_FLAG; 
2032
1.04G
        fxSweepValue(the, bSlot);
2033
1.04G
        aTotal++;
2034
1.04G
      }
2035
2.25G
      else {
2036
2.25G
      #ifndef mxLink
2037
2.25G
        if (bSlot->kind == XS_HOST_KIND) {
2038
14.2k
          if (bSlot->flag & XS_HOST_HOOKS_FLAG) {
2039
0
            if (bSlot->value.host.variant.hooks->destructor)
2040
0
              (*(bSlot->value.host.variant.hooks->destructor))(bSlot->value.host.data);
2041
0
          }
2042
14.2k
          else if (bSlot->value.host.variant.destructor)
2043
12.6k
            (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
2044
14.2k
        }
2045
2.25G
      #endif
2046
//        if (bSlot->kind == XS_MODULE_KIND) {
2047
//          char* name = fxGetKeyName(the, bSlot->value.module.id);
2048
//          fprintf(stderr, "gc module %d %s\n", bSlot->value.module.id, name);
2049
//        }
2050
      #if mxInstrument
2051
        if ((bSlot->kind == XS_MODULE_KIND) && (bSlot->ID == XS_MODULE_BEHAVIOR))
2052
          the->loadedModulesCount--;
2053
      #endif
2054
2.25G
        bSlot->kind = XS_UNDEFINED_KIND;
2055
2.25G
        bSlot->next = freeSlot;
2056
      #if mxPoisonSlots
2057
        ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
2058
      #endif
2059
2.25G
        freeSlot = bSlot;
2060
2.25G
      }
2061
3.30G
      bSlot++;
2062
3.30G
    }
2063
100k
    aSlot = aSlot->next;
2064
100k
  }
2065
80.0k
  the->currentHeapCount = aTotal;
2066
80.0k
  the->freeHeap = freeSlot;
2067
  
2068
#ifdef mxNever
2069
  stopTime(&gxSweepSlotTime);
2070
  startTime(&gxCompactChunkTime);
2071
#endif
2072
2073
#if mxNoChunks
2074
  address = (txChunk**)&(the->firstBlock);
2075
  while ((chunk = *address)) {
2076
    aSize = chunk->size;
2077
    if (aSize & mxChunkFlag) {
2078
      *address = (txChunk*)(chunk->temporary);
2079
      c_free(chunk);
2080
    }
2081
    else {
2082
      address = (txChunk**)&(chunk->temporary);
2083
    }
2084
  }
2085
#else
2086
80.0k
  aBlock = the->firstBlock;
2087
295k
  while (aBlock) {
2088
215k
    txByte* former = C_NULL;
2089
215k
    current = ((txByte*)aBlock) + sizeof(txBlock);
2090
215k
    limit = aBlock->current;
2091
687M
    while (current < limit) {
2092
687M
      aSize = ((txChunk*)current)->size;
2093
687M
      next = current + aSize;
2094
687M
      if ((temporary = ((txChunk*)current)->temporary)) {
2095
411M
        if (former) {
2096
410M
          ((txChunk*)former)->temporary = temporary;
2097
410M
          ((txChunk*)former)->size = (txSize)(temporary - former);
2098
410M
        }
2099
411M
        if (temporary != current)
2100
43.9M
          c_memmove(temporary, current, aSize);
2101
411M
        former = temporary;
2102
411M
      }
2103
687M
      current = next;
2104
687M
    }
2105
215k
    if (former) {
2106
198k
      ((txChunk*)former)->temporary = aBlock->temporary;
2107
198k
      ((txChunk*)former)->size = (txSize)(aBlock->temporary - former);
2108
198k
    }
2109
215k
    aBlock->current = aBlock->temporary;
2110
215k
    aBlock->temporary = C_NULL;
2111
215k
    aBlock = aBlock->nextBlock;
2112
215k
  }
2113
80.0k
#endif
2114
  
2115
#ifdef mxNever
2116
  stopTime(&gxCompactChunkTime);
2117
#endif
2118
80.0k
}
2119
2120
void fxSweepValue(txMachine* the, txSlot* theSlot)
2121
1.63G
{
2122
1.63G
  txSlot* aSlot;
2123
1.63G
  txByte* data;
2124
  
2125
1.63G
#define mxSweepChunk(_THE_DATA, _THE_DATA_TYPE) \
2126
1.63G
  if ((data = (txByte*)(((txChunk*)(((txByte*)(_THE_DATA)) - sizeof(txChunk)))->temporary))) \
2127
507M
    ((_THE_DATA)) = (_THE_DATA_TYPE)(data + sizeof(txChunk))
2128
2129
1.63G
  switch (theSlot->kind) {
2130
341M
  case XS_STRING_KIND:
2131
341M
    mxSweepChunk(theSlot->value.string, txString);
2132
341M
    break;
2133
17.8k
  case XS_BIGINT_KIND:
2134
17.8k
    mxSweepChunk(theSlot->value.bigint.data, txU4*);
2135
17.8k
    break;
2136
2137
0
  case XS_ARGUMENTS_SLOPPY_KIND:
2138
0
  case XS_ARGUMENTS_STRICT_KIND:
2139
59.7M
  case XS_ARRAY_KIND:
2140
68.8M
  case XS_STACK_KIND:
2141
68.8M
    if ((aSlot = theSlot->value.array.address)) {
2142
#if mxNoChunks
2143
      mxSweepChunk(theSlot->value.array.address, txSlot*);
2144
      aSlot = theSlot->value.array.address;
2145
#endif
2146
60.8M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
2147
60.8M
      txIndex aLength = chunk->size / sizeof(txSlot);
2148
60.8M
      if (aLength > theSlot->value.array.length)
2149
28.1k
        aLength = theSlot->value.array.length;
2150
580M
      while (aLength) {
2151
519M
        fxSweepValue(the, aSlot);
2152
519M
        aSlot++;
2153
519M
        aLength--;
2154
519M
      }
2155
#if mxNoChunks
2156
#else
2157
60.8M
      mxSweepChunk(theSlot->value.array.address, txSlot*);
2158
60.8M
#endif
2159
60.8M
    }
2160
68.8M
    break;
2161
1.17M
  case XS_ARRAY_BUFFER_KIND:
2162
1.17M
    if (theSlot->value.arrayBuffer.address)
2163
1.17M
      mxSweepChunk(theSlot->value.arrayBuffer.address, txByte*);
2164
1.17M
    break;
2165
1.34M
  case XS_CODE_KIND:
2166
1.34M
    mxSweepChunk(theSlot->value.code.address, txByte*);
2167
1.34M
    break;
2168
80.0k
  case XS_GLOBAL_KIND:
2169
80.0k
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2170
80.0k
    break;
2171
212k
  case XS_HOST_KIND:
2172
212k
    if (theSlot->value.host.data) {
2173
132k
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
2174
129
        mxSweepChunk(theSlot->value.host.data, void*);
2175
132k
    }
2176
212k
    break;
2177
5
  case XS_IDS_KIND:
2178
5
    if (theSlot->value.IDs)
2179
5
      mxSweepChunk(theSlot->value.IDs, txID*);
2180
5
    break;
2181
28.0k
  case XS_REGEXP_KIND:
2182
28.0k
    if (theSlot->value.regexp.code)
2183
27.4k
      mxSweepChunk(theSlot->value.regexp.code, void*);
2184
28.0k
    if (theSlot->value.regexp.data)
2185
27.9k
      mxSweepChunk(theSlot->value.regexp.data, void*);
2186
28.0k
    break;
2187
102M
  case XS_KEY_KIND:
2188
102M
    if (theSlot->value.key.string)
2189
102M
      mxSweepChunk(theSlot->value.key.string, txString);
2190
102M
    break;
2191
12
  case XS_MAP_KIND:
2192
5.14k
  case XS_SET_KIND:
2193
5.14k
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2194
5.14k
    break;
2195
1.63G
  }
2196
1.63G
}