This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] libstdc++-v3: Have aligned_alloc() on Newlib

On 08/08/18 15:33 +0100, Jonathan Wakely wrote:
On 08/08/18 16:22 +0200, Ulrich Weigand wrote:
Jonathan Wakely wrote:

Aha, so newlib was using memalign previously:

@@ -53,20 +54,24 @@ aligned_alloc (std::size_t al, std::size_t sz)
extern "C" void *memalign(std::size_t boundary, std::size_t size);
-#define aligned_alloc memalign

Yes, exactly ... this commit introduced the regression.

OK, I've regressed the branches then - I'll fix that.

This should fix it. I'll finish testing and commit it.

Sebastian, your patch to define HAVE_ALIGNED_ALLOC is OK for
gcc-7-branch and gcc-8-branch, because changing newlib from using
memalign to aligned_alloc is safe.

With the patch this time ...

commit cb2a7aae2668b690cfaed5093e0107e0ee64bb0e
Author: Jonathan Wakely <>
Date:   Wed Aug 8 15:28:48 2018 +0100

    Prevent internal aligned_alloc clashing with libc version
    If configure fails to detect aligned_alloc we will try to define our
    own in but that could clash with the libcversion in
    <stdlib.h>. Use a namespace to keep them distinct.
            * libsupc++/ (aligned_alloc): Declare inside namespace to
            avoid clashing with an ::aligned_alloc function that was not detected
            by configure.

diff --git a/libstdc++-v3/libsupc++/ b/libstdc++-v3/libsupc++/
index 5be0cc2ca65..68eac5b8ceb 100644
--- a/libstdc++-v3/libsupc++/
+++ b/libstdc++-v3/libsupc++/
@@ -25,15 +25,30 @@
 #include <bits/c++config.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <bits/exception_defines.h>
 #include "new"
+# if _GLIBCXX_HOSTED && __has_include(<malloc.h>)
+// Some C libraries declare memalign in <malloc.h>
+#  include <malloc.h>
+# else
+extern "C" void *memalign(std::size_t boundary, std::size_t size);
+# endif
 using std::new_handler;
 using std::bad_alloc;
-#define aligned_alloc(al,sz) _aligned_malloc(sz,al)
+namespace __gnu_cxx {
+using ::aligned_alloc;
+static inline void*
+aligned_alloc (std::size_t al, std::size_t sz)
+{ return _aligned_malloc(sz, al); }
 static inline void*
 aligned_alloc (std::size_t al, std::size_t sz)
@@ -49,11 +64,6 @@ aligned_alloc (std::size_t al, std::size_t sz)
   return nullptr;
-#include <malloc.h>
-extern "C" void *memalign(std::size_t boundary, std::size_t size);
 static inline void*
 aligned_alloc (std::size_t al, std::size_t sz)
@@ -66,7 +76,6 @@ aligned_alloc (std::size_t al, std::size_t sz)
   return memalign (al, sz);
-#include <stdint.h>
 // The C library doesn't provide any aligned allocation functions, define one.
 // This is a modified version of code from gcc/config/i386/gmm_malloc.h
 static inline void*
@@ -87,7 +96,7 @@ aligned_alloc (std::size_t al, std::size_t sz)
   return aligned_ptr;
+} // namespace __gnu_cxx
 operator new (std::size_t sz, std::align_val_t al)
@@ -116,6 +125,7 @@ operator new (std::size_t sz, std::align_val_t al)
     sz += align - rem;
+  using __gnu_cxx::aligned_alloc;
   while (__builtin_expect ((p = aligned_alloc (align, sz)) == 0, false))
       new_handler handler = std::get_new_handler ();

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]