ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Arena.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Arena.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
62/*--------------------------------------------------------------------------*/
63/*----------------------------- DEFINITIONS --------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#ifndef __ADS_Arena
67 #define __ADS_Arena
68/* self-identification: #endif constant */
69
70/*--------------------------------------------------------------------------*/
71/*------------------------------ INCLUDES ----------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#include "ADS/Contract.h"
75#include "ADS/Portable.h"
76
77#include <algorithm>
78#include <bit>
79#include <compare>
80#include <cstddef>
81#include <cstdint>
82#include <functional>
83#include <iterator>
84#include <limits>
85#include <memory>
86#include <new>
87#include <type_traits>
88#include <utility>
89#include <vector>
90
91/*--------------------------------------------------------------------------*/
92/*--------------------------- NAMESPACE ------------------------------------*/
93/*--------------------------------------------------------------------------*/
94
95namespace ADS
96{
97
98/*--------------------------------------------------------------------------*/
99/*----------------------------- CLASS Arena --------------------------------*/
100/*--------------------------------------------------------------------------*/
101
103template< class T , class A = std::allocator< T > >
104class Arena
105{
106
107/*--------------------------------------------------------------------------*/
108 public:
111 explicit Arena( std::size_t n = std::size_t( 1 ) << 12 ,
112 const A & alloc = A() );
113
115 Arena( const Arena & ) = delete;
117 Arena & operator=( const Arena & ) = delete;
118
121
145 template< class... Args >
146 T * make( Args &&... args );
147
166 void clear( void ) noexcept;
167
172 [[nodiscard]] bool fresh( const T * p ) const;
173
175 [[nodiscard]] std::size_t size( void ) const;
176
178 [[nodiscard]] std::size_t bytes( void ) const;
179
180/*------------------------- THE STANDARD INTERFACE -------------------------*/
181
182 // the objects made in the epoch, in the order of `make()`, random access
183 // across the blocks; an iterator holds the arena's vector of the blocks,
184 // not its array, which an append may move, so that the iterators stay
185 // valid while the epoch lasts, and a new object only moves the end
186
188 template< bool Const >
190 {
191 using Blocks = const std::vector< T * > *; // the vector of the blocks
192
193 public:
195 using iterator_category = std::random_access_iterator_tag;
197 using value_type = T;
199 using difference_type = std::ptrdiff_t;
201 using reference = std::conditional_t< Const , const T & , T & >;
203 using pointer = std::conditional_t< Const , const T * , T * >;
204
206 Iterator( void ) = default;
209 Iterator( Blocks B , std::size_t n , std::size_t i )
210 : f_B( B ) , f_n( n ) , f_i( i ) { }
211
213 template< bool K >
214 requires( Const && ! K )
216 : f_B( o.f_B ) , f_n( o.f_n ) , f_i( o.f_i ) { }
217
219 reference operator*( void ) const { return( ( *this )[ 0 ] ); }
221 pointer operator->( void ) const { return( &**this ); }
225 const std::size_t j = f_i + std::size_t( k );
226 return( ( *f_B )[ j / f_n ][ j % f_n ] );
227 }
228
231 ++f_i;
232 return( *this );
233 }
234
236 --f_i;
237 return( *this );
238 }
239
241 auto t = *this;
242 ++f_i;
243 return( t );
244 }
245
247 auto t = *this;
248 --f_i;
249 return( t );
250 }
251
253 f_i += std::size_t( k );
254 return( *this );
255 }
256
258 f_i -= std::size_t( k );
259 return( *this );
260 }
261
263 return( a += k );
264 }
265
267 return( a += k );
268 }
269
271 return( a -= k );
272 }
273
274 friend difference_type operator-( const Iterator & a , const Iterator & b ) {
275 return( difference_type( a.f_i ) - difference_type( b.f_i ) );
276 }
277
278 bool operator==( const Iterator & o ) const { return( f_i == o.f_i ); }
280 auto operator<=>( const Iterator & o ) const { return( f_i <=> o.f_i ); }
281
282 private:
283 Blocks f_B = nullptr;
284 std::size_t f_n = 1 , f_i = 0;
285
286 template< bool >
287 friend class Iterator;
288 };
289
290 using value_type = T;
291 using size_type = std::size_t;
292 using difference_type = std::ptrdiff_t;
293 using reference = T &;
294 using const_reference = const T &;
297
299 [[nodiscard]] iterator begin( void ) {
300 return( iterator( &f_blocks , f_n , 0 ) );
301 }
302
304 [[nodiscard]] iterator end( void ) {
305 return( iterator( &f_blocks , f_n , f_size ) );
306 }
307
309 [[nodiscard]] const_iterator begin( void ) const {
310 return( const_iterator( &f_blocks , f_n , 0 ) );
311 }
312
314 [[nodiscard]] const_iterator end( void ) const {
315 return( const_iterator( &f_blocks , f_n , f_size ) );
316 }
317
319 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
320
322 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
323
324/*--------------------------------------------------------------------------*/
325 private:
327 void next( void );
328
330 [[nodiscard]] std::size_t used( void ) const {
331 return( f_blocks.empty() ? 0 : std::size_t( f_place - f_blocks[ f_cur ] ) );
332 }
333
334 std::size_t f_n;
335 T * f_place = nullptr;
336 T * f_end = nullptr;
337 std::size_t f_cur = 0;
338 std::size_t f_size = 0;
339 std::vector< T * > f_blocks;
340 A f_alloc;
341
342 }; // end( class Arena )
343
344/*--------------------------------------------------------------------------*/
345/*------------------------- CLASS ArenaResource ----------------------------*/
346/*--------------------------------------------------------------------------*/
347
350template< class A = std::allocator< std::max_align_t > >
352{
353
354/*--------------------------------------------------------------------------*/
355 public:
359 explicit ArenaResource( std::size_t n = std::size_t( 1 ) << 16 ,
360 const A & alloc = A() );
361
363 ArenaResource( const ArenaResource & ) = delete;
365 ArenaResource & operator=( const ArenaResource & ) = delete;
366
369
372 template< std::size_t b , std::size_t a = alignof( std::max_align_t ) >
373 [[nodiscard]] void * allocate( void ) {
374 return( allocate( b , a ) );
375 }
376
378 template< std::size_t b , std::size_t a = alignof( std::max_align_t ) >
379 void deallocate( void * ) noexcept { }
380
402 [[nodiscard]] void * allocate( std::size_t b ,
403 std::size_t a = alignof( std::max_align_t ) );
404
406 void deallocate( void * , std::size_t ,
407 std::size_t = alignof( std::max_align_t ) ) noexcept { }
408
411 void release( void ) noexcept;
412
414 [[nodiscard]] std::size_t bytes( void ) const { return( f_bytes ); }
415
417 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
418
419/*--------------------------------------------------------------------------*/
420 private:
421 using Unit = std::max_align_t;
422
424 struct Block
425 {
426 Block * prev;
427 std::size_t k;
428 };
429
430 static constexpr std::size_t H =
431 ( sizeof( Block ) + sizeof( Unit ) - 1 ) / sizeof( Unit );
432
433 using UnitAlloc =
434 typename std::allocator_traits< A >::template rebind_alloc< Unit >;
435
436 unsigned char * f_next = nullptr;
437 unsigned char * f_end = nullptr;
438 Block * f_last = nullptr;
439 std::size_t f_n;
440 std::size_t f_bytes = 0;
441 std::size_t f_size = 0;
442 ADS_NO_UNIQUE_ADDRESS UnitAlloc f_alloc;
443
444 }; // end( class ArenaResource )
445
446/*--------------------------------------------------------------------------*/
447
448} // end( namespace ADS )
449
450/*--------------------------------------------------------------------------*/
451/*-------------------------- IMPLEMENTATION --------------------------------*/
452/*--------------------------------------------------------------------------*/
453
454#include "ADS/memory/impl/Arena.tpp"
455
456/*--------------------------------------------------------------------------*/
457
458#endif /* Arena.h included */
459
460/*--------------------------------------------------------------------------*/
461/*---------------------------- End File Arena.h ----------------------------*/
462/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
~ArenaResource()
frees the blocks; the objects still alive are not destroyed
std::size_t bytes(void) const
the memory of the blocks, in bytes
Definition Arena.h:414
std::size_t size(void) const
the number of requests served since the construction or release()
Definition Arena.h:417
ArenaResource(const ArenaResource &)=delete
no copy, since its allocators point to it
void deallocate(void *) noexcept
nothing: the bytes go back with the blocks
Definition Arena.h:379
ArenaResource(std::size_t n=std::size_t(1)<< 16, const A &alloc=A())
the empty resource whose blocks hold bytes, taken from alloc
void release(void) noexcept
all the blocks given back to the allocator: the memory given so far is no longer to be used
void deallocate(void *, std::size_t, std::size_t=alignof(std::max_align_t)) noexcept
nothing: the bytes go back with the blocks
Definition Arena.h:406
void * allocate(std::size_t b, std::size_t a=alignof(std::max_align_t))
bytes aligned to , in the current block or in a new one
ArenaResource & operator=(const ArenaResource &)=delete
no copy, since its allocators point to it
void * allocate(void)
bytes aligned to , and known at compile time
Definition Arena.h:373
std::conditional_t< Const, const T *, T * > pointer
a pointer to an object, constant if Const
Definition Arena.h:203
Iterator(const Iterator< K > &o)
the constant iterator of the object of
Definition Arena.h:215
bool operator==(const Iterator &o) const
whether the two are at the same object
Definition Arena.h:278
Iterator & operator-=(difference_type k)
objects back
Definition Arena.h:257
reference operator[](difference_type k) const
the object places ahead: the index is at the place of the block
Definition Arena.h:224
friend Iterator operator+(Iterator a, difference_type k)
the iterator objects after
Definition Arena.h:262
Iterator & operator--(void)
the previous object
Definition Arena.h:235
std::ptrdiff_t difference_type
a distance in objects
Definition Arena.h:199
std::random_access_iterator_tag iterator_category
random access
Definition Arena.h:195
auto operator<=>(const Iterator &o) const
the order of the objects
Definition Arena.h:280
friend Iterator operator+(difference_type k, Iterator a)
the iterator objects after
Definition Arena.h:266
Iterator operator++(int)
the next object, the iterator before the step returned
Definition Arena.h:240
T value_type
the objects
Definition Arena.h:197
Iterator & operator++(void)
the next object
Definition Arena.h:230
Iterator operator--(int)
the previous object, the iterator before the step returned
Definition Arena.h:246
std::conditional_t< Const, const T &, T & > reference
a reference to an object, constant if Const
Definition Arena.h:201
pointer operator->(void) const
the address of the object
Definition Arena.h:221
Iterator(Blocks B, std::size_t n, std::size_t i)
the object of the vector of the blocks , of objects each
Definition Arena.h:209
friend difference_type operator-(const Iterator &a, const Iterator &b)
the number of objects from to
Definition Arena.h:274
Iterator(void)=default
an iterator of no arena
Iterator & operator+=(difference_type k)
objects ahead
Definition Arena.h:252
friend class Iterator
Definition Arena.h:287
reference operator*(void) const
the object
Definition Arena.h:219
friend Iterator operator-(Iterator a, difference_type k)
the iterator objects before
Definition Arena.h:270
std::size_t size(void) const
the number of objects made in the epoch
void clear(void) noexcept
all the objects destroyed and the blocks kept for the next ones, the first of which goes to the first...
Arena & operator=(const Arena &)=delete
no copy, since the objects are reached by their addresses
iterator end(void)
past the last object made
Definition Arena.h:304
T * make(Args &&... args)
a new object built from args, whose address never changes in the epoch; with no args the object is va...
const_iterator end(void) const
past the last object made, constant
Definition Arena.h:314
std::size_t size_type
a number of objects
Definition Arena.h:291
const_iterator cend(void) const
past the last object made, constant
Definition Arena.h:322
Iterator< true > const_iterator
the constant iterator
Definition Arena.h:296
std::size_t bytes(void) const
the memory of the blocks, in bytes, those kept for the epoch included
iterator begin(void)
the first object made
Definition Arena.h:299
Arena(std::size_t n=std::size_t(1)<< 12, const A &alloc=A())
the empty arena whose blocks hold objects, taken from alloc
~Arena()
destroys all the objects and frees the blocks
const T & const_reference
a constant one
Definition Arena.h:294
const_iterator cbegin(void) const
the first object made, constant
Definition Arena.h:319
T & reference
a reference to an object
Definition Arena.h:293
bool fresh(const T *p) const
whether is the address of an object made in the current epoch, in time linear in the blocks: false f...
std::ptrdiff_t difference_type
a distance in objects
Definition Arena.h:292
T value_type
the objects
Definition Arena.h:290
Arena(const Arena &)=delete
no copy, since the objects are reached by their addresses
Iterator< false > iterator
the iterator
Definition Arena.h:295
const_iterator begin(void) const
the first object made, constant
Definition Arena.h:309
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96