From c3f84bfe9c37df94abaff9eaf73cba23ede32c44 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 29 Apr 2025 17:39:09 +0200 Subject: Move ProtectedAllocator::Protected_lua_Alloc in universe.cpp --- src/universe.cpp | 10 ++++++++++ src/universe.hpp | 11 +++-------- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/universe.cpp b/src/universe.cpp index 0f41585..06f7f21 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -51,6 +51,16 @@ static constexpr RegistryUniqueKey kUniverseFullRegKey{ 0x1C2D76870DD9DD9Full }; // ################################################################################################# +[[nodiscard]] +void* ProtectedAllocator::Protected_lua_Alloc(void* const ud_, void* const ptr_, size_t const osize_, size_t const nsize_) +{ + ProtectedAllocator* const _allocator{ static_cast(ud_) }; + std::lock_guard _guard{ _allocator->mutex }; + return _allocator->alloc(ptr_, osize_, nsize_); +} + +// ################################################################################################# + Universe::Universe() { //--- diff --git a/src/universe.hpp b/src/universe.hpp index 88a53a1..fac5f50 100644 --- a/src/universe.hpp +++ b/src/universe.hpp @@ -27,12 +27,7 @@ class ProtectedAllocator final std::mutex mutex; [[nodiscard]] - static void* protected_lua_Alloc(void* const ud_, void* const ptr_, size_t const osize_, size_t const nsize_) - { - ProtectedAllocator* const allocator{ static_cast(ud_) }; - std::lock_guard _guard{ allocator->mutex }; - return allocator->alloc(ptr_, osize_, nsize_); - } + static void* Protected_lua_Alloc(void* const ud_, void* const ptr_, size_t const osize_, size_t const nsize_); public: // we are not like our base class: we can't be created inside a full userdata (or we would have to install a metatable and __gc handler to destroy ourselves properly) @@ -42,13 +37,13 @@ class ProtectedAllocator final AllocatorDefinition makeDefinition() { - return AllocatorDefinition{ protected_lua_Alloc, this }; + return AllocatorDefinition{ Protected_lua_Alloc, this }; } void installIn(lua_State* const L_) const { // install our replacement allocator function (this is a C function, we need to deconst ourselves) - lua_setallocf(L_, protected_lua_Alloc, static_cast(const_cast(this))); + lua_setallocf(L_, Protected_lua_Alloc, static_cast(const_cast(this))); } void removeFrom(lua_State* const L_) const -- cgit v1.2.3-55-g6feb