SeqAn3 3.4.0-rc.4
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
aligned_allocator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13#include <limits>
14#include <memory>
15#include <type_traits>
16
18
19namespace seqan3
20{
21
68template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
70{
71public:
73 static constexpr size_t alignment = alignment_v;
74
83
86
90 aligned_allocator() = default;
95 ~aligned_allocator() = default;
96
98 template <class other_value_type, size_t other_alignment>
102
135 {
136 constexpr size_type max_size = std::numeric_limits<size_type>::max() / sizeof(value_type);
137 if (n > max_size)
138 throw std::bad_alloc{};
139
140 size_t bytes_to_allocate = n * sizeof(value_type);
142 return static_cast<pointer>(::operator new(bytes_to_allocate));
143 else // Use alignment aware allocator function.
144 return static_cast<pointer>(::operator new(bytes_to_allocate, static_cast<std::align_val_t>(alignment)));
145 }
146
173 void deallocate(pointer const p, size_type const n) const noexcept
174 {
175 size_t bytes_to_deallocate = n * sizeof(value_type);
176
178 ::operator delete(p, bytes_to_deallocate);
179 else // Use alignment aware deallocator function.
180 ::operator delete(p, bytes_to_deallocate, static_cast<std::align_val_t>(alignment));
181 }
182
193 template <typename new_value_type>
201
206 template <class value_type2, size_t alignment2>
208 {
209 return alignment == alignment2;
210 }
211
213 template <class value_type2, size_t alignment2>
215 {
216 return alignment != alignment2;
217 }
219};
220
221} // namespace seqan3
Allocates uninitialized storage whose memory-alignment is specified by alignment.
Definition aligned_allocator.hpp:70
value_t value_type
The value type of the allocation.
Definition aligned_allocator.hpp:76
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:173
constexpr bool operator==(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns true if the memory-alignment matches.
Definition aligned_allocator.hpp:207
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:134
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:99
~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:214
aligned_allocator(aligned_allocator const &)=default
Defaulted.
static constexpr size_t alignment
The memory-alignment of the allocation.
Definition aligned_allocator.hpp:73
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:80
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
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:195
static constexpr size_t other_alignment
The alignment for the rebound allocator.
Definition aligned_allocator.hpp:197
Hide me