diff options
Diffstat (limited to 'src/allocator.hpp')
-rw-r--r-- | src/allocator.hpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/allocator.hpp b/src/allocator.hpp new file mode 100644 index 0000000..fa48c46 --- /dev/null +++ b/src/allocator.hpp | |||
@@ -0,0 +1,55 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "compat.hpp" | ||
4 | |||
5 | // ################################################################################################# | ||
6 | |||
7 | namespace lanes { | ||
8 | |||
9 | // everything we need to provide to lua_newstate() | ||
10 | class AllocatorDefinition | ||
11 | { | ||
12 | public: | ||
13 | // xxh64 of string "kAllocatorVersion_1" generated at https://www.pelock.com/products/hash-calculator | ||
14 | static constexpr uintptr_t kAllocatorVersion{ static_cast<uintptr_t>(0xCF9D321B0DFB5715ull) }; | ||
15 | uintptr_t version{ kAllocatorVersion }; | ||
16 | lua_Alloc allocF{ nullptr }; | ||
17 | void* allocUD{ nullptr }; | ||
18 | |||
19 | [[nodiscard]] static void* operator new(size_t const size_) noexcept = delete; // can't create one outside of a Lua state | ||
20 | [[nodiscard]] static void* operator new(size_t const size_, lua_State* const L_) noexcept { return lua_newuserdatauv(L_, size_, UserValueCount{ 0 }); } | ||
21 | // always embedded somewhere else or "in-place constructed" as a full userdata | ||
22 | // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception | ||
23 | static void operator delete([[maybe_unused]] void* const p_, [[maybe_unused]] lua_State* const L_) {} | ||
24 | |||
25 | AllocatorDefinition(uintptr_t const version_, lua_Alloc const allocF_, void* const allocUD_) noexcept | ||
26 | : version{ version_ } | ||
27 | , allocF{ allocF_ } | ||
28 | , allocUD{ allocUD_ } | ||
29 | { | ||
30 | } | ||
31 | |||
32 | // rule of 5 | ||
33 | AllocatorDefinition() = default; | ||
34 | AllocatorDefinition(AllocatorDefinition const& rhs_) = default; | ||
35 | AllocatorDefinition(AllocatorDefinition&& rhs_) = default; | ||
36 | AllocatorDefinition& operator=(AllocatorDefinition const& rhs_) = default; | ||
37 | AllocatorDefinition& operator=(AllocatorDefinition&& rhs_) = default; | ||
38 | |||
39 | void initFrom(lua_State* const L_) | ||
40 | { | ||
41 | allocF = lua_getallocf(L_, &allocUD); | ||
42 | } | ||
43 | |||
44 | void* alloc(size_t const nsize_) | ||
45 | { | ||
46 | return allocF(allocUD, nullptr, 0, nsize_); | ||
47 | } | ||
48 | |||
49 | void free(void* const ptr_, size_t const osize_) | ||
50 | { | ||
51 | std::ignore = allocF(allocUD, ptr_, osize_, 0); | ||
52 | } | ||
53 | }; | ||
54 | |||
55 | } // namespace lanes | ||