-
Notifications
You must be signed in to change notification settings - Fork 1
/
region_allocator.hpp
208 lines (173 loc) · 4.71 KB
/
region_allocator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#pragma once
#include "monitor.hpp"
#include <atomic>
#include <cassert>
#include <cstddef>
#include <memory>
#include <vector>
namespace cu
{
class RegionAllocatorImpl
{
public:
RegionAllocatorImpl()
: storagePtr( std::make_shared<Storage>() )
{}
void * allocate( std::size_t n, std::size_t alignment )
{
assert( ( alignment & (alignment-1) ) == 0 &&
"The alignment must be a power of two." );
auto & storage = *storagePtr;
if ( n >= minBigChunkSize )
{
auto p = makeMallocPtr( n );
const auto result = p.get();
assert( result == align( result, alignment ) );
storage.bigChunks( [&]( auto & bigChunks )
{
bigChunks.push_back( std::move(p) );
} );
return result;
}
const auto p = updateRange( storage.range, n, alignment );
if ( p )
return p;
return storage.regions( [&]( auto & regions ) -> void*
{
const auto p = updateRange( storage.range, n, alignment );
if ( p )
return p;
auto region = makeMallocPtr( regionSize );
const auto result = region.get();
assert( result == align( result, alignment ) );
storage.range = { result, result + regionSize };
regions.push_back( std::move( region ) );
return result;
} );
}
bool operator==( const RegionAllocatorImpl & other ) const
{
return storagePtr.get() == other.storagePtr.get();
}
private:
enum {
regionSize = 8192,
minBigChunkSize = 2048,
};
struct Range
{
char * begin;
char * end;
};
struct FreeDeleter
{
void operator()( void * p )
{
std::free( p );
}
};
using MallocPtr = std::unique_ptr<char[],FreeDeleter>;
static MallocPtr makeMallocPtr( std::size_t n )
{
const auto p = static_cast<char*>( malloc(n) );
if ( !p )
throw std::bad_alloc{};
return MallocPtr( p );
}
static char * align( char * p, std::size_t alignment )
{
return reinterpret_cast<char*>(
reinterpret_cast<std::size_t>( p + (alignment-1) ) & -alignment );
}
static Range chopRange(
const Range & range,
std::size_t n,
std::size_t alignment )
{
return { align( range.begin, alignment ) + n, range.end };
}
/// Atomically chops at least @c n bytes from the front of the range and
/// returns an aligned pointer @c p that points into the original range,
/// such that @c [p,p+n] does not overlap with the resulting @c range.
/// If that is not possible, then a @c nullptr will be returned.
static void * updateRange(
std::atomic<Range> & range,
std::size_t n,
std::size_t alignment )
{
auto oldRange = range.load();
for ( ;; )
{
const auto choppedRange = chopRange( oldRange, n, alignment );
if ( choppedRange.begin > choppedRange.end )
return nullptr; // failure
if ( range.compare_exchange_weak( oldRange, choppedRange ) )
return align( oldRange.begin, alignment ); // success
}
}
struct Storage
{
Storage()
{
const auto reserver = []( auto & v ){ v.reserve(64/sizeof(MallocPtr)); };
regions ( reserver );
bigChunks( reserver );
}
cu::Monitor<std::vector<MallocPtr>> regions;
cu::Monitor<std::vector<MallocPtr>> bigChunks;
std::atomic<Range> range{};
};
std::shared_ptr<Storage> storagePtr;
};
template <typename T>
class RegionAllocator {
public:
using value_type = T;
RegionAllocator( const RegionAllocator & ) = default;
RegionAllocator & operator=( const RegionAllocator & ) = default;
template <typename U>
RegionAllocator( const RegionAllocator<U> & other )
: impl( std::move( other.impl ) )
{}
T* allocate( std::size_t n )
{
return static_cast<T*>( impl.allocate( n * sizeof(T), alignof(T) ) );
}
void deallocate( T *, std::size_t )
{
// NOOP. Memory is freed in one go when the last copy of @c impl
// is destroyed.
}
template <typename U>
std::enable_if_t<!std::is_same<U,void>::value,bool>
operator==( const RegionAllocator<U> & other )
{
return impl == other.impl;
}
template <typename U>
std::enable_if_t<!std::is_same<U,void>::value,bool>
operator!=( const RegionAllocator<U> & other )
{
return impl != other.impl;
}
template <typename U>
friend class RegionAllocator;
private:
RegionAllocatorImpl impl;
};
template <>
class RegionAllocator<void>
{
public:
using value_type = void;
RegionAllocator() = default;
RegionAllocator( const RegionAllocator & ) = default;
RegionAllocator & operator=( const RegionAllocator & ) = default;
template <typename U>
friend class RegionAllocator;
private:
RegionAllocatorImpl impl;
};
template <typename T>
using RegionVector = std::vector<T, RegionAllocator<T>>;
} // namespace cu