2011-11-07 00:07:53 +01:00
/*
2026-02-10 23:48:09 +01:00
HeapMemBufferStore - plain heap allocating BufferProvider storage implementation for tests
2011-11-07 00:07:53 +01:00
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
Copyright ( C )
2026-02-10 23:48:09 +01:00
2011 , 2026 Hermann Vosseler < Ichthyostega @ web . de >
2011-11-07 00:07:53 +01:00
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* * Lumiera * * 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 . See the file COPYING for further details .
2011-11-07 00:07:53 +01:00
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-11-07 00:07:53 +01:00
2026-02-10 23:48:09 +01:00
/** @file heap-mem-buffer-store.cpp
* * Implementation details of simple heap based engine : : BufferProvider storage .
2016-11-03 18:20:10 +01:00
*/
2011-11-07 00:07:53 +01:00
# include "lib/error.hpp"
# include "include/logging.h"
2011-11-19 17:51:08 +01:00
# include "lib/scoped-ptrvect.hpp"
2011-11-13 04:20:14 +01:00
# include "lib/util-foreach.hpp"
2011-11-07 00:07:53 +01:00
2026-02-10 23:48:09 +01:00
# include "steam/engine/heap-mem-buffer-store.hpp"
2011-11-07 00:07:53 +01:00
2011-11-18 01:23:50 +01:00
# include <algorithm>
2011-11-13 04:20:14 +01:00
# include <vector>
2011-11-07 00:07:53 +01:00
2011-11-19 17:51:08 +01:00
using util : : and_all ;
2011-11-13 04:20:14 +01:00
using std : : vector ;
2011-11-19 17:51:08 +01:00
using lib : : ScopedPtrVect ;
2011-11-07 00:07:53 +01:00
2018-11-15 23:55:13 +01:00
namespace steam {
2011-11-07 00:07:53 +01:00
namespace engine {
2011-11-15 04:47:31 +01:00
namespace error = lumiera : : error ;
2024-07-28 19:29:27 +02:00
using Buff = StreamType : : ImplFacade : : DataBuffer ;
2011-11-19 17:51:08 +01:00
2026-02-12 02:28:41 +01:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 1410 : need to question what implementation structures are needed, after »tracking« was extracted...
/**
* Helper for implementing a diagnostic BufferProvider :
* A block of heap allocated storage , with the capability
* to store some additional tracking information .
*/
class HeapMemProvider : : Block
: util : : NonCopyable
{
unique_ptr < char [ ] > storage_ ;
bool was_released_ ;
public :
explicit
Block ( size_t bufferSize )
: storage_ ( bufferSize ? new char [ bufferSize ] : NULL )
, was_released_ ( false )
{ }
bool
was_used ( ) const
{
return bool ( storage_ ) ;
}
bool
was_closed ( ) const
{
return was_released_ ;
}
void *
accessMemory ( ) const
{
REQUIRE ( storage_ , " Block was never prepared for use " ) ;
return storage_ . get ( ) ;
}
void
markReleased ( )
{
was_released_ = true ;
}
} ;
2024-12-20 01:47:40 +01:00
namespace { // implementation helpers...
2011-11-19 17:51:08 +01:00
2024-07-28 19:29:27 +02:00
inline Buff *
asBuffer ( void * mem )
{ // type tag to mark memory address as Buffer
return static_cast < Buff * > ( mem ) ;
}
2026-02-12 02:28:41 +01:00
using Block = HeapMemProvider : : Block ;
2011-11-19 17:51:08 +01:00
/** helper to find Block entries
2011-11-21 03:26:08 +01:00
* based on their raw memory address */
2011-11-19 17:51:08 +01:00
inline bool
identifyBlock ( Block const & inQuestion , void * storage )
{
2011-11-21 03:26:08 +01:00
return storage = = & inQuestion ;
2011-11-19 17:51:08 +01:00
}
/** build a searching predicate */
inline function < bool ( Block const & ) >
search_for_block_using_this_storage ( void * storage )
{
return bind ( identifyBlock , _1 , storage ) ;
}
template < class VEC >
inline Block *
2011-11-21 03:26:08 +01:00
pick_Block_by_storage ( VEC & vec , void * blockLocation )
2011-11-19 17:51:08 +01:00
{
typename VEC : : iterator pos
= std : : find_if ( vec . begin ( ) , vec . end ( )
2011-11-21 03:26:08 +01:00
, search_for_block_using_this_storage ( blockLocation ) ) ;
2011-11-19 17:51:08 +01:00
if ( pos ! = vec . end ( ) )
return & ( * pos ) ;
else
return NULL ;
}
}
2026-02-12 02:28:41 +01:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 1410 : need to question what implementation structures are needed, after »tracking« was extracted...
2011-11-13 04:20:14 +01:00
2026-02-12 02:28:41 +01:00
using PoolBlocks = ScopedPtrVect < HeapMemProvider : : Block > ;
2011-11-13 04:20:14 +01:00
/**
* @ internal Pool of allocated buffer Blocks of a specific size .
* Helper for implementing a Diagnostic BufferProvider ; actually does
* just heap allocations for the Blocks , but keeps a collection of
* allocated Blocks around . Individual entries can be retrieved
* and thus removed from the responsibility of BlockPool .
*
* The idea is that each buffer starts its lifecycle within some pool
* and later gets " emitted " to an output sequence , where it remains for
* later investigation and diagnostics .
*/
2026-02-12 02:28:41 +01:00
class HeapMemProvider : : BlockPool
2011-11-13 04:20:14 +01:00
{
2011-11-20 01:35:52 +01:00
uint maxAllocCount_ ;
2011-11-13 04:20:14 +01:00
size_t memBlockSize_ ;
2026-02-12 02:28:41 +01:00
using PoolBlocks = ScopedPtrVect < Block > ;
2025-05-31 19:43:43 +02:00
PoolBlocks blockList_ ;
2011-11-13 04:20:14 +01:00
public :
BlockPool ( )
2011-11-20 01:35:52 +01:00
: maxAllocCount_ ( 0 ) // unlimited by default
, memBlockSize_ ( 0 )
2025-05-31 19:43:43 +02:00
, blockList_ { }
2011-11-13 04:20:14 +01:00
{ }
void
initialise ( size_t blockSize )
{
2011-11-15 04:47:31 +01:00
memBlockSize_ = blockSize ;
2011-11-13 04:20:14 +01:00
}
// standard copy operations are valid, but will
// raise an runtime error, once BlockPool is initialised.
~ BlockPool ( )
2011-11-19 17:51:08 +01:00
{
2011-11-20 01:35:52 +01:00
if ( ! verify_all_children_idle ( ) )
2011-11-19 17:51:08 +01:00
ERROR ( test , " Block actively in use while shutting down BufferProvider "
" allocation pool. This might lead to Segfault and memory leaks. " ) ;
}
2024-12-20 01:47:40 +01:00
/** mark all managed blocks as disposed */
void
discard ( )
{
2025-05-31 19:43:43 +02:00
for ( Block & block : blockList_ )
block . markReleased ( ) ;
2024-12-20 01:47:40 +01:00
}
2011-11-13 04:20:14 +01:00
2024-12-20 01:47:40 +01:00
uint
2011-11-20 01:35:52 +01:00
prepare_for ( uint number_of_expected_buffers )
{
2024-12-20 01:47:40 +01:00
if ( maxAllocCount_ & &
2025-05-31 19:43:43 +02:00
maxAllocCount_ < blockList_ . size ( ) + number_of_expected_buffers )
2011-11-20 01:35:52 +01:00
{
2025-05-31 19:43:43 +02:00
ASSERT ( maxAllocCount_ > = blockList_ . size ( ) ) ;
return maxAllocCount_ - blockList_ . size ( ) ;
2011-11-20 01:35:52 +01:00
}
// currently no hard limit imposed
return number_of_expected_buffers ;
}
2011-11-13 04:20:14 +01:00
Block &
createBlock ( )
{
2025-05-31 19:43:43 +02:00
return blockList_ . manage ( new Block ( memBlockSize_ ) ) ;
2011-11-13 04:20:14 +01:00
}
2011-11-18 01:23:50 +01:00
Block *
2011-11-21 03:26:08 +01:00
find ( void * blockLocation )
2011-11-18 01:23:50 +01:00
{
2025-05-31 19:43:43 +02:00
return pick_Block_by_storage ( blockList_ , blockLocation ) ;
2011-11-18 01:23:50 +01:00
}
2011-11-14 01:43:29 +01:00
Block *
2011-11-13 04:20:14 +01:00
transferResponsibility ( Block * allocatedBlock )
{
2025-05-31 19:43:43 +02:00
return blockList_ . detach ( allocatedBlock ) ;
2011-11-13 04:20:14 +01:00
}
size_t
size ( ) const
{
2025-05-31 19:43:43 +02:00
return blockList_ . size ( ) ;
2011-11-13 04:20:14 +01:00
}
2011-11-18 01:23:50 +01:00
bool
isValid ( ) const
{
2025-05-31 19:43:43 +02:00
return not blockList_ . empty ( ) ;
2017-04-02 06:33:56 +02:00
}
explicit
operator bool ( ) const
{
return isValid ( ) ;
2011-11-18 01:23:50 +01:00
}
2011-11-13 04:20:14 +01:00
private :
2011-11-19 17:51:08 +01:00
bool
2011-11-20 01:35:52 +01:00
verify_all_children_idle ( )
2011-11-19 17:51:08 +01:00
{
try {
2025-05-31 19:43:43 +02:00
return and_all ( blockList_ , is_in_sane_state ) ;
2011-11-19 17:51:08 +01:00
}
ERROR_LOG_AND_IGNORE ( test , " State verification of diagnostic BufferProvider allocation pool " ) ;
return true ;
}
static bool
is_in_sane_state ( Block const & block )
2011-11-13 04:20:14 +01:00
{
2015-09-25 02:38:59 +02:00
return not block . was_used ( ) or block . was_closed ( ) ;
2011-11-13 04:20:14 +01:00
}
} ;
2026-02-12 02:28:41 +01:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 1410 : need to question what implementation structures are needed, after »tracking« was extracted...
2011-11-07 00:07:53 +01:00
2024-12-20 01:47:40 +01:00
2011-11-07 00:07:53 +01:00
namespace { // Details of allocation and accounting
const uint MAX_BUFFERS = 50 ;
2011-11-21 03:26:08 +01:00
2026-02-12 02:28:41 +01:00
Block emptyPlaceholder ( 0 ) ;
2011-11-07 00:07:53 +01:00
} // (END) Details of allocation and accounting
/**
* @ internal create a memory tracking BufferProvider ,
*/
2026-02-08 22:58:07 +01:00
HeapMemProvider : : HeapMemProvider ( )
2026-02-12 02:28:41 +01:00
: pool_ ( new PoolTable )
2011-11-18 01:23:50 +01:00
, outSeq_ ( )
2011-11-07 00:07:53 +01:00
{ }
2026-02-08 23:44:17 +01:00
HeapMemProvider : : ~ HeapMemProvider ( ) { /* emit dtor of BlockPool here */ }
2011-11-07 00:07:53 +01:00
/* ==== Implementation of the BufferProvider interface ==== */
uint
2026-02-12 22:25:32 +01:00
HeapMemProvider : : prepareBuffers ( uint numBuffers , size_t buffSiz , HashVal typeID )
2011-11-07 00:07:53 +01:00
{
2026-02-12 22:25:32 +01:00
BlockPool & responsiblePool = getBlockPoolFor ( buffSiz , typeID ) ;
return responsiblePool . prepare_for ( numBuffers ) ;
2011-11-07 00:07:53 +01:00
}
BufferProvider: consolidate information passed to BufferStore
This change largely resolves the problems with the storage API,
so that the implementation can now be split in two disjoint segments.
One operation, `mark_emitted()`, is still misplaced on that API, yet
must be retained until the refactoring on the metadata side is complete
(this latter refactoring can not be started sensibly, before the old
»tracking« implementation is cleaned up to care for allocation solely).
With this change, the existing implementation, which combines the
tracking functionality and the allocation, has been squeezed down
into the heap-memory implementation class, yet the tracking functionality
is already detached. The remaining code down there is a huge pile of mess,
that can hopefully be dismantled soon. But at least all of this code
passes the compiler again, and so it should be possible now to focus
on getting the `DiagnosticBufferProvider` up and running, thereby
providing a new clean re-implementation of the diagnostic functions.
2026-02-12 23:43:35 +01:00
Buff &
HeapMemProvider : : provideBuffer ( size_t buffSiz , HashVal typeID , LocalTag & )
2011-11-07 00:07:53 +01:00
{
2026-02-12 22:25:32 +01:00
BlockPool & blocks = getBlockPoolFor ( buffSiz , typeID ) ;
2026-02-12 02:28:41 +01:00
Block & newBlock = blocks . createBlock ( ) ;
BufferProvider: consolidate information passed to BufferStore
This change largely resolves the problems with the storage API,
so that the implementation can now be split in two disjoint segments.
One operation, `mark_emitted()`, is still misplaced on that API, yet
must be retained until the refactoring on the metadata side is complete
(this latter refactoring can not be started sensibly, before the old
»tracking« implementation is cleaned up to care for allocation solely).
With this change, the existing implementation, which combines the
tracking functionality and the allocation, has been squeezed down
into the heap-memory implementation class, yet the tracking functionality
is already detached. The remaining code down there is a huge pile of mess,
that can hopefully be dismantled soon. But at least all of this code
passes the compiler again, and so it should be possible now to focus
on getting the `DiagnosticBufferProvider` up and running, thereby
providing a new clean re-implementation of the diagnostic functions.
2026-02-12 23:43:35 +01:00
LocalTag specifics { & newBlock } ; // used by this implementation to find the storage to release later
return * asBuffer ( newBlock . accessMemory ( ) ) ;
2011-11-07 00:07:53 +01:00
}
2011-11-08 02:59:56 +01:00
void
2026-02-12 22:25:32 +01:00
HeapMemProvider : : mark_emitted ( size_t buffSiz , HashVal typeID , LocalTag const & specifics )
2011-11-08 02:59:56 +01:00
{
2026-02-12 22:25:32 +01:00
Block * block4buffer = locateBlock ( buffSiz , typeID , specifics ) ;
2011-11-15 04:47:31 +01:00
if ( ! block4buffer )
throw error : : Logic ( " Attempt to emit a buffer not known to this BufferProvider "
, LUMIERA_ERROR_BUFFER_MANAGEMENT ) ;
2026-02-12 22:25:32 +01:00
BlockPool & pool = getBlockPoolFor ( buffSiz , typeID ) ;
2025-02-09 01:44:45 +01:00
Block * active = pool . transferResponsibility ( block4buffer ) ;
if ( active )
outSeq_ . manage ( active ) ;
else
if ( block4buffer - > was_closed ( ) )
WARN ( proc_mem , " Attempt to emit() an already closed buffer. " ) ;
else
WARN ( proc_mem , " Attempt to emit() a buffer not found in active pool. "
" Maybe duplicate call to emit()? " ) ;
2011-11-08 02:59:56 +01:00
}
2011-11-14 01:43:29 +01:00
/** mark a buffer as officially discarded */
2011-11-07 00:07:53 +01:00
void
BufferProvider: consolidate information passed to BufferStore
This change largely resolves the problems with the storage API,
so that the implementation can now be split in two disjoint segments.
One operation, `mark_emitted()`, is still misplaced on that API, yet
must be retained until the refactoring on the metadata side is complete
(this latter refactoring can not be started sensibly, before the old
»tracking« implementation is cleaned up to care for allocation solely).
With this change, the existing implementation, which combines the
tracking functionality and the allocation, has been squeezed down
into the heap-memory implementation class, yet the tracking functionality
is already detached. The remaining code down there is a huge pile of mess,
that can hopefully be dismantled soon. But at least all of this code
passes the compiler again, and so it should be possible now to focus
on getting the `DiagnosticBufferProvider` up and running, thereby
providing a new clean re-implementation of the diagnostic functions.
2026-02-12 23:43:35 +01:00
HeapMemProvider : : detachBuffer ( size_t buffSiz , HashVal typeID , LocalTag specifics , Buff & storage )
2011-11-07 00:07:53 +01:00
{
2026-02-12 22:25:32 +01:00
Block * block4buffer = locateBlock ( buffSiz , typeID , specifics ) ;
2011-11-15 04:47:31 +01:00
REQUIRE ( block4buffer , " releasing a buffer not allocated through this provider " ) ;
2024-11-14 22:10:43 +01:00
REQUIRE ( util : : isSameAdr ( storage , block4buffer - > accessMemory ( ) ) ) ;
2011-11-14 01:43:29 +01:00
block4buffer - > markReleased ( ) ;
2011-11-07 00:07:53 +01:00
}
/* ==== Implementation details ==== */
2011-11-18 01:23:50 +01:00
size_t
2026-02-08 22:58:07 +01:00
HeapMemProvider : : emittedCnt ( ) const
2011-11-18 01:23:50 +01:00
{
return outSeq_ . size ( ) ;
}
2024-12-20 01:47:40 +01:00
void
2026-02-08 22:58:07 +01:00
HeapMemProvider : : markAllEmitted ( )
2024-12-20 01:47:40 +01:00
{
for ( auto & [ _ , blockPool ] : * pool_ )
blockPool . discard ( ) ;
}
2026-02-12 02:28:41 +01:00
HeapMemProvider : : Block &
2026-02-08 22:58:07 +01:00
HeapMemProvider : : access_emitted ( uint bufferID )
2011-11-07 00:07:53 +01:00
{
2011-11-21 03:26:08 +01:00
if ( ! withinOutputSequence ( bufferID ) )
2011-12-09 01:03:02 +01:00
return emptyPlaceholder ; ////////////////////////////////TICKET #856
2011-11-21 03:26:08 +01:00
else
return outSeq_ [ bufferID ] ;
2011-11-07 00:07:53 +01:00
}
bool
2026-02-08 22:58:07 +01:00
HeapMemProvider : : withinOutputSequence ( uint bufferID ) const
2011-11-07 00:07:53 +01:00
{
if ( bufferID > = MAX_BUFFERS )
throw error : : Fatal ( " hardwired internal limit for test buffers exceeded " ) ;
2011-11-18 01:23:50 +01:00
return bufferID < outSeq_ . size ( ) ;
2011-11-07 00:07:53 +01:00
}
2026-02-12 02:28:41 +01:00
HeapMemProvider : : BlockPool &
2026-02-12 22:25:32 +01:00
HeapMemProvider : : getBlockPoolFor ( size_t buffSiz , HashVal typeID )
2011-11-15 04:47:31 +01:00
{
2026-02-12 02:28:41 +01:00
BlockPool & pool = ( * pool_ ) [ typeID ] ;
2026-02-12 22:25:32 +01:00
if ( not pool )
pool . initialise ( buffSiz ) ;
2011-11-18 01:23:50 +01:00
return pool ;
2011-11-15 04:47:31 +01:00
}
2026-02-12 02:28:41 +01:00
HeapMemProvider : : Block *
2026-02-12 22:25:32 +01:00
HeapMemProvider : : locateBlock ( size_t buffSiz , HashVal typeID , void * storage )
2011-11-14 01:43:29 +01:00
{
2026-02-12 22:25:32 +01:00
BlockPool & pool = getBlockPoolFor ( buffSiz , typeID ) ;
2026-02-12 02:28:41 +01:00
Block * block4buffer = pool . find ( storage ) ; ////////////////////////////////TICKET #856
2011-11-18 01:23:50 +01:00
return block4buffer ? block4buffer
: searchInOutSeqeuence ( storage ) ;
}
2026-02-12 02:28:41 +01:00
HeapMemProvider : : Block *
2026-02-08 22:58:07 +01:00
HeapMemProvider : : searchInOutSeqeuence ( void * blockLocation )
2011-11-18 01:23:50 +01:00
{
2011-12-08 23:02:19 +01:00
return pick_Block_by_storage ( outSeq_ , blockLocation ) ; ////////////////////////////////TICKET #856
2011-11-14 01:43:29 +01:00
}
2011-11-07 00:07:53 +01:00
2011-12-02 16:10:03 +01:00
} } // namespace engine