From a2d374f650d880a56cdbdf72a4fd9e50a2eb95f7 Mon Sep 17 00:00:00 2001 From: elasota Date: Wed, 28 Apr 2021 01:46:07 -0400 Subject: [PATCH] Allocator refactor --- Aerofoil/Aerofoil.vcxproj | 3 + Aerofoil/Aerofoil.vcxproj.filters | 3 + Aerofoil/GpBWCursor_Win32.cpp | 27 ++-- Aerofoil/GpBWCursor_Win32.h | 7 +- Aerofoil/GpColorCursor_Win32.cpp | 17 ++- Aerofoil/GpColorCursor_Win32.h | 6 +- Aerofoil/GpFileSystem_Win32.cpp | 28 +++-- Aerofoil/GpFileSystem_Win32.h | 2 + Aerofoil/GpLogDriver_Win32.cpp | 6 +- Aerofoil/GpLogDriver_Win32.h | 2 + Aerofoil/GpMain_Win32.cpp | 11 +- Aerofoil/GpMutex_Win32.cpp | 13 +- Aerofoil/GpMutex_Win32.h | 7 +- Aerofoil/GpSystemServices_Win32.cpp | 8 +- Aerofoil/GpSystemServices_Win32.h | 2 + Aerofoil/GpThreadEvent_Win32.cpp | 13 +- Aerofoil/GpThreadEvent_Win32.h | 5 +- AerofoilPortable.props | 8 +- AerofoilPortable/GpAllocator_C.cpp | 115 ++++++++++++++++++ AerofoilPortable/GpAllocator_C.h | 17 +++ AerofoilSDL/AerofoilSDL.vcxproj | 1 + AerofoilSDL/AerofoilSDL.vcxproj.filters | 3 + AerofoilSDL/GpMain_SDL_Win32.cpp | 3 + ConvertColorCursors/ConvertColorCursors.cpp | 5 + .../ConvertColorCursors.vcxproj | 3 + .../ConvertColorCursors.vcxproj.filters | 3 + GenerateFonts/GenerateFonts.cpp | 7 ++ GenerateFonts/GenerateFonts.vcxproj | 3 + GenerateFonts/GenerateFonts.vcxproj.filters | 3 + .../GpAudioBufferXAudio2.cpp | 16 +-- GpAudioDriver_XAudio2/GpAudioBufferXAudio2.h | 7 +- .../GpAudioChannelXAudio2.cpp | 15 ++- GpAudioDriver_XAudio2/GpAudioChannelXAudio2.h | 6 +- .../GpAudioDriverXAudio2.cpp | 5 +- GpCommon/GpAudioDriverProperties.h | 2 + GpCommon/GpDisplayDriverProperties.h | 2 + GpCommon/GpDriverIndex.h | 2 + GpCommon/GpFontHandlerProperties.h | 4 + GpCommon/GpInputDriverProperties.h | 2 + GpCommon/GpWindows.h | 5 +- GpCommon/IGpAllocator.h | 12 ++ .../GpDisplayDriverD3D11.cpp | 13 +- .../GpDisplayDriverSurfaceD3D11.cpp | 13 +- .../GpDisplayDriverSurfaceD3D11.h | 6 +- .../GpFontHandler_FreeType2.cpp | 61 ++++++---- .../GpFontHandler_FreeType2.h | 6 +- GpInputDriver_XInput/GpInputDriverXInput.cpp | 6 +- GpShell/GpGlobalConfig.h | 2 + GpShell/GpMain.cpp | 10 +- MergeGPF/MergeGPF.cpp | 5 + MergeGPF/MergeGPF.vcxproj | 3 + MergeGPF/MergeGPF.vcxproj.filters | 3 + PortabilityLayer/DeflateCodec.cpp | 2 +- PortabilityLayer/DialogManager.cpp | 9 +- PortabilityLayer/FileManager.cpp | 5 +- PortabilityLayer/FileSectionStream.cpp | 5 +- PortabilityLayer/FontFamily.cpp | 5 +- PortabilityLayer/FontRenderer.cpp | 5 +- PortabilityLayer/InflateStream.cpp | 5 +- PortabilityLayer/MMBlock.cpp | 12 -- PortabilityLayer/MMBlock.h | 20 --- PortabilityLayer/MemoryManager.cpp | 68 +---------- PortabilityLayer/MemoryManager.h | 6 +- PortabilityLayer/MenuManager.cpp | 4 +- PortabilityLayer/PLDrivers.cpp | 6 + PortabilityLayer/PLDrivers.h | 1 + PortabilityLayer/PortabilityLayer.vcxproj | 2 - .../PortabilityLayer.vcxproj.filters | 6 - .../PortabilityLayer_Combined.cpp | 1 - PortabilityLayer/ScanlineMask.cpp | 6 +- PortabilityLayer/ScanlineMaskConverter.cpp | 5 +- PortabilityLayer/WorkerThread.cpp | 5 +- gpr2gpa/gpr2gpa.cpp | 5 + gpr2gpa/gpr2gpa.vcxproj | 3 + gpr2gpa/gpr2gpa.vcxproj.filters | 3 + 75 files changed, 473 insertions(+), 253 deletions(-) create mode 100644 AerofoilPortable/GpAllocator_C.cpp create mode 100644 AerofoilPortable/GpAllocator_C.h create mode 100644 GpCommon/IGpAllocator.h delete mode 100644 PortabilityLayer/MMBlock.cpp delete mode 100644 PortabilityLayer/MMBlock.h diff --git a/Aerofoil/Aerofoil.vcxproj b/Aerofoil/Aerofoil.vcxproj index 074ea6e..9a46344 100644 --- a/Aerofoil/Aerofoil.vcxproj +++ b/Aerofoil/Aerofoil.vcxproj @@ -43,6 +43,7 @@ + @@ -52,6 +53,7 @@ + @@ -82,6 +84,7 @@ + diff --git a/Aerofoil/Aerofoil.vcxproj.filters b/Aerofoil/Aerofoil.vcxproj.filters index ded91bd..6e8d027 100644 --- a/Aerofoil/Aerofoil.vcxproj.filters +++ b/Aerofoil/Aerofoil.vcxproj.filters @@ -28,6 +28,9 @@ Source Files + + Source Files + diff --git a/Aerofoil/GpBWCursor_Win32.cpp b/Aerofoil/GpBWCursor_Win32.cpp index da4a07e..01d1cd9 100644 --- a/Aerofoil/GpBWCursor_Win32.cpp +++ b/Aerofoil/GpBWCursor_Win32.cpp @@ -21,6 +21,7 @@ #include "GpBWCursor_Win32.h" #include "GpWindows.h" +#include "IGpAllocator.h" #include #include @@ -34,19 +35,19 @@ void GpBWCursor_Win32::Destroy() this->DecRef(); } -IGpCursor_Win32 *GpBWCursor_Win32::Create(size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY) +IGpCursor_Win32 *GpBWCursor_Win32::Create(IGpAllocator *alloc, size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY) { size_t numBits = width * height; - size_t numBytes = (width * height + 7) / 8; - uint8_t *convertedAndData = static_cast(malloc(numBytes)); - uint8_t *convertedXorData = static_cast(malloc(numBytes)); + size_t numBytes = (numBits + 7) / 8; + uint8_t *convertedAndData = static_cast(alloc->Alloc(numBytes)); + uint8_t *convertedXorData = static_cast(alloc->Alloc(numBytes)); if (!convertedAndData || !convertedXorData) { if (convertedAndData) - free(convertedAndData); + alloc->Release(convertedAndData); if (convertedXorData) - free(convertedXorData); + alloc->Release(convertedXorData); return nullptr; } @@ -62,25 +63,26 @@ IGpCursor_Win32 *GpBWCursor_Win32::Create(size_t width, size_t height, const voi HCURSOR hcursor = CreateCursor(g_gpWindowsGlobals.m_hInstance, static_cast(hotSpotX), static_cast(hotSpotY), static_cast(width), static_cast(height), convertedAndData, convertedXorData); - free(convertedAndData); - free(convertedXorData); + alloc->Release(convertedAndData); + alloc->Release(convertedXorData); if (!hcursor) return nullptr; - void *storage = malloc(sizeof(GpBWCursor_Win32)); + void *storage = alloc->Alloc(sizeof(GpBWCursor_Win32)); if (!storage) { DestroyCursor(hcursor); return nullptr; } - return new (storage) GpBWCursor_Win32(hcursor); + return new (storage) GpBWCursor_Win32(hcursor, alloc); } -GpBWCursor_Win32::GpBWCursor_Win32(HCURSOR cursor) +GpBWCursor_Win32::GpBWCursor_Win32(HCURSOR cursor, IGpAllocator *alloc) : m_cursor(cursor) , m_refCount(1) + , m_alloc(alloc) { } @@ -104,7 +106,8 @@ void GpBWCursor_Win32::DecRef() m_refCount--; if (m_refCount == 0) { + IGpAllocator *alloc = m_alloc; this->~GpBWCursor_Win32(); - free(this); + alloc->Release(this); } } diff --git a/Aerofoil/GpBWCursor_Win32.h b/Aerofoil/GpBWCursor_Win32.h index 070bb7b..92958d8 100644 --- a/Aerofoil/GpBWCursor_Win32.h +++ b/Aerofoil/GpBWCursor_Win32.h @@ -3,6 +3,8 @@ #include "IGpCursor_Win32.h" #include "GpWindows.h" +struct IGpAllocator; + class GpBWCursor_Win32 final : public IGpCursor_Win32 { public: @@ -13,12 +15,13 @@ public: void IncRef() override; void DecRef() override; - static IGpCursor_Win32 *Create(size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY); + static IGpCursor_Win32 *Create(IGpAllocator *alloc, size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY); private: - GpBWCursor_Win32(HCURSOR cursor); + GpBWCursor_Win32(HCURSOR cursor, IGpAllocator *alloc); ~GpBWCursor_Win32(); HCURSOR m_cursor; int m_refCount; + IGpAllocator *m_alloc; }; diff --git a/Aerofoil/GpColorCursor_Win32.cpp b/Aerofoil/GpColorCursor_Win32.cpp index 5dbfd84..07dc4be 100644 --- a/Aerofoil/GpColorCursor_Win32.cpp +++ b/Aerofoil/GpColorCursor_Win32.cpp @@ -20,6 +20,7 @@ */ #include "GpColorCursor_Win32.h" +#include "IGpAllocator.h" #include #include @@ -31,7 +32,7 @@ void GpColorCursor_Win32::Destroy() this->DecRef(); } -IGpCursor_Win32 *GpColorCursor_Win32::Create(size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY) +IGpCursor_Win32 *GpColorCursor_Win32::Create(IGpAllocator *alloc, size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY) { const size_t paddingBits = (sizeof(void*) * 8); @@ -52,7 +53,7 @@ IGpCursor_Win32 *GpColorCursor_Win32::Create(size_t width, size_t height, const size_t maskPitch = width + paddingBits - 1; maskPitch -= maskPitch % paddingBits; - LPVOID maskBits = malloc(maskPitch * height); + LPVOID maskBits = alloc->Alloc(maskPitch * height); if (!maskBits) return nullptr; @@ -71,6 +72,8 @@ IGpCursor_Win32 *GpColorCursor_Win32::Create(size_t width, size_t height, const ii.hbmMask = CreateBitmap(width, height, 1, 1, maskBits); ReleaseDC(NULL, hdc); + alloc->Release(maskBits); + size_t cursorPitch = width * 4; size_t numPixels = width * height; @@ -90,19 +93,20 @@ IGpCursor_Win32 *GpColorCursor_Win32::Create(size_t width, size_t height, const if (!hicon) return nullptr; - void *storage = malloc(sizeof(GpColorCursor_Win32)); + void *storage = alloc->Alloc(sizeof(GpColorCursor_Win32)); if (!storage) { DestroyIcon(hicon); return nullptr; } - return new (storage) GpColorCursor_Win32(reinterpret_cast(hicon)); + return new (storage) GpColorCursor_Win32(alloc, reinterpret_cast(hicon)); } -GpColorCursor_Win32::GpColorCursor_Win32(HCURSOR cursor) +GpColorCursor_Win32::GpColorCursor_Win32(IGpAllocator *alloc, HCURSOR cursor) : m_cursor(cursor) , m_refCount(1) + , m_alloc(alloc) { } @@ -126,7 +130,8 @@ void GpColorCursor_Win32::DecRef() m_refCount--; if (m_refCount == 0) { + IGpAllocator *alloc = m_alloc; this->~GpColorCursor_Win32(); - free(this); + alloc->Release(this); } } diff --git a/Aerofoil/GpColorCursor_Win32.h b/Aerofoil/GpColorCursor_Win32.h index a451ea2..b030f60 100644 --- a/Aerofoil/GpColorCursor_Win32.h +++ b/Aerofoil/GpColorCursor_Win32.h @@ -3,6 +3,7 @@ #include "IGpCursor_Win32.h" #include "GpWindows.h" +struct IGpAllocator; class GpColorCursor_Win32 final : public IGpCursor_Win32 { @@ -14,12 +15,13 @@ public: void IncRef() override; void DecRef() override; - static IGpCursor_Win32 *Create(size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY); + static IGpCursor_Win32 *Create(IGpAllocator *alloc, size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY); private: - GpColorCursor_Win32(HCURSOR cursor); + GpColorCursor_Win32(IGpAllocator *alloc, HCURSOR cursor); ~GpColorCursor_Win32(); HCURSOR m_cursor; int m_refCount; + IGpAllocator *m_alloc; }; diff --git a/Aerofoil/GpFileSystem_Win32.cpp b/Aerofoil/GpFileSystem_Win32.cpp index 0d0c8ec..485fbcc 100644 --- a/Aerofoil/GpFileSystem_Win32.cpp +++ b/Aerofoil/GpFileSystem_Win32.cpp @@ -1,8 +1,10 @@ #include "GpFileSystem_Win32.h" +#include "GpAllocator_C.h" #include "GpApplicationName.h" #include "GpFileStream_Win32.h" #include "GpWindows.h" +#include "IGpAllocator.h" #include "IGpDirectoryCursor.h" #include @@ -12,33 +14,36 @@ #include +struct IGpAllocator; + extern GpWindowsGlobals g_gpWindowsGlobals; class GpDirectoryCursor_Win32 final : public IGpDirectoryCursor { public: - static GpDirectoryCursor_Win32 *Create(const HANDLE &handle, const WIN32_FIND_DATAW &findData); + static GpDirectoryCursor_Win32 *Create(IGpAllocator *alloc, const HANDLE &handle, const WIN32_FIND_DATAW &findData); bool GetNext(const char *&outFileName) override; void Destroy() override; private: - GpDirectoryCursor_Win32(const HANDLE &handle, const WIN32_FIND_DATAW &findData); + GpDirectoryCursor_Win32(IGpAllocator *alloc, const HANDLE &handle, const WIN32_FIND_DATAW &findData); ~GpDirectoryCursor_Win32(); + IGpAllocator *m_alloc; HANDLE m_handle; WIN32_FIND_DATAW m_findData; char m_chars[MAX_PATH + 1]; bool m_haveNext; }; -GpDirectoryCursor_Win32 *GpDirectoryCursor_Win32::Create(const HANDLE &handle, const WIN32_FIND_DATAW &findData) +GpDirectoryCursor_Win32 *GpDirectoryCursor_Win32::Create(IGpAllocator *alloc, const HANDLE &handle, const WIN32_FIND_DATAW &findData) { - void *storage = malloc(sizeof(GpDirectoryCursor_Win32)); + void *storage = alloc->Alloc(sizeof(GpDirectoryCursor_Win32)); if (!storage) return nullptr; - return new (storage) GpDirectoryCursor_Win32(handle, findData); + return new (storage) GpDirectoryCursor_Win32(alloc, handle, findData); } bool GpDirectoryCursor_Win32::GetNext(const char *&outFileName) @@ -82,14 +87,16 @@ bool GpDirectoryCursor_Win32::GetNext(const char *&outFileName) void GpDirectoryCursor_Win32::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpDirectoryCursor_Win32(); - free(this); + alloc->Release(this); } -GpDirectoryCursor_Win32::GpDirectoryCursor_Win32(const HANDLE &handle, const WIN32_FIND_DATAW &findData) +GpDirectoryCursor_Win32::GpDirectoryCursor_Win32(IGpAllocator *alloc, const HANDLE &handle, const WIN32_FIND_DATAW &findData) : m_handle(handle) , m_findData(findData) , m_haveNext(true) + , m_alloc(alloc) { } @@ -99,6 +106,7 @@ GpDirectoryCursor_Win32::~GpDirectoryCursor_Win32() } GpFileSystem_Win32::GpFileSystem_Win32() + : m_alloc(GpAllocator_C::GetInstance()) { // GP TODO: This shouldn't be static init since it allocates memory m_executablePath[0] = 0; @@ -277,7 +285,7 @@ IGpDirectoryCursor *GpFileSystem_Win32::ScanDirectoryNested(PortabilityLayer::Vi { wchar_t winPath[MAX_PATH + 2]; - const char **expandedPaths = static_cast(malloc(sizeof(const char*) * (numPaths + 1))); + const char **expandedPaths = static_cast(m_alloc->Alloc(sizeof(const char*) * (numPaths + 1))); if (!expandedPaths) return nullptr; @@ -286,7 +294,7 @@ IGpDirectoryCursor *GpFileSystem_Win32::ScanDirectoryNested(PortabilityLayer::Vi expandedPaths[numPaths] = "*"; const bool isPathResolved = ResolvePath(virtualDirectory, expandedPaths, numPaths + 1, winPath); - free(expandedPaths); + m_alloc->Release(expandedPaths); if (!isPathResolved) return nullptr; @@ -297,7 +305,7 @@ IGpDirectoryCursor *GpFileSystem_Win32::ScanDirectoryNested(PortabilityLayer::Vi if (ff == INVALID_HANDLE_VALUE) return nullptr; - return GpDirectoryCursor_Win32::Create(ff, findData); + return GpDirectoryCursor_Win32::Create(m_alloc, ff, findData); } bool GpFileSystem_Win32::ValidateFilePathUnicodeChar(uint32_t c) const diff --git a/Aerofoil/GpFileSystem_Win32.h b/Aerofoil/GpFileSystem_Win32.h index 6deff90..45acb89 100644 --- a/Aerofoil/GpFileSystem_Win32.h +++ b/Aerofoil/GpFileSystem_Win32.h @@ -41,5 +41,7 @@ private: std::wstring m_fontCacheDir; wchar_t m_executablePath[MAX_PATH]; + IGpAllocator *m_alloc; + static GpFileSystem_Win32 ms_instance; }; diff --git a/Aerofoil/GpLogDriver_Win32.cpp b/Aerofoil/GpLogDriver_Win32.cpp index 3ac6ea1..1ab20f4 100644 --- a/Aerofoil/GpLogDriver_Win32.cpp +++ b/Aerofoil/GpLogDriver_Win32.cpp @@ -1,3 +1,4 @@ +#include "GpAllocator_C.h" #include "GpLogDriver_Win32.h" #include "GpFileSystem_Win32.h" @@ -7,6 +8,7 @@ GpLogDriver_Win32::GpLogDriver_Win32() : m_stream(nullptr) , m_isInitialized(false) + , m_alloc(GpAllocator_C::GetInstance()) { } @@ -58,14 +60,14 @@ void GpLogDriver_Win32::VPrintf(Category category, const char *fmt, va_list args if (formattedSize <= 0) return; - char *charBuff = static_cast(malloc(formattedSize + 1)); + char *charBuff = static_cast(m_alloc->Alloc(formattedSize + 1)); if (!charBuff) return; vsnprintf(charBuff, formattedSize + 1, fmt, args); m_stream->Write(charBuff, formattedSize); - free(charBuff); + m_alloc->Release(charBuff); } m_stream->Write("\n", 1); diff --git a/Aerofoil/GpLogDriver_Win32.h b/Aerofoil/GpLogDriver_Win32.h index fe7f9a2..b607cd7 100644 --- a/Aerofoil/GpLogDriver_Win32.h +++ b/Aerofoil/GpLogDriver_Win32.h @@ -3,6 +3,7 @@ #include "IGpLogDriver.h" class GpIOStream; +struct IGpAllocator; class GpLogDriver_Win32 : public IGpLogDriver { @@ -20,6 +21,7 @@ private: void InitInternal(); GpIOStream *m_stream; + IGpAllocator *m_alloc; bool m_isInitialized; static GpLogDriver_Win32 ms_instance; diff --git a/Aerofoil/GpMain_Win32.cpp b/Aerofoil/GpMain_Win32.cpp index 6a5fc5c..2343ac7 100644 --- a/Aerofoil/GpMain_Win32.cpp +++ b/Aerofoil/GpMain_Win32.cpp @@ -1,4 +1,5 @@ #include "GpMain.h" +#include "GpAllocator_C.h" #include "GpAudioDriverFactory.h" #include "GpBWCursor_Win32.h" #include "GpColorCursor_Win32.h" @@ -410,12 +411,15 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } IGpLogDriver *logger = GpLogDriver_Win32::GetInstance(); + IGpAllocator *alloc = GpAllocator_C::GetInstance(); + IGpSystemServices *sysServices = GpSystemServices_Win32::GetInstance(); GpDriverCollection *drivers = GpAppInterface_Get()->PL_GetDriverCollection(); drivers->SetDriver(GpFileSystem_Win32::GetInstance()); - drivers->SetDriver(GpSystemServices_Win32::GetInstance()); - drivers->SetDriver(GpLogDriver_Win32::GetInstance()); + drivers->SetDriver(sysServices); + drivers->SetDriver(logger); + drivers->SetDriver(alloc); g_gpWindowsGlobals.m_hInstance = hInstance; g_gpWindowsGlobals.m_hPrevInstance = hPrevInstance; @@ -448,7 +452,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine g_gpGlobalConfig.m_osGlobals = &g_gpWindowsGlobals; g_gpGlobalConfig.m_logger = logger; - g_gpGlobalConfig.m_systemServices = GpSystemServices_Win32::GetInstance(); + g_gpGlobalConfig.m_systemServices = sysServices; + g_gpGlobalConfig.m_allocator = alloc; GpDisplayDriverFactory::RegisterDisplayDriverFactory(EGpDisplayDriverType_D3D11, GpDriver_CreateDisplayDriver_D3D11); GpAudioDriverFactory::RegisterAudioDriverFactory(EGpAudioDriverType_XAudio2, GpDriver_CreateAudioDriver_XAudio2); diff --git a/Aerofoil/GpMutex_Win32.cpp b/Aerofoil/GpMutex_Win32.cpp index 849fc4f..a5acb2b 100644 --- a/Aerofoil/GpMutex_Win32.cpp +++ b/Aerofoil/GpMutex_Win32.cpp @@ -1,5 +1,6 @@ #include "GpMutex_Win32.h" +#include "IGpAllocator.h" #include "GpWindows.h" #include @@ -7,8 +8,9 @@ void GpMutex_Win32::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpMutex_Win32(); - free(this); + alloc->Release(this); } void GpMutex_Win32::Lock() @@ -22,16 +24,17 @@ void GpMutex_Win32::Unlock() } -GpMutex_Win32 *GpMutex_Win32::Create() +GpMutex_Win32 *GpMutex_Win32::Create(IGpAllocator *alloc) { - void *storage = malloc(sizeof(GpMutex_Win32)); + void *storage = alloc->Alloc(sizeof(GpMutex_Win32)); if (!storage) return nullptr; - return new (storage) GpMutex_Win32(); + return new (storage) GpMutex_Win32(alloc); } -GpMutex_Win32::GpMutex_Win32() +GpMutex_Win32::GpMutex_Win32(IGpAllocator *alloc) + : m_alloc(alloc) { InitializeCriticalSection(&m_critSection); } diff --git a/Aerofoil/GpMutex_Win32.h b/Aerofoil/GpMutex_Win32.h index 6b7ca86..d9bf6fb 100644 --- a/Aerofoil/GpMutex_Win32.h +++ b/Aerofoil/GpMutex_Win32.h @@ -4,6 +4,8 @@ #include "GpWindows.h" +struct IGpAllocator; + class GpMutex_Win32 final : public IGpMutex { public: @@ -11,11 +13,12 @@ public: void Lock() override; void Unlock() override; - static GpMutex_Win32 *Create(); + static GpMutex_Win32 *Create(IGpAllocator *alloc); private: - const GpMutex_Win32(); + explicit GpMutex_Win32(IGpAllocator *alloc); ~GpMutex_Win32(); CRITICAL_SECTION m_critSection; + IGpAllocator *m_alloc; }; diff --git a/Aerofoil/GpSystemServices_Win32.cpp b/Aerofoil/GpSystemServices_Win32.cpp index f2af474..f1e5f86 100644 --- a/Aerofoil/GpSystemServices_Win32.cpp +++ b/Aerofoil/GpSystemServices_Win32.cpp @@ -2,6 +2,7 @@ #include "GpMutex_Win32.h" #include "GpThreadEvent_Win32.h" #include "GpWindows.h" +#include "GpAllocator_C.h" #include "IGpClipboardContents.h" @@ -133,6 +134,7 @@ static DWORD WINAPI StaticStartThread(LPVOID lpThreadParameter) GpSystemServices_Win32::GpSystemServices_Win32() : m_isTouchscreenSimulation(false) + , m_alloc(GpAllocator_C::GetInstance()) { } @@ -180,17 +182,17 @@ void GpSystemServices_Win32::GetLocalDateTime(unsigned int &year, unsigned int & IGpMutex *GpSystemServices_Win32::CreateMutex() { - return GpMutex_Win32::Create(); + return GpMutex_Win32::Create(m_alloc); } IGpMutex *GpSystemServices_Win32::CreateRecursiveMutex() { - return GpMutex_Win32::Create(); + return GpMutex_Win32::Create(m_alloc); } IGpThreadEvent *GpSystemServices_Win32::CreateThreadEvent(bool autoReset, bool startSignaled) { - return GpThreadEvent_Win32::Create(autoReset, startSignaled); + return GpThreadEvent_Win32::Create(m_alloc, autoReset, startSignaled); } void *GpSystemServices_Win32::CreateThread(ThreadFunc_t threadFunc, void *context) diff --git a/Aerofoil/GpSystemServices_Win32.h b/Aerofoil/GpSystemServices_Win32.h index 90b26b2..d72e2b8 100644 --- a/Aerofoil/GpSystemServices_Win32.h +++ b/Aerofoil/GpSystemServices_Win32.h @@ -48,6 +48,8 @@ public: private: bool m_isTouchscreenSimulation; + IGpAllocator *m_alloc; + static GpSystemServices_Win32 ms_instance; }; diff --git a/Aerofoil/GpThreadEvent_Win32.cpp b/Aerofoil/GpThreadEvent_Win32.cpp index 1497d10..52d38ee 100644 --- a/Aerofoil/GpThreadEvent_Win32.cpp +++ b/Aerofoil/GpThreadEvent_Win32.cpp @@ -1,4 +1,5 @@ #include "GpThreadEvent_Win32.h" +#include "IGpAllocator.h" #include #include @@ -20,28 +21,30 @@ void GpThreadEvent_Win32::Signal() void GpThreadEvent_Win32::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpThreadEvent_Win32(); - free(this); + alloc->Release(this); } -GpThreadEvent_Win32 *GpThreadEvent_Win32::Create(bool autoReset, bool startSignaled) +GpThreadEvent_Win32 *GpThreadEvent_Win32::Create(IGpAllocator *alloc, bool autoReset, bool startSignaled) { HANDLE handle = CreateEventA(nullptr, autoReset ? FALSE : TRUE, startSignaled ? TRUE : FALSE, nullptr); if (handle == nullptr) return nullptr; - void *storage = malloc(sizeof(GpThreadEvent_Win32)); + void *storage = alloc->Alloc(sizeof(GpThreadEvent_Win32)); if (!storage) { CloseHandle(handle); return nullptr; } - return new (storage) GpThreadEvent_Win32(handle); + return new (storage) GpThreadEvent_Win32(alloc, handle); } -GpThreadEvent_Win32::GpThreadEvent_Win32(const HANDLE &handle) +GpThreadEvent_Win32::GpThreadEvent_Win32(IGpAllocator *alloc, const HANDLE &handle) : m_event(handle) + , m_alloc(alloc) { } diff --git a/Aerofoil/GpThreadEvent_Win32.h b/Aerofoil/GpThreadEvent_Win32.h index 20454de..dd6d3f3 100644 --- a/Aerofoil/GpThreadEvent_Win32.h +++ b/Aerofoil/GpThreadEvent_Win32.h @@ -12,11 +12,12 @@ public: void Signal() override; void Destroy() override; - static GpThreadEvent_Win32 *Create(bool autoReset, bool startSignaled); + static GpThreadEvent_Win32 *Create(IGpAllocator *alloc, bool autoReset, bool startSignaled); private: - explicit GpThreadEvent_Win32(const HANDLE &handle); + explicit GpThreadEvent_Win32(IGpAllocator *alloc, const HANDLE &handle); ~GpThreadEvent_Win32(); HANDLE m_event; + IGpAllocator *m_alloc; }; diff --git a/AerofoilPortable.props b/AerofoilPortable.props index 08b2b3e..5412b99 100644 --- a/AerofoilPortable.props +++ b/AerofoilPortable.props @@ -1,8 +1,10 @@ - + - + + $(SolutionDir)AerofoilPortable;$(IncludePath) + - + \ No newline at end of file diff --git a/AerofoilPortable/GpAllocator_C.cpp b/AerofoilPortable/GpAllocator_C.cpp new file mode 100644 index 0000000..247bfb5 --- /dev/null +++ b/AerofoilPortable/GpAllocator_C.cpp @@ -0,0 +1,115 @@ +#include "GpAllocator_C.h" +#include "CoreDefs.h" + +#include +#include +#include + +struct GpAllocator_C_MMBlock +{ + uint8_t m_offsetFromAllocLocation; + + static size_t AlignedSize(); +}; + +size_t GpAllocator_C_MMBlock::AlignedSize() +{ + const size_t paddedSize = sizeof(GpAllocator_C_MMBlock) + GP_SYSTEM_MEMORY_ALIGNMENT - 1; + const size_t paddedSizeTruncated = paddedSize - (paddedSize % GP_SYSTEM_MEMORY_ALIGNMENT); + + return paddedSizeTruncated; +} + +void *GpAllocator_C::Realloc(void *buf, size_t newSize) +{ + if (buf == nullptr) + { + if (newSize == 0) + return nullptr; + + return this->Alloc(newSize); + } + + if (newSize == 0) + { + this->Free(buf); + return nullptr; + } + + assert(buf != nullptr); + + const size_t mmBlockSize = GpAllocator_C_MMBlock::AlignedSize(); + uint8_t *oldBufBytes = static_cast(buf); + const GpAllocator_C_MMBlock *oldBufMMBlock = reinterpret_cast(oldBufBytes - GpAllocator_C_MMBlock::AlignedSize()); + + const size_t oldBufOffsetFromAlignLoc = oldBufMMBlock->m_offsetFromAllocLocation; + uint8_t *oldBufBase = oldBufBytes - GpAllocator_C_MMBlock::AlignedSize() - oldBufOffsetFromAlignLoc; + + const size_t mmBlockSizeWithMaxPadding = GpAllocator_C_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(realloc(oldBufBase, newSize + mmBlockSizeWithMaxPadding)); + if (!newBuffer) + return nullptr; + + const intptr_t offsetFromAlignPoint = reinterpret_cast(newBuffer) & static_cast(GP_SYSTEM_MEMORY_ALIGNMENT - 1); + intptr_t alignPadding = 0; + if (offsetFromAlignPoint != 0) + alignPadding = static_cast(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint; + + // Check if the alignment changed, if so relocate + if (static_cast(alignPadding) != oldBufOffsetFromAlignLoc) + memmove(newBuffer + alignPadding, newBuffer + oldBufOffsetFromAlignLoc, GpAllocator_C_MMBlock::AlignedSize() + newSize); + + GpAllocator_C_MMBlock *newMMBlock = reinterpret_cast(newBuffer + alignPadding); + newMMBlock->m_offsetFromAllocLocation = static_cast(alignPadding); + + return newBuffer + alignPadding + GpAllocator_C_MMBlock::AlignedSize(); +} + +void *GpAllocator_C::Alloc(size_t size) +{ + if (size == 0) + return nullptr; + + const size_t mmBlockSizeWithMaxPadding = GpAllocator_C_MMBlock::AlignedSize() + GP_SYSTEM_MEMORY_ALIGNMENT - 1; + if (SIZE_MAX - size < mmBlockSizeWithMaxPadding) + return nullptr; + + uint8_t *buffer = static_cast(realloc(nullptr, size + mmBlockSizeWithMaxPadding)); + if (!buffer) + return nullptr; + + const intptr_t offsetFromAlignPoint = reinterpret_cast(buffer) & static_cast(GP_SYSTEM_MEMORY_ALIGNMENT - 1); + intptr_t alignPadding = 0; + if (offsetFromAlignPoint != 0) + alignPadding = static_cast(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint; + + GpAllocator_C_MMBlock *mmBlock = reinterpret_cast(buffer + alignPadding); + mmBlock->m_offsetFromAllocLocation = static_cast(alignPadding); + + return buffer + alignPadding + GpAllocator_C_MMBlock::AlignedSize(); +} + +void GpAllocator_C::Free(void *buf) +{ + if (!buf) + return; + + const size_t mmBlockSize = GpAllocator_C_MMBlock::AlignedSize(); + + uint8_t *bytes = static_cast(buf); + const GpAllocator_C_MMBlock *mmBlock = reinterpret_cast(bytes - GpAllocator_C_MMBlock::AlignedSize()); + + void *freeLoc = bytes - GpAllocator_C_MMBlock::AlignedSize() - mmBlock->m_offsetFromAllocLocation; + realloc(freeLoc, 0); +} + +GpAllocator_C *GpAllocator_C::GetInstance() +{ + return &ms_instance; +} + +GpAllocator_C GpAllocator_C::ms_instance; diff --git a/AerofoilPortable/GpAllocator_C.h b/AerofoilPortable/GpAllocator_C.h new file mode 100644 index 0000000..8325f31 --- /dev/null +++ b/AerofoilPortable/GpAllocator_C.h @@ -0,0 +1,17 @@ +#pragma once + +#include "IGpAllocator.h" + +class GpAllocator_C final : public IGpAllocator +{ +public: + void *Realloc(void *buf, size_t newSize) override; + + static GpAllocator_C *GetInstance(); + +private: + void *Alloc(size_t size); + void Free(void *ptr); + + static GpAllocator_C ms_instance; +}; diff --git a/AerofoilSDL/AerofoilSDL.vcxproj b/AerofoilSDL/AerofoilSDL.vcxproj index d674526..071b61d 100644 --- a/AerofoilSDL/AerofoilSDL.vcxproj +++ b/AerofoilSDL/AerofoilSDL.vcxproj @@ -84,6 +84,7 @@ + diff --git a/AerofoilSDL/AerofoilSDL.vcxproj.filters b/AerofoilSDL/AerofoilSDL.vcxproj.filters index 3c86551..d5a22b0 100644 --- a/AerofoilSDL/AerofoilSDL.vcxproj.filters +++ b/AerofoilSDL/AerofoilSDL.vcxproj.filters @@ -66,6 +66,9 @@ Source Files + + Source Files + diff --git a/AerofoilSDL/GpMain_SDL_Win32.cpp b/AerofoilSDL/GpMain_SDL_Win32.cpp index d492b13..39a0198 100644 --- a/AerofoilSDL/GpMain_SDL_Win32.cpp +++ b/AerofoilSDL/GpMain_SDL_Win32.cpp @@ -1,6 +1,7 @@ #include "SDL.h" #include "GpMain.h" +#include "GpAllocator_C.h" #include "GpAudioDriverFactory.h" #include "GpDisplayDriverFactory.h" #include "GpGlobalConfig.h" @@ -59,6 +60,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine drivers->SetDriver(GpFileSystem_Win32::GetInstance()); drivers->SetDriver(GpSystemServices_Win32::GetInstance()); drivers->SetDriver(GpLogDriver_Win32::GetInstance()); + drivers->SetDriver(GpAllocator_C::GetInstance()); g_gpWindowsGlobals.m_hInstance = hInstance; g_gpWindowsGlobals.m_hPrevInstance = hPrevInstance; @@ -86,6 +88,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine g_gpGlobalConfig.m_osGlobals = &g_gpWindowsGlobals; g_gpGlobalConfig.m_logger = logger; g_gpGlobalConfig.m_systemServices = GpSystemServices_Win32::GetInstance(); + g_gpGlobalConfig.m_allocator = GpAllocator_C::GetInstance(); GpDisplayDriverFactory::RegisterDisplayDriverFactory(EGpDisplayDriverType_SDL_GL2, GpDriver_CreateDisplayDriver_SDL_GL2); GpAudioDriverFactory::RegisterAudioDriverFactory(EGpAudioDriverType_SDL2, GpDriver_CreateAudioDriver_SDL); diff --git a/ConvertColorCursors/ConvertColorCursors.cpp b/ConvertColorCursors/ConvertColorCursors.cpp index a35b39e..4d8ee7e 100644 --- a/ConvertColorCursors/ConvertColorCursors.cpp +++ b/ConvertColorCursors/ConvertColorCursors.cpp @@ -1,11 +1,13 @@ #include "CFileStream.h" #include "BMPFormat.h" +#include "GpAllocator_C.h" #include "MMHandleBlock.h" #include "ResourceCompiledTypeList.h" #include "ResourceFile.h" #include "SharedTypes.h" #include "QDStandardPalette.h" #include "PLBigEndian.h" +#include "PLDrivers.h" #include #include @@ -141,6 +143,9 @@ int main(int argc, const char **argv) PortabilityLayer::CFileStream stream(f); + GpDriverCollection *drivers = PLDrivers::GetDriverCollection(); + drivers->SetDriver(GpAllocator_C::GetInstance()); + PortabilityLayer::ResourceFile *resFile = PortabilityLayer::ResourceFile::Create(); if (!resFile->Load(&stream)) return -1; diff --git a/ConvertColorCursors/ConvertColorCursors.vcxproj b/ConvertColorCursors/ConvertColorCursors.vcxproj index 803089b..930df10 100644 --- a/ConvertColorCursors/ConvertColorCursors.vcxproj +++ b/ConvertColorCursors/ConvertColorCursors.vcxproj @@ -42,6 +42,7 @@ + @@ -50,6 +51,7 @@ + @@ -81,6 +83,7 @@ + diff --git a/ConvertColorCursors/ConvertColorCursors.vcxproj.filters b/ConvertColorCursors/ConvertColorCursors.vcxproj.filters index 310100b..d12c435 100644 --- a/ConvertColorCursors/ConvertColorCursors.vcxproj.filters +++ b/ConvertColorCursors/ConvertColorCursors.vcxproj.filters @@ -21,5 +21,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/GenerateFonts/GenerateFonts.cpp b/GenerateFonts/GenerateFonts.cpp index e5e6bbb..d425ddb 100644 --- a/GenerateFonts/GenerateFonts.cpp +++ b/GenerateFonts/GenerateFonts.cpp @@ -3,6 +3,7 @@ #include "FontRenderer.h" #include "IGpFont.h" #include "IGpFontHandler.h" +#include "GpAllocator_C.h" #include "GpAppInterface.h" #include "GpDriverIndex.h" #include "GpFontHandlerProperties.h" @@ -167,8 +168,14 @@ bool KnownFontSpec::operator!=(const KnownFontSpec &other) const int toolMain(int argc, const char **argv) { + IGpAllocator *alloc = GpAllocator_C::GetInstance(); + GpFontHandlerProperties fhProperties; fhProperties.m_type = EGpFontHandlerType_FreeType2; + fhProperties.m_alloc = alloc; + + GpDriverCollection *drivers = PLDrivers::GetDriverCollection(); + drivers->SetDriver(alloc); IGpFontHandler *ft2Handler = GpDriver_CreateFontHandler_FreeType2(fhProperties); diff --git a/GenerateFonts/GenerateFonts.vcxproj b/GenerateFonts/GenerateFonts.vcxproj index 2a6530b..57a1326 100644 --- a/GenerateFonts/GenerateFonts.vcxproj +++ b/GenerateFonts/GenerateFonts.vcxproj @@ -43,6 +43,7 @@ + @@ -52,6 +53,7 @@ + @@ -93,6 +95,7 @@ + diff --git a/GenerateFonts/GenerateFonts.vcxproj.filters b/GenerateFonts/GenerateFonts.vcxproj.filters index 9e43b4f..e46cc5f 100644 --- a/GenerateFonts/GenerateFonts.vcxproj.filters +++ b/GenerateFonts/GenerateFonts.vcxproj.filters @@ -18,5 +18,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.cpp b/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.cpp index 780fb37..6f4e76d 100644 --- a/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.cpp +++ b/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.cpp @@ -1,12 +1,12 @@ #include "GpAudioBufferXAudio2.h" #include "CoreDefs.h" #include "GpWindows.h" +#include "IGpAllocator.h" -#include #include #include -GpAudioBufferXAudio2 *GpAudioBufferXAudio2::Create(const void *buffer, size_t size) +GpAudioBufferXAudio2 *GpAudioBufferXAudio2::Create(IGpAllocator *alloc, const void *buffer, size_t size) { size_t baseSize = sizeof(GpAudioBufferXAudio2); baseSize = baseSize + GP_SYSTEM_MEMORY_ALIGNMENT - 1; @@ -14,14 +14,14 @@ GpAudioBufferXAudio2 *GpAudioBufferXAudio2::Create(const void *buffer, size_t si size_t totalSize = baseSize + size; - void *storage = _aligned_malloc(totalSize, GP_SYSTEM_MEMORY_ALIGNMENT); + void *storage = alloc->Realloc(nullptr, totalSize); if (!storage) return nullptr; void *dataPos = static_cast(storage) + baseSize; memcpy(dataPos, buffer, size); - return new (storage) GpAudioBufferXAudio2(dataPos, size); + return new (storage) GpAudioBufferXAudio2(alloc, dataPos, size); } void GpAudioBufferXAudio2::AddRef() @@ -40,8 +40,9 @@ const XAUDIO2_BUFFER *GpAudioBufferXAudio2::GetXA2Buffer() const return &m_xa2Buffer; } -GpAudioBufferXAudio2::GpAudioBufferXAudio2(const void *data, size_t size) - : m_data(data) +GpAudioBufferXAudio2::GpAudioBufferXAudio2(IGpAllocator *alloc, const void *data, size_t size) + : m_alloc(alloc) + , m_data(data) , m_size(size) , m_count(1) { @@ -62,6 +63,7 @@ GpAudioBufferXAudio2::~GpAudioBufferXAudio2() void GpAudioBufferXAudio2::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpAudioBufferXAudio2(); - _aligned_free(this); + m_alloc->Realloc(this, 0); } diff --git a/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.h b/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.h index b97d724..ad5ac06 100644 --- a/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.h +++ b/GpAudioDriver_XAudio2/GpAudioBufferXAudio2.h @@ -4,10 +4,12 @@ #include +struct IGpAllocator; + class GpAudioBufferXAudio2 final : public IGpAudioBuffer { public: - static GpAudioBufferXAudio2 *Create(const void *buffer, size_t size); + static GpAudioBufferXAudio2 *Create(IGpAllocator *alloc, const void *buffer, size_t size); void AddRef() override; void Release() override; @@ -15,13 +17,14 @@ public: const XAUDIO2_BUFFER *GetXA2Buffer() const; private: - GpAudioBufferXAudio2(const void *data, size_t size); + GpAudioBufferXAudio2(IGpAllocator *alloc, const void *data, size_t size); ~GpAudioBufferXAudio2(); void Destroy(); const void *m_data; size_t m_size; + IGpAllocator *m_alloc; XAUDIO2_BUFFER m_xa2Buffer; diff --git a/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.cpp b/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.cpp index fc7e858..36ccaeb 100644 --- a/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.cpp +++ b/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.cpp @@ -1,26 +1,27 @@ #include "GpAudioBufferXAudio2.h" #include "GpAudioChannelXAudio2.h" #include "GpAudioDriverXAudio2.h" +#include "IGpAllocator.h" #include "IGpAudioChannelCallbacks.h" #include "IGpLogDriver.h" #include #include -GpAudioChannelXAudio2 *GpAudioChannelXAudio2::Create(GpAudioDriverXAudio2 *driver) +GpAudioChannelXAudio2 *GpAudioChannelXAudio2::Create(IGpAllocator *alloc, GpAudioDriverXAudio2 *driver) { IGpLogDriver *logger = driver->GetProperties().m_logger; - void *storage = malloc(sizeof(GpAudioChannelXAudio2)); + void *storage = alloc->Realloc(nullptr, sizeof(GpAudioChannelXAudio2)); if (!storage) { if (!logger) - logger->Printf(IGpLogDriver::Category_Error, "GpAudioChannelXAudio2::Create failed, malloc failed"); + logger->Printf(IGpLogDriver::Category_Error, "GpAudioChannelXAudio2::Create failed, alloc failed"); return nullptr; } - GpAudioChannelXAudio2 *channel = new (storage) GpAudioChannelXAudio2(driver); + GpAudioChannelXAudio2 *channel = new (storage) GpAudioChannelXAudio2(alloc, driver); if (!channel->Init()) { if (!logger) @@ -110,8 +111,9 @@ void GpAudioChannelXAudio2::Stop() void GpAudioChannelXAudio2::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpAudioChannelXAudio2(); - free(this); + alloc->Realloc(this, 0); } void GpAudioChannelXAudio2::OnBufferEnd() @@ -120,12 +122,13 @@ void GpAudioChannelXAudio2::OnBufferEnd() m_contextCallbacks->NotifyBufferFinished(); } -GpAudioChannelXAudio2::GpAudioChannelXAudio2(GpAudioDriverXAudio2 *driver) +GpAudioChannelXAudio2::GpAudioChannelXAudio2(IGpAllocator *alloc, GpAudioDriverXAudio2 *driver) : m_driver(driver) , m_xAudioCallbacks(this) , m_sourceVoice(nullptr) , m_contextCallbacks(nullptr) , m_voiceState(VoiceState_Idle) + , m_alloc(alloc) { } diff --git a/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.h b/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.h index 31dd4dd..1c0c9b7 100644 --- a/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.h +++ b/GpAudioDriver_XAudio2/GpAudioChannelXAudio2.h @@ -7,13 +7,14 @@ class GpAudioDriverXAudio2; class GpAudioChannelXAudio2Callbacks; struct IXAudio2SourceVoice; +struct IGpAllocator; class GpAudioChannelXAudio2 final : public IGpAudioChannel { public: friend class GpAudioChannelXAudio2Callbacks; - static GpAudioChannelXAudio2 *Create(GpAudioDriverXAudio2 *driver); + static GpAudioChannelXAudio2 *Create(IGpAllocator *alloc, GpAudioDriverXAudio2 *driver); void SetAudioChannelContext(IGpAudioChannelCallbacks *callbacks) override; bool PostBuffer(IGpAudioBuffer *buffer) override; @@ -32,12 +33,13 @@ private: VoiceState_Active, }; - explicit GpAudioChannelXAudio2(GpAudioDriverXAudio2 *driver); + explicit GpAudioChannelXAudio2(IGpAllocator *alloc, GpAudioDriverXAudio2 *driver); ~GpAudioChannelXAudio2(); GpAudioDriverXAudio2 *m_driver; IXAudio2SourceVoice *m_sourceVoice; GpAudioChannelXAudio2Callbacks m_xAudioCallbacks; IGpAudioChannelCallbacks *m_contextCallbacks; + IGpAllocator *m_alloc; VoiceState m_voiceState; }; diff --git a/GpAudioDriver_XAudio2/GpAudioDriverXAudio2.cpp b/GpAudioDriver_XAudio2/GpAudioDriverXAudio2.cpp index 5b30b9a..5fe3986 100644 --- a/GpAudioDriver_XAudio2/GpAudioDriverXAudio2.cpp +++ b/GpAudioDriver_XAudio2/GpAudioDriverXAudio2.cpp @@ -6,7 +6,6 @@ #include "CoreDefs.h" #include -#include void GpAudioDriverXAudio2::Shutdown() { @@ -101,12 +100,12 @@ GpAudioDriverXAudio2 *GpAudioDriverXAudio2::Create(const GpAudioDriverProperties IGpAudioBuffer *GpAudioDriverXAudio2::CreateBuffer(const void *buffer, size_t bufferSize) { - return GpAudioBufferXAudio2::Create(buffer, bufferSize); + return GpAudioBufferXAudio2::Create(m_properties.m_alloc, buffer, bufferSize); } IGpAudioChannel *GpAudioDriverXAudio2::CreateChannel() { - return GpAudioChannelXAudio2::Create(this); + return GpAudioChannelXAudio2::Create(m_properties.m_alloc, this); } void GpAudioDriverXAudio2::SetMasterVolume(uint32_t vol, uint32_t maxVolume) diff --git a/GpCommon/GpAudioDriverProperties.h b/GpCommon/GpAudioDriverProperties.h index 1372270..7643463 100644 --- a/GpCommon/GpAudioDriverProperties.h +++ b/GpCommon/GpAudioDriverProperties.h @@ -5,6 +5,7 @@ struct IGpSystemServices; struct IGpAudioDriver; struct IGpLogDriver; +struct IGpAllocator; struct GpAudioDriverProperties { @@ -15,4 +16,5 @@ struct GpAudioDriverProperties IGpLogDriver *m_logger; IGpSystemServices *m_systemServices; + IGpAllocator *m_alloc; }; diff --git a/GpCommon/GpDisplayDriverProperties.h b/GpCommon/GpDisplayDriverProperties.h index 733aa4c..1ffb639 100644 --- a/GpCommon/GpDisplayDriverProperties.h +++ b/GpCommon/GpDisplayDriverProperties.h @@ -10,6 +10,7 @@ struct IGpFiber; struct IGpVOSEventQueue; struct IGpLogDriver; struct IGpSystemServices; +struct IGpAllocator; struct GpDisplayDriverProperties { @@ -43,4 +44,5 @@ struct GpDisplayDriverProperties IGpVOSEventQueue *m_eventQueue; IGpLogDriver *m_logger; IGpSystemServices *m_systemServices; + IGpAllocator *m_alloc; }; diff --git a/GpCommon/GpDriverIndex.h b/GpCommon/GpDriverIndex.h index 6664218..9d346a2 100644 --- a/GpCommon/GpDriverIndex.h +++ b/GpCommon/GpDriverIndex.h @@ -16,6 +16,7 @@ namespace GpDriverIDs kSystemServices, kFont, kEventQueue, + kAlloc, kCount }; @@ -54,6 +55,7 @@ GP_DEFINE_MULTI_DRIVER(kInput, IGpInputDriver); GP_DEFINE_DRIVER(kSystemServices, IGpSystemServices); GP_DEFINE_DRIVER(kFont, IGpFontHandler); GP_DEFINE_DRIVER(kEventQueue, IGpVOSEventQueue); +GP_DEFINE_DRIVER(kAlloc, IGpAllocator); struct GpDriverCollection { diff --git a/GpCommon/GpFontHandlerProperties.h b/GpCommon/GpFontHandlerProperties.h index 2ca1355..413c3d0 100644 --- a/GpCommon/GpFontHandlerProperties.h +++ b/GpCommon/GpFontHandlerProperties.h @@ -2,7 +2,11 @@ #include "EGpFontHandlerType.h" +struct IGpAllocator; + struct GpFontHandlerProperties { EGpFontHandlerType m_type; + + IGpAllocator *m_alloc; }; diff --git a/GpCommon/GpInputDriverProperties.h b/GpCommon/GpInputDriverProperties.h index 0e13548..e8d003d 100644 --- a/GpCommon/GpInputDriverProperties.h +++ b/GpCommon/GpInputDriverProperties.h @@ -4,10 +4,12 @@ struct IGpAudioDriver; struct IGpVOSEventQueue; +struct IGpAllocator; struct GpInputDriverProperties { EGpInputDriverType m_type; IGpVOSEventQueue *m_eventQueue; + IGpAllocator *m_alloc; }; diff --git a/GpCommon/GpWindows.h b/GpCommon/GpWindows.h index bc53aac..8857d87 100644 --- a/GpCommon/GpWindows.h +++ b/GpCommon/GpWindows.h @@ -13,6 +13,7 @@ struct IGpBWCursor_Win32; struct IGpCursor_Win32; +struct IGpAllocator; struct IGpVOSEventQueue; struct GpWindowsGlobals @@ -28,7 +29,7 @@ struct GpWindowsGlobals HICON m_hIconSm; int m_nCmdShow; - IGpCursor_Win32 *(*m_createColorCursorFunc)(size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY); - IGpCursor_Win32 *(*m_createBWCursorFunc)(size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY); + IGpCursor_Win32 *(*m_createColorCursorFunc)(IGpAllocator *alloc, size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY); + IGpCursor_Win32 *(*m_createBWCursorFunc)(IGpAllocator *alloc, size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY); void (*m_translateWindowsMessageFunc)(const MSG *msg, IGpVOSEventQueue *eventQueue, float pixelScaleX, float pixelScaleY); }; diff --git a/GpCommon/IGpAllocator.h b/GpCommon/IGpAllocator.h new file mode 100644 index 0000000..9cc0703 --- /dev/null +++ b/GpCommon/IGpAllocator.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +struct IGpAllocator +{ + virtual void *Realloc(void *buf, size_t newSize) = 0; + + inline void *Alloc(size_t size) { return this->Realloc(nullptr, size); } + inline void Release(void *ptr) { this->Realloc(ptr, 0); } +}; diff --git a/GpDisplayDriver_D3D11/GpDisplayDriverD3D11.cpp b/GpDisplayDriver_D3D11/GpDisplayDriverD3D11.cpp index 0990779..d182a29 100644 --- a/GpDisplayDriver_D3D11/GpDisplayDriverD3D11.cpp +++ b/GpDisplayDriver_D3D11/GpDisplayDriverD3D11.cpp @@ -1,8 +1,10 @@ #include "GpApplicationName.h" #include "GpDisplayDriverD3D11.h" +#include "GpDisplayDriverProperties.h" #include "GpDisplayDriverSurfaceD3D11.h" #include "GpVOSEvent.h" #include "GpWindows.h" +#include "IGpAllocator.h" #include "IGpCursor_Win32.h" #include "IGpVOSEventQueue.h" @@ -1243,8 +1245,9 @@ void GpDisplayDriverD3D11::ServeTicks(int tickCount) void GpDisplayDriverD3D11::Shutdown() { + IGpAllocator *alloc = m_properties.m_alloc; this->~GpDisplayDriverD3D11(); - free(this); + alloc->Release(this); } void GpDisplayDriverD3D11::GetInitialDisplayResolution(unsigned int *width, unsigned int *height) @@ -1257,7 +1260,7 @@ void GpDisplayDriverD3D11::GetInitialDisplayResolution(unsigned int *width, unsi IGpDisplayDriverSurface *GpDisplayDriverD3D11::CreateSurface(size_t width, size_t height, size_t pitch, GpPixelFormat_t pixelFormat, IGpDisplayDriver::SurfaceInvalidateCallback_t invalidateCallback, void *invalidateContext) { - return GpDisplayDriverSurfaceD3D11::Create(m_device, m_deviceContext, width, height, pixelFormat); + return GpDisplayDriverSurfaceD3D11::Create(m_device, m_deviceContext, width, height, pixelFormat, m_properties.m_alloc); } void GpDisplayDriverD3D11::DrawSurface(IGpDisplayDriverSurface *surface, int32_t x, int32_t y, size_t width, size_t height, const GpDisplayDriverSurfaceEffects *effects) @@ -1385,12 +1388,12 @@ void GpDisplayDriverD3D11::DrawSurface(IGpDisplayDriverSurface *surface, int32_t IGpCursor *GpDisplayDriverD3D11::CreateColorCursor(size_t width, size_t height, const void *pixelDataRGBA, size_t hotSpotX, size_t hotSpotY) { - return m_osGlobals->m_createColorCursorFunc(width, height, pixelDataRGBA, hotSpotX, hotSpotY); + return m_osGlobals->m_createColorCursorFunc(m_properties.m_alloc, width, height, pixelDataRGBA, hotSpotX, hotSpotY); } IGpCursor *GpDisplayDriverD3D11::CreateBWCursor(size_t width, size_t height, const void *pixelData, const void *maskData, size_t hotSpotX, size_t hotSpotY) { - return m_osGlobals->m_createBWCursorFunc(width, height, pixelData, maskData, hotSpotX, hotSpotY); + return m_osGlobals->m_createBWCursorFunc(m_properties.m_alloc, width, height, pixelData, maskData, hotSpotX, hotSpotY); } // We can't just set the cursor because we want to post WM_SETCURSOR to keep it limited @@ -1507,7 +1510,7 @@ bool GpDisplayDriverD3D11::SavePrefs(void *context, IGpPrefsHandler::WritePrefsF GpDisplayDriverD3D11 *GpDisplayDriverD3D11::Create(const GpDisplayDriverProperties &properties) { - void *storage = malloc(sizeof(GpDisplayDriverD3D11)); + void *storage = properties.m_alloc->Alloc(sizeof(GpDisplayDriverD3D11)); if (!storage) return nullptr; diff --git a/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.cpp b/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.cpp index 9f2f3d2..cd17fbd 100644 --- a/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.cpp +++ b/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.cpp @@ -1,5 +1,6 @@ #include "GpDisplayDriverSurfaceD3D11.h" #include "GpComPtr.h" +#include "IGpAllocator.h" #include #include @@ -55,8 +56,9 @@ void GpDisplayDriverSurfaceD3D11::UploadEntire(const void *data, size_t pitch) void GpDisplayDriverSurfaceD3D11::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpDisplayDriverSurfaceD3D11(); - free(this); + alloc->Release(this); } ID3D11ShaderResourceView *GpDisplayDriverSurfaceD3D11::GetSRV() const @@ -79,7 +81,7 @@ size_t GpDisplayDriverSurfaceD3D11::GetHeight() const return m_height; } -GpDisplayDriverSurfaceD3D11 *GpDisplayDriverSurfaceD3D11::Create(ID3D11Device *device, ID3D11DeviceContext *deviceContext, size_t width, size_t height, GpPixelFormat_t pixelFormat) +GpDisplayDriverSurfaceD3D11 *GpDisplayDriverSurfaceD3D11::Create(ID3D11Device *device, ID3D11DeviceContext *deviceContext, size_t width, size_t height, GpPixelFormat_t pixelFormat, IGpAllocator *alloc) { DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R8_UNORM; @@ -127,17 +129,17 @@ GpDisplayDriverSurfaceD3D11 *GpDisplayDriverSurfaceD3D11::Create(ID3D11Device *d if (device->CreateShaderResourceView(texture, &srvDesc, srv.GetMutablePtr()) != S_OK) return nullptr; - void *storage = malloc(sizeof(GpDisplayDriverSurfaceD3D11)); + void *storage = alloc->Alloc(sizeof(GpDisplayDriverSurfaceD3D11)); if (!storage) { texture->Release(); return nullptr; } - return new (storage) GpDisplayDriverSurfaceD3D11(device, deviceContext, texture, srv, width, height, pixelFormat); + return new (storage) GpDisplayDriverSurfaceD3D11(device, deviceContext, texture, srv, width, height, pixelFormat, alloc); } -GpDisplayDriverSurfaceD3D11::GpDisplayDriverSurfaceD3D11(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Texture2D *texture, ID3D11ShaderResourceView *srv, size_t width, size_t height, GpPixelFormat_t pixelFormat) +GpDisplayDriverSurfaceD3D11::GpDisplayDriverSurfaceD3D11(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Texture2D *texture, ID3D11ShaderResourceView *srv, size_t width, size_t height, GpPixelFormat_t pixelFormat, IGpAllocator *alloc) : m_width(width) , m_height(height) , m_pixelFormat(pixelFormat) @@ -145,6 +147,7 @@ GpDisplayDriverSurfaceD3D11::GpDisplayDriverSurfaceD3D11(ID3D11Device *device, I , m_deviceContext(deviceContext) , m_texture(texture) , m_srv(srv) + , m_alloc(alloc) { } diff --git a/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.h b/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.h index a7d7a9c..afbb050 100644 --- a/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.h +++ b/GpDisplayDriver_D3D11/GpDisplayDriverSurfaceD3D11.h @@ -8,6 +8,7 @@ struct ID3D11Device; struct ID3D11DeviceContext; struct ID3D11ShaderResourceView; struct ID3D11Texture2D; +struct IGpAllocator; class GpDisplayDriverSurfaceD3D11 final : public IGpDisplayDriverSurface { @@ -21,10 +22,10 @@ public: size_t GetWidth() const; size_t GetHeight() const; - static GpDisplayDriverSurfaceD3D11 *Create(ID3D11Device *device, ID3D11DeviceContext *deviceContext, size_t width, size_t height, GpPixelFormat_t pixelFormat); + static GpDisplayDriverSurfaceD3D11 *Create(ID3D11Device *device, ID3D11DeviceContext *deviceContext, size_t width, size_t height, GpPixelFormat_t pixelFormat, IGpAllocator *alloc); private: - GpDisplayDriverSurfaceD3D11(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Texture2D *texture, ID3D11ShaderResourceView *srv, size_t width, size_t height, GpPixelFormat_t pixelFormat); + GpDisplayDriverSurfaceD3D11(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Texture2D *texture, ID3D11ShaderResourceView *srv, size_t width, size_t height, GpPixelFormat_t pixelFormat, IGpAllocator *alloc); ~GpDisplayDriverSurfaceD3D11(); size_t m_width; @@ -32,6 +33,7 @@ private: GpPixelFormat_t m_pixelFormat; ID3D11Device *m_device; ID3D11DeviceContext *m_deviceContext; + IGpAllocator *m_alloc; GpComPtr m_texture; GpComPtr m_srv; diff --git a/GpFontHandler_FreeType2/GpFontHandler_FreeType2.cpp b/GpFontHandler_FreeType2/GpFontHandler_FreeType2.cpp index d537a92..270861a 100644 --- a/GpFontHandler_FreeType2/GpFontHandler_FreeType2.cpp +++ b/GpFontHandler_FreeType2/GpFontHandler_FreeType2.cpp @@ -3,6 +3,7 @@ #include "CoreDefs.h" #include "GpIOStream.h" +#include "IGpAllocator.h" #include "IGpFont.h" #include "IGpFontRenderedGlyph.h" #include "GpRenderedGlyphMetrics.h" @@ -17,6 +18,8 @@ #include #include +struct IGpAllocator; + class GpFontRenderedGlyph_FreeType2 final : public IGpFontRenderedGlyph { public: @@ -24,14 +27,15 @@ public: const void *GetData() const override; void Destroy() override; - static GpFontRenderedGlyph_FreeType2 *Create(size_t dataSize, const GpRenderedGlyphMetrics &metrics); + static GpFontRenderedGlyph_FreeType2 *Create(IGpAllocator *alloc, size_t dataSize, const GpRenderedGlyphMetrics &metrics); void *GetMutableData(); private: - GpFontRenderedGlyph_FreeType2(void *data, const GpRenderedGlyphMetrics &metrics); + GpFontRenderedGlyph_FreeType2(IGpAllocator *alloc, void *data, const GpRenderedGlyphMetrics &metrics); ~GpFontRenderedGlyph_FreeType2(); + IGpAllocator *m_alloc; void *m_data; GpRenderedGlyphMetrics m_metrics; }; @@ -44,17 +48,18 @@ public: bool GetLineSpacing(unsigned int size, int32_t &outSpacing) override; bool SupportScaling() const override; - static GpFont_FreeType2 *Create(const FT_StreamRec_ &streamRec, GpIOStream *stream); + static GpFont_FreeType2 *Create(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream); bool FTLoad(const FT_Library &library, int typeFaceIndex); private: - explicit GpFont_FreeType2(const FT_StreamRec_ &streamRec, GpIOStream *stream); + GpFont_FreeType2(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream); ~GpFont_FreeType2(); FT_StreamRec_ m_ftStream; FT_Face m_face; GpIOStream *m_stream; + IGpAllocator *m_alloc; unsigned int m_currentSize; }; @@ -70,20 +75,21 @@ const void *GpFontRenderedGlyph_FreeType2::GetData() const void GpFontRenderedGlyph_FreeType2::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpFontRenderedGlyph_FreeType2(); - free(this); + alloc->Release(this); } -GpFontRenderedGlyph_FreeType2 *GpFontRenderedGlyph_FreeType2::Create(size_t dataSize, const GpRenderedGlyphMetrics &metrics) +GpFontRenderedGlyph_FreeType2 *GpFontRenderedGlyph_FreeType2::Create(IGpAllocator *alloc, size_t dataSize, const GpRenderedGlyphMetrics &metrics) { size_t alignedPrefixSize = (sizeof(GpFontRenderedGlyph_FreeType2) + GP_SYSTEM_MEMORY_ALIGNMENT - 1); alignedPrefixSize -= alignedPrefixSize % GP_SYSTEM_MEMORY_ALIGNMENT; - void *storage = malloc(alignedPrefixSize + dataSize); + void *storage = alloc->Alloc(alignedPrefixSize + dataSize); if (!storage) return nullptr; - return new (storage) GpFontRenderedGlyph_FreeType2(static_cast(storage) + alignedPrefixSize, metrics); + return new (storage) GpFontRenderedGlyph_FreeType2(alloc, static_cast(storage) + alignedPrefixSize, metrics); } void *GpFontRenderedGlyph_FreeType2::GetMutableData() @@ -92,9 +98,10 @@ void *GpFontRenderedGlyph_FreeType2::GetMutableData() } -GpFontRenderedGlyph_FreeType2::GpFontRenderedGlyph_FreeType2(void *data, const GpRenderedGlyphMetrics &metrics) +GpFontRenderedGlyph_FreeType2::GpFontRenderedGlyph_FreeType2(IGpAllocator *alloc, void *data, const GpRenderedGlyphMetrics &metrics) : m_metrics(metrics) , m_data(data) + , m_alloc(alloc) { } @@ -104,8 +111,9 @@ GpFontRenderedGlyph_FreeType2::~GpFontRenderedGlyph_FreeType2() void GpFont_FreeType2::Destroy() { + IGpAllocator *alloc = m_alloc; this->~GpFont_FreeType2(); - free(this); + alloc->Release(this); } IGpFontRenderedGlyph *GpFont_FreeType2::Render(uint32_t unicodeCodePoint, unsigned int size, unsigned int xScale, unsigned int yScale, bool aa) @@ -190,7 +198,7 @@ IGpFontRenderedGlyph *GpFont_FreeType2::Render(uint32_t unicodeCodePoint, unsign metrics.m_glyphDataPitch = pitchRequired; - GpFontRenderedGlyph_FreeType2 *renderedGlyph = GpFontRenderedGlyph_FreeType2::Create(glyphDataSize, metrics); + GpFontRenderedGlyph_FreeType2 *renderedGlyph = GpFontRenderedGlyph_FreeType2::Create(m_alloc, glyphDataSize, metrics); if (!renderedGlyph) return nullptr; @@ -263,13 +271,13 @@ bool GpFont_FreeType2::SupportScaling() const return true; } -GpFont_FreeType2 *GpFont_FreeType2::Create(const FT_StreamRec_ &streamRec, GpIOStream *stream) +GpFont_FreeType2 *GpFont_FreeType2::Create(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream) { - void *storage = malloc(sizeof(GpFont_FreeType2)); + void *storage = alloc->Alloc(sizeof(GpFont_FreeType2)); if (!storage) return nullptr; - return new (storage) GpFont_FreeType2(streamRec, stream); + return new (storage) GpFont_FreeType2(alloc, streamRec, stream); } bool GpFont_FreeType2::FTLoad(const FT_Library &library, int typeFaceIndex) @@ -288,11 +296,12 @@ bool GpFont_FreeType2::FTLoad(const FT_Library &library, int typeFaceIndex) return true; } -GpFont_FreeType2::GpFont_FreeType2(const FT_StreamRec_ &streamRec, GpIOStream *stream) +GpFont_FreeType2::GpFont_FreeType2(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream) : m_face(nullptr) , m_ftStream(streamRec) , m_stream(stream) , m_currentSize(0) + , m_alloc(alloc) { assert(stream); } @@ -305,13 +314,13 @@ GpFont_FreeType2::~GpFont_FreeType2() m_stream->Close(); } -GpFontHandler_FreeType2 *GpFontHandler_FreeType2::Create() +GpFontHandler_FreeType2 *GpFontHandler_FreeType2::Create(IGpAllocator *alloc) { - void *storage = malloc(sizeof(GpFontHandler_FreeType2)); + void *storage = alloc->Alloc(sizeof(GpFontHandler_FreeType2)); if (!storage) return nullptr; - GpFontHandler_FreeType2 *fh = new (storage) GpFontHandler_FreeType2(); + GpFontHandler_FreeType2 *fh = new (storage) GpFontHandler_FreeType2(alloc); if (!fh->Init()) { fh->Shutdown(); @@ -331,7 +340,7 @@ IGpFont *GpFontHandler_FreeType2::LoadFont(GpIOStream *stream, int typeFaceIndex ftStream.read = FTStreamIo; ftStream.close = FTStreamClose; - GpFont_FreeType2 *font = GpFont_FreeType2::Create(ftStream, stream); + GpFont_FreeType2 *font = GpFont_FreeType2::Create(m_alloc, ftStream, stream); if (!font) { stream->Close(); @@ -354,14 +363,16 @@ bool GpFontHandler_FreeType2::KeepStreamOpen() const void GpFontHandler_FreeType2::Shutdown() { + IGpAllocator *alloc = m_alloc; this->~GpFontHandler_FreeType2(); - free(this); + alloc->Release(this); } -GpFontHandler_FreeType2::GpFontHandler_FreeType2() +GpFontHandler_FreeType2::GpFontHandler_FreeType2(IGpAllocator *alloc) : m_ftIsInitialized(false) , m_library(nullptr) , m_currentSize(0) + , m_alloc(alloc) { } @@ -411,18 +422,18 @@ void GpFontHandler_FreeType2::FTStreamClose(FT_Stream stream) void *GpFontHandler_FreeType2::FTAlloc(long size) { - return malloc(static_cast(size)); + return m_alloc->Alloc(static_cast(size)); } void GpFontHandler_FreeType2::FTFree(void* block) { - free(block); + m_alloc->Release(block); } void *GpFontHandler_FreeType2::FTRealloc(long curSize, long newSize, void *block) { (void)curSize; - return realloc(block, static_cast(newSize)); + return m_alloc->Realloc(block, static_cast(newSize)); } bool GpFontHandler_FreeType2::Init() @@ -448,5 +459,5 @@ __declspec(dllexport) #endif IGpFontHandler *GpDriver_CreateFontHandler_FreeType2(const GpFontHandlerProperties &properties) { - return GpFontHandler_FreeType2::Create(); + return GpFontHandler_FreeType2::Create(properties.m_alloc); } diff --git a/GpFontHandler_FreeType2/GpFontHandler_FreeType2.h b/GpFontHandler_FreeType2/GpFontHandler_FreeType2.h index a5bc97d..d190082 100644 --- a/GpFontHandler_FreeType2/GpFontHandler_FreeType2.h +++ b/GpFontHandler_FreeType2/GpFontHandler_FreeType2.h @@ -7,6 +7,7 @@ #include FT_FREETYPE_H class GpIOStream; +struct IGpAllocator; namespace PortabilityLayer { @@ -21,10 +22,10 @@ public: bool KeepStreamOpen() const override; - static GpFontHandler_FreeType2 *Create(); + static GpFontHandler_FreeType2 *Create(IGpAllocator *alloc); private: - GpFontHandler_FreeType2(); + explicit GpFontHandler_FreeType2(IGpAllocator *alloc); ~GpFontHandler_FreeType2(); static void *FTAllocThunk(FT_Memory memory, long size); @@ -44,4 +45,5 @@ private: FT_Library m_library; unsigned int m_currentSize; bool m_ftIsInitialized; + IGpAllocator *m_alloc; }; diff --git a/GpInputDriver_XInput/GpInputDriverXInput.cpp b/GpInputDriver_XInput/GpInputDriverXInput.cpp index b3ccf41..4fefd05 100644 --- a/GpInputDriver_XInput/GpInputDriverXInput.cpp +++ b/GpInputDriver_XInput/GpInputDriverXInput.cpp @@ -2,6 +2,7 @@ #include "GpVOSEvent.h" #include "GpWindows.h" #include "IGpVOSEventQueue.h" +#include "IGpAllocator.h" #include #include @@ -72,8 +73,9 @@ void GpInputDriverXInput::ProcessInput() void GpInputDriverXInput::Shutdown() { + IGpAllocator *alloc = m_properties.m_alloc; this->~GpInputDriverXInput(); - free(this); + alloc->Release(this); } IGpPrefsHandler *GpInputDriverXInput::GetPrefsHandler() const @@ -83,7 +85,7 @@ IGpPrefsHandler *GpInputDriverXInput::GetPrefsHandler() const GpInputDriverXInput *GpInputDriverXInput::Create(const GpInputDriverProperties &props) { - void *storage = malloc(sizeof(GpInputDriverXInput)); + void *storage = props.m_alloc->Alloc(sizeof(GpInputDriverXInput)); if (!storage) return nullptr; diff --git a/GpShell/GpGlobalConfig.h b/GpShell/GpGlobalConfig.h index d37849d..09d5ae6 100644 --- a/GpShell/GpGlobalConfig.h +++ b/GpShell/GpGlobalConfig.h @@ -10,6 +10,7 @@ struct IGpLogDriver; struct IGpSystemServices; +struct IGpAllocator; struct GpGlobalConfig { @@ -22,6 +23,7 @@ struct GpGlobalConfig IGpLogDriver *m_logger; IGpSystemServices *m_systemServices; + IGpAllocator *m_allocator; void *m_osGlobals; }; diff --git a/GpShell/GpMain.cpp b/GpShell/GpMain.cpp index b30f3b3..9488181 100644 --- a/GpShell/GpMain.cpp +++ b/GpShell/GpMain.cpp @@ -11,6 +11,7 @@ #include "GpInputDriverProperties.h" #include "GpGlobalConfig.h" #include "GpAppEnvironment.h" +#include "IGpAllocator.h" #include "IGpAudioDriver.h" #include "IGpDisplayDriver.h" #include "IGpFontHandler.h" @@ -36,6 +37,7 @@ int GpMain::Run() { GpVOSEventQueue *eventQueue = new GpVOSEventQueue(); GpAppEnvironment *appEnvironment = new GpAppEnvironment(); + IGpAllocator *alloc = g_gpGlobalConfig.m_allocator; GpDisplayDriverProperties ddProps; memset(&ddProps, 0, sizeof(ddProps)); @@ -60,6 +62,7 @@ int GpMain::Run() ddProps.m_eventQueue = eventQueue; ddProps.m_logger = g_gpGlobalConfig.m_logger; ddProps.m_systemServices = g_gpGlobalConfig.m_systemServices; + ddProps.m_alloc = g_gpGlobalConfig.m_allocator; GpAudioDriverProperties adProps; memset(&adProps, 0, sizeof(adProps)); @@ -68,6 +71,7 @@ int GpMain::Run() memset(&fontProps, 0, sizeof(fontProps)); fontProps.m_type = g_gpGlobalConfig.m_fontHandlerType; + fontProps.m_alloc = g_gpGlobalConfig.m_allocator; // The sample rate used in all of Glider PRO's sound is 0x56ee8ba3 // This appears to be the "standard" Mac sample rate, probably rounded from 244800/11. @@ -80,8 +84,9 @@ int GpMain::Run() #endif adProps.m_logger = g_gpGlobalConfig.m_logger; adProps.m_systemServices = g_gpGlobalConfig.m_systemServices; + adProps.m_alloc = g_gpGlobalConfig.m_allocator; - IGpInputDriver **inputDrivers = static_cast(malloc(sizeof(IGpInputDriver*) * g_gpGlobalConfig.m_numInputDrivers)); + IGpInputDriver **inputDrivers = static_cast(alloc->Alloc(sizeof(IGpInputDriver*) * g_gpGlobalConfig.m_numInputDrivers)); size_t numCreatedInputDrivers = 0; if (inputDrivers) @@ -93,6 +98,7 @@ int GpMain::Run() inputProps.m_type = g_gpGlobalConfig.m_inputDriverTypes[i]; inputProps.m_eventQueue = eventQueue; + inputProps.m_alloc = g_gpGlobalConfig.m_allocator; if (IGpInputDriver *driver = GpInputDriverFactory::CreateInputDriver(inputProps)) inputDrivers[numCreatedInputDrivers++] = driver; @@ -123,7 +129,7 @@ int GpMain::Run() for (size_t i = 0; i < numCreatedInputDrivers; i++) inputDrivers[i]->Shutdown(); - free(inputDrivers); + alloc->Release(inputDrivers); } return 0; diff --git a/MergeGPF/MergeGPF.cpp b/MergeGPF/MergeGPF.cpp index b1ea2fd..783f8e1 100644 --- a/MergeGPF/MergeGPF.cpp +++ b/MergeGPF/MergeGPF.cpp @@ -5,6 +5,8 @@ #include "DeflateCodec.h" #include "MacFileInfo.h" #include "ZipFile.h" +#include "GpAllocator_C.h" +#include "PLDrivers.h" #include #include @@ -12,6 +14,9 @@ int toolMain(int argc, const char **argv) { + GpDriverCollection *drivers = PLDrivers::GetDriverCollection(); + drivers->SetDriver(GpAllocator_C::GetInstance()); + if (argc != 2) { fprintf(stderr, "Usage: MergeGPF "); diff --git a/MergeGPF/MergeGPF.vcxproj b/MergeGPF/MergeGPF.vcxproj index 808798b..bc0571a 100644 --- a/MergeGPF/MergeGPF.vcxproj +++ b/MergeGPF/MergeGPF.vcxproj @@ -42,6 +42,7 @@ + @@ -50,6 +51,7 @@ + @@ -84,6 +86,7 @@ + diff --git a/MergeGPF/MergeGPF.vcxproj.filters b/MergeGPF/MergeGPF.vcxproj.filters index f3f177d..3c48d44 100644 --- a/MergeGPF/MergeGPF.vcxproj.filters +++ b/MergeGPF/MergeGPF.vcxproj.filters @@ -18,5 +18,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/PortabilityLayer/DeflateCodec.cpp b/PortabilityLayer/DeflateCodec.cpp index b87b64c..ff1caa0 100644 --- a/PortabilityLayer/DeflateCodec.cpp +++ b/PortabilityLayer/DeflateCodec.cpp @@ -371,6 +371,6 @@ uint32_t PortabilityLayer::DeflateContext::CRC32(uint32_t inputValue, const void PortabilityLayer::InflateContext *PortabilityLayer::InflateContext::Create() -{ +{ return InflateContextImpl::Create(); } diff --git a/PortabilityLayer/DialogManager.cpp b/PortabilityLayer/DialogManager.cpp index 2a01013..18f0803 100644 --- a/PortabilityLayer/DialogManager.cpp +++ b/PortabilityLayer/DialogManager.cpp @@ -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 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(); diff --git a/PortabilityLayer/FileManager.cpp b/PortabilityLayer/FileManager.cpp index aae4990..0ffdf85 100644 --- a/PortabilityLayer/FileManager.cpp +++ b/PortabilityLayer/FileManager.cpp @@ -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; diff --git a/PortabilityLayer/FileSectionStream.cpp b/PortabilityLayer/FileSectionStream.cpp index ac5ce70..38fe5aa 100644 --- a/PortabilityLayer/FileSectionStream.cpp +++ b/PortabilityLayer/FileSectionStream.cpp @@ -1,4 +1,5 @@ #include "FileSectionStream.h" +#include "PLCore.h" #include #include @@ -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; diff --git a/PortabilityLayer/FontFamily.cpp b/PortabilityLayer/FontFamily.cpp index 0cf96fe..8e7c85e 100644 --- a/PortabilityLayer/FontFamily.cpp +++ b/PortabilityLayer/FontFamily.cpp @@ -7,6 +7,7 @@ #include "MemReaderStream.h" #include "MemoryManager.h" +#include "PLCore.h" #include "PLDrivers.h" #include @@ -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) diff --git a/PortabilityLayer/FontRenderer.cpp b/PortabilityLayer/FontRenderer.cpp index a6c2a13..82c6c90 100644 --- a/PortabilityLayer/FontRenderer.cpp +++ b/PortabilityLayer/FontRenderer.cpp @@ -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; diff --git a/PortabilityLayer/InflateStream.cpp b/PortabilityLayer/InflateStream.cpp index 3328b59..619a65c 100644 --- a/PortabilityLayer/InflateStream.cpp +++ b/PortabilityLayer/InflateStream.cpp @@ -1,5 +1,6 @@ #include "InflateStream.h" #include "DeflateCodec.h" +#include "PLCore.h" #include #include @@ -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(); diff --git a/PortabilityLayer/MMBlock.cpp b/PortabilityLayer/MMBlock.cpp deleted file mode 100644 index 3902d6e..0000000 --- a/PortabilityLayer/MMBlock.cpp +++ /dev/null @@ -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; - } -} diff --git a/PortabilityLayer/MMBlock.h b/PortabilityLayer/MMBlock.h deleted file mode 100644 index 7e9ef0c..0000000 --- a/PortabilityLayer/MMBlock.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#ifndef __PL_MM_BLOCK_H__ -#define __PL_MM_BLOCK_H__ - -#include - -#include "CoreDefs.h" -#include "SmallestInt.h" - -namespace PortabilityLayer -{ - struct MMBlock - { - SmallestUInt::ValueType_t m_offsetFromAllocLocation; - - static size_t AlignedSize(); - }; -} - -#endif diff --git a/PortabilityLayer/MemoryManager.cpp b/PortabilityLayer/MemoryManager.cpp index 5697df2..6fba318 100644 --- a/PortabilityLayer/MemoryManager.cpp +++ b/PortabilityLayer/MemoryManager.cpp @@ -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 #include @@ -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(buf); - const MMBlock *oldBufMMBlock = reinterpret_cast(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(realloc(oldBufBase, newSize + mmBlockSizeWithMaxPadding)); - if (!newBuffer) - return nullptr; - - const intptr_t offsetFromAlignPoint = reinterpret_cast(newBuffer) & static_cast(GP_SYSTEM_MEMORY_ALIGNMENT - 1); - intptr_t alignPadding = 0; - if (offsetFromAlignPoint != 0) - alignPadding = static_cast(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint; - - // Check if the alignment changed, if so relocate - if (static_cast(alignPadding) != oldBufOffsetFromAlignLoc) - memmove(newBuffer + alignPadding, newBuffer + oldBufOffsetFromAlignLoc, MMBlock::AlignedSize() + newSize); - - MMBlock *newMMBlock = reinterpret_cast(newBuffer + alignPadding); - newMMBlock->m_offsetFromAllocLocation = static_cast::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(malloc(size + mmBlockSizeWithMaxPadding)); - if (!buffer) - return nullptr; - - const intptr_t offsetFromAlignPoint = reinterpret_cast(buffer) & static_cast(GP_SYSTEM_MEMORY_ALIGNMENT - 1); - intptr_t alignPadding = 0; - if (offsetFromAlignPoint != 0) - alignPadding = static_cast(GP_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint; - - MMBlock *mmBlock = reinterpret_cast(buffer + alignPadding); - mmBlock->m_offsetFromAllocLocation = static_cast::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(buf); - const MMBlock *mmBlock = reinterpret_cast(bytes - MMBlock::AlignedSize()); - - void *freeLoc = bytes - MMBlock::AlignedSize() - mmBlock->m_offsetFromAllocLocation; - free(freeLoc); + PLDrivers::GetAlloc()->Realloc(buf, 0); } MMHandleBlock *MemoryManagerImpl::AllocHandle(size_t size) diff --git a/PortabilityLayer/MemoryManager.h b/PortabilityLayer/MemoryManager.h index 5d56fe4..0b0baf2 100644 --- a/PortabilityLayer/MemoryManager.h +++ b/PortabilityLayer/MemoryManager.h @@ -1,10 +1,10 @@ #pragma once -#ifndef __PL_MEMORY_MANAGER_H__ -#define __PL_MEMORY_MANAGER_H__ #include #include +struct IGpAllocator; + namespace PortabilityLayer { struct MMHandleBlock; @@ -50,5 +50,3 @@ namespace PortabilityLayer return objectHdl; } } - -#endif diff --git a/PortabilityLayer/MenuManager.cpp b/PortabilityLayer/MenuManager.cpp index f2f07c9..7ba0c94 100644 --- a/PortabilityLayer/MenuManager.cpp +++ b/PortabilityLayer/MenuManager.cpp @@ -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(malloc(sizeof(GraphicType_t))); + void *storage = static_cast(NewPtr(sizeof(GraphicType_t))); if (storage) { memcpy(m_iconMask, static_cast(*icsHandle) + 32, 32); diff --git a/PortabilityLayer/PLDrivers.cpp b/PortabilityLayer/PLDrivers.cpp index 57f5dd0..d3a36a6 100644 --- a/PortabilityLayer/PLDrivers.cpp +++ b/PortabilityLayer/PLDrivers.cpp @@ -50,4 +50,10 @@ IGpVOSEventQueue *PLDrivers::GetVOSEventQueue() return ms_drivers.GetDriver(); } +IGpAllocator *PLDrivers::GetAlloc() +{ + return ms_drivers.GetDriver(); +} + + GpDriverCollection PLDrivers::ms_drivers; diff --git a/PortabilityLayer/PLDrivers.h b/PortabilityLayer/PLDrivers.h index 22a898b..5d410ae 100644 --- a/PortabilityLayer/PLDrivers.h +++ b/PortabilityLayer/PLDrivers.h @@ -16,6 +16,7 @@ public: static IGpSystemServices *GetSystemServices(); static IGpFontHandler *GetFontHandler(); static IGpVOSEventQueue *GetVOSEventQueue(); + static IGpAllocator *GetAlloc(); private: static GpDriverCollection ms_drivers; diff --git a/PortabilityLayer/PortabilityLayer.vcxproj b/PortabilityLayer/PortabilityLayer.vcxproj index faacf9f..6d4d862 100644 --- a/PortabilityLayer/PortabilityLayer.vcxproj +++ b/PortabilityLayer/PortabilityLayer.vcxproj @@ -124,7 +124,6 @@ - @@ -250,7 +249,6 @@ - diff --git a/PortabilityLayer/PortabilityLayer.vcxproj.filters b/PortabilityLayer/PortabilityLayer.vcxproj.filters index 94f7687..73b1f28 100644 --- a/PortabilityLayer/PortabilityLayer.vcxproj.filters +++ b/PortabilityLayer/PortabilityLayer.vcxproj.filters @@ -132,9 +132,6 @@ Header Files - - Header Files - Header Files @@ -494,9 +491,6 @@ Source Files - - Source Files - Source Files diff --git a/PortabilityLayer/PortabilityLayer_Combined.cpp b/PortabilityLayer/PortabilityLayer_Combined.cpp index 29c8afb..ae49bf3 100644 --- a/PortabilityLayer/PortabilityLayer_Combined.cpp +++ b/PortabilityLayer/PortabilityLayer_Combined.cpp @@ -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" diff --git a/PortabilityLayer/ScanlineMask.cpp b/PortabilityLayer/ScanlineMask.cpp index 6ff11a9..d239621 100644 --- a/PortabilityLayer/ScanlineMask.cpp +++ b/PortabilityLayer/ScanlineMask.cpp @@ -3,6 +3,8 @@ #include "ScanlineMaskBuilder.h" #include "ScanlineMaskIterator.h" +#include "PLCore.h" + #include #include @@ -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; diff --git a/PortabilityLayer/ScanlineMaskConverter.cpp b/PortabilityLayer/ScanlineMaskConverter.cpp index 03bfdbe..6951cd2 100644 --- a/PortabilityLayer/ScanlineMaskConverter.cpp +++ b/PortabilityLayer/ScanlineMaskConverter.cpp @@ -7,6 +7,7 @@ #include "LinePlotter.h" #include "ScanlineMaskBuilder.h" #include "IPlotter.h" +#include "PLCore.h" #include #include @@ -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(height), minPoint.m_x + static_cast(width)), maskBuilder); } diff --git a/PortabilityLayer/WorkerThread.cpp b/PortabilityLayer/WorkerThread.cpp index 5ba031c..85288b4 100644 --- a/PortabilityLayer/WorkerThread.cpp +++ b/PortabilityLayer/WorkerThread.cpp @@ -2,6 +2,7 @@ #include "IGpThreadEvent.h" #include "IGpSystemServices.h" +#include "PLCore.h" #include "PLDrivers.h" #include @@ -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; diff --git a/gpr2gpa/gpr2gpa.cpp b/gpr2gpa/gpr2gpa.cpp index bc329dd..366e936 100644 --- a/gpr2gpa/gpr2gpa.cpp +++ b/gpr2gpa/gpr2gpa.cpp @@ -2,6 +2,7 @@ #include "CFileStream.h" #include "CombinedTimestamp.h" #include "GPArchive.h" +#include "GpAllocator_C.h" #include "MacRomanConversion.h" #include "MemReaderStream.h" #include "QDPictDecoder.h" @@ -15,6 +16,7 @@ #include "ZipFile.h" #include "WaveFormat.h" #include "GpUnicode.h" +#include "PLDrivers.h" #include "zlib.h" @@ -1431,6 +1433,9 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes PortabilityLayer::CFileStream cfs(inF); + GpDriverCollection *drivers = PLDrivers::GetDriverCollection(); + drivers->SetDriver(GpAllocator_C::GetInstance()); + PortabilityLayer::ResourceFile *resFile = PortabilityLayer::ResourceFile::Create(); resFile->Load(&cfs); cfs.Close(); diff --git a/gpr2gpa/gpr2gpa.vcxproj b/gpr2gpa/gpr2gpa.vcxproj index 23bcd52..2b8f5c9 100644 --- a/gpr2gpa/gpr2gpa.vcxproj +++ b/gpr2gpa/gpr2gpa.vcxproj @@ -45,6 +45,7 @@ + @@ -56,6 +57,7 @@ + @@ -95,6 +97,7 @@ + diff --git a/gpr2gpa/gpr2gpa.vcxproj.filters b/gpr2gpa/gpr2gpa.vcxproj.filters index e68c12b..ae6bba6 100644 --- a/gpr2gpa/gpr2gpa.vcxproj.filters +++ b/gpr2gpa/gpr2gpa.vcxproj.filters @@ -21,6 +21,9 @@ Source Files + + Source Files +