From 32bea9521bfa3005753dec272289cd1399c97fff Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 16 Jun 2024 03:22:32 +0200 Subject: [PATCH] Library: get the simple testcase to work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - create two vectors, attached to the `TrackingAllocator` - emplace Tracker-Objects - move an object to the other vector - destroy the containers 🠲 Event-Log looks plausible! --- src/lib/test/tracking-allocator.cpp | 77 +++++++++++++++++++++++------ src/lib/test/tracking-allocator.hpp | 76 ++-------------------------- wiki/thinkPad.ichthyo.mm | 77 +++++++++++++++++++++-------- 3 files changed, 120 insertions(+), 110 deletions(-) diff --git a/src/lib/test/tracking-allocator.cpp b/src/lib/test/tracking-allocator.cpp index 5d6065713..08fee70a9 100644 --- a/src/lib/test/tracking-allocator.cpp +++ b/src/lib/test/tracking-allocator.cpp @@ -35,6 +35,7 @@ //#include "lib/format-string.hpp" //#include "lib/format-cout.hpp" //#include "lib/unique-malloc-owner.hpp" +#include "lib/iter-explorer.hpp" #include "lib/depend.hpp" #include "lib/uninitialised-storage.hpp" #include "lib/util.hpp" @@ -49,6 +50,7 @@ using std::make_pair; //using std::string; using util::contains; using util::joinDash; +using util::showAddr; namespace lib { namespace test{ @@ -72,22 +74,31 @@ namespace test{ : util::MoveOnly { UninitialisedDynBlock buff{}; + size_t entryID; }; using AllocTab = std::unordered_map; Literal poolID_; - AllocTab allocs_{}; + AllocTab allocs_; + HashVal checksum_; + size_t entryNr_; public: MemoryPool (Literal id) : poolID_{id} , allocs_{} + , checksum_{0} + , entryNr_{0} { } Allocation& addAlloc (size_t bytes); - void discardAlloc (void* loc); + void discardAlloc (void* loc, size_t); + + HashVal getChecksum() const; + size_t getAllocationCnt() const; + size_t calcAllocSize() const; }; @@ -164,15 +175,18 @@ namespace test{ void* - TrackingAllocator::allocate (size_t n) + TrackingAllocator::allocate (size_t cnt) { - UNIMPLEMENTED ("allocate memory block of size n"); + ENSURE (mem_); + return mem_->addAlloc (cnt) + .buff.front(); } void - TrackingAllocator::deallocate (void* loc) noexcept + TrackingAllocator::deallocate (void* loc, size_t cnt) noexcept { - UNIMPLEMENTED ("allocate memory block of size n"); + ENSURE (mem_); + mem_->discardAlloc (loc, cnt); } MemoryPool::Allocation& @@ -182,24 +196,49 @@ namespace test{ newAlloc.buff.allocate (bytes); Location loc = newAlloc.buff.front(); ASSERT (not contains (allocs_, loc)); - logAlloc (poolID_, "allocate", bytes, loc); + newAlloc.entryID = ++entryNr_; + logAlloc (poolID_, "allocate", entryNr_, bytes, showAddr(loc)); + checksum_ += entryNr_ * bytes; return allocs_.emplace (loc, move(newAlloc)) .first->second; } void - MemoryPool::discardAlloc (void* loc) + MemoryPool::discardAlloc (void* loc, size_t bytes) { if (contains (allocs_, loc)) { auto& entry = allocs_[loc]; ASSERT (entry.buff); ASSERT (entry.buff.front() == loc); - logAlloc (poolID_, "deallocate", entry.buff.size()); + if (entry.buff.size() != bytes) + logAlarm ("SizeMismatch", entry.entryID, bytes, showAddr(loc)); + logAlloc (poolID_, "deallocate", entry.entryID, bytes, showAddr(loc)); + checksum_ -= entryNr_ * bytes; allocs_.erase(loc); } else // deliberately no exception here (for better diagnostics) - logAlarm("FreeUnknown", loc); + logAlarm ("FreeUnknown", bytes, showAddr(loc)); + } + + HashVal + MemoryPool::getChecksum() const + { + return checksum_; + } + + size_t + MemoryPool::getAllocationCnt() const + { + return allocs_.size(); + } + + size_t + MemoryPool::calcAllocSize() const + { + return explore(allocs_) + .transform ([](auto& it){ return it->second.buff.size(); }) + .resultSum(); } @@ -209,22 +248,28 @@ namespace test{ EventLog TrackingAllocator::log{"test::TrackingAllocator"}; + /** get Checksum for mem-pool */ HashVal - TrackingAllocator::checksum (Literal pool) + TrackingAllocator::checksum (Literal poolID) { - UNIMPLEMENTED ("get Checksum for mem-pool"); + PoolHandle pool = PoolRegistry::locate (poolID); + return pool->getChecksum(); } + /** get allocation count for mem-pool */ size_t - TrackingAllocator::numAlloc (Literal pool) + TrackingAllocator::numAlloc (Literal poolID) { - UNIMPLEMENTED ("get allocation count for mem-pool"); + PoolHandle pool = PoolRegistry::locate (poolID); + return pool->getAllocationCnt(); } + /** calculate currently allotted Bytes for mem-pool */ size_t - TrackingAllocator::numBytes (Literal pool) + TrackingAllocator::numBytes (Literal poolID) { - UNIMPLEMENTED ("calc allotted Bytes for mem-pool"); + PoolHandle pool = PoolRegistry::locate (poolID); + return pool->calcAllocSize(); } diff --git a/src/lib/test/tracking-allocator.hpp b/src/lib/test/tracking-allocator.hpp index 26b936820..9c20c5113 100644 --- a/src/lib/test/tracking-allocator.hpp +++ b/src/lib/test/tracking-allocator.hpp @@ -75,7 +75,7 @@ namespace test { [[nodiscard]] void* allocate (size_t n); - void deallocate (void*) noexcept; + void deallocate (void*, size_t =0) noexcept; friend bool @@ -130,7 +130,7 @@ namespace test { TY* TrackAlloc::allocate (size_t cnt) { - UNIMPLEMENTED ("type-sized alloc"); + return static_cast (TrackingAllocator::allocate (cnt * sizeof(TY))); } /** @@ -141,79 +141,9 @@ namespace test { void TrackAlloc::deallocate (TY* loc, size_t cnt) noexcept { - UNIMPLEMENTED ("type-sized de-alloc"); + TrackingAllocator::deallocate (loc, cnt * sizeof(TY)); } - /** - * Placeholder implementation for a custom allocator - * @todo shall be replaced by an AllocationCluster eventually - * @todo 5/2024 to be reworked and aligned with a prospective C++20 Allocator Concept /////////////////////TICKET #1366 - * @remark using `std::list` container, since re-entrant allocation calls are possible, - * meaning that further allocations will be requested recursively from a ctor. - * Moreover, for the same reason we separate the allocation from the ctor call, - * so we can capture the address of the new allocation prior to any possible - * re-entrant call, and handle clean-up of allocation without requiring any - * additional state flags..... - */ - template - class AllocatorHandle - { - struct Allocation - { - alignas(TY) - std::byte buf_[sizeof(TY)]; - - template - TY& - create (ARGS&& ...args) - { - return *new(&buf_) TY {std::forward (args)...}; - } - - TY& - access() - { - return * std::launder (reinterpret_cast (&buf_)); - } - void - discard() /// @warning strong assumption made here: Payload was created - { - access().~TY(); - } - }; - - std::list storage_; - - public: - template - TY& - operator() (ARGS&& ...args) - { // EX_STRONG - auto pos = storage_.emplace (storage_.end()); ////////////////////////////////////////////////////TICKET #230 : real implementation should care for concurrency here - try { - return pos->create (std::forward (args)...); - } - catch(...) - { - storage_.erase (pos); // EX_FREE - - const char* errID = lumiera_error(); - ERROR (memory, "Allocation failed with unknown exception. " - "Lumiera errorID=%s", errID?errID:"??"); - throw; - } - } - - /** @note need to do explicit clean-up, since a ctor-call might have been failed, - * and we have no simple way to record this fact internally in Allocation, - * short of wasting additional memory for a flag to mark this situation */ - ~AllocatorHandle() - try { - for (auto& alloc : storage_) - alloc.discard(); - } - ERROR_LOG_AND_IGNORE (memory, "clean-up of custom AllocatorHandle") - }; diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index d888ae570..7769ca8db 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -83558,10 +83558,30 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -83587,17 +83607,26 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - - + + + + + + +

+ Name: TrackAlloc<TY> +

+ +
+ +
+ +
- - - +

verwende die Speicheradresse direkt als Hash-Wert; muß dazu eine Hasher-Funktion implementieren und in den Typ der Hashtable aufnehmen @@ -83609,12 +83638,18 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + + + + - - + + @@ -83626,8 +83661,8 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + @@ -83638,14 +83673,14 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + - - + + - - + +