Implement lf_array::for_each

This commit is contained in:
Elad 2024-11-16 09:32:41 +02:00
parent 935e83a490
commit 1475625705
2 changed files with 65 additions and 19 deletions

View File

@ -72,6 +72,58 @@ public:
return *result; return *result;
} }
template <typename F> requires (std::is_invocable_v<F, T&>)
auto for_each(F&& func, bool is_finite = true)
{
lf_array* _this = this;
using return_t = decltype(func(std::declval<T&>()));
for (usz i = 0; _this; i += N)
{
for (usz j = 0; j < N; j++)
{
if constexpr (std::is_void_v<return_t>)
{
std::invoke(func, _this->m_data[j]);
}
else
{
auto ret = std::invoke(func, _this->m_data[j]);
if (ret)
{
return std::make_pair(std::addressof(_this->m_data[j]), std::move(ret));
}
}
}
lf_array* next = m_next;
if (!next && !std::is_void_v<return_t> && !is_finite)
{
for (auto _new = new lf_array, ptr = _this; ptr;)
{
// Install the pointer. If failed, go deeper.
ptr = ptr->m_next.compare_and_swap(nullptr, _new);
if (!next)
{
// Determine the next pointer (if null then the new memory has been installed)
next = ptr ? ptr : _new;
}
}
}
_this = next;
}
if constexpr (!std::is_void_v<return_t>)
{
return std::make_pair(std::add_pointer_t<T>{}, return_t());
}
}
u64 size() const u64 size() const
{ {
u64 size_n = 0; u64 size_n = 0;

View File

@ -112,12 +112,8 @@ namespace utils
return false; return false;
} }
const u64 map_size = s_is_mapping.size(); return s_is_mapping.for_each([addr](const map_info_t& info)
for (u64 i = map_size - 1; i != umax; i--)
{ {
const auto& info = s_is_mapping[i];
if (info.state == 1) if (info.state == 1)
{ {
if (addr >= info.addr && addr < info.addr + info.size) if (addr >= info.addr && addr < info.addr + info.size)
@ -125,24 +121,20 @@ namespace utils
return true; return true;
} }
} }
}
return false; return false;
}).second;
} }
u64 unmap_mappping_memory(u64 addr, u64 size) u64 unmap_mappping_memory(u64 addr, u64 size)
{ {
if (!addr || !size) if (!addr || !size)
{ {
return false; return 0;
} }
const u64 map_size = s_is_mapping.size(); return s_is_mapping.for_each([addr, size](map_info_t& info) -> u64
for (u64 i = map_size - 1; i != umax; i--)
{ {
auto& info = s_is_mapping[i];
if (info.state == 1) if (info.state == 1)
{ {
if (addr == info.addr && size == info.size) if (addr == info.addr && size == info.size)
@ -153,9 +145,9 @@ namespace utils
} }
} }
} }
}
return false; return 0;
}).second;
} }
bool map_mappping_memory(u64 addr, u64 size) bool map_mappping_memory(u64 addr, u64 size)
@ -165,10 +157,8 @@ namespace utils
return false; return false;
} }
for (u64 i = 0;; i++) ensure(s_is_mapping.for_each([addr, size](map_info_t& info)
{ {
auto& info = s_is_mapping[i];
if (!info.addr && info.state.compare_and_swap_test(0, 2)) if (!info.addr && info.state.compare_and_swap_test(0, 2))
{ {
info.addr = addr; info.addr = addr;
@ -176,7 +166,11 @@ namespace utils
info.state = 1; info.state = 1;
return true; return true;
} }
}
return false;
}, true).second);
return true;
} }
bool is_memory_mappping_memory(const void* addr) bool is_memory_mappping_memory(const void* addr)