Coverage Report

Created: 2025-06-13 06:10

/src/moddable/xs/sources/xsMemory.c
Line
Count
Source (jump to first uncovered line)
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
81.8M
{
62
81.8M
  if (!gxStress)
63
81.8M
    return 0;
64
65
0
  if (gxStress > 0)
66
0
    return 1;
67
68
0
  gxStress += 1;
69
0
  return 0 == gxStress;
70
0
}
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
220M
#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
16.7M
{
144
16.7M
  txSize c;
145
16.7M
#if __has_builtin(__builtin_add_overflow)
146
16.7M
  if (__builtin_add_overflow(a, b, &c)) {
147
#else
148
  c = a + b;
149
  if (((a ^ c) & (b ^ c)) < 0) {
150
#endif
151
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
152
0
  }
153
16.7M
  return c;
154
16.7M
}
155
156
txSize fxAdjustChunkSize(txMachine* the, txSize size)
157
14.7M
{
158
14.7M
  txSize adjust = sizeof(txChunk);
159
14.7M
  txSize modulo = size & (sizeof(size_t) - 1);
160
14.7M
  if (modulo)
161
7.30M
    adjust += sizeof(size_t) - modulo;
162
14.7M
  return fxAddChunkSizes(the, size, adjust);
163
14.7M
}
164
165
void fxAllocate(txMachine* the, txCreation* theCreation)
166
6.57k
{
167
#ifdef mxNever
168
  startTime(&gxLifeTime);
169
#endif
170
6.57k
#if mxStress
171
6.57k
  gxStress = 0;
172
6.57k
#endif
173
174
6.57k
  the->currentChunksSize = 0;
175
6.57k
  the->peakChunksSize = 0;
176
6.57k
  the->maximumChunksSize = 0;
177
6.57k
  the->minimumChunksSize = theCreation->incrementalChunkSize;
178
  
179
6.57k
  the->currentHeapCount = 0;
180
6.57k
  the->peakHeapCount = 0;
181
6.57k
  the->maximumHeapCount = 0;
182
6.57k
  the->minimumHeapCount = theCreation->incrementalHeapCount;
183
  
184
6.57k
  the->firstBlock = C_NULL;
185
6.57k
  the->firstHeap = C_NULL;
186
187
#if mxNoChunks
188
  the->maximumChunksSize = theCreation->initialChunkSize;
189
#else
190
6.57k
  fxGrowChunks(the, theCreation->initialChunkSize);
191
6.57k
#endif
192
193
6.57k
  the->stackBottom = fxAllocateSlots(the, theCreation->stackCount);
194
6.57k
  the->stackTop = the->stackBottom + theCreation->stackCount;
195
6.57k
  the->stackIntrinsics = the->stackTop;
196
6.57k
  the->stackPrototypes = the->stackTop - XS_INTRINSICS_COUNT;
197
6.57k
  the->stack = the->stackTop;
198
#ifdef mxInstrument
199
  the->stackPeak = the->stackTop;
200
#endif
201
202
6.57k
  fxGrowSlots(the, theCreation->initialHeapCount);
203
204
6.57k
  the->keyCount = (txID)theCreation->initialKeyCount;
205
6.57k
  the->keyDelta = (txID)theCreation->incrementalKeyCount;
206
6.57k
  the->keyIndex = 0;
207
6.57k
  the->keyArray = (txSlot **)c_malloc_uint32(theCreation->initialKeyCount * sizeof(txSlot*));
208
6.57k
  if (!the->keyArray)
209
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);   
210
  
211
6.57k
  the->nameModulo = theCreation->nameModulo;
212
6.57k
  the->nameTable = (txSlot **)c_malloc_uint32(theCreation->nameModulo * sizeof(txSlot*));
213
6.57k
  if (!the->nameTable)
214
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
215
216
6.57k
  the->symbolModulo = theCreation->symbolModulo;
217
6.57k
  the->symbolTable = (txSlot **)c_malloc_uint32(theCreation->symbolModulo * sizeof(txSlot*));
218
6.57k
  if (!the->symbolTable)
219
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
220
    
221
6.57k
  fxAllocateStringInfoCache(the);
222
223
6.57k
  the->stackLimit = fxCStackLimit();
224
225
6.57k
  the->cRoot = C_NULL;
226
6.57k
  the->parserBufferSize = theCreation->parserBufferSize;
227
6.57k
  the->parserTableModulo = theCreation->parserTableModulo;
228
  
229
6.57k
#ifdef mxDebug
230
6.57k
  the->pathCount = 256;
231
6.57k
  the->pathValue = c_malloc(the->pathCount);
232
6.57k
  if (!the->pathValue)
233
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
234
6.57k
#endif
235
6.57k
}
236
237
void* fxCheckChunk(txMachine* the, txChunk* chunk, txSize size, txSize offset)
238
14.6M
{
239
14.6M
  if (chunk) {
240
14.6M
    txByte* data = (txByte*)chunk;
241
#if mxNoChunks
242
    chunk->size = size;
243
    the->currentChunksSize += size;
244
#else
245
14.6M
    txSize capacity = (txSize)(chunk->temporary - data);
246
14.6M
  #ifdef mxSnapshot
247
14.6M
    #if INTPTR_MAX == INT64_MAX
248
14.6M
      chunk->dummy = 0;
249
14.6M
    #endif
250
  #ifdef mxSnapshotRandomInit
251
    arc4random_buf(data + sizeof(txChunk), offset);
252
  #endif    
253
14.6M
  #endif
254
14.6M
    offset += sizeof(txChunk);
255
14.6M
    c_memset(data + offset, 0, capacity - offset);
256
14.6M
    chunk->size = size;
257
14.6M
    the->currentChunksSize += capacity;
258
14.6M
#endif
259
14.6M
    if (the->peakChunksSize < the->currentChunksSize)
260
10.3M
      the->peakChunksSize = the->currentChunksSize;
261
14.6M
    return data + sizeof(txChunk);
262
14.6M
  }
263
0
  fxReport(the, "# Chunk allocation: failed for %ld bytes\n", (long)size);
264
0
  fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
265
0
  return C_NULL;
266
14.6M
}
267
268
#if defined(__clang__) || defined (__GNUC__)
269
  __attribute__((no_sanitize_address))
270
#endif
271
void fxCheckCStack(txMachine* the)
272
224M
{
273
224M
    char x;
274
224M
    char *stack = &x;
275
224M
  if (stack <= the->stackLimit) {
276
0
    fxAbort(the, XS_NATIVE_STACK_OVERFLOW_EXIT);
277
0
  }
278
224M
}
279
280
void fxCollect(txMachine* the, txFlag theFlag)
281
10.7k
{
282
10.7k
  txSize aCount;
283
10.7k
  txSlot* freeSlot;
284
10.7k
  txSlot* aSlot;
285
10.7k
  txSlot* bSlot;
286
10.7k
  txSlot* cSlot;
287
288
10.7k
  if ((the->collectFlag & XS_COLLECTING_FLAG) == 0) {
289
0
    the->collectFlag |= XS_SKIPPED_COLLECT_FLAG;
290
0
    return;
291
0
  }
292
10.7k
  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
10.7k
  if (theFlag & XS_COMPACT_FLAG) {
308
3.16k
    fxInvalidateStringInfoCache(the);
309
3.16k
    fxMark(the, fxMarkValue);
310
3.16k
    fxMarkWeakStuff(the);
311
  #ifdef mxNever
312
    stopTime(&gxMarkTime);
313
  #endif
314
3.16k
    fxSweep(the);
315
3.16k
  }
316
7.62k
  else {
317
7.62k
    fxMark(the, fxMarkReference);
318
7.62k
    fxMarkWeakStuff(the);
319
  #ifdef mxNever
320
    stopTime(&gxMarkTime);
321
    startTime(&gxSweepSlotTime);
322
  #endif
323
7.62k
    aCount = 0;
324
7.62k
    freeSlot = C_NULL;
325
7.62k
    aSlot = the->firstHeap;
326
26.8k
    while (aSlot) {
327
19.2k
      bSlot = aSlot + 1;
328
19.2k
      cSlot = aSlot->value.reference;
329
629M
      while (bSlot < cSlot) {
330
629M
        if (bSlot->flag & XS_MARK_FLAG) {
331
473M
          bSlot->flag &= ~XS_MARK_FLAG; 
332
          
333
473M
          if (bSlot->kind == XS_REFERENCE_KIND)
334
97.5M
            mxCheck(the, bSlot->value.reference->kind == XS_INSTANCE_KIND);
335
          
336
473M
          aCount++;
337
473M
        }
338
156M
        else {
339
156M
          if (bSlot->kind == XS_HOST_KIND) {
340
432
            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
432
            else if (bSlot->value.host.variant.destructor)
345
0
              (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
346
432
          }
347
        #if mxInstrument
348
          if ((bSlot->kind == XS_MODULE_KIND) && (bSlot->ID == XS_MODULE_BEHAVIOR))
349
            the->loadedModulesCount--;
350
        #endif
351
156M
          bSlot->kind = XS_UNDEFINED_KIND;
352
156M
          bSlot->next = freeSlot;
353
        #if mxPoisonSlots
354
          ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
355
        #endif
356
156M
          freeSlot = bSlot;
357
156M
        }
358
629M
        bSlot++;
359
629M
      }
360
19.2k
      aSlot = aSlot->next;
361
19.2k
    }
362
7.62k
    the->currentHeapCount = aCount;
363
7.62k
    the->freeHeap = freeSlot;
364
  #ifdef mxNever
365
    stopTime(&gxSweepSlotTime);
366
  #endif
367
7.62k
  }
368
  
369
10.7k
  aSlot = the->stack;
370
49.2M
  while (aSlot < the->stackTop) {
371
49.2M
    aSlot->flag &= ~XS_MARK_FLAG; 
372
49.2M
    aSlot++;
373
49.2M
  }
374
  
375
10.7k
  the->collectFlag &= ~(XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
376
  
377
10.7k
  if (theFlag & XS_COMPACT_FLAG) {
378
3.16k
    if ((the->maximumChunksSize - the->currentChunksSize) < the->minimumChunksSize)
379
1.33k
      the->collectFlag |= XS_TRASHING_CHUNKS_FLAG;
380
1.83k
    else
381
1.83k
      the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
382
3.16k
  }
383
  
384
10.7k
  if ((the->maximumHeapCount - the->currentHeapCount) < the->minimumHeapCount)
385
10.1k
    the->collectFlag |= XS_TRASHING_SLOTS_FLAG;
386
645
  else
387
645
    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
10.7k
#if defined(mxInstrument) || defined(mxProfile)
422
10.7k
  fxCheckProfiler(the, C_NULL);
423
10.7k
#endif
424
10.7k
}
425
426
txSlot* fxDuplicateSlot(txMachine* the, txSlot* theSlot)
427
0
{
428
0
  txSlot* result;
429
  
430
0
  result = fxNewSlot(the);
431
0
  result->ID = theSlot->ID;
432
0
  result->kind = theSlot->kind;
433
0
  result->flag = theSlot->flag & ~XS_MARK_FLAG;
434
0
  result->value = theSlot->value;
435
0
  return result;
436
0
}
437
438
void* fxFindChunk(txMachine* the, txSize size, txBoolean *once)
439
14.6M
{
440
14.6M
  txBlock* block;
441
14.6M
  txChunk* chunk;
442
14.6M
#if mxStress
443
14.6M
  if (fxShouldStress()) {
444
0
    if (*once) {
445
0
      fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
446
0
      *once = 0;
447
0
    }
448
0
  }
449
14.6M
#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
14.7M
again:
465
14.7M
  block = the->firstBlock;
466
17.4M
  while (block) {
467
17.4M
    if ((block->current + size) <= block->limit) {
468
14.6M
      chunk = (txChunk*)(block->current);
469
14.6M
      block->current += size;
470
14.6M
      chunk->temporary = block->current;
471
14.6M
      return chunk;
472
14.6M
    }
473
2.73M
    block = block->nextBlock;
474
2.73M
  }
475
3.44k
  if (*once) {
476
3.16k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0), isThrashing;
477
3.16k
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
478
3.16k
    isThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0);
479
3.16k
    *once = 0;
480
3.16k
    if (wasThrashing && isThrashing)
481
389
      return C_NULL;
482
2.77k
    goto again;
483
3.16k
  }
484
283
  return C_NULL;
485
3.44k
}
486
487
txSlot* fxFindKey(txMachine* the)
488
3.68M
{
489
3.68M
#if mxKeysGarbageCollection
490
3.68M
  txBoolean once = 1;
491
3.68M
#endif
492
3.68M
  txID id;
493
3.68M
  txSlot* result;
494
3.68M
more:
495
3.68M
  id = the->keyIndex;
496
3.68M
  if (id < the->keyCount) {
497
3.63M
    result = fxNewSlot(the);
498
3.63M
    result->ID = id;
499
3.63M
    the->keyArray[id - the->keyOffset] = result;
500
3.63M
    the->keyIndex++;
501
3.63M
    return result;
502
3.63M
  }
503
45.0k
#if mxKeysGarbageCollection
504
51.0k
again:
505
51.0k
  result = the->keyholeList;
506
51.0k
  if (result) {
507
44.9k
    the->keyholeCount--;
508
44.9k
    the->keyholeList = result->next;
509
44.9k
    result->next = C_NULL;
510
44.9k
    return result;
511
44.9k
  }
512
6.16k
  if (once) {
513
6.05k
    fxCollect(the, XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
514
6.05k
    once = 0;
515
6.05k
    goto again;
516
6.05k
  }
517
113
#endif
518
113
  else {
519
113
    fxGrowKeys(the, 1);
520
113
    goto more;
521
113
  }
522
0
  return C_NULL;
523
6.16k
}
524
525
void fxFree(txMachine* the) 
526
6.57k
{
527
6.57k
  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
6.57k
  fxFreeStringInfoCache(the);
536
537
6.57k
  if (the->symbolTable)
538
6.57k
    c_free_uint32(the->symbolTable);
539
6.57k
  the->symbolTable = C_NULL;
540
6.57k
  if (the->nameTable)
541
6.57k
    c_free_uint32(the->nameTable);
542
6.57k
  the->nameTable = C_NULL;
543
6.57k
  if (the->keyArray)
544
6.57k
    c_free_uint32(the->keyArray);
545
6.57k
  the->keyArray = C_NULL;
546
  
547
14.0k
  while (the->firstHeap) {
548
7.42k
    aHeap = the->firstHeap;
549
7.42k
    the->firstHeap = aHeap->next;
550
7.42k
    fxFreeSlots(the, aHeap);
551
7.42k
  }
552
6.57k
  the->firstHeap = C_NULL;
553
554
6.57k
  if (the->stackBottom)
555
6.57k
    fxFreeSlots(the, the->stackBottom);
556
6.57k
  the->stackBottom = C_NULL;
557
6.57k
  the->stackTop = C_NULL;
558
6.57k
  the->stackIntrinsics = C_NULL;
559
6.57k
  the->stackPrototypes = C_NULL;
560
6.57k
  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
6.57k
  {
574
6.57k
    txBlock* aBlock;
575
13.8k
    while (the->firstBlock) {
576
7.24k
      aBlock = the->firstBlock;
577
7.24k
      the->firstBlock = aBlock->nextBlock;
578
7.24k
      fxFreeChunks(the, aBlock);
579
7.24k
    }
580
6.57k
    the->firstBlock = C_NULL;
581
6.57k
  }
582
6.57k
#endif
583
584
6.57k
#ifdef mxDebug
585
6.57k
  if (the->pathValue)
586
6.57k
    c_free(the->pathValue);
587
6.57k
  the->pathValue = C_NULL;
588
6.57k
#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
6.57k
}
605
606
void* fxGrowChunk(txMachine* the, txSize size) 
607
672
{
608
672
  txBlock* block = fxGrowChunks(the, size);
609
672
  txChunk* chunk = C_NULL;
610
672
  if (block) {
611
672
    chunk = (txChunk*)(block->current);
612
672
    block->current += size;
613
672
    chunk->temporary = block->current;
614
672
  }
615
672
  return chunk;
616
672
}
617
618
void* fxGrowChunks(txMachine* the, txSize size) 
619
7.24k
{
620
7.24k
  txByte* buffer;
621
7.24k
  txBlock* block = C_NULL;
622
623
7.24k
  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
7.24k
  if ((the->firstBlock != C_NULL) && (!(the->collectFlag & XS_SKIPPED_COLLECT_FLAG))) {
629
672
    txSize modulo = size % (the->minimumChunksSize ? the->minimumChunksSize : 16);
630
672
    if (modulo)
631
669
      size = fxAddChunkSizes(the, size, the->minimumChunksSize - modulo);
632
672
  }
633
7.24k
  size = fxAddChunkSizes(the, size, sizeof(txBlock));
634
7.24k
  buffer = fxAllocateChunks(the, size);
635
7.24k
  if (buffer) {
636
7.24k
  #ifdef mxSnapshot
637
7.24k
    c_memset(buffer, 0, size);
638
7.24k
  #endif
639
7.24k
    if ((the->firstBlock != C_NULL) && (the->firstBlock->limit == buffer)) {
640
0
      the->firstBlock->limit += size;
641
0
      block = the->firstBlock;
642
0
    }
643
7.24k
    else {
644
7.24k
      block = (txBlock*)buffer;
645
7.24k
      block->nextBlock = the->firstBlock;
646
7.24k
      block->current = buffer + sizeof(txBlock);
647
7.24k
      block->limit = buffer + size;
648
7.24k
      block->temporary = C_NULL;
649
7.24k
      the->firstBlock = block;
650
7.24k
      size -= sizeof(txBlock);
651
7.24k
    }
652
7.24k
    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
7.24k
  }
658
7.24k
  the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
659
7.24k
  return block;
660
7.24k
}
661
662
void fxGrowKeys(txMachine* the, txID theCount) 
663
113
{
664
113
  if (the->keyDelta > 0) {
665
113
    txID keyDelta = (theCount > the->keyDelta) ? theCount : the->keyDelta;
666
113
    txID keyCount = (the->keyCount + keyDelta) - the->keyOffset;
667
113
    txSlot** keyArray = c_realloc(the->keyArray, keyCount * sizeof(txSlot*));
668
113
    if (keyArray == C_NULL)
669
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
670
113
    the->keyArray = keyArray;
671
113
    the->keyCount = keyCount + the->keyOffset;
672
113
  }
673
0
  else 
674
0
    fxAbort(the, XS_NO_MORE_KEYS_EXIT);
675
113
}
676
677
void fxGrowSlots(txMachine* the, txSize theCount) 
678
7.42k
{
679
7.42k
  txSlot* aHeap;
680
7.42k
  txSlot* aSlot;
681
682
7.42k
  aHeap = fxAllocateSlots(the, theCount);
683
7.42k
  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
7.42k
  if ((void *)-1 == aHeap)
688
0
    return;
689
    
690
7.42k
  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
7.42k
  else {
706
7.42k
    the->maximumHeapCount += theCount - 1;
707
7.42k
    aHeap->next = the->firstHeap;
708
7.42k
    aHeap->ID = 0;
709
7.42k
    aHeap->flag = 0;
710
7.42k
    aHeap->kind = 0;
711
7.42k
    aHeap->value.reference = aHeap + theCount;
712
7.42k
    theCount -= 2;
713
7.42k
    the->firstHeap = aHeap;
714
7.42k
    aSlot = aHeap + 1;
715
7.42k
  }
716
243M
    while (theCount--) {
717
243M
    txSlot* next = aSlot + 1;
718
243M
    aSlot->next = next;
719
243M
    aSlot->flag = XS_NO_FLAG;
720
243M
    aSlot->kind = XS_UNDEFINED_KIND;
721
  #if mxPoisonSlots
722
    ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
723
  #endif
724
243M
        aSlot = next;
725
243M
    }
726
7.42k
  aSlot->next = the->freeHeap;
727
7.42k
  aSlot->flag = XS_NO_FLAG;
728
7.42k
  aSlot->kind = XS_UNDEFINED_KIND;
729
#if mxPoisonSlots
730
  ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
731
#endif
732
7.42k
  the->freeHeap = aHeap + 1;
733
7.42k
  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
7.42k
}
741
742
void fxMark(txMachine* the, void (*theMarker)(txMachine*, txSlot*))
743
10.7k
{
744
10.7k
  txSlot** p;
745
10.7k
  txSlot** q;
746
10.7k
  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
10.7k
  slot = the->stackTop;
761
49.2M
  while (slot > the->stack) {
762
49.2M
        slot--;
763
49.2M
    (*theMarker)(the, slot);
764
49.2M
  }
765
10.7k
  slot = the->cRoot;
766
10.7k
  while (slot) {
767
0
    (*theMarker)(the, slot);
768
0
    slot = slot->next;
769
0
  }
770
  
771
10.7k
#if mxKeysGarbageCollection
772
10.7k
  if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
773
6.05k
    txInteger deletions = 0;
774
6.05k
    p = the->keyArray;
775
6.05k
    q = p + the->keyIndex - the->keyOffset;
776
6.20M
    while (p < q) {
777
6.19M
      slot = *p++;
778
6.19M
      if (!(slot->flag & XS_MARK_FLAG)) {
779
477k
        if (slot->flag & XS_DONT_DELETE_FLAG)
780
429k
          slot->flag |= XS_MARK_FLAG;
781
47.9k
        else if (slot->flag & XS_DONT_ENUM_FLAG)
782
47.9k
          deletions++;
783
477k
      }
784
6.19M
    }
785
  
786
  //  fprintf(stderr, "\n### KEYS GC %d", deletions);
787
6.05k
    p = the->nameTable;
788
6.05k
    q = the->nameTable + the->nameModulo;
789
9.93M
    while ((p < q) && deletions) {
790
9.93M
      txSlot** address = p;
791
15.0M
      while (((slot = *address)) && deletions) {
792
5.10M
        if (slot->flag & XS_MARK_FLAG)
793
5.05M
          address = &(slot->next);
794
47.9k
        else {
795
47.9k
          *address = slot->next;
796
47.9k
          deletions--;
797
47.9k
        }
798
5.10M
      }
799
9.93M
      p++;
800
9.93M
    }
801
  //  fprintf(stderr, " => %d", deletions);
802
  
803
6.05k
    the->keyholeCount = 0;
804
6.05k
    the->keyholeList = C_NULL;
805
6.05k
    p = the->keyArray;
806
6.05k
    q = p + the->keyIndex - the->keyOffset;
807
6.20M
    while (p < q) {
808
6.19M
      slot = *p;
809
6.19M
      if (slot->flag & XS_MARK_FLAG)
810
6.14M
        (*theMarker)(the, slot);
811
47.9k
      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
47.9k
        slot->flag = XS_INTERNAL_FLAG | XS_MARK_FLAG;
817
47.9k
        slot->next = the->keyholeList;
818
47.9k
        slot->kind = XS_UNDEFINED_KIND;
819
47.9k
        the->keyholeCount++;
820
47.9k
        the->keyholeList = slot;
821
47.9k
      }
822
6.19M
      p++;
823
6.19M
    }
824
  //  fprintf(stderr, "\n");
825
6.05k
  }
826
4.73k
  else
827
4.73k
#endif
828
4.73k
  {
829
4.73k
    p = the->keyArray;
830
4.73k
    q = p + the->keyIndex - the->keyOffset;
831
2.78M
    while (p < q) {
832
2.77M
      slot = *p++;
833
2.77M
      slot->flag |= XS_MARK_FLAG;
834
2.77M
      (*theMarker)(the, slot);
835
2.77M
    }
836
4.73k
  }
837
10.7k
}
838
839
#if mxKeysGarbageCollection
840
void fxMarkID(txMachine* the, txID id)
841
52.6M
{
842
52.6M
  txSlot* slot;
843
52.6M
  if (id == XS_NO_ID)
844
17.2M
    return;
845
35.3M
  if (id < the->keyOffset)
846
0
    return;
847
35.3M
  id -= the->keyOffset;
848
35.3M
  slot = the->keyArray[id];
849
35.3M
  slot->flag |= XS_MARK_FLAG;
850
35.3M
}
851
#endif
852
853
void fxMarkFinalizationRegistry(txMachine* the, txSlot* registry) 
854
0
{
855
0
  txSlot* slot = registry->value.finalizationRegistry.callback->next;
856
0
  txSlot* instance;
857
0
  while (slot) {
858
0
    slot = slot->next;
859
0
    if (slot) {
860
0
      instance = slot->value.finalizationCell.target;
861
0
      if (instance && !(instance->flag & XS_MARK_FLAG)) {
862
0
        slot->value.finalizationCell.target = C_NULL;
863
0
        registry->value.finalizationRegistry.flags |= XS_FINALIZATION_REGISTRY_CHANGED;
864
0
      }
865
0
      instance = slot->value.finalizationCell.token;
866
0
      if (instance && !(instance->flag & XS_MARK_FLAG))
867
0
        slot->value.finalizationCell.token = C_NULL;
868
0
      slot = slot->next;
869
0
    }
870
0
  }
871
0
}
872
873
void fxMarkInstance(txMachine* the, txSlot* theCurrent, void (*theMarker)(txMachine*, txSlot*))
874
131M
{
875
131M
  txSlot* aProperty;
876
131M
  txSlot* aTemporary;
877
878
131M
  mxCheck(the, theCurrent->kind == XS_INSTANCE_KIND);
879
131M
  aProperty = theCurrent;
880
131M
  theCurrent->value.instance.garbage = C_NULL;
881
841M
  for (;;) {
882
841M
    if (aProperty) {
883
607M
      if (!(aProperty->flag & XS_MARK_FLAG)) {
884
607M
        aProperty->flag |= XS_MARK_FLAG;
885
607M
        switch (aProperty->kind) {
886
233M
        case XS_INSTANCE_KIND:
887
233M
          aTemporary = aProperty->value.instance.prototype;
888
233M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
889
43.1k
            aProperty->value.instance.prototype = theCurrent;
890
43.1k
            theCurrent = aTemporary;
891
43.1k
            theCurrent->value.instance.garbage = aProperty;
892
43.1k
            aProperty = theCurrent;
893
43.1k
          }
894
233M
          else
895
233M
            aProperty = aProperty->next;
896
233M
          break;
897
102M
        case XS_REFERENCE_KIND:
898
102M
        #if mxKeysGarbageCollection
899
102M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
900
21.5M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
901
21.5M
              fxMarkID(the, aProperty->ID);
902
21.5M
          }
903
102M
        #endif
904
102M
          aTemporary = aProperty->value.reference;
905
102M
          if (!(aTemporary->flag & XS_MARK_FLAG)) {
906
101M
            aProperty->value.reference = theCurrent;
907
101M
            theCurrent = aTemporary;
908
101M
            theCurrent->value.instance.garbage = aProperty;
909
101M
            aProperty = theCurrent;
910
101M
          }
911
798k
          else
912
798k
            aProperty = aProperty->next;
913
102M
          break;
914
          
915
0
        case XS_PROXY_KIND:
916
0
          aTemporary = aProperty->value.proxy.handler;
917
0
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
918
0
            aProperty->flag |= XS_INSPECTOR_FLAG;
919
0
            aProperty->value.proxy.handler = theCurrent;
920
0
            theCurrent = aTemporary;
921
0
            theCurrent->value.instance.garbage = aProperty;
922
0
            aProperty = theCurrent;
923
0
          }
924
0
          else {
925
0
            aTemporary = aProperty->value.proxy.target;
926
0
            if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
927
0
              aProperty->value.proxy.target = theCurrent;
928
0
              theCurrent = aTemporary;
929
0
              theCurrent->value.instance.garbage = aProperty;
930
0
              aProperty = theCurrent;
931
0
            }
932
0
            else
933
0
              aProperty = aProperty->next;
934
0
          }
935
0
          break;
936
          
937
0
        case XS_CLOSURE_KIND:
938
0
        #if mxKeysGarbageCollection
939
0
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
940
0
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
941
0
              fxMarkID(the, aProperty->ID);
942
0
          }
943
0
        #endif
944
0
          aTemporary = aProperty->value.closure;
945
0
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
946
0
            aTemporary->flag |= XS_MARK_FLAG; 
947
0
            if (aTemporary->kind == XS_REFERENCE_KIND) {
948
0
              aTemporary = aTemporary->value.reference;
949
0
              if (!(aTemporary->flag & XS_MARK_FLAG)) {
950
0
                aProperty->value.closure->value.reference = theCurrent;
951
0
                theCurrent = aTemporary;
952
0
                theCurrent->value.instance.garbage = aProperty;
953
0
                aProperty = theCurrent;
954
            
955
0
              }
956
0
            }
957
0
            else {
958
0
              (*theMarker)(the, aTemporary);
959
0
              aProperty = aProperty->next;
960
0
            }
961
0
          }
962
0
          else
963
0
            aProperty = aProperty->next;
964
0
          break;
965
          
966
6.29M
        case XS_CALLBACK_KIND:
967
6.29M
        case XS_CODE_KIND:
968
6.29M
        #if mxKeysGarbageCollection
969
6.29M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
970
3.53M
            fxMarkID(the, aProperty->ID);
971
6.29M
        #endif
972
6.29M
          (*theMarker)(the, aProperty);
973
6.29M
          aProperty = aProperty->next;
974
6.29M
          break;  
975
        
976
265M
        default:
977
265M
        #if mxKeysGarbageCollection
978
265M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
979
46.7M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
980
15.2M
              fxMarkID(the, aProperty->ID);
981
46.7M
          }
982
265M
        #endif
983
265M
          (*theMarker)(the, aProperty);
984
265M
          aProperty = aProperty->next;
985
265M
          break; 
986
607M
        }
987
607M
      }
988
0
      else
989
0
        aProperty = aProperty->next;
990
607M
    }
991
233M
    else if (theCurrent->value.instance.garbage) {
992
101M
      aProperty = theCurrent->value.instance.garbage;
993
101M
      theCurrent->value.instance.garbage = C_NULL;
994
101M
      switch (aProperty->kind) {
995
43.1k
      case XS_INSTANCE_KIND:
996
43.1k
        aTemporary = aProperty->value.instance.prototype;
997
43.1k
        aProperty->value.instance.prototype = theCurrent;
998
43.1k
        theCurrent = aTemporary;
999
43.1k
        aProperty = aProperty->next;
1000
43.1k
        break;
1001
101M
      case XS_REFERENCE_KIND:
1002
101M
        aTemporary = aProperty->value.reference;
1003
101M
        aProperty->value.reference = theCurrent;
1004
101M
        theCurrent = aTemporary;
1005
101M
        aProperty = aProperty->next;
1006
101M
        break;
1007
0
      case XS_PROXY_KIND:
1008
0
        if (aProperty->flag & XS_INSPECTOR_FLAG) {
1009
0
          aProperty->flag &= ~XS_INSPECTOR_FLAG;
1010
0
          aTemporary = aProperty->value.proxy.handler;
1011
0
          aProperty->value.proxy.handler = theCurrent;
1012
0
          theCurrent = aTemporary;
1013
          
1014
0
          aTemporary = aProperty->value.proxy.target;
1015
0
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
1016
0
            aProperty->value.proxy.target = theCurrent;
1017
0
            theCurrent = aTemporary;
1018
0
            theCurrent->value.instance.garbage = aProperty;
1019
0
            aProperty = theCurrent;
1020
0
          }
1021
0
          else {
1022
0
            aProperty = aProperty->next;
1023
0
          }
1024
0
        }
1025
0
        else {
1026
0
          aTemporary = aProperty->value.proxy.target;
1027
0
          aProperty->value.proxy.target = theCurrent;
1028
0
          theCurrent = aTemporary;
1029
0
          aProperty = aProperty->next;
1030
0
        }
1031
0
        break;
1032
0
      case XS_CLOSURE_KIND:
1033
0
        aTemporary = aProperty->value.closure->value.reference;
1034
0
        aProperty->value.closure->value.reference = theCurrent;
1035
0
        theCurrent = aTemporary;
1036
0
        aProperty = aProperty->next;
1037
0
        break;
1038
101M
      }
1039
101M
    }
1040
131M
    else
1041
131M
      break;
1042
841M
  }
1043
131M
}
1044
1045
void fxMarkReference(txMachine* the, txSlot* theSlot)
1046
313M
{
1047
313M
  txSlot* aSlot;
1048
313M
  switch (theSlot->kind) {
1049
75.1M
  case XS_REFERENCE_KIND:
1050
75.1M
    aSlot = theSlot->value.reference;
1051
75.1M
    if (!(aSlot->flag & XS_MARK_FLAG))
1052
74.2M
      fxMarkInstance(the, aSlot, fxMarkReference);
1053
75.1M
    break;
1054
0
  case XS_CLOSURE_KIND:
1055
0
    aSlot = theSlot->value.closure;
1056
0
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG))) {
1057
0
      aSlot->flag |= XS_MARK_FLAG; 
1058
0
      fxMarkReference(the, aSlot);
1059
0
    }
1060
0
    break;
1061
0
  case XS_INSTANCE_KIND:
1062
0
    if (!(theSlot->flag & XS_MARK_FLAG))
1063
0
      fxMarkInstance(the, theSlot, fxMarkReference);
1064
0
    break;
1065
441k
  case XS_ACCESSOR_KIND:
1066
441k
    aSlot = theSlot->value.accessor.getter;
1067
441k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1068
434k
      fxCheckCStack(the);
1069
434k
      fxMarkInstance(the, aSlot, fxMarkReference);
1070
434k
    }
1071
441k
    aSlot = theSlot->value.accessor.setter;
1072
441k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1073
60.9k
      fxCheckCStack(the);
1074
60.9k
      fxMarkInstance(the, aSlot, fxMarkReference);
1075
60.9k
    }
1076
441k
    break;
1077
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1078
0
  case XS_ARGUMENTS_STRICT_KIND:
1079
153M
  case XS_ARRAY_KIND:
1080
153M
  case XS_STACK_KIND:
1081
153M
    fxCheckCStack(the);
1082
153M
    if ((aSlot = theSlot->value.array.address)) {
1083
45.4M
      txIndex aLength = (((txChunk*)(((txByte*)aSlot) - sizeof(txChunk)))->size) / sizeof(txSlot);
1084
45.4M
      if (aLength > theSlot->value.array.length)
1085
0
        aLength = theSlot->value.array.length;
1086
112M
      while (aLength) {
1087
67.4M
        fxMarkReference(the, aSlot);
1088
67.4M
        aSlot++;
1089
67.4M
        aLength--;
1090
67.4M
      }
1091
45.4M
    }
1092
153M
    break;
1093
4.45M
  case XS_CALLBACK_KIND:
1094
4.45M
    aSlot = theSlot->value.callback.closures;
1095
4.45M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1096
0
      fxCheckCStack(the);
1097
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1098
0
    }
1099
4.45M
    break;
1100
0
  case XS_CODE_KIND:
1101
    // continue
1102
15.2k
  case XS_CODE_X_KIND:
1103
15.2k
    aSlot = theSlot->value.code.closures;
1104
15.2k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1105
0
      fxCheckCStack(the);
1106
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1107
0
    }
1108
15.2k
    break;
1109
4.45M
  case XS_HOME_KIND:
1110
4.45M
    aSlot = theSlot->value.home.object;
1111
4.45M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1112
0
      fxCheckCStack(the);
1113
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1114
0
    }
1115
4.45M
    aSlot = theSlot->value.home.module;
1116
4.45M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1117
0
      fxCheckCStack(the);
1118
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1119
0
    }
1120
4.45M
    break;
1121
0
  case XS_MODULE_KIND:
1122
7.62k
  case XS_PROGRAM_KIND:
1123
7.62k
#if mxKeysGarbageCollection
1124
7.62k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1125
6.05k
      fxMarkID(the, theSlot->value.module.id);
1126
7.62k
#endif
1127
7.62k
    aSlot = theSlot->value.module.realm;
1128
7.62k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1129
7.62k
      fxCheckCStack(the);
1130
7.62k
      fxMarkInstance(the, aSlot, fxMarkReference);
1131
7.62k
    }
1132
7.62k
    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
7.62k
  case XS_HOST_KIND:
1144
7.62k
    if (theSlot->value.host.data) {
1145
0
      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
0
    }
1148
7.62k
    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
9
  case XS_ERROR_KIND:
1164
9
    aSlot = theSlot->value.error.info;
1165
9
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1166
4
      fxMarkInstance(the, aSlot, fxMarkReference);
1167
9
    break;
1168
0
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1169
0
  case XS_DISPOSABLE_STACK_KIND:
1170
22.8k
  case XS_LIST_KIND:
1171
22.8k
    fxCheckCStack(the);
1172
22.8k
    aSlot = theSlot->value.list.first;
1173
22.8k
    while (aSlot) {
1174
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1175
0
        aSlot->flag |= XS_MARK_FLAG;
1176
0
        fxMarkReference(the, aSlot);
1177
0
      }
1178
0
      aSlot = aSlot->next;
1179
0
    }
1180
22.8k
    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
0
  case XS_MAP_KIND:
1196
0
  case XS_SET_KIND:
1197
0
    {
1198
0
      txSlot** anAddress = theSlot->value.table.address;
1199
0
      txInteger aLength = theSlot->value.table.length;
1200
0
      while (aLength) {
1201
0
        aSlot = *anAddress;
1202
0
        while (aSlot) {
1203
0
          aSlot->flag |= XS_MARK_FLAG; 
1204
0
          aSlot = aSlot->next;
1205
0
        }
1206
0
        anAddress++;
1207
0
        aLength--;
1208
0
      }
1209
0
    }
1210
0
    break;
1211
0
  case XS_WEAK_MAP_KIND:
1212
0
  case XS_WEAK_SET_KIND:
1213
0
    aSlot = theSlot->value.weakList.first;
1214
0
    while (aSlot) {
1215
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1216
0
        aSlot->flag |= XS_MARK_FLAG;
1217
0
        fxMarkReference(the, aSlot);
1218
0
      }
1219
0
      aSlot = aSlot->next;
1220
0
    }
1221
0
    break;
1222
0
  case XS_WEAK_ENTRY_KIND:
1223
0
    aSlot = theSlot->value.weakEntry.check;
1224
0
    if (aSlot->flag & XS_MARK_FLAG) {
1225
0
      aSlot = theSlot->value.weakEntry.value;
1226
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1227
0
        aSlot->flag |= XS_MARK_FLAG; 
1228
0
        fxMarkReference(the, aSlot);
1229
0
      }
1230
0
    }
1231
0
    break;
1232
0
  case XS_WEAK_REF_KIND:
1233
0
    aSlot = theSlot->value.weakRef.target;
1234
0
    if (aSlot) {
1235
0
  #ifdef mxSnapshot
1236
0
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1237
0
        fxMarkReference(the, aSlot);
1238
0
      }
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
0
    }
1248
0
    break;
1249
0
  case XS_FINALIZATION_REGISTRY_KIND:
1250
0
    aSlot = theSlot->value.finalizationRegistry.callback;
1251
0
    if (aSlot) {
1252
0
      fxCheckCStack(the);
1253
0
      aSlot->flag |= XS_MARK_FLAG;
1254
0
      fxMarkReference(the, aSlot);
1255
0
      aSlot = aSlot->next;
1256
0
      while (aSlot) {
1257
0
        aSlot->flag |= XS_MARK_FLAG;
1258
0
        fxMarkReference(the, aSlot); // holdings
1259
0
        aSlot = aSlot->next;
1260
0
        if (aSlot) {
1261
0
          aSlot->flag |= XS_MARK_FLAG;
1262
          // weak target and token
1263
0
          aSlot = aSlot->next;
1264
0
        }
1265
0
      }
1266
0
    }
1267
0
    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
228k
  case XS_SYMBOL_KIND:
1276
228k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1277
181k
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1278
90.7k
        fxMarkID(the, theSlot->value.symbol);
1279
181k
    }
1280
228k
    break; 
1281
12.2M
  case XS_AT_KIND:
1282
12.2M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1283
12.1M
      fxMarkID(the, theSlot->value.at.id);
1284
12.2M
    break; 
1285
313M
#endif
1286
313M
  }
1287
313M
}
1288
1289
void fxMarkValue(txMachine* the, txSlot* theSlot)
1290
159M
{
1291
159M
#define mxMarkChunk(_THE_DATA) \
1292
159M
  ((txChunk*)(((txByte*)_THE_DATA) - sizeof(txChunk)))->size |= mxChunkFlag
1293
1294
159M
  txSlot* aSlot;
1295
159M
  switch (theSlot->kind) {
1296
3.89M
  case XS_STRING_KIND:
1297
3.89M
    mxMarkChunk(theSlot->value.string);
1298
3.89M
    break;
1299
0
  case XS_BIGINT_KIND:
1300
0
    mxMarkChunk(theSlot->value.bigint.data);
1301
0
    break;
1302
57.3M
  case XS_REFERENCE_KIND:
1303
57.3M
    aSlot = theSlot->value.reference;
1304
57.3M
    if (!(aSlot->flag & XS_MARK_FLAG))
1305
56.9M
      fxMarkInstance(the, aSlot, fxMarkValue);
1306
57.3M
    break;
1307
0
  case XS_CLOSURE_KIND:
1308
0
    aSlot = theSlot->value.closure;
1309
0
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG))) {
1310
0
      aSlot->flag |= XS_MARK_FLAG; 
1311
0
      fxMarkValue(the, aSlot);
1312
0
    }
1313
0
    break;
1314
0
  case XS_INSTANCE_KIND:
1315
0
    if (!(theSlot->flag & XS_MARK_FLAG))
1316
0
      fxMarkInstance(the, theSlot, fxMarkValue);
1317
0
    break;
1318
    
1319
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1320
0
  case XS_ARGUMENTS_STRICT_KIND:
1321
58.7M
  case XS_ARRAY_KIND:
1322
58.7M
  case XS_STACK_KIND:
1323
58.7M
    fxCheckCStack(the);
1324
58.7M
    if ((aSlot = theSlot->value.array.address)) {
1325
50.4M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
1326
50.4M
      if (!(chunk->size & mxChunkFlag)) {
1327
50.4M
        txIndex aLength = chunk->size / sizeof(txSlot);
1328
50.4M
        if (aLength > theSlot->value.array.length)
1329
0
          aLength = theSlot->value.array.length;
1330
125M
        while (aLength) {
1331
75.5M
          fxMarkValue(the, aSlot);
1332
75.5M
          aSlot++;
1333
75.5M
          aLength--;
1334
75.5M
        }
1335
50.4M
        mxMarkChunk(theSlot->value.array.address);
1336
50.4M
      }
1337
50.4M
    }
1338
58.7M
    break;
1339
131
  case XS_ARRAY_BUFFER_KIND:
1340
131
    if (theSlot->value.arrayBuffer.address)
1341
103
      mxMarkChunk(theSlot->value.arrayBuffer.address);
1342
131
    break;
1343
1.84M
  case XS_CALLBACK_KIND:
1344
1.84M
    aSlot = theSlot->value.callback.closures;
1345
1.84M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1346
0
      fxCheckCStack(the);
1347
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1348
0
    }
1349
1.84M
    break;
1350
0
  case XS_CODE_KIND:
1351
0
    mxMarkChunk(theSlot->value.code.address);
1352
    /* continue */
1353
6.32k
  case XS_CODE_X_KIND:
1354
6.32k
    aSlot = theSlot->value.code.closures;
1355
6.32k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1356
0
      fxCheckCStack(the);
1357
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1358
0
    }
1359
6.32k
    break;
1360
3.16k
  case XS_GLOBAL_KIND:
1361
3.16k
    mxMarkChunk(theSlot->value.table.address);
1362
3.16k
    break;
1363
3.26k
  case XS_HOST_KIND:
1364
3.26k
    if (theSlot->value.host.data) {
1365
101
      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
101
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
1368
101
        mxMarkChunk(theSlot->value.host.data);
1369
101
    }
1370
3.26k
    break;
1371
0
  case XS_IDS_KIND:
1372
0
    if (theSlot->value.IDs)
1373
0
      mxMarkChunk(theSlot->value.IDs);
1374
0
    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
0
  case XS_REGEXP_KIND:
1384
0
    if (theSlot->value.regexp.code)
1385
0
      mxMarkChunk(theSlot->value.regexp.code);
1386
0
    if (theSlot->value.regexp.data)
1387
0
      mxMarkChunk(theSlot->value.regexp.data);
1388
0
    break;
1389
    
1390
183k
  case XS_ACCESSOR_KIND:
1391
183k
    aSlot = theSlot->value.accessor.getter;
1392
183k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1393
180k
      fxCheckCStack(the);
1394
180k
      fxMarkInstance(the, aSlot, fxMarkValue);
1395
180k
    }
1396
183k
    aSlot = theSlot->value.accessor.setter;
1397
183k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1398
25.3k
      fxCheckCStack(the);
1399
25.3k
      fxMarkInstance(the, aSlot, fxMarkValue);
1400
25.3k
    }
1401
183k
    break;
1402
1.85M
  case XS_HOME_KIND:
1403
1.85M
    aSlot = theSlot->value.home.object;
1404
1.85M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1405
0
      fxCheckCStack(the);
1406
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1407
0
    }
1408
1.85M
    aSlot = theSlot->value.home.module;
1409
1.85M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1410
0
      fxCheckCStack(the);
1411
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1412
0
    }
1413
1.85M
    break;
1414
0
  case XS_MODULE_KIND:
1415
3.16k
  case XS_PROGRAM_KIND:
1416
3.16k
#if mxKeysGarbageCollection
1417
3.16k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1418
0
      fxMarkID(the, theSlot->value.module.id);
1419
3.16k
#endif
1420
3.16k
    aSlot = theSlot->value.module.realm;
1421
3.16k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1422
3.16k
      fxCheckCStack(the);
1423
3.16k
      fxMarkInstance(the, aSlot, fxMarkValue);
1424
3.16k
    }
1425
3.16k
    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
1.86M
  case XS_KEY_KIND:
1437
1.86M
    if (theSlot->value.key.string)
1438
1.86M
      mxMarkChunk(theSlot->value.key.string);
1439
1.86M
    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
53
  case XS_ERROR_KIND:
1447
53
    aSlot = theSlot->value.error.info;
1448
53
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1449
53
      fxMarkInstance(the, aSlot, fxMarkValue);
1450
53
    break;
1451
0
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1452
0
  case XS_DISPOSABLE_STACK_KIND:
1453
9.49k
  case XS_LIST_KIND:
1454
9.49k
    aSlot = theSlot->value.list.first;
1455
9.49k
    while (aSlot) {
1456
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1457
0
        aSlot->flag |= XS_MARK_FLAG;
1458
0
        fxMarkValue(the, aSlot);
1459
0
      }
1460
0
      aSlot = aSlot->next;
1461
0
    }
1462
9.49k
    break;
1463
    
1464
0
  case XS_PRIVATE_KIND:
1465
0
    fxCheckCStack(the);
1466
0
    aSlot = theSlot->value.private.check;
1467
0
    if (!(aSlot->flag & XS_MARK_FLAG))
1468
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1469
0
    aSlot = theSlot->value.private.first;
1470
0
    while (aSlot) {
1471
0
      aSlot->flag |= XS_MARK_FLAG;
1472
0
      fxMarkValue(the, aSlot);
1473
0
      aSlot = aSlot->next;
1474
0
    }
1475
0
    break;
1476
1477
0
  case XS_MAP_KIND:
1478
0
  case XS_SET_KIND:
1479
0
    {
1480
0
      txSlot** anAddress = theSlot->value.table.address;
1481
0
      txInteger aLength = theSlot->value.table.length;
1482
0
      while (aLength) {
1483
0
        aSlot = *anAddress;
1484
0
        while (aSlot) {
1485
0
          aSlot->flag |= XS_MARK_FLAG; 
1486
0
          aSlot = aSlot->next;
1487
0
        }
1488
0
        anAddress++;
1489
0
        aLength--;
1490
0
      }
1491
0
    }
1492
0
    mxMarkChunk(theSlot->value.table.address);
1493
0
    break;
1494
1495
0
  case XS_WEAK_MAP_KIND:
1496
0
  case XS_WEAK_SET_KIND:
1497
0
    aSlot = theSlot->value.weakList.first;
1498
0
    while (aSlot) {
1499
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1500
0
        aSlot->flag |= XS_MARK_FLAG;
1501
0
        fxMarkValue(the, aSlot);
1502
0
      }
1503
0
      aSlot = aSlot->next;
1504
0
    }
1505
0
    break;
1506
0
  case XS_WEAK_ENTRY_KIND:
1507
0
    aSlot = theSlot->value.weakEntry.check;
1508
0
    if (aSlot->flag & XS_MARK_FLAG) {
1509
0
      aSlot = theSlot->value.weakEntry.value;
1510
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1511
0
        aSlot->flag |= XS_MARK_FLAG; 
1512
0
        fxMarkValue(the, aSlot);
1513
0
      }
1514
0
    }
1515
0
    break;
1516
0
  case XS_WEAK_REF_KIND:
1517
0
    aSlot = theSlot->value.weakRef.target;
1518
0
    if (aSlot) {
1519
0
  #ifdef mxSnapshot
1520
0
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1521
0
        fxMarkValue(the, aSlot);
1522
0
      }
1523
0
      else {
1524
0
        theSlot->value.weakRef.link = the->firstWeakRefLink;
1525
0
        the->firstWeakRefLink = theSlot;
1526
0
      }
1527
  #else
1528
      theSlot->value.weakRef.link = the->firstWeakRefLink;
1529
      the->firstWeakRefLink = theSlot;
1530
  #endif
1531
0
    }
1532
0
    break;
1533
0
  case XS_FINALIZATION_REGISTRY_KIND:
1534
0
    aSlot = theSlot->value.finalizationRegistry.callback;
1535
0
    if (aSlot) {
1536
0
      aSlot->flag |= XS_MARK_FLAG;
1537
0
      fxMarkValue(the, aSlot);
1538
0
      aSlot = aSlot->next;
1539
0
      while (aSlot) {
1540
0
        aSlot->flag |= XS_MARK_FLAG;
1541
0
                fxCheckCStack(the);
1542
0
        fxMarkValue(the, aSlot); // holdings
1543
0
        aSlot = aSlot->next;
1544
0
        if (aSlot) {
1545
0
          aSlot->flag |= XS_MARK_FLAG;
1546
          // weak target and token
1547
0
          aSlot = aSlot->next;
1548
0
        }
1549
0
      }
1550
0
    }
1551
0
    break;
1552
    
1553
0
  case XS_HOST_INSPECTOR_KIND:
1554
0
    aSlot = theSlot->value.hostInspector.cache;
1555
0
    if (!(aSlot->flag & XS_MARK_FLAG))
1556
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1557
0
    break;  
1558
    
1559
0
#if mxKeysGarbageCollection
1560
94.9k
  case XS_SYMBOL_KIND:
1561
94.9k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1562
0
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1563
0
        fxMarkID(the, theSlot->value.symbol);
1564
0
    }
1565
94.9k
    break; 
1566
1.23M
  case XS_AT_KIND:
1567
1.23M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1568
0
      fxMarkID(the, theSlot->value.at.id);
1569
1.23M
    break; 
1570
159M
#endif
1571
159M
  }
1572
159M
}
1573
1574
void fxMarkWeakStuff(txMachine* the) 
1575
10.7k
{
1576
10.7k
  txSlot* slot;
1577
10.7k
  txSlot** address;
1578
1579
10.7k
  {
1580
10.7k
    txSlot* list;
1581
10.7k
    txSlot** listAddress = &the->firstWeakListLink;
1582
10.7k
    while ((list = *listAddress)) {
1583
0
      if (list->flag & XS_MARK_FLAG) {
1584
0
        txSlot* listEntry;
1585
0
        txSlot** listEntryAddress = &list->value.weakList.first;
1586
0
        while ((listEntry = *listEntryAddress)) {
1587
0
          txSlot* value = listEntry->value.weakEntry.value;
1588
0
          if ((value->flag & XS_MARK_FLAG) && (value->kind != XS_UNINITIALIZED_KIND)) {
1589
0
            listEntryAddress = &listEntry->next;
1590
0
          }
1591
0
          else {
1592
0
            listEntry->flag &= ~XS_MARK_FLAG;
1593
0
            *listEntryAddress = listEntry->next;
1594
0
          }
1595
0
        }
1596
0
        listAddress = &list->value.weakList.link;
1597
0
      }
1598
0
      else {
1599
0
        txSlot* listEntry = list->value.weakList.first;
1600
0
        while (listEntry) {
1601
0
          txSlot* key = listEntry->value.weakEntry.check;
1602
0
          if (key->flag & XS_MARK_FLAG) {
1603
0
            txSlot* keyEntry;
1604
0
            txSlot** keyEntryAddress = &key->next;
1605
0
            while ((keyEntry = *keyEntryAddress)) {
1606
0
              if (!(keyEntry->flag & XS_INTERNAL_FLAG))
1607
0
                break;
1608
0
              if ((keyEntry->kind == XS_WEAK_ENTRY_KIND) && (keyEntry->value.weakEntry.check == list)) {
1609
0
                keyEntry->flag &= ~XS_MARK_FLAG;
1610
0
                *keyEntryAddress = keyEntry->next;
1611
0
                break;
1612
0
              }
1613
0
              keyEntryAddress = &keyEntry->next;
1614
0
            }
1615
0
          }
1616
0
          listEntry = listEntry->next;
1617
0
        }
1618
0
        *listAddress = list->value.weakList.link;
1619
0
      }
1620
0
    }
1621
10.7k
  }
1622
10.7k
  address = &the->firstWeakRefLink;
1623
10.7k
  while ((slot = *address)) {
1624
0
    if (!(slot->value.weakRef.target->flag & XS_MARK_FLAG))
1625
0
      slot->value.weakRef.target = C_NULL;
1626
0
    *address = C_NULL;
1627
0
    address = &(slot->value.weakRef.link);
1628
0
  }
1629
  
1630
10.7k
  if (mxFinalizationRegistries.kind == XS_REFERENCE_KIND) {
1631
10.7k
    slot = mxFinalizationRegistries.value.reference->next;
1632
10.7k
    while (slot) {
1633
0
      fxMarkFinalizationRegistry(the, slot->value.closure);
1634
0
      slot = slot->next;
1635
0
    }
1636
10.7k
  }
1637
10.7k
}
1638
1639
txSize fxMultiplyChunkSizes(txMachine* the, txSize a, txSize b)
1640
6.62M
{
1641
6.62M
  txSize c;
1642
6.62M
#if __has_builtin(__builtin_mul_overflow)
1643
6.62M
  if (__builtin_mul_overflow(a, b, &c) || (c < 0)) {
1644
#else
1645
  txNumber C = (txNumber)a * (txNumber)b;
1646
  c = (txSize)C;
1647
  if ((C > (txNumber)0x7FFFFFFF) || (C < (txNumber)0)) {
1648
#endif
1649
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1650
0
  }
1651
6.62M
  return c;
1652
6.62M
}
1653
1654
void* fxNewChunk(txMachine* the, txSize size)
1655
14.6M
{
1656
14.6M
  txSize offset = size;
1657
14.6M
  txChunk* chunk;
1658
14.6M
  txBoolean once = 1;
1659
14.6M
  size = fxAdjustChunkSize(the, size);
1660
14.6M
  chunk = fxFindChunk(the, size, &once);
1661
14.6M
  if (!chunk) {
1662
672
    chunk = fxGrowChunk(the, size);
1663
672
  }
1664
14.6M
#ifdef mxMetering
1665
14.6M
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1666
14.6M
#endif
1667
14.6M
  return fxCheckChunk(the, chunk, size, offset);
1668
14.6M
}
1669
1670
void* fxNewGrowableChunk(txMachine* the, txSize size, txSize capacity)
1671
0
{
1672
#if mxNoChunks
1673
  return fxNewChunk(the, size);
1674
#else
1675
0
  txSize offset = size;
1676
0
  txChunk* chunk;
1677
0
  txBoolean once = 1;
1678
0
  size = fxAdjustChunkSize(the, size);
1679
0
  capacity = fxAdjustChunkSize(the, capacity);
1680
0
  chunk = fxFindChunk(the, capacity, &once);
1681
0
  if (!chunk) {
1682
0
    chunk = fxGrowChunk(the, capacity);
1683
0
    if (!chunk) {
1684
0
      chunk = fxFindChunk(the, size, &once);
1685
0
      if (!chunk) {
1686
0
        chunk = fxGrowChunk(the, size);
1687
0
      }
1688
0
    }
1689
0
  }
1690
0
#ifdef mxMetering
1691
0
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1692
0
#endif
1693
0
  return fxCheckChunk(the, chunk, size, offset);
1694
0
#endif
1695
0
}
1696
1697
txSlot* fxNewSlot(txMachine* the) 
1698
67.1M
{
1699
67.1M
  txSlot* aSlot;
1700
67.1M
  txBoolean once = 1, allocate;
1701
  
1702
67.1M
#if mxStress
1703
67.1M
  if (fxShouldStress()) {
1704
0
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
1705
0
    once = 0;
1706
0
  }
1707
67.1M
#endif
1708
67.1M
again:
1709
67.1M
  aSlot = the->freeHeap;
1710
67.1M
  if (aSlot) {
1711
67.1M
    the->freeHeap = aSlot->next;
1712
67.1M
    aSlot->next = C_NULL;
1713
67.1M
    aSlot->ID = XS_NO_ID;
1714
67.1M
    aSlot->flag = XS_NO_FLAG;
1715
67.1M
  #ifdef mxSnapshot
1716
67.1M
    #if mx32bitID
1717
67.1M
      aSlot->dummy = 0;
1718
    #elif INTPTR_MAX == INT64_MAX
1719
      aSlot->dummy = 0;
1720
    #endif
1721
67.1M
  #endif
1722
#if mxPoisonSlots
1723
    ASAN_UNPOISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
1724
#endif
1725
67.1M
    the->currentHeapCount++;
1726
67.1M
    if (the->peakHeapCount < the->currentHeapCount)
1727
60.6M
      the->peakHeapCount = the->currentHeapCount;
1728
67.1M
#ifdef mxMetering
1729
67.1M
    the->meterIndex += XS_SLOT_ALLOCATION_METERING;
1730
67.1M
#endif
1731
67.1M
    return aSlot;
1732
67.1M
  }
1733
1.65k
  if (once) {
1734
1.56k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0), isThrashing;
1735
1736
1.56k
    fxCollect(the, XS_ORGANIC_FLAG);
1737
1738
1.56k
    isThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0);
1739
1.56k
    allocate = wasThrashing && isThrashing;
1740
1741
1.56k
    once = 0;
1742
1.56k
  }
1743
84
  else
1744
84
    allocate = 1;
1745
1.65k
  if (allocate) {
1746
851
    if (!the->minimumHeapCount) {
1747
0
      fxReport(the, "# Slot allocation: failed in fixed size heap\n");
1748
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1749
0
    }
1750
851
    fxGrowSlots(the, !(the->collectFlag & XS_SKIPPED_COLLECT_FLAG) ? the->minimumHeapCount : 64);
1751
851
  }
1752
1.65k
  goto again;
1753
0
  return C_NULL;
1754
67.1M
}
1755
1756
void* fxRenewChunk(txMachine* the, void* theData, txSize size)
1757
6.69k
{
1758
#if mxNoChunks
1759
  txByte* aData = ((txByte*)theData) - sizeof(txChunk);
1760
  txChunk* aChunk = (txChunk*)aData;
1761
  size = fxAdjustChunkSize(the, size);
1762
  if (size <= aChunk->size) {
1763
    aChunk->size = size;
1764
    return theData;
1765
  }
1766
  return C_NULL;
1767
#else
1768
6.69k
  txByte* aData = ((txByte*)theData) - sizeof(txChunk);
1769
6.69k
  txChunk* aChunk = (txChunk*)aData;
1770
6.69k
  txSize capacity = (txSize)(aChunk->temporary - aData);
1771
6.69k
  txBlock* aBlock = the->firstBlock;
1772
6.69k
  size = fxAdjustChunkSize(the, size);
1773
6.69k
  if (size <= capacity) {
1774
0
    the->currentChunksSize += size - aChunk->size;
1775
0
    if (the->peakChunksSize < the->currentChunksSize)
1776
0
      the->peakChunksSize = the->currentChunksSize;
1777
0
    aChunk->size = size;
1778
0
  #ifdef mxMetering
1779
0
    the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1780
0
  #endif
1781
0
    return theData;
1782
0
  }
1783
18.8k
  while (aBlock) {
1784
12.3k
    if (aChunk->temporary == aBlock->current) {
1785
133
      txSize delta = size - capacity;
1786
133
      if (aBlock->current + delta <= aBlock->limit) {
1787
122
        the->currentChunksSize += size - aChunk->size;
1788
122
        if (the->peakChunksSize < the->currentChunksSize)
1789
49
          the->peakChunksSize = the->currentChunksSize;
1790
122
        aBlock->current += delta;
1791
122
        aChunk->temporary = aBlock->current;
1792
122
        aChunk->size = size;
1793
122
      #ifdef mxSnapshot
1794
122
        c_memset(aData + capacity, 0, delta);
1795
122
      #endif
1796
122
      #ifdef mxMetering
1797
122
        the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1798
122
      #endif
1799
122
        return theData;
1800
122
      }
1801
11
      else {
1802
11
        return C_NULL;
1803
11
      }
1804
133
    }
1805
12.2k
    aBlock = aBlock->nextBlock;
1806
12.2k
  }
1807
6.55k
  return C_NULL;
1808
6.69k
#endif
1809
6.69k
}
1810
1811
#if mxAliasInstance
1812
void fxShare(txMachine* the)
1813
{
1814
  txID aliasCount = 0;
1815
  txSlot *heap, *slot, *limit;
1816
1817
  heap = the->firstHeap;
1818
  while (heap) {
1819
    slot = heap + 1;
1820
    limit = heap->value.reference;
1821
    while (slot < limit) {
1822
      if (slot->kind == XS_INSTANCE_KIND) {
1823
        txBoolean frozen = (slot->flag & XS_DONT_PATCH_FLAG) ? 1 : 0;
1824
        if (frozen) {
1825
          txSlot *property = slot->next;
1826
          while (property) {
1827
            if (property->kind == XS_ARRAY_KIND) {
1828
              txSlot* item = property->value.array.address;
1829
              txInteger length = (txInteger)fxGetIndexSize(the, property);
1830
              while (length > 0) {
1831
                if (item->kind != XS_ACCESSOR_KIND) 
1832
                  if (!(item->flag & XS_DONT_SET_FLAG))
1833
                    frozen = 0;
1834
                if (!(item->flag & XS_DONT_DELETE_FLAG))
1835
                  frozen = 0;
1836
                item++;
1837
                length--;
1838
              }
1839
            }
1840
            else {
1841
              if (property->kind != XS_ACCESSOR_KIND) 
1842
                if (!(property->flag & XS_DONT_SET_FLAG))
1843
                  frozen = 0;
1844
              if (!(property->flag & XS_DONT_DELETE_FLAG))
1845
                frozen = 0;
1846
            }
1847
            property = property->next;
1848
          }
1849
        }
1850
        if (frozen)
1851
          slot->ID = XS_NO_ID;
1852
        else
1853
          slot->ID = aliasCount++;
1854
      }
1855
      else if (slot->kind == XS_CLOSURE_KIND) {
1856
        txSlot* closure = slot->value.closure;
1857
        if (closure->flag & XS_DONT_SET_FLAG)
1858
          closure->flag |= XS_DONT_DELETE_FLAG;
1859
        else {
1860
          if (closure->ID == XS_NO_ID)
1861
            closure->ID = aliasCount++;
1862
          slot->flag &= ~XS_DONT_SET_FLAG;
1863
        }
1864
      }
1865
      slot->flag |= XS_MARK_FLAG; 
1866
      slot++;
1867
    }
1868
    heap = heap->next;
1869
  }
1870
  the->aliasCount = aliasCount;
1871
  /*
1872
  fxReport(the, "# Share\n");
1873
  fxReport(the, "# \tSlots: %ld\n", the->currentHeapCount);
1874
  fxReport(the, "# \t\tSymbols: %ld\n", the->keyIndex);
1875
  fxReport(the, "# \t\tInstances: %ld\n", aliasCount);
1876
  fxReport(the, "# \tChunks: %ld bytes\n", the->currentChunksSize);
1877
  */
1878
}
1879
#endif
1880
1881
void fxSweep(txMachine* the)
1882
3.16k
{
1883
3.16k
  txSize aTotal;
1884
#if mxNoChunks
1885
  txChunk** address;
1886
  txChunk* chunk;
1887
#else
1888
3.16k
  txBlock* aBlock;
1889
3.16k
  txByte* limit;
1890
3.16k
  txByte* next;
1891
3.16k
#endif
1892
3.16k
  txByte* current;
1893
3.16k
  txByte* temporary;
1894
3.16k
  txSize aSize;
1895
3.16k
  txByte** aCodeAddress;
1896
3.16k
  txSlot* aSlot;
1897
3.16k
  txSlot* bSlot;
1898
3.16k
  txSlot* cSlot;
1899
3.16k
  txSlot* freeSlot;
1900
3.16k
  txJump* jump;
1901
1902
#ifdef mxNever
1903
  startTime(&gxSweepChunkTime);
1904
#endif
1905
1906
3.16k
  aTotal = 0;
1907
#if mxNoChunks
1908
  address = (txChunk**)&(the->firstBlock);
1909
  while ((chunk = *address)) {
1910
    aSize = chunk->size;
1911
    if (aSize & mxChunkFlag) {
1912
      aSize &= ~mxChunkFlag;
1913
      temporary = c_malloc_noforcefail(aSize);
1914
      if (!temporary)
1915
        fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);    // should never happen
1916
      c_memcpy(temporary, chunk, aSize);
1917
      ((txChunk*)temporary)->size = aSize;
1918
      chunk->temporary = temporary;
1919
      address = (txChunk**)&(((txChunk*)temporary)->temporary);
1920
      aTotal += aSize;
1921
    }
1922
    else {
1923
      *address = (txChunk*)(chunk->temporary);
1924
      c_free(chunk);
1925
    }
1926
  }
1927
#else
1928
3.16k
  aBlock = the->firstBlock;
1929
10.7k
  while (aBlock) {
1930
7.56k
    current = ((txByte*)aBlock) + sizeof(txBlock);
1931
7.56k
    limit = aBlock->current;
1932
7.56k
    temporary = current;
1933
59.2M
    while (current < limit) {
1934
59.2M
      aSize = ((txChunk*)current)->size;
1935
59.2M
      next = ((txChunk*)current)->temporary;
1936
59.2M
      if (aSize & mxChunkFlag) {
1937
54.5M
        aSize &= ~mxChunkFlag;
1938
54.5M
        ((txChunk*)current)->temporary = temporary;
1939
54.5M
        temporary += aSize;
1940
54.5M
        aTotal += aSize;
1941
54.5M
      }
1942
4.65M
      else {
1943
4.65M
        ((txChunk*)current)->temporary = C_NULL;
1944
4.65M
      }
1945
59.2M
      ((txChunk*)current)->size = (txSize)(next - current);
1946
59.2M
      current = next;
1947
59.2M
    }  
1948
7.56k
    aBlock->temporary = temporary;
1949
7.56k
    aBlock = aBlock->nextBlock;
1950
7.56k
  }
1951
3.16k
#endif
1952
3.16k
  the->currentChunksSize = aTotal;
1953
1954
3.16k
  aCodeAddress = &(the->code);
1955
3.16k
  aSlot = the->frame;
1956
9.46k
  while (aSlot) {
1957
6.30k
    mxCheck(the, aSlot->kind == XS_FRAME_KIND);
1958
6.30k
    if ((aSlot->flag & XS_C_FLAG) == 0) {
1959
0
      bSlot = (aSlot + 3)->value.reference->next;
1960
0
      if (bSlot->kind == XS_CODE_KIND) {
1961
0
        current = bSlot->value.code.address;
1962
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1963
0
        if (temporary) {
1964
0
          temporary += sizeof(txChunk);
1965
0
          *aCodeAddress += temporary - current;
1966
0
        }
1967
0
      }
1968
0
    }
1969
6.30k
    else {
1970
6.30k
      current = *aCodeAddress;
1971
6.30k
      if (current) {
1972
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1973
0
        if (temporary)
1974
0
          *aCodeAddress = temporary + sizeof(txChunk);
1975
0
      }
1976
6.30k
    }
1977
6.30k
    aCodeAddress = &(aSlot->value.frame.code);
1978
6.30k
    aSlot = aSlot->next;
1979
6.30k
  }
1980
  
1981
3.16k
  jump = the->firstJump;
1982
12.6k
  while (jump) {
1983
9.49k
    if (jump->flag) {
1984
0
      aSlot = jump->frame;
1985
0
      bSlot = (aSlot + 3)->value.reference->next;
1986
0
      if (bSlot->kind == XS_CODE_KIND) {
1987
0
        current = bSlot->value.code.address;
1988
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1989
0
        if (temporary) {
1990
0
          temporary += sizeof(txChunk);
1991
0
          jump->code += temporary - current;
1992
0
        }
1993
0
      }
1994
0
    }
1995
9.49k
    else {
1996
9.49k
      current = jump->code;
1997
9.49k
      if (current) {
1998
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1999
0
        if (temporary)
2000
0
          jump->code = temporary + sizeof(txChunk);
2001
0
      }
2002
9.49k
    }
2003
9.49k
    jump = jump->nextJump;
2004
9.49k
  }
2005
  
2006
3.16k
  aSlot = the->stack;
2007
7.70M
  while (aSlot < the->stackTop) {
2008
7.69M
    fxSweepValue(the, aSlot);
2009
7.69M
    aSlot++;
2010
7.69M
  }
2011
3.16k
  aSlot = the->cRoot;
2012
3.16k
  while (aSlot) {
2013
0
    fxSweepValue(the, aSlot);
2014
0
    aSlot = aSlot->next;
2015
0
  }
2016
2017
#ifdef mxNever
2018
  stopTime(&gxSweepChunkTime);
2019
  startTime(&gxSweepSlotTime);
2020
#endif
2021
  
2022
3.16k
  aTotal = 0;
2023
3.16k
  freeSlot = C_NULL;
2024
3.16k
  aSlot = the->firstHeap;
2025
10.1k
  while (aSlot) {
2026
6.99k
    bSlot = aSlot + 1;
2027
6.99k
    cSlot = aSlot->value.reference;
2028
229M
    while (bSlot < cSlot) {
2029
229M
      if (bSlot->flag & XS_MARK_FLAG) {
2030
143M
        bSlot->flag &= ~XS_MARK_FLAG; 
2031
143M
        fxSweepValue(the, bSlot);
2032
143M
        aTotal++;
2033
143M
      }
2034
86.0M
      else {
2035
86.0M
      #ifndef mxLink
2036
86.0M
        if (bSlot->kind == XS_HOST_KIND) {
2037
443
          if (bSlot->flag & XS_HOST_HOOKS_FLAG) {
2038
0
            if (bSlot->value.host.variant.hooks->destructor)
2039
0
              (*(bSlot->value.host.variant.hooks->destructor))(bSlot->value.host.data);
2040
0
          }
2041
443
          else if (bSlot->value.host.variant.destructor)
2042
0
            (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
2043
443
        }
2044
86.0M
      #endif
2045
//        if (bSlot->kind == XS_MODULE_KIND) {
2046
//          char* name = fxGetKeyName(the, bSlot->value.module.id);
2047
//          fprintf(stderr, "gc module %d %s\n", bSlot->value.module.id, name);
2048
//        }
2049
      #if mxInstrument
2050
        if ((bSlot->kind == XS_MODULE_KIND) && (bSlot->ID == XS_MODULE_BEHAVIOR))
2051
          the->loadedModulesCount--;
2052
      #endif
2053
86.0M
        bSlot->kind = XS_UNDEFINED_KIND;
2054
86.0M
        bSlot->next = freeSlot;
2055
      #if mxPoisonSlots
2056
        ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
2057
      #endif
2058
86.0M
        freeSlot = bSlot;
2059
86.0M
      }
2060
229M
      bSlot++;
2061
229M
    }
2062
6.99k
    aSlot = aSlot->next;
2063
6.99k
  }
2064
3.16k
  the->currentHeapCount = aTotal;
2065
3.16k
  the->freeHeap = freeSlot;
2066
  
2067
#ifdef mxNever
2068
  stopTime(&gxSweepSlotTime);
2069
  startTime(&gxCompactChunkTime);
2070
#endif
2071
2072
#if mxNoChunks
2073
  address = (txChunk**)&(the->firstBlock);
2074
  while ((chunk = *address)) {
2075
    aSize = chunk->size;
2076
    if (aSize & mxChunkFlag) {
2077
      *address = (txChunk*)(chunk->temporary);
2078
      c_free(chunk);
2079
    }
2080
    else {
2081
      address = (txChunk**)&(chunk->temporary);
2082
    }
2083
  }
2084
#else
2085
3.16k
  aBlock = the->firstBlock;
2086
10.7k
  while (aBlock) {
2087
7.56k
    txByte* former = C_NULL;
2088
7.56k
    current = ((txByte*)aBlock) + sizeof(txBlock);
2089
7.56k
    limit = aBlock->current;
2090
59.2M
    while (current < limit) {
2091
59.2M
      aSize = ((txChunk*)current)->size;
2092
59.2M
      next = current + aSize;
2093
59.2M
      if ((temporary = ((txChunk*)current)->temporary)) {
2094
54.5M
        if (former) {
2095
54.5M
          ((txChunk*)former)->temporary = temporary;
2096
54.5M
          ((txChunk*)former)->size = (txSize)(temporary - former);
2097
54.5M
        }
2098
54.5M
        if (temporary != current)
2099
3.01M
          c_memmove(temporary, current, aSize);
2100
54.5M
        former = temporary;
2101
54.5M
      }
2102
59.2M
      current = next;
2103
59.2M
    }
2104
7.56k
    if (former) {
2105
7.46k
      ((txChunk*)former)->temporary = aBlock->temporary;
2106
7.46k
      ((txChunk*)former)->size = (txSize)(aBlock->temporary - former);
2107
7.46k
    }
2108
7.56k
    aBlock->current = aBlock->temporary;
2109
7.56k
    aBlock->temporary = C_NULL;
2110
7.56k
    aBlock = aBlock->nextBlock;
2111
7.56k
  }
2112
3.16k
#endif
2113
  
2114
#ifdef mxNever
2115
  stopTime(&gxCompactChunkTime);
2116
#endif
2117
3.16k
}
2118
2119
void fxSweepValue(txMachine* the, txSlot* theSlot)
2120
226M
{
2121
226M
  txSlot* aSlot;
2122
226M
  txByte* data;
2123
  
2124
226M
#define mxSweepChunk(_THE_DATA, _THE_DATA_TYPE) \
2125
226M
  if ((data = (txByte*)(((txChunk*)(((txByte*)(_THE_DATA)) - sizeof(txChunk)))->temporary))) \
2126
56.2M
    ((_THE_DATA)) = (_THE_DATA_TYPE)(data + sizeof(txChunk))
2127
2128
226M
  switch (theSlot->kind) {
2129
3.89M
  case XS_STRING_KIND:
2130
3.89M
    mxSweepChunk(theSlot->value.string, txString);
2131
3.89M
    break;
2132
0
  case XS_BIGINT_KIND:
2133
0
    mxSweepChunk(theSlot->value.bigint.data, txU4*);
2134
0
    break;
2135
2136
0
  case XS_ARGUMENTS_SLOPPY_KIND:
2137
0
  case XS_ARGUMENTS_STRICT_KIND:
2138
58.7M
  case XS_ARRAY_KIND:
2139
58.7M
  case XS_STACK_KIND:
2140
58.7M
    if ((aSlot = theSlot->value.array.address)) {
2141
#if mxNoChunks
2142
      mxSweepChunk(theSlot->value.array.address, txSlot*);
2143
      aSlot = theSlot->value.array.address;
2144
#endif
2145
50.4M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
2146
50.4M
      txIndex aLength = chunk->size / sizeof(txSlot);
2147
50.4M
      if (aLength > theSlot->value.array.length)
2148
0
        aLength = theSlot->value.array.length;
2149
125M
      while (aLength) {
2150
75.5M
        fxSweepValue(the, aSlot);
2151
75.5M
        aSlot++;
2152
75.5M
        aLength--;
2153
75.5M
      }
2154
#if mxNoChunks
2155
#else
2156
50.4M
      mxSweepChunk(theSlot->value.array.address, txSlot*);
2157
50.4M
#endif
2158
50.4M
    }
2159
58.7M
    break;
2160
131
  case XS_ARRAY_BUFFER_KIND:
2161
131
    if (theSlot->value.arrayBuffer.address)
2162
103
      mxSweepChunk(theSlot->value.arrayBuffer.address, txByte*);
2163
131
    break;
2164
0
  case XS_CODE_KIND:
2165
0
    mxSweepChunk(theSlot->value.code.address, txByte*);
2166
0
    break;
2167
3.16k
  case XS_GLOBAL_KIND:
2168
3.16k
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2169
3.16k
    break;
2170
3.26k
  case XS_HOST_KIND:
2171
3.26k
    if (theSlot->value.host.data) {
2172
101
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
2173
101
        mxSweepChunk(theSlot->value.host.data, void*);
2174
101
    }
2175
3.26k
    break;
2176
0
  case XS_IDS_KIND:
2177
0
    if (theSlot->value.IDs)
2178
0
      mxSweepChunk(theSlot->value.IDs, txID*);
2179
0
    break;
2180
0
  case XS_REGEXP_KIND:
2181
0
    if (theSlot->value.regexp.code)
2182
0
      mxSweepChunk(theSlot->value.regexp.code, void*);
2183
0
    if (theSlot->value.regexp.data)
2184
0
      mxSweepChunk(theSlot->value.regexp.data, void*);
2185
0
    break;
2186
1.86M
  case XS_KEY_KIND:
2187
1.86M
    if (theSlot->value.key.string)
2188
1.86M
      mxSweepChunk(theSlot->value.key.string, txString);
2189
1.86M
    break;
2190
0
  case XS_MAP_KIND:
2191
0
  case XS_SET_KIND:
2192
0
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2193
0
    break;
2194
226M
  }
2195
226M
}