Make aligned buffer allocation a common function, out of serialiser

This commit is contained in:
baldurk
2017-11-03 16:15:19 +00:00
parent eb7e14d46d
commit 9ef0c5282c
14 changed files with 85 additions and 85 deletions
+3 -44
View File
@@ -249,7 +249,7 @@ Chunk::Chunk(Serialiser *ser, uint32_t chunkType, bool temporary)
if(ser->HasAlignedData())
{
m_Data = Serialiser::AllocAlignedBuffer(m_Length);
m_Data = AllocAlignedBuffer(m_Length);
m_AlignedData = true;
}
else
@@ -289,7 +289,7 @@ Chunk *Chunk::Duplicate()
ret->m_AlignedData = m_AlignedData;
if(m_AlignedData)
ret->m_Data = Serialiser::AllocAlignedBuffer(m_Length);
ret->m_Data = AllocAlignedBuffer(m_Length);
else
ret->m_Data = new byte[m_Length];
@@ -321,7 +321,7 @@ Chunk::~Chunk()
if(m_AlignedData)
{
if(m_Data)
Serialiser::FreeAlignedBuffer(m_Data);
FreeAlignedBuffer(m_Data);
m_Data = NULL;
}
@@ -1157,47 +1157,6 @@ void Serialiser::ReadFromFile(uint64_t bufferOffs, size_t length)
}
}
byte *Serialiser::AllocAlignedBuffer(size_t size, size_t alignment)
{
byte *rawAlloc = NULL;
#if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
try
#endif
{
rawAlloc = new byte[size + sizeof(byte *) + alignment];
}
#if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
catch(std::bad_alloc &)
{
rawAlloc = NULL;
}
#endif
if(rawAlloc == NULL)
RDCFATAL("Allocation for %llu bytes failed", (uint64_t)size);
RDCASSERT(rawAlloc);
byte *alignedAlloc = (byte *)AlignUp((size_t)(rawAlloc + sizeof(byte *)), alignment);
byte **realPointer = (byte **)alignedAlloc;
realPointer[-1] = rawAlloc;
return alignedAlloc;
}
void Serialiser::FreeAlignedBuffer(byte *buf)
{
if(buf == NULL)
return;
byte **realPointer = (byte **)buf;
byte *rawAlloc = realPointer[-1];
delete[] rawAlloc;
}
void Serialiser::SetPersistentBlock(uint64_t offs)
{
// as long as this is called immediately after pushing the chunk context at the