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