SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
aligned_allocator.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <limits>
16 #include <memory>
17 #include <type_traits>
18 
19 #include <seqan3/core/platform.hpp>
20 
21 namespace seqan3
22 {
23 
70 template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
72 {
73 public:
75  static constexpr size_t alignment = alignment_v;
76 
78  using value_type = value_t;
80  using pointer = value_type*;
85 
88 
92  aligned_allocator() = default;
93  aligned_allocator(aligned_allocator const &) = default;
97  ~aligned_allocator() = default;
98 
100  template <class other_value_type, size_t other_alignment>
102  {}
104 
136  [[nodiscard]]
137  pointer allocate(size_type const n) const
138  {
139  constexpr size_type max_size = std::numeric_limits<size_type>::max() / sizeof(value_type);
140  if (n > max_size)
141  throw std::bad_alloc{};
142 
143  size_t bytes_to_allocate = n * sizeof(value_type);
144  if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
145  return static_cast<pointer>(::operator new(bytes_to_allocate));
146  else // Use alignment aware allocator function.
147  return static_cast<pointer>(::operator new(bytes_to_allocate, static_cast<std::align_val_t>(alignment)));
148  }
149 
176  void deallocate(pointer const p, size_type const n) const noexcept
177  {
178  size_t bytes_to_deallocate = n * sizeof(value_type);
179  if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
180  ::operator delete(p, bytes_to_deallocate);
181  else // Use alignment aware deallocator function.
182  ::operator delete(p, bytes_to_deallocate, static_cast<std::align_val_t>(alignment));
183  }
184 
195  template <typename new_value_type>
196  struct rebind
197  {
199  static constexpr size_t other_alignment = std::max(alignof(new_value_type), alignment);
202  };
203 
208  template <class value_type2, size_t alignment2>
210  {
211  return alignment == alignment2;
212  }
213 
215  template <class value_type2, size_t alignment2>
217  {
218  return alignment != alignment2;
219  }
221 };
222 
223 } // namespace seqan3
Allocates uninitialized storage whose memory-alignment is specified by alignment.
Definition: aligned_allocator.hpp:72
value_type * pointer
The pointer type of the allocation.
Definition: aligned_allocator.hpp:80
value_t value_type
The value type of the allocation.
Definition: aligned_allocator.hpp:78
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:176
constexpr bool operator==(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns true if the memory-alignment matches.
Definition: aligned_allocator.hpp:209
pointer allocate(size_type const n) const
Allocates sufficiently large memory to hold n many elements of value_type.
Definition: aligned_allocator.hpp:137
constexpr aligned_allocator(aligned_allocator< other_value_type, other_alignment > const &) noexcept
Copy constructor with different value type and alignment.
Definition: aligned_allocator.hpp:101
~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:216
aligned_allocator & operator=(aligned_allocator &&)=default
Defaulted.
aligned_allocator & operator=(aligned_allocator const &)=default
Defaulted.
aligned_allocator(aligned_allocator const &)=default
Defaulted.
static constexpr size_t alignment
The memory-alignment of the allocation.
Definition: aligned_allocator.hpp:75
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:82
T max(T... args)
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
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:197
static constexpr size_t other_alignment
The alignment for the rebound allocator.
Definition: aligned_allocator.hpp:199