From 09c4750ef8973c08d6ef2aba70f5385ffd75f4c4 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 27 May 2024 15:24:06 +0200 Subject: More string_view --- src/intercopycontext.cpp | 58 ++++++++++++++++++++++++++---------------------- src/lanes.cpp | 2 +- src/threading.cpp | 30 ++++++++++++------------- src/threading.h | 4 ++-- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 67e4e03..6c72b1c 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -450,18 +450,18 @@ void InterCopyContext::copy_cached_func() const return false; } else if (!lua_istable(L2, -1)) { // this can happen if someone decides to replace same already registered item (for a example a standard lib function) with a table lua_getglobal(L1, "decoda_name"); // L1: ... t ... decoda_name - char const* from{ lua_tostring(L1, -1) }; + char const* _from{ lua_tostring(L1, -1) }; lua_pop(L1, 1); // L1: ... t ... lua_getglobal(L2, "decoda_name"); // L1: ... t ... L2: {} t decoda_name - char const* to{ lua_tostring(L2, -1) }; + char const* _to{ lua_tostring(L2, -1) }; lua_pop(L2, 1); // L1: ... t ... L2: {} t raise_luaL_error( getErrL(), "%s: source table '%s' found as %s in %s destination transfer database.", - from ? from : "main", + _from ? _from : "main", _fqn, lua_typename(L2, lua_type_as_enum(L2, -1)), - to ? to : "main"); + _to ? _to : "main"); } lua_remove(L2, -2); // L1: ... t ... L2: t break; @@ -1051,24 +1051,28 @@ void InterCopyContext::inter_copy_keyvaluepair() const // ################################################################################################# #if USE_DEBUG_SPEW() -static char const* lua_type_names[] = { - "LUA_TNIL" - , "LUA_TBOOLEAN" - , "LUA_TLIGHTUSERDATA" - , "LUA_TNUMBER" - , "LUA_TSTRING" - , "LUA_TTABLE" - , "LUA_TFUNCTION" - , "LUA_TUSERDATA" - , "LUA_TTHREAD" - , "" // not really a type - , "LUA_TJITCDATA" // LuaJIT specific -}; -static char const* vt_names[] = { - "VT::NORMAL" - , "VT::KEY" - , "VT::METATABLE" -}; +namespace { + namespace local { + static std::string_view const sLuaTypeNames[] = { + "LUA_TNIL" + , "LUA_TBOOLEAN" + , "LUA_TLIGHTUSERDATA" + , "LUA_TNUMBER" + , "LUA_TSTRING" + , "LUA_TTABLE" + , "LUA_TFUNCTION" + , "LUA_TUSERDATA" + , "LUA_TTHREAD" + , "" // not really a type + , "LUA_TJITCDATA" // LuaJIT specific + }; + static std::string_view const sValueTypeNames[] = { + "VT::NORMAL" + , "VT::KEY" + , "VT::METATABLE" + }; + } +} #endif // USE_DEBUG_SPEW() /* @@ -1092,7 +1096,7 @@ static char const* vt_names[] = { DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U }); LuaType _val_type{ lua_type_as_enum(L1, L1_i) }; - DEBUGSPEW_CODE(DebugSpew(U) << lua_type_names[static_cast(_val_type)] << " " << vt_names[static_cast(vt)] << ": "); + DEBUGSPEW_CODE(DebugSpew(U) << local::sLuaTypeNames[static_cast(_val_type)] << " " << local::sValueTypeNames[static_cast(vt)] << ": "); // Non-POD can be skipped if its metatable contains { __lanesignore = true } if (((1 << static_cast(_val_type)) & kPODmask) == 0) { @@ -1203,9 +1207,9 @@ static char const* vt_names[] = { // but don't copy it anyway, as the function names change depending on the slot index! // users should provide an on_state_create function to setup custom loaders instead // don't copy package.preload in keeper states (they don't know how to translate functions) - char const* _entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; - for (char const* const _entry : _entries) { - if (!_entry) { + std::string_view const _entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : "" /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, "" }; + for (std::string_view const& _entry : _entries) { + if (_entry.empty()) { continue; } DEBUGSPEW_CODE(DebugSpew(U) << "package." << _entry << std::endl); @@ -1218,7 +1222,7 @@ static char const* vt_names[] = { STACK_CHECK(L1, 0); } if (_result == InterCopyResult::Success) { - lua_setfield(L2, -2, _entry); // set package[entry] + lua_setfield(L2, -2, _entry.data()); // set package[entry] } else { lua_pushfstring(L1, "failed to copy package entry %s", _entry); // raise the error when copying from lane to lane, else just leave it on the stack to be raised later diff --git a/src/lanes.cpp b/src/lanes.cpp index 0369171..20636b2 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -548,7 +548,7 @@ LUAG_FUNC(wakeup_conv) // .isdst (daylight saving on/off) STACK_CHECK_START_REL(L_, 0); - auto _readInteger = [L = L_](char const* name_) { + auto _readInteger = [L = L_](std::string_view const& name_) { std::ignore = luaG_getfield(L, 1, name_); lua_Integer const val{ lua_tointeger(L, -1) }; lua_pop(L, 1); diff --git a/src/threading.cpp b/src/threading.cpp index 254b2e3..43bd107 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -139,9 +139,9 @@ void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, [[maybe_unused]] boo // ################################################################################################# -void THREAD_SET_AFFINITY(unsigned int aff) +void THREAD_SET_AFFINITY(unsigned int aff_) { - if (!SetThreadAffinityMask(GetCurrentThread(), aff)) { + if (!SetThreadAffinityMask(GetCurrentThread(), aff_)) { FAIL("THREAD_SET_AFFINITY", GetLastError()); } } @@ -162,12 +162,12 @@ typedef struct tagTHREADNAME_INFO #pragma pack(pop) #endif // !__GNUC__ -void THREAD_SETNAME(char const* _name) +void THREAD_SETNAME(std::string_view const& name_) { #if !defined __GNUC__ THREADNAME_INFO info; info.dwType = 0x1000; - info.szName = _name; + info.szName = name_.data(); info.dwThreadID = GetCurrentThreadId(); info.dwFlags = 0; @@ -385,7 +385,7 @@ void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, [[maybe_unused]] boo // ################################################################################################# -void THREAD_SET_AFFINITY(unsigned int aff) +void THREAD_SET_AFFINITY(unsigned int aff_) { int bit = 0; #ifdef __NetBSD__ @@ -397,12 +397,12 @@ void THREAD_SET_AFFINITY(unsigned int aff) cpu_set_t cpuset; CPU_ZERO(&cpuset); #endif - while (aff != 0) { - if (aff & 1) { + while (aff_ != 0) { + if (aff_ & 1) { CPU_SET(bit, &cpuset); } ++bit; - aff >>= 1; + aff_ >>= 1; } #ifdef __ANDROID__ PT_CALL(sched_setaffinity(pthread_self(), sizeof(cpu_set_t), &cpuset)); @@ -416,24 +416,24 @@ void THREAD_SET_AFFINITY(unsigned int aff) // ################################################################################################# -void THREAD_SETNAME(char const* _name) +void THREAD_SETNAME(std::string_view const& name_) { // exact API to set the thread name is platform-dependant // if you need to fix the build, or if you know how to fill a hole, tell me (bnt.germain@gmail.com) so that I can submit the fix in github. #if defined PLATFORM_BSD && !defined __NetBSD__ - pthread_set_name_np(pthread_self(), _name); + pthread_set_name_np(pthread_self(), name_.data()); #elif defined PLATFORM_BSD && defined __NetBSD__ - pthread_setname_np(pthread_self(), "%s", (void*) _name); + pthread_setname_np(pthread_self(), "%s", (void*) name_.data()); #elif defined PLATFORM_LINUX #if LINUX_USE_PTHREAD_SETNAME_NP - pthread_setname_np(pthread_self(), _name); + pthread_setname_np(pthread_self(), name_.data()); #else // LINUX_USE_PTHREAD_SETNAME_NP - prctl(PR_SET_NAME, _name, 0, 0, 0); + prctl(PR_SET_NAME, name_.data(), 0, 0, 0); #endif // LINUX_USE_PTHREAD_SETNAME_NP #elif defined PLATFORM_QNX || defined PLATFORM_CYGWIN - pthread_setname_np(pthread_self(), _name); + pthread_setname_np(pthread_self(), name_.data()); #elif defined PLATFORM_OSX - pthread_setname_np(_name); + pthread_setname_np(name_.data()); #else fprintf(stderr, "THREAD_SETNAME: unsupported platform\n"); abort(); diff --git a/src/threading.h b/src/threading.h index d88d308..521618b 100644 --- a/src/threading.h +++ b/src/threading.h @@ -68,8 +68,8 @@ static constexpr int kThreadPrioMax{ +3 }; // ################################################################################################# // ################################################################################################# -void THREAD_SETNAME(char const* _name); +void THREAD_SETNAME(std::string_view const& name_); void THREAD_SET_PRIORITY(int prio_, bool sudo_); -void THREAD_SET_AFFINITY(unsigned int aff); +void THREAD_SET_AFFINITY(unsigned int aff_); void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, bool sudo_); -- cgit v1.2.3-55-g6feb