aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-18 10:06:53 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-18 10:06:53 +0200
commit3229fa753cef10eda963247eaedec1d6ed9de9e4 (patch)
tree6b8ab7c1895aee63e4db3cca0b38c9a812430a2f /src
parente3818609b54c81a83aa73beb322ea9c282b7add8 (diff)
downloadlanes-3229fa753cef10eda963247eaedec1d6ed9de9e4.tar.gz
lanes-3229fa753cef10eda963247eaedec1d6ed9de9e4.tar.bz2
lanes-3229fa753cef10eda963247eaedec1d6ed9de9e4.zip
Moved AllocatorDefinition in a lanes namespace
Diffstat (limited to 'src')
-rw-r--r--src/allocator.h84
-rw-r--r--src/lanesconf.h1
-rw-r--r--src/state.cpp4
-rw-r--r--src/universe.cpp4
-rw-r--r--src/universe.h4
5 files changed, 51 insertions, 46 deletions
diff --git a/src/allocator.h b/src/allocator.h
index 9a9c245..e09ab38 100644
--- a/src/allocator.h
+++ b/src/allocator.h
@@ -2,46 +2,50 @@
2 2
3// ################################################################################################# 3// #################################################################################################
4 4
5// everything we need to provide to lua_newstate() 5namespace lanes {
6class AllocatorDefinition
7{
8 public:
9 // xxh64 of string "kAllocatorVersion_1" generated at https://www.pelock.com/products/hash-calculator
10 static constexpr uintptr_t kAllocatorVersion{ static_cast<uintptr_t>(0xCF9D321B0DFB5715ull) };
11 uintptr_t version{ kAllocatorVersion };
12 lua_Alloc allocF{ nullptr };
13 void* allocUD{ nullptr };
14
15 [[nodiscard]] static void* operator new(size_t size_) noexcept = delete; // can't create one outside of a Lua state
16 [[nodiscard]] static void* operator new(size_t size_, lua_State* L_) noexcept { return lua_newuserdatauv(L_, size_, 0); }
17 // always embedded somewhere else or "in-place constructed" as a full userdata
18 // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception
19 static void operator delete([[maybe_unused]] void* p_, [[maybe_unused]] lua_State* L_) {}
20
21 AllocatorDefinition(uintptr_t const version_, lua_Alloc const allocF_, void* const allocUD_) noexcept
22 : version{ version_ }
23 , allocF{ allocF_ }
24 , allocUD{ allocUD_ }
25 {
26 }
27 AllocatorDefinition() = default;
28 AllocatorDefinition(AllocatorDefinition const& rhs_) = default;
29 AllocatorDefinition(AllocatorDefinition&& rhs_) = default;
30 AllocatorDefinition& operator=(AllocatorDefinition const& rhs_) = default;
31 AllocatorDefinition& operator=(AllocatorDefinition&& rhs_) = default;
32
33 void initFrom(lua_State* L_)
34 {
35 allocF = lua_getallocf(L_, &allocUD);
36 }
37 6
38 void* alloc(size_t nsize_) 7 // everything we need to provide to lua_newstate()
8 class AllocatorDefinition
39 { 9 {
40 return allocF(allocUD, nullptr, 0, nsize_); 10 public:
41 } 11 // xxh64 of string "kAllocatorVersion_1" generated at https://www.pelock.com/products/hash-calculator
12 static constexpr uintptr_t kAllocatorVersion{ static_cast<uintptr_t>(0xCF9D321B0DFB5715ull) };
13 uintptr_t version{ kAllocatorVersion };
14 lua_Alloc allocF{ nullptr };
15 void* allocUD{ nullptr };
42 16
43 void free(void* ptr_, size_t osize_) 17 [[nodiscard]] static void* operator new(size_t size_) noexcept = delete; // can't create one outside of a Lua state
44 { 18 [[nodiscard]] static void* operator new(size_t size_, lua_State* L_) noexcept { return lua_newuserdatauv(L_, size_, 0); }
45 std::ignore = allocF(allocUD, ptr_, osize_, 0); 19 // always embedded somewhere else or "in-place constructed" as a full userdata
46 } 20 // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception
47}; 21 static void operator delete([[maybe_unused]] void* p_, [[maybe_unused]] lua_State* L_) {}
22
23 AllocatorDefinition(uintptr_t const version_, lua_Alloc const allocF_, void* const allocUD_) noexcept
24 : version{ version_ }
25 , allocF{ allocF_ }
26 , allocUD{ allocUD_ }
27 {
28 }
29 AllocatorDefinition() = default;
30 AllocatorDefinition(AllocatorDefinition const& rhs_) = default;
31 AllocatorDefinition(AllocatorDefinition&& rhs_) = default;
32 AllocatorDefinition& operator=(AllocatorDefinition const& rhs_) = default;
33 AllocatorDefinition& operator=(AllocatorDefinition&& rhs_) = default;
34
35 void initFrom(lua_State* L_)
36 {
37 allocF = lua_getallocf(L_, &allocUD);
38 }
39
40 void* alloc(size_t nsize_)
41 {
42 return allocF(allocUD, nullptr, 0, nsize_);
43 }
44
45 void free(void* ptr_, size_t osize_)
46 {
47 std::ignore = allocF(allocUD, ptr_, osize_, 0);
48 }
49 };
50
51} // namespace lanes
diff --git a/src/lanesconf.h b/src/lanesconf.h
index f20dfff..0afd493 100644
--- a/src/lanesconf.h
+++ b/src/lanesconf.h
@@ -26,6 +26,7 @@
26// static function variable: prefix s, followed by an uppercase letter 26// static function variable: prefix s, followed by an uppercase letter
27// function local variable: prefix _, followed by an uppercase letter 27// function local variable: prefix _, followed by an uppercase letter
28// named lambda capture: no prefix, start with a lowercase letter 28// named lambda capture: no prefix, start with a lowercase letter
29// stuff for external consumption in a 'lanes' namespace
29 30
30#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 31#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
31#ifdef __cplusplus 32#ifdef __cplusplus
diff --git a/src/state.cpp b/src/state.cpp
index bae2ba5..6d3910b 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -209,8 +209,8 @@ namespace state {
209 lua_pushcclosure(from, U->provideAllocator, 0); // L: provideAllocator() 209 lua_pushcclosure(from, U->provideAllocator, 0); // L: provideAllocator()
210 luaG_pushstring(from, hint); // L: provideAllocator() "<hint>" 210 luaG_pushstring(from, hint); // L: provideAllocator() "<hint>"
211 lua_call(from, 1, 1); // L: result 211 lua_call(from, 1, 1); // L: result
212 AllocatorDefinition* const _def{ luaG_tofulluserdata<AllocatorDefinition>(from, -1) }; 212 lanes::AllocatorDefinition* const _def{ luaG_tofulluserdata<lanes::AllocatorDefinition>(from, -1) };
213 if (!_def || _def->version != AllocatorDefinition::kAllocatorVersion) { 213 if (!_def || _def->version != lanes::AllocatorDefinition::kAllocatorVersion) {
214 raise_luaL_error(from, "Bad config.allocator function, must provide a valid AllocatorDefinition"); 214 raise_luaL_error(from, "Bad config.allocator function, must provide a valid AllocatorDefinition");
215 } 215 }
216 lua_State* const _L{ lua_newstate(_def->allocF, _def->allocUD) }; 216 lua_State* const _L{ lua_newstate(_def->allocF, _def->allocUD) };
diff --git a/src/universe.cpp b/src/universe.cpp
index d24a784..770fccf 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -166,7 +166,7 @@ Universe::Universe()
166{ 166{
167 Universe* const _U{ Universe::Get(L_) }; 167 Universe* const _U{ Universe::Get(L_) };
168 // push a new full userdata on the stack, giving access to the universe's protected allocator 168 // push a new full userdata on the stack, giving access to the universe's protected allocator
169 [[maybe_unused]] AllocatorDefinition* const _def{ new (L_) AllocatorDefinition{ _U->protectedAllocator.makeDefinition() } }; 169 [[maybe_unused]] lanes::AllocatorDefinition* const _def{ new (L_) lanes::AllocatorDefinition{ _U->protectedAllocator.makeDefinition() } };
170 return 1; 170 return 1;
171} 171}
172 172
@@ -218,7 +218,7 @@ void Universe::initializeAllocatorFunction(lua_State* const L_)
218 std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" 218 std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator"
219 std::string_view const _allocator{ luaG_tostring(L_, -1) }; 219 std::string_view const _allocator{ luaG_tostring(L_, -1) };
220 if (_allocator == "libc") { 220 if (_allocator == "libc") {
221 internalAllocator = AllocatorDefinition{ AllocatorDefinition::kAllocatorVersion, libc_lua_Alloc, nullptr }; 221 internalAllocator = lanes::AllocatorDefinition{ lanes::AllocatorDefinition::kAllocatorVersion, libc_lua_Alloc, nullptr };
222 } else if (provideAllocator == luaG_provide_protected_allocator) { 222 } else if (provideAllocator == luaG_provide_protected_allocator) {
223 // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. 223 // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case.
224 internalAllocator = protectedAllocator.makeDefinition(); 224 internalAllocator = protectedAllocator.makeDefinition();
diff --git a/src/universe.h b/src/universe.h
index 2edd825..d890e5c 100644
--- a/src/universe.h
+++ b/src/universe.h
@@ -17,7 +17,7 @@ class Lane;
17 17
18// 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
19class ProtectedAllocator 19class ProtectedAllocator
20: public AllocatorDefinition 20: public lanes::AllocatorDefinition
21{ 21{
22 private: 22 private:
23 std::mutex mutex; 23 std::mutex mutex;
@@ -90,7 +90,7 @@ class Universe
90 // contains a mutex and the original allocator definition 90 // contains a mutex and the original allocator definition
91 ProtectedAllocator protectedAllocator; 91 ProtectedAllocator protectedAllocator;
92 92
93 AllocatorDefinition internalAllocator; 93 lanes::AllocatorDefinition internalAllocator;
94 94
95 Keepers keepers; 95 Keepers keepers;
96 96