Library: define requirements for tracking test-allocator

- ability to verify a hash-checksum
- ability to watch number of allocations and allotted bytes
- using either a common global pool or a separate dedicated pool
- log all operations into a common `EventLog` instance
- front-end adaptors for use as C++ custom allocator
This commit is contained in:
Fischlurch 2024-06-15 23:52:13 +02:00
parent e82dd86b39
commit ad90b7d687
5 changed files with 373 additions and 145 deletions

View file

@ -0,0 +1,120 @@
/*
TrackingAllocator - test dummy objects for tracking ctor/dtor calls
Copyright (C) Lumiera.org
2024, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
/** @file tracking-allocator.cpp
** Implementation of the common storage backend for the tracking test allocator.
**
** @see tracking-allocator.hpp
** @see TestTracking_test#demonstrate_checkAllocator()
**
*/
//#include "lib/test/test-helper.hpp"
#include "lib/test/tracking-allocator.hpp"
//#include "lib/format-string.hpp"
//#include "lib/format-cout.hpp"
//#include "lib/unique-malloc-owner.hpp"
#include "lib/depend.hpp"
#include "lib/uninitialised-storage.hpp"
//#include <string>
//using util::_Fmt;
//using std::string;
namespace lib {
namespace test{
namespace { // implementation details for common memory management
struct Allocation
{
UninitialisedDynBlock<byte> buff;
};
/**
* @internal registration and tracking of memory allocations handed out.
*/
class MemoryPool
{
};
Depend<MemoryPool> globalPool;
}
TrackingAllocator::TrackingAllocator()
: mem_{}
{ }
TrackingAllocator::TrackingAllocator (Literal id)
: mem_{}
{
UNIMPLEMENTED ("attach to specific pool");
}
void*
TrackingAllocator::allocate (size_t n)
{
UNIMPLEMENTED ("allocate memory block of size n");
}
void
TrackingAllocator::deallocate (void* loc) noexcept
{
UNIMPLEMENTED ("allocate memory block of size n");
}
/* ===== Diagnostics ===== */
EventLog TrackingAllocator::log{"test::TrackingAllocator"};
HashVal
TrackingAllocator::checksum (Literal pool)
{
UNIMPLEMENTED ("get Checksum for mem-pool");
}
size_t
TrackingAllocator::numAlloc (Literal pool)
{
UNIMPLEMENTED ("get allocation count for mem-pool");
}
size_t
TrackingAllocator::numBytes (Literal pool)
{
UNIMPLEMENTED ("calc allotted Bytes for mem-pool");
}
}} // namespace lib::test

View file

@ -32,151 +32,114 @@
#define LIB_TEST_TRACKING_ALLOCATOR_H
#include "lib/error.hpp"
#include "lib/nocopy.hpp"
#include "lib/hash-value.h"
#include "lib/symbol.hpp"
#include "lib/test/event-log.hpp"
#include <cstddef>
//#include <cstddef>
#include <utility>
#include <memory>
#include <list>
using std::byte;
namespace lib {
namespace allo {///< Concepts and Adaptors for custom memory management
namespace test {
/////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1366 : define Allocator Concepts here
/// TODO the following Concepts can be expected here (with C++20)
/// - Allocator : for the bare memory allocation
/// - Factory : for object fabrication and disposal
/// - Handle : a functor front-end to be dependency-injected
namespace { // common memory management for the TrackingAllocator
const Symbol GLOBAL{"GLOBAL"};
/**
* Adapter to implement the *Factory* concept based on a `std::allocator`
* @tparam ALO a std::allocator instance or anything compliant to [Allocator]
* [Allocator]: https://en.cppreference.com/w/cpp/named_req/Allocator
* @note in addition to the abilities defined by the standard, this adapter
* strives to provide some kind of _lateral leeway,_ attempting to
* create dedicated allocators for other types than the BaseType
* implied by the given \a ALO (standard-allocator).
* - this is possible if the rebound allocator can be constructed
* from the given base allocator
* - alternatively, an attempt will be made to default-construct
* the rebound allocator for the other type requested.
* @warning Both avenues for adaptation may fail,
* which could lead to compilation or runtime failure.
* @remark deliberately this class inherits from the allocator,
* allowing to exploit empty-base-optimisation, since
* usage of monostate allocators is quite common.
*/
template<class ALO>
class StdFactory
: private ALO
{
using Allo = ALO;
using AlloT = std::allocator_traits<Allo>;
using BaseType = typename Allo::value_type;
Allo& baseAllocator() { return *this; }
template<typename X>
auto
adaptAllocator()
{
using XAllo = typename AlloT::template rebind_alloc<X>;
if constexpr (std::is_constructible_v<XAllo, Allo>)
return XAllo{baseAllocator()};
else
return XAllo{};
}
template<class ALOT, typename...ARGS>
typename ALOT::pointer
construct (typename ALOT::allocator_type& allo, ARGS&& ...args)
{
auto loc = ALOT::allocate (allo, 1);
try { ALOT::construct (allo, loc, std::forward<ARGS>(args)...); }
catch(...)
{
ALOT::deallocate (allo, loc, 1);
throw;
}
return loc;
}
template<class ALOT>
void
destroy (typename ALOT::allocator_type& allo, typename ALOT::pointer elm)
{
ALOT::destroy (allo, elm);
ALOT::deallocate (allo, elm, 1);
}
public:
/**
* Create an instance of the adapter factory,
* forwarding to the embedded standard conforming allocator
* for object creation and destruction and memory management.
* @param allo (optional) instance of the C++ standard allocator
* used for delegation, will be default constructed if omitted.
* @remark the adapted standard allocator is assumed to be either a copyable
* value object, or even a mono-state; in both cases, a dedicated
* manager instance residing »elsewhere« is referred, rendering
* all those front-end instances exchangeable.
*/
StdFactory (Allo allo = Allo{})
: Allo{std::move (allo)}
{ }
template<class XALO>
bool constexpr operator== (StdFactory<XALO> const& o) const
{
return baseAllocator() == o.baseAllocator();
}
template<class XALO>
bool constexpr operator!= (StdFactory<XALO> const& o) const
{
return not (*this == o);
}
/** create new element using the embedded allocator */
template<class TY, typename...ARGS>
TY*
create (ARGS&& ...args)
{
if constexpr (std::is_same_v<TY, BaseType>)
{
return construct<AlloT> (baseAllocator(), std::forward<ARGS>(args)...);
}
else
{
using XAlloT = typename AlloT::template rebind_traits<TY>;
auto xAllo = adaptAllocator<TY>();
return construct<XAlloT> (xAllo, std::forward<ARGS>(args)...);
}
}
/** destroy the given element and discard the associated memory */
template<class TY>
void
dispose (TY* elm)
{
if constexpr (std::is_same_v<TY, BaseType>)
{
destroy<AlloT> (baseAllocator(), elm);
}
else
{
using XAlloT = typename AlloT::template rebind_traits<TY>;
auto xAllo = adaptAllocator<TY>();
destroy<XAlloT> (xAllo, elm);
}
}
};
/** common registration table and memory pool */
class MemoryPool;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1366 : the following code becomes obsolete in the long term
class TrackingAllocator
{
std::shared_ptr<MemoryPool> mem_;
public:
/** can be default created to attach to a common pool */
TrackingAllocator();
/** create a separate, marked memory pool */
TrackingAllocator (Literal id);
// standard copy operations acceptable
[[nodiscard]] void* allocate (size_t n);
void deallocate (void*) noexcept;
friend bool
operator== (TrackingAllocator const& a1, TrackingAllocator const& a2)
{
return a1.mem_ == a2.mem_;
}
friend bool
operator!= (TrackingAllocator const& a1, TrackingAllocator const& a2)
{
return not (a1 == a2);
}
/* ===== Diagnostics ===== */
static HashVal checksum (Literal pool =GLOBAL);
static size_t numAlloc (Literal pool =GLOBAL);
static size_t numBytes (Literal pool =GLOBAL);
static EventLog log;
};
template<typename TY>
class TrackAlloc
: public TrackingAllocator
{
public:
using TrackingAllocator::TrackingAllocator;
/** cross-building for another type, using a common pool */
template<typename X>
TrackAlloc (TrackAlloc<X> const& anchor)
: TrackingAllocator{anchor}
{ }
/* ===== C++ standard allocator interface ===== */
using value_type = TY;
[[nodiscard]] TY* allocate (size_t cnt);
void deallocate (TY*, size_t) noexcept;
};
/**
*
*/
template<typename TY>
TY*
TrackAlloc<TY>::allocate (size_t cnt)
{
UNIMPLEMENTED ("type-sized alloc");
}
/**
*
*/
template<typename TY>
void
TrackAlloc<TY>::deallocate (TY* loc, size_t cnt) noexcept
{
UNIMPLEMENTED ("type-sized de-alloc");
}
/**
* Placeholder implementation for a custom allocator
@ -251,5 +214,5 @@ namespace lib {
} // namespace lib
}} // namespace lib::test
#endif /*LIB_TEST_TRACKING_ALLOCATOR_H*/

View file

@ -84,7 +84,10 @@ namespace test{
operator= (Dummy && oDummy)
{
if (&oDummy != this)
swap (*this, oDummy);
{
swap (*this, oDummy);
oDummy.setVal(0);
}
return *this;
}

View file

@ -30,11 +30,14 @@
#include "lib/test/tracking-dummy.hpp"
#include "lib/test/tracking-allocator.hpp"
#include "lib/format-cout.hpp"
#include "lib/format-util.hpp"
#include "lib/test/diagnostic-output.hpp"///////////////////////TODO
#include <string>
using std::string;
using util::toString;
using util::join;
namespace lib {
@ -113,16 +116,81 @@ namespace test{
void
demonstrate_checkObject ()
{
UNIMPLEMENTED ("dummy");
CHECK (Dummy::checksum() == 0);
{
Dummy dum1; // picks a random positive int by default...
CHECK (0 < dum1.getVal() and dum1.getVal() <= 100'000'000);
CHECK (Dummy::checksum() == dum1.getVal());
Dummy dum2{55};
CHECK (55 == dum2.getVal());
CHECK (Dummy::checksum() == dum1.getVal() + 55);
Dummy dum3{move (dum2)};
CHECK (55 == dum3.getVal());
CHECK (0 == dum2.getVal());
dum3.setVal (23);
CHECK (23 == dum3.getVal());
dum1 = move (dum3);
CHECK (23 == dum1.getVal());
CHECK (0 == dum2.getVal());
CHECK (0 == dum3.getVal());
CHECK (Dummy::checksum() == 23);
Dummy::activateCtorFailure (true);
try {
Dummy kabooom;
}
catch (int v)
{
CHECK (0 < v and v <= 100'000'000);
CHECK (Dummy::checksum() == 23 + v);
Dummy::checksum() -= v;
}
Dummy::activateCtorFailure (false);
CHECK (23 == dum1.getVal());
CHECK (0 == dum2.getVal());
CHECK (0 == dum3.getVal());
CHECK (Dummy::checksum() == 23);
}
CHECK (Dummy::checksum() == 0);
}
/** @test custom allocator to track memory handling.
*/
void
demonstrate_checkAllocator ()
demonstrate_checkAllocator()
{
UNIMPLEMENTED ("allo");
// setup a common lock for the tracking objects and -allocator
auto& log = TrackingAllocator::log;
Tracker::log.clear("Tracking-Allocator-Test");
Tracker::log.joinInto(log);
CHECK (TrackingAllocator::checksum() == 0);
{
using SpyVec = std::vector<Tracker, TrackAlloc<Tracker>>;
SpyVec vec1(3);
int v3 = vec1.back().val;
SHOW_EXPR(v3);
SHOW_EXPR(join(vec1))
SpyVec vec2;
vec2.emplace_back (move (vec1[2]));
SHOW_EXPR(join(vec1))
SHOW_EXPR(join(vec2))
}
CHECK (TrackingAllocator::checksum() == 0);
cout << "____Tracking-Allo-Log_________\n"
<< util::join(Tracker::log, "\n")
<< "\n───╼━━━━━━━━━━━━━━━━━╾────────"<<endl;
}
};

View file

@ -65285,6 +65285,9 @@
<node CREATED="1716677865819" ID="ID_884450390" MODIFIED="1716677938838" TEXT="BlockFlow (f&#xfc;r Scheduler-Activities)">
<arrowlink COLOR="#504f86" DESTINATION="ID_1478014419" ENDARROW="Default" ENDINCLINATION="-665;-48;" ID="Arrow_ID_1014939669" STARTARROW="None" STARTINCLINATION="-1064;85;"/>
</node>
<node CREATED="1718495068144" ID="ID_1722995316" MODIFIED="1718495182358" TEXT="Tracking-Allocator f&#xfc;r Tests">
<arrowlink COLOR="#24309b" DESTINATION="ID_360498640" ENDARROW="Default" ENDINCLINATION="-850;-83;" ID="Arrow_ID_1221752034" STARTARROW="None" STARTINCLINATION="-909;93;"/>
</node>
</node>
</node>
<node CREATED="1696538097698" ID="ID_762902476" MODIFIED="1696538102051" TEXT="Concurrency">
@ -83514,7 +83517,9 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718368775878" ID="ID_1921994351" LINK="#ID_28139752" MODIFIED="1718368785941" TEXT="testen">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1718462814861" ID="ID_360498640" MODIFIED="1718462821406" TEXT="brauche einen Test-Allocator">
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1718462814861" ID="ID_360498640" MODIFIED="1718495176574" TEXT="brauche einen Test-Allocator">
<linktarget COLOR="#24309b" DESTINATION="ID_360498640" ENDARROW="Default" ENDINCLINATION="-850;-83;" ID="Arrow_ID_1221752034" SOURCE="ID_1722995316" STARTARROW="None" STARTINCLINATION="-909;93;"/>
<icon BUILTIN="pencil"/>
<node CREATED="1718462822608" ID="ID_1246144327" MODIFIED="1718462832939" TEXT="soll Heap-Allocations machen"/>
<node CREATED="1718462833600" ID="ID_148354832" MODIFIED="1718462843746" TEXT="aber jede Allocation registrieren"/>
<node CREATED="1718462844949" ID="ID_878000215" MODIFIED="1718462853370" TEXT="Schnittstelle: Standard-Allocator"/>
@ -83534,21 +83539,90 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node CREATED="1718467863033" ID="ID_577989623" MODIFIED="1718467879864" TEXT="aber wenn man verifyEvent mit dem type-Feld verwendet, geht&apos;s"/>
<node CREATED="1718467884100" ID="ID_629777074" MODIFIED="1718467902540" TEXT="naja...">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<head/>
<body>
<p>
diese erweiterten Event-Log-Matches sind wohl etwas ad hoc
</p>
</body>
</html>
</richcontent>
</html></richcontent>
</node>
</node>
</node>
<node CREATED="1718462887129" ID="ID_1798695685" MODIFIED="1718462911977" TEXT="demonstrate_checkObject"/>
<node CREATED="1718462887129" ID="ID_1322153003" MODIFIED="1718462969529" TEXT="demonstrate_checkAllocator"/>
<node COLOR="#338800" CREATED="1718462887129" ID="ID_1798695685" MODIFIED="1718486561234" TEXT="demonstrate_checkObject">
<icon BUILTIN="button_ok"/>
<node COLOR="#435e98" CREATED="1718486562974" ID="ID_354453585" MODIFIED="1718486605207" TEXT="runterprogrammiert nach Schema-F"/>
<node COLOR="#435e98" CREATED="1718486576684" ID="ID_1250760750" MODIFIED="1718486605208" TEXT="incl move-ctor und move-assign"/>
<node COLOR="#435e98" CREATED="1718486587442" ID="ID_78825133" MODIFIED="1718486605209" TEXT="change value"/>
<node COLOR="#435e98" CREATED="1718486592101" ID="ID_121488607" MODIFIED="1718486605209" TEXT="throw from ctor"/>
<node COLOR="#435e98" CREATED="1718486596443" ID="ID_1250015263" MODIFIED="1718486605209" TEXT="verify checksum"/>
</node>
<node CREATED="1718462887129" ID="ID_1322153003" MODIFIED="1718462969529" TEXT="demonstrate_checkAllocator">
<node CREATED="1718492999206" ID="ID_94881308" MODIFIED="1718493015632" TEXT="Vector an den globalen Pool ankoppeln"/>
<node CREATED="1718493041125" ID="ID_1894747169" MODIFIED="1718493584012" TEXT="anderen Vector an einen privaten Pool ankoppeln"/>
<node CREATED="1718493020625" ID="ID_149523402" MODIFIED="1718493030293" TEXT="einige Dummy-Objekte einf&#xfc;gen"/>
<node CREATED="1718493068940" ID="ID_943352942" MODIFIED="1718493073781" TEXT="Statistiken pr&#xfc;fen"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1718489410226" ID="ID_593247865" MODIFIED="1718495041962" TEXT="Design">
<icon BUILTIN="yes"/>
<node CREATED="1718489502694" ID="ID_1159188868" MODIFIED="1718489514638" TEXT="Backend: eine opaque Klasse MemoryPool">
<node CREATED="1718489522522" ID="ID_1654685241" MODIFIED="1718489535581" TEXT="f&#xfc;hrt eine Hashtable mit allen ausgegebenen Allokationen"/>
<node CREATED="1718489536409" ID="ID_1011933328" MODIFIED="1718489548355" TEXT="die einzel-Allokation ist ein UninitialisedDynBlock"/>
<node CREATED="1718489553206" ID="ID_110179143" MODIFIED="1718489581359" TEXT="key in der Hashtable: die Ziel-Adresse (Beginn des allozierten speichers)"/>
</node>
<node CREATED="1718489413681" ID="ID_3436449" MODIFIED="1718489426419" TEXT="das Front-End ist zugleich ein Standard-Konformer Allocator"/>
<node CREATED="1718489427280" ID="ID_392688098" MODIFIED="1718489445667" TEXT="default-konstruiert &#x27f9; h&#xe4;ngt sich an den globalen Pool"/>
<node CREATED="1718489446398" ID="ID_483466406" MODIFIED="1718489459127" TEXT="mit ID konstruiert &#x27f9; macht einen separaten Pool auf"/>
<node CREATED="1718489460851" ID="ID_1289960058" MODIFIED="1718489472206" TEXT="kopierbar / verschiebbar &#x27f9; &#xfc;bernimmt Pool-Zugeh&#xf6;rigkeit"/>
<node CREATED="1718489474505" ID="ID_152241765" MODIFIED="1718489497534" TEXT="kreuz-Konstruierbar &#x27f9; h&#xe4;ngt sich an den bestehenden Pool an"/>
<node CREATED="1718489589458" ID="ID_1502607235" MODIFIED="1718489597940" TEXT="Informations-Funktionen">
<node CREATED="1718489601567" ID="ID_973775208" MODIFIED="1718489609800" TEXT="eine gemeinsame EventLog-Instanz"/>
<node CREATED="1718489611591" ID="ID_1909546439" MODIFIED="1718489675778" TEXT="eine Checksumme f&#xfc;r jeden Pool"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718489712994" ID="ID_1366290941" MODIFIED="1718489752146" TEXT="Implementierung">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1718489717485" ID="ID_1890743066" MODIFIED="1718489729339" TEXT="das Front-End ist ein verpackter Shared-Ptr"/>
<node CREATED="1718491430872" ID="ID_146190908" MODIFIED="1718491450055" TEXT="brauche eine getypte Sub-Klasse als Standard-Allocator(Adapter)">
<node CREATED="1718491670798" ID="ID_846381324" MODIFIED="1718491676634" TEXT="Name: TrackAlloc&lt;TY&gt;"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494826583" ID="ID_721892573" MODIFIED="1718494833802" TEXT="delegierende impl">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494843824" ID="ID_15578546" MODIFIED="1718494857263" TEXT="MemoryPool f&#xfc;hrt eine Hashtable aller Allokationen">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494864443" ID="ID_1936586349" MODIFIED="1718494876166" TEXT="Allokation belegen und freigeben">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718489730263" ID="ID_5464621" MODIFIED="1718494857263" TEXT="der MemoryPool verwendet Mutex-Locking">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494880219" ID="ID_1016934813" MODIFIED="1718494895130" TEXT="Meta-Registry aller Pools">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494895881" ID="ID_1075022808" MODIFIED="1718494905847" TEXT="Zugriff auf Pool &#xfc;ber den Namen">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494907182" ID="ID_1199907969" MODIFIED="1718494918916" TEXT="Informations-Funktionen">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494920027" ID="ID_1244146950" MODIFIED="1718495028271" TEXT="Checksumme f&#xfc;r einen Pool">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494938195" ID="ID_1072075538" MODIFIED="1718495028271" TEXT="Anzahl aktive Allokationen pro Pool">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494948897" ID="ID_220648009" MODIFIED="1718495028272" TEXT="Summe belegter Bytes berechnen">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718494960176" ID="ID_1007231429" MODIFIED="1718495028272" TEXT="Zugriff auf belegte Bytes f&#xfc;r Einzel-Allokation">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1718495002832" ID="ID_992819817" MODIFIED="1718495028273" TEXT="Gesamtsummen berechnen">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
</node>
</node>