#pragma once
#include "AllocationFailureMode.h"
#include "Allocator.h"
#include "CellAttributes.h"
#include "FreeList.h"
#include "LocalAllocator.h"
#include "MarkedBlock.h"
#include <wtf/DataLog.h>
#include <wtf/FastBitVector.h>
#include <wtf/SharedTask.h>
#include <wtf/Vector.h>
namespace JSC {
class GCDeferralContext;
class Heap;
class IsoCellSet;
class MarkedSpace;
class LLIntOffsetsExtractor;
class ThreadLocalCache;
class ThreadLocalCacheLayout;
#define FOR_EACH_BLOCK_DIRECTORY_BIT(macro) \
macro(live, Live) \
macro(empty, Empty) \
macro(allocated, Allocated) \
macro(canAllocateButNotEmpty, CanAllocateButNotEmpty) \
macro(destructible, Destructible) \
macro(eden, Eden) \
macro(unswept, Unswept) \
\
\
macro(markingNotEmpty, MarkingNotEmpty) \
macro(markingRetired, MarkingRetired)
class BlockDirectory {
WTF_MAKE_NONCOPYABLE(BlockDirectory);
WTF_MAKE_FAST_ALLOCATED;
friend class LLIntOffsetsExtractor;
public:
BlockDirectory(Heap*, size_t cellSize);
~BlockDirectory();
void setSubspace(Subspace*);
void lastChanceToFinalize();
void prepareForAllocation();
void stopAllocating();
void stopAllocatingForGood();
void resumeAllocating();
void beginMarkingForFullCollection();
void endMarking();
void snapshotUnsweptForEdenCollection();
void snapshotUnsweptForFullCollection();
void sweep();
void shrink();
void assertNoUnswept();
size_t cellSize() const { return m_cellSize; }
const CellAttributes& attributes() const { return m_attributes; }
bool needsDestruction() const { return m_attributes.destruction == NeedsDestruction; }
DestructionMode destruction() const { return m_attributes.destruction; }
HeapCell::Kind cellKind() const { return m_attributes.cellKind; }
Heap* heap() { return m_heap; }
bool isFreeListedCell(const void* target);
template<typename Functor> void forEachBlock(const Functor&);
template<typename Functor> void forEachNotEmptyBlock(const Functor&);
RefPtr<SharedTask<MarkedBlock::Handle*()>> parallelNotEmptyBlockSource();
void addBlock(MarkedBlock::Handle*);
void removeBlock(MarkedBlock::Handle*);
bool isPagedOut(double deadline);
Lock& bitvectorLock() { return m_bitvectorLock; }
#define BLOCK_DIRECTORY_BIT_ACCESSORS(lowerBitName, capitalBitName) \
bool is ## capitalBitName(const AbstractLocker&, size_t index) const { return m_ ## lowerBitName[index]; } \
bool is ## capitalBitName(const AbstractLocker& locker, MarkedBlock::Handle* block) const { return is ## capitalBitName(locker, block->index()); } \
void setIs ## capitalBitName(const AbstractLocker&, size_t index, bool value) { m_ ## lowerBitName[index] = value; } \
void setIs ## capitalBitName(const AbstractLocker& locker, MarkedBlock::Handle* block, bool value) { setIs ## capitalBitName(locker, block->index(), value); }
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_ACCESSORS)
#undef BLOCK_DIRECTORY_BIT_ACCESSORS
template<typename Func>
void forEachBitVector(const AbstractLocker&, const Func& func)
{
#define BLOCK_DIRECTORY_BIT_CALLBACK(lowerBitName, capitalBitName) \
func(m_ ## lowerBitName);
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_CALLBACK);
#undef BLOCK_DIRECTORY_BIT_CALLBACK
}
template<typename Func>
void forEachBitVectorWithName(const AbstractLocker&, const Func& func)
{
#define BLOCK_DIRECTORY_BIT_CALLBACK(lowerBitName, capitalBitName) \
func(m_ ## lowerBitName, #capitalBitName);
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_CALLBACK);
#undef BLOCK_DIRECTORY_BIT_CALLBACK
}
BlockDirectory* nextDirectory() const { return m_nextDirectory; }
BlockDirectory* nextDirectoryInSubspace() const { return m_nextDirectoryInSubspace; }
BlockDirectory* nextDirectoryInAlignedMemoryAllocator() const { return m_nextDirectoryInAlignedMemoryAllocator; }
void setNextDirectory(BlockDirectory* directory) { m_nextDirectory = directory; }
void setNextDirectoryInSubspace(BlockDirectory* directory) { m_nextDirectoryInSubspace = directory; }
void setNextDirectoryInAlignedMemoryAllocator(BlockDirectory* directory) { m_nextDirectoryInAlignedMemoryAllocator = directory; }
MarkedBlock::Handle* findEmptyBlockToSteal();
MarkedBlock::Handle* findBlockToSweep();
Subspace* subspace() const { return m_subspace; }
MarkedSpace& markedSpace() const;
Allocator allocator() const { return Allocator(m_tlcOffset); }
void dump(PrintStream&) const;
void dumpBits(PrintStream& = WTF::dataFile());
private:
friend class IsoCellSet;
friend class LocalAllocator;
friend class LocalSideAllocator;
friend class MarkedBlock;
friend class ThreadLocalCacheLayout;
MarkedBlock::Handle* findBlockForAllocation(LocalAllocator&);
MarkedBlock::Handle* tryAllocateBlock();
Vector<MarkedBlock::Handle*> m_blocks;
Vector<unsigned> m_freeBlockIndices;
Lock m_bitvectorLock;
#define BLOCK_DIRECTORY_BIT_DECLARATION(lowerBitName, capitalBitName) \
FastBitVector m_ ## lowerBitName;
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_DECLARATION)
#undef BLOCK_DIRECTORY_BIT_DECLARATION
size_t m_emptyCursor { 0 };
size_t m_unsweptCursor { 0 };
unsigned m_cellSize;
CellAttributes m_attributes;
Heap* m_heap { nullptr };
Subspace* m_subspace { nullptr };
BlockDirectory* m_nextDirectory { nullptr };
BlockDirectory* m_nextDirectoryInSubspace { nullptr };
BlockDirectory* m_nextDirectoryInAlignedMemoryAllocator { nullptr };
Lock m_localAllocatorsLock;
size_t m_tlcOffset;
SentinelLinkedList<LocalAllocator, BasicRawSentinelNode<LocalAllocator>> m_localAllocators;
};
}