libstdc++: align.h Source File

libstdc++

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