ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MappedFile.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File MappedFile.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
41/*--------------------------------------------------------------------------*/
42/*----------------------------- DEFINITIONS --------------------------------*/
43/*--------------------------------------------------------------------------*/
44
45#ifndef __ADS_MappedFile
46 #define __ADS_MappedFile
47/* self-identification: #endif constant */
48
49/*--------------------------------------------------------------------------*/
50/*------------------------------ INCLUDES ----------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#include "ADS/Contract.h"
54
55#include <atomic>
56#include <cstddef>
57#include <cstdint>
58#include <filesystem>
59#include <initializer_list>
60#include <memory>
61#include <new>
62#include <span>
63#include <stdexcept>
64#include <string_view>
65#include <type_traits>
66#include <utility>
67
68/*--------------------------------------------------------------------------*/
69/*--------------------------- NAMESPACE ------------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72namespace ADS
73{
74
75/*--------------------------------------------------------------------------*/
76/*--------------------------- CLASS PersistError ---------------------------*/
77/*--------------------------------------------------------------------------*/
78
82class PersistError : public std::runtime_error
83{
84 public:
85 using std::runtime_error::runtime_error;
86 };
87
88/*--------------------------------------------------------------------------*/
89/*---------------------------- CLASS MappedFile ----------------------------*/
90/*--------------------------------------------------------------------------*/
91
94{
95
96/*--------------------------------------------------------------------------*/
97 public:
99 MappedFile( void ) = default;
100
102 explicit MappedFile( const std::filesystem::path & p );
103
105 MappedFile( const MappedFile & o ) noexcept;
106
108 MappedFile( MappedFile && o ) noexcept;
109
111 MappedFile & operator=( const MappedFile & o ) noexcept;
112
114 MappedFile & operator=( MappedFile && o ) noexcept;
115
117 ~MappedFile( void );
118
121 [[nodiscard]] std::span< std::byte > bytes( void ) const;
122
124 [[nodiscard]] explicit operator bool( void ) const { return( f_r ); }
125
127 [[nodiscard]] bool mapped( void ) const;
128
130 [[nodiscard]] bool holds( const void * p ) const;
131
135 void offer( void * p , std::size_t m ) const;
136
138 [[nodiscard]] void * take( std::size_t m ) const;
139
141 friend bool operator==( const MappedFile & a , const MappedFile & b ) {
142 return( a.f_r == b.f_r );
143 }
144
145/*--------------------------------------------------------------------------*/
146 private:
148 static constexpr std::size_t kPage = 4096;
149
151 struct Region
152 {
153 std::atomic< std::size_t > refs{ 1 };
154 std::byte * base = nullptr;
155 std::size_t size = 0;
156 bool system = false;
157 void * handle = nullptr;
158 void ( *drop )( Region & ) = nullptr;
159 void * offered = nullptr;
160 std::size_t offeredSize = 0;
161 };
162
164 void release( void ) noexcept;
165
166 Region * f_r = nullptr;
167
168 }; // end( class MappedFile )
169
170namespace detail
171{
172
175[[nodiscard]] constexpr std::uint64_t
176persistMix( std::initializer_list< std::uint64_t > v )
177{
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;
182 }
183 return( h );
184 }
185
188[[nodiscard]] constexpr std::uint64_t persistHash( std::string_view s )
189{
190 std::uint64_t h = 0xCBF29CE484222325ULL;
191 for( const char c : s )
192 h = ( h ^ std::uint64_t( static_cast< unsigned char >( c ) ) ) *
193 0x100000001B3ULL;
194 return( h );
195 }
196
197} // end( namespace detail )
198
199/*--------------------------------------------------------------------------*/
200/*------------------------ CLASS MappableAllocator -------------------------*/
201/*--------------------------------------------------------------------------*/
202
207template< class T , bool V = true >
209{
210 public:
212 using value_type = T;
214 using adopts_mapping = void;
218 using propagate_on_container_swap = std::true_type;
220 using is_always_equal = std::false_type;
222 template< class U >
223 struct rebind
224 {
226 };
227
229 MappableAllocator( void ) = default;
231 explicit MappableAllocator( MappedFile m ) noexcept
232 : f_map( std::move( m ) ) { }
233 template< class U >
235 : f_map( o.f_map ) { }
236
238 [[nodiscard]] MappableAllocator
240 return( MappableAllocator() );
241 }
242
243 [[nodiscard]] T * allocate( std::size_t n ) {
244 if( 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 ) );
248 }
249
250 void deallocate( T * p , std::size_t n ) noexcept {
251 if( f_map && f_map.holds( p ) )
252 return;
253 std::allocator< T >().deallocate( p , n );
254 }
255
259 template< class U , class... A >
260 void construct( U * p , A &&... a ) {
261 if constexpr( ( sizeof...( A ) == 0 ) && std::is_trivially_copyable_v< U > )
262 if( f_map && f_map.holds( p ) )
263 return;
264 if constexpr( ( sizeof...( A ) == 0 ) && ! V )
265 ::new( static_cast< void * >( p ) ) U;
266 else
267 ::new( static_cast< void * >( p ) ) U( std::forward< A >( a )... );
268 }
269
271 friend bool operator==( const MappableAllocator & a ,
272 const MappableAllocator & b ) noexcept {
273 return( a.f_map == b.f_map );
274 }
275
276 private:
277 template< class U , bool W >
278 friend class MappableAllocator;
279
280 MappedFile f_map;
281
282 }; // end( class MappableAllocator )
283
284/*--------------------------------------------------------------------------*/
285
286} // end( namespace ADS )
287
288/*--------------------------------------------------------------------------*/
289/*-------------------------- IMPLEMENTATION --------------------------------*/
290/*--------------------------------------------------------------------------*/
291
292#include "ADS/external/impl/MappedFile.tpp"
293
294/*--------------------------------------------------------------------------*/
295
296#endif /* MappedFile.h included */
297
298/*--------------------------------------------------------------------------*/
299/*------------------------- End File MappedFile.h --------------------------*/
300/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
the same allocator, of items of the type U
Definition MappedFile.h:224
MappableAllocator< U, V > other
Definition MappedFile.h:225