Library: sketch for a deleter trampoline

for simple allocators this can be static
This commit is contained in:
Fischlurch 2024-06-06 18:41:07 +02:00
parent 98d5b2962c
commit bf74ba6292
3 changed files with 60 additions and 27 deletions

View file

@ -71,6 +71,42 @@ namespace lib {
} }
}; };
template<class I, class E>
struct DeleterTrampoline
{
using Allo = std::allocator<E>;
using AlloT = std::allocator_traits<Allo>;
template<typename X>
static auto
adaptAllocator()
{
using XAllo = typename AlloT::template rebind_alloc<X>;
if constexpr (std::is_constructible_v<XAllo, Allo>)
return XAllo{Allo()};
else
return XAllo{};
}
static void
destroy (ArrayBucket<I>* bucket, size_t size)
{
Allo allo{};
for (size_t i=0; i<size; ++i)
AlloT::destroy (allo, & bucket->subscript(i));
size_t storageBytes = sizeof(ArrayBucket<I>) - sizeof(ArrayBucket<I>::storage)
+ size * sizeof(E);
using BAlloT = typename AlloT::template rebind_traits<std::byte>;
auto bAllo = adaptAllocator<std::byte>();
BAlloT::deallocate (bAllo, bucket, storageBytes);
};
};
using std::is_trivially_move_constructible_v; using std::is_trivially_move_constructible_v;
using std::is_trivially_destructible_v; using std::is_trivially_destructible_v;
using std::has_virtual_destructor_v; using std::has_virtual_destructor_v;
@ -125,6 +161,7 @@ namespace lib {
> >
class SeveralBuilder class SeveralBuilder
: Several<I> : Several<I>
, util::MoveOnly
, POL , POL
{ {
using Col = Several<I>; using Col = Several<I>;

View file

@ -50,7 +50,7 @@ namespace lib {
{ {
union Manager union Manager
{ {
typedef void (*Deleter) (void*, size_t, size_t); typedef void (*Deleter) (ArrayBucket*, size_t);
bool unmanaged :1; bool unmanaged :1;
Deleter deleter; Deleter deleter;
@ -89,7 +89,7 @@ namespace lib {
*/ */
template<class I> template<class I>
class Several class Several
// : util::MoveAssign ////////////////////////////////////////OOO fundamental design mismatch with intended builder usage! : util::MoveAssign
{ {
protected: protected:
using Bucket = ArrayBucket<I>*; using Bucket = ArrayBucket<I>*;
@ -97,11 +97,17 @@ namespace lib {
size_t size_{0}; size_t size_{0};
Bucket data_{nullptr}; Bucket data_{nullptr};
Several() =default; ///< may only be created through SeveralBuilder
public: public:
~Several() noexcept ~Several() noexcept
try { discardData(); } try { discardData(); }
ERROR_LOG_AND_IGNORE (progress, "clean-up Several data") ERROR_LOG_AND_IGNORE (progress, "clean-up Several data")
/// Move-Assignment allowed...
Several (Several&&) =default;
Several& operator= (Several&&) =default;
size_t size_t
size() const size() const
{ {
@ -139,19 +145,8 @@ namespace lib {
void void
discardData() discardData()
{ {
if (data_ and not data_->manager.unmanaged) if (data_ and data_->manager.deleter)
{ (*data_->manager.deleter) (data_, size_);
if (data_->manager.deleter)
{
(*data_->manager.deleter) (data_, size_, data_->spread);
}
else
{
for (size_t i=0; i<size_; ++i)
operator [](i).~I();
////////////////////////////////////////////OOO really a good idea to invoke the std::allocator_traits directly here??
}
}
} }
}; };

View file

@ -81883,8 +81883,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
hei&#223;t: er kann zwar nicht wegfallen (weil sich das dann im Typ ausdr&#252;cken w&#252;rde) &#8212; aber der Pointer bleibt dann NULL und der Container f&#228;llt auf Standard-Verhalten zur&#252;ck hei&#223;t: er kann zwar nicht wegfallen (weil sich das dann im Typ ausdr&#252;cken w&#252;rde) &#8212; aber der Pointer bleibt dann NULL und der Container f&#228;llt auf Standard-Verhalten zur&#252;ck
</p> </p>
</body> </body>
</html> </html></richcontent>
</richcontent>
<node CREATED="1717595296327" ID="ID_312816298" MODIFIED="1717595311329" TEXT="entweder er macht Standard-Heap-Allokationen"/> <node CREATED="1717595296327" ID="ID_312816298" MODIFIED="1717595311329" TEXT="entweder er macht Standard-Heap-Allokationen"/>
<node CREATED="1717595311917" ID="ID_1199827778" MODIFIED="1717595477064" TEXT="oder er de-alloziert &#xfc;berhaupt nicht"> <node CREATED="1717595311917" ID="ID_1199827778" MODIFIED="1717595477064" TEXT="oder er de-alloziert &#xfc;berhaupt nicht">
<richcontent TYPE="NOTE"><html> <richcontent TYPE="NOTE"><html>
@ -81929,8 +81928,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
Wenn dieser Test <i>scheitert, </i>dann <b>mu&#223; die Allokator-Instanz erhalten bleiben<i>.</i></b> Wenn dieser Test <i>scheitert, </i>dann <b>mu&#223; die Allokator-Instanz erhalten bleiben<i>.</i></b>
</p> </p>
</body> </body>
</html> </html></richcontent>
</richcontent>
<icon BUILTIN="yes"/> <icon BUILTIN="yes"/>
</node> </node>
</node> </node>
@ -81973,8 +81971,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
In der Tat: siehe special-job-fun.hpp In der Tat: siehe special-job-fun.hpp
</p> </p>
</body> </body>
</html> </html></richcontent>
</richcontent>
<icon BUILTIN="idea"/> <icon BUILTIN="idea"/>
</node> </node>
</node> </node>
@ -82223,8 +82220,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
&#160;oder es ist eine <i>selbstzerst&#246;rende </i>Heap-Allokation &#160;oder es ist eine <i>selbstzerst&#246;rende </i>Heap-Allokation
</p> </p>
</body> </body>
</html> </html></richcontent>
</richcontent>
</node> </node>
</node> </node>
</node> </node>
@ -82245,10 +82241,15 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
weil der Several-Container selber nur den Spread kennt, aber keine konkrete Typ-Info mehr hat weil der Several-Container selber nur den Spread kennt, aber keine konkrete Typ-Info mehr hat
</p> </p>
</body> </body>
</html> </html></richcontent>
</richcontent>
<icon BUILTIN="smily_bad"/> <icon BUILTIN="smily_bad"/>
</node> </node>
<node COLOR="#435e98" CREATED="1717691947664" ID="ID_954213667" MODIFIED="1717691981598" TEXT="Ha! ArrayBucket&lt;I&gt; ausnutzen">
<icon BUILTIN="idea"/>
<node CREATED="1717691983435" ID="ID_1420225271" MODIFIED="1717692005073" TEXT="das hat bereits die ganze ben&#xf6;tigte Logik">
<font NAME="SansSerif" SIZE="10"/>
</node>
</node>
</node> </node>
</node> </node>
<node CREATED="1717543083078" ID="ID_530261806" MODIFIED="1717543114805" TEXT="getMover()"/> <node CREATED="1717543083078" ID="ID_530261806" MODIFIED="1717543114805" TEXT="getMover()"/>
@ -82258,8 +82259,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node> </node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1716914864769" ID="ID_966642586" MODIFIED="1716914906125" TEXT="Builder-Operationen"> <node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1716914864769" ID="ID_966642586" MODIFIED="1716914906125" TEXT="Builder-Operationen">
<icon BUILTIN="pencil"/> <icon BUILTIN="pencil"/>
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1717634297656" ID="ID_330412196" MODIFIED="1717634315310" TEXT="Problem: Container soll move-only sein"> <node COLOR="#338800" CREATED="1717634297656" ID="ID_330412196" MODIFIED="1717688579115" TEXT="Move-builder und Container per Slice ausgeben">
<icon BUILTIN="messagebox_warning"/> <icon BUILTIN="button_ok"/>
</node> </node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716914869721" ID="ID_756769385" MODIFIED="1716914902541" TEXT="basis-Allokation"> <node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716914869721" ID="ID_756769385" MODIFIED="1716914902541" TEXT="basis-Allokation">
<icon BUILTIN="flag-yellow"/> <icon BUILTIN="flag-yellow"/>