Library: low-level implementation internals covered

...including overflow into new extents, alignment padding
and chaining and invocation of destructors.
This commit is contained in:
Fischlurch 2024-05-25 20:01:23 +02:00
parent be398e950a
commit 08e0f52e61
3 changed files with 59 additions and 38 deletions

View file

@ -28,11 +28,6 @@
** created within a single build process and is replaced or released
** as a whole. AllocationCluster implements memory management to
** support this usage pattern.
**
** @warning in rework 5/2024 with the goal to simplify the logic,
** remove all thread safety and make the implementation
** usable as standard conformant allocator for STL.
**
** @see allocation-cluster-test.cpp
** @see builder::ToolFactory
** @see linked-elements.hpp

View file

@ -37,14 +37,12 @@
#include <vector>
#include <limits>
#include <functional>
//#include <boost/lexical_cast.hpp>
//using boost::lexical_cast;
using ::Test;
using lib::explore;
using lib::test::showSizeof;
using util::getAddr;
using util::isnil;
using ::Test;
using std::numeric_limits;
using std::function;
@ -65,7 +63,7 @@ namespace test {
const size_t BLOCKSIZ = 256; ///< @warning actually defined in allocation-cluster.cpp
long checksum = 0; // validate proper pairing of ctor/dtor calls
int64_t checksum = 0; // validate proper pairing of ctor/dtor calls
template<uint i>
class Dummy
@ -85,7 +83,7 @@ namespace test {
checksum -= explore(content_).resultSum();
}
char getID() { return content_[0]; }
uint getID() { return content_[0]; }
};
@ -189,13 +187,14 @@ namespace test {
}
/** @test cover some tricky aspects of the low-level allocator
* @remark due to the expected leverage of AllocationCluster,
* an optimised low-level approach was taken on various aspects of storage management;
* the additional metadata overhead is a power of two, exploiting contextual knowledge
* about layout; moreover, a special usage-mode allows to skip invocation of destructors.
* To document these machinations, change to internal data is explicitly verified here.
* @todo WIP 5/24 🔁 define 🔁 implement
* @todo WIP 5/24 define implement
*/
void
verifyInternals()
@ -252,7 +251,8 @@ namespace test {
// existing storage unaffected
CHECK (i1 == i1pre);
CHECK (i2 == 55555);
CHECK (slot(0) == 0);
CHECK (slot(0) == 0); // no administrative data yet...
CHECK (slot(1) == 0);
// alignment is handled properly
char& c1 = clu.create<char> ('X');
@ -296,7 +296,7 @@ namespace test {
markSum = checksum;
CHECK (checksum == 4+4);
CHECK (alignof(Dummy<2>) == alignof(char));
CHECK (posOffset() - pp == sizeof(Dummy<2>));
CHECK (posOffset() - pp == sizeof(Dummy<2>)); // for disposable objects only the object storage itself plus alignment
// allocate a similar object,
// but this time also enrolling the destructor
@ -314,21 +314,42 @@ namespace test {
// any other object with non-trivial destructor....
string rands = lib::test::randStr(9);
pp = posOffset();
string& s1 = clu.create<string> (rands);
SHOW_EXPR(pp)
SHOW_EXPR(posOffset())
SHOW_EXPR(size_t(&s1))
string& s1 = clu.create<string> (rands); // a string that fits into the small-string optimisation
CHECK (s1 == rands);
CHECK (posOffset() - pp >= sizeof(string) + 2*sizeof(void*));
CHECK (size_t(&s1) - slot(1) == 2*sizeof(void*)); // again the Destructor frame is placed immediately before the object
auto dtor2 = (Dtor*)slot(1); // and it has been prepended to the destructors-list in current extent
SHOW_EXPR(size_t(dtor2->next))
CHECK (dtor2->next == dtor); // with the destructor of o2 hooked up behind
CHECK (dtor->next == nullptr);
// provoke overflow into a new extent
// by placing an object that does not fit
// into the residual space in current one
auto& o3 = clu.create<Dummy<223>> (3);
CHECK (clu.numExtents() == 3); // a third extent has been opened to accommodate this object
CHECK (checksum == markSum + 8+8 + uchar(223*3));
auto dtor3 = (Dtor*)slot(1);
CHECK (dtor3->next == nullptr); // Destructors are chained for each extent separately
CHECK (dtor3 != dtor2);
CHECK (dtor2->next == dtor); // the destructor chain from previous extent is also still valid
CHECK (dtor->next == nullptr);
CHECK (i1 == i1pre); // all data is intact (no corruption)
CHECK (s1 == rands);
CHECK (i2 == 55555);
CHECK (c1 == 'X');
CHECK (c2 == 'U');
CHECK (i3 == 42);
CHECK (o1.getID() == 4);
CHECK (o2.getID() == 8);
CHECK (o3.getID() == 3);
}
CHECK (checksum == markSum);
CHECK (checksum == markSum); // only the destructor of the "disposable" object o1 was not invoked
}
/** @test TODO demonstrate use as Standard-Allocator
* @todo WIP 5/24 🔁 define implement
*/

View file

@ -81637,8 +81637,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715725837111" ID="ID_1956366876" MODIFIED="1715818720819" TEXT="Basis-Allocation vorsehen">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1715725837111" ID="ID_1956366876" MODIFIED="1716659923208" TEXT="Basis-Allocation vorsehen">
<icon BUILTIN="button_ok"/>
<node COLOR="#435e98" CREATED="1715817001701" ID="ID_315311831" MODIFIED="1715817053842" TEXT="wie einbinden?">
<node COLOR="#5b280f" CREATED="1715817015550" ID="ID_116856376" MODIFIED="1715817030369" TEXT="als Standard-Allocator">
<icon BUILTIN="button_cancel"/>
@ -81651,8 +81651,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node CREATED="1715817106468" ID="ID_1098144156" MODIFIED="1715817115970" TEXT="...und das ist nur unn&#xf6;tige Komplikation"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715818949821" ID="ID_1747630681" MODIFIED="1715818962407" TEXT="Thema: Alignment">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#e0ceaa" COLOR="#690f14" CREATED="1715818949821" ID="ID_1747630681" MODIFIED="1716659945354" TEXT="Thema: Alignment">
<icon BUILTIN="messagebox_warning"/>
<node CREATED="1715985108575" ID="ID_1415977694" MODIFIED="1715988173554" TEXT="das kann man der Standard-Lib &#xfc;berlassen">
<richcontent TYPE="NOTE"><html>
<head/>
@ -81666,8 +81666,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node CREATED="1715818964036" ID="ID_1917864530" MODIFIED="1715818999461" TEXT="bedeutet: der Offset wird justiert bis er auf einer Alignment-Grenze liegt"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715819022774" ID="ID_541145684" MODIFIED="1715819026470" TEXT="Anforderungen">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#c8c0b6" COLOR="#435e98" CREATED="1715819022774" ID="ID_541145684" MODIFIED="1716659939640" TEXT="Anforderungen">
<icon BUILTIN="yes"/>
<node CREATED="1715819037772" ID="ID_1987233619" MODIFIED="1715819050902" TEXT="soll selber mit dem absoluten Minimum an Storage auskommen"/>
<node CREATED="1715819058921" ID="ID_895637699" MODIFIED="1715819099232" TEXT="mu&#xdf; zur de-Allokation eine Basis-Block-Kette navigieren"/>
<node CREATED="1715819206460" ID="ID_897739837" MODIFIED="1715819231318" TEXT="mu&#xdf; jedweils im aktuellen Block den Rest-Speicher feststellen k&#xf6;nnen"/>
@ -81759,8 +81759,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715820305436" ID="ID_1741026762" MODIFIED="1715820308396" TEXT="Clean-up">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1715820305436" ID="ID_1741026762" MODIFIED="1716659881133" TEXT="Clean-up">
<icon BUILTIN="button_ok"/>
<node CREATED="1716129938550" ID="ID_1201172803" MODIFIED="1716129943077" TEXT="C++ Magie">
<icon BUILTIN="idea"/>
<node CREATED="1716129945797" ID="ID_582558526" MODIFIED="1716129956272" TEXT="die LinkedElements machen bereits die gesamte Arbeit"/>
@ -81836,7 +81836,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#c8c0b6" CREATED="1716657678423" ID="ID_567905569" LINK="https://en.cppreference.com/w/cpp/named_req/TrivialType" MODIFIED="1716657768484" TEXT="wird nicht verwendet bei &#xbb;trivialen Typen&#xab;">
<node BACKGROUND_COLOR="#c8c0b6" CREATED="1716657678423" ID="ID_567905569" LINK="https://en.cppreference.com/w/cpp/named_req/TrivialType" MODIFIED="1716659870492" TEXT="wird nicht verwendet bei &#xbb;trivialen Typen&#xab;">
<linktarget COLOR="#7ea8d4" DESTINATION="ID_567905569" ENDARROW="Default" ENDINCLINATION="128;19;" ID="Arrow_ID_1002046418" SOURCE="ID_265572112" STARTARROW="None" STARTINCLINATION="254;-17;"/>
<node CREATED="1716657717681" ID="ID_739380369" MODIFIED="1716657732347" TEXT="Skalare (numerics, pointer)"/>
<node CREATED="1716657733587" ID="ID_1787247560" MODIFIED="1716657739437" TEXT="triviale Klassen">
<node CREATED="1716657746230" ID="ID_1898446055" MODIFIED="1716657751752" TEXT="trivial kopierbar"/>
@ -81870,9 +81871,9 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715790315814" ID="ID_878890004" MODIFIED="1715790393998" TEXT="L&#xf6;sung f&#xfc;r die Destruktoren schaffen">
<node COLOR="#338800" CREATED="1715790315814" ID="ID_878890004" MODIFIED="1716659839834" TEXT="L&#xf6;sung f&#xfc;r die Destruktoren schaffen">
<linktarget COLOR="#af578d" DESTINATION="ID_878890004" ENDARROW="Default" ENDINCLINATION="-446;33;" ID="Arrow_ID_464677545" SOURCE="ID_412040509" STARTARROW="None" STARTINCLINATION="-568;-24;"/>
<icon BUILTIN="flag-yellow"/>
<icon BUILTIN="button_ok"/>
<node CREATED="1715790397014" ID="ID_14966020" LINK="#ID_869337683" MODIFIED="1715790537195" TEXT="Ergebnis einer Konfliktl&#xf6;sung">
<richcontent TYPE="NOTE"><html>
<head/>
@ -81895,8 +81896,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</html></richcontent>
<icon BUILTIN="idea"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715790540241" ID="ID_698638121" MODIFIED="1715795875494" TEXT="Registry f&#xfc;r clean-up-Aktionen direkt im Cluster verankern">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1715790540241" ID="ID_698638121" MODIFIED="1716659835942" TEXT="Registry f&#xfc;r clean-up-Aktionen direkt im Cluster verankern">
<icon BUILTIN="button_ok"/>
</node>
<node CREATED="1715790571755" ID="ID_1943399206" MODIFIED="1715790774444" TEXT="die Factory-Funktion(en) bieten eine direkte Wahlm&#xf6;glichkeit">
<richcontent TYPE="NOTE"><html>
@ -81909,6 +81910,10 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</html></richcontent>
<node CREATED="1715790777543" ID="ID_953637060" MODIFIED="1715790789971" TEXT="create &#x27f9; Destruktor wird aufgerufen"/>
<node CREATED="1715790790703" ID="ID_578796550" MODIFIED="1715790803259" TEXT="createDisposable &#x27f9; Destruktor wird nicht aufgerufen"/>
<node COLOR="#435e98" CREATED="1716659842334" ID="ID_265572112" MODIFIED="1716659876928" TEXT="automatich auch f&#xfc;r &#xbb;triviale Typen&#xab;">
<arrowlink COLOR="#7ea8d4" DESTINATION="ID_567905569" ENDARROW="Default" ENDINCLINATION="128;19;" ID="Arrow_ID_1002046418" STARTARROW="None" STARTINCLINATION="254;-17;"/>
<icon BUILTIN="idea"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715725950719" ID="ID_1150594476" MODIFIED="1715725971326" TEXT="Testf&#xe4;lle vereinfachen">
@ -81929,11 +81934,11 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715783068923" ID="ID_34296369" MODIFIED="1715783077530" TEXT="Tests f&#xfc;r Error-Handling zur&#xfc;ckbauen">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1716130852555" ID="ID_1309770620" MODIFIED="1716605447662" TEXT="Test f&#xfc;r interne low-level-Mechanismen">
<icon BUILTIN="pencil"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716130874040" ID="ID_253325898" MODIFIED="1716133346231" TEXT="verifyInternals">
<node COLOR="#338800" CREATED="1716130852555" ID="ID_1309770620" MODIFIED="1716659827935" TEXT="Test f&#xfc;r interne low-level-Mechanismen">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1716130874040" ID="ID_253325898" MODIFIED="1716659830373" TEXT="verifyInternals">
<arrowlink COLOR="#4173be" DESTINATION="ID_1783945506" ENDARROW="Default" ENDINCLINATION="204;17;" ID="Arrow_ID_1547348126" STARTARROW="None" STARTINCLINATION="292;-22;"/>
<icon BUILTIN="flag-yellow"/>
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1716552160019" ID="ID_146077309" MODIFIED="1716566510566" TEXT="die Mechanik &#xfc;berhaupt erst mal zum Laufen bekommen">
<icon BUILTIN="button_ok"/>
@ -81964,8 +81969,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716552266490" ID="ID_34007851" MODIFIED="1716552275535" TEXT="weiteren &#xdc;berlauf mit Alignment">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1716552266490" ID="ID_34007851" MODIFIED="1716659826793" TEXT="weiteren &#xdc;berlauf mit Alignment">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715782674401" ID="ID_1481008630" MODIFIED="1715782690591" TEXT="neue Tests f&#xfc;r Verwendung mit STL-Container">