SeqAn3
aligned_allocator.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
42 #include <limits>
43 #include <memory>
44 #include <type_traits>
45 
46 #include <seqan3/core/platform.hpp>
47 
48 namespace seqan3
49 {
50 
85 template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
87 {
88 public:
90  static constexpr size_t alignment = alignment_v;
91 
93  using value_type = value_t;
95  using pointer = value_type*;
97  using difference_type = typename std::pointer_traits<pointer>::difference_type;
99  using size_type = std::make_unsigned_t<difference_type>;
100 
102  using is_always_equal = std::true_type;
103 
107  aligned_allocator() = default;
108  aligned_allocator(aligned_allocator const &) = default;
109  aligned_allocator(aligned_allocator &&) = default;
110  aligned_allocator& operator=(aligned_allocator const &) = default;
111  aligned_allocator& operator=(aligned_allocator &&) = default;
112  ~aligned_allocator() = default;
113 
114  template <class other_value_type>
116  {}
118 
124  [[nodiscard]]
126  {
127  constexpr size_type max_size = std::numeric_limits<size_type>::max() / sizeof(value_type);
128  if (n > max_size)
129  throw std::bad_alloc();
130 
131  // NOTE: On macOS glibc does not implement aligned_alloc, so we need to fallback to posix_memalign instead.
132 #if defined(__APPLE__) && (!defined(_GLIBCXX_HAVE_ALIGNED_ALLOC) && !defined(_ISOC11_SOURCE))
133  void * p{};
134  if (int res = posix_memalign(&p, alignment, n * sizeof(value_type)); res == 0 && p != nullptr)
135  return static_cast<pointer>(p);
136 #else
137  // NOTE:
138  // Allocate size bytes of uninitialized storage whose alignment is
139  // specified by alignment. The size parameter must be an integral
140  // multiple of alignment.
141  //
142  // Passing a size which is not an integral multiple of alignment or an
143  // alignment which is not valid or not supported by the implementation
144  // causes the function to fail and return a null pointer (C11, as
145  // published, specified undefined behavior in this case, this was
146  // corrected by DR 460).
147  // http://en.cppreference.com/w/cpp/memory/c/aligned_alloc
148  if (auto p = static_cast<pointer>(std::aligned_alloc(alignment, n * sizeof(value_type))))
149  return p;
150 #endif
151 
152  throw std::bad_alloc();
153  }
154 
165  void deallocate(pointer p, size_type) noexcept
166  {
167  std::free(p);
168  }
169 
174  template<typename new_value_type>
175  struct rebind
176  {
179  };
180 
184  template <class value_type2, size_t alignment2>
187  {
188  return alignment == alignment2;
189  }
190 
192  template <class value_type2, size_t alignment2>
194  {
195  return alignment != alignment2;
196  }
198 };
199 
200 } // namespace seqan3
Contains platform and dependency checks.
constexpr bool operator==(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns true if the memory-alignment matches.
Definition: aligned_allocator.hpp:186
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
void deallocate(pointer p, size_type) noexcept
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
Definition: aligned_allocator.hpp:165
static constexpr size_t alignment
The memory-alignment of the allocation.
Definition: aligned_allocator.hpp:90
std::make_unsigned_t< difference_type > size_type
The size type of the allocation.
Definition: aligned_allocator.hpp:99
constexpr bool operator!=(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns false if the memory-alignment mismatches.
Definition: aligned_allocator.hpp:193
typename std::pointer_traits< pointer >::difference_type difference_type
The difference type of the allocation.
Definition: aligned_allocator.hpp:97
The aligned_allocator member template class aligned_allocator::rebind provides a way to obtain an all...
Definition: aligned_allocator.hpp:175
pointer allocate(size_type n)
Allocates n * sizeof(T) bytes of uninitialized storage by calling std::aligned_alloc, but it is unspecified when and how this function is called.
Definition: aligned_allocator.hpp:125
value_t value_type
The value type of the allocation.
Definition: aligned_allocator.hpp:93
Allocates uninitialized storage whose memory-alignment is specified by alignment. ...
Definition: aligned_allocator.hpp:86
Provides C++20 additions to the type_traits header.
value_type * pointer
The pointer type of the allocation.
Definition: aligned_allocator.hpp:95
std::true_type is_always_equal
Are any two allocators of the same aligned_allocator type always compare equal?
Definition: aligned_allocator.hpp:102