diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-11-20 12:47:41 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-11-20 12:47:41 +0100 |
commit | 872826ecaca5370e3492385cff3795d995b33ec7 (patch) | |
tree | 6604e8c27564b2fba6d183b304e188a2cf0cb12e /src/universe.hpp | |
parent | b8b4335dba57e1c6159b77d5cb7bbe91318664b4 (diff) | |
download | lanes-872826ecaca5370e3492385cff3795d995b33ec7.tar.gz lanes-872826ecaca5370e3492385cff3795d995b33ec7.tar.bz2 lanes-872826ecaca5370e3492385cff3795d995b33ec7.zip |
AllocatorDefinition implementation improvements
Diffstat (limited to 'src/universe.hpp')
-rw-r--r-- | src/universe.hpp | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/universe.hpp b/src/universe.hpp index a45ce86..77fbb52 100644 --- a/src/universe.hpp +++ b/src/universe.hpp | |||
@@ -20,13 +20,16 @@ class ProtectedAllocator | |||
20 | : public lanes::AllocatorDefinition | 20 | : public lanes::AllocatorDefinition |
21 | { | 21 | { |
22 | private: | 22 | private: |
23 | |||
24 | using super = lanes::AllocatorDefinition; | ||
25 | |||
23 | std::mutex mutex; | 26 | std::mutex mutex; |
24 | 27 | ||
25 | [[nodiscard]] static void* protected_lua_Alloc(void* ud_, void* ptr_, size_t osize_, size_t nsize_) | 28 | [[nodiscard]] static void* protected_lua_Alloc(void* ud_, void* ptr_, size_t osize_, size_t nsize_) |
26 | { | 29 | { |
27 | ProtectedAllocator* const allocator{ static_cast<ProtectedAllocator*>(ud_) }; | 30 | ProtectedAllocator* const allocator{ static_cast<ProtectedAllocator*>(ud_) }; |
28 | std::lock_guard<std::mutex> guard{ allocator->mutex }; | 31 | std::lock_guard<std::mutex> guard{ allocator->mutex }; |
29 | return allocator->allocF(allocator->allocUD, ptr_, osize_, nsize_); | 32 | return allocator->alloc(ptr_, osize_, nsize_); |
30 | } | 33 | } |
31 | 34 | ||
32 | public: | 35 | public: |
@@ -36,21 +39,19 @@ class ProtectedAllocator | |||
36 | 39 | ||
37 | AllocatorDefinition makeDefinition() | 40 | AllocatorDefinition makeDefinition() |
38 | { | 41 | { |
39 | return AllocatorDefinition{ version, protected_lua_Alloc, this }; | 42 | return AllocatorDefinition{ protected_lua_Alloc, this }; |
40 | } | 43 | } |
41 | 44 | ||
42 | void installIn(lua_State* L_) | 45 | void installIn(lua_State* const L_) const |
43 | { | 46 | { |
44 | lua_setallocf(L_, protected_lua_Alloc, this); | 47 | // install our replacement allocator function (this is a C function, we need to deconst ourselves) |
48 | lua_setallocf(L_, protected_lua_Alloc, static_cast<void*>(const_cast<ProtectedAllocator*>(this))); | ||
45 | } | 49 | } |
46 | 50 | ||
47 | void removeFrom(lua_State* L_) | 51 | void removeFrom(lua_State* const L_) const |
48 | { | 52 | { |
49 | // remove the protected allocator, if any | 53 | // restore the base allocator function |
50 | if (allocF != nullptr) { | 54 | super::installIn(L_); |
51 | // install the non-protected allocator | ||
52 | lua_setallocf(L_, allocF, allocUD); | ||
53 | } | ||
54 | } | 55 | } |
55 | }; | 56 | }; |
56 | 57 | ||