From ce7006e3c063fc1383c23d2e0e36917b31d04e3f Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 24 Apr 2024 12:24:18 +0200 Subject: C++ migration: split UniqueKey into UniqueKey+RegistryUniqueKey --- src/cancel.h | 2 +- src/deep.cpp | 4 +-- src/keeper.cpp | 2 +- src/keeper.h | 2 +- src/lanes.cpp | 10 +++--- src/lanes_private.h | 2 +- src/tools.cpp | 8 ++--- src/tools.h | 8 ++--- src/uniquekey.h | 91 +++++++++++++++++++++++++++++++---------------------- src/universe.cpp | 4 +-- 10 files changed, 74 insertions(+), 59 deletions(-) diff --git a/src/cancel.h b/src/cancel.h index 8cff1c9..a25c078 100644 --- a/src/cancel.h +++ b/src/cancel.h @@ -45,7 +45,7 @@ enum class CancelOp }; // crc64/we of string "CANCEL_ERROR" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey CANCEL_ERROR{ 0xe97d41626cc97577ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel +static constexpr UniqueKey CANCEL_ERROR{ 0xE97D41626CC97577ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel [[nodiscard]] CancelOp which_cancel_op(char const* op_string_); [[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hook_count_, lua_Duration secs_, bool wake_lindas_); diff --git a/src/deep.cpp b/src/deep.cpp index 3326f98..3e1d90a 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -54,13 +54,13 @@ THE SOFTWARE. * factory -> metatable */ // crc64/we of string "DEEP_LOOKUP_KEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey DEEP_LOOKUP_KEY{ 0x9fb9b4f3f633d83dull }; +static constexpr RegistryUniqueKey DEEP_LOOKUP_KEY{ 0x9FB9B4F3F633D83Dull }; /* * The deep proxy cache is a weak valued table listing all deep UD proxies indexed by the deep UD that they are proxying * crc64/we of string "DEEP_PROXY_CACHE_KEY" generated at http://www.nitrxgen.net/hashgen/ */ -static constexpr UniqueKey DEEP_PROXY_CACHE_KEY{ 0x05773d6fc26be106ull }; +static constexpr RegistryUniqueKey DEEP_PROXY_CACHE_KEY{ 0x05773D6FC26BE106ull }; /* * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. diff --git a/src/keeper.cpp b/src/keeper.cpp index 5d94944..0875b87 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -182,7 +182,7 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo_, int count_) // in: linda_ud expected at stack slot idx // out: fifos[ud] // crc64/we of string "FIFOS_KEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey FIFOS_KEY{ 0xdce50bbc351cd465ull }; +static constexpr RegistryUniqueKey FIFOS_KEY{ 0xDCE50BBC351CD465ull }; static void push_table(lua_State* L, int idx_) { STACK_GROW(L, 5); diff --git a/src/keeper.h b/src/keeper.h index c1ee244..30de3a6 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -34,7 +34,7 @@ struct Keepers static constexpr uintptr_t KEEPER_MAGIC_SHIFT{ 3 }; // crc64/we of string "NIL_SENTINEL" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey NIL_SENTINEL{ 0x7eaafa003a1d11a1ull, "linda.null" }; +static constexpr UniqueKey NIL_SENTINEL{ 0x7EAAFA003A1D11A1ull, "linda.null" }; void init_keepers(Universe* U, lua_State* L); void close_keepers(Universe* U); diff --git a/src/lanes.cpp b/src/lanes.cpp index 3aa3365..6b3a9a1 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -219,7 +219,7 @@ static void securize_debug_threadname(lua_State* L, Lane* lane_) #if ERROR_FULL_STACK [[nodiscard]] static int lane_error(lua_State* L); // crc64/we of string "STACKTRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey STACKTRACE_REGKEY{ 0x534af7d3226a429full }; +static constexpr RegistryUniqueKey STACKTRACE_REGKEY{ 0x534AF7D3226A429Full }; #endif // ERROR_FULL_STACK /* @@ -231,7 +231,7 @@ static constexpr UniqueKey STACKTRACE_REGKEY{ 0x534af7d3226a429full }; * anyways complicate that approach. */ // crc64/we of string "FINALIZER_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey FINALIZER_REGKEY{ 0x188fccb8bf348e09ull }; +static constexpr RegistryUniqueKey FINALIZER_REGKEY{ 0x188FCCB8BF348E09ull }; // ################################################################################################# @@ -634,7 +634,7 @@ LUAG_FUNC( set_singlethreaded) #if ERROR_FULL_STACK // crc64/we of string "EXTENDED_STACKTRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey EXTENDED_STACKTRACE_REGKEY{ 0x2357c69a7c92c936ull }; // used as registry key +static constexpr RegistryUniqueKey EXTENDED_STACKTRACE_REGKEY{ 0x2357C69A7C92C936ull }; // used as registry key LUAG_FUNC( set_error_reporting) { @@ -731,7 +731,7 @@ LUAG_FUNC( set_error_reporting) LUAG_FUNC(set_debug_threadname) { // fnv164 of string "debug_threadname" generated at https://www.pelock.com/products/hash-calculator - constexpr UniqueKey hidden_regkey{ 0x79C0669AAAE04440ull }; + constexpr RegistryUniqueKey hidden_regkey{ 0x79C0669AAAE04440ull }; // C s_lane structure is a light userdata upvalue Lane* const lane{ lua_tolightuserdata(L, lua_upvalueindex(1)) }; luaL_checktype(L, -1, LUA_TSTRING); // "name" @@ -960,7 +960,7 @@ LUAG_FUNC(register) // ################################################################################################# // crc64/we of string "GCCB_KEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey GCCB_KEY{ 0xcfb1f046ef074e88ull }; +static constexpr UniqueKey GCCB_KEY{ 0xCFB1F046EF074E88ull }; //--- // lane_ud = lane_new( function diff --git a/src/lanes_private.h b/src/lanes_private.h index 5e6160d..807359f 100644 --- a/src/lanes_private.h +++ b/src/lanes_private.h @@ -92,7 +92,7 @@ class Lane }; // xxh64 of string "LANE_POINTER_REGKEY" generated at https://www.pelock.com/products/hash-calculator -static constexpr UniqueKey LANE_POINTER_REGKEY{ 0xB3022205633743BCull }; // used as registry key +static constexpr RegistryUniqueKey LANE_POINTER_REGKEY{ 0xB3022205633743BCull }; // used as registry key // To allow free-running threads (longer lifespan than the handle's) // 'Lane' are malloc/free'd and the handle only carries a pointer. diff --git a/src/tools.cpp b/src/tools.cpp index cd25eda..98cbf27 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -38,14 +38,14 @@ THE SOFTWARE. DEBUGSPEW_CODE(char const* const DebugSpewIndentScope::debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+"); // crc64/we of string "LOOKUPCACHE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey LOOKUPCACHE_REGKEY{ 0x837a68dfc6fcb716ull }; +static constexpr RegistryUniqueKey LOOKUPCACHE_REGKEY{ 0x837A68DFC6FCB716ull }; // ################################################################################################# /* * Does what the original 'push_registry_subtable' function did, but adds an optional mode argument to it */ -void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_) +void push_registry_subtable_mode(lua_State* L, RegistryUniqueKey key_, const char* mode_) { STACK_GROW(L, 3); STACK_CHECK_START_REL(L, 0); @@ -81,7 +81,7 @@ void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode * Push a registry subtable (keyed by unique 'key_') onto the stack. * If the subtable does not exist, it is created and chained. */ -void push_registry_subtable( lua_State* L, UniqueKey key_) +void push_registry_subtable(lua_State* L, RegistryUniqueKey key_) { push_registry_subtable_mode(L, key_, nullptr); } @@ -534,7 +534,7 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) /*---=== Inter-state copying ===---*/ // crc64/we of string "REG_MTID" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey REG_MTID{ 0x2e68f9b4751584dcull }; +static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; /* * Get a unique ID for metatable at [i]. diff --git a/src/tools.h b/src/tools.h index ddf5c67..673d6f0 100644 --- a/src/tools.h +++ b/src/tools.h @@ -8,8 +8,8 @@ class Universe; // ################################################################################################# -void push_registry_subtable_mode(lua_State* L, UniqueKey key_, const char* mode_); -void push_registry_subtable(lua_State* L, UniqueKey key_); +void push_registry_subtable_mode(lua_State* L, RegistryUniqueKey key_, const char* mode_); +void push_registry_subtable(lua_State* L, RegistryUniqueKey key_); enum class VT { @@ -88,7 +88,7 @@ void initialize_allocator_function(Universe* U, lua_State* L); // ################################################################################################# // crc64/we of string "CONFIG_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey CONFIG_REGKEY{ 0x31cd24894eae8624ull }; // registry key to access the configuration +static constexpr RegistryUniqueKey CONFIG_REGKEY{ 0x31CD24894EAE8624ull }; // registry key to access the configuration // crc64/we of string "LOOKUP_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey LOOKUP_REGKEY{ 0x5051ed67ee7b51a1ull }; // registry key to access the lookup database +static constexpr RegistryUniqueKey LOOKUP_REGKEY{ 0x5051ED67EE7B51A1ull }; // registry key to access the lookup database diff --git a/src/uniquekey.h b/src/uniquekey.h index 84553a5..738cb51 100644 --- a/src/uniquekey.h +++ b/src/uniquekey.h @@ -5,77 +5,92 @@ #include +// ################################################################################################# + class UniqueKey { - private: + protected: - uintptr_t m_storage; + uintptr_t const m_storage{ 0 }; public: char const* m_debugName{ nullptr }; + // --------------------------------------------------------------------------------------------- constexpr explicit UniqueKey(uint64_t val_, char const* debugName_ = nullptr) #if LUAJIT_FLAVOR() == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations - : m_storage{ static_cast(val_ & 0x7fffffffffffull) } + : m_storage{ static_cast(val_ & 0x7FFFFFFFFFFFull) } #else // LUAJIT_FLAVOR() : m_storage{ static_cast(val_) } #endif // LUAJIT_FLAVOR() , m_debugName{ debugName_ } { } + // --------------------------------------------------------------------------------------------- constexpr UniqueKey(UniqueKey const& rhs_) = default; - constexpr bool operator!=(UniqueKey const& rhs_) const + // --------------------------------------------------------------------------------------------- + constexpr std::strong_ordering operator<=>(UniqueKey const& rhs_) const = default; + // --------------------------------------------------------------------------------------------- + bool equals(lua_State* const L_, int i_) const { - return m_storage != rhs_.m_storage; + return lua_touserdata(L_, i_) == std::bit_cast(m_storage); } - constexpr bool operator==(UniqueKey const& rhs_) const + // --------------------------------------------------------------------------------------------- + void pushKey(lua_State* const L_) const { - return m_storage == rhs_.m_storage; + lua_pushlightuserdata(L_, std::bit_cast(m_storage)); } +}; - void pushKey(lua_State* const L) const - { - lua_pushlightuserdata(L, std::bit_cast(m_storage)); - } - bool equals(lua_State* const L, int i) const - { - return lua_touserdata(L, i) == std::bit_cast(m_storage); - } - void pushValue(lua_State* const L) const +// ################################################################################################# + +class RegistryUniqueKey : public UniqueKey +{ + public: + + using UniqueKey::UniqueKey; + + // --------------------------------------------------------------------------------------------- + void pushValue(lua_State* const L_) const { - STACK_CHECK_START_REL(L, 0); - pushKey(L); - lua_rawget(L, LUA_REGISTRYINDEX); - STACK_CHECK(L, 1); + STACK_CHECK_START_REL(L_, 0); + pushKey(L_); + lua_rawget(L_, LUA_REGISTRYINDEX); + STACK_CHECK(L_, 1); } + // --------------------------------------------------------------------------------------------- template - void setValue(lua_State* L, OP operation_) const + void setValue(lua_State* L_, OP operation_) const { // Note we can't check stack consistency because operation is not always a push (could be insert, replace, whatever) - pushKey(L); // ... key - operation_(L); // ... key value - lua_rawset(L, LUA_REGISTRYINDEX); // ... + pushKey(L_); // ... key + operation_(L_); // ... key value + lua_rawset(L_, LUA_REGISTRYINDEX); // ... } + // --------------------------------------------------------------------------------------------- template - T* readLightUserDataValue(lua_State* const L) const + T* readLightUserDataValue(lua_State* const L_) const { - STACK_GROW(L, 1); - STACK_CHECK_START_REL(L, 0); - pushValue(L); - T* const value{ lua_tolightuserdata(L, -1) }; // lightuserdata/nil - lua_pop(L, 1); - STACK_CHECK(L, 0); + STACK_GROW(L_, 1); + STACK_CHECK_START_REL(L_, 0); + pushValue(L_); + T* const value{ lua_tolightuserdata(L_, -1) }; // lightuserdata/nil + lua_pop(L_, 1); + STACK_CHECK(L_, 0); return value; } - bool readBoolValue(lua_State* const L) const + // --------------------------------------------------------------------------------------------- + bool readBoolValue(lua_State* const L_) const { - STACK_GROW(L, 1); - STACK_CHECK_START_REL(L, 0); - pushValue(L); - bool const value{ lua_toboolean(L, -1) ? true : false}; // bool/nil - lua_pop(L, 1); - STACK_CHECK(L, 0); + STACK_GROW(L_, 1); + STACK_CHECK_START_REL(L_, 0); + pushValue(L_); + bool const value{ lua_toboolean(L_, -1) ? true : false}; // bool/nil + lua_pop(L_, 1); + STACK_CHECK(L_, 0); return value; } }; + +// ################################################################################################# diff --git a/src/universe.cpp b/src/universe.cpp index a02a5e6..112c840 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -37,9 +37,9 @@ THE SOFTWARE. #include "uniquekey.h" // xxh64 of string "UNIVERSE_FULL_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey UNIVERSE_FULL_REGKEY{ 0x99CA130C09EDC074ull }; +static constexpr RegistryUniqueKey UNIVERSE_FULL_REGKEY{ 0x99CA130C09EDC074ull }; // xxh64 of string "UNIVERSE_LIGHT_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey UNIVERSE_LIGHT_REGKEY{ 0x3663C07C742CEB81ull }; +static constexpr RegistryUniqueKey UNIVERSE_LIGHT_REGKEY{ 0x3663C07C742CEB81ull }; // ################################################################################################# -- cgit v1.2.3-55-g6feb