From d7942c3fd09696807a651817b023836d7db84ce9 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 19 Dec 2025 22:23:27 +0000 Subject: [PATCH] Codechange: [Script] Add allocator that uses script memory allocation accounting --- src/script/api/script_object.hpp | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 2305a2af15..76e7d3993c 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -510,4 +510,37 @@ public: } }; +/** + * Allocator that uses script memory allocation accounting. + * @tparam T Type of allocator. + */ +template +struct ScriptStdAllocator +{ + using value_type = T; + + ScriptStdAllocator() = default; + + template + constexpr ScriptStdAllocator(const ScriptStdAllocator &) noexcept {} + + T *allocate(std::size_t n) + { + Squirrel::IncreaseAllocatedSize(n * sizeof(T)); + return std::allocator{}.allocate(n); + } + + void deallocate(T *mem, std::size_t n) + { + Squirrel::DecreaseAllocatedSize(n * sizeof(T)); + std::allocator{}.deallocate(mem, n); + } +}; + +template +bool operator==(const ScriptStdAllocator &, const ScriptStdAllocator &) { return true; } + +template +bool operator!=(const ScriptStdAllocator &, const ScriptStdAllocator &) { return false; } + #endif /* SCRIPT_OBJECT_HPP */