From 33c967a558fd82e33c0a81eb4d1e37fca3b09928 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 19 Dec 2025 17:42:31 +0000 Subject: [PATCH] Codechange: [Script] Add helpers for script memory allocation accounting --- src/script/squirrel.cpp | 13 +++++++++++++ src/script/squirrel.hpp | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index a3b584e52f..25769d7246 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -30,6 +30,8 @@ */ struct ScriptAllocator { + friend class Squirrel; + private: std::allocator allocator; size_t allocated_size = 0; ///< Sum of allocated data size @@ -814,3 +816,14 @@ SQInteger Squirrel::GetOpsTillSuspend() { return this->vm->_ops_till_suspend; } + +void Squirrel::IncreaseAllocatedSize(size_t bytes) +{ + _squirrel_allocator->CheckAllocationAllowed(bytes); + _squirrel_allocator->allocated_size += bytes; +} + +void Squirrel::DecreaseAllocatedSize(size_t bytes) +{ + _squirrel_allocator->allocated_size -= bytes; +} diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp index 142b19bee4..6ddf2cb200 100644 --- a/src/script/squirrel.hpp +++ b/src/script/squirrel.hpp @@ -291,6 +291,18 @@ public: * Get number of bytes allocated by this VM. */ size_t GetAllocatedMemory() const noexcept; + + /** + * Increase number of bytes allocated in the current script allocator scope. + * @param bytes Number of bytes to increase. + */ + static void IncreaseAllocatedSize(size_t bytes); + + /** + * Decrease number of bytes allocated in the current script allocator scope. + * @param bytes Number of bytes to decrease. + */ + static void DecreaseAllocatedSize(size_t bytes); };