Coverage Report

Created: 2026-01-29 06:33

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
846M
{
62
846M
  if (!gxStress)
63
846M
    return 0;
64
65
386k
  if (gxStress > 0)
66
0
    return 1;
67
68
386k
  gxStress += 1;
69
386k
  return 0 == gxStress;
70
386k
}
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.17G
#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
389M
{
144
389M
  txSize c;
145
389M
#if __has_builtin(__builtin_add_overflow)
146
389M
  if (__builtin_add_overflow(a, b, &c)) {
147
#else
148
  c = a + b;
149
  if (((a ^ c) & (b ^ c)) < 0) {
150
#endif
151
8
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
152
8
  }
153
389M
  return c;
154
389M
}
155
156
txSize fxAdjustChunkSize(txMachine* the, txSize size)
157
277M
{
158
277M
  txSize adjust = sizeof(txChunk);
159
277M
  txSize modulo = size & (sizeof(size_t) - 1);
160
277M
  if (modulo)
161
206M
    adjust += sizeof(size_t) - modulo;
162
277M
  return fxAddChunkSizes(the, size, adjust);
163
277M
}
164
165
void fxAllocate(txMachine* the, txCreation* theCreation)
166
27.5k
{
167
#ifdef mxNever
168
  startTime(&gxLifeTime);
169
#endif
170
27.5k
#if mxStress
171
27.5k
  gxStress = 0;
172
27.5k
#endif
173
174
27.5k
  the->currentChunksSize = 0;
175
27.5k
  the->peakChunksSize = 0;
176
27.5k
  the->maximumChunksSize = 0;
177
27.5k
  the->minimumChunksSize = theCreation->incrementalChunkSize;
178
  
179
27.5k
  the->currentHeapCount = 0;
180
27.5k
  the->peakHeapCount = 0;
181
27.5k
  the->maximumHeapCount = 0;
182
27.5k
  the->minimumHeapCount = theCreation->incrementalHeapCount;
183
  
184
27.5k
  the->firstBlock = C_NULL;
185
27.5k
  the->firstHeap = C_NULL;
186
187
#if mxNoChunks
188
  the->maximumChunksSize = theCreation->initialChunkSize;
189
#else
190
27.5k
  fxGrowChunks(the, theCreation->initialChunkSize);
191
27.5k
#endif
192
193
27.5k
  the->stackBottom = fxAllocateSlots(the, theCreation->stackCount);
194
27.5k
  the->stackTop = the->stackBottom + theCreation->stackCount;
195
27.5k
  the->stackIntrinsics = the->stackTop;
196
27.5k
  the->stackPrototypes = the->stackTop - XS_INTRINSICS_COUNT;
197
27.5k
  the->stack = the->stackTop;
198
#ifdef mxInstrument
199
  the->stackPeak = the->stackTop;
200
#endif
201
202
27.5k
  fxGrowSlots(the, theCreation->initialHeapCount);
203
204
27.5k
  the->keyCount = (txID)theCreation->initialKeyCount;
205
27.5k
  the->keyDelta = (txID)theCreation->incrementalKeyCount;
206
27.5k
  the->keyIndex = 0;
207
27.5k
  the->keyArray = (txSlot **)c_malloc_uint32(theCreation->initialKeyCount * sizeof(txSlot*));
208
27.5k
  if (!the->keyArray)
209
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);   
210
  
211
27.5k
  the->nameModulo = theCreation->nameModulo;
212
27.5k
  the->nameTable = (txSlot **)c_malloc_uint32(theCreation->nameModulo * sizeof(txSlot*));
213
27.5k
  if (!the->nameTable)
214
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
215
216
27.5k
  the->symbolModulo = theCreation->symbolModulo;
217
27.5k
  the->symbolTable = (txSlot **)c_malloc_uint32(theCreation->symbolModulo * sizeof(txSlot*));
218
27.5k
  if (!the->symbolTable)
219
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
220
    
221
27.5k
  fxAllocateStringInfoCache(the);
222
223
27.5k
  the->stackLimit = fxCStackLimit();
224
225
27.5k
  the->cRoot = C_NULL;
226
27.5k
  the->parserBufferSize = theCreation->parserBufferSize;
227
27.5k
  the->parserTableModulo = theCreation->parserTableModulo;
228
  
229
27.5k
#ifdef mxDebug
230
27.5k
  the->pathCount = 256;
231
27.5k
  the->pathValue = c_malloc(the->pathCount);
232
27.5k
  if (!the->pathValue)
233
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
234
27.5k
#endif
235
27.5k
}
236
237
void* fxCheckChunk(txMachine* the, txChunk* chunk, txSize size, txSize offset)
238
253M
{
239
253M
  if (chunk) {
240
253M
    txByte* data = (txByte*)chunk;
241
#if mxNoChunks
242
    chunk->size = size;
243
    the->currentChunksSize += size;
244
#else
245
253M
    txSize capacity = (txSize)(chunk->temporary - data);
246
253M
  #ifdef mxSnapshot
247
253M
    #if INTPTR_MAX == INT64_MAX
248
253M
      chunk->dummy = 0;
249
253M
    #endif
250
  #ifdef mxSnapshotRandomInit
251
    arc4random_buf(data + sizeof(txChunk), offset);
252
  #endif    
253
253M
  #endif
254
253M
    offset += sizeof(txChunk);
255
253M
    c_memset(data + offset, 0, capacity - offset);
256
253M
    chunk->size = size;
257
253M
    the->currentChunksSize += capacity;
258
253M
#endif
259
253M
    if (the->peakChunksSize < the->currentChunksSize)
260
73.4M
      the->peakChunksSize = the->currentChunksSize;
261
253M
    return data + sizeof(txChunk);
262
253M
  }
263
4
  fxReport(the, "# Chunk allocation: failed for %ld bytes\n", (long)size);
264
4
  fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
265
4
  return C_NULL;
266
253M
}
267
268
#if defined(__clang__) || defined (__GNUC__)
269
  __attribute__((no_sanitize_address))
270
#endif
271
void fxCheckCStack(txMachine* the)
272
345M
{
273
345M
    char x;
274
345M
    char *stack = &x;
275
345M
  if (stack <= the->stackLimit) {
276
13
    fxAbort(the, XS_NATIVE_STACK_OVERFLOW_EXIT);
277
13
  }
278
345M
}
279
280
void fxCollect(txMachine* the, txFlag theFlag)
281
59.1k
{
282
59.1k
  txSize aCount;
283
59.1k
  txSlot* freeSlot;
284
59.1k
  txSlot* aSlot;
285
59.1k
  txSlot* bSlot;
286
59.1k
  txSlot* cSlot;
287
288
59.1k
  if ((the->collectFlag & XS_COLLECTING_FLAG) == 0) {
289
0
    the->collectFlag |= XS_SKIPPED_COLLECT_FLAG;
290
0
    return;
291
0
  }
292
59.1k
  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
59.1k
  if (theFlag & XS_COMPACT_FLAG) {
308
39.2k
    fxInvalidateStringInfoCache(the);
309
39.2k
    fxMark(the, fxMarkValue);
310
39.2k
    fxMarkWeakStuff(the);
311
  #ifdef mxNever
312
    stopTime(&gxMarkTime);
313
  #endif
314
39.2k
    fxSweep(the);
315
39.2k
  }
316
19.8k
  else {
317
19.8k
    fxMark(the, fxMarkReference);
318
19.8k
    fxMarkWeakStuff(the);
319
  #ifdef mxNever
320
    stopTime(&gxMarkTime);
321
    startTime(&gxSweepSlotTime);
322
  #endif
323
19.8k
    aCount = 0;
324
19.8k
    freeSlot = C_NULL;
325
19.8k
    aSlot = the->firstHeap;
326
75.2k
    while (aSlot) {
327
55.3k
      bSlot = aSlot + 1;
328
55.3k
      cSlot = aSlot->value.reference;
329
1.81G
      while (bSlot < cSlot) {
330
1.81G
        if (bSlot->flag & XS_MARK_FLAG) {
331
1.25G
          bSlot->flag &= ~XS_MARK_FLAG; 
332
          
333
1.25G
          if (bSlot->kind == XS_REFERENCE_KIND)
334
102M
            mxCheck(the, bSlot->value.reference->kind == XS_INSTANCE_KIND);
335
          
336
1.25G
          aCount++;
337
1.25G
        }
338
558M
        else {
339
558M
          if (bSlot->kind == XS_HOST_KIND) {
340
2.92k
            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
2.92k
            else if (bSlot->value.host.variant.destructor)
345
1.72k
              (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
346
2.92k
          }
347
        #if mxInstrument
348
          if ((bSlot->kind == XS_MODULE_KIND) && (bSlot->ID == XS_MODULE_BEHAVIOR))
349
            the->loadedModulesCount--;
350
        #endif
351
558M
          bSlot->kind = XS_UNDEFINED_KIND;
352
558M
          bSlot->next = freeSlot;
353
        #if mxPoisonSlots
354
          ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
355
        #endif
356
558M
          freeSlot = bSlot;
357
558M
        }
358
1.81G
        bSlot++;
359
1.81G
      }
360
55.3k
      aSlot = aSlot->next;
361
55.3k
    }
362
19.8k
    the->currentHeapCount = aCount;
363
19.8k
    the->freeHeap = freeSlot;
364
  #ifdef mxNever
365
    stopTime(&gxSweepSlotTime);
366
  #endif
367
19.8k
  }
368
  
369
59.1k
  aSlot = the->stack;
370
100M
  while (aSlot < the->stackTop) {
371
100M
    aSlot->flag &= ~XS_MARK_FLAG; 
372
100M
    aSlot++;
373
100M
  }
374
  
375
59.1k
  the->collectFlag &= ~(XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
376
  
377
59.1k
  if (theFlag & XS_COMPACT_FLAG) {
378
39.2k
    if ((the->maximumChunksSize - the->currentChunksSize) < the->minimumChunksSize)
379
10.9k
      the->collectFlag |= XS_TRASHING_CHUNKS_FLAG;
380
28.3k
    else
381
28.3k
      the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
382
39.2k
  }
383
  
384
59.1k
  if ((the->maximumHeapCount - the->currentHeapCount) < the->minimumHeapCount)
385
52.1k
    the->collectFlag |= XS_TRASHING_SLOTS_FLAG;
386
6.94k
  else
387
6.94k
    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
59.1k
#if defined(mxInstrument) || defined(mxProfile)
422
59.1k
  fxCheckProfiler(the, C_NULL);
423
59.1k
#endif
424
59.1k
}
425
426
txSlot* fxDuplicateSlot(txMachine* the, txSlot* theSlot)
427
4.44k
{
428
4.44k
  txSlot* result;
429
  
430
4.44k
  result = fxNewSlot(the);
431
4.44k
  result->ID = theSlot->ID;
432
4.44k
  result->kind = theSlot->kind;
433
4.44k
  result->flag = theSlot->flag & ~XS_MARK_FLAG;
434
4.44k
  result->value = theSlot->value;
435
4.44k
  return result;
436
4.44k
}
437
438
void* fxFindChunk(txMachine* the, txSize size, txBoolean *once)
439
253M
{
440
253M
  txBlock* block;
441
253M
  txChunk* chunk;
442
253M
#if mxStress
443
253M
  if (fxShouldStress()) {
444
26
    if (*once) {
445
26
      fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
446
26
      *once = 0;
447
26
    }
448
26
  }
449
253M
#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
253M
again:
465
253M
  block = the->firstBlock;
466
342M
  while (block) {
467
342M
    if ((block->current + size) <= block->limit) {
468
253M
      chunk = (txChunk*)(block->current);
469
253M
      block->current += size;
470
253M
      chunk->temporary = block->current;
471
253M
      return chunk;
472
253M
    }
473
88.9M
    block = block->nextBlock;
474
88.9M
  }
475
34.2k
  if (*once) {
476
32.6k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0), isThrashing;
477
32.6k
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
478
32.6k
    isThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0);
479
32.6k
    *once = 0;
480
32.6k
    if (wasThrashing && isThrashing)
481
1.37k
      return C_NULL;
482
31.3k
    goto again;
483
32.6k
  }
484
1.51k
  return C_NULL;
485
34.2k
}
486
487
txSlot* fxFindKey(txMachine* the)
488
17.9M
{
489
17.9M
#if mxKeysGarbageCollection
490
17.9M
  txBoolean once = 1;
491
17.9M
#endif
492
17.9M
  txID id;
493
17.9M
  txSlot* result;
494
17.9M
more:
495
17.9M
  id = the->keyIndex;
496
17.9M
  if (id < the->keyCount) {
497
15.7M
    result = fxNewSlot(the);
498
15.7M
    result->ID = id;
499
15.7M
    the->keyArray[id - the->keyOffset] = result;
500
15.7M
    the->keyIndex++;
501
15.7M
    return result;
502
15.7M
  }
503
2.20M
#if mxKeysGarbageCollection
504
2.21M
again:
505
2.21M
  result = the->keyholeList;
506
2.21M
  if (result) {
507
2.20M
    the->keyholeCount--;
508
2.20M
    the->keyholeList = result->next;
509
2.20M
    result->next = C_NULL;
510
2.20M
    return result;
511
2.20M
  }
512
14.2k
  if (once) {
513
13.5k
    fxCollect(the, XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
514
13.5k
    once = 0;
515
13.5k
    goto again;
516
13.5k
  }
517
653
#endif
518
653
  else {
519
653
    fxGrowKeys(the, 1);
520
653
    goto more;
521
653
  }
522
0
  return C_NULL;
523
14.2k
}
524
525
void fxFree(txMachine* the) 
526
27.5k
{
527
27.5k
  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
27.5k
  fxFreeStringInfoCache(the);
536
537
27.5k
  if (the->symbolTable)
538
27.5k
    c_free_uint32(the->symbolTable);
539
27.5k
  the->symbolTable = C_NULL;
540
27.5k
  if (the->nameTable)
541
27.5k
    c_free_uint32(the->nameTable);
542
27.5k
  the->nameTable = C_NULL;
543
27.5k
  if (the->keyArray)
544
27.5k
    c_free_uint32(the->keyArray);
545
27.5k
  the->keyArray = C_NULL;
546
  
547
57.7k
  while (the->firstHeap) {
548
30.2k
    aHeap = the->firstHeap;
549
30.2k
    the->firstHeap = aHeap->next;
550
30.2k
    fxFreeSlots(the, aHeap);
551
30.2k
  }
552
27.5k
  the->firstHeap = C_NULL;
553
554
27.5k
  if (the->stackBottom)
555
27.5k
    fxFreeSlots(the, the->stackBottom);
556
27.5k
  the->stackBottom = C_NULL;
557
27.5k
  the->stackTop = C_NULL;
558
27.5k
  the->stackIntrinsics = C_NULL;
559
27.5k
  the->stackPrototypes = C_NULL;
560
27.5k
  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
27.5k
  {
574
27.5k
    txBlock* aBlock;
575
58.0k
    while (the->firstBlock) {
576
30.4k
      aBlock = the->firstBlock;
577
30.4k
      the->firstBlock = aBlock->nextBlock;
578
30.4k
      fxFreeChunks(the, aBlock);
579
30.4k
    }
580
27.5k
    the->firstBlock = C_NULL;
581
27.5k
  }
582
27.5k
#endif
583
584
27.5k
#ifdef mxDebug
585
27.5k
  if (the->pathValue)
586
27.5k
    c_free(the->pathValue);
587
27.5k
  the->pathValue = C_NULL;
588
27.5k
#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
27.5k
}
605
606
void* fxGrowChunk(txMachine* the, txSize size) 
607
2.88k
{
608
2.88k
  txBlock* block = fxGrowChunks(the, size);
609
2.88k
  txChunk* chunk = C_NULL;
610
2.88k
  if (block) {
611
2.88k
    chunk = (txChunk*)(block->current);
612
2.88k
    block->current += size;
613
2.88k
    chunk->temporary = block->current;
614
2.88k
  }
615
2.88k
  return chunk;
616
2.88k
}
617
618
void* fxGrowChunks(txMachine* the, txSize size) 
619
30.4k
{
620
30.4k
  txByte* buffer;
621
30.4k
  txBlock* block = C_NULL;
622
623
30.4k
  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
30.4k
  if ((the->firstBlock != C_NULL) && (!(the->collectFlag & XS_SKIPPED_COLLECT_FLAG))) {
629
2.88k
    txSize modulo = size % (the->minimumChunksSize ? the->minimumChunksSize : 16);
630
2.88k
    if (modulo)
631
2.88k
      size = fxAddChunkSizes(the, size, the->minimumChunksSize - modulo);
632
2.88k
  }
633
30.4k
  size = fxAddChunkSizes(the, size, sizeof(txBlock));
634
30.4k
  buffer = fxAllocateChunks(the, size);
635
30.4k
  if (buffer) {
636
30.4k
  #ifdef mxSnapshot
637
30.4k
    c_memset(buffer, 0, size);
638
30.4k
  #endif
639
30.4k
    if ((the->firstBlock != C_NULL) && (the->firstBlock->limit == buffer)) {
640
0
      the->firstBlock->limit += size;
641
0
      block = the->firstBlock;
642
0
    }
643
30.4k
    else {
644
30.4k
      block = (txBlock*)buffer;
645
30.4k
      block->nextBlock = the->firstBlock;
646
30.4k
      block->current = buffer + sizeof(txBlock);
647
30.4k
      block->limit = buffer + size;
648
30.4k
      block->temporary = C_NULL;
649
30.4k
      the->firstBlock = block;
650
30.4k
      size -= sizeof(txBlock);
651
30.4k
    }
652
30.4k
    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
30.4k
  }
658
30.4k
  the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
659
30.4k
  return block;
660
30.4k
}
661
662
void fxGrowKeys(txMachine* the, txID theCount) 
663
653
{
664
653
  if (the->keyDelta > 0) {
665
653
    txID keyDelta = (theCount > the->keyDelta) ? theCount : the->keyDelta;
666
653
    txID keyCount = (the->keyCount + keyDelta) - the->keyOffset;
667
653
    txSlot** keyArray = c_realloc(the->keyArray, keyCount * sizeof(txSlot*));
668
653
    if (keyArray == C_NULL)
669
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
670
653
    the->keyArray = keyArray;
671
653
    the->keyCount = keyCount + the->keyOffset;
672
653
  }
673
0
  else 
674
0
    fxAbort(the, XS_NO_MORE_KEYS_EXIT);
675
653
}
676
677
void fxGrowSlots(txMachine* the, txSize theCount) 
678
30.2k
{
679
30.2k
  txSlot* aHeap;
680
30.2k
  txSlot* aSlot;
681
682
30.2k
  aHeap = fxAllocateSlots(the, theCount);
683
30.2k
  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
30.2k
  if ((void *)-1 == aHeap)
688
0
    return;
689
    
690
30.2k
  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
30.2k
  else {
706
30.2k
    the->maximumHeapCount += theCount - 1;
707
30.2k
    aHeap->next = the->firstHeap;
708
30.2k
    aHeap->ID = 0;
709
30.2k
    aHeap->flag = 0;
710
30.2k
    aHeap->kind = 0;
711
30.2k
    aHeap->value.reference = aHeap + theCount;
712
30.2k
    theCount -= 2;
713
30.2k
    the->firstHeap = aHeap;
714
30.2k
    aSlot = aHeap + 1;
715
30.2k
  }
716
990M
    while (theCount--) {
717
990M
    txSlot* next = aSlot + 1;
718
990M
    aSlot->next = next;
719
990M
    aSlot->flag = XS_NO_FLAG;
720
990M
    aSlot->kind = XS_UNDEFINED_KIND;
721
  #if mxPoisonSlots
722
    ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
723
  #endif
724
990M
        aSlot = next;
725
990M
    }
726
30.2k
  aSlot->next = the->freeHeap;
727
30.2k
  aSlot->flag = XS_NO_FLAG;
728
30.2k
  aSlot->kind = XS_UNDEFINED_KIND;
729
#if mxPoisonSlots
730
  ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
731
#endif
732
30.2k
  the->freeHeap = aHeap + 1;
733
30.2k
  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
30.2k
}
741
742
void fxMark(txMachine* the, void (*theMarker)(txMachine*, txSlot*))
743
59.1k
{
744
59.1k
  txSlot** p;
745
59.1k
  txSlot** q;
746
59.1k
  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
59.1k
  slot = the->stackTop;
761
100M
  while (slot > the->stack) {
762
100M
        slot--;
763
100M
    (*theMarker)(the, slot);
764
100M
  }
765
59.1k
  slot = the->cRoot;
766
59.1k
  while (slot) {
767
0
    (*theMarker)(the, slot);
768
0
    slot = slot->next;
769
0
  }
770
  
771
59.1k
#if mxKeysGarbageCollection
772
59.1k
  if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
773
20.0k
    txInteger deletions = 0;
774
20.0k
    p = the->keyArray;
775
20.0k
    q = p + the->keyIndex - the->keyOffset;
776
21.5M
    while (p < q) {
777
21.4M
      slot = *p++;
778
21.4M
      if (!(slot->flag & XS_MARK_FLAG)) {
779
7.73M
        if (slot->flag & XS_DONT_DELETE_FLAG)
780
3.67M
          slot->flag |= XS_MARK_FLAG;
781
4.06M
        else if (slot->flag & XS_DONT_ENUM_FLAG)
782
1.21M
          deletions++;
783
7.73M
      }
784
21.4M
    }
785
  
786
  //  fprintf(stderr, "\n### KEYS GC %d", deletions);
787
20.0k
    p = the->nameTable;
788
20.0k
    q = the->nameTable + the->nameModulo;
789
27.6M
    while ((p < q) && deletions) {
790
27.6M
      txSlot** address = p;
791
39.2M
      while (((slot = *address)) && deletions) {
792
11.6M
        if (slot->flag & XS_MARK_FLAG)
793
10.4M
          address = &(slot->next);
794
1.21M
        else {
795
1.21M
          *address = slot->next;
796
1.21M
          deletions--;
797
1.21M
        }
798
11.6M
      }
799
27.6M
      p++;
800
27.6M
    }
801
  //  fprintf(stderr, " => %d", deletions);
802
  
803
20.0k
    the->keyholeCount = 0;
804
20.0k
    the->keyholeList = C_NULL;
805
20.0k
    p = the->keyArray;
806
20.0k
    q = p + the->keyIndex - the->keyOffset;
807
21.5M
    while (p < q) {
808
21.4M
      slot = *p;
809
21.4M
      if (slot->flag & XS_MARK_FLAG)
810
17.4M
        (*theMarker)(the, slot);
811
4.06M
      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
4.06M
        slot->flag = XS_INTERNAL_FLAG | XS_MARK_FLAG;
817
4.06M
        slot->next = the->keyholeList;
818
4.06M
        slot->kind = XS_UNDEFINED_KIND;
819
4.06M
        the->keyholeCount++;
820
4.06M
        the->keyholeList = slot;
821
4.06M
      }
822
21.4M
      p++;
823
21.4M
    }
824
  //  fprintf(stderr, "\n");
825
20.0k
  }
826
39.0k
  else
827
39.0k
#endif
828
39.0k
  {
829
39.0k
    p = the->keyArray;
830
39.0k
    q = p + the->keyIndex - the->keyOffset;
831
23.0M
    while (p < q) {
832
22.9M
      slot = *p++;
833
22.9M
      slot->flag |= XS_MARK_FLAG;
834
22.9M
      (*theMarker)(the, slot);
835
22.9M
    }
836
39.0k
  }
837
59.1k
}
838
839
#if mxKeysGarbageCollection
840
void fxMarkID(txMachine* the, txID id)
841
74.2M
{
842
74.2M
  txSlot* slot;
843
74.2M
  if (id == XS_NO_ID)
844
13.9M
    return;
845
60.3M
  if (id < the->keyOffset)
846
0
    return;
847
60.3M
  id -= the->keyOffset;
848
60.3M
  slot = the->keyArray[id];
849
60.3M
  slot->flag |= XS_MARK_FLAG;
850
60.3M
}
851
#endif
852
853
void fxMarkFinalizationRegistry(txMachine* the, txSlot* registry) 
854
216k
{
855
216k
  txSlot* slot = registry->value.finalizationRegistry.callback->next;
856
216k
  txSlot* instance;
857
630k
  while (slot) {
858
413k
    slot = slot->next;
859
413k
    if (slot) {
860
413k
      instance = slot->value.finalizationCell.target;
861
413k
      if (instance && !(instance->flag & XS_MARK_FLAG)) {
862
35.6k
        slot->value.finalizationCell.target = C_NULL;
863
35.6k
        registry->value.finalizationRegistry.flags |= XS_FINALIZATION_REGISTRY_CHANGED;
864
35.6k
      }
865
413k
      instance = slot->value.finalizationCell.token;
866
413k
      if (instance && !(instance->flag & XS_MARK_FLAG))
867
17
        slot->value.finalizationCell.token = C_NULL;
868
413k
      slot = slot->next;
869
413k
    }
870
413k
  }
871
216k
}
872
873
void fxMarkInstance(txMachine* the, txSlot* theCurrent, void (*theMarker)(txMachine*, txSlot*))
874
157M
{
875
157M
  txSlot* aProperty;
876
157M
  txSlot* aTemporary;
877
878
157M
  mxCheck(the, theCurrent->kind == XS_INSTANCE_KIND);
879
157M
  aProperty = theCurrent;
880
157M
  theCurrent->value.instance.garbage = C_NULL;
881
2.08G
  for (;;) {
882
2.08G
    if (aProperty) {
883
1.81G
      if (!(aProperty->flag & XS_MARK_FLAG)) {
884
1.81G
        aProperty->flag |= XS_MARK_FLAG;
885
1.81G
        switch (aProperty->kind) {
886
276M
        case XS_INSTANCE_KIND:
887
276M
          aTemporary = aProperty->value.instance.prototype;
888
276M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
889
501k
            aProperty->value.instance.prototype = theCurrent;
890
501k
            theCurrent = aTemporary;
891
501k
            theCurrent->value.instance.garbage = aProperty;
892
501k
            aProperty = theCurrent;
893
501k
          }
894
275M
          else
895
275M
            aProperty = aProperty->next;
896
276M
          break;
897
136M
        case XS_REFERENCE_KIND:
898
136M
        #if mxKeysGarbageCollection
899
136M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
900
13.9M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
901
13.5M
              fxMarkID(the, aProperty->ID);
902
13.9M
          }
903
136M
        #endif
904
136M
          aTemporary = aProperty->value.reference;
905
136M
          if (!(aTemporary->flag & XS_MARK_FLAG)) {
906
116M
            aProperty->value.reference = theCurrent;
907
116M
            theCurrent = aTemporary;
908
116M
            theCurrent->value.instance.garbage = aProperty;
909
116M
            aProperty = theCurrent;
910
116M
          }
911
20.2M
          else
912
20.2M
            aProperty = aProperty->next;
913
136M
          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.36k
          else {
925
2.36k
            aTemporary = aProperty->value.proxy.target;
926
2.36k
            if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
927
10
              aProperty->value.proxy.target = theCurrent;
928
10
              theCurrent = aTemporary;
929
10
              theCurrent->value.instance.garbage = aProperty;
930
10
              aProperty = theCurrent;
931
10
            }
932
2.35k
            else
933
2.35k
              aProperty = aProperty->next;
934
2.36k
          }
935
1.08M
          break;
936
          
937
4.32M
        case XS_CLOSURE_KIND:
938
4.32M
        #if mxKeysGarbageCollection
939
4.32M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
940
136k
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
941
83.0k
              fxMarkID(the, aProperty->ID);
942
136k
          }
943
4.32M
        #endif
944
4.32M
          aTemporary = aProperty->value.closure;
945
4.32M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
946
1.86M
            aTemporary->flag |= XS_MARK_FLAG; 
947
1.86M
            if (aTemporary->kind == XS_REFERENCE_KIND) {
948
391k
              aTemporary = aTemporary->value.reference;
949
391k
              if (!(aTemporary->flag & XS_MARK_FLAG)) {
950
298k
                aProperty->value.closure->value.reference = theCurrent;
951
298k
                theCurrent = aTemporary;
952
298k
                theCurrent->value.instance.garbage = aProperty;
953
298k
                aProperty = theCurrent;
954
            
955
298k
              }
956
391k
            }
957
1.47M
            else {
958
1.47M
              (*theMarker)(the, aTemporary);
959
1.47M
              aProperty = aProperty->next;
960
1.47M
            }
961
1.86M
          }
962
2.45M
          else
963
2.45M
            aProperty = aProperty->next;
964
4.32M
          break;
965
          
966
49.3M
        case XS_CALLBACK_KIND:
967
53.5M
        case XS_CODE_KIND:
968
53.5M
        #if mxKeysGarbageCollection
969
53.5M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
970
12.1M
            fxMarkID(the, aProperty->ID);
971
53.5M
        #endif
972
53.5M
          (*theMarker)(the, aProperty);
973
53.5M
          aProperty = aProperty->next;
974
53.5M
          break;  
975
        
976
1.34G
        default:
977
1.34G
        #if mxKeysGarbageCollection
978
1.34G
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
979
62.4M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
980
37.1M
              fxMarkID(the, aProperty->ID);
981
62.4M
          }
982
1.34G
        #endif
983
1.34G
          (*theMarker)(the, aProperty);
984
1.34G
          aProperty = aProperty->next;
985
1.34G
          break; 
986
1.81G
        }
987
1.81G
      }
988
103k
      else
989
103k
        aProperty = aProperty->next;
990
1.81G
    }
991
276M
    else if (theCurrent->value.instance.garbage) {
992
119M
      aProperty = theCurrent->value.instance.garbage;
993
119M
      theCurrent->value.instance.garbage = C_NULL;
994
119M
      switch (aProperty->kind) {
995
501k
      case XS_INSTANCE_KIND:
996
501k
        aTemporary = aProperty->value.instance.prototype;
997
501k
        aProperty->value.instance.prototype = theCurrent;
998
501k
        theCurrent = aTemporary;
999
501k
        aProperty = aProperty->next;
1000
501k
        break;
1001
116M
      case XS_REFERENCE_KIND:
1002
116M
        aTemporary = aProperty->value.reference;
1003
116M
        aProperty->value.reference = theCurrent;
1004
116M
        theCurrent = aTemporary;
1005
116M
        aProperty = aProperty->next;
1006
116M
        break;
1007
2.16M
      case XS_PROXY_KIND:
1008
2.16M
        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
165
          else {
1022
165
            aProperty = aProperty->next;
1023
165
          }
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.16M
        break;
1032
298k
      case XS_CLOSURE_KIND:
1033
298k
        aTemporary = aProperty->value.closure->value.reference;
1034
298k
        aProperty->value.closure->value.reference = theCurrent;
1035
298k
        theCurrent = aTemporary;
1036
298k
        aProperty = aProperty->next;
1037
298k
        break;
1038
119M
      }
1039
119M
    }
1040
157M
    else
1041
157M
      break;
1042
2.08G
  }
1043
157M
}
1044
1045
void fxMarkReference(txMachine* the, txSlot* theSlot)
1046
1.43G
{
1047
1.43G
  txSlot* aSlot;
1048
1.43G
  switch (theSlot->kind) {
1049
84.3M
  case XS_REFERENCE_KIND:
1050
84.3M
    aSlot = theSlot->value.reference;
1051
84.3M
    if (!(aSlot->flag & XS_MARK_FLAG))
1052
70.2M
      fxMarkInstance(the, aSlot, fxMarkReference);
1053
84.3M
    break;
1054
1.77M
  case XS_CLOSURE_KIND:
1055
1.77M
    aSlot = theSlot->value.closure;
1056
1.77M
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG))) {
1057
264k
      aSlot->flag |= XS_MARK_FLAG; 
1058
264k
      fxMarkReference(the, aSlot);
1059
264k
    }
1060
1.77M
    break;
1061
231k
  case XS_INSTANCE_KIND:
1062
231k
    if (!(theSlot->flag & XS_MARK_FLAG))
1063
231k
      fxMarkInstance(the, theSlot, fxMarkReference);
1064
231k
    break;
1065
1.94M
  case XS_ACCESSOR_KIND:
1066
1.94M
    aSlot = theSlot->value.accessor.getter;
1067
1.94M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1068
1.92M
      fxCheckCStack(the);
1069
1.92M
      fxMarkInstance(the, aSlot, fxMarkReference);
1070
1.92M
    }
1071
1.94M
    aSlot = theSlot->value.accessor.setter;
1072
1.94M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1073
947k
      fxCheckCStack(the);
1074
947k
      fxMarkInstance(the, aSlot, fxMarkReference);
1075
947k
    }
1076
1.94M
    break;
1077
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1078
0
  case XS_ARGUMENTS_STRICT_KIND:
1079
103M
  case XS_ARRAY_KIND:
1080
104M
  case XS_STACK_KIND:
1081
104M
    fxCheckCStack(the);
1082
104M
    if ((aSlot = theSlot->value.array.address)) {
1083
45.8M
      txIndex aLength = (((txChunk*)(((txByte*)aSlot) - sizeof(txChunk)))->size) / sizeof(txSlot);
1084
45.8M
      if (aLength > theSlot->value.array.length)
1085
5.71k
        aLength = theSlot->value.array.length;
1086
440M
      while (aLength) {
1087
394M
        fxMarkReference(the, aSlot);
1088
394M
        aSlot++;
1089
394M
        aLength--;
1090
394M
      }
1091
45.8M
    }
1092
104M
    break;
1093
24.0M
  case XS_CALLBACK_KIND:
1094
24.0M
    aSlot = theSlot->value.callback.closures;
1095
24.0M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1096
0
      fxCheckCStack(the);
1097
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1098
0
    }
1099
24.0M
    break;
1100
3.52M
  case XS_CODE_KIND:
1101
    // continue
1102
3.56M
  case XS_CODE_X_KIND:
1103
3.56M
    aSlot = theSlot->value.code.closures;
1104
3.56M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1105
1.81M
      fxCheckCStack(the);
1106
1.81M
      fxMarkInstance(the, aSlot, fxMarkReference);
1107
1.81M
    }
1108
3.56M
    break;
1109
27.6M
  case XS_HOME_KIND:
1110
27.6M
    aSlot = theSlot->value.home.object;
1111
27.6M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1112
8.50M
      fxCheckCStack(the);
1113
8.50M
      fxMarkInstance(the, aSlot, fxMarkReference);
1114
8.50M
    }
1115
27.6M
    aSlot = theSlot->value.home.module;
1116
27.6M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1117
2.04k
      fxCheckCStack(the);
1118
2.04k
      fxMarkInstance(the, aSlot, fxMarkReference);
1119
2.04k
    }
1120
27.6M
    break;
1121
13
  case XS_MODULE_KIND:
1122
19.8k
  case XS_PROGRAM_KIND:
1123
19.8k
#if mxKeysGarbageCollection
1124
19.8k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1125
13.5k
      fxMarkID(the, theSlot->value.module.id);
1126
19.8k
#endif
1127
19.8k
    aSlot = theSlot->value.module.realm;
1128
19.8k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1129
19.8k
      fxCheckCStack(the);
1130
19.8k
      fxMarkInstance(the, aSlot, fxMarkReference);
1131
19.8k
    }
1132
19.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
19.9k
  case XS_HOST_KIND:
1144
19.9k
    if (theSlot->value.host.data) {
1145
67
      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
67
    }
1148
19.9k
    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
230k
  case XS_ERROR_KIND:
1164
230k
    aSlot = theSlot->value.error.info;
1165
230k
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1166
230k
      fxMarkInstance(the, aSlot, fxMarkReference);
1167
230k
    break;
1168
51
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1169
114
  case XS_DISPOSABLE_STACK_KIND:
1170
99.3k
  case XS_LIST_KIND:
1171
99.3k
    fxCheckCStack(the);
1172
99.3k
    aSlot = theSlot->value.list.first;
1173
146k
    while (aSlot) {
1174
46.6k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1175
46.6k
        aSlot->flag |= XS_MARK_FLAG;
1176
46.6k
        fxMarkReference(the, aSlot);
1177
46.6k
      }
1178
46.6k
      aSlot = aSlot->next;
1179
46.6k
    }
1180
99.3k
    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
34
  case XS_MAP_KIND:
1196
14.4k
  case XS_SET_KIND:
1197
14.4k
    {
1198
14.4k
      txSlot** anAddress = theSlot->value.table.address;
1199
14.4k
      txInteger aLength = theSlot->value.table.length;
1200
82.6k
      while (aLength) {
1201
68.2k
        aSlot = *anAddress;
1202
103k
        while (aSlot) {
1203
35.0k
          aSlot->flag |= XS_MARK_FLAG; 
1204
35.0k
          aSlot = aSlot->next;
1205
35.0k
        }
1206
68.2k
        anAddress++;
1207
68.2k
        aLength--;
1208
68.2k
      }
1209
14.4k
    }
1210
14.4k
    break;
1211
164k
  case XS_WEAK_MAP_KIND:
1212
164k
  case XS_WEAK_SET_KIND:
1213
164k
    aSlot = theSlot->value.weakList.first;
1214
194k
    while (aSlot) {
1215
29.9k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1216
29.9k
        aSlot->flag |= XS_MARK_FLAG;
1217
29.9k
        fxMarkReference(the, aSlot);
1218
29.9k
      }
1219
29.9k
      aSlot = aSlot->next;
1220
29.9k
    }
1221
164k
    break;
1222
93.1k
  case XS_WEAK_ENTRY_KIND:
1223
93.1k
    aSlot = theSlot->value.weakEntry.check;
1224
93.1k
    if (aSlot->flag & XS_MARK_FLAG) {
1225
21.6k
      aSlot = theSlot->value.weakEntry.value;
1226
21.6k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1227
21.6k
        aSlot->flag |= XS_MARK_FLAG; 
1228
21.6k
        fxMarkReference(the, aSlot);
1229
21.6k
      }
1230
21.6k
    }
1231
93.1k
    break;
1232
230k
  case XS_WEAK_REF_KIND:
1233
230k
    aSlot = theSlot->value.weakRef.target;
1234
230k
    if (aSlot) {
1235
230k
  #ifdef mxSnapshot
1236
230k
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1237
230k
        fxMarkReference(the, aSlot);
1238
230k
      }
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
230k
    }
1248
230k
    break;
1249
171k
  case XS_FINALIZATION_REGISTRY_KIND:
1250
171k
    aSlot = theSlot->value.finalizationRegistry.callback;
1251
171k
    if (aSlot) {
1252
171k
      fxCheckCStack(the);
1253
171k
      aSlot->flag |= XS_MARK_FLAG;
1254
171k
      fxMarkReference(the, aSlot);
1255
171k
      aSlot = aSlot->next;
1256
454k
      while (aSlot) {
1257
283k
        aSlot->flag |= XS_MARK_FLAG;
1258
283k
        fxMarkReference(the, aSlot); // holdings
1259
283k
        aSlot = aSlot->next;
1260
283k
        if (aSlot) {
1261
283k
          aSlot->flag |= XS_MARK_FLAG;
1262
          // weak target and token
1263
283k
          aSlot = aSlot->next;
1264
283k
        }
1265
283k
      }
1266
171k
    }
1267
171k
    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
818k
  case XS_SYMBOL_KIND:
1276
818k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1277
487k
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1278
240k
        fxMarkID(the, theSlot->value.symbol);
1279
487k
    }
1280
818k
    break;  
1281
7.60M
  case XS_AT_KIND:
1282
7.60M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1283
7.03M
      fxMarkID(the, theSlot->value.at.id);
1284
7.60M
    break; 
1285
1.43G
#endif
1286
1.43G
  }
1287
1.43G
}
1288
1289
void fxMarkValue(txMachine* the, txSlot* theSlot)
1290
851M
{
1291
851M
#define mxMarkChunk(_THE_DATA) \
1292
851M
  ((txChunk*)(((txByte*)_THE_DATA) - sizeof(txChunk)))->size |= mxChunkFlag
1293
1294
851M
  txSlot* aSlot;
1295
851M
  switch (theSlot->kind) {
1296
210M
  case XS_STRING_KIND:
1297
210M
    mxMarkChunk(theSlot->value.string);
1298
210M
    break;
1299
1.27k
  case XS_BIGINT_KIND:
1300
1.27k
    mxMarkChunk(theSlot->value.bigint.data);
1301
1.27k
    break;
1302
114M
  case XS_REFERENCE_KIND:
1303
114M
    aSlot = theSlot->value.reference;
1304
114M
    if (!(aSlot->flag & XS_MARK_FLAG))
1305
68.4M
      fxMarkInstance(the, aSlot, fxMarkValue);
1306
114M
    break;
1307
1.50M
  case XS_CLOSURE_KIND:
1308
1.50M
    aSlot = theSlot->value.closure;
1309
1.50M
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG))) {
1310
131k
      aSlot->flag |= XS_MARK_FLAG; 
1311
131k
      fxMarkValue(the, aSlot);
1312
131k
    }
1313
1.50M
    break;
1314
244k
  case XS_INSTANCE_KIND:
1315
244k
    if (!(theSlot->flag & XS_MARK_FLAG))
1316
244k
      fxMarkInstance(the, theSlot, fxMarkValue);
1317
244k
    break;
1318
    
1319
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1320
0
  case XS_ARGUMENTS_STRICT_KIND:
1321
59.3M
  case XS_ARRAY_KIND:
1322
65.6M
  case XS_STACK_KIND:
1323
65.6M
    fxCheckCStack(the);
1324
65.6M
    if ((aSlot = theSlot->value.array.address)) {
1325
59.4M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
1326
59.4M
      if (!(chunk->size & mxChunkFlag)) {
1327
59.4M
        txIndex aLength = chunk->size / sizeof(txSlot);
1328
59.4M
        if (aLength > theSlot->value.array.length)
1329
6.60k
          aLength = theSlot->value.array.length;
1330
409M
        while (aLength) {
1331
350M
          fxMarkValue(the, aSlot);
1332
350M
          aSlot++;
1333
350M
          aLength--;
1334
350M
        }
1335
59.4M
        mxMarkChunk(theSlot->value.array.address);
1336
59.4M
      }
1337
59.4M
    }
1338
65.6M
    break;
1339
847k
  case XS_ARRAY_BUFFER_KIND:
1340
847k
    if (theSlot->value.arrayBuffer.address)
1341
847k
      mxMarkChunk(theSlot->value.arrayBuffer.address);
1342
847k
    break;
1343
25.2M
  case XS_CALLBACK_KIND:
1344
25.2M
    aSlot = theSlot->value.callback.closures;
1345
25.2M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1346
0
      fxCheckCStack(the);
1347
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1348
0
    }
1349
25.2M
    break;
1350
639k
  case XS_CODE_KIND:
1351
639k
    mxMarkChunk(theSlot->value.code.address);
1352
    /* continue */
1353
754k
  case XS_CODE_X_KIND:
1354
754k
    aSlot = theSlot->value.code.closures;
1355
754k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1356
402k
      fxCheckCStack(the);
1357
402k
      fxMarkInstance(the, aSlot, fxMarkValue);
1358
402k
    }
1359
754k
    break;
1360
39.2k
  case XS_GLOBAL_KIND:
1361
39.2k
    mxMarkChunk(theSlot->value.table.address);
1362
39.2k
    break;
1363
39.4k
  case XS_HOST_KIND:
1364
39.4k
    if (theSlot->value.host.data) {
1365
230
      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
230
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
1368
122
        mxMarkChunk(theSlot->value.host.data);
1369
230
    }
1370
39.4k
    break;
1371
3
  case XS_IDS_KIND:
1372
3
    if (theSlot->value.IDs)
1373
3
      mxMarkChunk(theSlot->value.IDs);
1374
3
    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
12.2k
  case XS_REGEXP_KIND:
1384
12.2k
    if (theSlot->value.regexp.code)
1385
11.8k
      mxMarkChunk(theSlot->value.regexp.code);
1386
12.2k
    if (theSlot->value.regexp.data)
1387
12.1k
      mxMarkChunk(theSlot->value.regexp.data);
1388
12.2k
    break;
1389
    
1390
2.44M
  case XS_ACCESSOR_KIND:
1391
2.44M
    aSlot = theSlot->value.accessor.getter;
1392
2.44M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1393
2.40M
      fxCheckCStack(the);
1394
2.40M
      fxMarkInstance(the, aSlot, fxMarkValue);
1395
2.40M
    }
1396
2.44M
    aSlot = theSlot->value.accessor.setter;
1397
2.44M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1398
482k
      fxCheckCStack(the);
1399
482k
      fxMarkInstance(the, aSlot, fxMarkValue);
1400
482k
    }
1401
2.44M
    break;
1402
25.9M
  case XS_HOME_KIND:
1403
25.9M
    aSlot = theSlot->value.home.object;
1404
25.9M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1405
1.11M
      fxCheckCStack(the);
1406
1.11M
      fxMarkInstance(the, aSlot, fxMarkValue);
1407
1.11M
    }
1408
25.9M
    aSlot = theSlot->value.home.module;
1409
25.9M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1410
1.81k
      fxCheckCStack(the);
1411
1.81k
      fxMarkInstance(the, aSlot, fxMarkValue);
1412
1.81k
    }
1413
25.9M
    break;
1414
34
  case XS_MODULE_KIND:
1415
39.2k
  case XS_PROGRAM_KIND:
1416
39.2k
#if mxKeysGarbageCollection
1417
39.2k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1418
6.50k
      fxMarkID(the, theSlot->value.module.id);
1419
39.2k
#endif
1420
39.2k
    aSlot = theSlot->value.module.realm;
1421
39.2k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1422
39.2k
      fxCheckCStack(the);
1423
39.2k
      fxMarkInstance(the, aSlot, fxMarkValue);
1424
39.2k
    }
1425
39.2k
    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
81.0M
  case XS_KEY_KIND:
1437
81.0M
    if (theSlot->value.key.string)
1438
81.0M
      mxMarkChunk(theSlot->value.key.string);
1439
81.0M
    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
244k
  case XS_ERROR_KIND:
1447
244k
    aSlot = theSlot->value.error.info;
1448
244k
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1449
244k
      fxMarkInstance(the, aSlot, fxMarkValue);
1450
244k
    break;
1451
14
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1452
42
  case XS_DISPOSABLE_STACK_KIND:
1453
133k
  case XS_LIST_KIND:
1454
133k
    fxCheckCStack(the);
1455
133k
    aSlot = theSlot->value.list.first;
1456
157k
    while (aSlot) {
1457
23.9k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1458
23.9k
        aSlot->flag |= XS_MARK_FLAG;
1459
23.9k
        fxMarkValue(the, aSlot);
1460
23.9k
      }
1461
23.9k
      aSlot = aSlot->next;
1462
23.9k
    }
1463
133k
    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
15
  case XS_MAP_KIND:
1479
4.92k
  case XS_SET_KIND:
1480
4.92k
    {
1481
4.92k
      txSlot** anAddress = theSlot->value.table.address;
1482
4.92k
      txInteger aLength = theSlot->value.table.length;
1483
40.1k
      while (aLength) {
1484
35.2k
        aSlot = *anAddress;
1485
54.4k
        while (aSlot) {
1486
19.2k
          aSlot->flag |= XS_MARK_FLAG; 
1487
19.2k
          aSlot = aSlot->next;
1488
19.2k
        }
1489
35.2k
        anAddress++;
1490
35.2k
        aLength--;
1491
35.2k
      }
1492
4.92k
    }
1493
4.92k
    mxMarkChunk(theSlot->value.table.address);
1494
4.92k
    break;
1495
1496
76.3k
  case XS_WEAK_MAP_KIND:
1497
76.3k
  case XS_WEAK_SET_KIND:
1498
76.3k
    aSlot = theSlot->value.weakList.first;
1499
93.5k
    while (aSlot) {
1500
17.1k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1501
17.1k
        aSlot->flag |= XS_MARK_FLAG;
1502
17.1k
        fxMarkValue(the, aSlot);
1503
17.1k
      }
1504
17.1k
      aSlot = aSlot->next;
1505
17.1k
    }
1506
76.3k
    break;
1507
54.6k
  case XS_WEAK_ENTRY_KIND:
1508
54.6k
    aSlot = theSlot->value.weakEntry.check;
1509
54.6k
    if (aSlot->flag & XS_MARK_FLAG) {
1510
15.7k
      aSlot = theSlot->value.weakEntry.value;
1511
15.7k
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1512
15.7k
        aSlot->flag |= XS_MARK_FLAG; 
1513
15.7k
        fxMarkValue(the, aSlot);
1514
15.7k
      }
1515
15.7k
    }
1516
54.6k
    break;
1517
243k
  case XS_WEAK_REF_KIND:
1518
243k
    aSlot = theSlot->value.weakRef.target;
1519
243k
    if (aSlot) {
1520
243k
  #ifdef mxSnapshot
1521
243k
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1522
243k
        fxMarkValue(the, aSlot);
1523
243k
      }
1524
29
      else {
1525
29
        theSlot->value.weakRef.link = the->firstWeakRefLink;
1526
29
        the->firstWeakRefLink = theSlot;
1527
29
      }
1528
  #else
1529
      theSlot->value.weakRef.link = the->firstWeakRefLink;
1530
      the->firstWeakRefLink = theSlot;
1531
  #endif
1532
243k
    }
1533
243k
    break;
1534
80.2k
  case XS_FINALIZATION_REGISTRY_KIND:
1535
80.2k
    aSlot = theSlot->value.finalizationRegistry.callback;
1536
80.2k
    if (aSlot) {
1537
80.2k
      aSlot->flag |= XS_MARK_FLAG;
1538
80.2k
      fxMarkValue(the, aSlot);
1539
80.2k
      aSlot = aSlot->next;
1540
209k
      while (aSlot) {
1541
129k
        aSlot->flag |= XS_MARK_FLAG;
1542
129k
                fxCheckCStack(the);
1543
129k
        fxMarkValue(the, aSlot); // holdings
1544
129k
        aSlot = aSlot->next;
1545
129k
        if (aSlot) {
1546
129k
          aSlot->flag |= XS_MARK_FLAG;
1547
          // weak target and token
1548
129k
          aSlot = aSlot->next;
1549
129k
        }
1550
129k
      }
1551
80.2k
    }
1552
80.2k
    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
1.36M
  case XS_SYMBOL_KIND:
1562
1.36M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1563
194k
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1564
97.2k
        fxMarkID(the, theSlot->value.symbol);
1565
194k
    }
1566
1.36M
    break;  
1567
57.9M
  case XS_AT_KIND:
1568
57.9M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1569
3.96M
      fxMarkID(the, theSlot->value.at.id);
1570
57.9M
    break; 
1571
851M
#endif
1572
851M
  }
1573
851M
}
1574
1575
void fxMarkWeakStuff(txMachine* the) 
1576
59.1k
{
1577
59.1k
  txSlot* slot;
1578
59.1k
  txSlot** address;
1579
1580
59.1k
  {
1581
59.1k
    txSlot* list;
1582
59.1k
    txSlot** listAddress = &the->firstWeakListLink;
1583
462k
    while ((list = *listAddress)) {
1584
403k
      if (list->flag & XS_MARK_FLAG) {
1585
241k
        txSlot* listEntry;
1586
241k
        txSlot** listEntryAddress = &list->value.weakList.first;
1587
288k
        while ((listEntry = *listEntryAddress)) {
1588
47.0k
          txSlot* value = listEntry->value.weakEntry.value;
1589
47.0k
          if ((value->flag & XS_MARK_FLAG) && (value->kind != XS_UNINITIALIZED_KIND)) {
1590
37.3k
            listEntryAddress = &listEntry->next;
1591
37.3k
          }
1592
9.72k
          else {
1593
9.72k
            listEntry->flag &= ~XS_MARK_FLAG;
1594
9.72k
            *listEntryAddress = listEntry->next;
1595
9.72k
          }
1596
47.0k
        }
1597
241k
        listAddress = &list->value.weakList.link;
1598
241k
      }
1599
162k
      else {
1600
162k
        txSlot* listEntry = list->value.weakList.first;
1601
229k
        while (listEntry) {
1602
67.3k
          txSlot* key = listEntry->value.weakEntry.check;
1603
67.3k
          if (key->flag & XS_MARK_FLAG) {
1604
63.5k
            txSlot* keyEntry;
1605
63.5k
            txSlot** keyEntryAddress = &key->next;
1606
80.7M
            while ((keyEntry = *keyEntryAddress)) {
1607
80.7M
              if (!(keyEntry->flag & XS_INTERNAL_FLAG))
1608
30
                break;
1609
80.7M
              if ((keyEntry->kind == XS_WEAK_ENTRY_KIND) && (keyEntry->value.weakEntry.check == list)) {
1610
63.4k
                keyEntry->flag &= ~XS_MARK_FLAG;
1611
63.4k
                *keyEntryAddress = keyEntry->next;
1612
63.4k
                break;
1613
63.4k
              }
1614
80.7M
              keyEntryAddress = &keyEntry->next;
1615
80.7M
            }
1616
63.5k
          }
1617
67.3k
          listEntry = listEntry->next;
1618
67.3k
        }
1619
162k
        *listAddress = list->value.weakList.link;
1620
162k
      }
1621
403k
    }
1622
59.1k
  }
1623
59.1k
  address = &the->firstWeakRefLink;
1624
59.1k
  while ((slot = *address)) {
1625
29
    if (!(slot->value.weakRef.target->flag & XS_MARK_FLAG))
1626
13
      slot->value.weakRef.target = C_NULL;
1627
29
    *address = C_NULL;
1628
29
    address = &(slot->value.weakRef.link);
1629
29
  }
1630
  
1631
59.1k
  if (mxFinalizationRegistries.kind == XS_REFERENCE_KIND) {
1632
59.1k
    slot = mxFinalizationRegistries.value.reference->next;
1633
275k
    while (slot) {
1634
216k
      fxMarkFinalizationRegistry(the, slot->value.closure);
1635
216k
      slot = slot->next;
1636
216k
    }
1637
59.1k
  }
1638
59.1k
}
1639
1640
txSize fxMultiplyChunkSizes(txMachine* the, txSize a, txSize b)
1641
52.3M
{
1642
52.3M
  txSize c;
1643
52.3M
#if __has_builtin(__builtin_mul_overflow)
1644
52.3M
  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
4
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1651
4
  }
1652
52.3M
  return c;
1653
52.3M
}
1654
1655
void* fxNewChunk(txMachine* the, txSize size)
1656
251M
{
1657
251M
  txSize offset = size;
1658
251M
  txChunk* chunk;
1659
251M
  txBoolean once = 1;
1660
251M
  size = fxAdjustChunkSize(the, size);
1661
251M
  chunk = fxFindChunk(the, size, &once);
1662
251M
  if (!chunk) {
1663
2.79k
    chunk = fxGrowChunk(the, size);
1664
2.79k
  }
1665
251M
#ifdef mxMetering
1666
251M
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1667
251M
#endif
1668
251M
  return fxCheckChunk(the, chunk, size, offset);
1669
251M
}
1670
1671
void* fxNewGrowableChunk(txMachine* the, txSize size, txSize capacity)
1672
1.99M
{
1673
#if mxNoChunks
1674
  return fxNewChunk(the, size);
1675
#else
1676
1.99M
  txSize offset = size;
1677
1.99M
  txChunk* chunk;
1678
1.99M
  txBoolean once = 1;
1679
1.99M
  size = fxAdjustChunkSize(the, size);
1680
1.99M
  capacity = fxAdjustChunkSize(the, capacity);
1681
1.99M
  chunk = fxFindChunk(the, capacity, &once);
1682
1.99M
  if (!chunk) {
1683
88
    chunk = fxGrowChunk(the, capacity);
1684
88
    if (!chunk) {
1685
0
      chunk = fxFindChunk(the, size, &once);
1686
0
      if (!chunk) {
1687
0
        chunk = fxGrowChunk(the, size);
1688
0
      }
1689
0
    }
1690
88
  }
1691
1.99M
#ifdef mxMetering
1692
1.99M
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1693
1.99M
#endif
1694
1.99M
  return fxCheckChunk(the, chunk, size, offset);
1695
1.99M
#endif
1696
1.99M
}
1697
1698
txSlot* fxNewSlot(txMachine* the) 
1699
592M
{
1700
592M
  txSlot* aSlot;
1701
592M
  txBoolean once = 1, allocate;
1702
  
1703
592M
#if mxStress
1704
592M
  if (fxShouldStress()) {
1705
34
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
1706
34
    once = 0;
1707
34
  }
1708
592M
#endif
1709
592M
again:
1710
592M
  aSlot = the->freeHeap;
1711
592M
  if (aSlot) {
1712
592M
    the->freeHeap = aSlot->next;
1713
592M
    aSlot->next = C_NULL;
1714
592M
    aSlot->ID = XS_NO_ID;
1715
592M
    aSlot->flag = XS_NO_FLAG;
1716
592M
  #ifdef mxSnapshot
1717
592M
    #if mx32bitID
1718
592M
      aSlot->dummy = 0;
1719
    #elif INTPTR_MAX == INT64_MAX
1720
      aSlot->dummy = 0;
1721
    #endif
1722
592M
  #endif
1723
#if mxPoisonSlots
1724
    ASAN_UNPOISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
1725
#endif
1726
592M
    the->currentHeapCount++;
1727
592M
    if (the->peakHeapCount < the->currentHeapCount)
1728
243M
      the->peakHeapCount = the->currentHeapCount;
1729
592M
#ifdef mxMetering
1730
592M
    the->meterIndex += XS_SLOT_ALLOCATION_METERING;
1731
592M
#endif
1732
592M
    return aSlot;
1733
592M
  }
1734
7.01k
  if (once) {
1735
6.27k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0), isThrashing;
1736
1737
6.27k
    fxCollect(the, XS_ORGANIC_FLAG);
1738
1739
6.27k
    isThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0);
1740
6.27k
    allocate = wasThrashing && isThrashing;
1741
1742
6.27k
    once = 0;
1743
6.27k
  }
1744
741
  else
1745
741
    allocate = 1;
1746
7.01k
  if (allocate) {
1747
2.65k
    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
2.65k
    fxGrowSlots(the, !(the->collectFlag & XS_SKIPPED_COLLECT_FLAG) ? the->minimumHeapCount : 64);
1752
2.65k
  }
1753
7.01k
  goto again;
1754
0
  return C_NULL;
1755
592M
}
1756
1757
void* fxRenewChunk(txMachine* the, void* theData, txSize size)
1758
21.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
21.9M
  txByte* aData = ((txByte*)theData) - sizeof(txChunk);
1770
21.9M
  txChunk* aChunk = (txChunk*)aData;
1771
21.9M
  txSize capacity = (txSize)(aChunk->temporary - aData);
1772
21.9M
  txBlock* aBlock = the->firstBlock;
1773
21.9M
  size = fxAdjustChunkSize(the, size);
1774
21.9M
  if (size <= capacity) {
1775
6.91M
    the->currentChunksSize += size - aChunk->size;
1776
6.91M
    if (the->peakChunksSize < the->currentChunksSize)
1777
751k
      the->peakChunksSize = the->currentChunksSize;
1778
6.91M
    aChunk->size = size;
1779
6.91M
  #ifdef mxMetering
1780
6.91M
    the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1781
6.91M
  #endif
1782
6.91M
    return theData;
1783
6.91M
  }
1784
32.4M
  while (aBlock) {
1785
27.4M
    if (aChunk->temporary == aBlock->current) {
1786
10.0M
      txSize delta = size - capacity;
1787
10.0M
      if (aBlock->current + delta <= aBlock->limit) {
1788
10.0M
        the->currentChunksSize += size - aChunk->size;
1789
10.0M
        if (the->peakChunksSize < the->currentChunksSize)
1790
883k
          the->peakChunksSize = the->currentChunksSize;
1791
10.0M
        aBlock->current += delta;
1792
10.0M
        aChunk->temporary = aBlock->current;
1793
10.0M
        aChunk->size = size;
1794
10.0M
      #ifdef mxSnapshot
1795
10.0M
        c_memset(aData + capacity, 0, delta);
1796
10.0M
      #endif
1797
10.0M
      #ifdef mxMetering
1798
10.0M
        the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1799
10.0M
      #endif
1800
10.0M
        return theData;
1801
10.0M
      }
1802
820
      else {
1803
820
        return C_NULL;
1804
820
      }
1805
10.0M
    }
1806
17.3M
    aBlock = aBlock->nextBlock;
1807
17.3M
  }
1808
4.91M
  return C_NULL;
1809
15.0M
#endif
1810
15.0M
}
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
39.2k
{
1884
39.2k
  txSize aTotal;
1885
#if mxNoChunks
1886
  txChunk** address;
1887
  txChunk* chunk;
1888
#else
1889
39.2k
  txBlock* aBlock;
1890
39.2k
  txByte* limit;
1891
39.2k
  txByte* next;
1892
39.2k
#endif
1893
39.2k
  txByte* current;
1894
39.2k
  txByte* temporary;
1895
39.2k
  txSize aSize;
1896
39.2k
  txByte** aCodeAddress;
1897
39.2k
  txSlot* aSlot;
1898
39.2k
  txSlot* bSlot;
1899
39.2k
  txSlot* cSlot;
1900
39.2k
  txSlot* freeSlot;
1901
39.2k
  txJump* jump;
1902
1903
#ifdef mxNever
1904
  startTime(&gxSweepChunkTime);
1905
#endif
1906
1907
39.2k
  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
39.2k
  aBlock = the->firstBlock;
1930
154k
  while (aBlock) {
1931
115k
    current = ((txByte*)aBlock) + sizeof(txBlock);
1932
115k
    limit = aBlock->current;
1933
115k
    temporary = current;
1934
479M
    while (current < limit) {
1935
479M
      aSize = ((txChunk*)current)->size;
1936
479M
      next = ((txChunk*)current)->temporary;
1937
479M
      if (aSize & mxChunkFlag) {
1938
285M
        aSize &= ~mxChunkFlag;
1939
285M
        ((txChunk*)current)->temporary = temporary;
1940
285M
        temporary += aSize;
1941
285M
        aTotal += aSize;
1942
285M
      }
1943
194M
      else {
1944
194M
        ((txChunk*)current)->temporary = C_NULL;
1945
194M
      }
1946
479M
      ((txChunk*)current)->size = (txSize)(next - current);
1947
479M
      current = next;
1948
479M
    }  
1949
115k
    aBlock->temporary = temporary;
1950
115k
    aBlock = aBlock->nextBlock;
1951
115k
  }
1952
39.2k
#endif
1953
39.2k
  the->currentChunksSize = aTotal;
1954
1955
39.2k
  aCodeAddress = &(the->code);
1956
39.2k
  aSlot = the->frame;
1957
2.21M
  while (aSlot) {
1958
2.17M
    mxCheck(the, aSlot->kind == XS_FRAME_KIND);
1959
2.17M
    if ((aSlot->flag & XS_C_FLAG) == 0) {
1960
1.61M
      bSlot = (aSlot + 3)->value.reference->next;
1961
1.61M
      if (bSlot->kind == XS_CODE_KIND) {
1962
1.57M
        current = bSlot->value.code.address;
1963
1.57M
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1964
1.57M
        if (temporary) {
1965
1.57M
          temporary += sizeof(txChunk);
1966
1.57M
          *aCodeAddress += temporary - current;
1967
1.57M
        }
1968
1.57M
      }
1969
1.61M
    }
1970
567k
    else {
1971
567k
      current = *aCodeAddress;
1972
567k
      if (current) {
1973
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1974
0
        if (temporary)
1975
0
          *aCodeAddress = temporary + sizeof(txChunk);
1976
0
      }
1977
567k
    }
1978
2.17M
    aCodeAddress = &(aSlot->value.frame.code);
1979
2.17M
    aSlot = aSlot->next;
1980
2.17M
  }
1981
  
1982
39.2k
  jump = the->firstJump;
1983
446k
  while (jump) {
1984
407k
    if (jump->flag) {
1985
43.3k
      aSlot = jump->frame;
1986
43.3k
      bSlot = (aSlot + 3)->value.reference->next;
1987
43.3k
      if (bSlot->kind == XS_CODE_KIND) {
1988
6.01k
        current = bSlot->value.code.address;
1989
6.01k
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1990
6.01k
        if (temporary) {
1991
6.01k
          temporary += sizeof(txChunk);
1992
6.01k
          jump->code += temporary - current;
1993
6.01k
        }
1994
6.01k
      }
1995
43.3k
    }
1996
364k
    else {
1997
364k
      current = jump->code;
1998
364k
      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
364k
    }
2004
407k
    jump = jump->nextJump;
2005
407k
  }
2006
  
2007
39.2k
  aSlot = the->stack;
2008
39.1M
  while (aSlot < the->stackTop) {
2009
39.1M
    fxSweepValue(the, aSlot);
2010
39.1M
    aSlot++;
2011
39.1M
  }
2012
39.2k
  aSlot = the->cRoot;
2013
39.2k
  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
39.2k
  aTotal = 0;
2024
39.2k
  freeSlot = C_NULL;
2025
39.2k
  aSlot = the->firstHeap;
2026
92.8k
  while (aSlot) {
2027
53.5k
    bSlot = aSlot + 1;
2028
53.5k
    cSlot = aSlot->value.reference;
2029
1.75G
    while (bSlot < cSlot) {
2030
1.75G
      if (bSlot->flag & XS_MARK_FLAG) {
2031
604M
        bSlot->flag &= ~XS_MARK_FLAG; 
2032
604M
        fxSweepValue(the, bSlot);
2033
604M
        aTotal++;
2034
604M
      }
2035
1.15G
      else {
2036
1.15G
      #ifndef mxLink
2037
1.15G
        if (bSlot->kind == XS_HOST_KIND) {
2038
4.75k
          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
4.75k
          else if (bSlot->value.host.variant.destructor)
2043
3.62k
            (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
2044
4.75k
        }
2045
1.15G
      #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
1.15G
        bSlot->kind = XS_UNDEFINED_KIND;
2055
1.15G
        bSlot->next = freeSlot;
2056
      #if mxPoisonSlots
2057
        ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
2058
      #endif
2059
1.15G
        freeSlot = bSlot;
2060
1.15G
      }
2061
1.75G
      bSlot++;
2062
1.75G
    }
2063
53.5k
    aSlot = aSlot->next;
2064
53.5k
  }
2065
39.2k
  the->currentHeapCount = aTotal;
2066
39.2k
  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
39.2k
  aBlock = the->firstBlock;
2087
154k
  while (aBlock) {
2088
115k
    txByte* former = C_NULL;
2089
115k
    current = ((txByte*)aBlock) + sizeof(txBlock);
2090
115k
    limit = aBlock->current;
2091
479M
    while (current < limit) {
2092
479M
      aSize = ((txChunk*)current)->size;
2093
479M
      next = current + aSize;
2094
479M
      if ((temporary = ((txChunk*)current)->temporary)) {
2095
285M
        if (former) {
2096
285M
          ((txChunk*)former)->temporary = temporary;
2097
285M
          ((txChunk*)former)->size = (txSize)(temporary - former);
2098
285M
        }
2099
285M
        if (temporary != current)
2100
16.6M
          c_memmove(temporary, current, aSize);
2101
285M
        former = temporary;
2102
285M
      }
2103
479M
      current = next;
2104
479M
    }
2105
115k
    if (former) {
2106
103k
      ((txChunk*)former)->temporary = aBlock->temporary;
2107
103k
      ((txChunk*)former)->size = (txSize)(aBlock->temporary - former);
2108
103k
    }
2109
115k
    aBlock->current = aBlock->temporary;
2110
115k
    aBlock->temporary = C_NULL;
2111
115k
    aBlock = aBlock->nextBlock;
2112
115k
  }
2113
39.2k
#endif
2114
  
2115
#ifdef mxNever
2116
  stopTime(&gxCompactChunkTime);
2117
#endif
2118
39.2k
}
2119
2120
void fxSweepValue(txMachine* the, txSlot* theSlot)
2121
993M
{
2122
993M
  txSlot* aSlot;
2123
993M
  txByte* data;
2124
  
2125
993M
#define mxSweepChunk(_THE_DATA, _THE_DATA_TYPE) \
2126
993M
  if ((data = (txByte*)(((txChunk*)(((txByte*)(_THE_DATA)) - sizeof(txChunk)))->temporary))) \
2127
352M
    ((_THE_DATA)) = (_THE_DATA_TYPE)(data + sizeof(txChunk))
2128
2129
993M
  switch (theSlot->kind) {
2130
210M
  case XS_STRING_KIND:
2131
210M
    mxSweepChunk(theSlot->value.string, txString);
2132
210M
    break;
2133
1.27k
  case XS_BIGINT_KIND:
2134
1.27k
    mxSweepChunk(theSlot->value.bigint.data, txU4*);
2135
1.27k
    break;
2136
2137
0
  case XS_ARGUMENTS_SLOPPY_KIND:
2138
0
  case XS_ARGUMENTS_STRICT_KIND:
2139
59.3M
  case XS_ARRAY_KIND:
2140
65.6M
  case XS_STACK_KIND:
2141
65.6M
    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
59.4M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
2147
59.4M
      txIndex aLength = chunk->size / sizeof(txSlot);
2148
59.4M
      if (aLength > theSlot->value.array.length)
2149
8.71k
        aLength = theSlot->value.array.length;
2150
409M
      while (aLength) {
2151
350M
        fxSweepValue(the, aSlot);
2152
350M
        aSlot++;
2153
350M
        aLength--;
2154
350M
      }
2155
#if mxNoChunks
2156
#else
2157
59.4M
      mxSweepChunk(theSlot->value.array.address, txSlot*);
2158
59.4M
#endif
2159
59.4M
    }
2160
65.6M
    break;
2161
847k
  case XS_ARRAY_BUFFER_KIND:
2162
847k
    if (theSlot->value.arrayBuffer.address)
2163
847k
      mxSweepChunk(theSlot->value.arrayBuffer.address, txByte*);
2164
847k
    break;
2165
639k
  case XS_CODE_KIND:
2166
639k
    mxSweepChunk(theSlot->value.code.address, txByte*);
2167
639k
    break;
2168
39.2k
  case XS_GLOBAL_KIND:
2169
39.2k
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2170
39.2k
    break;
2171
39.4k
  case XS_HOST_KIND:
2172
39.4k
    if (theSlot->value.host.data) {
2173
230
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
2174
122
        mxSweepChunk(theSlot->value.host.data, void*);
2175
230
    }
2176
39.4k
    break;
2177
3
  case XS_IDS_KIND:
2178
3
    if (theSlot->value.IDs)
2179
3
      mxSweepChunk(theSlot->value.IDs, txID*);
2180
3
    break;
2181
12.2k
  case XS_REGEXP_KIND:
2182
12.2k
    if (theSlot->value.regexp.code)
2183
11.8k
      mxSweepChunk(theSlot->value.regexp.code, void*);
2184
12.2k
    if (theSlot->value.regexp.data)
2185
12.1k
      mxSweepChunk(theSlot->value.regexp.data, void*);
2186
12.2k
    break;
2187
81.0M
  case XS_KEY_KIND:
2188
81.0M
    if (theSlot->value.key.string)
2189
81.0M
      mxSweepChunk(theSlot->value.key.string, txString);
2190
81.0M
    break;
2191
15
  case XS_MAP_KIND:
2192
4.92k
  case XS_SET_KIND:
2193
4.92k
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2194
4.92k
    break;
2195
993M
  }
2196
993M
}