Library: simplified and generic realloc

since this is meant as a policy implementation, reduce it to the bare operation;
the actual container storage handling logic shall be implemented in the container
and based on those primitive and configurable base operations
This commit is contained in:
Fischlurch 2024-06-08 22:44:07 +02:00
parent 3a263c8c63
commit e99f4d531b
3 changed files with 42 additions and 10 deletions

View file

@ -145,6 +145,7 @@ namespace lib {
};
};
using std::is_trivially_copyable_v;
template<class I, class E, template<typename> class ALO>
struct AllocationPolicy
@ -160,8 +161,18 @@ namespace lib {
bool canExpand(size_t){ return false; }
Bucket*
realloc (Bucket* data, size_t demand)
realloc (Bucket* data, size_t cnt, size_t spread)
{
Bucket* newBucket = Fac::create (cnt, spread);
if (data)
{
size_t elms = min (cnt, data->cnt);
for (size_t idx=0; idx<elms; ++idx)
moveElem(idx, data, newBucket);
data->destroy();
}
return newBucket;
/*
size_t buffSiz{data? data->buffSiz : 0};
if (demand == buffSiz)
return data;
@ -205,7 +216,7 @@ namespace lib {
}
return newBucket;
}
*/
// // ensure sufficient storage or verify the ability to re-allocate
// if (not (Coll::hasReserve(sizeof(TY))
// or POL::canExpand(sizeof(TY))
@ -213,6 +224,23 @@ namespace lib {
// throw err::Invalid{_Fmt{"Unable to accommodate further element of type %s "}
// % util::typeStr<TY>()};
}
void
moveElem (size_t idx, Bucket* src, Bucket* tar)
{
if constexpr (is_trivially_copyable_v<E>)
{
void* oldPos = & src->subscript(idx);
void* newPos = & tar->subscript(idx);
size_t amount = min (src->spread, tar->spread);
std::memmove (newPos, oldPos, amount);
}
else
{
Fac::template createAt<E> (tar, idx
,std::move_if_noexcept (src->subscript(idx)));
}
}
};
template<class I, class E>
@ -333,7 +361,7 @@ namespace lib {
adjustStorage (size_t cnt, size_t spread)
{
size_t demand{cnt*spread};
Coll::data_ = POL::realloc (Coll::data_, demand);
Coll::data_ = POL::realloc (Coll::data_, cnt,spread); /////////////////OOO anpassen
ENSURE (Coll::data_);
if (spread != Coll::spread())
adjustSpread (spread);

View file

@ -114,6 +114,13 @@ namespace lib {
elm += off;
return * std::launder (reinterpret_cast<I*> (elm));
}
void
destroy()
{
if (deleter)
(*deleter) (this);
}
};
}//(End)implementation details
@ -140,7 +147,7 @@ namespace lib {
public:
~Several() noexcept
try { discardData(); }
try { if (data_) data_->destroy(); }
ERROR_LOG_AND_IGNORE (progress, "clean-up Several data")
/// Move-Assignment allowed...
@ -198,12 +205,6 @@ namespace lib {
}
private:
void
discardData()
{
if (data_ and data_->deleter)
(*data_->deleter) (data_);
}
};

View file

@ -82651,6 +82651,9 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node CREATED="1717884707582" ID="ID_1628844564" MODIFIED="1717884772953" TEXT="daf&#xfc;r dann auch die potentiell gef&#xe4;hrlichen Aktionen direkt am Eingang unterbinden"/>
<node CREATED="1717884782596" ID="ID_1264810588" MODIFIED="1717884808660" TEXT="damit kann dann die AllocationPolicy selber entscheiden, was zu tun ist"/>
</node>
<node COLOR="#435e98" CREATED="1717891370579" ID="ID_589922430" MODIFIED="1717891403679" TEXT="ArrayBucket stets indirekt via Deleter freigeben">
<icon BUILTIN="idea"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717857964116" ID="ID_1398927698" MODIFIED="1717858080950" TEXT="spread-Erweiterung korrekt handhaben">