| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #include "CommonMemoryPch.h"
- #define ASSERT_THREAD() AssertMsg(this->pageAllocator->ValidThreadAccess(), "Arena allocation should only be used by a single thread")
- const uint Memory::StandAloneFreeListPolicy::MaxEntriesGrowth;
- template __forceinline BVSparseNode * BVSparse<JitArenaAllocator>::NodeFromIndex(BVIndex i, BVSparseNode *** prevNextFieldOut, bool create);
- ArenaData::ArenaData(PageAllocator * pageAllocator) :
- pageAllocator(pageAllocator),
- bigBlocks(nullptr),
- mallocBlocks(nullptr),
- fullBlocks(nullptr),
- cacheBlockCurrent(nullptr),
- lockBlockList(false)
- {
- }
- void ArenaData::UpdateCacheBlock() const
- {
- if (bigBlocks != nullptr)
- {
- size_t currentByte = (cacheBlockCurrent - bigBlocks->GetBytes());
- // Avoid writing to the page unnecessary, it might be write watched
- if (currentByte != bigBlocks->currentByte)
- {
- bigBlocks->currentByte = currentByte;
- }
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- ArenaAllocatorBase(__in LPCWSTR name, PageAllocator * pageAllocator, void(*outOfMemoryFunc)(), void(*recoverMemoryFunc)()) :
- Allocator(outOfMemoryFunc, recoverMemoryFunc),
- ArenaData(pageAllocator),
- #ifdef ARENA_ALLOCATOR_FREE_LIST_SIZE
- freeListSize(0),
- #endif
- freeList(nullptr),
- largestHole(0),
- cacheBlockEnd(nullptr),
- blockState(0)
- {
- #ifdef PROFILE_MEM
- this->name = name;
- LogBegin();
- #endif
- ArenaMemoryTracking::ArenaCreated(this, name);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- ~ArenaAllocatorBase()
- {
- Assert(!lockBlockList);
- ArenaMemoryTracking::ReportFreeAll(this);
- ArenaMemoryTracking::ArenaDestroyed(this);
- if (!pageAllocator->IsClosed())
- {
- ReleasePageMemory();
- }
- ReleaseHeapMemory();
- TFreeListPolicy::Release(this->freeList);
- #ifdef PROFILE_MEM
- LogEnd();
- #endif
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- Move(ArenaAllocatorBase *srcAllocator)
- {
- Assert(!lockBlockList);
- Assert(srcAllocator != nullptr);
- Allocator::Move(srcAllocator);
- Assert(this->pageAllocator == srcAllocator->pageAllocator);
- AllocatorFieldMove(this, srcAllocator, bigBlocks);
- AllocatorFieldMove(this, srcAllocator, largestHole);
- AllocatorFieldMove(this, srcAllocator, cacheBlockCurrent);
- AllocatorFieldMove(this, srcAllocator, cacheBlockEnd);
- AllocatorFieldMove(this, srcAllocator, mallocBlocks);
- AllocatorFieldMove(this, srcAllocator, fullBlocks);
- AllocatorFieldMove(this, srcAllocator, blockState);
- AllocatorFieldMove(this, srcAllocator, freeList);
- #ifdef PROFILE_MEM
- this->name = srcAllocator->name;
- srcAllocator->name = nullptr;
- AllocatorFieldMove(this, srcAllocator, memoryData);
- #endif
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- size_t
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- AllocatedSize(ArenaMemoryBlock * blockList)
- {
- ArenaMemoryBlock * memoryBlock = blockList;
- size_t totalBytes = 0;
- while (memoryBlock != NULL)
- {
- totalBytes += memoryBlock->nbytes;
- memoryBlock = memoryBlock->next;
- }
- return totalBytes;
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- size_t
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- AllocatedSize()
- {
- UpdateCacheBlock();
- return AllocatedSize(this->fullBlocks) + AllocatedSize(this->bigBlocks) + AllocatedSize(this->mallocBlocks);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- size_t
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- Size()
- {
- UpdateCacheBlock();
- return Size(this->fullBlocks) + Size(this->bigBlocks) + AllocatedSize(this->mallocBlocks);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- size_t
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- Size(BigBlock * blockList)
- {
- BigBlock * memoryBlock = blockList;
- size_t totalBytes = 0;
- while (memoryBlock != NULL)
- {
- totalBytes += memoryBlock->currentByte;
- memoryBlock = (BigBlock *)memoryBlock->next;
- }
- return totalBytes;
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- char *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- RealAlloc(size_t nbytes)
- {
- return RealAllocInlined(nbytes);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- char *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- RealAllocInlined(size_t nbytes)
- {
- Assert(nbytes != 0);
- Assert((nbytes & (ObjectAlignment - 1)) == 0);
- #ifdef ARENA_MEMORY_VERIFY
- if (Js::Configuration::Global.flags.ArenaUseHeapAlloc)
- {
- return AllocFromHeap<true>(nbytes);
- }
- #endif
- Assert(cacheBlockEnd >= cacheBlockCurrent);
- char * p = cacheBlockCurrent;
- if ((size_t)(cacheBlockEnd - p) >= nbytes)
- {
- Assert(cacheBlockEnd == bigBlocks->GetBytes() + bigBlocks->nbytes);
- Assert(bigBlocks->GetBytes() <= cacheBlockCurrent && cacheBlockCurrent <= cacheBlockEnd);
- cacheBlockCurrent = p + nbytes;
- ArenaMemoryTracking::ReportAllocation(this, p, nbytes);
- return(p);
- }
- return SnailAlloc(nbytes);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- SetCacheBlock(BigBlock * newCacheBlock)
- {
- if (bigBlocks != nullptr)
- {
- Assert(cacheBlockEnd == bigBlocks->GetBytes() + bigBlocks->nbytes);
- Assert(bigBlocks->GetBytes() <= cacheBlockCurrent && cacheBlockCurrent <= cacheBlockEnd);
- bigBlocks->currentByte = (cacheBlockCurrent - bigBlocks->GetBytes());
- uint cacheBlockRemainBytes = (uint)(cacheBlockEnd - cacheBlockCurrent);
- if (cacheBlockRemainBytes < ObjectAlignment && !lockBlockList)
- {
- BigBlock * cacheBlock = bigBlocks;
- bigBlocks = bigBlocks->nextBigBlock;
- cacheBlock->next = fullBlocks;
- fullBlocks = cacheBlock;
- }
- else
- {
- largestHole = max(largestHole, static_cast<size_t>(cacheBlockRemainBytes));
- }
- }
- cacheBlockCurrent = newCacheBlock->GetBytes() + newCacheBlock->currentByte;
- cacheBlockEnd = newCacheBlock->GetBytes() + newCacheBlock->nbytes;
- newCacheBlock->nextBigBlock = bigBlocks;
- bigBlocks = newCacheBlock;
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- char *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- SnailAlloc(size_t nbytes)
- {
- BigBlock* blockp = NULL;
- size_t currentLargestHole = 0;
- if (nbytes <= largestHole)
- {
- Assert(bigBlocks != nullptr);
- Assert(cacheBlockEnd == bigBlocks->GetBytes() + bigBlocks->nbytes);
- Assert(bigBlocks->GetBytes() <= cacheBlockCurrent && cacheBlockCurrent <= cacheBlockEnd);
- BigBlock * cacheBlock = bigBlocks;
- BigBlock** pPrev= &(bigBlocks->nextBigBlock);
- blockp = bigBlocks->nextBigBlock;
- int giveUpAfter = 10;
- do
- {
- size_t remainingBytes = blockp->nbytes - blockp->currentByte;
- if (remainingBytes >= nbytes)
- {
- char *p = blockp->GetBytes() + blockp->currentByte;
- blockp->currentByte += nbytes;
- if (remainingBytes == largestHole || currentLargestHole > largestHole)
- {
- largestHole = currentLargestHole;
- }
- remainingBytes -= nbytes;
- if (remainingBytes > cacheBlock->nbytes - cacheBlock->currentByte)
- {
- *pPrev = blockp->nextBigBlock;
- SetCacheBlock(blockp);
- }
- else if (remainingBytes < ObjectAlignment && !lockBlockList)
- {
- *pPrev = blockp->nextBigBlock;
- blockp->nextBigBlock = fullBlocks;
- fullBlocks = blockp;
- }
- ArenaMemoryTracking::ReportAllocation(this, p, nbytes);
- return(p);
- }
- currentLargestHole = max(currentLargestHole, remainingBytes);
- if (--giveUpAfter == 0)
- {
- break;
- }
- pPrev = &(blockp->nextBigBlock);
- blockp = blockp->nextBigBlock;
- }
- while (blockp != nullptr);
- }
- blockp = AddBigBlock(nbytes);
- if (blockp == nullptr)
- {
- return AllocFromHeap<false>(nbytes); // Passing DoRecoverMemory=false as we already tried recovering memory in AddBigBlock, and it is costly.
- }
- this->blockState++;
- SetCacheBlock(blockp);
- char *p = cacheBlockCurrent;
- Assert(p + nbytes <= cacheBlockEnd);
- cacheBlockCurrent += nbytes;
- ArenaMemoryTracking::ReportAllocation(this, p, nbytes);
- return(p);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- template <bool DoRecoverMemory>
- char *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- AllocFromHeap(size_t requestBytes)
- {
- size_t allocBytes = AllocSizeMath::Add(requestBytes, sizeof(ArenaMemoryBlock));
- ARENA_FAULTINJECT_MEMORY(this->name, requestBytes);
- char * buffer = HeapNewNoThrowArray(char, allocBytes);
- if (buffer == nullptr)
- {
- if (DoRecoverMemory && recoverMemoryFunc)
- {
- // Try to recover some memory and see if after that we can allocate.
- recoverMemoryFunc();
- buffer = HeapNewNoThrowArray(char, allocBytes);
- }
- if (buffer == nullptr)
- {
- if (outOfMemoryFunc)
- {
- outOfMemoryFunc();
- }
- return nullptr;
- }
- }
- ArenaMemoryBlock * memoryBlock = (ArenaMemoryBlock *)buffer;
- memoryBlock->nbytes = requestBytes;
- memoryBlock->next = this->mallocBlocks;
- this->mallocBlocks = memoryBlock;
- this->blockState = 2; // set the block state to 2 to disable the reset fast path.
- ArenaMemoryTracking::ReportAllocation(this, buffer + sizeof(ArenaMemoryBlock), requestBytes);
- return buffer + sizeof(ArenaMemoryBlock);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- BigBlock *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- AddBigBlock(size_t requestBytes)
- {
- FAULTINJECT_MEMORY_NOTHROW(this->name, requestBytes);
- size_t allocBytes = AllocSizeMath::Add(requestBytes, sizeof(BigBlock));
- PageAllocation * allocation = this->GetPageAllocator()->AllocPagesForBytes(allocBytes);
- if (allocation == nullptr)
- {
- // Try to recover some memory and see if after that we can allocate.
- if (recoverMemoryFunc)
- {
- recoverMemoryFunc();
- allocation = this->GetPageAllocator()->AllocPagesForBytes(allocBytes);
- }
- if (allocation == nullptr)
- {
- return nullptr;
- }
- }
- BigBlock * blockp = (BigBlock *)allocation->GetAddress();
- blockp->allocation = allocation;
- blockp->nbytes = allocation->GetSize() - sizeof(BigBlock);
- blockp->currentByte = 0;
- #ifdef PROFILE_MEM
- LogRealAlloc(allocation->GetSize() + sizeof(PageAllocation));
- #endif
- return(blockp);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- FullReset()
- {
- BigBlock * initBlock = this->bigBlocks;
- if (initBlock != nullptr)
- {
- this->bigBlocks = initBlock->nextBigBlock;
- }
- Clear();
- if (initBlock != nullptr)
- {
- this->blockState = 1;
- initBlock->currentByte = 0;
- SetCacheBlock(initBlock);
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- ReleaseMemory()
- {
- ReleasePageMemory();
- ReleaseHeapMemory();
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- ReleasePageMemory()
- {
- pageAllocator->SuspendIdleDecommit();
- #ifdef ARENA_MEMORY_VERIFY
- bool reenableDisablePageReuse = false;
- if (Js::Configuration::Global.flags.ArenaNoPageReuse)
- {
- reenableDisablePageReuse = !pageAllocator->DisablePageReuse();
- }
- #endif
- BigBlock *blockp = bigBlocks;
- while (blockp != NULL)
- {
- PageAllocation * allocation = blockp->allocation;
- blockp = blockp->nextBigBlock;
- GetPageAllocator()->ReleaseAllocationNoSuspend(allocation);
- }
- blockp = fullBlocks;
- while (blockp != NULL)
- {
- PageAllocation * allocation = blockp->allocation;
- blockp = blockp->nextBigBlock;
- GetPageAllocator()->ReleaseAllocationNoSuspend(allocation);
- }
- #ifdef ARENA_MEMORY_VERIFY
- if (reenableDisablePageReuse)
- {
- pageAllocator->ReenablePageReuse();
- }
- #endif
- pageAllocator->ResumeIdleDecommit();
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- ReleaseHeapMemory()
- {
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- ArenaMemoryBlock * next = memoryBlock->next;
- HeapDeleteArray(memoryBlock->nbytes + sizeof(ArenaMemoryBlock), (char *)memoryBlock);
- memoryBlock = next;
- }
- }
- template __forceinline char *ArenaAllocatorBase<InPlaceFreeListPolicy, 0, 0, 0>::AllocInternal(size_t requestedBytes);
- template __forceinline char *ArenaAllocatorBase<InPlaceFreeListPolicy, 3, 0, 0>::AllocInternal(size_t requestedBytes);
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- char *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- AllocInternal(size_t requestedBytes)
- {
- Assert(requestedBytes != 0);
- if (MaxObjectSize > 0)
- {
- Assert(requestedBytes <= MaxObjectSize);
- }
- if (RequireObjectAlignment)
- {
- Assert(requestedBytes % ObjectAlignment == 0);
- }
- // If out of memory function is set, that means that the caller is a throwing allocation
- // routine, so we can throw from here. Otherwise, we shouldn't throw.
- ARENA_FAULTINJECT_MEMORY(this->name, requestedBytes);
- ASSERT_THREAD();
- size_t nbytes;
- if (freeList != nullptr && requestedBytes > 0 && requestedBytes <= ArenaAllocatorBase::MaxSmallObjectSize)
- {
- // We have checked the size requested, so no integer overflow check
- nbytes = Math::Align(requestedBytes, ArenaAllocator::ObjectAlignment);
- Assert(nbytes <= ArenaAllocator::MaxSmallObjectSize);
- #ifdef PROFILE_MEM
- LogAlloc(requestedBytes, nbytes);
- #endif
- void * freeObject = TFreeListPolicy::Allocate(this->freeList, nbytes);
- if (freeObject != nullptr)
- {
- #ifdef ARENA_MEMORY_VERIFY
- TFreeListPolicy::VerifyFreeObjectIsFreeMemFilled(freeObject, nbytes);
- #endif
- #ifdef ARENA_ALLOCATOR_FREE_LIST_SIZE
- this->freeListSize -= nbytes;
- #endif
- #ifdef PROFILE_MEM
- LogReuse(nbytes);
- #endif
- ArenaMemoryTracking::ReportAllocation(this, freeObject, nbytes);
- return (char *)freeObject;
- }
- }
- else
- {
- nbytes = AllocSizeMath::Align(requestedBytes, ArenaAllocator::ObjectAlignment);
- #ifdef PROFILE_MEM
- LogAlloc(requestedBytes, nbytes);
- #endif
- }
- // TODO: Support large object free listing
- return ArenaAllocatorBase::RealAllocInlined(nbytes);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- Free(void * buffer, size_t byteSize)
- {
- ASSERT_THREAD();
- Assert(byteSize != 0);
- if (MaxObjectSize > 0)
- {
- Assert(byteSize <= MaxObjectSize);
- }
- if (RequireObjectAlignment)
- {
- Assert(byteSize % ObjectAlignment == 0);
- }
- // Since we successfully allocated, we shouldn't have integer overflow here
- size_t size = Math::Align(byteSize, ArenaAllocator::ObjectAlignment);
- Assert(size >= byteSize);
- ArenaMemoryTracking::ReportFree(this, buffer, byteSize);
- #ifdef ARENA_MEMORY_VERIFY
- if (Js::Configuration::Global.flags.ArenaNoFreeList)
- {
- return;
- }
- #endif
- if (buffer == cacheBlockCurrent - byteSize)
- {
- #ifdef PROFILE_MEM
- LogFree(byteSize);
- #endif
- cacheBlockCurrent = (char *)buffer;
- return;
- }
- else if (this->pageAllocator->IsClosed())
- {
- return;
- }
- else if (size <= ArenaAllocator::MaxSmallObjectSize)
- {
- // If we plan to free-list this object, we must prepare (typically, debug pattern fill) its memory here, in case we fail to allocate the free list because we're out of memory (see below),
- // and we never get to call TFreeListPolicy::Free.
- TFreeListPolicy::PrepareFreeObject(buffer, size);
- if (freeList == nullptr)
- {
- // Caution: TFreeListPolicy::New may fail silently if we're out of memory.
- freeList = TFreeListPolicy::New(this);
- if (freeList == nullptr)
- {
- return;
- }
- }
- this->freeList = TFreeListPolicy::Free(this->freeList, buffer, size);
- #ifdef ARENA_ALLOCATOR_FREE_LIST_SIZE
- this->freeListSize += size;
- #endif
- #ifdef PROFILE_MEM
- LogFree(byteSize);
- #endif
- return;
- }
- // TODO: Free list bigger objects
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- char *
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- Realloc(void* buffer, size_t existingBytes, size_t requestedBytes)
- {
- ASSERT_THREAD();
- if (existingBytes == 0)
- {
- Assert(buffer == nullptr);
- return AllocInternal(requestedBytes);
- }
- if (MaxObjectSize > 0)
- {
- Assert(requestedBytes <= MaxObjectSize);
- }
- if (RequireObjectAlignment)
- {
- Assert(requestedBytes % ObjectAlignment == 0);
- }
- size_t nbytes = AllocSizeMath::Align(requestedBytes, ArenaAllocator::ObjectAlignment);
- // Since we successfully allocated, we shouldn't have integer overflow here
- size_t nbytesExisting = Math::Align(existingBytes, ArenaAllocator::ObjectAlignment);
- Assert(nbytesExisting >= existingBytes);
- if (nbytes == nbytesExisting)
- {
- return (char *)buffer;
- }
- if (nbytes < nbytesExisting)
- {
- ArenaMemoryTracking::ReportReallocation(this, buffer, nbytesExisting, nbytes);
- Free(((char *)buffer) + nbytes, nbytesExisting - nbytes);
- return (char *)buffer;
- }
- char* replacementBuf = nullptr;
- if (requestedBytes > 0)
- {
- replacementBuf = AllocInternal(requestedBytes);
- if (replacementBuf != nullptr)
- {
- js_memcpy_s(replacementBuf, requestedBytes, buffer, existingBytes);
- }
- }
- if (nbytesExisting > 0)
- {
- Free(buffer, nbytesExisting);
- }
- return replacementBuf;
- }
- #ifdef PROFILE_MEM
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogBegin()
- {
- memoryData = MemoryProfiler::Begin(this->name);
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogReset()
- {
- if (memoryData)
- {
- MemoryProfiler::Reset(this->name, memoryData);
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogEnd()
- {
- if (memoryData)
- {
- MemoryProfiler::End(this->name, memoryData);
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogAlloc(size_t requestedBytes, size_t allocateBytes)
- {
- if (memoryData)
- {
- memoryData->requestCount++;
- memoryData->requestBytes += requestedBytes;
- memoryData->alignmentBytes += allocateBytes - requestedBytes;
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogRealAlloc(size_t size)
- {
- if (memoryData)
- {
- memoryData->allocatedBytes += size;
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogFree(size_t size)
- {
- if (memoryData)
- {
- memoryData->freelistBytes += size;
- memoryData->freelistCount++;
- }
- }
- template <class TFreeListPolicy, size_t ObjectAlignmentBitShiftArg, bool RequireObjectAlignment, size_t MaxObjectSize>
- void
- ArenaAllocatorBase<TFreeListPolicy, ObjectAlignmentBitShiftArg, RequireObjectAlignment, MaxObjectSize>::
- LogReuse(size_t size)
- {
- if (memoryData)
- {
- memoryData->reuseCount++;
- memoryData->reuseBytes += size;
- }
- }
- #endif
- void * InPlaceFreeListPolicy::New(ArenaAllocatorBase<InPlaceFreeListPolicy> * allocator)
- {
- return AllocatorNewNoThrowNoRecoveryArrayZ(ArenaAllocator, allocator, FreeObject *, buckets);
- }
- void * InPlaceFreeListPolicy::Allocate(void * policy, size_t size)
- {
- Assert(policy);
- FreeObject ** freeObjectLists = reinterpret_cast<FreeObject **>(policy);
- size_t index = (size >> ArenaAllocator::ObjectAlignmentBitShift) - 1;
- FreeObject * freeObject = freeObjectLists[index];
- if (NULL != freeObject)
- {
- freeObjectLists[index] = freeObject->next;
- #ifdef ARENA_MEMORY_VERIFY
- #ifndef _MSC_VER
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wsizeof-pointer-memaccess"
- #endif
- // Make sure the next pointer bytes are also DbgFreeMemFill-ed.
- memset(freeObject, DbgFreeMemFill, sizeof(freeObject->next));
- #ifndef _MSC_VER
- #pragma clang diagnostic pop
- #endif
- #endif
- }
- return freeObject;
- }
- void * InPlaceFreeListPolicy::Free(void * policy, void * object, size_t size)
- {
- Assert(policy);
- FreeObject ** freeObjectLists = reinterpret_cast<FreeObject **>(policy);
- FreeObject * freeObject = reinterpret_cast<FreeObject *>(object);
- size_t index = (size >> ArenaAllocator::ObjectAlignmentBitShift) - 1;
- freeObject->next = freeObjectLists[index];
- freeObjectLists[index] = freeObject;
- return policy;
- }
- void * InPlaceFreeListPolicy::Reset(void * policy)
- {
- return NULL;
- }
- #ifdef ARENA_MEMORY_VERIFY
- void InPlaceFreeListPolicy::VerifyFreeObjectIsFreeMemFilled(void * object, size_t size)
- {
- unsigned char * bytes = reinterpret_cast<unsigned char*>(object);
- for (size_t i = 0; i < size; i++)
- {
- Assert(bytes[i] == InPlaceFreeListPolicy::DbgFreeMemFill);
- }
- }
- #endif
- namespace Memory
- {
- template class ArenaAllocatorBase<InPlaceFreeListPolicy>;
- }
- void * StandAloneFreeListPolicy::New(ArenaAllocatorBase<StandAloneFreeListPolicy> * /*allocator*/)
- {
- return NewInternal(InitialEntries);
- }
- void * StandAloneFreeListPolicy::Allocate(void * policy, size_t size)
- {
- Assert(policy);
- StandAloneFreeListPolicy * _this = reinterpret_cast<StandAloneFreeListPolicy *>(policy);
- size_t index = (size >> ArenaAllocator::ObjectAlignmentBitShift) - 1;
- void * object = NULL;
- uint * freeObjectList = &_this->freeObjectLists[index];
- if (0 != *freeObjectList)
- {
- FreeObjectListEntry * entry = &_this->entries[*freeObjectList - 1];
- uint oldFreeList = _this->freeList;
- _this->freeList = *freeObjectList;
- *freeObjectList = entry->next;
- object = entry->object;
- Assert(object != NULL);
- entry->next = oldFreeList;
- entry->object = NULL;
- }
- return object;
- }
- void * StandAloneFreeListPolicy::Free(void * policy, void * object, size_t size)
- {
- Assert(policy);
- StandAloneFreeListPolicy * _this = reinterpret_cast<StandAloneFreeListPolicy *>(policy);
- size_t index = (size >> ArenaAllocator::ObjectAlignmentBitShift) - 1;
- if (TryEnsureFreeListEntry(_this))
- {
- Assert(_this->freeList != 0);
- uint * freeObjectList = &_this->freeObjectLists[index];
- FreeObjectListEntry * entry = &_this->entries[_this->freeList - 1];
- uint oldFreeObjectList = *freeObjectList;
- *freeObjectList = _this->freeList;
- _this->freeList = entry->next;
- entry->object = object;
- entry->next = oldFreeObjectList;
- }
- return _this;
- }
- void * StandAloneFreeListPolicy::Reset(void * policy)
- {
- Assert(policy);
- StandAloneFreeListPolicy * _this = reinterpret_cast<StandAloneFreeListPolicy *>(policy);
- HeapDeletePlus(GetPlusSize(_this), _this);
- return NULL;
- }
- #ifdef ARENA_MEMORY_VERIFY
- void StandAloneFreeListPolicy::VerifyFreeObjectIsFreeMemFilled(void * object, size_t size)
- {
- char * bytes = reinterpret_cast<char*>(object);
- for (size_t i = 0; i < size; i++)
- {
- Assert(bytes[i] == StandAloneFreeListPolicy::DbgFreeMemFill);
- }
- }
- #endif
- void StandAloneFreeListPolicy::Release(void * policy)
- {
- if (NULL != policy)
- {
- Reset(policy);
- }
- }
- StandAloneFreeListPolicy * StandAloneFreeListPolicy::NewInternal(uint entries)
- {
- size_t plusSize = buckets * sizeof(uint) + entries * sizeof(FreeObjectListEntry);
- StandAloneFreeListPolicy * _this = HeapNewNoThrowPlusZ(plusSize, StandAloneFreeListPolicy);
- if (NULL != _this)
- {
- _this->allocated = entries;
- _this->freeObjectLists = (uint *)(_this + 1);
- _this->entries = (FreeObjectListEntry *)(_this->freeObjectLists + buckets);
- }
- return _this;
- }
- bool StandAloneFreeListPolicy::TryEnsureFreeListEntry(StandAloneFreeListPolicy *& _this)
- {
- if (0 == _this->freeList)
- {
- if (_this->used < _this->allocated)
- {
- _this->used++;
- _this->freeList = _this->used;
- }
- else
- {
- Assert(_this->used == _this->allocated);
- StandAloneFreeListPolicy * oldThis = _this;
- uint entries = oldThis->allocated + min(oldThis->allocated, MaxEntriesGrowth);
- StandAloneFreeListPolicy * newThis = NewInternal(entries);
- if (NULL != newThis)
- {
- uint sizeInBytes = buckets * sizeof(uint);
- js_memcpy_s(newThis->freeObjectLists, sizeInBytes, oldThis->freeObjectLists, sizeInBytes);
- js_memcpy_s(newThis->entries, newThis->allocated * sizeof(FreeObjectListEntry), oldThis->entries, oldThis->used * sizeof(FreeObjectListEntry));
- newThis->used = oldThis->used + 1;
- newThis->freeList = newThis->used;
- _this = newThis;
- HeapDeletePlus(GetPlusSize(oldThis), oldThis);
- }
- else
- {
- return false;
- }
- }
- }
- return true;
- }
- namespace Memory
- {
- template class ArenaAllocatorBase<StandAloneFreeListPolicy>;
- }
- #ifdef PERSISTENT_INLINE_CACHES
- void * InlineCacheFreeListPolicy::New(ArenaAllocatorBase<InlineCacheAllocatorTraits> * allocator)
- {
- return NewInternal();
- }
- InlineCacheFreeListPolicy * InlineCacheFreeListPolicy::NewInternal()
- {
- InlineCacheFreeListPolicy * _this = HeapNewNoThrowZ(InlineCacheFreeListPolicy);
- return _this;
- }
- InlineCacheFreeListPolicy::InlineCacheFreeListPolicy()
- {
- Assert(AreFreeListBucketsEmpty());
- }
- bool InlineCacheFreeListPolicy::AreFreeListBucketsEmpty()
- {
- for (int b = 0; b < bucketCount; b++)
- {
- if (this->freeListBuckets[b] != 0) return false;
- }
- return true;
- }
- void * InlineCacheFreeListPolicy::Allocate(void * policy, size_t size)
- {
- Assert(policy);
- FreeObject ** freeObjectLists = reinterpret_cast<FreeObject **>(policy);
- size_t index = (size >> InlineCacheAllocatorInfo::ObjectAlignmentBitShift) - 1;
- FreeObject * freeObject = freeObjectLists[index];
- if (NULL != freeObject)
- {
- freeObjectLists[index] = reinterpret_cast<FreeObject *>(reinterpret_cast<intptr_t>(freeObject->next) & ~InlineCacheFreeListTag);
- #ifdef ARENA_MEMORY_VERIFY
- // Make sure the next pointer bytes are also DbgFreeMemFill-ed, before we give them out.
- memset(&freeObject->next, DbgFreeMemFill, sizeof(freeObject->next));
- #endif
- }
- return freeObject;
- }
- void * InlineCacheFreeListPolicy::Free(void * policy, void * object, size_t size)
- {
- Assert(policy);
- FreeObject ** freeObjectLists = reinterpret_cast<FreeObject **>(policy);
- FreeObject * freeObject = reinterpret_cast<FreeObject *>(object);
- size_t index = (size >> InlineCacheAllocatorInfo::ObjectAlignmentBitShift) - 1;
- freeObject->next = reinterpret_cast<FreeObject *>(reinterpret_cast<intptr_t>(freeObjectLists[index]) | InlineCacheFreeListTag);
- freeObjectLists[index] = freeObject;
- return policy;
- }
- void * InlineCacheFreeListPolicy::Reset(void * policy)
- {
- Assert(policy);
- InlineCacheFreeListPolicy * _this = reinterpret_cast<InlineCacheFreeListPolicy *>(policy);
- HeapDelete(_this);
- return NULL;
- }
- #ifdef ARENA_MEMORY_VERIFY
- void InlineCacheFreeListPolicy::VerifyFreeObjectIsFreeMemFilled(void * object, size_t size)
- {
- unsigned char * bytes = reinterpret_cast<unsigned char*>(object);
- for (size_t i = 0; i < size; i++)
- {
- // We must allow for zero-filled free listed objects (at least their weakRefs/blankSlots bytes), because during garbage collection, we may zero out
- // some of the weakRefs (those that have become unreachable), and this is NOT a sign of "use after free" problem. It would be nice if during collection
- // we could reliably distinguish free-listed objects from live caches, but that's not possible because caches can be allocated and freed in batches
- // (see more on that in comments inside InlineCacheFreeListPolicy::PrepareFreeObject).
- Assert(bytes[i] == NULL || bytes[i] == InlineCacheFreeListPolicy::DbgFreeMemFill);
- }
- }
- #endif
- void InlineCacheFreeListPolicy::Release(void * policy)
- {
- if (NULL != policy)
- {
- Reset(policy);
- }
- }
- namespace Memory
- {
- template class ArenaAllocatorBase<InlineCacheAllocatorTraits>;
- }
- #if DBG
- bool InlineCacheAllocator::IsAllZero()
- {
- UpdateCacheBlock();
- // See InlineCacheAllocator::ZeroAll for why we ignore the strongRef slot of the CacheLayout.
- BigBlock *bigBlock = this->bigBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- unsigned char* weakRefBytes = (unsigned char *)cache->weakRefs;
- for (size_t i = 0; i < sizeof(cache->weakRefs); i++)
- {
- // If we're verifying arena memory (in debug builds) caches on the free list
- // will be debug pattern filled (specifically, at least their weak reference slots).
- // All other caches must be zeroed out (again, at least their weak reference slots).
- #ifdef ARENA_MEMORY_VERIFY
- if (weakRefBytes[i] != NULL && weakRefBytes[i] != InlineCacheFreeListPolicy::DbgFreeMemFill)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- return false;
- }
- #else
- if (weakRefBytes[i] != NULL)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- return false;
- }
- #endif
- }
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- bigBlock = this->fullBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- char* weakRefBytes = (char *)cache->weakRefs;
- for (size_t i = 0; i < sizeof(cache->weakRefs); i++)
- {
- // If we're verifying arena memory (in debug builds) caches on the free list
- // will be debug pattern filled (specifically, their weak reference slots).
- // All other caches must be zeroed out (again, their weak reference slots).
- #ifdef ARENA_MEMORY_VERIFY
- if (weakRefBytes[i] != NULL && weakRefBytes[i] != InlineCacheFreeListPolicy::DbgFreeMemFill)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- return false;
- }
- #else
- if (weakRefBytes[i] != NULL)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- return false;
- }
- #endif
- }
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- Assert(memoryBlock->nbytes % sizeof(CacheLayout) == 0);
- ArenaMemoryBlock * next = memoryBlock->next;
- CacheLayout* endPtr = (CacheLayout*)(memoryBlock->GetBytes() + memoryBlock->nbytes);
- for (CacheLayout* cache = (CacheLayout*)memoryBlock->GetBytes(); cache < endPtr; cache++)
- {
- unsigned char* weakRefBytes = (unsigned char *)cache->weakRefs;
- for (size_t i = 0; i < sizeof(cache->weakRefs); i++)
- {
- #ifdef ARENA_MEMORY_VERIFY
- if (weakRefBytes[i] != NULL && weakRefBytes[i] != InlineCacheFreeListPolicy::DbgFreeMemFill)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- return false;
- }
- #else
- if (weakRefBytes[i] != NULL)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- return false;
- }
- #endif
- }
- }
- memoryBlock = next;
- }
- return true;
- }
- #endif
- void InlineCacheAllocator::ZeroAll()
- {
- UpdateCacheBlock();
- // We zero the weakRefs part of each cache in the arena unconditionally. The strongRef slot is zeroed only
- // if it isn't tagged with InlineCacheFreeListTag. That's so we don't lose our free list, which is
- // formed by caches linked via their strongRef slot tagged with InlineCacheFreeListTag. On the other hand,
- // inline caches that require invalidation use the same slot as a pointer (untagged) to the cache's address
- // in the invalidation list. Hence, we must zero the strongRef slot when untagged to ensure the cache
- // doesn't appear registered for invalidation when it's actually blank (which would trigger asserts in InlineCache::VerifyRegistrationForInvalidation).
- BigBlock *bigBlock = this->bigBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- memset(cache->weakRefs, 0, sizeof(cache->weakRefs));
- // We want to preserve the free list, whose next pointers are tagged with InlineCacheFreeListTag.
- if ((cache->strongRef & InlineCacheFreeListTag) == 0) cache->strongRef = 0;
- if (cache->weakRefs[0] != NULL || cache->weakRefs[1] != NULL || cache->weakRefs[2] != NULL)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- }
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- bigBlock = this->fullBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- memset(cache->weakRefs, 0, sizeof(cache->weakRefs));
- // We want to preserve the free list, whose next pointers are tagged with InlineCacheFreeListTag.
- if ((cache->strongRef & InlineCacheFreeListTag) == 0) cache->strongRef = 0;
- if (cache->weakRefs[0] != NULL || cache->weakRefs[1] != NULL || cache->weakRefs[2] != NULL)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- }
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- Assert(memoryBlock->nbytes % sizeof(CacheLayout) == 0);
- ArenaMemoryBlock * next = memoryBlock->next;
- CacheLayout* endPtr = (CacheLayout*)(memoryBlock->GetBytes() + memoryBlock->nbytes);
- for (CacheLayout* cache = (CacheLayout*)memoryBlock->GetBytes(); cache < endPtr; cache++)
- {
- memset(cache->weakRefs, 0, sizeof(cache->weakRefs));
- // We want to preserve the free list, whose next pointers are tagged with InlineCacheFreeListTag.
- if ((cache->strongRef & InlineCacheFreeListTag) == 0) cache->strongRef = 0;
- if (cache->weakRefs[0] != NULL || cache->weakRefs[1] != NULL || cache->weakRefs[2] != NULL)
- {
- AssertMsg(false, "Inline cache arena is not zeroed!");
- }
- }
- memoryBlock = next;
- }
- }
- bool InlineCacheAllocator::IsDeadWeakRef(Recycler* recycler, void* ptr)
- {
- return recycler->IsObjectMarked(ptr);
- }
- bool InlineCacheAllocator::CacheHasDeadWeakRefs(Recycler* recycler, CacheLayout* cache)
- {
- for (intptr_t* curWeakRefPtr = cache->weakRefs; curWeakRefPtr < &cache->strongRef; curWeakRefPtr++)
- {
- intptr_t curWeakRef = *curWeakRefPtr;
- if (curWeakRef == 0)
- {
- continue;
- }
- curWeakRef &= ~(intptr_t)InlineCacheAuxSlotTypeTag;
- if ((curWeakRef & (HeapConstants::ObjectGranularity - 1)) != 0)
- {
- continue;
- }
- if (!recycler->IsObjectMarked((void*)curWeakRef))
- {
- return true;
- }
- }
- return false;
- }
- bool InlineCacheAllocator::HasNoDeadWeakRefs(Recycler* recycler)
- {
- UpdateCacheBlock();
- BigBlock *bigBlock = this->bigBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- if (CacheHasDeadWeakRefs(recycler, cache))
- {
- return false;
- }
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- bigBlock = this->fullBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- if (CacheHasDeadWeakRefs(recycler, cache))
- {
- return false;
- }
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- Assert(memoryBlock->nbytes % sizeof(CacheLayout) == 0);
- ArenaMemoryBlock * next = memoryBlock->next;
- CacheLayout* endPtr = (CacheLayout*)(memoryBlock->GetBytes() + memoryBlock->nbytes);
- for (CacheLayout* cache = (CacheLayout*)memoryBlock->GetBytes(); cache < endPtr; cache++)
- {
- if (CacheHasDeadWeakRefs(recycler, cache))
- {
- return false;
- }
- }
- memoryBlock = next;
- }
- return true;
- }
- void InlineCacheAllocator::ClearCacheIfHasDeadWeakRefs(Recycler* recycler, CacheLayout* cache)
- {
- for (intptr_t* curWeakRefPtr = cache->weakRefs; curWeakRefPtr < &cache->strongRef; curWeakRefPtr++)
- {
- intptr_t curWeakRef = *curWeakRefPtr;
- if (curWeakRef == 0)
- {
- continue;
- }
- curWeakRef &= ~(intptr_t)InlineCacheAuxSlotTypeTag;
- if ((curWeakRef & (HeapConstants::ObjectGranularity - 1)) != 0)
- {
- continue;
- }
- if (!recycler->IsObjectMarked((void*)curWeakRef))
- {
- cache->weakRefs[0] = 0;
- cache->weakRefs[1] = 0;
- cache->weakRefs[2] = 0;
- break;
- }
- }
- }
- void InlineCacheAllocator::ClearCachesWithDeadWeakRefs(Recycler* recycler)
- {
- UpdateCacheBlock();
- BigBlock *bigBlock = this->bigBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- ClearCacheIfHasDeadWeakRefs(recycler, cache);
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- bigBlock = this->fullBlocks;
- while (bigBlock != NULL)
- {
- Assert(bigBlock->currentByte % sizeof(CacheLayout) == 0);
- CacheLayout* endPtr = (CacheLayout*)(bigBlock->GetBytes() + bigBlock->currentByte);
- for (CacheLayout* cache = (CacheLayout*)bigBlock->GetBytes(); cache < endPtr; cache++)
- {
- ClearCacheIfHasDeadWeakRefs(recycler, cache);
- }
- bigBlock = bigBlock->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- Assert(memoryBlock->nbytes % sizeof(CacheLayout) == 0);
- ArenaMemoryBlock * next = memoryBlock->next;
- CacheLayout* endPtr = (CacheLayout*)(memoryBlock->GetBytes() + memoryBlock->nbytes);
- for (CacheLayout* cache = (CacheLayout*)memoryBlock->GetBytes(); cache < endPtr; cache++)
- {
- ClearCacheIfHasDeadWeakRefs(recycler, cache);
- }
- memoryBlock = next;
- }
- }
- #else
- template class ArenaAllocatorBase<InlineCacheAllocatorTraits>;
- #if DBG
- bool InlineCacheAllocator::IsAllZero()
- {
- UpdateCacheBlock();
- BigBlock *blockp = this->bigBlocks;
- while (blockp != NULL)
- {
- for (size_t i = 0; i < blockp->currentByte; i++)
- {
- if (blockp->GetBytes()[i] != 0)
- {
- return false;
- }
- }
- blockp = blockp->nextBigBlock;
- }
- blockp = this->fullBlocks;
- while (blockp != NULL)
- {
- for (size_t i = 0; i < blockp->currentByte; i++)
- {
- if (blockp->GetBytes()[i] != 0)
- {
- return false;
- }
- }
- blockp = blockp->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- ArenaMemoryBlock * next = memoryBlock->next;
- for (size_t i = 0; i < memoryBlock->nbytes; i++)
- {
- if (memoryBlock->GetBytes()[i] != 0)
- {
- return false;
- }
- }
- memoryBlock = next;
- }
- return true;
- }
- #endif
- void InlineCacheAllocator::ZeroAll()
- {
- UpdateCacheBlock();
- BigBlock *blockp = this->bigBlocks;
- while (blockp != NULL)
- {
- memset(blockp->GetBytes(), 0, blockp->currentByte);
- blockp = blockp->nextBigBlock;
- }
- blockp = this->fullBlocks;
- while (blockp != NULL)
- {
- memset(blockp->GetBytes(), 0, blockp->currentByte);
- blockp = blockp->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- ArenaMemoryBlock * next = memoryBlock->next;
- memset(memoryBlock->GetBytes(), 0, memoryBlock->nbytes);
- memoryBlock = next;
- }
- }
- #endif
- #if DBG
- bool IsInstInlineCacheAllocator::IsAllZero()
- {
- UpdateCacheBlock();
- BigBlock *blockp = this->bigBlocks;
- while (blockp != NULL)
- {
- for (size_t i = 0; i < blockp->currentByte; i++)
- {
- if (blockp->GetBytes()[i] != 0)
- {
- return false;
- }
- }
- blockp = blockp->nextBigBlock;
- }
- blockp = this->fullBlocks;
- while (blockp != NULL)
- {
- for (size_t i = 0; i < blockp->currentByte; i++)
- {
- if (blockp->GetBytes()[i] != 0)
- {
- return false;
- }
- }
- blockp = blockp->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- ArenaMemoryBlock * next = memoryBlock->next;
- for (size_t i = 0; i < memoryBlock->nbytes; i++)
- {
- if (memoryBlock->GetBytes()[i] != 0)
- {
- return false;
- }
- }
- memoryBlock = next;
- }
- return true;
- }
- #endif
- void IsInstInlineCacheAllocator::ZeroAll()
- {
- UpdateCacheBlock();
- BigBlock *blockp = this->bigBlocks;
- while (blockp != NULL)
- {
- memset(blockp->GetBytes(), 0, blockp->currentByte);
- blockp = blockp->nextBigBlock;
- }
- blockp = this->fullBlocks;
- while (blockp != NULL)
- {
- memset(blockp->GetBytes(), 0, blockp->currentByte);
- blockp = blockp->nextBigBlock;
- }
- ArenaMemoryBlock * memoryBlock = this->mallocBlocks;
- while (memoryBlock != nullptr)
- {
- ArenaMemoryBlock * next = memoryBlock->next;
- memset(memoryBlock->GetBytes(), 0, memoryBlock->nbytes);
- memoryBlock = next;
- }
- }
- #undef ASSERT_TRHEAD
|