Library: sketch for a deleter trampoline
for simple allocators this can be static
This commit is contained in:
parent
98d5b2962c
commit
bf74ba6292
3 changed files with 60 additions and 27 deletions
|
|
@ -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_destructible_v;
|
||||
using std::has_virtual_destructor_v;
|
||||
|
|
@ -125,6 +161,7 @@ namespace lib {
|
|||
>
|
||||
class SeveralBuilder
|
||||
: Several<I>
|
||||
, util::MoveOnly
|
||||
, POL
|
||||
{
|
||||
using Col = Several<I>;
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ namespace lib {
|
|||
{
|
||||
union Manager
|
||||
{
|
||||
typedef void (*Deleter) (void*, size_t, size_t);
|
||||
typedef void (*Deleter) (ArrayBucket*, size_t);
|
||||
|
||||
bool unmanaged :1;
|
||||
Deleter deleter;
|
||||
|
|
@ -89,7 +89,7 @@ namespace lib {
|
|||
*/
|
||||
template<class I>
|
||||
class Several
|
||||
// : util::MoveAssign ////////////////////////////////////////OOO fundamental design mismatch with intended builder usage!
|
||||
: util::MoveAssign
|
||||
{
|
||||
protected:
|
||||
using Bucket = ArrayBucket<I>*;
|
||||
|
|
@ -97,11 +97,17 @@ namespace lib {
|
|||
size_t size_{0};
|
||||
Bucket data_{nullptr};
|
||||
|
||||
Several() =default; ///< may only be created through SeveralBuilder
|
||||
|
||||
public:
|
||||
~Several() noexcept
|
||||
try { discardData(); }
|
||||
ERROR_LOG_AND_IGNORE (progress, "clean-up Several data")
|
||||
|
||||
/// Move-Assignment allowed...
|
||||
Several (Several&&) =default;
|
||||
Several& operator= (Several&&) =default;
|
||||
|
||||
size_t
|
||||
size() const
|
||||
{
|
||||
|
|
@ -139,19 +145,8 @@ namespace lib {
|
|||
void
|
||||
discardData()
|
||||
{
|
||||
if (data_ and not data_->manager.unmanaged)
|
||||
{
|
||||
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??
|
||||
}
|
||||
}
|
||||
if (data_ and data_->manager.deleter)
|
||||
(*data_->manager.deleter) (data_, size_);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -81883,8 +81883,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
heißt: er kann zwar nicht wegfallen (weil sich das dann im Typ ausdrücken würde) — aber der Pointer bleibt dann NULL und der Container fällt auf Standard-Verhalten zurück
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</richcontent>
|
||||
</html></richcontent>
|
||||
<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 überhaupt nicht">
|
||||
<richcontent TYPE="NOTE"><html>
|
||||
|
|
@ -81929,8 +81928,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
Wenn dieser Test <i>scheitert, </i>dann <b>muß die Allokator-Instanz erhalten bleiben<i>.</i></b>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</richcontent>
|
||||
</html></richcontent>
|
||||
<icon BUILTIN="yes"/>
|
||||
</node>
|
||||
</node>
|
||||
|
|
@ -81973,8 +81971,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
In der Tat: siehe special-job-fun.hpp
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</richcontent>
|
||||
</html></richcontent>
|
||||
<icon BUILTIN="idea"/>
|
||||
</node>
|
||||
</node>
|
||||
|
|
@ -82223,8 +82220,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
 oder es ist eine <i>selbstzerstörende </i>Heap-Allokation
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</richcontent>
|
||||
</html></richcontent>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
|
|
@ -82245,10 +82241,15 @@ Date:   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
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</richcontent>
|
||||
</html></richcontent>
|
||||
<icon BUILTIN="smily_bad"/>
|
||||
</node>
|
||||
<node COLOR="#435e98" CREATED="1717691947664" ID="ID_954213667" MODIFIED="1717691981598" TEXT="Ha! ArrayBucket<I> ausnutzen">
|
||||
<icon BUILTIN="idea"/>
|
||||
<node CREATED="1717691983435" ID="ID_1420225271" MODIFIED="1717692005073" TEXT="das hat bereits die ganze benötigte Logik">
|
||||
<font NAME="SansSerif" SIZE="10"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node CREATED="1717543083078" ID="ID_530261806" MODIFIED="1717543114805" TEXT="getMover()"/>
|
||||
|
|
@ -82258,8 +82259,8 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
</node>
|
||||
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1716914864769" ID="ID_966642586" MODIFIED="1716914906125" TEXT="Builder-Operationen">
|
||||
<icon BUILTIN="pencil"/>
|
||||
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1717634297656" ID="ID_330412196" MODIFIED="1717634315310" TEXT="Problem: Container soll move-only sein">
|
||||
<icon BUILTIN="messagebox_warning"/>
|
||||
<node COLOR="#338800" CREATED="1717634297656" ID="ID_330412196" MODIFIED="1717688579115" TEXT="Move-builder und Container per Slice ausgeben">
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716914869721" ID="ID_756769385" MODIFIED="1716914902541" TEXT="basis-Allokation">
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
|
|
|
|||
Loading…
Reference in a new issue