SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
aligned_allocator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <limits>
13#include <memory>
14#include <type_traits>
15
17
18// __cpp_aligned_new is a C++17 feature that we use in this allocator and we require it.
19#if __cpp_aligned_new < 201606
20# pragma GCC warning "Non-C++17 compliant compiler! Please open an issue with your compiler and platform!"
21#endif // __cpp_aligned_new < 201606
22
23namespace seqan3
24{
25
72template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
74{
75public:
77 static constexpr size_t alignment = alignment_v;
78
80 using value_type = value_t;
87
90
94 aligned_allocator() = default;
99 ~aligned_allocator() = default;
100
102 template <class other_value_type, size_t other_alignment>
106
138 [[nodiscard]] pointer allocate(size_type const n) const
139 {
140 constexpr size_type max_size = std::numeric_limits<size_type>::max() / sizeof(value_type);
141 if (n > max_size)
142 throw std::bad_alloc{};
143
144 size_t bytes_to_allocate = n * sizeof(value_type);
145 if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
146 return static_cast<pointer>(::operator new(bytes_to_allocate));
147 else // Use alignment aware allocator function.
148 return static_cast<pointer>(::operator new(bytes_to_allocate, static_cast<std::align_val_t>(alignment)));
149 }
150
177 void deallocate(pointer const p, size_type const n) const noexcept
178 {
179 size_t bytes_to_deallocate = n * sizeof(value_type);
180
181 // Clang doesn't have __cpp_sized_deallocation defined by default even though this is a C++14! feature
182 // > In Clang 3.7 and later, sized deallocation is only enabled if the user passes the `-fsized-deallocation`
183 // > flag.
184 // see also https://clang.llvm.org/cxx_status.html#n3778
185#if __cpp_sized_deallocation >= 201309
186 // gcc
187 if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
188 ::operator delete(p, bytes_to_deallocate);
189 else // Use alignment aware deallocator function.
190 ::operator delete(p, bytes_to_deallocate, static_cast<std::align_val_t>(alignment));
191#else /*__cpp_sized_deallocation >= 201309*/
192 // e.g. clang++
193 (void)bytes_to_deallocate;
194 if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
195 ::operator delete(p);
196 else // Use alignment aware deallocator function.
197 ::operator delete(p, static_cast<std::align_val_t>(alignment));
198#endif // __cpp_sized_deallocation >= 201309
199 }
200
211 template <typename new_value_type>
212 struct rebind
213 {
215 static constexpr size_t other_alignment = std::max(alignof(new_value_type), alignment);
218 };
219
224 template <class value_type2, size_t alignment2>
226 {
227 return alignment == alignment2;
228 }
229
231 template <class value_type2, size_t alignment2>
233 {
234 return alignment != alignment2;
235 }
237};
238
239} // namespace seqan3
Allocates uninitialized storage whose memory-alignment is specified by alignment.
Definition aligned_allocator.hpp:74
value_t value_type
The value type of the allocation.
Definition aligned_allocator.hpp:80
void deallocate(pointer const p, size_type const n) const noexcept
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
Definition aligned_allocator.hpp:177
constexpr bool operator==(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns true if the memory-alignment matches.
Definition aligned_allocator.hpp:225
aligned_allocator & operator=(aligned_allocator &&)=default
Defaulted.
pointer allocate(size_type const n) const
Allocates sufficiently large memory to hold n many elements of value_type.
Definition aligned_allocator.hpp:138
value_type * pointer
The pointer type of the allocation.
Definition aligned_allocator.hpp:82
aligned_allocator & operator=(aligned_allocator const &)=default
Defaulted.
constexpr aligned_allocator(aligned_allocator< other_value_type, other_alignment > const &) noexcept
Copy constructor with different value type and alignment.
Definition aligned_allocator.hpp:103
~aligned_allocator()=default
Defaulted.
constexpr bool operator!=(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns false if the memory-alignment mismatches.
Definition aligned_allocator.hpp:232
aligned_allocator(aligned_allocator const &)=default
Defaulted.
static constexpr size_t alignment
The memory-alignment of the allocation.
Definition aligned_allocator.hpp:77
aligned_allocator(aligned_allocator &&)=default
Defaulted.
aligned_allocator()=default
Defaulted.
typename std::pointer_traits< pointer >::difference_type difference_type
The difference type of the allocation.
Definition aligned_allocator.hpp:84
T max(T... args)
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides platform and dependency checks.
The aligned_allocator member template class aligned_allocator::rebind provides a way to obtain an all...
Definition aligned_allocator.hpp:213
static constexpr size_t other_alignment
The alignment for the rebound allocator.
Definition aligned_allocator.hpp:215
Hide me