SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
seqan3::aligned_allocator< value_t, alignment_v > Class Template Reference

Allocates uninitialized storage whose memory-alignment is specified by alignment. More...

#include <seqan3/range/container/aligned_allocator.hpp>

Classes

struct  rebind
 The aligned_allocator member template class aligned_allocator::rebind provides a way to obtain an allocator for a different type. More...
 

Public Types

using difference_type = typename std::pointer_traits< pointer >::difference_type
 The difference type of the allocation.
 
using is_always_equal = std::true_type
 Are any two allocators of the same aligned_allocator type always compare equal?
 
using pointer = value_type *
 The pointer type of the allocation.
 
using size_type = std::make_unsigned_t< difference_type >
 The size type of the allocation.
 
using value_type = value_t
 The value type of the allocation.
 

Public Member Functions

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. More...
 
void deallocate (pointer p, size_type) noexcept
 Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier call to allocate(). More...
 
Constructors, destructor and assignment
 aligned_allocator ()=default
 Defaulted.
 
 aligned_allocator (aligned_allocator const &)=default
 Defaulted.
 
 aligned_allocator (aligned_allocator &&)=default
 Defaulted.
 
aligned_allocatoroperator= (aligned_allocator const &)=default
 Defaulted.
 
aligned_allocatoroperator= (aligned_allocator &&)=default
 Defaulted.
 
 ~aligned_allocator ()=default
 Defaulted.
 
template<class other_value_type >
constexpr aligned_allocator (aligned_allocator< other_value_type, alignment > const &) noexcept
 Copy constructor with different value type.
 
Comparison operators
template<class value_type2 , size_t alignment2>
constexpr bool operator== (aligned_allocator< value_type2, alignment2 > const &) noexcept
 Returns true if the memory-alignment matches.
 
template<class value_type2 , size_t alignment2>
constexpr bool operator!= (aligned_allocator< value_type2, alignment2 > const &) noexcept
 Returns false if the memory-alignment mismatches.
 

Static Public Attributes

static constexpr size_t alignment = alignment_v
 The memory-alignment of the allocation.
 

Detailed Description

template<typename value_t, size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
class seqan3::aligned_allocator< value_t, alignment_v >

Allocates uninitialized storage whose memory-alignment is specified by alignment.

Template Parameters
value_tThe value type of the allocation.
alignment_vThe memory-alignment of the allocation.
#include <iostream>
#include <vector>
size_t memory_alignment(void * value, size_t alignment)
{
return (reinterpret_cast<size_t>(value) & (alignment - 1));
}
int main()
{
using namespace seqan3;
// 128-byte memory aligned and 16bit = 2byte address width for each element
// vector has no alignment and 16bit = 2byte address width for each element
std::vector<int16_t> vec_unaligned{1,2,3,4,5};
// 256-byte memory aligned and 32bit = 4byte address width for each element
for (auto && x: vec128)
std::cout << "Item: " << x << " (" << &x << ", 128-byte aligned offset: " << memory_alignment(&x, 128u) << ")\n";
for (auto && x: vec_unaligned)
std::cout << "Item: " << x << " (" << &x << ", unaligned start: " << memory_alignment(&x, 128u) << ")\n";
for (auto && x: vec256)
std::cout << "Item: " << x << " (" << &x << ", 256-byte aligned offset: " << memory_alignment(&x, 256u) << ")\n";
}

Will output something like:

Item: 1 (0x55d5d0722f00, 128-byte aligned offset: 0)
Item: 2 (0x55d5d0722f02, 128-byte aligned offset: 2)
Item: 3 (0x55d5d0722f04, 128-byte aligned offset: 4)
Item: 4 (0x55d5d0722f06, 128-byte aligned offset: 6)
Item: 5 (0x55d5d0722f08, 128-byte aligned offset: 8)
Item: 1 (0x55d5d0722f40, unaligned start: 64)
Item: 2 (0x55d5d0722f42, unaligned start: 66)
Item: 3 (0x55d5d0722f44, unaligned start: 68)
Item: 4 (0x55d5d0722f46, unaligned start: 70)
Item: 5 (0x55d5d0722f48, unaligned start: 72)
Item: 1 (0x55d5d0723000, 256-byte aligned offset: 0)
Item: 2 (0x55d5d0723004, 256-byte aligned offset: 4)
Item: 3 (0x55d5d0723008, 256-byte aligned offset: 8)
Item: 4 (0x55d5d072300c, 256-byte aligned offset: 12)
Item: 5 (0x55d5d0723010, 256-byte aligned offset: 16)

As you can see, in the case of the aligned_allocator it is guaranteed that the first element in the vector starts at offset 0.

See also
http://en.cppreference.com/w/cpp/concept/Allocator
http://en.cppreference.com/w/cpp/memory/c/aligned_alloc

Member Function Documentation

◆ allocate()

template<typename value_t , size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
pointer seqan3::aligned_allocator< value_t, alignment_v >::allocate ( size_type  n)
inline

Allocates n * sizeof(T) bytes of uninitialized storage by calling std::aligned_alloc, but it is unspecified when and how this function is called.

Exceptions
Throwsstd::bad_alloc if allocation fails.
See also
https://en.cppreference.com/w/cpp/memory/allocator/allocate

◆ deallocate()

template<typename value_t , size_t alignment_v = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
void seqan3::aligned_allocator< value_t, alignment_v >::deallocate ( pointer  p,
size_type   
)
inlinenoexcept

Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier call to allocate().

The argument n must be equal to the first argument of the call to allocate() that originally produced p; otherwise, the behavior is undefined.

Calls std::free, but it is unspecified when and how it is called.

See also
https://en.cppreference.com/w/cpp/memory/allocator/deallocate

The documentation for this class was generated from the following file: