BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
aligned_allocator.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <limits>
17#include <memory>
18#include <type_traits>
19
20#include <bio/core.hpp>
21
22namespace bio::ranges
23{
24
69template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
71{
72public:
74 static constexpr size_t alignment = alignment_v;
75
77 using value_type = value_t;
84
87
91 aligned_allocator() noexcept = default;
92 aligned_allocator(aligned_allocator const &) noexcept = default;
93 aligned_allocator(aligned_allocator &&) noexcept = default;
94 aligned_allocator & operator=(aligned_allocator const &) noexcept = default;
95 aligned_allocator & operator=(aligned_allocator &&) noexcept = default;
96 ~aligned_allocator() noexcept = default;
97
99 template <class other_value_type, size_t other_alignment>
100 constexpr aligned_allocator(aligned_allocator<other_value_type, other_alignment> const &) noexcept
101 {}
103
135 [[nodiscard]] 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
206 template <class value_type2, size_t alignment2>
208 {
209 return alignment == alignment2;
210 }
212};
213
214} // namespace bio::ranges
Allocates uninitialized storage whose memory-alignment is specified by alignment.
Definition: aligned_allocator.hpp:71
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
value_type * pointer
The pointer type of the allocation.
Definition: aligned_allocator.hpp:79
aligned_allocator() noexcept=default
Defaulted.
value_t value_type
The value type of the allocation.
Definition: aligned_allocator.hpp:77
pointer allocate(size_type const n) const
Allocates sufficiently large memory to hold n many elements of value_type.
Definition: aligned_allocator.hpp:135
typename std::pointer_traits< pointer >::difference_type difference_type
The difference type of the allocation.
Definition: aligned_allocator.hpp:81
constexpr bool operator==(aligned_allocator< value_type2, alignment2 > const &) noexcept
Returns true if the memory-alignment matches.
Definition: aligned_allocator.hpp:207
static constexpr size_t alignment
The memory-alignment of the allocation.
Definition: aligned_allocator.hpp:74
Provides platform and dependency checks.
T max(T... args)
The ranges module's namespace.
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