m_pBuffer = new BYTE[bufferSize];
memset(m_pBuffer, 0, bufferSize);
- m_pCurrent = m_pBuffer;
m_pLimit = m_pBuffer + bufferSize;
+ m_pCurrent = GetNextAlignedAddress(m_pBuffer);
m_mostRecentTimeStamp.QuadPart = 0;
m_pLastPoppedEvent = NULL;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(pThread != NULL);
+ PRECONDITION(((size_t)m_pCurrent % AlignmentSize) == 0);
}
CONTRACTL_END;
if(success)
{
// Advance the current pointer past the event.
- m_pCurrent += eventSize;
+ m_pCurrent = GetNextAlignedAddress(m_pCurrent + eventSize);
}
return success;
CONTRACTL_END;
memset(m_pBuffer, 0, (size_t)(m_pLimit - m_pBuffer));
- m_pCurrent = m_pBuffer;
+ m_pCurrent = GetNextAlignedAddress(m_pBuffer);
m_mostRecentTimeStamp.QuadPart = 0;
m_pLastPoppedEvent = NULL;
}
if(pEvent == NULL)
{
// If this buffer contains an event, select it.
- if(m_pCurrent > m_pBuffer)
+ BYTE *pFirstAlignedInstance = GetNextAlignedAddress(m_pBuffer);
+ if(m_pCurrent > pFirstAlignedInstance)
{
- pNextInstance = (EventPipeEventInstance*)m_pBuffer;
+ pNextInstance = (EventPipeEventInstance*)pFirstAlignedInstance;
}
else
{
// We have a pointer within the bounds of the buffer.
// Find the next event by skipping the current event with it's data payload immediately after the instance.
- pNextInstance = (EventPipeEventInstance *)(pEvent->GetData() + pEvent->GetDataLength());
+ pNextInstance = (EventPipeEventInstance *)GetNextAlignedAddress(const_cast<BYTE *>(pEvent->GetData() + pEvent->GetDataLength()));
// Check to see if we've reached the end of the written portion of the buffer.
if((BYTE*)pNextInstance >= m_pCurrent)
CONTRACTL_END;
// Check to see if the buffer is empty.
- if(m_pBuffer == m_pCurrent)
+ if(GetNextAlignedAddress(m_pBuffer) == m_pCurrent)
{
// Make sure that the buffer size is greater than zero.
_ASSERTE(m_pBuffer != m_pLimit);
}
// Validate the contents of the filled portion of the buffer.
- BYTE *ptr = m_pBuffer;
+ BYTE *ptr = GetNextAlignedAddress(m_pBuffer);
while(ptr < m_pCurrent)
{
// Validate the event.
_ASSERTE((pInstance->GetData() != NULL && pInstance->GetDataLength() > 0) || (pInstance->GetData() == NULL && pInstance->GetDataLength() == 0));
// Skip the event.
- ptr += sizeof(*pInstance) + pInstance->GetDataLength();
+ ptr = GetNextAlignedAddress(ptr + sizeof(*pInstance) + pInstance->GetDataLength());
}
// When we're done walking the filled portion of the buffer,
private:
+ // Instances of EventPipeEventInstance in the buffer must be 8-byte aligned.
+ // It is OK for the data payloads to be unaligned because they are opaque blobs that are copied via memcpy.
+ const size_t AlignmentSize = 8;
+
// A pointer to the actual buffer.
BYTE *m_pBuffer;
m_pNextBuffer = pBuffer;
}
+ FORCEINLINE BYTE* GetNextAlignedAddress(BYTE *pAddress)
+ {
+ LIMITED_METHOD_CONTRACT;
+ _ASSERTE(m_pBuffer <= pAddress && m_pLimit > pAddress);
+
+ pAddress = (BYTE*)ALIGN_UP(pAddress, AlignmentSize);
+
+ _ASSERTE((size_t)pAddress % AlignmentSize == 0);
+ return pAddress;
+ }
+
public:
EventPipeBuffer(unsigned int bufferSize);