]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/bits/align.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / align.h
index faa92bec2f892bec7cbabb88f93f203c57da52f1..1a06b814fbb6a0d8c791a158f490987008e7c2fb 100644 (file)
@@ -1,6 +1,6 @@
 // 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)
 {
@@ -60,10 +60,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 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
     {
@@ -72,8 +74,7 @@ align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
     }
 }
 
-#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)
@@ -100,7 +101,7 @@ align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
          return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
        }
     }
-#endif // C++2a
+#endif // __glibcxx_assume_aligned
 
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace