Loading...
Searching...
No Matches
aligned_allocator.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <algorithm>
#include <limits>
#include <memory>
#include <type_traits>
#include <seqan3/core/platform.hpp>
namespace seqan3
{
template <typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
class aligned_allocator
{
public:
static constexpr size_t alignment = alignment_v;
using value_type = value_t;
using pointer = value_type *;
using difference_type = typename std::pointer_traits<pointer>::difference_type;
using size_type = std::make_unsigned_t<difference_type>;
using is_always_equal = std::true_type;
aligned_allocator() = default;
aligned_allocator(aligned_allocator const &) = default;
aligned_allocator(aligned_allocator &&) = default;
aligned_allocator & operator=(aligned_allocator const &) = default;
aligned_allocator & operator=(aligned_allocator &&) = default;
~aligned_allocator() = default;
template <class other_value_type, size_t other_alignment>
constexpr aligned_allocator(aligned_allocator<other_value_type, other_alignment> const &) noexcept
{}
[[nodiscard]] pointer allocate(size_type const n) const
{
constexpr size_type max_size = std::numeric_limits<size_type>::max() / sizeof(value_type);
if (n > max_size)
throw std::bad_alloc{};
size_t bytes_to_allocate = n * sizeof(value_type);
if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
return static_cast<pointer>(::operator new(bytes_to_allocate));
else // Use alignment aware allocator function.
return static_cast<pointer>(::operator new(bytes_to_allocate, static_cast<std::align_val_t>(alignment)));
}
void deallocate(pointer const p, size_type const n) const noexcept
{
size_t bytes_to_deallocate = n * sizeof(value_type);
if constexpr (alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
::operator delete(p, bytes_to_deallocate);
else // Use alignment aware deallocator function.
::operator delete(p, bytes_to_deallocate, static_cast<std::align_val_t>(alignment));
}
template <typename new_value_type>
struct rebind
{
static constexpr size_t other_alignment = std::max(alignof(new_value_type), alignment);
using other = aligned_allocator<new_value_type, other_alignment>;
};
template <class value_type2, size_t alignment2>
constexpr bool operator==(aligned_allocator<value_type2, alignment2> const &) noexcept
{
return alignment == alignment2;
}
template <class value_type2, size_t alignment2>
constexpr bool operator!=(aligned_allocator<value_type2, alignment2> const &) noexcept
{
return alignment != alignment2;
}
};
} // namespace seqan3
algorithm
std::align_val_t
std::bad_alloc
seqan3::aligned_allocator< element_type, sizeof(element_type)>::value_type
element_type value_type
Definition aligned_allocator.hpp:76
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:173
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 &&)=default
Defaulted.
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:134
seqan3::aligned_allocator< element_type, sizeof(element_type)>::pointer
value_type * pointer
Definition aligned_allocator.hpp:78
seqan3::aligned_allocator::operator=
aligned_allocator & operator=(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
seqan3::aligned_allocator::~aligned_allocator
~aligned_allocator()=default
Defaulted.
seqan3::aligned_allocator< element_type, sizeof(element_type)>::is_always_equal
std::true_type is_always_equal
Definition aligned_allocator.hpp:85
seqan3::aligned_allocator< element_type, sizeof(element_type)>::size_type
std::make_unsigned_t< difference_type > size_type
Definition aligned_allocator.hpp:82
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::aligned_allocator
aligned_allocator(aligned_allocator const &)=default
Defaulted.
seqan3::aligned_allocator< element_type, sizeof(element_type)>::alignment
static constexpr size_t alignment
Definition aligned_allocator.hpp:73
seqan3::aligned_allocator::aligned_allocator
aligned_allocator(aligned_allocator &&)=default
Defaulted.
seqan3::aligned_allocator::aligned_allocator
aligned_allocator()=default
Defaulted.
seqan3::aligned_allocator< element_type, sizeof(element_type)>::difference_type
typename std::pointer_traits< pointer >::difference_type difference_type
Definition aligned_allocator.hpp:80
std::true_type
limits
std::make_unsigned_t
std::numeric_limits::max
T max(T... args)
memory
seqan3
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
platform.hpp
Provides platform and dependency checks.
std::pointer_traits
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::rebind::other_alignment
static constexpr size_t other_alignment
The alignment for the rebound allocator.
Definition aligned_allocator.hpp:197
seqan3::aligned_allocator::rebind::other
aligned_allocator< new_value_type, other_alignment > other
The type of the allocator for a different value type.
Definition aligned_allocator.hpp:199
type_traits
Hide me