ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Scratch.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Scratch.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
26/*--------------------------------------------------------------------------*/
27/*----------------------------- DEFINITIONS --------------------------------*/
28/*--------------------------------------------------------------------------*/
29
30#ifndef __ADS_Scratch
31 #define __ADS_Scratch
32/* self-identification: #endif constant */
33
34/*--------------------------------------------------------------------------*/
35/*------------------------------ INCLUDES ----------------------------------*/
36/*--------------------------------------------------------------------------*/
37
39
40#include <algorithm>
41#include <cstddef>
42#include <mutex>
43#include <utility>
44
45/*--------------------------------------------------------------------------*/
46/*--------------------------- NAMESPACE ------------------------------------*/
47/*--------------------------------------------------------------------------*/
48
49namespace ADS::detail
50{
51
57class ScratchRoom
58{
59 public:
86 static std::byte * take( std::size_t b , std::size_t & got ,
87 std::size_t a = __STDCPP_DEFAULT_NEW_ALIGNMENT__ ) {
88 if( a > __STDCPP_DEFAULT_NEW_ALIGNMENT__ )
89 b = std::max( b , HugePageAllocator< std::byte >::PAGE );
90 {
91 std::lock_guard< std::mutex > g( slot().lock );
92 if( slot().p && ( slot().size >= b ) ) {
93 got = slot().size;
94 return( std::exchange( slot().p , nullptr ) );
95 }
96 }
97 got = b;
98 return( HugePageAllocator< std::byte >().allocate( b ) );
99 }
100
102 static void give( std::byte * p , std::size_t b ) {
103 std::byte * drop = p;
104 std::size_t db = b;
105 {
106 std::lock_guard< std::mutex > g( slot().lock );
107 if( ! slot().p || ( slot().size < b ) ) {
108 std::swap( drop , slot().p );
109 std::swap( db , slot().size );
110 }
111 }
112 if( drop )
113 HugePageAllocator< std::byte >().deallocate( drop , db );
114 }
115
116 private:
117 struct Slot
118 {
119 std::mutex lock;
120 std::byte * p = nullptr;
121 std::size_t size = 0;
122 ~Slot() {
123 if( p )
124 HugePageAllocator< std::byte >().deallocate( p , size );
125 }
126 };
127
128 static Slot & slot( void ) {
129 static Slot s;
130 return( s );
131 }
132 };
133
135template< class T >
136class ScratchBuffer
137{
138 public:
140 explicit ScratchBuffer( std::size_t n )
141 : f_p( reinterpret_cast< T * >(
142 ScratchRoom::take( std::max< std::size_t >( n , 1 ) * sizeof( T ) ,
143 f_b ,
144 alignof( T ) ) ) ) { }
146 ~ScratchBuffer() {
147 ScratchRoom::give( reinterpret_cast< std::byte * >( f_p ) , f_b );
148 }
149 ScratchBuffer( const ScratchBuffer & ) = delete;
150 ScratchBuffer & operator=( const ScratchBuffer & ) = delete;
152 [[nodiscard]] T * data( void ) const { return( f_p ); }
153
154 private:
155 std::size_t f_b = 0;
156 T * f_p;
157 };
158
159} // end( namespace ADS::detail )
160
161/*--------------------------------------------------------------------------*/
162
163#endif /* Scratch.h included */
164
165/*--------------------------------------------------------------------------*/
166/*--------------------------- End File Scratch.h ---------------------------*/
167/*--------------------------------------------------------------------------*/
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
static constexpr std::size_t PAGE
the size of a huge page, in bytes
Definition HugePageAllocator.h:177