VCTR
Loading...
Searching...
No Matches
AlignedAllocator.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr
24{
26template <typename ElementType, size_t alignmentInBytes>
27requires (alignmentInBytes >= alignof (ElementType) && is::powerOfTwoInt<alignmentInBytes>)
29{
30public:
31 using value_type = ElementType;
32 using size_type = std::size_t;
33
34 AlignedAllocator() noexcept = default;
35
36 template <class OtherElementType>
38
39 [[nodiscard]] ElementType* allocate (size_t nElementsToAllocate)
40 {
41 if (nElementsToAllocate > std::numeric_limits<size_t>::max() / sizeof (ElementType))
42 {
43 throw std::bad_array_new_length();
44 }
45
46 const auto nBytesToAllocate = detail::nextMultipleOf<Config::maxSIMDRegisterSize> (nElementsToAllocate * sizeof (ElementType));
47
48// Aligned operator new is only available on macOS 10.14 or higher. Therefore, we use posix_memalign on non-Windows
49// platforms which is widely available on unix platforms to perform aligned allocation if not on Windows.
50#if VCTR_WINDOWS
51 return reinterpret_cast<ElementType*> (::operator new[] (nBytesToAllocate, std::align_val_t (alignmentInBytes)));
52#else
53 void* ptr;
54 auto status = posix_memalign (&ptr, alignmentInBytes, nBytesToAllocate);
55 if (status != 0)
56 throw std::bad_alloc();
57 return reinterpret_cast<ElementType*> (ptr);
58#endif
59 }
60
61 void deallocate (ElementType* allocatedPointer, [[maybe_unused]] size_t nBytesAllocated)
62 {
63#if VCTR_WINDOWS
64 ::operator delete[] (allocatedPointer, std::align_val_t (alignmentInBytes));
65#else
66 free (allocatedPointer);
67#endif
68 }
69
70 template <class T>
71 struct rebind
72 {
74 };
75};
76
77template <class ElementType, size_t alignmentInBytesLHS, size_t alignmentInBytesRHS>
79{
80 return alignmentInBytesLHS == alignmentInBytesRHS;
81}
82
83} // namespace vctr
Returns aligned pointers when allocations are requested.
Definition: AlignedAllocator.h:29
The main namespace of the VCTR project.
Definition: Array.h:24
constexpr bool operator==(const Lhs &lhs, const Rhs &rhs)
Compares lhs and rhs for equality.
Definition: VctrBase.h:1162
Definition: AlignedAllocator.h:72