aboutsummaryrefslogtreecommitdiff
path: root/src/universe.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/universe.h')
-rw-r--r--src/universe.h47
1 files changed, 1 insertions, 46 deletions
diff --git a/src/universe.h b/src/universe.h
index 3742ddd..2edd825 100644
--- a/src/universe.h
+++ b/src/universe.h
@@ -1,5 +1,6 @@
1#pragma once 1#pragma once
2 2
3#include "allocator.h"
3#include "keeper.h" 4#include "keeper.h"
4#include "lanesconf.h" 5#include "lanesconf.h"
5#include "tracker.h" 6#include "tracker.h"
@@ -14,52 +15,6 @@ class Lane;
14 15
15// ################################################################################################# 16// #################################################################################################
16 17
17// everything we need to provide to lua_newstate()
18class AllocatorDefinition
19{
20 public:
21 // xxh64 of string "kAllocatorVersion_1" generated at https://www.pelock.com/products/hash-calculator
22 static constexpr uintptr_t kAllocatorVersion{ static_cast<uintptr_t>(0xCF9D321B0DFB5715ull) };
23 uintptr_t version{ kAllocatorVersion };
24 lua_Alloc allocF{ nullptr };
25 void* allocUD{ nullptr };
26
27 [[nodiscard]] static void* operator new(size_t size_) noexcept = delete; // can't create one outside of a Lua state
28 [[nodiscard]] static void* operator new(size_t size_, lua_State* L_) noexcept { return lua_newuserdatauv(L_, size_, 0); }
29 // always embedded somewhere else or "in-place constructed" as a full userdata
30 // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception
31 static void operator delete([[maybe_unused]] void* p_, [[maybe_unused]] lua_State* L_) { LUA_ASSERT(L_, !"should never be called"); }
32
33 AllocatorDefinition(uintptr_t const version_, lua_Alloc const allocF_, void* const allocUD_) noexcept
34 : version{ version_ }
35 , allocF{ allocF_ }
36 , allocUD{ allocUD_ }
37 {
38 }
39 AllocatorDefinition() = default;
40 AllocatorDefinition(AllocatorDefinition const& rhs_) = default;
41 AllocatorDefinition(AllocatorDefinition&& rhs_) = default;
42 AllocatorDefinition& operator=(AllocatorDefinition const& rhs_) = default;
43 AllocatorDefinition& operator=(AllocatorDefinition&& rhs_) = default;
44
45 void initFrom(lua_State* L_)
46 {
47 allocF = lua_getallocf(L_, &allocUD);
48 }
49
50 void* alloc(size_t nsize_)
51 {
52 return allocF(allocUD, nullptr, 0, nsize_);
53 }
54
55 void free(void* ptr_, size_t osize_)
56 {
57 std::ignore = allocF(allocUD, ptr_, osize_, 0);
58 }
59};
60
61// #################################################################################################
62
63// mutex-protected allocator for use with Lua states that share a non-threadsafe allocator 18// mutex-protected allocator for use with Lua states that share a non-threadsafe allocator
64class ProtectedAllocator 19class ProtectedAllocator
65: public AllocatorDefinition 20: public AllocatorDefinition