// Support routines for the -*- C++ -*- dynamic memory management.
-// Copyright (C) 1997-2019 Free Software Foundation, Inc.
+// Copyright (C) 1997-2024 Free Software Foundation, Inc.
//
// This file is part of GCC.
//
#include <bits/c++config.h>
#include <stdlib.h>
#include <stdint.h>
-#include <bits/exception_defines.h>
#include <bit>
#include "new"
using std::new_handler;
using std::bad_alloc;
+#if ! _GLIBCXX_HOSTED
+using std::size_t;
+extern "C"
+{
+# if _GLIBCXX_HAVE_ALIGNED_ALLOC
+ void *aligned_alloc(size_t alignment, size_t size);
+# elif _GLIBCXX_HAVE__ALIGNED_MALLOC
+ void *_aligned_malloc(size_t size, size_t alignment);
+# elif _GLIBCXX_HAVE_POSIX_MEMALIGN
+ void *posix_memalign(void **, size_t alignment, size_t size);
+# elif _GLIBCXX_HAVE_MEMALIGN
+ void *memalign(size_t alignment, size_t size);
+# else
+ // A freestanding C runtime may not provide "malloc" -- but there is no
+ // other reasonable way to implement "operator new".
+ void *malloc(size_t);
+# endif
+}
+#endif
+
namespace __gnu_cxx {
#if _GLIBCXX_HAVE_ALIGNED_ALLOC
using ::aligned_alloc;
static inline void*
aligned_alloc (std::size_t al, std::size_t sz)
{
+ // Solaris requires al >= sizeof a word and QNX requires >= sizeof(void*)
+ // but they both provide posix_memalign, so will use the definition above.
return memalign (al, sz);
}
#else // !HAVE__ALIGNED_MALLOC && !HAVE_POSIX_MEMALIGN && !HAVE_MEMALIGN
/* Alignment must be a power of two. */
/* XXX This should be checked by the compiler (PR 86878). */
- if (__builtin_expect (!std::__ispow2(align), false))
+ if (__builtin_expect (!std::__has_single_bit(align), false))
_GLIBCXX_THROW_OR_ABORT(bad_alloc());
/* malloc (0) is unpredictable; avoid it. */
sz = 1;
#if _GLIBCXX_HAVE_ALIGNED_ALLOC
-# ifdef _AIX
+# if defined _AIX || defined __APPLE__
/* AIX 7.2.0.0 aligned_alloc incorrectly has posix_memalign's requirement
- * that alignment is a multiple of sizeof(void*). */
+ * that alignment is a multiple of sizeof(void*).
+ * OS X 10.15 has the same requirement. */
if (align < sizeof(void*))
align = sizeof(void*);
# endif