Coverage Report

Created: 2025-10-28 07:21

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
78.2M
{
62
78.2M
  if (!gxStress)
63
78.2M
    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
216M
#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
17.2M
{
144
17.2M
  txSize c;
145
17.2M
#if __has_builtin(__builtin_add_overflow)
146
17.2M
  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
17.2M
  return c;
154
17.2M
}
155
156
txSize fxAdjustChunkSize(txMachine* the, txSize size)
157
15.2M
{
158
15.2M
  txSize adjust = sizeof(txChunk);
159
15.2M
  txSize modulo = size & (sizeof(size_t) - 1);
160
15.2M
  if (modulo)
161
7.54M
    adjust += sizeof(size_t) - modulo;
162
15.2M
  return fxAddChunkSizes(the, size, adjust);
163
15.2M
}
164
165
void fxAllocate(txMachine* the, txCreation* theCreation)
166
6.53k
{
167
#ifdef mxNever
168
  startTime(&gxLifeTime);
169
#endif
170
6.53k
#if mxStress
171
6.53k
  gxStress = 0;
172
6.53k
#endif
173
174
6.53k
  the->currentChunksSize = 0;
175
6.53k
  the->peakChunksSize = 0;
176
6.53k
  the->maximumChunksSize = 0;
177
6.53k
  the->minimumChunksSize = theCreation->incrementalChunkSize;
178
  
179
6.53k
  the->currentHeapCount = 0;
180
6.53k
  the->peakHeapCount = 0;
181
6.53k
  the->maximumHeapCount = 0;
182
6.53k
  the->minimumHeapCount = theCreation->incrementalHeapCount;
183
  
184
6.53k
  the->firstBlock = C_NULL;
185
6.53k
  the->firstHeap = C_NULL;
186
187
#if mxNoChunks
188
  the->maximumChunksSize = theCreation->initialChunkSize;
189
#else
190
6.53k
  fxGrowChunks(the, theCreation->initialChunkSize);
191
6.53k
#endif
192
193
6.53k
  the->stackBottom = fxAllocateSlots(the, theCreation->stackCount);
194
6.53k
  the->stackTop = the->stackBottom + theCreation->stackCount;
195
6.53k
  the->stackIntrinsics = the->stackTop;
196
6.53k
  the->stackPrototypes = the->stackTop - XS_INTRINSICS_COUNT;
197
6.53k
  the->stack = the->stackTop;
198
#ifdef mxInstrument
199
  the->stackPeak = the->stackTop;
200
#endif
201
202
6.53k
  fxGrowSlots(the, theCreation->initialHeapCount);
203
204
6.53k
  the->keyCount = (txID)theCreation->initialKeyCount;
205
6.53k
  the->keyDelta = (txID)theCreation->incrementalKeyCount;
206
6.53k
  the->keyIndex = 0;
207
6.53k
  the->keyArray = (txSlot **)c_malloc_uint32(theCreation->initialKeyCount * sizeof(txSlot*));
208
6.53k
  if (!the->keyArray)
209
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);   
210
  
211
6.53k
  the->nameModulo = theCreation->nameModulo;
212
6.53k
  the->nameTable = (txSlot **)c_malloc_uint32(theCreation->nameModulo * sizeof(txSlot*));
213
6.53k
  if (!the->nameTable)
214
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
215
216
6.53k
  the->symbolModulo = theCreation->symbolModulo;
217
6.53k
  the->symbolTable = (txSlot **)c_malloc_uint32(theCreation->symbolModulo * sizeof(txSlot*));
218
6.53k
  if (!the->symbolTable)
219
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
220
    
221
6.53k
  fxAllocateStringInfoCache(the);
222
223
6.53k
  the->stackLimit = fxCStackLimit();
224
225
6.53k
  the->cRoot = C_NULL;
226
6.53k
  the->parserBufferSize = theCreation->parserBufferSize;
227
6.53k
  the->parserTableModulo = theCreation->parserTableModulo;
228
  
229
6.53k
#ifdef mxDebug
230
6.53k
  the->pathCount = 256;
231
6.53k
  the->pathValue = c_malloc(the->pathCount);
232
6.53k
  if (!the->pathValue)
233
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
234
6.53k
#endif
235
6.53k
}
236
237
void* fxCheckChunk(txMachine* the, txChunk* chunk, txSize size, txSize offset)
238
15.2M
{
239
15.2M
  if (chunk) {
240
15.2M
    txByte* data = (txByte*)chunk;
241
#if mxNoChunks
242
    chunk->size = size;
243
    the->currentChunksSize += size;
244
#else
245
15.2M
    txSize capacity = (txSize)(chunk->temporary - data);
246
15.2M
  #ifdef mxSnapshot
247
15.2M
    #if INTPTR_MAX == INT64_MAX
248
15.2M
      chunk->dummy = 0;
249
15.2M
    #endif
250
  #ifdef mxSnapshotRandomInit
251
    arc4random_buf(data + sizeof(txChunk), offset);
252
  #endif    
253
15.2M
  #endif
254
15.2M
    offset += sizeof(txChunk);
255
15.2M
    c_memset(data + offset, 0, capacity - offset);
256
15.2M
    chunk->size = size;
257
15.2M
    the->currentChunksSize += capacity;
258
15.2M
#endif
259
15.2M
    if (the->peakChunksSize < the->currentChunksSize)
260
10.4M
      the->peakChunksSize = the->currentChunksSize;
261
15.2M
    return data + sizeof(txChunk);
262
15.2M
  }
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
15.2M
}
267
268
#if defined(__clang__) || defined (__GNUC__)
269
  __attribute__((no_sanitize_address))
270
#endif
271
void fxCheckCStack(txMachine* the)
272
213M
{
273
213M
    char x;
274
213M
    char *stack = &x;
275
213M
  if (stack <= the->stackLimit) {
276
0
    fxAbort(the, XS_NATIVE_STACK_OVERFLOW_EXIT);
277
0
  }
278
213M
}
279
280
void fxCollect(txMachine* the, txFlag theFlag)
281
13.4k
{
282
13.4k
  txSize aCount;
283
13.4k
  txSlot* freeSlot;
284
13.4k
  txSlot* aSlot;
285
13.4k
  txSlot* bSlot;
286
13.4k
  txSlot* cSlot;
287
288
13.4k
  if ((the->collectFlag & XS_COLLECTING_FLAG) == 0) {
289
0
    the->collectFlag |= XS_SKIPPED_COLLECT_FLAG;
290
0
    return;
291
0
  }
292
13.4k
  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
13.4k
  if (theFlag & XS_COMPACT_FLAG) {
308
3.62k
    fxInvalidateStringInfoCache(the);
309
3.62k
    fxMark(the, fxMarkValue);
310
3.62k
    fxMarkWeakStuff(the);
311
  #ifdef mxNever
312
    stopTime(&gxMarkTime);
313
  #endif
314
3.62k
    fxSweep(the);
315
3.62k
  }
316
9.81k
  else {
317
9.81k
    fxMark(the, fxMarkReference);
318
9.81k
    fxMarkWeakStuff(the);
319
  #ifdef mxNever
320
    stopTime(&gxMarkTime);
321
    startTime(&gxSweepSlotTime);
322
  #endif
323
9.81k
    aCount = 0;
324
9.81k
    freeSlot = C_NULL;
325
9.81k
    aSlot = the->firstHeap;
326
30.5k
    while (aSlot) {
327
20.7k
      bSlot = aSlot + 1;
328
20.7k
      cSlot = aSlot->value.reference;
329
678M
      while (bSlot < cSlot) {
330
678M
        if (bSlot->flag & XS_MARK_FLAG) {
331
447M
          bSlot->flag &= ~XS_MARK_FLAG; 
332
          
333
447M
          if (bSlot->kind == XS_REFERENCE_KIND)
334
90.8M
            mxCheck(the, bSlot->value.reference->kind == XS_INSTANCE_KIND);
335
          
336
447M
          aCount++;
337
447M
        }
338
231M
        else {
339
231M
          if (bSlot->kind == XS_HOST_KIND) {
340
415
            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
415
            else if (bSlot->value.host.variant.destructor)
345
0
              (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
346
415
          }
347
        #if mxInstrument
348
          if ((bSlot->kind == XS_MODULE_KIND) && (bSlot->ID == XS_MODULE_BEHAVIOR))
349
            the->loadedModulesCount--;
350
        #endif
351
231M
          bSlot->kind = XS_UNDEFINED_KIND;
352
231M
          bSlot->next = freeSlot;
353
        #if mxPoisonSlots
354
          ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
355
        #endif
356
231M
          freeSlot = bSlot;
357
231M
        }
358
678M
        bSlot++;
359
678M
      }
360
20.7k
      aSlot = aSlot->next;
361
20.7k
    }
362
9.81k
    the->currentHeapCount = aCount;
363
9.81k
    the->freeHeap = freeSlot;
364
  #ifdef mxNever
365
    stopTime(&gxSweepSlotTime);
366
  #endif
367
9.81k
  }
368
  
369
13.4k
  aSlot = the->stack;
370
32.6M
  while (aSlot < the->stackTop) {
371
32.6M
    aSlot->flag &= ~XS_MARK_FLAG; 
372
32.6M
    aSlot++;
373
32.6M
  }
374
  
375
13.4k
  the->collectFlag &= ~(XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
376
  
377
13.4k
  if (theFlag & XS_COMPACT_FLAG) {
378
3.62k
    if ((the->maximumChunksSize - the->currentChunksSize) < the->minimumChunksSize)
379
1.30k
      the->collectFlag |= XS_TRASHING_CHUNKS_FLAG;
380
2.32k
    else
381
2.32k
      the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
382
3.62k
  }
383
  
384
13.4k
  if ((the->maximumHeapCount - the->currentHeapCount) < the->minimumHeapCount)
385
12.6k
    the->collectFlag |= XS_TRASHING_SLOTS_FLAG;
386
785
  else
387
785
    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
13.4k
#if defined(mxInstrument) || defined(mxProfile)
422
13.4k
  fxCheckProfiler(the, C_NULL);
423
13.4k
#endif
424
13.4k
}
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
15.2M
{
440
15.2M
  txBlock* block;
441
15.2M
  txChunk* chunk;
442
15.2M
#if mxStress
443
15.2M
  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
15.2M
#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
15.2M
again:
465
15.2M
  block = the->firstBlock;
466
18.4M
  while (block) {
467
18.4M
    if ((block->current + size) <= block->limit) {
468
15.2M
      chunk = (txChunk*)(block->current);
469
15.2M
      block->current += size;
470
15.2M
      chunk->temporary = block->current;
471
15.2M
      return chunk;
472
15.2M
    }
473
3.19M
    block = block->nextBlock;
474
3.19M
  }
475
3.94k
  if (*once) {
476
3.62k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0), isThrashing;
477
3.62k
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
478
3.62k
    isThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0);
479
3.62k
    *once = 0;
480
3.62k
    if (wasThrashing && isThrashing)
481
353
      return C_NULL;
482
3.27k
    goto again;
483
3.62k
  }
484
322
  return C_NULL;
485
3.94k
}
486
487
txSlot* fxFindKey(txMachine* the)
488
3.67M
{
489
3.67M
#if mxKeysGarbageCollection
490
3.67M
  txBoolean once = 1;
491
3.67M
#endif
492
3.67M
  txID id;
493
3.67M
  txSlot* result;
494
3.67M
more:
495
3.67M
  id = the->keyIndex;
496
3.67M
  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
37.2k
#if mxKeysGarbageCollection
504
45.6k
again:
505
45.6k
  result = the->keyholeList;
506
45.6k
  if (result) {
507
37.0k
    the->keyholeCount--;
508
37.0k
    the->keyholeList = result->next;
509
37.0k
    result->next = C_NULL;
510
37.0k
    return result;
511
37.0k
  }
512
8.60k
  if (once) {
513
8.45k
    fxCollect(the, XS_COLLECT_KEYS_FLAG | XS_ORGANIC_FLAG);
514
8.45k
    once = 0;
515
8.45k
    goto again;
516
8.45k
  }
517
155
#endif
518
155
  else {
519
155
    fxGrowKeys(the, 1);
520
155
    goto more;
521
155
  }
522
0
  return C_NULL;
523
8.60k
}
524
525
void fxFree(txMachine* the) 
526
6.53k
{
527
6.53k
  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.53k
  fxFreeStringInfoCache(the);
536
537
6.53k
  if (the->symbolTable)
538
6.53k
    c_free_uint32(the->symbolTable);
539
6.53k
  the->symbolTable = C_NULL;
540
6.53k
  if (the->nameTable)
541
6.53k
    c_free_uint32(the->nameTable);
542
6.53k
  the->nameTable = C_NULL;
543
6.53k
  if (the->keyArray)
544
6.53k
    c_free_uint32(the->keyArray);
545
6.53k
  the->keyArray = C_NULL;
546
  
547
13.8k
  while (the->firstHeap) {
548
7.27k
    aHeap = the->firstHeap;
549
7.27k
    the->firstHeap = aHeap->next;
550
7.27k
    fxFreeSlots(the, aHeap);
551
7.27k
  }
552
6.53k
  the->firstHeap = C_NULL;
553
554
6.53k
  if (the->stackBottom)
555
6.53k
    fxFreeSlots(the, the->stackBottom);
556
6.53k
  the->stackBottom = C_NULL;
557
6.53k
  the->stackTop = C_NULL;
558
6.53k
  the->stackIntrinsics = C_NULL;
559
6.53k
  the->stackPrototypes = C_NULL;
560
6.53k
  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.53k
  {
574
6.53k
    txBlock* aBlock;
575
13.7k
    while (the->firstBlock) {
576
7.20k
      aBlock = the->firstBlock;
577
7.20k
      the->firstBlock = aBlock->nextBlock;
578
7.20k
      fxFreeChunks(the, aBlock);
579
7.20k
    }
580
6.53k
    the->firstBlock = C_NULL;
581
6.53k
  }
582
6.53k
#endif
583
584
6.53k
#ifdef mxDebug
585
6.53k
  if (the->pathValue)
586
6.53k
    c_free(the->pathValue);
587
6.53k
  the->pathValue = C_NULL;
588
6.53k
#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.53k
}
605
606
void* fxGrowChunk(txMachine* the, txSize size) 
607
675
{
608
675
  txBlock* block = fxGrowChunks(the, size);
609
675
  txChunk* chunk = C_NULL;
610
675
  if (block) {
611
675
    chunk = (txChunk*)(block->current);
612
675
    block->current += size;
613
675
    chunk->temporary = block->current;
614
675
  }
615
675
  return chunk;
616
675
}
617
618
void* fxGrowChunks(txMachine* the, txSize size) 
619
7.20k
{
620
7.20k
  txByte* buffer;
621
7.20k
  txBlock* block = C_NULL;
622
623
7.20k
  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.20k
  if ((the->firstBlock != C_NULL) && (!(the->collectFlag & XS_SKIPPED_COLLECT_FLAG))) {
629
675
    txSize modulo = size % (the->minimumChunksSize ? the->minimumChunksSize : 16);
630
675
    if (modulo)
631
672
      size = fxAddChunkSizes(the, size, the->minimumChunksSize - modulo);
632
675
  }
633
7.20k
  size = fxAddChunkSizes(the, size, sizeof(txBlock));
634
7.20k
  buffer = fxAllocateChunks(the, size);
635
7.20k
  if (buffer) {
636
7.20k
  #ifdef mxSnapshot
637
7.20k
    c_memset(buffer, 0, size);
638
7.20k
  #endif
639
7.20k
    if ((the->firstBlock != C_NULL) && (the->firstBlock->limit == buffer)) {
640
0
      the->firstBlock->limit += size;
641
0
      block = the->firstBlock;
642
0
    }
643
7.20k
    else {
644
7.20k
      block = (txBlock*)buffer;
645
7.20k
      block->nextBlock = the->firstBlock;
646
7.20k
      block->current = buffer + sizeof(txBlock);
647
7.20k
      block->limit = buffer + size;
648
7.20k
      block->temporary = C_NULL;
649
7.20k
      the->firstBlock = block;
650
7.20k
      size -= sizeof(txBlock);
651
7.20k
    }
652
7.20k
    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.20k
  }
658
7.20k
  the->collectFlag &= ~XS_TRASHING_CHUNKS_FLAG;
659
7.20k
  return block;
660
7.20k
}
661
662
void fxGrowKeys(txMachine* the, txID theCount) 
663
155
{
664
155
  if (the->keyDelta > 0) {
665
155
    txID keyDelta = (theCount > the->keyDelta) ? theCount : the->keyDelta;
666
155
    txID keyCount = (the->keyCount + keyDelta) - the->keyOffset;
667
155
    txSlot** keyArray = c_realloc(the->keyArray, keyCount * sizeof(txSlot*));
668
155
    if (keyArray == C_NULL)
669
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
670
155
    the->keyArray = keyArray;
671
155
    the->keyCount = keyCount + the->keyOffset;
672
155
  }
673
0
  else 
674
0
    fxAbort(the, XS_NO_MORE_KEYS_EXIT);
675
155
}
676
677
void fxGrowSlots(txMachine* the, txSize theCount) 
678
7.27k
{
679
7.27k
  txSlot* aHeap;
680
7.27k
  txSlot* aSlot;
681
682
7.27k
  aHeap = fxAllocateSlots(the, theCount);
683
7.27k
  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.27k
  if ((void *)-1 == aHeap)
688
0
    return;
689
    
690
7.27k
  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.27k
  else {
706
7.27k
    the->maximumHeapCount += theCount - 1;
707
7.27k
    aHeap->next = the->firstHeap;
708
7.27k
    aHeap->ID = 0;
709
7.27k
    aHeap->flag = 0;
710
7.27k
    aHeap->kind = 0;
711
7.27k
    aHeap->value.reference = aHeap + theCount;
712
7.27k
    theCount -= 2;
713
7.27k
    the->firstHeap = aHeap;
714
7.27k
    aSlot = aHeap + 1;
715
7.27k
  }
716
238M
    while (theCount--) {
717
238M
    txSlot* next = aSlot + 1;
718
238M
    aSlot->next = next;
719
238M
    aSlot->flag = XS_NO_FLAG;
720
238M
    aSlot->kind = XS_UNDEFINED_KIND;
721
  #if mxPoisonSlots
722
    ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
723
  #endif
724
238M
        aSlot = next;
725
238M
    }
726
7.27k
  aSlot->next = the->freeHeap;
727
7.27k
  aSlot->flag = XS_NO_FLAG;
728
7.27k
  aSlot->kind = XS_UNDEFINED_KIND;
729
#if mxPoisonSlots
730
  ASAN_POISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
731
#endif
732
7.27k
  the->freeHeap = aHeap + 1;
733
7.27k
  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.27k
}
741
742
void fxMark(txMachine* the, void (*theMarker)(txMachine*, txSlot*))
743
13.4k
{
744
13.4k
  txSlot** p;
745
13.4k
  txSlot** q;
746
13.4k
  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
13.4k
  slot = the->stackTop;
761
32.6M
  while (slot > the->stack) {
762
32.6M
        slot--;
763
32.6M
    (*theMarker)(the, slot);
764
32.6M
  }
765
13.4k
  slot = the->cRoot;
766
13.4k
  while (slot) {
767
0
    (*theMarker)(the, slot);
768
0
    slot = slot->next;
769
0
  }
770
  
771
13.4k
#if mxKeysGarbageCollection
772
13.4k
  if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
773
8.45k
    txInteger deletions = 0;
774
8.45k
    p = the->keyArray;
775
8.45k
    q = p + the->keyIndex - the->keyOffset;
776
8.66M
    while (p < q) {
777
8.65M
      slot = *p++;
778
8.65M
      if (!(slot->flag & XS_MARK_FLAG)) {
779
640k
        if (slot->flag & XS_DONT_DELETE_FLAG)
780
600k
          slot->flag |= XS_MARK_FLAG;
781
39.9k
        else if (slot->flag & XS_DONT_ENUM_FLAG)
782
39.9k
          deletions++;
783
640k
      }
784
8.65M
    }
785
  
786
  //  fprintf(stderr, "\n### KEYS GC %d", deletions);
787
8.45k
    p = the->nameTable;
788
8.45k
    q = the->nameTable + the->nameModulo;
789
13.6M
    while ((p < q) && deletions) {
790
13.6M
      txSlot** address = p;
791
20.5M
      while (((slot = *address)) && deletions) {
792
6.94M
        if (slot->flag & XS_MARK_FLAG)
793
6.90M
          address = &(slot->next);
794
39.9k
        else {
795
39.9k
          *address = slot->next;
796
39.9k
          deletions--;
797
39.9k
        }
798
6.94M
      }
799
13.6M
      p++;
800
13.6M
    }
801
  //  fprintf(stderr, " => %d", deletions);
802
  
803
8.45k
    the->keyholeCount = 0;
804
8.45k
    the->keyholeList = C_NULL;
805
8.45k
    p = the->keyArray;
806
8.45k
    q = p + the->keyIndex - the->keyOffset;
807
8.66M
    while (p < q) {
808
8.65M
      slot = *p;
809
8.65M
      if (slot->flag & XS_MARK_FLAG)
810
8.61M
        (*theMarker)(the, slot);
811
39.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
39.9k
        slot->flag = XS_INTERNAL_FLAG | XS_MARK_FLAG;
817
39.9k
        slot->next = the->keyholeList;
818
39.9k
        slot->kind = XS_UNDEFINED_KIND;
819
39.9k
        the->keyholeCount++;
820
39.9k
        the->keyholeList = slot;
821
39.9k
      }
822
8.65M
      p++;
823
8.65M
    }
824
  //  fprintf(stderr, "\n");
825
8.45k
  }
826
4.99k
  else
827
4.99k
#endif
828
4.99k
  {
829
4.99k
    p = the->keyArray;
830
4.99k
    q = p + the->keyIndex - the->keyOffset;
831
2.86M
    while (p < q) {
832
2.85M
      slot = *p++;
833
2.85M
      slot->flag |= XS_MARK_FLAG;
834
2.85M
      (*theMarker)(the, slot);
835
2.85M
    }
836
4.99k
  }
837
13.4k
}
838
839
#if mxKeysGarbageCollection
840
void fxMarkID(txMachine* the, txID id)
841
53.4M
{
842
53.4M
  txSlot* slot;
843
53.4M
  if (id == XS_NO_ID)
844
22.1M
    return;
845
31.2M
  if (id < the->keyOffset)
846
0
    return;
847
31.2M
  id -= the->keyOffset;
848
31.2M
  slot = the->keyArray[id];
849
31.2M
  slot->flag |= XS_MARK_FLAG;
850
31.2M
}
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
123M
{
875
123M
  txSlot* aProperty;
876
123M
  txSlot* aTemporary;
877
878
123M
  mxCheck(the, theCurrent->kind == XS_INSTANCE_KIND);
879
123M
  aProperty = theCurrent;
880
123M
  theCurrent->value.instance.garbage = C_NULL;
881
791M
  for (;;) {
882
791M
    if (aProperty) {
883
574M
      if (!(aProperty->flag & XS_MARK_FLAG)) {
884
574M
        aProperty->flag |= XS_MARK_FLAG;
885
574M
        switch (aProperty->kind) {
886
217M
        case XS_INSTANCE_KIND:
887
217M
          aTemporary = aProperty->value.instance.prototype;
888
217M
          if (aTemporary && !(aTemporary->flag & XS_MARK_FLAG)) {
889
53.7k
            aProperty->value.instance.prototype = theCurrent;
890
53.7k
            theCurrent = aTemporary;
891
53.7k
            theCurrent->value.instance.garbage = aProperty;
892
53.7k
            aProperty = theCurrent;
893
53.7k
          }
894
217M
          else
895
217M
            aProperty = aProperty->next;
896
217M
          break;
897
95.3M
        case XS_REFERENCE_KIND:
898
95.3M
        #if mxKeysGarbageCollection
899
95.3M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
900
27.3M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
901
27.3M
              fxMarkID(the, aProperty->ID);
902
27.3M
          }
903
95.3M
        #endif
904
95.3M
          aTemporary = aProperty->value.reference;
905
95.3M
          if (!(aTemporary->flag & XS_MARK_FLAG)) {
906
94.3M
            aProperty->value.reference = theCurrent;
907
94.3M
            theCurrent = aTemporary;
908
94.3M
            theCurrent->value.instance.garbage = aProperty;
909
94.3M
            aProperty = theCurrent;
910
94.3M
          }
911
994k
          else
912
994k
            aProperty = aProperty->next;
913
95.3M
          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
7.85M
        case XS_CALLBACK_KIND:
967
7.85M
        case XS_CODE_KIND:
968
7.85M
        #if mxKeysGarbageCollection
969
7.85M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
970
4.93M
            fxMarkID(the, aProperty->ID);
971
7.85M
        #endif
972
7.85M
          (*theMarker)(the, aProperty);
973
7.85M
          aProperty = aProperty->next;
974
7.85M
          break;  
975
        
976
253M
        default:
977
253M
        #if mxKeysGarbageCollection
978
253M
          if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
979
56.6M
            if (!(aProperty->flag & XS_INTERNAL_FLAG))
980
15.5M
              fxMarkID(the, aProperty->ID);
981
56.6M
          }
982
253M
        #endif
983
253M
          (*theMarker)(the, aProperty);
984
253M
          aProperty = aProperty->next;
985
253M
          break; 
986
574M
        }
987
574M
      }
988
0
      else
989
0
        aProperty = aProperty->next;
990
574M
    }
991
217M
    else if (theCurrent->value.instance.garbage) {
992
94.3M
      aProperty = theCurrent->value.instance.garbage;
993
94.3M
      theCurrent->value.instance.garbage = C_NULL;
994
94.3M
      switch (aProperty->kind) {
995
53.7k
      case XS_INSTANCE_KIND:
996
53.7k
        aTemporary = aProperty->value.instance.prototype;
997
53.7k
        aProperty->value.instance.prototype = theCurrent;
998
53.7k
        theCurrent = aTemporary;
999
53.7k
        aProperty = aProperty->next;
1000
53.7k
        break;
1001
94.3M
      case XS_REFERENCE_KIND:
1002
94.3M
        aTemporary = aProperty->value.reference;
1003
94.3M
        aProperty->value.reference = theCurrent;
1004
94.3M
        theCurrent = aTemporary;
1005
94.3M
        aProperty = aProperty->next;
1006
94.3M
        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
94.3M
      }
1039
94.3M
    }
1040
123M
    else
1041
123M
      break;
1042
791M
  }
1043
123M
}
1044
1045
void fxMarkReference(txMachine* the, txSlot* theSlot)
1046
299M
{
1047
299M
  txSlot* aSlot;
1048
299M
  switch (theSlot->kind) {
1049
69.2M
  case XS_REFERENCE_KIND:
1050
69.2M
    aSlot = theSlot->value.reference;
1051
69.2M
    if (!(aSlot->flag & XS_MARK_FLAG))
1052
68.1M
      fxMarkInstance(the, aSlot, fxMarkReference);
1053
69.2M
    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
569k
  case XS_ACCESSOR_KIND:
1066
569k
    aSlot = theSlot->value.accessor.getter;
1067
569k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1068
559k
      fxCheckCStack(the);
1069
559k
      fxMarkInstance(the, aSlot, fxMarkReference);
1070
559k
    }
1071
569k
    aSlot = theSlot->value.accessor.setter;
1072
569k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1073
78.5k
      fxCheckCStack(the);
1074
78.5k
      fxMarkInstance(the, aSlot, fxMarkReference);
1075
78.5k
    }
1076
569k
    break;
1077
0
  case XS_ARGUMENTS_SLOPPY_KIND:
1078
0
  case XS_ARGUMENTS_STRICT_KIND:
1079
147M
  case XS_ARRAY_KIND:
1080
147M
  case XS_STACK_KIND:
1081
147M
    fxCheckCStack(the);
1082
147M
    if ((aSlot = theSlot->value.array.address)) {
1083
48.6M
      txIndex aLength = (((txChunk*)(((txByte*)aSlot) - sizeof(txChunk)))->size) / sizeof(txSlot);
1084
48.6M
      if (aLength > theSlot->value.array.length)
1085
0
        aLength = theSlot->value.array.length;
1086
122M
      while (aLength) {
1087
74.2M
        fxMarkReference(the, aSlot);
1088
74.2M
        aSlot++;
1089
74.2M
        aLength--;
1090
74.2M
      }
1091
48.6M
    }
1092
147M
    break;
1093
5.73M
  case XS_CALLBACK_KIND:
1094
5.73M
    aSlot = theSlot->value.callback.closures;
1095
5.73M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1096
0
      fxCheckCStack(the);
1097
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1098
0
    }
1099
5.73M
    break;
1100
0
  case XS_CODE_KIND:
1101
    // continue
1102
19.6k
  case XS_CODE_X_KIND:
1103
19.6k
    aSlot = theSlot->value.code.closures;
1104
19.6k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1105
0
      fxCheckCStack(the);
1106
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1107
0
    }
1108
19.6k
    break;
1109
5.74M
  case XS_HOME_KIND:
1110
5.74M
    aSlot = theSlot->value.home.object;
1111
5.74M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1112
0
      fxCheckCStack(the);
1113
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1114
0
    }
1115
5.74M
    aSlot = theSlot->value.home.module;
1116
5.74M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1117
0
      fxCheckCStack(the);
1118
0
      fxMarkInstance(the, aSlot, fxMarkReference);
1119
0
    }
1120
5.74M
    break;
1121
0
  case XS_MODULE_KIND:
1122
9.81k
  case XS_PROGRAM_KIND:
1123
9.81k
#if mxKeysGarbageCollection
1124
9.81k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1125
8.45k
      fxMarkID(the, theSlot->value.module.id);
1126
9.81k
#endif
1127
9.81k
    aSlot = theSlot->value.module.realm;
1128
9.81k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1129
9.81k
      fxCheckCStack(the);
1130
9.81k
      fxMarkInstance(the, aSlot, fxMarkReference);
1131
9.81k
    }
1132
9.81k
    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
9.81k
  case XS_HOST_KIND:
1144
9.81k
    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
9.81k
    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
16
  case XS_ERROR_KIND:
1164
16
    aSlot = theSlot->value.error.info;
1165
16
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1166
13
      fxMarkInstance(the, aSlot, fxMarkReference);
1167
16
    break;
1168
0
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1169
0
  case XS_DISPOSABLE_STACK_KIND:
1170
29.4k
  case XS_LIST_KIND:
1171
29.4k
    fxCheckCStack(the);
1172
29.4k
    aSlot = theSlot->value.list.first;
1173
29.4k
    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
29.4k
    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
294k
  case XS_SYMBOL_KIND:
1276
294k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1277
253k
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1278
126k
        fxMarkID(the, theSlot->value.symbol);
1279
253k
    }
1280
294k
    break; 
1281
5.56M
  case XS_AT_KIND:
1282
5.56M
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1283
5.51M
      fxMarkID(the, theSlot->value.at.id);
1284
5.56M
    break; 
1285
299M
#endif
1286
299M
  }
1287
299M
}
1288
1289
void fxMarkValue(txMachine* the, txSlot* theSlot)
1290
167M
{
1291
167M
#define mxMarkChunk(_THE_DATA) \
1292
167M
  ((txChunk*)(((txByte*)_THE_DATA) - sizeof(txChunk)))->size |= mxChunkFlag
1293
1294
167M
  txSlot* aSlot;
1295
167M
  switch (theSlot->kind) {
1296
4.02M
  case XS_STRING_KIND:
1297
4.02M
    mxMarkChunk(theSlot->value.string);
1298
4.02M
    break;
1299
0
  case XS_BIGINT_KIND:
1300
0
    mxMarkChunk(theSlot->value.bigint.data);
1301
0
    break;
1302
54.4M
  case XS_REFERENCE_KIND:
1303
54.4M
    aSlot = theSlot->value.reference;
1304
54.4M
    if (!(aSlot->flag & XS_MARK_FLAG))
1305
54.0M
      fxMarkInstance(the, aSlot, fxMarkValue);
1306
54.4M
    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
56.0M
  case XS_ARRAY_KIND:
1322
56.0M
  case XS_STACK_KIND:
1323
56.0M
    fxCheckCStack(the);
1324
56.0M
    if ((aSlot = theSlot->value.array.address)) {
1325
49.2M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
1326
49.2M
      if (!(chunk->size & mxChunkFlag)) {
1327
49.2M
        txIndex aLength = chunk->size / sizeof(txSlot);
1328
49.2M
        if (aLength > theSlot->value.array.length)
1329
0
          aLength = theSlot->value.array.length;
1330
136M
        while (aLength) {
1331
87.3M
          fxMarkValue(the, aSlot);
1332
87.3M
          aSlot++;
1333
87.3M
          aLength--;
1334
87.3M
        }
1335
49.2M
        mxMarkChunk(theSlot->value.array.address);
1336
49.2M
      }
1337
49.2M
    }
1338
56.0M
    break;
1339
150
  case XS_ARRAY_BUFFER_KIND:
1340
150
    if (theSlot->value.arrayBuffer.address)
1341
108
      mxMarkChunk(theSlot->value.arrayBuffer.address);
1342
150
    break;
1343
2.11M
  case XS_CALLBACK_KIND:
1344
2.11M
    aSlot = theSlot->value.callback.closures;
1345
2.11M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1346
0
      fxCheckCStack(the);
1347
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1348
0
    }
1349
2.11M
    break;
1350
0
  case XS_CODE_KIND:
1351
0
    mxMarkChunk(theSlot->value.code.address);
1352
    /* continue */
1353
7.25k
  case XS_CODE_X_KIND:
1354
7.25k
    aSlot = theSlot->value.code.closures;
1355
7.25k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1356
0
      fxCheckCStack(the);
1357
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1358
0
    }
1359
7.25k
    break;
1360
3.62k
  case XS_GLOBAL_KIND:
1361
3.62k
    mxMarkChunk(theSlot->value.table.address);
1362
3.62k
    break;
1363
3.73k
  case XS_HOST_KIND:
1364
3.73k
    if (theSlot->value.host.data) {
1365
107
      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
107
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
1368
107
        mxMarkChunk(theSlot->value.host.data);
1369
107
    }
1370
3.73k
    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
210k
  case XS_ACCESSOR_KIND:
1391
210k
    aSlot = theSlot->value.accessor.getter;
1392
210k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1393
206k
      fxCheckCStack(the);
1394
206k
      fxMarkInstance(the, aSlot, fxMarkValue);
1395
206k
    }
1396
210k
    aSlot = theSlot->value.accessor.setter;
1397
210k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1398
29.0k
      fxCheckCStack(the);
1399
29.0k
      fxMarkInstance(the, aSlot, fxMarkValue);
1400
29.0k
    }
1401
210k
    break;
1402
2.12M
  case XS_HOME_KIND:
1403
2.12M
    aSlot = theSlot->value.home.object;
1404
2.12M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1405
0
      fxCheckCStack(the);
1406
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1407
0
    }
1408
2.12M
    aSlot = theSlot->value.home.module;
1409
2.12M
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1410
0
      fxCheckCStack(the);
1411
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1412
0
    }
1413
2.12M
    break;
1414
0
  case XS_MODULE_KIND:
1415
3.62k
  case XS_PROGRAM_KIND:
1416
3.62k
#if mxKeysGarbageCollection
1417
3.62k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1418
0
      fxMarkID(the, theSlot->value.module.id);
1419
3.62k
#endif
1420
3.62k
    aSlot = theSlot->value.module.realm;
1421
3.62k
    if (aSlot && !(aSlot->flag & XS_MARK_FLAG)) {
1422
3.62k
      fxCheckCStack(the);
1423
3.62k
      fxMarkInstance(the, aSlot, fxMarkValue);
1424
3.62k
    }
1425
3.62k
    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
2.04M
  case XS_KEY_KIND:
1437
2.04M
    if (theSlot->value.key.string)
1438
2.04M
      mxMarkChunk(theSlot->value.key.string);
1439
2.04M
    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
60
  case XS_ERROR_KIND:
1447
60
    aSlot = theSlot->value.error.info;
1448
60
    if (aSlot && (!(aSlot->flag & XS_MARK_FLAG)))
1449
60
      fxMarkInstance(the, aSlot, fxMarkValue);
1450
60
    break;
1451
0
  case XS_ASYNC_DISPOSABLE_STACK_KIND:
1452
0
  case XS_DISPOSABLE_STACK_KIND:
1453
10.8k
  case XS_LIST_KIND:
1454
10.8k
    fxCheckCStack(the);
1455
10.8k
    aSlot = theSlot->value.list.first;
1456
10.8k
    while (aSlot) {
1457
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1458
0
        aSlot->flag |= XS_MARK_FLAG;
1459
0
        fxMarkValue(the, aSlot);
1460
0
      }
1461
0
      aSlot = aSlot->next;
1462
0
    }
1463
10.8k
    break;
1464
    
1465
0
  case XS_PRIVATE_KIND:
1466
0
    fxCheckCStack(the);
1467
0
    aSlot = theSlot->value.private.check;
1468
0
    if (!(aSlot->flag & XS_MARK_FLAG))
1469
0
      fxMarkInstance(the, aSlot, fxMarkValue);
1470
0
    aSlot = theSlot->value.private.first;
1471
0
    while (aSlot) {
1472
0
      aSlot->flag |= XS_MARK_FLAG;
1473
0
      fxMarkValue(the, aSlot);
1474
0
      aSlot = aSlot->next;
1475
0
    }
1476
0
    break;
1477
1478
0
  case XS_MAP_KIND:
1479
0
  case XS_SET_KIND:
1480
0
    {
1481
0
      txSlot** anAddress = theSlot->value.table.address;
1482
0
      txInteger aLength = theSlot->value.table.length;
1483
0
      while (aLength) {
1484
0
        aSlot = *anAddress;
1485
0
        while (aSlot) {
1486
0
          aSlot->flag |= XS_MARK_FLAG; 
1487
0
          aSlot = aSlot->next;
1488
0
        }
1489
0
        anAddress++;
1490
0
        aLength--;
1491
0
      }
1492
0
    }
1493
0
    mxMarkChunk(theSlot->value.table.address);
1494
0
    break;
1495
1496
0
  case XS_WEAK_MAP_KIND:
1497
0
  case XS_WEAK_SET_KIND:
1498
0
    aSlot = theSlot->value.weakList.first;
1499
0
    while (aSlot) {
1500
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1501
0
        aSlot->flag |= XS_MARK_FLAG;
1502
0
        fxMarkValue(the, aSlot);
1503
0
      }
1504
0
      aSlot = aSlot->next;
1505
0
    }
1506
0
    break;
1507
0
  case XS_WEAK_ENTRY_KIND:
1508
0
    aSlot = theSlot->value.weakEntry.check;
1509
0
    if (aSlot->flag & XS_MARK_FLAG) {
1510
0
      aSlot = theSlot->value.weakEntry.value;
1511
0
      if (!(aSlot->flag & XS_MARK_FLAG)) {
1512
0
        aSlot->flag |= XS_MARK_FLAG; 
1513
0
        fxMarkValue(the, aSlot);
1514
0
      }
1515
0
    }
1516
0
    break;
1517
0
  case XS_WEAK_REF_KIND:
1518
0
    aSlot = theSlot->value.weakRef.target;
1519
0
    if (aSlot) {
1520
0
  #ifdef mxSnapshot
1521
0
      if (the->collectFlag & XS_ORGANIC_FLAG) {
1522
0
        fxMarkValue(the, aSlot);
1523
0
      }
1524
0
      else {
1525
0
        theSlot->value.weakRef.link = the->firstWeakRefLink;
1526
0
        the->firstWeakRefLink = theSlot;
1527
0
      }
1528
  #else
1529
      theSlot->value.weakRef.link = the->firstWeakRefLink;
1530
      the->firstWeakRefLink = theSlot;
1531
  #endif
1532
0
    }
1533
0
    break;
1534
0
  case XS_FINALIZATION_REGISTRY_KIND:
1535
0
    aSlot = theSlot->value.finalizationRegistry.callback;
1536
0
    if (aSlot) {
1537
0
      aSlot->flag |= XS_MARK_FLAG;
1538
0
      fxMarkValue(the, aSlot);
1539
0
      aSlot = aSlot->next;
1540
0
      while (aSlot) {
1541
0
        aSlot->flag |= XS_MARK_FLAG;
1542
0
                fxCheckCStack(the);
1543
0
        fxMarkValue(the, aSlot); // holdings
1544
0
        aSlot = aSlot->next;
1545
0
        if (aSlot) {
1546
0
          aSlot->flag |= XS_MARK_FLAG;
1547
          // weak target and token
1548
0
          aSlot = aSlot->next;
1549
0
        }
1550
0
      }
1551
0
    }
1552
0
    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
108k
  case XS_SYMBOL_KIND:
1562
108k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG) {
1563
0
      if (!(theSlot->flag & XS_INTERNAL_FLAG))
1564
0
        fxMarkID(the, theSlot->value.symbol);
1565
0
    }
1566
108k
    break; 
1567
314k
  case XS_AT_KIND:
1568
314k
    if (the->collectFlag & XS_COLLECT_KEYS_FLAG)
1569
0
      fxMarkID(the, theSlot->value.at.id);
1570
314k
    break; 
1571
167M
#endif
1572
167M
  }
1573
167M
}
1574
1575
void fxMarkWeakStuff(txMachine* the) 
1576
13.4k
{
1577
13.4k
  txSlot* slot;
1578
13.4k
  txSlot** address;
1579
1580
13.4k
  {
1581
13.4k
    txSlot* list;
1582
13.4k
    txSlot** listAddress = &the->firstWeakListLink;
1583
13.4k
    while ((list = *listAddress)) {
1584
0
      if (list->flag & XS_MARK_FLAG) {
1585
0
        txSlot* listEntry;
1586
0
        txSlot** listEntryAddress = &list->value.weakList.first;
1587
0
        while ((listEntry = *listEntryAddress)) {
1588
0
          txSlot* value = listEntry->value.weakEntry.value;
1589
0
          if ((value->flag & XS_MARK_FLAG) && (value->kind != XS_UNINITIALIZED_KIND)) {
1590
0
            listEntryAddress = &listEntry->next;
1591
0
          }
1592
0
          else {
1593
0
            listEntry->flag &= ~XS_MARK_FLAG;
1594
0
            *listEntryAddress = listEntry->next;
1595
0
          }
1596
0
        }
1597
0
        listAddress = &list->value.weakList.link;
1598
0
      }
1599
0
      else {
1600
0
        txSlot* listEntry = list->value.weakList.first;
1601
0
        while (listEntry) {
1602
0
          txSlot* key = listEntry->value.weakEntry.check;
1603
0
          if (key->flag & XS_MARK_FLAG) {
1604
0
            txSlot* keyEntry;
1605
0
            txSlot** keyEntryAddress = &key->next;
1606
0
            while ((keyEntry = *keyEntryAddress)) {
1607
0
              if (!(keyEntry->flag & XS_INTERNAL_FLAG))
1608
0
                break;
1609
0
              if ((keyEntry->kind == XS_WEAK_ENTRY_KIND) && (keyEntry->value.weakEntry.check == list)) {
1610
0
                keyEntry->flag &= ~XS_MARK_FLAG;
1611
0
                *keyEntryAddress = keyEntry->next;
1612
0
                break;
1613
0
              }
1614
0
              keyEntryAddress = &keyEntry->next;
1615
0
            }
1616
0
          }
1617
0
          listEntry = listEntry->next;
1618
0
        }
1619
0
        *listAddress = list->value.weakList.link;
1620
0
      }
1621
0
    }
1622
13.4k
  }
1623
13.4k
  address = &the->firstWeakRefLink;
1624
13.4k
  while ((slot = *address)) {
1625
0
    if (!(slot->value.weakRef.target->flag & XS_MARK_FLAG))
1626
0
      slot->value.weakRef.target = C_NULL;
1627
0
    *address = C_NULL;
1628
0
    address = &(slot->value.weakRef.link);
1629
0
  }
1630
  
1631
13.4k
  if (mxFinalizationRegistries.kind == XS_REFERENCE_KIND) {
1632
13.4k
    slot = mxFinalizationRegistries.value.reference->next;
1633
13.4k
    while (slot) {
1634
0
      fxMarkFinalizationRegistry(the, slot->value.closure);
1635
0
      slot = slot->next;
1636
0
    }
1637
13.4k
  }
1638
13.4k
}
1639
1640
txSize fxMultiplyChunkSizes(txMachine* the, txSize a, txSize b)
1641
6.96M
{
1642
6.96M
  txSize c;
1643
6.96M
#if __has_builtin(__builtin_mul_overflow)
1644
6.96M
  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
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1651
0
  }
1652
6.96M
  return c;
1653
6.96M
}
1654
1655
void* fxNewChunk(txMachine* the, txSize size)
1656
15.2M
{
1657
15.2M
  txSize offset = size;
1658
15.2M
  txChunk* chunk;
1659
15.2M
  txBoolean once = 1;
1660
15.2M
  size = fxAdjustChunkSize(the, size);
1661
15.2M
  chunk = fxFindChunk(the, size, &once);
1662
15.2M
  if (!chunk) {
1663
675
    chunk = fxGrowChunk(the, size);
1664
675
  }
1665
15.2M
#ifdef mxMetering
1666
15.2M
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1667
15.2M
#endif
1668
15.2M
  return fxCheckChunk(the, chunk, size, offset);
1669
15.2M
}
1670
1671
void* fxNewGrowableChunk(txMachine* the, txSize size, txSize capacity)
1672
0
{
1673
#if mxNoChunks
1674
  return fxNewChunk(the, size);
1675
#else
1676
0
  txSize offset = size;
1677
0
  txChunk* chunk;
1678
0
  txBoolean once = 1;
1679
0
  size = fxAdjustChunkSize(the, size);
1680
0
  capacity = fxAdjustChunkSize(the, capacity);
1681
0
  chunk = fxFindChunk(the, capacity, &once);
1682
0
  if (!chunk) {
1683
0
    chunk = fxGrowChunk(the, capacity);
1684
0
    if (!chunk) {
1685
0
      chunk = fxFindChunk(the, size, &once);
1686
0
      if (!chunk) {
1687
0
        chunk = fxGrowChunk(the, size);
1688
0
      }
1689
0
    }
1690
0
  }
1691
0
#ifdef mxMetering
1692
0
  the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1693
0
#endif
1694
0
  return fxCheckChunk(the, chunk, size, offset);
1695
0
#endif
1696
0
}
1697
1698
txSlot* fxNewSlot(txMachine* the) 
1699
62.9M
{
1700
62.9M
  txSlot* aSlot;
1701
62.9M
  txBoolean once = 1, allocate;
1702
  
1703
62.9M
#if mxStress
1704
62.9M
  if (fxShouldStress()) {
1705
0
    fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
1706
0
    once = 0;
1707
0
  }
1708
62.9M
#endif
1709
62.9M
again:
1710
62.9M
  aSlot = the->freeHeap;
1711
62.9M
  if (aSlot) {
1712
62.9M
    the->freeHeap = aSlot->next;
1713
62.9M
    aSlot->next = C_NULL;
1714
62.9M
    aSlot->ID = XS_NO_ID;
1715
62.9M
    aSlot->flag = XS_NO_FLAG;
1716
62.9M
  #ifdef mxSnapshot
1717
62.9M
    #if mx32bitID
1718
62.9M
      aSlot->dummy = 0;
1719
    #elif INTPTR_MAX == INT64_MAX
1720
      aSlot->dummy = 0;
1721
    #endif
1722
62.9M
  #endif
1723
#if mxPoisonSlots
1724
    ASAN_UNPOISON_MEMORY_REGION(&aSlot->value, sizeof(aSlot->value));
1725
#endif
1726
62.9M
    the->currentHeapCount++;
1727
62.9M
    if (the->peakHeapCount < the->currentHeapCount)
1728
55.9M
      the->peakHeapCount = the->currentHeapCount;
1729
62.9M
#ifdef mxMetering
1730
62.9M
    the->meterIndex += XS_SLOT_ALLOCATION_METERING;
1731
62.9M
#endif
1732
62.9M
    return aSlot;
1733
62.9M
  }
1734
1.41k
  if (once) {
1735
1.36k
    txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0), isThrashing;
1736
1737
1.36k
    fxCollect(the, XS_ORGANIC_FLAG);
1738
1739
1.36k
    isThrashing = ((the->collectFlag & XS_TRASHING_SLOTS_FLAG) != 0);
1740
1.36k
    allocate = wasThrashing && isThrashing;
1741
1742
1.36k
    once = 0;
1743
1.36k
  }
1744
54
  else
1745
54
    allocate = 1;
1746
1.41k
  if (allocate) {
1747
741
    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
741
    fxGrowSlots(the, !(the->collectFlag & XS_SKIPPED_COLLECT_FLAG) ? the->minimumHeapCount : 64);
1752
741
  }
1753
1.41k
  goto again;
1754
0
  return C_NULL;
1755
62.9M
}
1756
1757
void* fxRenewChunk(txMachine* the, void* theData, txSize size)
1758
6.68k
{
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
6.68k
  txByte* aData = ((txByte*)theData) - sizeof(txChunk);
1770
6.68k
  txChunk* aChunk = (txChunk*)aData;
1771
6.68k
  txSize capacity = (txSize)(aChunk->temporary - aData);
1772
6.68k
  txBlock* aBlock = the->firstBlock;
1773
6.68k
  size = fxAdjustChunkSize(the, size);
1774
6.68k
  if (size <= capacity) {
1775
0
    the->currentChunksSize += size - aChunk->size;
1776
0
    if (the->peakChunksSize < the->currentChunksSize)
1777
0
      the->peakChunksSize = the->currentChunksSize;
1778
0
    aChunk->size = size;
1779
0
  #ifdef mxMetering
1780
0
    the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1781
0
  #endif
1782
0
    return theData;
1783
0
  }
1784
18.1k
  while (aBlock) {
1785
11.5k
    if (aChunk->temporary == aBlock->current) {
1786
85
      txSize delta = size - capacity;
1787
85
      if (aBlock->current + delta <= aBlock->limit) {
1788
74
        the->currentChunksSize += size - aChunk->size;
1789
74
        if (the->peakChunksSize < the->currentChunksSize)
1790
36
          the->peakChunksSize = the->currentChunksSize;
1791
74
        aBlock->current += delta;
1792
74
        aChunk->temporary = aBlock->current;
1793
74
        aChunk->size = size;
1794
74
      #ifdef mxSnapshot
1795
74
        c_memset(aData + capacity, 0, delta);
1796
74
      #endif
1797
74
      #ifdef mxMetering
1798
74
        the->meterIndex += size * XS_CHUNK_ALLOCATION_METERING;
1799
74
      #endif
1800
74
        return theData;
1801
74
      }
1802
11
      else {
1803
11
        return C_NULL;
1804
11
      }
1805
85
    }
1806
11.5k
    aBlock = aBlock->nextBlock;
1807
11.5k
  }
1808
6.59k
  return C_NULL;
1809
6.68k
#endif
1810
6.68k
}
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
3.62k
{
1884
3.62k
  txSize aTotal;
1885
#if mxNoChunks
1886
  txChunk** address;
1887
  txChunk* chunk;
1888
#else
1889
3.62k
  txBlock* aBlock;
1890
3.62k
  txByte* limit;
1891
3.62k
  txByte* next;
1892
3.62k
#endif
1893
3.62k
  txByte* current;
1894
3.62k
  txByte* temporary;
1895
3.62k
  txSize aSize;
1896
3.62k
  txByte** aCodeAddress;
1897
3.62k
  txSlot* aSlot;
1898
3.62k
  txSlot* bSlot;
1899
3.62k
  txSlot* cSlot;
1900
3.62k
  txSlot* freeSlot;
1901
3.62k
  txJump* jump;
1902
1903
#ifdef mxNever
1904
  startTime(&gxSweepChunkTime);
1905
#endif
1906
1907
3.62k
  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
3.62k
  aBlock = the->firstBlock;
1930
12.4k
  while (aBlock) {
1931
8.80k
    current = ((txByte*)aBlock) + sizeof(txBlock);
1932
8.80k
    limit = aBlock->current;
1933
8.80k
    temporary = current;
1934
58.5M
    while (current < limit) {
1935
58.5M
      aSize = ((txChunk*)current)->size;
1936
58.5M
      next = ((txChunk*)current)->temporary;
1937
58.5M
      if (aSize & mxChunkFlag) {
1938
53.4M
        aSize &= ~mxChunkFlag;
1939
53.4M
        ((txChunk*)current)->temporary = temporary;
1940
53.4M
        temporary += aSize;
1941
53.4M
        aTotal += aSize;
1942
53.4M
      }
1943
5.11M
      else {
1944
5.11M
        ((txChunk*)current)->temporary = C_NULL;
1945
5.11M
      }
1946
58.5M
      ((txChunk*)current)->size = (txSize)(next - current);
1947
58.5M
      current = next;
1948
58.5M
    }  
1949
8.80k
    aBlock->temporary = temporary;
1950
8.80k
    aBlock = aBlock->nextBlock;
1951
8.80k
  }
1952
3.62k
#endif
1953
3.62k
  the->currentChunksSize = aTotal;
1954
1955
3.62k
  aCodeAddress = &(the->code);
1956
3.62k
  aSlot = the->frame;
1957
10.8k
  while (aSlot) {
1958
7.21k
    mxCheck(the, aSlot->kind == XS_FRAME_KIND);
1959
7.21k
    if ((aSlot->flag & XS_C_FLAG) == 0) {
1960
0
      bSlot = (aSlot + 3)->value.reference->next;
1961
0
      if (bSlot->kind == XS_CODE_KIND) {
1962
0
        current = bSlot->value.code.address;
1963
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1964
0
        if (temporary) {
1965
0
          temporary += sizeof(txChunk);
1966
0
          *aCodeAddress += temporary - current;
1967
0
        }
1968
0
      }
1969
0
    }
1970
7.21k
    else {
1971
7.21k
      current = *aCodeAddress;
1972
7.21k
      if (current) {
1973
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1974
0
        if (temporary)
1975
0
          *aCodeAddress = temporary + sizeof(txChunk);
1976
0
      }
1977
7.21k
    }
1978
7.21k
    aCodeAddress = &(aSlot->value.frame.code);
1979
7.21k
    aSlot = aSlot->next;
1980
7.21k
  }
1981
  
1982
3.62k
  jump = the->firstJump;
1983
14.5k
  while (jump) {
1984
10.8k
    if (jump->flag) {
1985
0
      aSlot = jump->frame;
1986
0
      bSlot = (aSlot + 3)->value.reference->next;
1987
0
      if (bSlot->kind == XS_CODE_KIND) {
1988
0
        current = bSlot->value.code.address;
1989
0
        temporary = (txByte*)(((txChunk*)(current - sizeof(txChunk)))->temporary);
1990
0
        if (temporary) {
1991
0
          temporary += sizeof(txChunk);
1992
0
          jump->code += temporary - current;
1993
0
        }
1994
0
      }
1995
0
    }
1996
10.8k
    else {
1997
10.8k
      current = jump->code;
1998
10.8k
      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
10.8k
    }
2004
10.8k
    jump = jump->nextJump;
2005
10.8k
  }
2006
  
2007
3.62k
  aSlot = the->stack;
2008
5.74M
  while (aSlot < the->stackTop) {
2009
5.74M
    fxSweepValue(the, aSlot);
2010
5.74M
    aSlot++;
2011
5.74M
  }
2012
3.62k
  aSlot = the->cRoot;
2013
3.62k
  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
3.62k
  aTotal = 0;
2024
3.62k
  freeSlot = C_NULL;
2025
3.62k
  aSlot = the->firstHeap;
2026
10.9k
  while (aSlot) {
2027
7.33k
    bSlot = aSlot + 1;
2028
7.33k
    cSlot = aSlot->value.reference;
2029
240M
    while (bSlot < cSlot) {
2030
240M
      if (bSlot->flag & XS_MARK_FLAG) {
2031
138M
        bSlot->flag &= ~XS_MARK_FLAG; 
2032
138M
        fxSweepValue(the, bSlot);
2033
138M
        aTotal++;
2034
138M
      }
2035
102M
      else {
2036
102M
      #ifndef mxLink
2037
102M
        if (bSlot->kind == XS_HOST_KIND) {
2038
477
          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
477
          else if (bSlot->value.host.variant.destructor)
2043
0
            (*(bSlot->value.host.variant.destructor))(bSlot->value.host.data);
2044
477
        }
2045
102M
      #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
102M
        bSlot->kind = XS_UNDEFINED_KIND;
2055
102M
        bSlot->next = freeSlot;
2056
      #if mxPoisonSlots
2057
        ASAN_POISON_MEMORY_REGION(&bSlot->value, sizeof(bSlot->value));
2058
      #endif
2059
102M
        freeSlot = bSlot;
2060
102M
      }
2061
240M
      bSlot++;
2062
240M
    }
2063
7.33k
    aSlot = aSlot->next;
2064
7.33k
  }
2065
3.62k
  the->currentHeapCount = aTotal;
2066
3.62k
  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
3.62k
  aBlock = the->firstBlock;
2087
12.4k
  while (aBlock) {
2088
8.80k
    txByte* former = C_NULL;
2089
8.80k
    current = ((txByte*)aBlock) + sizeof(txBlock);
2090
8.80k
    limit = aBlock->current;
2091
58.5M
    while (current < limit) {
2092
58.5M
      aSize = ((txChunk*)current)->size;
2093
58.5M
      next = current + aSize;
2094
58.5M
      if ((temporary = ((txChunk*)current)->temporary)) {
2095
53.4M
        if (former) {
2096
53.3M
          ((txChunk*)former)->temporary = temporary;
2097
53.3M
          ((txChunk*)former)->size = (txSize)(temporary - former);
2098
53.3M
        }
2099
53.4M
        if (temporary != current)
2100
2.58M
          c_memmove(temporary, current, aSize);
2101
53.4M
        former = temporary;
2102
53.4M
      }
2103
58.5M
      current = next;
2104
58.5M
    }
2105
8.80k
    if (former) {
2106
8.69k
      ((txChunk*)former)->temporary = aBlock->temporary;
2107
8.69k
      ((txChunk*)former)->size = (txSize)(aBlock->temporary - former);
2108
8.69k
    }
2109
8.80k
    aBlock->current = aBlock->temporary;
2110
8.80k
    aBlock->temporary = C_NULL;
2111
8.80k
    aBlock = aBlock->nextBlock;
2112
8.80k
  }
2113
3.62k
#endif
2114
  
2115
#ifdef mxNever
2116
  stopTime(&gxCompactChunkTime);
2117
#endif
2118
3.62k
}
2119
2120
void fxSweepValue(txMachine* the, txSlot* theSlot)
2121
231M
{
2122
231M
  txSlot* aSlot;
2123
231M
  txByte* data;
2124
  
2125
231M
#define mxSweepChunk(_THE_DATA, _THE_DATA_TYPE) \
2126
231M
  if ((data = (txByte*)(((txChunk*)(((txByte*)(_THE_DATA)) - sizeof(txChunk)))->temporary))) \
2127
55.2M
    ((_THE_DATA)) = (_THE_DATA_TYPE)(data + sizeof(txChunk))
2128
2129
231M
  switch (theSlot->kind) {
2130
4.02M
  case XS_STRING_KIND:
2131
4.02M
    mxSweepChunk(theSlot->value.string, txString);
2132
4.02M
    break;
2133
0
  case XS_BIGINT_KIND:
2134
0
    mxSweepChunk(theSlot->value.bigint.data, txU4*);
2135
0
    break;
2136
2137
0
  case XS_ARGUMENTS_SLOPPY_KIND:
2138
0
  case XS_ARGUMENTS_STRICT_KIND:
2139
56.0M
  case XS_ARRAY_KIND:
2140
56.0M
  case XS_STACK_KIND:
2141
56.0M
    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
49.2M
      txChunk* chunk = (txChunk*)(((txByte*)aSlot) - sizeof(txChunk));
2147
49.2M
      txIndex aLength = chunk->size / sizeof(txSlot);
2148
49.2M
      if (aLength > theSlot->value.array.length)
2149
0
        aLength = theSlot->value.array.length;
2150
136M
      while (aLength) {
2151
87.3M
        fxSweepValue(the, aSlot);
2152
87.3M
        aSlot++;
2153
87.3M
        aLength--;
2154
87.3M
      }
2155
#if mxNoChunks
2156
#else
2157
49.2M
      mxSweepChunk(theSlot->value.array.address, txSlot*);
2158
49.2M
#endif
2159
49.2M
    }
2160
56.0M
    break;
2161
150
  case XS_ARRAY_BUFFER_KIND:
2162
150
    if (theSlot->value.arrayBuffer.address)
2163
108
      mxSweepChunk(theSlot->value.arrayBuffer.address, txByte*);
2164
150
    break;
2165
0
  case XS_CODE_KIND:
2166
0
    mxSweepChunk(theSlot->value.code.address, txByte*);
2167
0
    break;
2168
3.62k
  case XS_GLOBAL_KIND:
2169
3.62k
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2170
3.62k
    break;
2171
3.73k
  case XS_HOST_KIND:
2172
3.73k
    if (theSlot->value.host.data) {
2173
107
      if (theSlot->flag & XS_HOST_CHUNK_FLAG)
2174
107
        mxSweepChunk(theSlot->value.host.data, void*);
2175
107
    }
2176
3.73k
    break;
2177
0
  case XS_IDS_KIND:
2178
0
    if (theSlot->value.IDs)
2179
0
      mxSweepChunk(theSlot->value.IDs, txID*);
2180
0
    break;
2181
0
  case XS_REGEXP_KIND:
2182
0
    if (theSlot->value.regexp.code)
2183
0
      mxSweepChunk(theSlot->value.regexp.code, void*);
2184
0
    if (theSlot->value.regexp.data)
2185
0
      mxSweepChunk(theSlot->value.regexp.data, void*);
2186
0
    break;
2187
2.04M
  case XS_KEY_KIND:
2188
2.04M
    if (theSlot->value.key.string)
2189
2.04M
      mxSweepChunk(theSlot->value.key.string, txString);
2190
2.04M
    break;
2191
0
  case XS_MAP_KIND:
2192
0
  case XS_SET_KIND:
2193
0
    mxSweepChunk(theSlot->value.table.address, txSlot**);
2194
0
    break;
2195
231M
  }
2196
231M
}