Skip to content

Commit 5fd536f

Browse files
committed
Refactored static_storage_allocator in terms of static_storage
1 parent 557ac6f commit 5fd536f

File tree

3 files changed

+30
-79
lines changed

3 files changed

+30
-79
lines changed

include/sfl/detail/memory/destroy_at.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <sfl/detail/cpp.hpp>
2525

2626
#include <memory> // destroy_at
27-
#include <type_traits> // is_trivially_copyable
2827

2928
namespace sfl
3029
{
@@ -36,15 +35,8 @@ template <typename T>
3635
SFL_CONSTEXPR_20
3736
void destroy_at(T* p) noexcept
3837
{
39-
#if SFL_CPP_VERSION >= SFL_CPP_20
40-
if constexpr (std::is_trivially_copyable<T>::value)
41-
{
42-
// Do nothing
43-
}
44-
else
45-
{
46-
std::destroy_at(p);
47-
}
38+
#if SFL_CPP_VERSION >= SFL_CPP_17
39+
std::destroy_at(p);
4840
#else
4941
p->~T();
5042
#endif

include/sfl/detail/static_storage_allocator.hpp

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
#include <sfl/detail/utility/ignore_unused.hpp>
2626
#include <sfl/detail/allocator_traits.hpp>
2727
#include <sfl/detail/cpp.hpp>
28+
#include <sfl/detail/static_storage.hpp>
2829

2930
#include <cstddef> // size_t, ptrdiff_t
30-
#include <type_traits> // true_type, false_type, is_trivially_copyable
31+
#include <type_traits> // true_type, false_type
3132

3233
namespace sfl
3334
{
@@ -38,11 +39,15 @@ namespace dtl
3839
template <typename T, std::size_t N>
3940
class static_storage_allocator
4041
{
42+
private:
43+
44+
using static_storage_type = sfl::dtl::static_storage<T, N>;
45+
4146
public:
4247

4348
using value_type = T;
44-
using pointer = T*;
45-
using const_pointer = const T*;
49+
using pointer = typename static_storage_type::pointer;
50+
using const_pointer = typename static_storage_type::const_pointer;
4651
using reference = T&;
4752
using const_reference = const T&;
4853
using size_type = std::size_t;
@@ -62,48 +67,10 @@ class static_storage_allocator
6267

6368
private:
6469

65-
template <bool IsTriviallyCopyable, typename Dummy = void>
66-
struct data;
67-
68-
template <typename Dummy>
69-
struct data<true, Dummy>
70-
{
71-
T internal_storage_[N];
72-
73-
SFL_CONSTEXPR_20
74-
data() noexcept
75-
{}
76-
77-
SFL_CONSTEXPR_20
78-
~data()
79-
{}
80-
};
81-
82-
template <typename Dummy>
83-
struct data<false, Dummy>
84-
{
85-
union
86-
{
87-
T internal_storage_[N];
88-
};
89-
90-
SFL_CONSTEXPR_20
91-
data() noexcept
92-
{}
93-
94-
SFL_CONSTEXPR_20
95-
~data()
96-
{}
97-
};
98-
99-
#if SFL_CPP_VERSION >= SFL_CPP_20
100-
data<std::is_trivially_copyable<T>::value> data_;
101-
#else
102-
data<false> data_;
103-
#endif
70+
static_storage_type storage_;
10471

10572
#ifndef NDEBUG
106-
bool internal_storage_allocated_ = false;
73+
bool storage_allocated_ = false;
10774
#endif
10875

10976
public:
@@ -169,11 +136,11 @@ class static_storage_allocator
169136
SFL_ASSERT(n <= N);
170137

171138
#ifndef NDEBUG
172-
SFL_ASSERT(!internal_storage_allocated_);
173-
internal_storage_allocated_ = true;
139+
SFL_ASSERT(!storage_allocated_);
140+
storage_allocated_ = true;
174141
#endif
175142

176-
return pointer(data_.internal_storage_);
143+
return storage_.data();
177144
}
178145

179146
SFL_NODISCARD
@@ -185,24 +152,24 @@ class static_storage_allocator
185152
SFL_ASSERT(n <= N);
186153

187154
#ifndef NDEBUG
188-
SFL_ASSERT(!internal_storage_allocated_);
189-
internal_storage_allocated_ = true;
155+
SFL_ASSERT(!storage_allocated_);
156+
storage_allocated_ = true;
190157
#endif
191158

192-
return {pointer(data_.internal_storage_), size_type(N)};
159+
return {storage_.data(), size_type(N)};
193160
}
194161

195162
SFL_CONSTEXPR_20
196163
void deallocate(pointer p, std::size_t n) noexcept
197164
{
198165
sfl::dtl::ignore_unused(p, n);
199166

200-
SFL_ASSERT(p == pointer(data_.internal_storage_));
167+
SFL_ASSERT(p == storage_.data());
201168
SFL_ASSERT(n <= N);
202169

203170
#ifndef NDEBUG
204-
SFL_ASSERT(internal_storage_allocated_);
205-
internal_storage_allocated_ = false;
171+
SFL_ASSERT(storage_allocated_);
172+
storage_allocated_ = false;
206173
#endif
207174
}
208175

@@ -218,26 +185,14 @@ class static_storage_allocator
218185
{
219186
sfl::dtl::ignore_unused(p);
220187

221-
SFL_ASSERT(p == pointer(data_.internal_storage_));
188+
SFL_ASSERT(p == storage_.data());
222189

223190
#ifndef NDEBUG
224-
SFL_ASSERT(internal_storage_allocated_);
191+
SFL_ASSERT(storage_allocated_);
225192
#endif
226193

227194
return true;
228195
}
229-
230-
#if SFL_CPP_VERSION >= SFL_CPP_20
231-
232-
template <typename U, sfl::dtl::enable_if_t<std::is_trivially_copyable<U>::value>* = nullptr>
233-
SFL_CONSTEXPR_20
234-
void destroy(U* p)
235-
{
236-
// Do nothing
237-
sfl::dtl::ignore_unused(p);
238-
}
239-
240-
#endif // SFL_CPP_VERSION >= SFL_CPP_20
241196
};
242197

243198
template <typename T1, typename T2, std::size_t N>

test/src/static_storage_allocator/static_storage_allocator.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ bool test()
1515
{
1616
constexpr int N = 10;
1717

18+
using allocator_type = sfl::dtl::static_storage_allocator<T, N>;
19+
20+
using pointer = typename std::allocator_traits<allocator_type>::pointer;
21+
1822
for (int n = 1; n <= N; ++n)
1923
{
20-
sfl::dtl::static_storage_allocator<T, N> alloc;
24+
allocator_type alloc;
2125

22-
auto* p = alloc.allocate(n);
26+
pointer p = alloc.allocate(n);
2327

2428
for (int i = 0; i < n; ++i)
2529
{
@@ -41,7 +45,7 @@ bool test()
4145

4246
for (int n = 1; n <= N; ++n)
4347
{
44-
sfl::dtl::static_storage_allocator<T, N> alloc;
48+
allocator_type alloc;
4549

4650
auto res = alloc.allocate_at_least(n);
4751

0 commit comments

Comments
 (0)