45#ifndef __ADS_MappedFile
46 #define __ADS_MappedFile
59#include <initializer_list>
85 using std::runtime_error::runtime_error;
121 [[nodiscard]] std::span< std::byte >
bytes(
void )
const;
124 [[nodiscard]]
explicit operator bool(
void )
const {
return( f_r ); }
130 [[nodiscard]]
bool holds(
const void * p )
const;
135 void offer(
void * p , std::size_t m )
const;
138 [[nodiscard]]
void *
take( std::size_t m )
const;
142 return( a.f_r == b.f_r );
148 static constexpr std::size_t kPage = 4096;
153 std::atomic< std::size_t > refs{ 1 };
154 std::byte * base =
nullptr;
155 std::size_t size = 0;
157 void * handle =
nullptr;
158 void ( *drop )( Region & ) =
nullptr;
159 void * offered =
nullptr;
160 std::size_t offeredSize = 0;
164 void release(
void )
noexcept;
166 Region * f_r =
nullptr;
175[[nodiscard]]
constexpr std::uint64_t
176persistMix( std::initializer_list< std::uint64_t > v )
178 std::uint64_t h = 0x9E3779B97F4A7C15ULL;
179 for(
const std::uint64_t x : v ) {
180 h ^= x + 0x9E3779B97F4A7C15ULL + ( h << 6 ) + ( h >> 2 );
181 h *= 0xBF58476D1CE4E5B9ULL;
188[[nodiscard]]
constexpr std::uint64_t persistHash( std::string_view s )
190 std::uint64_t h = 0xCBF29CE484222325ULL;
191 for(
const char c : s )
192 h = ( h ^ std::uint64_t(
static_cast< unsigned char >( c ) ) ) *
207template<
class T ,
bool V = true >
232 : f_map( std::move( m ) ) { }
235 : f_map( o.f_map ) { }
245 if(
void * p = f_map.take( n *
sizeof( T ) ) )
246 return(
static_cast< T *
>( p ) );
247 return( std::allocator< T >().
allocate( n ) );
251 if( f_map && f_map.holds( p ) )
253 std::allocator< T >().deallocate( p , n );
259 template<
class U ,
class... A >
261 if constexpr( (
sizeof...( A ) == 0 ) && std::is_trivially_copyable_v< U > )
262 if( f_map && f_map.holds( p ) )
264 if constexpr( (
sizeof...( A ) == 0 ) && ! V )
265 ::new(
static_cast< void *
>( p ) ) U;
267 ::new(
static_cast< void *
>( p ) ) U( std::forward< A >( a )... );
273 return( a.f_map == b.f_map );
277 template<
class U ,
bool W >
292#include "ADS/external/impl/MappedFile.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
std::allocator, which also makes a vector over the bytes of a mapped file [see restoreMapped()]; an i...
Definition MappedFile.h:209
MappableAllocator(MappedFile m) noexcept
an allocator that takes the bytes that offers
Definition MappedFile.h:231
MappableAllocator(const MappableAllocator< U, V > &o) noexcept
Definition MappedFile.h:234
void adopts_mapping
a vector on it may be a view of a mapped file
Definition MappedFile.h:214
void construct(U *p, A &&... a)
an item constructed with no argument over the mapping is left as the file has it (an item of a mapped...
Definition MappedFile.h:260
T * allocate(std::size_t n)
Definition MappedFile.h:243
void deallocate(T *p, std::size_t n) noexcept
Definition MappedFile.h:250
std::true_type propagate_on_container_swap
the mapping goes with the memory
Definition MappedFile.h:218
std::false_type is_always_equal
two allocators of different mappings differ
Definition MappedFile.h:220
std::true_type propagate_on_container_move_assignment
the mapping goes with the memory
Definition MappedFile.h:216
friend class MappableAllocator
Definition MappedFile.h:278
MappableAllocator(void)=default
an allocator of new memory only
MappableAllocator select_on_container_copy_construction(void) const
the allocator of a copy, of new memory only
Definition MappedFile.h:239
T value_type
the type of an item
Definition MappedFile.h:212
friend bool operator==(const MappableAllocator &a, const MappableAllocator &b) noexcept
whether the two hold the same mapping, or none
Definition MappedFile.h:271
a whole file mapped privately in memory, shared by its copies
Definition MappedFile.h:94
MappedFile & operator=(const MappedFile &o) noexcept
the same mapping as , shared
MappedFile(const MappedFile &o) noexcept
the same mapping, shared
void offer(void *p, std::size_t m) const
the bytes at , of them, offered to the next take() of bytes
bool mapped(void) const
whether the file is mapped by the system, rather than read
MappedFile(MappedFile &&o) noexcept
the mapping of , which is left with no file
bool holds(const void *p) const
whether lies in the bytes of the file
MappedFile(void)=default
no file
friend bool operator==(const MappedFile &a, const MappedFile &b)
whether the two are copies of the same mapping
Definition MappedFile.h:141
~MappedFile(void)
the mapping released if this is its last copy
MappedFile & operator=(MappedFile &&o) noexcept
the mapping of , which is left with no file
void * take(std::size_t m) const
the bytes offered, if of them, taken; else null
std::span< std::byte > bytes(void) const
the bytes of the file, which start on a line of the cache (on a page where the file is mapped) and ma...
MappedFile(const std::filesystem::path &p)
the file at , mapped (or read, where it cannot be mapped)
the error of a file (or a stream, or a buffer) of a structure that cannot be read: not a file of ADS,...
Definition MappedFile.h:83
the same allocator, of items of the type U
Definition MappedFile.h:224
MappableAllocator< U, V > other
Definition MappedFile.h:225