Refactor PD::Li Pools system, Upgrade PD::Pool and add better logging
- Add ResetFast to Pool using c++20 concept - Add Put function to Pool - Use Pool Index instead of pointers in Command - Add accessor to Li Pools to prevent wrong command usage - Add Command Formatter - Use ResetFast for all Pools - Add Reset func to Li::Vertex (for ResetFast) - Add Pool Expandor and Put funcs to Lithium pools - Add getters for GfxDriverBase to PD::Li Pools
This commit is contained in:
@@ -3,6 +3,11 @@
|
||||
#include <memory>
|
||||
#include <pd/common.hpp>
|
||||
namespace PD {
|
||||
// lets take use of c++ 20 concepts
|
||||
// https://en.cppreference.com/w/cpp/language/constraints.html
|
||||
template <typename T>
|
||||
concept Resettable = requires(T& v) { v.Reset(); };
|
||||
|
||||
template <typename T, typename Alloc = std::allocator<T>>
|
||||
class Pool {
|
||||
public:
|
||||
@@ -13,6 +18,9 @@ class Pool {
|
||||
Pool() = default;
|
||||
~Pool() {
|
||||
if (pData) {
|
||||
for (size_t i = 0; i < pCap; i++) {
|
||||
std::allocator_traits<Alloc>::destroy(pAlloc, &pData[i]);
|
||||
}
|
||||
pAlloc.deallocate(pData, pCap);
|
||||
pData = nullptr;
|
||||
}
|
||||
@@ -47,6 +55,11 @@ class Pool {
|
||||
*e = elem;
|
||||
}
|
||||
|
||||
void Put(size_t idx, const T& elem) {
|
||||
if (idx >= pCap) ExpandIf(idx);
|
||||
pData[idx] = elem;
|
||||
}
|
||||
|
||||
void ExpandIf(size_t req) {
|
||||
if ((pPos + req) <= pCap) return;
|
||||
size_t ncap = std::max(pCap * 2, pPos + req);
|
||||
@@ -56,11 +69,14 @@ class Pool {
|
||||
std::allocator_traits<Alloc>::construct(
|
||||
pAlloc, &nu[i], std::move_if_noexcept(pData[i]));
|
||||
}
|
||||
for (size_t i = 0; i < pCap; i++) {
|
||||
std::allocator_traits<Alloc>::destroy(pAlloc, &pData[i]);
|
||||
}
|
||||
pAlloc.deallocate(pData, pCap);
|
||||
}
|
||||
for (size_t i = pPos; i < ncap; i++) {
|
||||
std::allocator_traits<Alloc>::construct(pAlloc, &nu[i]);
|
||||
}
|
||||
std::allocator_traits<Alloc>::construct(pAlloc, &nu[i]);
|
||||
}
|
||||
PDLOG("Pool::ExpandIf({}): {} -> {}", req, pCap, ncap);
|
||||
pData = nu;
|
||||
pCap = ncap;
|
||||
@@ -76,6 +92,21 @@ class Pool {
|
||||
}
|
||||
}
|
||||
|
||||
void ResetFast() {
|
||||
if constexpr (Resettable<T>) {
|
||||
for (size_t i = 0; i < pPos; i++) {
|
||||
pData[i].Reset();
|
||||
}
|
||||
} else if constexpr (!std::is_trivially_destructible_v<T>) {
|
||||
PDWARN(
|
||||
"ResetFast should only be executed with a non destructible type or a "
|
||||
"class/struct that has a Reset func! {} is not "
|
||||
"trivially_destructible and has no reset func.",
|
||||
TypeName<T>());
|
||||
}
|
||||
pPos = 0;
|
||||
}
|
||||
|
||||
size_t size() const { return pPos; }
|
||||
size_t capacity() const { return pCap; }
|
||||
T& at(size_t idx) { return pData[idx]; }
|
||||
@@ -89,7 +120,16 @@ class Pool {
|
||||
const T& operator[](size_t idx) const { return at(idx); }
|
||||
|
||||
private:
|
||||
Pool(size_t size) : pCap(size), pPos(0) { pData = pAlloc.allocate(size); }
|
||||
Pool(size_t size) : pCap(size), pPos(0) {
|
||||
pPos = 0;
|
||||
pCap = size;
|
||||
pData = pAlloc.allocate(size);
|
||||
for (size_t i = 0; i < pCap; i++) {
|
||||
std::allocator_traits<Alloc>::construct(pAlloc, &pData[i]);
|
||||
}
|
||||
PDLOG("Pool::Init({})", size);
|
||||
}
|
||||
|
||||
size_t pCap = 0;
|
||||
size_t pPos = 0;
|
||||
Alloc pAlloc;
|
||||
|
||||
Reference in New Issue
Block a user