MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / fxFindChunk

Function fxFindChunk

xs/sources/xsMemory.c:439–486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

437}
438
439void* fxFindChunk(txMachine* the, txSize size, txBoolean *once)
440{
441 txBlock* block;
442 txChunk* chunk;
443#if mxStress
444 if (fxShouldStress()) {
445 if (*once) {
446 fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
447 *once = 0;
448 }
449 }
450#endif
451#if mxNoChunks
452 if ((the->currentChunksSize + size > the->maximumChunksSize)) {
453 fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
454 if (the->collectFlag & XS_TRASHING_CHUNKS_FLAG)
455 the->maximumChunksSize += the->minimumChunksSize;
456 }
457 chunk = c_malloc_noforcefail(size);
458 if (!chunk)
459 fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
460 chunk->size = size;
461 chunk->temporary = (txByte*)the->firstBlock;
462 the->firstBlock = (txBlock*)chunk;
463 return chunk;
464#endif
465again:
466 block = the->firstBlock;
467 while (block) {
468 if ((block->current + size) <= block->limit) {
469 chunk = (txChunk*)(block->current);
470 block->current += size;
471 chunk->temporary = block->current;
472 return chunk;
473 }
474 block = block->nextBlock;
475 }
476 if (*once) {
477 txBoolean wasThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0), isThrashing;
478 fxCollect(the, XS_COMPACT_FLAG | XS_ORGANIC_FLAG);
479 isThrashing = ((the->collectFlag & XS_TRASHING_CHUNKS_FLAG) != 0);
480 *once = 0;
481 if (wasThrashing && isThrashing)
482 return C_NULL;
483 goto again;
484 }
485 return C_NULL;
486}
487
488txSlot* fxFindKey(txMachine* the)
489{

Callers 2

fxNewChunkFunction · 0.85
fxNewGrowableChunkFunction · 0.85

Calls 3

fxShouldStressFunction · 0.85
fxCollectFunction · 0.85
fxAbortFunction · 0.70

Tested by

no test coverage detected