aboutsummaryrefslogtreecommitdiff
path: root/src/allocator.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-10-28 17:57:31 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2024-10-28 17:57:31 +0100
commit76da2875eb2af9f8191c2adf4f9663ac3a2cec89 (patch)
tree112bd51318e401bf8745123919a635e765365318 /src/allocator.h
parentcb6e1c2f3b93d5de02de4a8c8f13891e33674a36 (diff)
downloadlanes-76da2875eb2af9f8191c2adf4f9663ac3a2cec89.tar.gz
lanes-76da2875eb2af9f8191c2adf4f9663ac3a2cec89.tar.bz2
lanes-76da2875eb2af9f8191c2adf4f9663ac3a2cec89.zip
Renamed allocator.h → allocator.hpp, cancel.h → cancel.hpp, keeper.h → keeper.hpp, tools.h → tools.hpp
Diffstat (limited to 'src/allocator.h')
-rw-r--r--src/allocator.h55
1 files changed, 0 insertions, 55 deletions
diff --git a/src/allocator.h b/src/allocator.h
deleted file mode 100644
index fa48c46..0000000
--- a/src/allocator.h
+++ /dev/null
@@ -1,55 +0,0 @@
1#pragma once
2
3#include "compat.hpp"
4
5// #################################################################################################
6
7namespace 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