EOL fixes

This commit is contained in:
elasota
2020-11-10 20:05:11 -05:00
parent 0f630a74a2
commit b47813330a

View File

@@ -1,178 +1,178 @@
#include "MemoryManager.h" #include "MemoryManager.h"
#include "MMBlock.h" #include "MMBlock.h"
#include "MMHandleBlock.h" #include "MMHandleBlock.h"
#include "ResourceCompiledRef.h" #include "ResourceCompiledRef.h"
#include "ResourceManager.h" #include "ResourceManager.h"
#include <stdlib.h> #include <stdlib.h>
#include <new> #include <new>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
namespace PortabilityLayer namespace PortabilityLayer
{ {
class MemoryManagerImpl final : public MemoryManager class MemoryManagerImpl final : public MemoryManager
{ {
public: public:
void Init() override; void Init() override;
void Shutdown() override; void Shutdown() override;
void *Alloc(size_t size) override; void *Alloc(size_t size) override;
void *Realloc(void *buf, size_t newSize) override; void *Realloc(void *buf, size_t newSize) override;
void Release(void *buf) override; void Release(void *buf) override;
MMHandleBlock *AllocHandle(size_t size) override; MMHandleBlock *AllocHandle(size_t size) override;
bool ResizeHandle(MMHandleBlock *hdl, size_t newSize) override; bool ResizeHandle(MMHandleBlock *hdl, size_t newSize) override;
void ReleaseHandle(MMHandleBlock *hdl) override; void ReleaseHandle(MMHandleBlock *hdl) override;
static MemoryManagerImpl *GetInstance(); static MemoryManagerImpl *GetInstance();
private: private:
static MemoryManagerImpl ms_instance; static MemoryManagerImpl ms_instance;
}; };
void MemoryManagerImpl::Init() void MemoryManagerImpl::Init()
{ {
} }
void MemoryManagerImpl::Shutdown() void MemoryManagerImpl::Shutdown()
{ {
} }
void *MemoryManagerImpl::Realloc(void *buf, size_t newSize) void *MemoryManagerImpl::Realloc(void *buf, size_t newSize)
{ {
assert(buf != nullptr); assert(buf != nullptr);
const size_t mmBlockSize = MMBlock::AlignedSize(); const size_t mmBlockSize = MMBlock::AlignedSize();
uint8_t *oldBufBytes = static_cast<uint8_t*>(buf); uint8_t *oldBufBytes = static_cast<uint8_t*>(buf);
const MMBlock *oldBufMMBlock = reinterpret_cast<const MMBlock*>(oldBufBytes - MMBlock::AlignedSize()); const MMBlock *oldBufMMBlock = reinterpret_cast<const MMBlock*>(oldBufBytes - MMBlock::AlignedSize());
const size_t oldBufOffsetFromAlignLoc = oldBufMMBlock->m_offsetFromAllocLocation; const size_t oldBufOffsetFromAlignLoc = oldBufMMBlock->m_offsetFromAllocLocation;
uint8_t *oldBufBase = oldBufBytes - MMBlock::AlignedSize() - oldBufOffsetFromAlignLoc; uint8_t *oldBufBase = oldBufBytes - MMBlock::AlignedSize() - oldBufOffsetFromAlignLoc;
const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1; const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1;
if (SIZE_MAX - newSize < mmBlockSizeWithMaxPadding) if (SIZE_MAX - newSize < mmBlockSizeWithMaxPadding)
return nullptr; return nullptr;
const size_t newBufferSize = newSize + mmBlockSizeWithMaxPadding; const size_t newBufferSize = newSize + mmBlockSizeWithMaxPadding;
uint8_t *newBuffer = static_cast<uint8_t*>(realloc(oldBufBase, newSize + mmBlockSizeWithMaxPadding)); uint8_t *newBuffer = static_cast<uint8_t*>(realloc(oldBufBase, newSize + mmBlockSizeWithMaxPadding));
if (!newBuffer) if (!newBuffer)
return nullptr; return nullptr;
const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(newBuffer) & static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT - 1); const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(newBuffer) & static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT - 1);
intptr_t alignPadding = 0; intptr_t alignPadding = 0;
if (offsetFromAlignPoint != 0) if (offsetFromAlignPoint != 0)
alignPadding = static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint; alignPadding = static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint;
// Check if the alignment changed, if so relocate // Check if the alignment changed, if so relocate
if (static_cast<size_t>(alignPadding) != oldBufOffsetFromAlignLoc) if (static_cast<size_t>(alignPadding) != oldBufOffsetFromAlignLoc)
memmove(newBuffer + alignPadding, newBuffer + oldBufOffsetFromAlignLoc, MMBlock::AlignedSize() + newSize); memmove(newBuffer + alignPadding, newBuffer + oldBufOffsetFromAlignLoc, MMBlock::AlignedSize() + newSize);
MMBlock *newMMBlock = reinterpret_cast<MMBlock*>(newBuffer + alignPadding); MMBlock *newMMBlock = reinterpret_cast<MMBlock*>(newBuffer + alignPadding);
newMMBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding); newMMBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding);
return newBuffer + alignPadding + MMBlock::AlignedSize(); return newBuffer + alignPadding + MMBlock::AlignedSize();
} }
void *MemoryManagerImpl::Alloc(size_t size) void *MemoryManagerImpl::Alloc(size_t size)
{ {
if (size == 0) if (size == 0)
return nullptr; return nullptr;
const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1; const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1;
if (SIZE_MAX - size < mmBlockSizeWithMaxPadding) if (SIZE_MAX - size < mmBlockSizeWithMaxPadding)
return nullptr; return nullptr;
uint8_t *buffer = static_cast<uint8_t*>(malloc(size + mmBlockSizeWithMaxPadding)); uint8_t *buffer = static_cast<uint8_t*>(malloc(size + mmBlockSizeWithMaxPadding));
if (!buffer) if (!buffer)
return nullptr; return nullptr;
const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(buffer) & static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT - 1); const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(buffer) & static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT - 1);
intptr_t alignPadding = 0; intptr_t alignPadding = 0;
if (offsetFromAlignPoint != 0) if (offsetFromAlignPoint != 0)
alignPadding = static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint; alignPadding = static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint;
MMBlock *mmBlock = reinterpret_cast<MMBlock*>(buffer + alignPadding); MMBlock *mmBlock = reinterpret_cast<MMBlock*>(buffer + alignPadding);
mmBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding); mmBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding);
return buffer + alignPadding + MMBlock::AlignedSize(); return buffer + alignPadding + MMBlock::AlignedSize();
} }
void MemoryManagerImpl::Release(void *buf) void MemoryManagerImpl::Release(void *buf)
{ {
if (!buf) if (!buf)
return; return;
const size_t mmBlockSize = MMBlock::AlignedSize(); const size_t mmBlockSize = MMBlock::AlignedSize();
uint8_t *bytes = static_cast<uint8_t*>(buf); uint8_t *bytes = static_cast<uint8_t*>(buf);
const MMBlock *mmBlock = reinterpret_cast<const MMBlock*>(bytes - MMBlock::AlignedSize()); const MMBlock *mmBlock = reinterpret_cast<const MMBlock*>(bytes - MMBlock::AlignedSize());
free(bytes - MMBlock::AlignedSize() - mmBlock->m_offsetFromAllocLocation); free(bytes - MMBlock::AlignedSize() - mmBlock->m_offsetFromAllocLocation);
} }
MMHandleBlock *MemoryManagerImpl::AllocHandle(size_t size) MMHandleBlock *MemoryManagerImpl::AllocHandle(size_t size)
{ {
void *contents = Alloc(size); void *contents = Alloc(size);
MMHandleBlock *handleBlock = static_cast<MMHandleBlock*>(Alloc(sizeof(MMHandleBlock))); MMHandleBlock *handleBlock = static_cast<MMHandleBlock*>(Alloc(sizeof(MMHandleBlock)));
return new (handleBlock) MMHandleBlock(contents, size); return new (handleBlock) MMHandleBlock(contents, size);
} }
bool MemoryManagerImpl::ResizeHandle(MMHandleBlock *hdl, size_t newSize) bool MemoryManagerImpl::ResizeHandle(MMHandleBlock *hdl, size_t newSize)
{ {
if (hdl->m_contents == nullptr) if (hdl->m_contents == nullptr)
{ {
if (newSize != 0) if (newSize != 0)
{ {
void *newBuf = Alloc(newSize); void *newBuf = Alloc(newSize);
if (!newBuf) if (!newBuf)
return false; return false;
hdl->m_contents = newBuf; hdl->m_contents = newBuf;
hdl->m_size = newSize; hdl->m_size = newSize;
} }
} }
if (newSize != hdl->m_size) if (newSize != hdl->m_size)
{ {
void *newBuf = Realloc(hdl->m_contents, newSize); void *newBuf = Realloc(hdl->m_contents, newSize);
if (!newBuf) if (!newBuf)
return false; return false;
hdl->m_contents = newBuf; hdl->m_contents = newBuf;
hdl->m_size = newSize; hdl->m_size = newSize;
} }
return true; return true;
} }
void MemoryManagerImpl::ReleaseHandle(MMHandleBlock *hdl) void MemoryManagerImpl::ReleaseHandle(MMHandleBlock *hdl)
{ {
if (!hdl) if (!hdl)
return; return;
if (hdl->m_rmSelfRef) if (hdl->m_rmSelfRef)
PortabilityLayer::ResourceManager::GetInstance()->DissociateHandle(hdl); PortabilityLayer::ResourceManager::GetInstance()->DissociateHandle(hdl);
if (hdl->m_contents) if (hdl->m_contents)
Release(hdl->m_contents); Release(hdl->m_contents);
hdl->~MMHandleBlock(); hdl->~MMHandleBlock();
Release(hdl); Release(hdl);
} }
MemoryManagerImpl *MemoryManagerImpl::GetInstance() MemoryManagerImpl *MemoryManagerImpl::GetInstance()
{ {
return &ms_instance; return &ms_instance;
} }
MemoryManagerImpl MemoryManagerImpl::ms_instance; MemoryManagerImpl MemoryManagerImpl::ms_instance;
MemoryManager *MemoryManager::GetInstance() MemoryManager *MemoryManager::GetInstance()
{ {
return MemoryManagerImpl::GetInstance(); return MemoryManagerImpl::GetInstance();
} }
} }