Allocator refactor

This commit is contained in:
elasota
2021-04-28 01:46:07 -04:00
parent 472462c535
commit a2d374f650
75 changed files with 473 additions and 253 deletions

View File

@@ -371,6 +371,6 @@ uint32_t PortabilityLayer::DeflateContext::CRC32(uint32_t inputValue, const void
PortabilityLayer::InflateContext *PortabilityLayer::InflateContext::Create()
{
{
return InflateContextImpl::Create();
}

View File

@@ -3,6 +3,7 @@
#include "IGpDisplayDriver.h"
#include "IGpLogDriver.h"
#include "IGpSystemServices.h"
#include "MemoryManager.h"
#include "ResourceManager.h"
#include "QDPixMap.h"
#include "Rect2i.h"
@@ -361,7 +362,7 @@ namespace PortabilityLayer
void DialogTemplate::Destroy()
{
this->~DialogTemplate();
free(this);
DisposePtr(this);
}
ArrayView<const DialogTemplateItem> DialogTemplate::GetItems() const
@@ -374,7 +375,7 @@ namespace PortabilityLayer
PortabilityLayer::WindowManager::GetInstance()->DestroyWindow(m_window);
this->~DialogImpl();
free(this);
DisposePtr(this);
}
Window *DialogImpl::GetWindow() const
@@ -646,7 +647,7 @@ namespace PortabilityLayer
const size_t itemsSize = sizeof(DialogItem) * numItems;
void *storage = malloc(alignedSize + itemsSize);
void *storage = NewPtr(alignedSize + itemsSize);
if (!storage)
return nullptr;
@@ -882,7 +883,7 @@ namespace PortabilityLayer
const size_t dtlItemSize = sizeof(DialogTemplateItem) * numItems;
void *storage = malloc(dtlAlignedSize + dtlItemSize);
void *storage = NewPtr(dtlAlignedSize + dtlItemSize);
if (!storage)
{
dtemplateH.Dispose();

View File

@@ -10,6 +10,7 @@
#include "ResTypeID.h"
#include "ZipFileProxy.h"
#include "PLCore.h"
#include "PLDrivers.h"
#include "PLPasStr.h"
#include "PLErrorCodes.h"
@@ -474,12 +475,12 @@ namespace PortabilityLayer
void CompositeFileImpl::Close()
{
this->~CompositeFileImpl();
free(this);
DisposePtr(this);
}
CompositeFileImpl *CompositeFileImpl::Create(VirtualDirectory_t dirID, const PLPasStr &filename, GpIOStream *stream, ZipFileProxy *zipFile, const MacFileProperties &mfp, bool resInline, bool dataInline, size_t inlineDataIndex)
{
void *storage = malloc(sizeof(CompositeFileImpl));
void *storage = NewPtr(sizeof(CompositeFileImpl));
if (!storage)
return nullptr;

View File

@@ -1,4 +1,5 @@
#include "FileSectionStream.h"
#include "PLCore.h"
#include <stdlib.h>
#include <new>
@@ -151,7 +152,7 @@ namespace PortabilityLayer
void FileSectionStreamImpl::GP_ASYNCIFY_PARANOID_NAMED(Close)()
{
this->~FileSectionStreamImpl();
free(this);
DisposePtr(this);
}
void FileSectionStreamImpl::Flush()
@@ -161,7 +162,7 @@ namespace PortabilityLayer
GpIOStream *FileSectionStream::Create(GpIOStream *stream, GpUFilePos_t start, GpUFilePos_t size)
{
void *storage = malloc(sizeof(FileSectionStreamImpl));
void *storage = NewPtr(sizeof(FileSectionStreamImpl));
if (!storage)
return nullptr;

View File

@@ -7,6 +7,7 @@
#include "MemReaderStream.h"
#include "MemoryManager.h"
#include "PLCore.h"
#include "PLDrivers.h"
#include <stdlib.h>
@@ -158,7 +159,7 @@ namespace PortabilityLayer
FontFamily *FontFamily::Create(FontFamilyID_t familyID)
{
void *storage = malloc(sizeof(FontFamily));
void *storage = NewPtr(sizeof(FontFamily));
if (!storage)
return nullptr;
@@ -168,7 +169,7 @@ namespace PortabilityLayer
void FontFamily::Destroy()
{
this->~FontFamily();
free(this);
DisposePtr(this);
}
FontFamily::FontFamily(FontFamilyID_t familyID)

View File

@@ -11,6 +11,7 @@
#include "GpRenderedGlyphMetrics.h"
#include "PLBigEndian.h"
#include "PLCore.h"
#include "PLDrivers.h"
#include "PLPasStr.h"
#include "DeflateCodec.h"
@@ -166,7 +167,7 @@ namespace PortabilityLayer
void RenderedFontImpl::Destroy()
{
this->~RenderedFontImpl();
free(this);
DisposePtr(this);
}
void RenderedFontImpl::SetCharData(unsigned int charID, const void *data, size_t dataOffset, const GpRenderedGlyphMetrics &metrics)
@@ -251,7 +252,7 @@ namespace PortabilityLayer
const size_t allocSize = alignedPrefixSize + glyphDataSize;
void *storage = malloc(allocSize);
void *storage = NewPtr(allocSize);
if (!storage)
return nullptr;

View File

@@ -1,5 +1,6 @@
#include "InflateStream.h"
#include "DeflateCodec.h"
#include "PLCore.h"
#include <stdlib.h>
#include <new>
@@ -225,7 +226,7 @@ namespace PortabilityLayer
void InflateStreamImpl::GP_ASYNCIFY_PARANOID_NAMED(Close)()
{
this->~InflateStreamImpl();
free(this);
DisposePtr(this);
}
void InflateStreamImpl::Flush()
@@ -238,7 +239,7 @@ namespace PortabilityLayer
if (!inflateContext)
return nullptr;
void *storage = malloc(sizeof(InflateStreamImpl));
void *storage = NewPtr(sizeof(InflateStreamImpl));
if (!storage)
{
inflateContext->Destroy();

View File

@@ -1,12 +0,0 @@
#include "MMBlock.h"
namespace PortabilityLayer
{
size_t MMBlock::AlignedSize()
{
const size_t paddedSize = sizeof(MMBlock) + GP_SYSTEM_MEMORY_ALIGNMENT - 1;
const size_t paddedSizeTruncated = paddedSize - (paddedSize % GP_SYSTEM_MEMORY_ALIGNMENT);
return paddedSizeTruncated;
}
}

View File

@@ -1,20 +0,0 @@
#pragma once
#ifndef __PL_MM_BLOCK_H__
#define __PL_MM_BLOCK_H__
#include <stdint.h>
#include "CoreDefs.h"
#include "SmallestInt.h"
namespace PortabilityLayer
{
struct MMBlock
{
SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t m_offsetFromAllocLocation;
static size_t AlignedSize();
};
}
#endif

View File

@@ -1,8 +1,9 @@
#include "MemoryManager.h"
#include "MMBlock.h"
#include "MMHandleBlock.h"
#include "ResourceCompiledRef.h"
#include "ResourceManager.h"
#include "IGpAllocator.h"
#include "PLDrivers.h"
#include <stdlib.h>
#include <new>
@@ -28,7 +29,6 @@ namespace PortabilityLayer
static MemoryManagerImpl *GetInstance();
private:
static MemoryManagerImpl ms_instance;
};
@@ -42,75 +42,17 @@ namespace PortabilityLayer
void *MemoryManagerImpl::Realloc(void *buf, size_t newSize)
{
assert(buf != nullptr);
const size_t mmBlockSize = MMBlock::AlignedSize();
uint8_t *oldBufBytes = static_cast<uint8_t*>(buf);
const MMBlock *oldBufMMBlock = reinterpret_cast<const MMBlock*>(oldBufBytes - MMBlock::AlignedSize());
const size_t oldBufOffsetFromAlignLoc = oldBufMMBlock->m_offsetFromAllocLocation;
uint8_t *oldBufBase = oldBufBytes - MMBlock::AlignedSize() - oldBufOffsetFromAlignLoc;
const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1;
if (SIZE_MAX - newSize < mmBlockSizeWithMaxPadding)
return nullptr;
const size_t newBufferSize = newSize + mmBlockSizeWithMaxPadding;
uint8_t *newBuffer = static_cast<uint8_t*>(realloc(oldBufBase, newSize + mmBlockSizeWithMaxPadding));
if (!newBuffer)
return nullptr;
const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(newBuffer) & static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT - 1);
intptr_t alignPadding = 0;
if (offsetFromAlignPoint != 0)
alignPadding = static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint;
// Check if the alignment changed, if so relocate
if (static_cast<size_t>(alignPadding) != oldBufOffsetFromAlignLoc)
memmove(newBuffer + alignPadding, newBuffer + oldBufOffsetFromAlignLoc, MMBlock::AlignedSize() + newSize);
MMBlock *newMMBlock = reinterpret_cast<MMBlock*>(newBuffer + alignPadding);
newMMBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding);
return newBuffer + alignPadding + MMBlock::AlignedSize();
return PLDrivers::GetAlloc()->Realloc(buf, newSize);
}
void *MemoryManagerImpl::Alloc(size_t size)
{
if (size == 0)
return nullptr;
const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1;
if (SIZE_MAX - size < mmBlockSizeWithMaxPadding)
return nullptr;
uint8_t *buffer = static_cast<uint8_t*>(malloc(size + mmBlockSizeWithMaxPadding));
if (!buffer)
return nullptr;
const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(buffer) & static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT - 1);
intptr_t alignPadding = 0;
if (offsetFromAlignPoint != 0)
alignPadding = static_cast<intptr_t>(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint;
MMBlock *mmBlock = reinterpret_cast<MMBlock*>(buffer + alignPadding);
mmBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<GP_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding);
return buffer + alignPadding + MMBlock::AlignedSize();
return PLDrivers::GetAlloc()->Realloc(nullptr, size);
}
void MemoryManagerImpl::Release(void *buf)
{
if (!buf)
return;
const size_t mmBlockSize = MMBlock::AlignedSize();
uint8_t *bytes = static_cast<uint8_t*>(buf);
const MMBlock *mmBlock = reinterpret_cast<const MMBlock*>(bytes - MMBlock::AlignedSize());
void *freeLoc = bytes - MMBlock::AlignedSize() - mmBlock->m_offsetFromAllocLocation;
free(freeLoc);
PLDrivers::GetAlloc()->Realloc(buf, 0);
}
MMHandleBlock *MemoryManagerImpl::AllocHandle(size_t size)

View File

@@ -1,10 +1,10 @@
#pragma once
#ifndef __PL_MEMORY_MANAGER_H__
#define __PL_MEMORY_MANAGER_H__
#include <stdint.h>
#include <stddef.h>
struct IGpAllocator;
namespace PortabilityLayer
{
struct MMHandleBlock;
@@ -50,5 +50,3 @@ namespace PortabilityLayer
return objectHdl;
}
}
#endif

View File

@@ -288,7 +288,7 @@ namespace PortabilityLayer
if (m_iconGraphic)
{
m_iconGraphic->~SimpleGraphic();
free(m_iconGraphic);
DisposePtr(m_iconGraphic);
}
// GP TODO: Dispose of menus properly
@@ -834,7 +834,7 @@ namespace PortabilityLayer
{
typedef SimpleGraphicInstanceStandardPalette<16, 16> GraphicType_t;
void *storage = static_cast<GraphicType_t*>(malloc(sizeof(GraphicType_t)));
void *storage = static_cast<GraphicType_t*>(NewPtr(sizeof(GraphicType_t)));
if (storage)
{
memcpy(m_iconMask, static_cast<const uint8_t*>(*icsHandle) + 32, 32);

View File

@@ -50,4 +50,10 @@ IGpVOSEventQueue *PLDrivers::GetVOSEventQueue()
return ms_drivers.GetDriver<GpDriverIDs::kEventQueue>();
}
IGpAllocator *PLDrivers::GetAlloc()
{
return ms_drivers.GetDriver<GpDriverIDs::kAlloc>();
}
GpDriverCollection PLDrivers::ms_drivers;

View File

@@ -16,6 +16,7 @@ public:
static IGpSystemServices *GetSystemServices();
static IGpFontHandler *GetFontHandler();
static IGpVOSEventQueue *GetVOSEventQueue();
static IGpAllocator *GetAlloc();
private:
static GpDriverCollection ms_drivers;

View File

@@ -124,7 +124,6 @@
<ClInclude Include="MacRsrcMap.h" />
<ClInclude Include="MemoryManager.h" />
<ClInclude Include="MemReaderStream.h" />
<ClInclude Include="MMBlock.h" />
<ClInclude Include="MMHandleBlock.h" />
<ClInclude Include="PascalStr.h" />
<ClInclude Include="PascalStrLiteral.h" />
@@ -250,7 +249,6 @@
<ClCompile Include="MemoryManager.cpp" />
<ClCompile Include="MemReaderStream.cpp" />
<ClCompile Include="MenuManager.cpp" />
<ClCompile Include="MMBlock.cpp" />
<ClCompile Include="MMHandleBlock.cpp" />
<ClCompile Include="PLApplication.cpp" />
<ClCompile Include="PLButtonWidget.cpp" />

View File

@@ -132,9 +132,6 @@
<ClInclude Include="MMHandleBlock.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MMBlock.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DisplayDeviceManager.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -494,9 +491,6 @@
<ClCompile Include="MemoryManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MMBlock.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MMHandleBlock.cpp">
<Filter>Source Files</Filter>
</ClCompile>

View File

@@ -26,7 +26,6 @@
#include "MemoryManager.cpp"
#include "MemReaderStream.cpp"
#include "MenuManager.cpp"
#include "MMBlock.cpp"
#include "MMHandleBlock.cpp"
#include "PLApplication.cpp"
#include "PLButtonWidget.cpp"

View File

@@ -3,6 +3,8 @@
#include "ScanlineMaskBuilder.h"
#include "ScanlineMaskIterator.h"
#include "PLCore.h"
#include <stdlib.h>
#include <new>
@@ -11,7 +13,7 @@ namespace PortabilityLayer
void ScanlineMask::Destroy()
{
this->~ScanlineMask();
free(this);
DisposePtr(this);
}
const Rect &ScanlineMask::GetRect() const
@@ -50,7 +52,7 @@ namespace PortabilityLayer
else
return nullptr;
void *storage = malloc(alignedPrefixSize + storageSize);
void *storage = NewPtr(alignedPrefixSize + storageSize);
if (!storage)
return nullptr;

View File

@@ -7,6 +7,7 @@
#include "LinePlotter.h"
#include "ScanlineMaskBuilder.h"
#include "IPlotter.h"
#include "PLCore.h"
#include <assert.h>
#include <algorithm>
@@ -135,7 +136,7 @@ namespace PortabilityLayer
#else
const size_t storageSize = (numElements * 2 + 7) / 8;
#endif
void *storage = malloc(storageSize);
void *storage = NewPtr(storageSize);
if (!storage)
return nullptr;
@@ -250,7 +251,7 @@ namespace PortabilityLayer
stbi_write_png(path, width, height, 4, flagBits, width * 4);
#endif
free(storage);
DisposePtr(storage);
return ScanlineMask::Create(Rect::Create(minPoint.m_y, minPoint.m_x, minPoint.m_y + static_cast<int16_t>(height), minPoint.m_x + static_cast<int16_t>(width)), maskBuilder);
}

View File

@@ -2,6 +2,7 @@
#include "IGpThreadEvent.h"
#include "IGpSystemServices.h"
#include "PLCore.h"
#include "PLDrivers.h"
#include <stdlib.h>
@@ -39,7 +40,7 @@ namespace PortabilityLayer
void PortabilityLayer::WorkerThreadImpl::Destroy()
{
this->~WorkerThreadImpl();
free(this);
DisposePtr(this);
}
void PortabilityLayer::WorkerThreadImpl::AsyncExecuteTask(PortabilityLayer::WorkerThread::Callback_t callback, void *context)
@@ -139,7 +140,7 @@ PortabilityLayer::WorkerThread::~WorkerThread()
PortabilityLayer::WorkerThread *PortabilityLayer::WorkerThread::Create()
{
void *storage = malloc(sizeof(PortabilityLayer::WorkerThreadImpl));
void *storage = NewPtr(sizeof(PortabilityLayer::WorkerThreadImpl));
if (!storage)
return nullptr;