From 02fea6561b8ea1a25d740088c4911b584bd763d9 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 28 May 2024 09:57:54 +0200 Subject: Sprinkle a bit of std::ranges::iota_view --- src/intercopycontext.cpp | 6 +++--- src/keeper.cpp | 21 +++++++++++---------- src/universe.cpp | 15 +++++++++------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 893305e..0b4aabc 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -1273,11 +1273,11 @@ namespace { InterCopyContext _c{ U, L2, L1, CacheIndex{ _top_L2 + 1 }, {}, VT::NORMAL, mode, _pBuf }; bool _copyok{ true }; STACK_CHECK_START_REL(L1, 0); - for (int i{ _top_L1 - n_ + 1 }, j{ 1 }; i <= _top_L1; ++i, ++j) { + for (int _i{ _top_L1 - n_ + 1 }, _j{ 1 }; _i <= _top_L1; ++_i, ++_j) { if (U->verboseErrors) { - sprintf(_tmpBuf, "arg_%d", j); + sprintf(_tmpBuf, "arg_%d", _j); } - _c.L1_i = SourceIndex{ i }; + _c.L1_i = SourceIndex{ _i }; _copyok = _c.inter_copy_one(); // L2: ... cache {}n if (!_copyok) { break; diff --git a/src/keeper.cpp b/src/keeper.cpp index b0ed062..ff5fe26 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -45,6 +45,7 @@ #include #include +#include // ################################################################################################# // Keeper implementation @@ -112,7 +113,7 @@ static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) int const _idx{ lua_gettop(L_) - count_ }; int const _start{ fifo_->first + fifo_->count - 1 }; // pop all additional arguments, storing them in the fifo - for (int _i = count_; _i >= 1; --_i) { + for (int const _i : std::ranges::reverse_view{ std::ranges::iota_view{ 1, count_ + 1 } }) { // store in the fifo the value at the top of the stack at the specified index, popping it from the stack lua_rawseti(L_, _idx, _start + _i); } @@ -126,10 +127,10 @@ static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) // expects exactly 1 value on the stack! // currently only called with a count of 1, but this may change in the future // function assumes that there is enough data in the fifo to satisfy the request -static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) +static void fifo_peek(lua_State* const L_, keeper_fifo const* const fifo_, int const count_) { STACK_GROW(L_, count_); - for (int _i = 0; _i < count_; ++_i) { + for (int const _i : std::ranges::iota_view{ 0, count_ }) { lua_rawgeti(L_, 1, (fifo_->first + _i)); } } @@ -145,20 +146,20 @@ static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) // each iteration pushes a value on the stack! STACK_GROW(L_, count_ + 2); // skip first item, we will push it last - for (int i = 1; i < count_; ++i) { - int const at{ fifo_->first + i }; + for (int const _i : std::ranges::iota_view{ 1, count_ }) { + int const _at{ fifo_->first + _i }; // push item on the stack - lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl val + lua_rawgeti(L_, _fifo_idx, _at); // L_: ... fifotbl val // remove item from the fifo lua_pushnil(L_); // L_: ... fifotbl val nil - lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl val + lua_rawseti(L_, _fifo_idx, _at); // L_: ... fifotbl val } // now process first item { - int const at{ fifo_->first }; - lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl vals val + int const _at{ fifo_->first }; + lua_rawgeti(L_, _fifo_idx, _at); // L_: ... fifotbl vals val lua_pushnil(L_); // L_: ... fifotbl vals val nil - lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl vals val + lua_rawseti(L_, _fifo_idx, _at); // L_: ... fifotbl vals val lua_replace(L_, _fifo_idx); // L_: ... vals } diff --git a/src/universe.cpp b/src/universe.cpp index 5fe53d5..5794048 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -36,6 +36,8 @@ THE SOFTWARE. #include "lane.h" #include "state.h" +#include + // ################################################################################################# Universe::Universe() @@ -124,7 +126,7 @@ void Universe::closeKeepers() // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success // which is early-outed with a keepers->nbKeepers null-check keepers->nb_keepers = 0; - for (int _i = 0; _i < _nbKeepers; ++_i) { + for (int const _i : std::ranges::iota_view{ 0, _nbKeepers }) { lua_State* const _K{ keepers->keeper_array[_i].L }; keepers->keeper_array[_i].L = KeeperState{ nullptr }; if (_K != nullptr) { @@ -134,7 +136,7 @@ void Universe::closeKeepers() _nbKeepers = _i; } } - for (int _i = 0; _i < _nbKeepers; ++_i) { + for (int const _i : std::ranges::iota_view{ 0, _nbKeepers }) { keepers->keeper_array[_i].~Keeper(); } // free the keeper bookkeeping structure @@ -225,8 +227,8 @@ void Universe::initializeKeepers(lua_State* L_) // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states { - size_t const bytes = sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper); - keepers = static_cast(internalAllocator.alloc(bytes)); + size_t const _bytes{ sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper) }; + keepers = static_cast(internalAllocator.alloc(_bytes)); if (keepers == nullptr) { raise_luaL_error(L_, "out of memory while creating keepers"); } @@ -234,11 +236,12 @@ void Universe::initializeKeepers(lua_State* L_) keepers->gc_threshold = keepers_gc_threshold; keepers->nb_keepers = _nb_keepers; - for (int _i = 0; _i < _nb_keepers; ++_i) { + for (int const _i : std::ranges::iota_view{ 0, _nb_keepers }) { keepers->keeper_array[_i].Keeper::Keeper(); } } - for (int _i = 0; _i < _nb_keepers; ++_i) { + + for (int const _i : std::ranges::iota_view{ 0, _nb_keepers }) { // note that we will leak K if we raise an error later KeeperState const _K{ create_state(this, L_) }; // L_: settings K: if (_K == nullptr) { -- cgit v1.2.3-55-g6feb