From 304e4dfabe4555dff4aa72e75b677405fd30d1b3 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 20 Nov 2024 17:51:49 +0100 Subject: Some [[nodiscard]] boyscouting --- src/allocator.hpp | 10 +++++++-- src/cancel.hpp | 10 +++++---- src/compat.hpp | 26 +++++++++++++++------- src/deep.hpp | 15 ++++++++----- src/intercopycontext.hpp | 57 ++++++++++++++++++++++++++++++++---------------- src/keeper.hpp | 44 ++++++++++++++++++++++++------------- src/lane.hpp | 39 ++++++++++++++++++++++----------- src/linda.hpp | 26 ++++++++++++++-------- src/lindafactory.hpp | 6 +++-- src/state.hpp | 7 +++--- src/threading_osx.h | 9 +++++--- src/tools.hpp | 8 +++++-- src/tracker.hpp | 9 +++++--- src/unique.hpp | 6 +++-- src/uniquekey.hpp | 9 +++++--- src/universe.hpp | 19 +++++++++++----- 16 files changed, 201 insertions(+), 99 deletions(-) (limited to 'src') diff --git a/src/allocator.hpp b/src/allocator.hpp index 4fec044..16db1e6 100644 --- a/src/allocator.hpp +++ b/src/allocator.hpp @@ -25,8 +25,10 @@ namespace lanes { public: - [[nodiscard]] static void* operator new(size_t const size_) noexcept = delete; // can't create one outside of a Lua state - [[nodiscard]] static void* operator new(size_t const size_, lua_State* const L_) noexcept { return lua_newuserdatauv(L_, size_, UserValueCount{ 0 }); } + [[nodiscard]] + static void* operator new(size_t const size_) noexcept = delete; // can't create one outside of a Lua state + [[nodiscard]] + static void* operator new(size_t const size_, lua_State* const L_) noexcept { return lua_newuserdatauv(L_, size_, UserValueCount{ 0 }); } // always embedded somewhere else or "in-place constructed" as a full userdata // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception static void operator delete([[maybe_unused]] void* const p_, [[maybe_unused]] lua_State* const L_) {} @@ -44,6 +46,7 @@ namespace lanes { AllocatorDefinition& operator=(AllocatorDefinition const& rhs_) = default; AllocatorDefinition& operator=(AllocatorDefinition&& rhs_) = default; + [[nodiscard]] static AllocatorDefinition& Validated(lua_State* L_, StackIndex idx_); void initFrom(lua_State* const L_) @@ -58,16 +61,19 @@ namespace lanes { } } + [[nodiscard]] lua_State* newState() const { return lua_newstate(allocF, allocUD); } + [[nodiscard]] void* alloc(size_t const nsize_) { return allocF(allocUD, nullptr, 0, nsize_); } + [[nodiscard]] void* alloc(void* const ptr_, size_t const osize_, size_t const nsize_) { return allocF(allocUD, ptr_, osize_, nsize_); diff --git a/src/cancel.hpp b/src/cancel.hpp index 7230169..8f4cf07 100644 --- a/src/cancel.hpp +++ b/src/cancel.hpp @@ -9,7 +9,6 @@ enum class CancelRequest { None, // no pending cancel request - // TODO: add a Wake mode: user wants to wake the waiting lindas (in effect resulting in a timeout before the initial operation duration) Soft, // user wants the lane to cancel itself manually on cancel_test() Hard // user wants the lane to be interrupted (meaning code won't return from those functions) from inside linda:send/receive calls }; @@ -42,12 +41,15 @@ static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_er // ################################################################################################# -[[nodiscard]] CancelRequest CheckCancelRequest(lua_State* L_); -[[nodiscard]] CancelOp WhichCancelOp(std::string_view const& opString_); +[[nodiscard]] +CancelRequest CheckCancelRequest(lua_State* L_); +[[nodiscard]] +CancelOp WhichCancelOp(std::string_view const& opString_); // ################################################################################################# -[[noreturn]] static inline void raise_cancel_error(lua_State* const L_) +[[noreturn]] +static inline void raise_cancel_error(lua_State* const L_) { STACK_GROW(L_, 1); kCancelError.pushKey(L_); // special error value diff --git a/src/compat.hpp b/src/compat.hpp index 59c8001..f66f703 100644 --- a/src/compat.hpp +++ b/src/compat.hpp @@ -115,6 +115,7 @@ enum class LuaError ERRFILE = LUA_ERRFILE }; +[[nodiscard]] inline constexpr LuaError ToLuaError(int const rc_) { assert(rc_ == LUA_OK || rc_ == LUA_YIELD || rc_ == LUA_ERRRUN || rc_ == LUA_ERRSYNTAX || rc_ == LUA_ERRMEM || rc_ == LUA_ERRGCMM || rc_ == LUA_ERRERR || rc_ == LUA_ERRFILE); @@ -124,6 +125,7 @@ inline constexpr LuaError ToLuaError(int const rc_) // ################################################################################################# // break lexical order for that one because it's needed below +[[nodiscard]] inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_) { return static_cast(lua_type(L_, idx_)); @@ -139,21 +141,24 @@ inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_) #define STRINGVIEW_FMT "%.*s" // a replacement of lua_tolstring -[[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_) +[[nodiscard]] +inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_) { size_t _len{ 0 }; char const* _str{ lua_tolstring(L_, idx_, &_len) }; return _str ? std::string_view{ _str, _len } : ""; } -[[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_) +[[nodiscard]] +inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_) { size_t _len{ 0 }; char const* _str{ luaL_checklstring(L_, idx_, &_len) }; return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_) +[[nodiscard]] +inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_) { if (lua_isnoneornil(L_, idx_)) { return default_; @@ -352,7 +357,8 @@ static inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) // ################################################################################################# template -[[nodiscard]] T* luaG_newuserdatauv(lua_State* L_, UserValueCount nuvalue_) +[[nodiscard]] +T* luaG_newuserdatauv(lua_State* const L_, UserValueCount const nuvalue_) { return static_cast(lua_newuserdatauv(L_, sizeof(T), nuvalue_)); } @@ -394,7 +400,8 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname // a small helper to extract a full userdata pointer from the stack in a safe way template -[[nodiscard]] T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_) +[[nodiscard]] +T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_) { LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); return static_cast(lua_touserdata(L_, index_)); @@ -403,7 +410,8 @@ template // ------------------------------------------------------------------------------------------------- template -[[nodiscard]] auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_) +[[nodiscard]] +auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_) { LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); if constexpr (std::is_pointer_v) { @@ -415,14 +423,16 @@ template // ------------------------------------------------------------------------------------------------- -[[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_) +[[nodiscard]] +inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_) { return lua_typename(L_, static_cast(t_)); } // ------------------------------------------------------------------------------------------------- -[[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, StackIndex const idx_) +[[nodiscard]] +inline std::string_view luaG_typename(lua_State* const L_, StackIndex const idx_) { return luaG_typename(L_, luaG_type(L_, idx_)); } diff --git a/src/deep.hpp b/src/deep.hpp index 3956f71..6fa12b1 100644 --- a/src/deep.hpp +++ b/src/deep.hpp @@ -57,8 +57,10 @@ class DeepFactory // NVI: private overrides virtual void createMetatable(lua_State* L_) const = 0; virtual void deleteDeepObjectInternal(lua_State* L_, DeepPrelude* o_) const = 0; - [[nodiscard]] virtual DeepPrelude* newDeepObjectInternal(lua_State* L_) const = 0; - [[nodiscard]] virtual std::string_view moduleName() const = 0; + [[nodiscard]] + virtual DeepPrelude* newDeepObjectInternal(lua_State* L_) const = 0; + [[nodiscard]] + virtual std::string_view moduleName() const = 0; private: void storeDeepLookup(lua_State* L_) const; @@ -66,11 +68,14 @@ class DeepFactory public: // NVI: public interface static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); - [[nodiscard]] static bool IsDeepUserdata(lua_State* const L_, StackIndex const idx_); - [[nodiscard]] static DeepFactory* LookupFactory(lua_State* L_, StackIndex index_, LookupMode mode_); + [[nodiscard]] + static bool IsDeepUserdata(lua_State* const L_, StackIndex const idx_); + [[nodiscard]] + static DeepFactory* LookupFactory(lua_State* L_, StackIndex index_, LookupMode mode_); static void PushDeepProxy(DestState L_, DeepPrelude* o_, UserValueCount nuv_, LookupMode mode_, lua_State* errL_); void pushDeepUserdata(DestState L_, UserValueCount nuv_) const; - [[nodiscard]] DeepPrelude* toDeep(lua_State* L_, StackIndex index_) const; + [[nodiscard]] + DeepPrelude* toDeep(lua_State* L_, StackIndex index_) const; }; // ################################################################################################# diff --git a/src/intercopycontext.hpp b/src/intercopycontext.hpp index 2b247d2..84d9e70 100644 --- a/src/intercopycontext.hpp +++ b/src/intercopycontext.hpp @@ -38,11 +38,13 @@ class InterCopyContext std::string_view name; // that one can change when we reuse the context private: - [[nodiscard]] std::string_view findLookupName() const; + [[nodiscard]] + std::string_view findLookupName() const; // when mode == LookupMode::FromKeeper, L1 is a keeper state and L2 is not, therefore L2 is the state where we want to raise the error // whon mode != LookupMode::FromKeeper, L1 is not a keeper state, therefore L1 is the state where we want to raise the error lua_State* getErrL() const { return (mode == LookupMode::FromKeeper) ? L2.value() : L1.value(); } - [[nodiscard]] LuaType processConversion() const; + [[nodiscard]] + LuaType processConversion() const; // for use in copyCachedFunction void copyFunction() const; @@ -50,30 +52,47 @@ class InterCopyContext // for use in inter_copy_function void copyCachedFunction() const; - [[nodiscard]] bool lookupTable() const; + [[nodiscard]] + bool lookupTable() const; // for use in inter_copy_table void interCopyKeyValuePair() const; - [[nodiscard]] bool pushCachedMetatable() const; - [[nodiscard]] bool pushCachedTable() const; + [[nodiscard]] + bool pushCachedMetatable() const; + [[nodiscard]] + bool pushCachedTable() const; // for use in inter_copy_userdata - [[nodiscard]] bool tryCopyClonable() const; - [[nodiscard]] bool tryCopyDeep() const; + [[nodiscard]] + bool tryCopyClonable() const; + [[nodiscard]] + bool tryCopyDeep() const; // copying a single Lua stack item - [[nodiscard]] bool interCopyBoolean() const; - [[nodiscard]] bool interCopyFunction() const; - [[nodiscard]] bool interCopyLightuserdata() const; - [[nodiscard]] bool interCopyNil() const; - [[nodiscard]] bool interCopyNumber() const; - [[nodiscard]] bool interCopyString() const; - [[nodiscard]] bool interCopyTable() const; - [[nodiscard]] bool interCopyUserdata() const; + [[nodiscard]] + bool interCopyBoolean() const; + [[nodiscard]] + bool interCopyFunction() const; + [[nodiscard]] + bool interCopyLightuserdata() const; + [[nodiscard]] + bool interCopyNil() const; + [[nodiscard]] + bool interCopyNumber() const; + [[nodiscard]] + bool interCopyString() const; + [[nodiscard]] + bool interCopyTable() const; + [[nodiscard]] + bool interCopyUserdata() const; public: - [[nodiscard]] InterCopyResult interCopy(int n_) const; - [[nodiscard]] InterCopyResult interCopyOne() const; - [[nodiscard]] InterCopyResult interCopyPackage() const; - [[nodiscard]] InterCopyResult interMove(int n_) const; + [[nodiscard]] + InterCopyResult interCopy(int n_) const; + [[nodiscard]] + InterCopyResult interCopyOne() const; + [[nodiscard]] + InterCopyResult interCopyPackage() const; + [[nodiscard]] + InterCopyResult interMove(int n_) const; }; diff --git a/src/keeper.hpp b/src/keeper.hpp index 2354c2e..b77f1a9 100644 --- a/src/keeper.hpp +++ b/src/keeper.hpp @@ -18,7 +18,8 @@ struct Keeper std::mutex mutex; KeeperState K{ static_cast(nullptr) }; - [[nodiscard]] static void* operator new[](size_t size_, Universe* U_) noexcept; + [[nodiscard]] + static void* operator new[](size_t size_, Universe* U_) noexcept; // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception static void operator delete[](void* p_, Universe* U_); @@ -31,7 +32,8 @@ struct Keeper Keeper& operator=(Keeper const&) = delete; Keeper& operator=(Keeper const&&) = delete; - [[nodiscard]] static int PushLindaStorage(Linda& linda_, DestState L_); + [[nodiscard]] + static int PushLindaStorage(Linda& linda_, DestState L_); }; // ################################################################################################# @@ -63,13 +65,17 @@ struct Keepers Keepers() = default; void close(); - [[nodiscard]] Keeper* getKeeper(KeeperIndex idx_); - [[nodiscard]] int getNbKeepers() const; + [[nodiscard]] + Keeper* getKeeper(KeeperIndex idx_); + [[nodiscard]] + int getNbKeepers() const; void initialize(Universe& U_, lua_State* L_, size_t nbKeepers_, int gc_threshold_); }; // ################################################################################################# +DECLARE_UNIQUE_TYPE(KeeperCallResult, std::optional); + // xxh64 of string "kNilSentinel" generated at https://www.pelock.com/products/hash-calculator static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; @@ -77,14 +83,22 @@ using keeper_api_t = lua_CFunction; #define KEEPER_API(_op) keepercall_##_op // lua_Cfunctions to run inside a keeper state -[[nodiscard]] int keepercall_count(lua_State* L_); -[[nodiscard]] int keepercall_destruct(lua_State* L_); -[[nodiscard]] int keepercall_get(lua_State* L_); -[[nodiscard]] int keepercall_limit(lua_State* L_); -[[nodiscard]] int keepercall_receive(lua_State* L_); -[[nodiscard]] int keepercall_receive_batched(lua_State* L_); -[[nodiscard]] int keepercall_send(lua_State* L_); -[[nodiscard]] int keepercall_set(lua_State* L_); - -DECLARE_UNIQUE_TYPE(KeeperCallResult, std::optional); -[[nodiscard]] KeeperCallResult keeper_call(KeeperState K_, keeper_api_t func_, lua_State* L_, Linda* linda_, StackIndex starting_index_); +[[nodiscard]] +int keepercall_count(lua_State* L_); +[[nodiscard]] +int keepercall_destruct(lua_State* L_); +[[nodiscard]] +int keepercall_get(lua_State* L_); +[[nodiscard]] +int keepercall_limit(lua_State* L_); +[[nodiscard]] +int keepercall_receive(lua_State* L_); +[[nodiscard]] +int keepercall_receive_batched(lua_State* L_); +[[nodiscard]] +int keepercall_send(lua_State* L_); +[[nodiscard]] +int keepercall_set(lua_State* L_); + +[[nodiscard]] +KeeperCallResult keeper_call(KeeperState K_, keeper_api_t func_, lua_State* L_, Linda* linda_, StackIndex starting_index_); diff --git a/src/lane.hpp b/src/lane.hpp index b5be9ab..29bf213 100644 --- a/src/lane.hpp +++ b/src/lane.hpp @@ -139,7 +139,8 @@ class Lane ErrorTraceLevel const errorTraceLevel{ Basic }; - [[nodiscard]] static void* operator new(size_t size_, Universe* U_) noexcept { return U_->internalAllocator.alloc(size_); } + [[nodiscard]] + static void* operator new(size_t size_, Universe* U_) noexcept { return U_->internalAllocator.alloc(size_); } // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception static void operator delete(void* p_, Universe* U_) { U_->internalAllocator.free(p_, sizeof(Lane)); } // this one is for us, to make sure memory is freed by the correct allocator @@ -156,7 +157,8 @@ class Lane private: - [[nodiscard]] CancelResult internalCancel(CancelRequest rq_, std::chrono::time_point until_, WakeLane wakeLane_); + [[nodiscard]] + CancelResult internalCancel(CancelRequest rq_, std::chrono::time_point until_, WakeLane wakeLane_); public: @@ -170,33 +172,43 @@ class Lane nresults = 0; lua_close(_L); // this collects our coroutine thread at the same time } - [[nodiscard]] std::string_view errorTraceLevelString() const; - [[nodiscard]] int errorHandlerCount() const noexcept + [[nodiscard]] + std::string_view errorTraceLevelString() const; + [[nodiscard]] + int errorHandlerCount() const noexcept { // don't push a error handler when in coroutine mode, as the first lua_resume wants only the function and its arguments on the stack return ((errorTraceLevel == Lane::Minimal) || isCoroutine()) ? 0 : 1; } - [[nodiscard]] bool isCoroutine() const noexcept { return S != L; } - [[nodiscard]] std::string_view getDebugName() const + [[nodiscard]] + bool isCoroutine() const noexcept { return S != L; } + [[nodiscard]] + std::string_view getDebugName() const { std::lock_guard _guard{ debugNameMutex }; return debugName; } static int LuaErrorHandler(lua_State* L_); - [[nodiscard]] int pushErrorHandler() const noexcept { return (errorHandlerCount() == 0) ? 0 : (lua_pushcfunction(L, LuaErrorHandler), 1); } - [[nodiscard]] std::string_view pushErrorTraceLevel(lua_State* L_) const; + [[nodiscard]] + int pushErrorHandler() const noexcept { return (errorHandlerCount() == 0) ? 0 : (lua_pushcfunction(L, LuaErrorHandler), 1); } + [[nodiscard]] + std::string_view pushErrorTraceLevel(lua_State* L_) const; static void PushMetatable(lua_State* L_); void pushStatusString(lua_State* L_) const; void pushIndexedResult(lua_State* L_, int key_) const; void resetResultsStorage(lua_State* L_, StackIndex self_idx_); void selfdestructAdd(); - [[nodiscard]] bool selfdestructRemove(); + [[nodiscard]] + bool selfdestructRemove(); void securizeDebugName(lua_State* L_); void startThread(int priority_); - [[nodiscard]] int storeResults(lua_State* L_); - [[nodiscard]] std::string_view threadStatusString() const; + [[nodiscard]] + int storeResults(lua_State* L_); + [[nodiscard]] + std::string_view threadStatusString() const; // wait until the lane stops working with its state (either Suspended or Done+) - [[nodiscard]] bool waitForCompletion(std::chrono::time_point until_); + [[nodiscard]] + bool waitForCompletion(std::chrono::time_point until_); }; // ################################################################################################# @@ -205,7 +217,8 @@ class Lane // 'Lane' are malloc/free'd and the handle only carries a pointer. // This is not deep userdata since the handle is not portable among lanes. // -[[nodiscard]] inline Lane* ToLane(lua_State* const L_, StackIndex const i_) +[[nodiscard]] +static inline Lane* ToLane(lua_State* const L_, StackIndex const i_) { return *(static_cast(luaL_checkudata(L_, i_, kLaneMetatableName.data()))); } diff --git a/src/linda.hpp b/src/linda.hpp index 5b5f683..aa63316 100644 --- a/src/linda.hpp +++ b/src/linda.hpp @@ -19,7 +19,7 @@ class Linda : public DeepPrelude // Deep userdata MUST start with this header { public: - class KeeperOperationInProgress + class [[nodiscard]] KeeperOperationInProgress { private: Linda& linda; @@ -64,7 +64,8 @@ class Linda Status cancelStatus{ Status::Active }; public: - [[nodiscard]] static void* operator new(size_t size_, Universe* U_) noexcept { return U_->internalAllocator.alloc(size_); } + [[nodiscard]] + static void* operator new(size_t size_, Universe* U_) noexcept { return U_->internalAllocator.alloc(size_); } // always embedded somewhere else or "in-place constructed" as a full userdata // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception static void operator delete(void* p_, Universe* U_) { U_->internalAllocator.free(p_, sizeof(Linda)); } @@ -85,19 +86,26 @@ class Linda void setName(std::string_view const& name_); public: - [[nodiscard]] Keeper* acquireKeeper() const; - [[nodiscard]] std::string_view getName() const; - [[nodiscard]] bool inKeeperOperation() const { return keeperOperationCount.load(std::memory_order_seq_cst) != 0; } + [[nodiscard]] + Keeper* acquireKeeper() const; + [[nodiscard]] + std::string_view getName() const; + [[nodiscard]] + bool inKeeperOperation() const { return keeperOperationCount.load(std::memory_order_seq_cst) != 0; } template - [[nodiscard]] T obfuscated() const + [[nodiscard]] + T obfuscated() const { // xxh64 of string "kObfuscator" generated at https://www.pelock.com/products/hash-calculator static constexpr UniqueKey kObfuscator{ 0x7B8AA1F99A3BD782ull }; return std::bit_cast(std::bit_cast(this) ^ kObfuscator.storage); }; void releaseKeeper(Keeper* keeper_) const; - [[nodiscard]] static int ProtectedCall(lua_State* L_, lua_CFunction f_); + [[nodiscard]] + static int ProtectedCall(lua_State* L_, lua_CFunction f_); void pushCancelString(lua_State* L_) const; - [[nodiscard]] KeeperOperationInProgress startKeeperOperation(lua_State* const L_) { return KeeperOperationInProgress{ *this, L_ }; }; - [[nodiscard]] Keeper* whichKeeper() const { return U->keepers.getKeeper(keeperIndex); } + [[nodiscard]] + KeeperOperationInProgress startKeeperOperation(lua_State* const L_) { return KeeperOperationInProgress{ *this, L_ }; }; + [[nodiscard]] + Keeper* whichKeeper() const { return U->keepers.getKeeper(keeperIndex); } }; diff --git a/src/lindafactory.hpp b/src/lindafactory.hpp index ac42c10..1512e9a 100644 --- a/src/lindafactory.hpp +++ b/src/lindafactory.hpp @@ -21,6 +21,8 @@ class LindaFactory void createMetatable(lua_State* L_) const override; void deleteDeepObjectInternal(lua_State* L_, DeepPrelude* o_) const override; - [[nodiscard]] std::string_view moduleName() const override; - [[nodiscard]] DeepPrelude* newDeepObjectInternal(lua_State* L_) const override; + [[nodiscard]] + std::string_view moduleName() const override; + [[nodiscard]] + DeepPrelude* newDeepObjectInternal(lua_State* L_) const override; }; diff --git a/src/state.hpp b/src/state.hpp index 7401fe9..1e1e1f5 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -4,11 +4,12 @@ #include "macros_and_utils.hpp" // forwards -enum class LookupMode; class Universe; namespace state { - [[nodiscard]] lua_State* CreateState(Universe* U_, lua_State* from_, std::string_view const& hint_); - [[nodiscard]] lua_State* NewLaneState(Universe* U_, SourceState from_, std::optional const& libs_); + [[nodiscard]] + lua_State* CreateState(Universe* U_, lua_State* from_, std::string_view const& hint_); + [[nodiscard]] + lua_State* NewLaneState(Universe* U_, SourceState from_, std::optional const& libs_); LUAG_FUNC(supported_libs); } // namespace state diff --git a/src/threading_osx.h b/src/threading_osx.h index f4d41e0..c198d6d 100644 --- a/src/threading_osx.h +++ b/src/threading_osx.h @@ -17,9 +17,11 @@ struct cpu_set_t static inline void CPU_ZERO(cpu_set_t *cs) { cs->count = 0; } static inline void CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); } -[[nodiscard]] static inline int CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); } +[[nodiscard]] +static inline int CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); } -[[nodiscard]] int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set) +[[nodiscard]] +int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set) { int32_t core_count = 0; size_t len = sizeof(core_count); @@ -38,7 +40,8 @@ static inline void CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); } return 0; } -[[nodiscard]] int pthread_setaffinity_np(pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) +[[nodiscard]] +int pthread_setaffinity_np(pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) { thread_port_t mach_thread; int core = 0; diff --git a/src/tools.hpp b/src/tools.hpp index c587500..77ba5d2 100644 --- a/src/tools.hpp +++ b/src/tools.hpp @@ -11,6 +11,8 @@ enum class LookupMode FromKeeper // send a function from a keeper state to a lane }; +// ################################################################################################# + enum class FuncSubType { Bytecode, @@ -18,7 +20,8 @@ enum class FuncSubType FastJIT }; -[[nodiscard]] FuncSubType luaG_getfuncsubtype(lua_State* L_, StackIndex i_); +[[nodiscard]] +FuncSubType luaG_getfuncsubtype(lua_State* L_, StackIndex i_); // ################################################################################################# @@ -32,6 +35,7 @@ static constexpr RegistryUniqueKey kLookupRegKey{ 0xBF1FC5CF3C6DD47Bull }; // re namespace tools { void PopulateFuncLookupTable(lua_State* L_, StackIndex i_, std::string_view const& name_); - [[nodiscard]] std::string_view PushFQN(lua_State* L_, StackIndex t_, int last_); + [[nodiscard]] + std::string_view PushFQN(lua_State* L_, StackIndex t_, int last_); void SerializeRequire(lua_State* L_); } // namespace tools diff --git a/src/tracker.hpp b/src/tracker.hpp index c1a38e2..f7dca80 100644 --- a/src/tracker.hpp +++ b/src/tracker.hpp @@ -15,12 +15,15 @@ class LaneTracker public: void tracking_add(Lane* lane_); - [[nodiscard]] bool tracking_remove(Lane* lane_); - [[nodiscard]] int pushThreadsTable(lua_State* L_) const; + [[nodiscard]] + bool tracking_remove(Lane* lane_); + [[nodiscard]] + int pushThreadsTable(lua_State* L_) const; void activate() { trackingFirst = TRACKING_END; } - [[nodiscard]] bool isActive() const { + [[nodiscard]] + bool isActive() const { return trackingFirst != nullptr; } }; diff --git a/src/unique.hpp b/src/unique.hpp index c214dbc..aec5610 100644 --- a/src/unique.hpp +++ b/src/unique.hpp @@ -3,8 +3,9 @@ // ################################################################################################# // A unique type generator +// Marking *all* Unique<> types as [[nodiscard]] is maybe overkill, but there is no way of marking a specific instanciation template -class Unique +class [[nodiscard]] Unique { private: T val; // no default initialization so that std::is_trivial_v> == true @@ -69,7 +70,8 @@ class Unique }; template -class Unique>> +// Marking *all* Unique<> types as [[nodiscard]] is maybe overkill, but there is no way of marking a specific instanciation +class [[nodiscard]] Unique>> : public T { public: diff --git a/src/uniquekey.hpp b/src/uniquekey.hpp index 9041363..a8386a2 100644 --- a/src/uniquekey.hpp +++ b/src/uniquekey.hpp @@ -73,7 +73,8 @@ class RegistryUniqueKey } // --------------------------------------------------------------------------------------------- template - [[nodiscard]] T* readLightUserDataValue(lua_State* const L_) const + [[nodiscard]] + T* readLightUserDataValue(lua_State* const L_) const { STACK_GROW(L_, 1); STACK_CHECK_START_REL(L_, 0); @@ -84,7 +85,8 @@ class RegistryUniqueKey return value; } // --------------------------------------------------------------------------------------------- - [[nodiscard]] bool readBoolValue(lua_State* const L_) const + [[nodiscard]] + bool readBoolValue(lua_State* const L_) const { STACK_GROW(L_, 1); STACK_CHECK_START_REL(L_, 0); @@ -96,7 +98,8 @@ class RegistryUniqueKey } // --------------------------------------------------------------------------------------------- // equivalent to luaL_getsubtable - [[nodiscard]] bool getSubTable(lua_State* const L_, NArr const narr_, NRec const nrec_) const + [[nodiscard]] + bool getSubTable(lua_State* const L_, NArr const narr_, NRec const nrec_) const { STACK_CHECK_START_REL(L_, 0); pushValue(L_); // L_: {}|nil diff --git a/src/universe.hpp b/src/universe.hpp index 77fbb52..fa1e238 100644 --- a/src/universe.hpp +++ b/src/universe.hpp @@ -25,7 +25,8 @@ class ProtectedAllocator std::mutex mutex; - [[nodiscard]] static void* protected_lua_Alloc(void* ud_, void* ptr_, size_t osize_, size_t nsize_) + [[nodiscard]] + static void* protected_lua_Alloc(void* ud_, void* ptr_, size_t osize_, size_t nsize_) { ProtectedAllocator* const allocator{ static_cast(ud_) }; std::lock_guard guard{ allocator->mutex }; @@ -34,7 +35,8 @@ class ProtectedAllocator public: // we are not like our base class: we can't be created inside a full userdata (or we would have to install a metatable and __gc handler to destroy ourselves properly) - [[nodiscard]] static void* operator new(size_t size_, lua_State* L_) noexcept = delete; + [[nodiscard]] + static void* operator new(size_t size_, lua_State* L_) noexcept = delete; static void operator delete(void* p_, lua_State* L_) = delete; AllocatorDefinition makeDefinition() @@ -121,7 +123,8 @@ class Universe std::atomic selfdestructingCount{ 0 }; public: - [[nodiscard]] static void* operator new([[maybe_unused]] size_t size_, lua_State* L_) noexcept { return luaG_newuserdatauv(L_, UserValueCount{ 0 }); }; + [[nodiscard]] + static void* operator new([[maybe_unused]] size_t size_, lua_State* L_) noexcept { return luaG_newuserdatauv(L_, UserValueCount{ 0 }); }; // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception static void operator delete([[maybe_unused]] void* p_, [[maybe_unused]] lua_State* L_) {} // nothing to do, as nothing is allocated independently @@ -134,18 +137,22 @@ class Universe Universe& operator=(Universe&&) = delete; void callOnStateCreate(lua_State* const L_, lua_State* const from_, LookupMode const mode_); - [[nodiscard]] static Universe* Create(lua_State* L_); - [[nodiscard]] static inline Universe* Get(lua_State* L_); + [[nodiscard]] + static Universe* Create(lua_State* L_); + [[nodiscard]] + static inline Universe* Get(lua_State* L_); void initializeAllocatorFunction(lua_State* L_); static int InitializeFinalizer(lua_State* L_); void initializeOnStateCreate(lua_State* const L_); lanes::AllocatorDefinition resolveAllocator(lua_State* const L_, std::string_view const& hint_) const; static inline void Store(lua_State* L_, Universe* U_); - [[nodiscard]] bool terminateFreeRunningLanes(lua_Duration shutdownTimeout_, CancelOp op_); + [[nodiscard]] + bool terminateFreeRunningLanes(lua_Duration shutdownTimeout_, CancelOp op_); }; // ################################################################################################# +[[nodiscard]] inline Universe* Universe::Get(lua_State* L_) { STACK_CHECK_START_REL(L_, 0); -- cgit v1.2.3-55-g6feb