From f5813a1f291e165df88830b636b856d5469aa124 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Wed, 12 Jul 2023 04:53:30 +0200 Subject: [PATCH] Block-Flow: veryfy proper handling of extent reuse - use a checksum to prove that ctor / dtor of "content" is not invoked - let the usage of active extents "wrap around" so that the mem block is re-used - verify that the same data is still there --- src/vault/mem/extent-family.hpp | 5 +- tests/vault/mem/extent-family-test.cpp | 79 +++++++++++++++++++++++++- wiki/thinkPad.ichthyo.mm | 23 ++++++++ 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/vault/mem/extent-family.hpp b/src/vault/mem/extent-family.hpp index 18647d87e..b33a3e949 100644 --- a/src/vault/mem/extent-family.hpp +++ b/src/vault/mem/extent-family.hpp @@ -276,8 +276,9 @@ namespace mem { REQUIRE (idx < extents_.size()); REQUIRE (activeSlotCnt() > 0); - return start_ <= idx - and idx < after_; + return isWrapped()? (start_ <= idx and idx < extents_.size()) + or idx < after_ + : (start_ <= idx and idx < after_); } Extent& diff --git a/tests/vault/mem/extent-family-test.cpp b/tests/vault/mem/extent-family-test.cpp index f3ed55cd9..b845f3ec9 100644 --- a/tests/vault/mem/extent-family-test.cpp +++ b/tests/vault/mem/extent-family-test.cpp @@ -28,7 +28,8 @@ #include "lib/test/run.hpp" #include "vault/mem/extent-family.hpp" //#include "lib/time/timevalue.hpp" -//#include "lib/format-cout.hpp" +//#include "lib/format-cout.hpp"////////////////////TODO +#include "lib/test/diagnostic-output.hpp" //#include "lib/util.hpp" #include @@ -69,6 +70,7 @@ namespace test { simpleUsage(); use_and_drop(); iteration(); + reuseUnclean(); wrapAround(); } @@ -126,9 +128,9 @@ namespace test { { Extents extents{5}; Iter it = extents.begin(); - CHECK (isnil (it)); + CHECK (isnil (it)); // no extents provided yet - extents.openNew(2); // allocate two Extents + extents.openNew(2); // allot two extents for active use CHECK (it); CHECK (0 == it.getIndex()); @@ -165,6 +167,77 @@ namespace test { + /** @test verify that neither constructors nor destructors are invoked + * automatically when discarding or re-using extents. + */ + void + reuseUnclean() + { + struct Probe + { + short val; + Probe() : val(1 + rand() % 1000) { } + ~Probe() { val = 0; } + }; + + using SpecialExtents = ExtentFamily; + + SpecialExtents spex{3}; + spex.openNew(2); + CHECK ( 0 == watch(spex).first()); + CHECK ( 2 == watch(spex).last()); + + // implant a new Probe object into each »slot« of the new extent + auto& extent = *spex.begin(); + for (Probe& probe : extent) + new(&probe) Probe; + + auto calcChecksum = [](SpecialExtents::Extent& extent) -> size_t + { + size_t sum{0}; + for (Probe& probe : extent) + sum += probe.val; + return sum; + }; + + size_t checksum = calcChecksum (*spex.begin()); + + // discard first extent, i.e. mark it as unused + // while the underlying memory block remains allocated + // and data within this block is not touched + spex.dropOld(1); + CHECK ( 1 == watch(spex).first()); + CHECK ( 2 == watch(spex).last()); + + // the »begin« (i.e. the first active extent is now another memory block + CHECK (not isSameObject (extent, *spex.begin())); + size_t checkSecond = calcChecksum (*spex.begin()); + CHECK (checkSecond != checksum); + + // but the random data generated above still sits in the original (first) memory block + CHECK (checksum == calcChecksum (extent)); + + // now let the actively allotted extents "wrap around"... + spex.dropOld(1); + CHECK ( 2 == watch(spex).first()); + CHECK ( 2 == watch(spex).last()); + spex.openNew(2); + CHECK ( 2 == watch(spex).first()); + CHECK ( 1 == watch(spex).last()); + + auto iter = spex.begin(); + CHECK ( 2 == iter.getIndex()); + ++iter; + CHECK ( 0 == iter.getIndex()); + CHECK (isSameObject(*iter, extent)); + + // and during all those allotting and dropping, data in the memory block was not touched, + // which also proves that constructors or destructors of the nominal "content" are not invoked + CHECK (checksum == calcChecksum (extent)); + } + + + /** @test verify in detail how iteration wraps around to also reuse * previously dropped extents, possibly rearranging the internal * management-vector to allow growing new extents at the end. diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index a60c786dd..05af905a9 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -78795,6 +78795,29 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + + + + + + + + + + + + + + + + + + + + +