// align implementation -*- C++ -*-
-// Copyright (C) 2014-2020 Free Software Foundation, Inc.
+// Copyright (C) 2014-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
#ifndef _GLIBCXX_ALIGN_H
#define _GLIBCXX_ALIGN_H 1
-#include <bits/c++config.h>
-
#include <bit> // std::has_single_bit
#include <stdint.h> // uintptr_t
+#include <debug/assertions.h> // _GLIBCXX_DEBUG_ASSERT
+#include <bits/version.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
inline void*
align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
{
+ if (__space < __size)
+ return nullptr;
const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
const auto __aligned = (__intptr - 1u + __align) & -__align;
const auto __diff = __aligned - __intptr;
- if ((__size + __diff) > __space)
+ if (__diff > (__space - __size))
return nullptr;
else
{
}
}
-#if __cplusplus > 201703L
-#define __cpp_lib_assume_aligned 201811L
+#ifdef __glibcxx_assume_aligned // C++ >= 20
/** @brief Inform the compiler that a pointer is aligned.
*
* @tparam _Align An alignment value (i.e. a power of two)
return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
}
}
-#endif // C++2a
+#endif // __glibcxx_assume_aligned
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace