-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar_sized.h
More file actions
215 lines (186 loc) · 7.92 KB
/
var_sized.h
File metadata and controls
215 lines (186 loc) · 7.92 KB
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
209
210
211
212
213
214
215
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _VAR_SIZED_H
#define _VAR_SIZED_H
#include <cstddef>
#include <memory>
#include <new>
#include <type_traits>
#include "ref.h"
// Variable-sized class allocation. Allows to create a new instance of a class
// together with an array in with single memory allocation.
namespace refptr {
// Allocates a given additional number of elements of type `A` to every
// allocated instance(s) of `T`.
template <typename A, typename Alloc, typename T>
class VarAllocator {
static_assert(!std::is_destructible<A>::value ||
std::is_trivially_destructible<A>::value,
"The array type must be primitive or trivially destructible");
public:
using value_type = T;
explicit VarAllocator(Alloc allocator, size_t size)
: allocator_(std::move(allocator)), size_(size) {
static_assert(std::is_trivial<Placeholder>::value,
"Internal error: Placeholder class must be trivial");
}
template <typename RebindAlloc, typename U>
VarAllocator(const VarAllocator<A, RebindAlloc, U>& other)
: allocator_(other.allocator_), size_(other.size_) {}
template <typename RebindAlloc, typename U>
VarAllocator(VarAllocator<A, RebindAlloc, U>&& other)
: allocator_(std::move(other.allocator_)), size_(other.size_) {}
template <typename RebindAlloc, typename U>
VarAllocator& operator=(const VarAllocator<A, RebindAlloc, U>& other) {
allocator_ = other.allocator_;
size_ = other.size_;
return *this;
}
template <typename RebindAlloc, typename U>
VarAllocator& operator=(VarAllocator<A, RebindAlloc, U>&& other) {
allocator_ = std::move(other.allocator_);
size_ = other.size_;
return *this;
}
T* allocate(size_t length) {
static_assert(offsetof(Placeholder, node) == 0,
"POD first member must be at the 0 offset");
auto* result =
reinterpret_cast<T*>(std::allocator_traits<UnitAlloc>::allocate(
allocator_, AllocatedUnits(/*t_elements=*/length)));
return result;
}
void deallocate(T* ptr, size_t length) {
std::allocator_traits<UnitAlloc>::deallocate(
allocator_, reinterpret_cast<Unit*>(ptr),
AllocatedUnits(/*t_elements=*/length));
}
template <class U, class... Arg>
void construct(U* ptr, Arg&&... args) {
std::allocator_traits<UnitAlloc>::construct(allocator_, ptr,
std::forward<Arg>(args)...);
}
template <class U>
void destroy(U* ptr) {
std::allocator_traits<UnitAlloc>::destroy(allocator_, ptr);
}
size_t max_size() const {
return (std::allocator_traits<UnitAlloc>::max_size(allocator_) *
sizeof(Unit) -
AdditionalBytes(size_)) /
sizeof(T);
}
// Returns an uninitialized area of memory co-allocated by a previous call to
// `allocate(length)`, that is suitable for holding `GetSize()` elements of
// type `A`.
A* Array(T* ptr, size_t length) const {
return reinterpret_cast<A*>(
&reinterpret_cast<Placeholder*>(ptr + (length - 1))->array);
}
size_t GetSize() const { return size_; }
template <typename U>
struct rebind {
using other = VarAllocator<A, Alloc, U>;
};
private:
// Holds a properly aligned instance of `T` and an array of length 1 of `A`.
struct Placeholder {
typename std::aligned_storage<sizeof(T), alignof(T)>::type node;
// The array type must be the last one in the struct.
typename std::aligned_storage<sizeof(A[1]), alignof(A[1])>::type array;
};
// Properly aligned unit used for the actual allocation.
// It can occupy more than 1 byte, therefore we need to properly compute
// their required number below.
using Unit = typename std::aligned_storage<1, alignof(Placeholder)>::type;
using UnitAlloc =
typename std::allocator_traits<Alloc>::template rebind_alloc<Unit>;
// The number of additional bytes necessary to allocate.
static constexpr size_t AdditionalBytes(size_t a_elements) {
return sizeof(Placeholder) - sizeof(T) + (a_elements - 1) * sizeof(A) +
sizeof(Unit) - 1l;
}
static constexpr size_t AllocatedUnits(size_t a_elements, size_t t_elements) {
return (sizeof(T) * t_elements + AdditionalBytes(a_elements)) /
sizeof(Unit);
}
size_t AllocatedUnits(size_t t_elements) const {
return AllocatedUnits(size_, t_elements);
}
UnitAlloc allocator_;
size_t size_;
template <typename B, typename BAlloc, typename U>
friend class VarAllocator;
};
// Destroys and deallocates an instance of `U` using `Alloc`.
template <typename Alloc>
struct AllocDeleter {
Alloc allocator;
using pointer = typename std::allocator_traits<Alloc>::pointer;
void operator()(pointer to_delete) {
std::allocator_traits<Alloc>::destroy(allocator, to_delete);
std::allocator_traits<Alloc>::deallocate(allocator, to_delete, 1);
}
};
// Constructs a new instance of `U` in-place using the given arguments, with an
// additional block of memory of `B[length]`, with a single memory allocation.
// A `B*` pointer to this buffer and its `size_t` length are passed as the
// first two arguments to the constructor of `U`.
template <typename U, typename B, typename... Arg,
typename Alloc = std::allocator<B>>
inline std::unique_ptr<U, AllocDeleter<VarAllocator<B, Alloc, U>>> MakeUnique(
size_t length, B*& varsized, Arg&&... args, Alloc alloc = {}) {
VarAllocator<B, Alloc, U> var_alloc(std::move(alloc), length);
U* node =
std::allocator_traits<VarAllocator<B, Alloc, U>>::allocate(var_alloc, 1);
try {
std::allocator_traits<VarAllocator<B, Alloc, U>>::construct(
var_alloc, node, std::forward<Arg>(args)...);
varsized = new (var_alloc.Array(node, 1)) B[length];
} catch (...) {
std::allocator_traits<VarAllocator<B, Alloc, U>>::deallocate(var_alloc,
node, 1);
throw;
}
return std::unique_ptr<U, AllocDeleter<VarAllocator<B, Alloc, U>>>(
node, {.allocator = std::move(var_alloc)});
}
template <typename U, typename B, typename... Arg,
typename Alloc = std::allocator<B>>
inline std::shared_ptr<U> MakeShared(size_t length, B*& varsized, Arg&&... args,
Alloc alloc = {}) {
VarAllocator<B, Alloc, U> var_alloc(std::move(alloc), length);
std::shared_ptr<U> shared =
std::allocate_shared<U, VarAllocator<B, Alloc, U>, Arg...>(
var_alloc, std::forward<Arg>(args)...);
varsized = new (var_alloc.Array(shared.get(), 1)) B[length];
return shared;
}
// Similar to `MakeUnique` above, also with a single memory allocation, with
// the difference that it creates a reference counted value to allow efficient
// and type-safe sharing of the construted value.
template <typename U, typename B, typename... Arg,
typename Alloc = std::allocator<U>>
inline Ref<U, VarAllocator<B, Alloc, U>> MakeRefCounted(size_t length,
B*& varsized,
Arg&&... args,
Alloc alloc = {}) {
auto* refcounted = Refcounted<U, VarAllocator<B, Alloc, U>>::New(
VarAllocator<B, Alloc, U>(std::move(alloc), length),
std::forward<Arg>(args)...);
varsized = new (refcounted->Allocator().Array(refcounted, 1)) B[length];
return Ref<U, VarAllocator<B, Alloc, U>>(refcounted);
}
} // namespace refptr
#endif // _VAR_SIZED_H