1
0
forked from Alepha/Alepha

Start moving towards the alignment solution>

Some of this needs to be branched out.
This commit is contained in:
2024-05-15 22:26:13 -04:00
parent 9bd1772fe2
commit 7070a9364c
3 changed files with 16 additions and 9 deletions

View File

@ -8,9 +8,10 @@ static_assert( __cplusplus > 2020'99 );
#include <memory> #include <memory>
#include <Alepha/swappable.h>
#include <Alepha/Exception.h>
#include "Buffer.h" #include "Buffer.h"
#include "swappable.h"
#include "Exception.h"
//#include "threading.h" //#include "threading.h"
#include "error.h" #include "error.h"
@ -221,11 +222,17 @@ namespace Alepha::Hydrogen ::detail:: Blob_m
* inside a large single physical backing. This helps maintain zero-copy semantics. * inside a large single physical backing. This helps maintain zero-copy semantics.
* *
* @param amount The amount of data to carve off. * @param amount The amount of data to carve off.
* @return A new `Blob` object referring to the same physical data, scoped to `amount` bytes. * @param alignment The size alignment that the new base should be at (the extra padding is
* considered part of the resulting `Blob`).
* @return A new `Blob` object referring to the same physical data, scoped to `amount` bytes (with
* possible extra space, due to alignment).
*/ */
Blob Blob
carveHead( const std::size_t amount ) carveHead( const std::size_t unalignedAmount, Alignment alignment= Alignment{ 1 } )
{ {
assert( std::popcount( alignment.amt ) == 1 );
// TODO: Consider arithmetic overflow, here.
const std::size_t amount= alignTo( ( reinterpret_cast< std::uintptr_t >( data() ) % alignment.amt ) + unalignedAmount, alignment );
if( amount > size() ) throw DataCarveTooLargeError( data(), amount, size() ); if( amount > size() ) throw DataCarveTooLargeError( data(), amount, size() );
if( not storage ) if( not storage )
{ {

View File

@ -52,7 +52,7 @@ namespace Alepha::inline Cavorite ::detail:: DataChain_m
friend DataChain; friend DataChain;
explicit Iterator( const ChainIter pos, cosnt std::size_t offset ) noexcept : pos( pos ), offset( offset ) {} explicit Iterator( const ChainIter pos, const std::size_t offset ) noexcept : pos( pos ), offset( offset ) {}
public: public:
auto auto
@ -165,7 +165,7 @@ namespace Alepha::inline Cavorite ::detail:: DataChain_m
std::copy_n( std::prev( end(), amount ), amount, rv.byte_data() ); std::copy_n( std::prev( end(), amount ), amount, rv.byte_data() );
return rv; return rv;
}
}; };
}; };
} }

View File

@ -58,15 +58,15 @@ namespace Alepha::Hydrogen::Memory ::detail:: ThreadSlab_m
{ {
// TODO: Alignment? // TODO: Alignment?
const std::size_t req= amt + sizeof( Blob::StorageReservation ); const std::size_t req= amt + sizeof( Blob::StorageReservation );
if( req > C::slabSize ) throw std::bad_alloc{}; //{ "Unable to allocate larger than the slab size." };
if( slab.size() < req ) slab.reset( std::max( req, C::slabSize ) ); if( slab.size() < req ) slab.reset( std::max( req, C::slabSize ) );
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when " std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " made an allocation." << std::endl; << (void *) this << " made an allocation." << std::endl;
auto next= slab.carveHead( req ); auto next= slab.carveHead( req, Alignment{ sizeof( Blob::StorageReservation ) } );
new ( &next.template as< Blob::StorageReservation >() ) Blob::StorageReservation{ next.reservation() };
const auto rv= reinterpret_cast< T * >( &next.template as< Blob::StorageReservation >() + 1 ); const auto rv= reinterpret_cast< T * >( &next.template as< Blob::StorageReservation >() + 1 );
next.reset(); new ( &next.template as< Blob::StorageReservation >() ) Blob::StorageReservation{ std::move( next.reservation() ) };
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when " std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " made an allocation." << std::endl; << (void *) this << " made an allocation." << std::endl;