1 1.1 mrg // align implementation -*- C++ -*- 2 1.1 mrg 3 1.1.1.2 mrg // Copyright (C) 2014-2024 Free Software Foundation, Inc. 4 1.1 mrg // 5 1.1 mrg // This file is part of the GNU ISO C++ Library. This library is free 6 1.1 mrg // software; you can redistribute it and/or modify it under the 7 1.1 mrg // terms of the GNU General Public License as published by the 8 1.1 mrg // Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg // any later version. 10 1.1 mrg 11 1.1 mrg // This library is distributed in the hope that it will be useful, 12 1.1 mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg // GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg // Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg // permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg // 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg // You should have received a copy of the GNU General Public License and 21 1.1 mrg // a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg // <http://www.gnu.org/licenses/>. 24 1.1 mrg 25 1.1 mrg /** @file bits/align.h 26 1.1 mrg * This is an internal header file, included by other library headers. 27 1.1 mrg * Do not attempt to use it directly. @headername{memory} 28 1.1 mrg */ 29 1.1 mrg 30 1.1 mrg #ifndef _GLIBCXX_ALIGN_H 31 1.1 mrg #define _GLIBCXX_ALIGN_H 1 32 1.1 mrg 33 1.1 mrg #include <bit> // std::has_single_bit 34 1.1 mrg #include <stdint.h> // uintptr_t 35 1.1 mrg #include <debug/assertions.h> // _GLIBCXX_DEBUG_ASSERT 36 1.1.1.2 mrg #include <bits/version.h> 37 1.1 mrg 38 1.1 mrg namespace std _GLIBCXX_VISIBILITY(default) 39 1.1 mrg { 40 1.1 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 41 1.1 mrg 42 1.1 mrg /** 43 1.1 mrg * @brief Fit aligned storage in buffer. 44 1.1 mrg * 45 1.1 mrg * This function tries to fit @a __size bytes of storage with alignment 46 1.1 mrg * @a __align into the buffer @a __ptr of size @a __space bytes. If such 47 1.1 mrg * a buffer fits then @a __ptr is changed to point to the first byte of the 48 1.1 mrg * aligned storage and @a __space is reduced by the bytes used for alignment. 49 1.1 mrg * 50 1.1 mrg * C++11 20.6.5 [ptr.align] 51 1.1 mrg * 52 1.1 mrg * @param __align A fundamental or extended alignment value. 53 1.1 mrg * @param __size Size of the aligned storage required. 54 1.1 mrg * @param __ptr Pointer to a buffer of @a __space bytes. 55 1.1 mrg * @param __space Size of the buffer pointed to by @a __ptr. 56 1.1 mrg * @return the updated pointer if the aligned storage fits, otherwise nullptr. 57 1.1 mrg * 58 1.1 mrg * @ingroup memory 59 1.1 mrg */ 60 1.1 mrg inline void* 61 1.1 mrg align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept 62 1.1 mrg { 63 1.1 mrg if (__space < __size) 64 1.1 mrg return nullptr; 65 1.1 mrg const auto __intptr = reinterpret_cast<uintptr_t>(__ptr); 66 1.1 mrg const auto __aligned = (__intptr - 1u + __align) & -__align; 67 1.1 mrg const auto __diff = __aligned - __intptr; 68 1.1 mrg if (__diff > (__space - __size)) 69 1.1 mrg return nullptr; 70 1.1 mrg else 71 1.1 mrg { 72 1.1 mrg __space -= __diff; 73 1.1 mrg return __ptr = reinterpret_cast<void*>(__aligned); 74 1.1 mrg } 75 1.1 mrg } 76 1.1 mrg 77 1.1.1.2 mrg #ifdef __glibcxx_assume_aligned // C++ >= 20 78 1.1 mrg /** @brief Inform the compiler that a pointer is aligned. 79 1.1 mrg * 80 1.1 mrg * @tparam _Align An alignment value (i.e. a power of two) 81 1.1 mrg * @tparam _Tp An object type 82 1.1 mrg * @param __ptr A pointer that is aligned to _Align 83 1.1 mrg * 84 1.1 mrg * C++20 20.10.6 [ptr.align] 85 1.1 mrg * 86 1.1 mrg * @ingroup memory 87 1.1 mrg */ 88 1.1 mrg template<size_t _Align, class _Tp> 89 1.1 mrg [[nodiscard,__gnu__::__always_inline__]] 90 1.1 mrg constexpr _Tp* 91 1.1 mrg assume_aligned(_Tp* __ptr) noexcept 92 1.1 mrg { 93 1.1 mrg static_assert(std::has_single_bit(_Align)); 94 1.1 mrg if (std::is_constant_evaluated()) 95 1.1 mrg return __ptr; 96 1.1 mrg else 97 1.1 mrg { 98 1.1 mrg // This function is expected to be used in hot code, where 99 1.1 mrg // __glibcxx_assert would add unwanted overhead. 100 1.1 mrg _GLIBCXX_DEBUG_ASSERT((uintptr_t)__ptr % _Align == 0); 101 1.1 mrg return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align)); 102 1.1 mrg } 103 1.1 mrg } 104 1.1.1.2 mrg #endif // __glibcxx_assume_aligned 105 1.1 mrg 106 1.1 mrg _GLIBCXX_END_NAMESPACE_VERSION 107 1.1 mrg } // namespace 108 1.1 mrg 109 1.1 mrg #endif /* _GLIBCXX_ALIGN_H */ 110