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 */