From f58cccc86fa4bfb8102d8e76f4815a3475f7d136 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:35:51 -0600 Subject: [PATCH] refactor(storm): one wrapper for SMemAlloc and SMemFree Storm's SMemAlloc (FUN_STORM_SMEM_ALLOC, 0x006462E0) and SMemFree (FUN_STORM_SMEM_FREE, 0x00646430), both __stdcall with four arguments, were called by hand in thirteen files, through AddOns::EngineIO's typedefs, local typedefs, Event::Custom's own pair and Frame::Subclass::Allocate. storm/Memory.h now holds Storm::Alloc, with a zero-fill option for the flag bit 3 (Offsets::STORM_SMEM_FLAG_ZERO_FILL), and Storm::Free. Every caller uses them, with the same file and line labels as before; the EngineIO typedefs, Custom's pair and Subclass::Allocate are gone. --- src/Offsets.h | 1 + src/addons/Embedded.cpp | 10 ++---- src/addons/EngineIO.h | 6 +--- src/addons/LocalTable.cpp | 5 ++- src/addons/Rescan.cpp | 5 ++- src/addons/SavedVarsFirst.cpp | 5 ++- src/addons/TocRewrite.cpp | 9 ++--- src/bindings/Inject.cpp | 9 ++--- src/bindings/ModifiedClick.cpp | 4 +-- src/event/Custom.cpp | 26 ++++---------- src/frame/Subclass.cpp | 7 ---- src/frame/Subclass.h | 14 ++++---- src/interface/Export.cpp | 5 ++- src/scrollingmessageframe/MessageInfo.cpp | 10 +++--- src/storm/Memory.h | 41 +++++++++++++++++++++++ src/texture/DimensionGate.cpp | 8 ++--- src/xml/Intrinsics.cpp | 4 +-- 17 files changed, 84 insertions(+), 85 deletions(-) create mode 100644 src/storm/Memory.h diff --git a/src/Offsets.h b/src/Offsets.h index 55c11b42..65b33191 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -9753,6 +9753,7 @@ enum Offsets { // engine's reload teardown loop (which `SMemFree`s every event entry's // name) doesn't trip its safety check on our injected pointers. FUN_STORM_SMEM_ALLOC = 0x006462E0, + STORM_SMEM_FLAG_ZERO_FILL = 0x08, // SMemAlloc flags bit 3 FUN_STORM_SMEM_FREE = 0x00646430, // Engine's main file-content reader. Used by the TOC parser at diff --git a/src/addons/Embedded.cpp b/src/addons/Embedded.cpp index 949100d3..3a5103cc 100644 --- a/src/addons/Embedded.cpp +++ b/src/addons/Embedded.cpp @@ -77,6 +77,7 @@ #include "addons/TocRewrite.h" #include "bindings/Inject.h" #include "embed/Resource.h" +#include "storm/Memory.h" #include #include @@ -153,8 +154,6 @@ Embed::Bytes LookupEmbedded(const char *relPath) { // by the caller's standard SMemFree in turn. using AddOns::EngineIO::FileExistsFn; using AddOns::EngineIO::FileReadFn; -using AddOns::EngineIO::SMemAllocFn; -using AddOns::EngineIO::SMemFreeFn; // The trampolines to the original FUN_FILE_READ / FUN_FILE_EXISTS (installed // by the co-hooks below). @@ -277,8 +276,7 @@ void DecideSource(int addon) { // Free the disk buffer we just borrowed — we only needed it for // the version-line scrape. The actual content for the engine's // TOC read comes through the regular hook path below. - auto SMemFree = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); - SMemFree(diskBuf, __FILE__, __LINE__, 0); + Storm::Free(diskBuf, __FILE__, __LINE__); // Missing or unparseable disk version → assume it's older than // anything we ship, embedded wins. @@ -320,10 +318,8 @@ int ReadAddonFile(int addon, const char *relPath, int unused, const char *path, flag1, flag2); } - auto SMemAlloc = reinterpret_cast( - Offsets::FUN_STORM_SMEM_ALLOC); const size_t totalSize = entry.size + extraBytes; - void *buf = SMemAlloc(totalSize, __FILE__, __LINE__, 0); + void *buf = Storm::Alloc(totalSize, __FILE__, __LINE__); if (buf == nullptr) return 0; std::memcpy(buf, entry.data, entry.size); if (extraBytes > 0) { diff --git a/src/addons/EngineIO.h b/src/addons/EngineIO.h index c389797a..2432765d 100644 --- a/src/addons/EngineIO.h +++ b/src/addons/EngineIO.h @@ -32,10 +32,6 @@ using FileReadFn = int(__stdcall *)(int unused, const char *path, void **outBuf, // FUN_FILE_EXISTS (0x00648A30) — __stdcall, RET 8. using FileExistsFn = int(__stdcall *)(const char *path, int mode); -// FUN_STORM_SMEM_ALLOC / FUN_STORM_SMEM_FREE — __stdcall, RET 0x10. Buffers -// FUN_FILE_READ hands out are freed with SMemFreeFn; a buffer we allocate with -// SMemAllocFn is freed cleanly by the engine's own SMemFree in turn. -using SMemAllocFn = void *(__stdcall *)(size_t size, const char *file, int line, int flags); -using SMemFreeFn = void(__stdcall *)(void *buf, const char *file, int line, int flags); +// Buffers FUN_FILE_READ hands out are freed with Storm::Free (storm/Memory.h). } // namespace AddOns::EngineIO diff --git a/src/addons/LocalTable.cpp b/src/addons/LocalTable.cpp index 29bef3f4..244eb53e 100644 --- a/src/addons/LocalTable.cpp +++ b/src/addons/LocalTable.cpp @@ -34,6 +34,7 @@ #include "addons/EngineIO.h" #include "addons/Toc.h" #include "luasyntax/AddonNamespace.h" +#include "storm/Memory.h" #include #include @@ -51,7 +52,6 @@ using IsLoadedFn_t = uint8_t(__fastcall *)(const char *name); // FUN_FILE_READ address routes through Embedded's file-read hook, which forwards // every non-`!!!ClassicAPI` path to the original untouched — what we want here. using AddOns::EngineIO::FileReadFn; -using AddOns::EngineIO::SMemFreeFn; // True iff the addon's `.toc` has a line `## AllowAddOnTableAccess: `. // The value is read as a TOC boolean flag: parse the leading integer, true iff @@ -87,8 +87,7 @@ bool TocAllowsTableAccess(const char *name) { allowed = anyDigit && value != 0; } - auto smemFree = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); - smemFree(buf, __FILE__, __LINE__, 0); + Storm::Free(buf, __FILE__, __LINE__); return allowed; } diff --git a/src/addons/Rescan.cpp b/src/addons/Rescan.cpp index 2a62a0e9..4f10e318 100644 --- a/src/addons/Rescan.cpp +++ b/src/addons/Rescan.cpp @@ -86,6 +86,7 @@ #include "addons/Embedded.h" #include "addons/EngineIO.h" #include "addons/Registry.h" +#include "storm/Memory.h" #include #include @@ -235,9 +236,7 @@ bool TryHashToc(const char *name, uint32_t *outHash) { if (!ReadToc(name, &buf, &size)) return false; *outHash = HashTocMetadata(static_cast(buf), size); - reinterpret_cast( - static_cast(Offsets::FUN_STORM_SMEM_FREE))( - buf, __FILE__, __LINE__, 0); + Storm::Free(buf, __FILE__, __LINE__); return true; } diff --git a/src/addons/SavedVarsFirst.cpp b/src/addons/SavedVarsFirst.cpp index a9a4ca01..c8eb23aa 100644 --- a/src/addons/SavedVarsFirst.cpp +++ b/src/addons/SavedVarsFirst.cpp @@ -48,6 +48,7 @@ #include "addons/EngineIO.h" #include "addons/Toc.h" #include "script/LoadFile.h" +#include "storm/Memory.h" #include #include @@ -66,7 +67,6 @@ constexpr size_t NPOS = static_cast(-1); // FlavorToc applies), the exists-check, and the Storm free. See addons/EngineIO.h. using AddOns::EngineIO::FileExistsFn; using AddOns::EngineIO::FileReadFn; -using AddOns::EngineIO::SMemFreeFn; using LoadTocFilesFn = uint32_t(__fastcall *)(char *tocPath, int *a2, int *a3); using NameFn = const char *(*)(); // no-arg readers (return in EAX) @@ -160,8 +160,7 @@ SvFlags ReadSvFlags(const char *tocPath) { f.perChar = AddOns::Toc::FindValue(b, size, "SavedVariablesPerCharacter", &v, &n) && n > 0; - reinterpret_cast(static_cast(Offsets::FUN_STORM_SMEM_FREE))( - buf, __FILE__, __LINE__, 0); + Storm::Free(buf, __FILE__, __LINE__); return f; } diff --git a/src/addons/TocRewrite.cpp b/src/addons/TocRewrite.cpp index d4a77f4d..9c713416 100644 --- a/src/addons/TocRewrite.cpp +++ b/src/addons/TocRewrite.cpp @@ -17,6 +17,7 @@ #include "localization/Locale.h" #include "Offsets.h" +#include "storm/Memory.h" #include "turtle/Detect.h" #include @@ -28,8 +29,6 @@ namespace AddOns::TocRewrite { namespace { -using AddOns::EngineIO::SMemAllocFn; -using AddOns::EngineIO::SMemFreeFn; using AddOns::Toc::EqCI; using AddOns::Toc::Lower; @@ -288,15 +287,13 @@ void Transform(const char *path, void **outBuf, size_t *outSize) { std::memcmp(rebuilt.data(), content, size) == 0) return; // brackets/commas were only in unaffected lines — nothing changed - auto SMemAlloc = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC); - auto SMemFree = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); const size_t newLen = rebuilt.size(); - void *buf = SMemAlloc(newLen + 1, __FILE__, __LINE__, 0); // +1 NUL (extraBytes=1) + void *buf = Storm::Alloc(newLen + 1, __FILE__, __LINE__); // +1 NUL (extraBytes=1) if (buf == nullptr) return; // OOM — keep the original std::memcpy(buf, rebuilt.data(), newLen); static_cast(buf)[newLen] = '\0'; - SMemFree(*outBuf, __FILE__, __LINE__, 0); + Storm::Free(*outBuf, __FILE__, __LINE__); *outBuf = buf; if (outSize != nullptr) *outSize = newLen; } diff --git a/src/bindings/Inject.cpp b/src/bindings/Inject.cpp index 19c25919..7bf803f1 100644 --- a/src/bindings/Inject.cpp +++ b/src/bindings/Inject.cpp @@ -49,6 +49,7 @@ #include "Inject.h" #include "Common.h" #include "Offsets.h" +#include "storm/Memory.h" #include "embed/Resource.h" #include "embedded_resources.h" @@ -213,8 +214,6 @@ const char *FindSplicePosition(const char *content, size_t size, return FindOutsideComment(content, end, ""); } -using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); -using SMemFree_t = void(__stdcall *)(void *buf, const char *file, int line, int flags); } // namespace @@ -257,10 +256,8 @@ bool TryHandle(int unused, const char *path, void **outBuf, size_t *outSize, const size_t splicePos = static_cast(splice - content); const size_t newSize = origSize + config.innerSize; - auto SMemAlloc = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC); - auto SMemFree = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); - void *newBuf = SMemAlloc(newSize + extraBytes, __FILE__, __LINE__, 0); + void *newBuf = Storm::Alloc(newSize + extraBytes, __FILE__, __LINE__); if (newBuf == nullptr) { // Allocation failed — fall back to the original buffer so the // engine still loads its own bindings. @@ -277,7 +274,7 @@ bool TryHandle(int unused, const char *path, void **outBuf, size_t *outSize, if (extraBytes > 0) std::memset(dest + newSize, 0, extraBytes); - SMemFree(origBuf, __FILE__, __LINE__, 0); + Storm::Free(origBuf, __FILE__, __LINE__); if (outBuf != nullptr) *outBuf = newBuf; if (outSize != nullptr) *outSize = newSize; diff --git a/src/bindings/ModifiedClick.cpp b/src/bindings/ModifiedClick.cpp index 79c78825..e9aa7c8e 100644 --- a/src/bindings/ModifiedClick.cpp +++ b/src/bindings/ModifiedClick.cpp @@ -46,6 +46,7 @@ #include "baselib/Ascii.h" #include "frame/ClickEvents.h" #include "input/Modifier.h" +#include "storm/Memory.h" #include "xml/FrameLoad.h" #include @@ -219,8 +220,7 @@ void LoadModifiedClicks(const char *path, void *status) { return; void *tree = reinterpret_cast(Offsets::FUN_XML_TREE_LOAD)( static_cast(data), static_cast(size)); - reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(data, __FILE__, - __LINE__, 0); + Storm::Free(data, __FILE__, __LINE__); if (tree == nullptr) return; // the engine loader reports the parse error const void *root = Game::Read(tree, 0); diff --git a/src/event/Custom.cpp b/src/event/Custom.cpp index 1a302def..150e1dea 100644 --- a/src/event/Custom.cpp +++ b/src/event/Custom.cpp @@ -19,6 +19,7 @@ #include "frame/ScriptArgs.h" #include "frame/UnitEvent.h" #include "profile/ScriptTime.h" +#include "storm/Memory.h" #include #include @@ -94,23 +95,6 @@ char *SStrDup(const char *s) { return fn(s, __FILE__, __LINE__); } -// Storm allocator/free — the same pair the engine uses for the event-table -// buffer (see RebuildEventTable), so its reload teardown `SMemFree`s our -// grown buffer exactly as its own. Both `__stdcall`, 4 args (`ret 0x10`). -// SMemAlloc flag bit 8 zero-fills (matching SetEventCount's fresh entries). -void *SMemAlloc(unsigned size) { - using SMemAlloc_t = void *(__stdcall *)(unsigned size, const char *file, - int line, int flags); - auto fn = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC); - return fn(size, __FILE__, __LINE__, 8 /* zero-fill */); -} -void SMemFree(void *ptr) { - using SMemFree_t = int(__stdcall *)(void *ptr, const char *file, int line, - int flags); - auto fn = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); - fn(ptr, __FILE__, __LINE__, 0); -} - // True when slot `id` of the live table can take one of our names: its name // is NULL (no one owns it) AND the engine never signals it by number. A NULL // name alone is not enough — the glue band (ids below GLUE_EVENT_COUNT) and @@ -294,8 +278,12 @@ void Grow() { constexpr int kMargin = 16; const int deficit = (nulls < demand) ? (demand - nulls) : 0; const int newCount = count + deficit + kMargin; + // Storm's heap, like the engine's own table buffer (see RebuildEventTable), + // so its reload teardown SMemFrees ours the same way; zero-filled, like + // SetEventCount's fresh entries. auto *newBuf = static_cast( - SMemAlloc(static_cast(newCount) * Offsets::EVENT_ENTRY_STRIDE)); + Storm::Alloc(static_cast(newCount) * Offsets::EVENT_ENTRY_STRIDE, __FILE__, + __LINE__, true)); if (newBuf == nullptr) return; // alloc failed — keep the old table; claiming still works @@ -323,7 +311,7 @@ void Grow() { Game::Ref(Offsets::VAR_EVENT_TABLE_BASE_PTR) = newBuf; Game::Ref(Offsets::VAR_EVENT_TABLE_CAP) = newCount; Game::Ref(Offsets::VAR_EVENT_TABLE_COUNT) = newCount; - SMemFree(base); + Storm::Free(base, __FILE__, __LINE__); Debug::Log::Printf("[event] grew table %d -> %d (%d NULL, %d reserved%s)", count, newCount, nulls, demand, diff --git a/src/frame/Subclass.cpp b/src/frame/Subclass.cpp index f76ce813..78c1d487 100644 --- a/src/frame/Subclass.cpp +++ b/src/frame/Subclass.cpp @@ -21,7 +21,6 @@ namespace { // static initialization whatever the order of the translation units. MethodHash *g_hashes = nullptr; -using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); using RegisterFactory_t = int(__fastcall *)(const char *type, Factory_t factory, int unique); using PushMethod_t = int(__fastcall *)(void *L, const char *name, void *hash); using UnregisterMethods_t = void(__fastcall *)(void *hash); @@ -41,12 +40,6 @@ const Game::HookAutoRegister _unregisterHook{ } // namespace -void *Allocate(size_t size, const char *file, int line) { - constexpr int kZeroFill = 0x08; - return reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC)(size, file, line, - kZeroFill); -} - void RegisterFactory(const char *type, Factory_t factory) { reinterpret_cast(Offsets::FUN_FRAMEXML_REGISTER_FACTORY)(type, factory, 0); } diff --git a/src/frame/Subclass.h b/src/frame/Subclass.h index 23218626..0db8bb29 100644 --- a/src/frame/Subclass.h +++ b/src/frame/Subclass.h @@ -21,6 +21,7 @@ #include "Game.h" #include "Offsets.h" #include "frame/Layout.h" +#include "storm/Memory.h" #include #include @@ -180,17 +181,14 @@ bool BuildVTables() { return true; } -// The allocation an engine factory makes (the "Frame" one, 0x006EEC10), sized -// for the whole object and zero-filled; null when it fails. -void *Allocate(size_t size, const char *file, int line); - -// A new frame of the class, shaped like the engine's factories: the block -// (our part zero), the engine class's constructor, then our vtables, which -// BuildVTables must have built. Null when the allocation fails. +// A new frame of the class, shaped like the engine's factories (the "Frame" +// one, 0x006EEC10): a Storm block for the whole object, zero-filled, the +// engine class's constructor, then our vtables, which BuildVTables must have +// built. Null when the allocation fails. template void *New(void *parent) { using Constructor_t = void(__fastcall *)(void *self, void *edx, void *parent); - void *frame = Allocate(C.size, __FILE__, __LINE__); + void *frame = Storm::Alloc(C.size, __FILE__, __LINE__, true); if (frame == nullptr) return nullptr; reinterpret_cast(C.constructor)(frame, nullptr, parent); diff --git a/src/interface/Export.cpp b/src/interface/Export.cpp index bcc6e5eb..6f1041ce 100644 --- a/src/interface/Export.cpp +++ b/src/interface/Export.cpp @@ -93,6 +93,7 @@ #include "Game.h" #include "Offsets.h" #include "debug/Log.h" +#include "storm/Memory.h" #include @@ -114,7 +115,6 @@ namespace { using FileRead_t = int(__stdcall *)(int unused, const char *path, void **outBuf, unsigned int *outSize, unsigned int extraBytes, int flag1, int flag2); -using SMemFree_t = void(__stdcall *)(void *ptr, const char *file, int line, int flags); // MPQ enumerator (see Offsets::FUN_MPQ_ENUM_FILES). The callback // receives each listfile entry's full path in ecx; returning 0 STOPS @@ -301,7 +301,6 @@ void ScanPathGetters(Collector &c) { int WriteFiles(const std::vector &files, size_t prefixLen, const char *dstRoot, int *skipped) { auto FileRead = reinterpret_cast(Offsets::FUN_FILE_READ); - auto SMemFree = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); int written = 0; int missed = 0; @@ -327,7 +326,7 @@ int WriteFiles(const std::vector &files, size_t prefixLen, static_cast(GetLastError()), dst.c_str()); } - SMemFree(buf, __FILE__, __LINE__, 0); + Storm::Free(buf, __FILE__, __LINE__); } if (skipped != nullptr) *skipped = missed; diff --git a/src/scrollingmessageframe/MessageInfo.cpp b/src/scrollingmessageframe/MessageInfo.cpp index 68e95581..d8ca899b 100644 --- a/src/scrollingmessageframe/MessageInfo.cpp +++ b/src/scrollingmessageframe/MessageInfo.cpp @@ -56,6 +56,7 @@ #include "Game.h" #include "Offsets.h" #include "scrollingmessageframe/DisplayRefreshed.h" +#include "storm/Memory.h" #include #include @@ -265,8 +266,6 @@ uint32_t MessageColor(void *L, void *frame) { // out as before (a hidden frame's layout places nothing). int __fastcall Script_BackFillMessage(void *L) { using namespace Game::Lua; - using StormAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); - using StormFree_t = int(__stdcall *)(void *ptr, const char *file, int line, int flags); using Layout_t = void(__fastcall *)(void *smf, void *edx, int newestRecord); static const char kStormFile[] = "ClassicAPI ScrollingMessageFrame:BackFillMessage"; @@ -284,7 +283,7 @@ int __fastcall Script_BackFillMessage(void *L) { const uint32_t color = MessageColor(L, frame); const size_t bytes = std::strlen(text) + 1; char *copy = static_cast( - reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC)(bytes, kStormFile, __LINE__, 0)); + Storm::Alloc(bytes, kStormFile, __LINE__)); if (copy == nullptr) return 0; std::memcpy(copy, text, bytes); @@ -298,7 +297,7 @@ int __fastcall Script_BackFillMessage(void *L) { uint8_t *record = Game::Read(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGES); char *&stored = *reinterpret_cast(record + Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_TEXT); if (stored != nullptr) - reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(stored, kStormFile, __LINE__, 0); + Storm::Free(stored, kStormFile, __LINE__); stored = copy; *reinterpret_cast(record + Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_COLOR) = color; *reinterpret_cast(record + Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_SHOWN) = 1; @@ -399,7 +398,6 @@ int __fastcall Script_RemoveMessagesByPredicate(void *L) { using Layout_t = void(__fastcall *)(void *smf, void *edx, int newestRecord); using Smf_t = void(__fastcall *)(void *smf); using ScrollOffset_t = int(__fastcall *)(void *smf); - using StormFree_t = int(__stdcall *)(void *ptr, const char *file, int line, int flags); static const char kStormFile[] = "ClassicAPI ScrollingMessageFrame:RemoveMessagesByPredicate"; void *frame = ResolveTypedObject(L, 1, Offsets::VAR_SCROLLINGMESSAGEFRAME_LUA_TYPE_ID); @@ -460,7 +458,7 @@ int __fastcall Script_RemoveMessagesByPredicate(void *L) { uint8_t *from = records + src * Offsets::SCROLLINGMESSAGEFRAME_MESSAGE_STRIDE; char *&landedOn = *reinterpret_cast(to + Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_TEXT); if (landedOn != nullptr) - reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(landedOn, kStormFile, __LINE__, 0); + Storm::Free(landedOn, kStormFile, __LINE__); std::memcpy(to, from, Offsets::SCROLLINGMESSAGEFRAME_MESSAGE_STRIDE); *reinterpret_cast(from + Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_TEXT) = nullptr; if (extras) { diff --git a/src/storm/Memory.h b/src/storm/Memory.h new file mode 100644 index 00000000..a3df548a --- /dev/null +++ b/src/storm/Memory.h @@ -0,0 +1,41 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +// Storm's heap (Offsets.h, at FUN_STORM_SMEM_ALLOC): the blocks the engine +// allocates and frees itself. A block the engine will free must come from +// Alloc, and a block the engine hands over is freed with Free; SMemFree +// rejects any other pointer (ERROR #124). `file` and `line` label the block +// in Storm's bookkeeping. + +#include "Offsets.h" + +#include + +namespace Storm { + +// SMemAlloc: null when out of memory. +inline void *Alloc(size_t size, const char *file, int line, bool zeroFill = false) { + using Fn = void *(__stdcall *)(size_t, const char *, int, int); + return reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC)( + size, file, line, zeroFill ? Offsets::STORM_SMEM_FLAG_ZERO_FILL : 0); +} + +// SMemFree. +inline void Free(void *ptr, const char *file, int line) { + using Fn = int(__stdcall *)(void *, const char *, int, int); + reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(ptr, file, line, 0); +} + +} // namespace Storm diff --git a/src/texture/DimensionGate.cpp b/src/texture/DimensionGate.cpp index a4084018..69a0eb78 100644 --- a/src/texture/DimensionGate.cpp +++ b/src/texture/DimensionGate.cpp @@ -116,6 +116,7 @@ #include "Common.h" #include "Game.h" #include "Offsets.h" +#include "storm/Memory.h" #include @@ -197,7 +198,6 @@ constexpr const char *kAllocTag = "ClassicAPI/texture/DimensionGate.cpp"; using DecodeBufferAlloc_t = void *(__fastcall *)(int format, uint32_t width, uint32_t height, const char *file, uint32_t line); -using SMemFree_t = void(__stdcall *)(void *buf, const char *file, int line, int flags); void *g_retiredScratch = nullptr; // the previous scratch, freed at the next growth @@ -224,8 +224,7 @@ bool GrowScratch(uint32_t side) { // means any holder not accounted for reads stale bytes instead of unmapped // memory. Bounded: one buffer, freed at the next growth. if (g_retiredScratch != nullptr) - reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(g_retiredScratch, kAllocTag, - __LINE__, 0); + Storm::Free(g_retiredScratch, kAllocTag, __LINE__); g_retiredScratch = old; WriteImm32(Offsets::PATCH_TEXDECODE_SCRATCH_DIM_W + 1, side); WriteImm32(Offsets::PATCH_TEXDECODE_SCRATCH_DIM_H + 1, side); @@ -473,8 +472,7 @@ void *__fastcall AllocMips_h(uint32_t format, uint32_t width, uint32_t height) { void __fastcall FreeMips_h(void *buffer, uint32_t format, uint32_t width, uint32_t height) { if (!IsPowerOfTwo(width) || !IsPowerOfTwo(height)) { if (buffer != nullptr) - reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(buffer, kAllocTag, __LINE__, - 0); + Storm::Free(buffer, kAllocTag, __LINE__); return; } FreeMips_o(buffer, format, width, height); diff --git a/src/xml/Intrinsics.cpp b/src/xml/Intrinsics.cpp index 4f3f2997..c337b539 100644 --- a/src/xml/Intrinsics.cpp +++ b/src/xml/Intrinsics.cpp @@ -46,6 +46,7 @@ #include "Game.h" #include "Offsets.h" #include "frame/ScriptBindings.h" +#include "storm/Memory.h" #include "xml/FrameLoad.h" #include "xml/Inherits.h" @@ -60,7 +61,6 @@ using Factory_t = void *(__fastcall *)(void *parent); using RegisterFactory_t = int(__fastcall *)(const char *type, Factory_t factory, int unique); using Hash_t = uint32_t(__stdcall *)(const char *s); using NodeOp_t = void(__fastcall *)(void *node); -using SMemFree_t = void(__stdcall *)(void *ptr, const char *file, int line, int flags); struct Def { const char *name; // the node's own `name` attribute @@ -137,7 +137,7 @@ void Walk(const void *node, void *status, int depth, const Def **found) { void PrepareForReload() { for (const Def &d : g_defs) { reinterpret_cast(Offsets::FUN_XML_NODE_DTOR)(d.node); - reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE)(d.node, __FILE__, __LINE__, 0); + Storm::Free(d.node, __FILE__, __LINE__); } g_defs.clear(); }