AlignedAllocator#
#include <hpk/alignedAllocator.hpp>
The AlignedAllocator class is an Allocator that provides cacheline-aligned memory, which is recommended for performance reasons.
This header file also defines the free function allocateMemory()
, which uses
AlignedAllocator by default to provide memory managed by a std::unique_ptr
.
This is a header-only implementation.
CLASS
-
template<class T, std::size_t align = hardware_constructive_interference_size>
struct AlignedAllocator# An allocator that provides aligned memory.
AlignedAllocator
allocates memory usingstd::aligned_alloc
and satisfies the C++ named requirements for an Allocator.The default alignment is
std::hardware_constructive_interference_size
if that is defined; otherwise, 64 bytes.Examples:
// Construct a std::vector whose data is (the default) 64B-aligned and // initialize it with 10 floats of value 0.0f. (Note that the type of // v1 is not std::vector<float>, as that uses std::allocator.) auto v1 = std::vector<float, hpk::AlignedAllocator<float>>(10); // Construct an empty std::vector whose data will be 32B-aligned. // (Note that the types of v1 and v2 are not the same.) auto v2 = std::vector<float, hpk::AlignedAllocator<float, 32>>();
Public Functions
-
inline constexpr AlignedAllocator() noexcept#
Default constructor.
-
template<class U>
inline constexpr AlignedAllocator(const AlignedAllocator<U, align>&) noexcept# Copy constructor.
-
inline T *allocate(std::size_t n) const#
Allocates uninitialized storage suitable for an array object of type
T
.Allocates aligned memory for
n
elements of typeT
, but does not construct array elements. Ifn == 0
, returnsnullptr
. If allocation fails and exceptions are enabled, an exception is thrown. If allocation fails and exceptions are disabled (e.g., by a compiler flag), thennullptr
is returned.
-
inline void deallocate(T *p, std::size_t) const noexcept#
Deallocates storage pointed to by
p
, which was a value returned by a previous call toallocate()
.Note that this does not call the destructor of the object pointed to by
p
.
-
template<class L, std::size_t alignL, class R, std::size_t alignR>
inline bool operator==(const AlignedAllocator<L, alignL>&, const AlignedAllocator<R, alignR>&) noexcept# Returns
true
. The storage allocated by anyAlignedAllocator
can be deallocated through another one, regardless of template parameters.
-
template<class L, std::size_t alignL, class R, std::size_t alignR>
inline bool operator!=(const AlignedAllocator<L, alignL>&, const AlignedAllocator<R, alignR>&) noexcept# Returns
false
.
-
template<class U>
struct rebind#
-
inline constexpr AlignedAllocator() noexcept#
FUNCTION
-
template<typename T, class Allocator = AlignedAllocator<T>>
auto hpk::allocateMemory(std::size_t n, Allocator &&alloc = Allocator())# Allocates memory for
n
elements of typeT
More specifically, the return value is a
std::unique_ptr<T, hpk::Deleter<Allocator>>
. If Allocator is not specified,hpk::AlignedAllocator<T>
is the default. Note thatT
must be trivially destructible (e.g., a non-class type compatible with the C language).Examples:
// Allocate (the default) 64B-aligned memory for 10 floats. auto tmp1 = hpk::allocateMemory<float>(10); // Allocate 8B-aligned memory for 20 floats. auto tmp2 = hpk::allocateMemory<float, hpk::AlignedAllocator<float, 8>>(20); // Allocate memory for 30 floats using the standard allocator. auto tmp3 = hpk::allocateMemory<float, std::allocator<float>>(30); // Use an instance to allocate 128B-aligned memory for 40 doubles. auto alloc = hpk::AlignedAllocator<double, 128>(); auto tmp4 = hpk::allocateMemory<double>(40, alloc);
- Returns:
std::unique_ptr
that owns the allocated memory.