}
-void BitStreamWriter::AllocMemoryBlock()
+BitStreamWriter::MemoryBlockList::MemoryBlockList()
+ : m_head(nullptr),
+ m_tail(nullptr)
{
- _ASSERTE( IS_ALIGNED( m_MemoryBlockSize, sizeof( size_t ) ) );
- m_pCurrentSlot = (size_t*) m_pAllocator->Alloc( m_MemoryBlockSize );
- m_OutOfBlockSlot = m_pCurrentSlot + m_MemoryBlockSize / sizeof( size_t );
+}
- MemoryBlockDesc* pMemBlockDesc = (MemoryBlockDesc*) m_pAllocator->Alloc( sizeof( MemoryBlockDesc ) );
- _ASSERTE( IS_ALIGNED( pMemBlockDesc, sizeof( void* ) ) );
+BitStreamWriter::MemoryBlock* BitStreamWriter::MemoryBlockList::AppendNew(IJitAllocator* allocator, size_t bytes)
+{
+ auto* memBlock = reinterpret_cast<MemoryBlock*>(allocator->Alloc(sizeof(MemoryBlock) + bytes));
+ memBlock->m_next = nullptr;
- pMemBlockDesc->Init();
- pMemBlockDesc->StartAddress = m_pCurrentSlot;
- if (m_MemoryBlocksTail != NULL)
+ if (m_tail != nullptr)
{
- _ASSERTE(m_MemoryBlocksHead != NULL);
- m_MemoryBlocksTail->m_Next = pMemBlockDesc;
+ _ASSERTE(m_head != nullptr);
+ m_tail->m_next = memBlock;
}
else
{
- _ASSERTE(m_MemoryBlocksHead == NULL);
- m_MemoryBlocksHead = pMemBlockDesc;
+ _ASSERTE(m_head == nullptr);
+ m_head = memBlock;
}
- m_MemoryBlocksTail = pMemBlockDesc;
-
-#ifdef _DEBUG
- m_MemoryBlocksCount++;
-#endif
+ m_tail = memBlock;
+ return memBlock;
}
-GcInfoEncoder::GcInfoEncoder(
- ICorJitInfo* pCorJitInfo,
- CORINFO_METHOD_INFO* pMethodInfo,
- IJitAllocator* pJitAllocator
- )
- : m_HeaderInfoWriter( pJitAllocator ),
-#if 0
-#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
- m_PartiallyInterruptibleInfoWriter( pJitAllocator ),
-#endif
+void BitStreamWriter::MemoryBlockList::Dispose(IJitAllocator* allocator)
+{
+#ifdef MUST_CALL_JITALLOCATOR_FREE
+ for (MemoryBlock* block = m_head, *next; block != nullptr; block = next)
+ {
+ next = block->m_next;
+ allocator->Free(block);
+ }
+ m_head = nullptr;
+ m_tail = nullptr;
#endif
+}
+
+
+void BitStreamWriter::AllocMemoryBlock()
+{
+ _ASSERTE( IS_ALIGNED( m_MemoryBlockSize, sizeof( size_t ) ) );
m_FullyInterruptibleInfoWriter( pJitAllocator ),
m_LifetimeTransitions( pJitAllocator )
{
int i,c;
BYTE* source = NULL;
- MemoryBlockDesc* pMemBlockDesc = m_MemoryBlocks.GetHead();
+ MemoryBlock* pMemBlock = m_MemoryBlocks.Head();
_ASSERTE( pMemBlockDesc != NULL );
- while (pMemBlockDesc->m_Next != NULL)
+ while (pMemBlock->Next() != NULL)
{
- source = (BYTE*) pMemBlockDesc->StartAddress;
+ source = (BYTE*) pMemBlock->Contents;
// @TODO: use memcpy instead
for( i = 0; i < m_MemoryBlockSize; i++ )
{
*( buffer++ ) = *( source++ );
}
- pMemBlockDesc = pMemBlockDesc->m_Next;
+ pMemBlock = pMemBlock->Next();
}
- source = (BYTE*) pMemBlockDesc->StartAddress;
+ source = (BYTE*) pMemBlock->Contents;
// The number of bytes to copy in the last block
c = (int) ((BYTE*) ( m_pCurrentSlot + 1 ) - source - m_FreeBitsInCurrentSlot/8);
_ASSERTE( c >= 0 );
void BitStreamWriter::Dispose()
{
-#ifdef MUST_CALL_JITALLOCATOR_FREE
- for (MemoryBlockDes* block = m_MemoryBlocksHead, *next; block != NULL; block = next)
- {
- next = block->m_Next;
- m_pAllocator->Free(block->StartAddress);
- m_pAllocator->Free(block);
- }
- m_MemoryBlocksHead = NULL;
- m_MemoryBlocksTail = NULL;
-
- m_pAllocator->Free( m_SlotMappings );
-#endif
+ m_MemoryBlocks.Dispose(m_pAllocator);
}
}
}
}
+BitStreamWriter::MemoryBlockList::MemoryBlockList()
+ : m_head(nullptr),
+ m_tail(nullptr)
+{
+}
+
+BitStreamWriter::MemoryBlock* BitStreamWriter::MemoryBlockList::AppendNew(IAllocator* allocator, size_t bytes)
+{
+ auto* memBlock = reinterpret_cast<MemoryBlock*>(allocator->Alloc(sizeof(MemoryBlock) + bytes));
+ memBlock->m_next = nullptr;
+
+ if (m_tail != nullptr)
+ {
+ _ASSERTE(m_head != nullptr);
+ m_tail->m_next = memBlock;
+ }
+ else
+ {
+ _ASSERTE(m_head == nullptr);
+ m_head = memBlock;
+ }
+
+ m_tail = memBlock;
+ return memBlock;
+}
+
+void BitStreamWriter::MemoryBlockList::Dispose(IAllocator* allocator)
+{
+#ifdef MUST_CALL_JITALLOCATOR_FREE
+ for (MemoryBlock* block = m_head, *next; block != nullptr; block = next)
+ {
+ next = block->m_next;
+ allocator->Free(block);
+ }
+ m_head = nullptr;
+ m_tail = nullptr;
+#endif
+}
+
void BitStreamWriter::Write(BitArray& a, UINT32 count)
{
size_t* dataPtr = a.DataPtr();
{
m_pAllocator = pAllocator;
m_BitCount = 0;
- m_MemoryBlocksHead = NULL;
- m_MemoryBlocksTail = NULL;
#ifdef _DEBUG
m_MemoryBlocksCount = 0;
#endif
int i,c;
BYTE* source = NULL;
- MemoryBlockDesc* pMemBlockDesc = m_MemoryBlocksHead;
- if( pMemBlockDesc == NULL )
+ MemoryBlock* pMemBlock = m_MemoryBlocks.Head();
+ if( pMemBlock == NULL )
return;
- while (pMemBlockDesc->m_Next != NULL)
+ while (pMemBlock->Next() != NULL)
{
- source = (BYTE*) pMemBlockDesc->StartAddress;
+ source = (BYTE*) pMemBlock->Contents;
// @TODO: use memcpy instead
for( i = 0; i < m_MemoryBlockSize; i++ )
{
*( buffer++ ) = *( source++ );
}
- pMemBlockDesc = pMemBlockDesc->m_Next;
+ pMemBlock = pMemBlock->Next();
}
- source = (BYTE*) pMemBlockDesc->StartAddress;
+ source = (BYTE*) pMemBlock->Contents;
// The number of bytes to copy in the last block
c = (int) ((BYTE*) ( m_pCurrentSlot + 1 ) - source - m_FreeBitsInCurrentSlot/8);
_ASSERTE( c >= 0 );
void BitStreamWriter::Dispose()
{
-#ifdef MUST_CALL_JITALLOCATOR_FREE
- for (MemoryBlockDes* block = m_MemoryBlocksHead, *next; block != NULL; block = next)
- {
- next = block->m_Next;
- m_pAllocator->Free(block->StartAddress);
- m_pAllocator->Free(block);
- }
- m_MemoryBlocksHead = NULL;
- m_MemoryBlocksTail = NULL;
-#endif
+ m_MemoryBlocks.Dispose(m_pAllocator);
}
int BitStreamWriter::SizeofVarLengthUnsigned( size_t n, UINT32 base)
private:
- class MemoryBlockDesc
+ class MemoryBlockList;
+ class MemoryBlock
{
+ friend class MemoryBlockList;
+ MemoryBlock* m_next;
+
+ public:
+ size_t Contents[];
+
+ inline MemoryBlock* Next()
+ {
+ return m_next;
+ }
+ };
+
+ class MemoryBlockList
+ {
+ MemoryBlock* m_head;
+ MemoryBlock* m_tail;
+
public:
- size_t* StartAddress;
- MemoryBlockDesc* m_Next;
+ MemoryBlockList();
- inline void Init()
+ inline MemoryBlock* Head()
{
- m_Next = NULL;
+ return m_head;
}
+
+ MemoryBlock* AppendNew(IAllocator* allocator, size_t bytes);
+ void Dispose(IAllocator* allocator);
};
IJitAllocator* m_pAllocator;
size_t m_BitCount;
int m_FreeBitsInCurrentSlot;
- MemoryBlockDesc* m_MemoryBlocksHead;
- MemoryBlockDesc* m_MemoryBlocksTail;
+ MemoryBlockList m_MemoryBlocks;
const static int m_MemoryBlockSize = 512; // must be a multiple of the pointer size
size_t* m_pCurrentSlot; // bits are written through this pointer
size_t* m_OutOfBlockSlot; // sentinel value to determine when the block is full
int EncodeVarLengthSigned( SSIZE_T n, UINT32 base );
private:
+ class MemoryBlockList;
+ class MemoryBlock
+ {
+ friend class MemoryBlockList;
+ MemoryBlock* m_next;
+
+ public:
+ size_t Contents[];
+
+ inline MemoryBlock* Next()
+ {
+ return m_next;
+ }
+ };
- class MemoryBlockDesc
+ class MemoryBlockList
{
+ MemoryBlock* m_head;
+ MemoryBlock* m_tail;
+
public:
- size_t* StartAddress;
- MemoryBlockDesc* m_Next;
+ MemoryBlockList();
- inline void Init()
+ inline MemoryBlock* Head()
{
- m_Next = NULL;
+ return m_head;
}
+
+ MemoryBlock* AppendNew(IAllocator* allocator, size_t bytes);
+ void Dispose(IAllocator* allocator);
};
IAllocator* m_pAllocator;
size_t m_BitCount;
UINT32 m_FreeBitsInCurrentSlot;
- MemoryBlockDesc* m_MemoryBlocksHead;
- MemoryBlockDesc* m_MemoryBlocksTail;
+ MemoryBlockList m_MemoryBlocks;
const static int m_MemoryBlockSize = 128; // must be a multiple of the pointer size
size_t* m_pCurrentSlot; // bits are written through this pointer
size_t* m_OutOfBlockSlot; // sentinel value to determine when the block is full
inline void AllocMemoryBlock()
{
_ASSERTE( IS_ALIGNED( m_MemoryBlockSize, sizeof( size_t ) ) );
- m_pCurrentSlot = (size_t*) m_pAllocator->Alloc( m_MemoryBlockSize );
- m_OutOfBlockSlot = m_pCurrentSlot + m_MemoryBlockSize / sizeof( size_t );
+ MemoryBlock* pMemBlock = m_MemoryBlocks.AppendNew(m_pAllocator, m_MemoryBlockSize);
- MemoryBlockDesc* pMemBlockDesc = (MemoryBlockDesc*) m_pAllocator->Alloc( sizeof( MemoryBlockDesc ) );
- _ASSERTE( IS_ALIGNED( pMemBlockDesc, sizeof( void* ) ) );
-
- pMemBlockDesc->Init();
- pMemBlockDesc->StartAddress = m_pCurrentSlot;
- if (m_MemoryBlocksTail != NULL)
- {
- _ASSERTE(m_MemoryBlocksHead != NULL);
- m_MemoryBlocksTail->m_Next = pMemBlockDesc;
- }
- else
- {
- _ASSERTE(m_MemoryBlocksHead == NULL);
- m_MemoryBlocksHead = pMemBlockDesc;
- }
- m_MemoryBlocksTail = pMemBlockDesc;
+ m_pCurrentSlot = pMemBlock->Contents;
+ m_OutOfBlockSlot = m_pCurrentSlot + m_MemoryBlockSize / sizeof( size_t );
#ifdef _DEBUG
m_MemoryBlocksCount++;