SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
aligned_allocator.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 
68 template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
70 {
71 public:
73  static constexpr size_t alignment = alignment_v;
74 
76  using value_type = value_t;
78  using pointer = value_type*;
83 
86 
90  aligned_allocator() = default;
91  aligned_allocator(aligned_allocator const &) = default;
95  ~aligned_allocator() = default;
96 
98  template <class other_value_type, size_t other_alignment>
100  {}
102 
134  [[nodiscard]]
135  pointer allocate(size_type const n) const
136  {
137  constexpr size_type max_size = std::numeric_limits<size_type>::max() / sizeof(value_type);
138  if (n > max_size)
139  throw std::bad_alloc{};
140 
141  size_t bytes_to_allocate = n * sizeof(value_type);
142  if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
143  return static_cast<pointer>(::operator new(bytes_to_allocate));
144  else // Use alignment aware allocator function.
145  return static_cast<pointer>(::operator new(bytes_to_allocate, static_cast<std::align_val_t>(alignment)));
146  }
147 
174  void deallocate(pointer const p, size_type const n) const noexcept
175  {
176  size_t bytes_to_deallocate = n * sizeof(value_type);
177  if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
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>
194  struct rebind
195  {
197  static constexpr size_t other_alignment = std::max(alignof(new_value_type), alignment);
200  };
201 
205  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
seqan3::aligned_allocator::value_type
value_t value_type
The value type of the allocation.
Definition: aligned_allocator.hpp:76
std::true_type
seqan3::aligned_allocator::operator!=
constexpr bool operator!=(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns false if the memory-alignment mismatches.
Definition: aligned_allocator.hpp:214
seqan3::aligned_allocator::difference_type
typename std::pointer_traits< pointer >::difference_type difference_type
The difference type of the allocation.
Definition: aligned_allocator.hpp:80
seqan3::aligned_allocator::~aligned_allocator
~aligned_allocator()=default
Defaulted.
std::bad_alloc
seqan3::aligned_allocator::allocate
pointer allocate(size_type const n) const
Allocates sufficiently large memory to hold n many elements of value_type.
Definition: aligned_allocator.hpp:135
seqan3::aligned_allocator::aligned_allocator
aligned_allocator()=default
Defaulted.
std::pointer_traits
seqan3::aligned_allocator::operator=
aligned_allocator & operator=(aligned_allocator &&)=default
Defaulted.
seqan3::aligned_allocator::aligned_allocator
aligned_allocator(aligned_allocator &&)=default
Defaulted.
seqan3::aligned_allocator::rebind::other_alignment
static constexpr size_t other_alignment
The alignment for the rebound allocator.
Definition: aligned_allocator.hpp:197
seqan3::aligned_allocator
Allocates uninitialized storage whose memory-alignment is specified by alignment.
Definition: aligned_allocator.hpp:70
seqan3::aligned_allocator::pointer
value_type * pointer
The pointer type of the allocation.
Definition: aligned_allocator.hpp:78
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
memory
seqan3::aligned_allocator::operator==
constexpr bool operator==(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns true if the memory-alignment matches.
Definition: aligned_allocator.hpp:207
seqan3::aligned_allocator::operator=
aligned_allocator & operator=(aligned_allocator const &)=default
Defaulted.
platform.hpp
Provides platform and dependency checks.
limits
seqan3::aligned_allocator::rebind
The aligned_allocator member template class aligned_allocator::rebind provides a way to obtain an all...
Definition: aligned_allocator.hpp:195
seqan3::aligned_allocator::aligned_allocator
aligned_allocator(aligned_allocator const &)=default
Defaulted.
seqan3::aligned_allocator::aligned_allocator
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
std::numeric_limits::max
T max(T... args)
seqan3::aligned_allocator::alignment
static constexpr size_t alignment
The memory-alignment of the allocation.
Definition: aligned_allocator.hpp:73
seqan3::aligned_allocator::deallocate
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:174
std::make_unsigned_t
std::align_val_t