2024-06-15 23:52:13 +02:00
|
|
|
/*
|
2024-06-16 19:31:16 +02:00
|
|
|
TrackingAllocator - custom allocator for memory management diagnostics
|
2024-06-15 23:52:13 +02:00
|
|
|
|
|
|
|
|
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.
|
2024-06-16 19:31:16 +02:00
|
|
|
** - PoolRegistry maintains a common hashtable with all MemoryPool instances;
|
|
|
|
|
** when using the standard accessors, registration and pool creating happens
|
|
|
|
|
** automatically.
|
|
|
|
|
** - in this setup, the pool with ID="GLOBAL" is just one further pool, which
|
|
|
|
|
** will be established when a front-end function is used with the default
|
|
|
|
|
** pool-ID (i.e. without explicit pool specification). Note however, that
|
|
|
|
|
** such a factory is still statefull (since it embeds a shared-ownership
|
|
|
|
|
** smart-pointer)
|
|
|
|
|
** - each MemoryPool contains a hashtable, where each active allocation is
|
|
|
|
|
** stored, using the storage-location as hashtable key. Each such entry
|
|
|
|
|
** gets a further consecutive internal ID, which is visible in the EventLog
|
2024-06-15 23:52:13 +02:00
|
|
|
** @see tracking-allocator.hpp
|
|
|
|
|
** @see TestTracking_test#demonstrate_checkAllocator()
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/tracking-allocator.hpp"
|
2024-06-16 19:31:16 +02:00
|
|
|
#include "lib/uninitialised-storage.hpp"
|
2024-06-16 03:22:32 +02:00
|
|
|
#include "lib/iter-explorer.hpp"
|
2024-06-15 23:52:13 +02:00
|
|
|
#include "lib/depend.hpp"
|
2024-06-16 02:36:37 +02:00
|
|
|
#include "lib/util.hpp"
|
2024-06-15 23:52:13 +02:00
|
|
|
|
|
|
|
|
|
2024-06-16 19:31:16 +02:00
|
|
|
#include <string>
|
2024-06-16 02:36:37 +02:00
|
|
|
#include <unordered_map>
|
2024-06-15 23:52:13 +02:00
|
|
|
|
2024-06-16 19:31:16 +02:00
|
|
|
using std::string;
|
2024-06-16 02:36:37 +02:00
|
|
|
using std::make_shared;
|
|
|
|
|
using std::make_pair;
|
|
|
|
|
using util::contains;
|
|
|
|
|
using util::joinDash;
|
2024-11-14 22:10:43 +01:00
|
|
|
using util::showAdr;
|
2024-06-15 23:52:13 +02:00
|
|
|
|
2024-06-16 19:31:16 +02:00
|
|
|
|
2024-06-15 23:52:13 +02:00
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
2024-06-16 02:36:37 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal registration and tracking of memory allocations handed out.
|
|
|
|
|
*/
|
|
|
|
|
class MemoryPool
|
|
|
|
|
: util::NonCopyable
|
|
|
|
|
{
|
|
|
|
|
// using the allocated memory location as key
|
|
|
|
|
using Location = void*;
|
|
|
|
|
struct LocationHash
|
|
|
|
|
{
|
|
|
|
|
HashVal operator() (Location const& loc) const { return HashVal(loc); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** registration entry to maintain a single allocation */
|
|
|
|
|
struct Allocation
|
|
|
|
|
: util::MoveOnly
|
|
|
|
|
{
|
|
|
|
|
UninitialisedDynBlock<byte> buff{};
|
2024-06-16 23:55:22 +02:00
|
|
|
size_t entryID{0};
|
2024-06-16 02:36:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using AllocTab = std::unordered_map<const Location, Allocation, LocationHash>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Literal poolID_;
|
2024-06-16 03:22:32 +02:00
|
|
|
AllocTab allocs_;
|
|
|
|
|
HashVal checksum_;
|
|
|
|
|
size_t entryNr_;
|
2024-06-16 02:36:37 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
MemoryPool (Literal id)
|
|
|
|
|
: poolID_{id}
|
|
|
|
|
, allocs_{}
|
2024-06-16 03:22:32 +02:00
|
|
|
, checksum_{0}
|
|
|
|
|
, entryNr_{0}
|
2024-06-16 02:36:37 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Allocation& addAlloc (size_t bytes);
|
2024-06-16 03:22:32 +02:00
|
|
|
void discardAlloc (void* loc, size_t);
|
|
|
|
|
|
2024-06-16 14:26:21 +02:00
|
|
|
Allocation const* findAlloc (Location) const;
|
|
|
|
|
|
2024-06-16 03:22:32 +02:00
|
|
|
HashVal getChecksum() const;
|
|
|
|
|
size_t getAllocationCnt() const;
|
|
|
|
|
size_t calcAllocSize() const;
|
2024-06-16 19:31:16 +02:00
|
|
|
|
|
|
|
|
Literal getPoolID() const { return poolID_; }
|
2024-06-16 02:36:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-06-15 23:52:13 +02:00
|
|
|
namespace { // implementation details for common memory management
|
|
|
|
|
|
2024-06-16 02:36:37 +02:00
|
|
|
/* === Logging primitives === */
|
|
|
|
|
|
|
|
|
|
inline EventLog& log() { return TrackingAllocator::log; }
|
|
|
|
|
|
|
|
|
|
template<typename...XS>
|
|
|
|
|
inline void
|
|
|
|
|
logAlarm (XS const& ...xs)
|
|
|
|
|
{
|
|
|
|
|
log().error (joinDash(xs...));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename...ARGS>
|
|
|
|
|
inline void
|
|
|
|
|
logAlloc (Literal pool, string fun, ARGS const& ...args)
|
|
|
|
|
{
|
|
|
|
|
log().call (pool,fun,args...);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 23:52:13 +02:00
|
|
|
|
2024-06-16 23:55:22 +02:00
|
|
|
/** keep track of any distinct memory pools used */
|
2024-06-16 02:36:37 +02:00
|
|
|
class PoolRegistry
|
|
|
|
|
: util::NonCopyable
|
2024-06-16 23:55:22 +02:00
|
|
|
{ // note: obsolete entries never discarded
|
|
|
|
|
using PoolTab = std::unordered_map<Literal, std::weak_ptr<MemoryPool>>;
|
|
|
|
|
|
|
|
|
|
PoolTab pools_{};
|
2024-06-16 02:36:37 +02:00
|
|
|
|
2024-06-16 23:55:22 +02:00
|
|
|
public:
|
2024-06-16 02:36:37 +02:00
|
|
|
static PoolHandle locate (Literal poolID);
|
2024-06-15 23:52:13 +02:00
|
|
|
|
2024-06-16 02:36:37 +02:00
|
|
|
private:
|
|
|
|
|
PoolHandle fetch_or_create (Literal poolID);
|
2024-06-15 23:52:13 +02:00
|
|
|
};
|
|
|
|
|
|
2024-06-16 23:55:22 +02:00
|
|
|
/** singleton for default pool */
|
2024-06-15 23:52:13 +02:00
|
|
|
Depend<MemoryPool> globalPool;
|
2024-06-16 02:36:37 +02:00
|
|
|
Depend<PoolRegistry> poolReg;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** static access front-end : locate (or create) a MemoryPool */
|
|
|
|
|
PoolHandle
|
|
|
|
|
PoolRegistry::locate (Literal poolID)
|
|
|
|
|
{
|
|
|
|
|
return poolReg().fetch_or_create (poolID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PoolHandle
|
|
|
|
|
PoolRegistry::fetch_or_create (Literal poolID)
|
|
|
|
|
{
|
2024-06-16 14:26:21 +02:00
|
|
|
auto& entry = pools_[poolID];
|
2024-06-16 02:36:37 +02:00
|
|
|
auto handle = entry.lock();
|
|
|
|
|
if (handle) return handle;
|
|
|
|
|
|
|
|
|
|
// create a new pool and enrol it for given ID
|
|
|
|
|
PoolHandle newPool = make_shared<MemoryPool> (poolID);
|
|
|
|
|
entry = newPool;
|
|
|
|
|
return newPool;
|
|
|
|
|
}
|
|
|
|
|
}//(End)Implementation memory pools and registration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-15 23:52:13 +02:00
|
|
|
TrackingAllocator::TrackingAllocator()
|
2024-06-16 02:36:37 +02:00
|
|
|
: mem_{PoolRegistry::locate(GLOBAL)}
|
2024-06-15 23:52:13 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
TrackingAllocator::TrackingAllocator (Literal id)
|
2024-06-16 02:36:37 +02:00
|
|
|
: mem_{PoolRegistry::locate(id)}
|
|
|
|
|
{ }
|
2024-06-15 23:52:13 +02:00
|
|
|
|
|
|
|
|
|
2024-06-16 19:31:16 +02:00
|
|
|
/**
|
2024-06-16 23:55:22 +02:00
|
|
|
* Allot a memory block of given size \a bytes.
|
2024-06-16 19:31:16 +02:00
|
|
|
* This allocation is recorded in the associated MemoryPool
|
|
|
|
|
* and proper deallocation can thus be verified.
|
|
|
|
|
* @return a `void*` to the start of the bare memory location
|
|
|
|
|
*/
|
|
|
|
|
TrackingAllocator::Location
|
|
|
|
|
TrackingAllocator::allocate (size_t bytes)
|
2024-06-15 23:52:13 +02:00
|
|
|
{
|
2024-06-16 03:22:32 +02:00
|
|
|
ENSURE (mem_);
|
2024-06-16 19:31:16 +02:00
|
|
|
return mem_->addAlloc (bytes)
|
2024-06-16 03:22:32 +02:00
|
|
|
.buff.front();
|
2024-06-15 23:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:31:16 +02:00
|
|
|
/**
|
|
|
|
|
* Discard and forget an allocation created through this allocator.
|
|
|
|
|
* The \a bytes argument serves as sanity check (since the actual allocation
|
|
|
|
|
* size is recorded anyway); a mismatch is logged as error, yet silently ignored.
|
|
|
|
|
*/
|
2024-06-15 23:52:13 +02:00
|
|
|
void
|
2024-06-16 19:31:16 +02:00
|
|
|
TrackingAllocator::deallocate (Location loc, size_t bytes) noexcept
|
2024-06-15 23:52:13 +02:00
|
|
|
{
|
2024-06-16 03:22:32 +02:00
|
|
|
ENSURE (mem_);
|
2024-06-16 19:31:16 +02:00
|
|
|
mem_->discardAlloc (loc, bytes);
|
2024-06-15 23:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-16 02:36:37 +02:00
|
|
|
MemoryPool::Allocation&
|
|
|
|
|
MemoryPool::addAlloc (size_t bytes)
|
|
|
|
|
{
|
|
|
|
|
Allocation newAlloc;
|
|
|
|
|
newAlloc.buff.allocate (bytes);
|
|
|
|
|
Location loc = newAlloc.buff.front();
|
|
|
|
|
ASSERT (not contains (allocs_, loc));
|
2024-06-16 03:22:32 +02:00
|
|
|
newAlloc.entryID = ++entryNr_;
|
2024-11-14 22:10:43 +01:00
|
|
|
logAlloc (poolID_, "allocate", bytes, newAlloc.entryID, showAdr(loc));
|
2024-06-18 17:20:23 +02:00
|
|
|
checksum_ += newAlloc.entryID * bytes;
|
2024-06-16 02:36:37 +02:00
|
|
|
return allocs_.emplace (loc, move(newAlloc))
|
|
|
|
|
.first->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2024-06-16 14:26:21 +02:00
|
|
|
MemoryPool::discardAlloc (Location loc, size_t bytes)
|
2024-06-16 02:36:37 +02:00
|
|
|
{
|
|
|
|
|
if (contains (allocs_, loc))
|
|
|
|
|
{
|
|
|
|
|
auto& entry = allocs_[loc];
|
|
|
|
|
ASSERT (entry.buff);
|
|
|
|
|
ASSERT (entry.buff.front() == loc);
|
2024-06-16 19:31:16 +02:00
|
|
|
if (entry.buff.size() != bytes) // *deliberately* tolerating wrong data, but log incident to support diagnostics
|
2024-11-14 22:10:43 +01:00
|
|
|
logAlarm ("SizeMismatch", bytes, "≠", entry.buff.size(), entry.entryID, showAdr(loc));
|
2024-06-16 19:31:16 +02:00
|
|
|
|
2024-11-14 22:10:43 +01:00
|
|
|
logAlloc (poolID_, "deallocate", bytes, entry.entryID, bytes, showAdr(loc));
|
2024-06-18 17:20:23 +02:00
|
|
|
checksum_ -= entry.entryID * bytes; // Note: using the given size (if wrong ⟿ checksum mismatch)
|
2024-06-16 02:36:37 +02:00
|
|
|
allocs_.erase(loc);
|
|
|
|
|
}
|
|
|
|
|
else // deliberately no exception here (for better diagnostics)
|
2024-11-14 22:10:43 +01:00
|
|
|
logAlarm ("FreeUnknown", bytes, showAdr(loc));
|
2024-06-16 03:22:32 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-16 23:55:22 +02:00
|
|
|
|
2024-06-16 14:26:21 +02:00
|
|
|
MemoryPool::Allocation const*
|
|
|
|
|
MemoryPool::findAlloc (Location loc) const
|
|
|
|
|
{
|
|
|
|
|
return contains (allocs_, loc)? & allocs_.at(loc)
|
|
|
|
|
: nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-16 03:22:32 +02:00
|
|
|
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();
|
2024-06-16 02:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-15 23:52:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ===== Diagnostics ===== */
|
|
|
|
|
|
2024-06-16 02:36:37 +02:00
|
|
|
EventLog TrackingAllocator::log{"test::TrackingAllocator"};
|
2024-06-15 23:52:13 +02:00
|
|
|
|
|
|
|
|
|
2024-06-16 23:55:22 +02:00
|
|
|
/** get Checksum for specific mem-pool */
|
2024-06-15 23:52:13 +02:00
|
|
|
HashVal
|
2024-06-16 03:22:32 +02:00
|
|
|
TrackingAllocator::checksum (Literal poolID)
|
2024-06-15 23:52:13 +02:00
|
|
|
{
|
2024-06-16 03:22:32 +02:00
|
|
|
PoolHandle pool = PoolRegistry::locate (poolID);
|
|
|
|
|
return pool->getChecksum();
|
2024-06-15 23:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-16 14:26:21 +02:00
|
|
|
/** determine number of active front-end handles */
|
|
|
|
|
size_t
|
|
|
|
|
TrackingAllocator::use_count (Literal poolID)
|
|
|
|
|
{
|
|
|
|
|
PoolHandle pool = PoolRegistry::locate (poolID);
|
|
|
|
|
return pool.use_count() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 23:55:22 +02:00
|
|
|
/** get active allocation count for mem-pool */
|
2024-06-15 23:52:13 +02:00
|
|
|
size_t
|
2024-06-16 03:22:32 +02:00
|
|
|
TrackingAllocator::numAlloc (Literal poolID)
|
2024-06-15 23:52:13 +02:00
|
|
|
{
|
2024-06-16 03:22:32 +02:00
|
|
|
PoolHandle pool = PoolRegistry::locate (poolID);
|
|
|
|
|
return pool->getAllocationCnt();
|
2024-06-15 23:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-16 03:22:32 +02:00
|
|
|
/** calculate currently allotted Bytes for mem-pool */
|
2024-06-15 23:52:13 +02:00
|
|
|
size_t
|
2024-06-16 03:22:32 +02:00
|
|
|
TrackingAllocator::numBytes (Literal poolID)
|
2024-06-15 23:52:13 +02:00
|
|
|
{
|
2024-06-16 03:22:32 +02:00
|
|
|
PoolHandle pool = PoolRegistry::locate (poolID);
|
|
|
|
|
return pool->calcAllocSize();
|
2024-06-15 23:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-16 14:26:21 +02:00
|
|
|
|
|
|
|
|
/** probe if this allocator pool did allocate the given memory location */
|
|
|
|
|
bool
|
|
|
|
|
TrackingAllocator::manages (Location memLoc) const
|
|
|
|
|
{
|
2024-06-16 19:31:16 +02:00
|
|
|
ENSURE (mem_);
|
2024-06-16 14:26:21 +02:00
|
|
|
return bool(mem_->findAlloc (memLoc));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** retrieve the registered size of this allocation, if known. */
|
|
|
|
|
size_t
|
|
|
|
|
TrackingAllocator::getSize(Location memLoc) const
|
|
|
|
|
{
|
2024-06-16 19:31:16 +02:00
|
|
|
ENSURE (mem_);
|
2024-06-16 14:26:21 +02:00
|
|
|
auto* entry = mem_->findAlloc (memLoc);
|
|
|
|
|
return entry? entry->buff.size()
|
|
|
|
|
: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** retrieve the internal registration ID for this allocation. */
|
|
|
|
|
HashVal
|
|
|
|
|
TrackingAllocator::getID (Location memLoc) const
|
|
|
|
|
{
|
2024-06-16 19:31:16 +02:00
|
|
|
ENSURE (mem_);
|
2024-06-16 14:26:21 +02:00
|
|
|
auto* entry = mem_->findAlloc (memLoc);
|
|
|
|
|
return entry? entry->entryID
|
|
|
|
|
: 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:31:16 +02:00
|
|
|
Literal
|
|
|
|
|
TrackingAllocator::poolID() const
|
|
|
|
|
{
|
|
|
|
|
ENSURE (mem_);
|
|
|
|
|
return mem_->getPoolID();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-15 23:52:13 +02:00
|
|
|
|
|
|
|
|
}} // namespace lib::test
|