]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Adjust arguments to aligned_alloc or posix_memalign
authorJonathan Wakely <jwakely@redhat.com>
Fri, 16 Sep 2016 12:11:19 +0000 (13:11 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 16 Sep 2016 12:11:19 +0000 (13:11 +0100)
* libsupc++/new_opa.cc [_GLIBCXX_HAVE_POSIX_MEMALIGN] (aligned_alloc):
Increase alignment if less than sizeof(void*).
[_GLIBCXX_HAVE_ALIGNED_ALLOC] (operator new(size_t, align_val_t)):
Increase size if not a multiple of alignment.

From-SVN: r240187

libstdc++-v3/ChangeLog
libstdc++-v3/libsupc++/new_opa.cc

index f81e87f9694489d898f1a066b1d30e8080a624c9..cddff99466dfe96d99f76a1e0656964389f55a7a 100644 (file)
@@ -1,3 +1,10 @@
+2016-09-16  Jonathan Wakely  <jwakely@redhat.com>
+
+       * libsupc++/new_opa.cc [_GLIBCXX_HAVE_POSIX_MEMALIGN] (aligned_alloc):
+       Increase alignment if less than sizeof(void*).
+       [_GLIBCXX_HAVE_ALIGNED_ALLOC] (operator new(size_t, align_val_t)):
+       Increase size if not a multiple of alignment.
+
 2016-09-15  Jonathan Wakely  <jwakely@redhat.com>
 
        * doc/xml/manual/debug_mode.xml: Minor editorial fixes.
index 6ff5421bd152ba98d76a963a9d1a7a45bcd7d4ea..9c859c1b61b21ad91e2e3787ba1a6dc8e74b751e 100644 (file)
@@ -39,6 +39,9 @@ static inline void*
 aligned_alloc (std::size_t al, std::size_t sz)
 {
   void *ptr;
+  // The value of alignment shall be a power of two multiple of sizeof(void *).
+  if (al < sizeof(void*))
+    al = sizeof(void*);
   int ret = posix_memalign (&ptr, al, sz);
   if (ret == 0)
     return ptr;
@@ -58,13 +61,19 @@ _GLIBCXX_WEAK_DEFINITION void *
 operator new (std::size_t sz, std::align_val_t al)
 {
   void *p;
+  std::size_t align = (std::size_t)al;
 
   /* malloc (0) is unpredictable; avoid it.  */
   if (sz == 0)
     sz = 1;
 
-  while (__builtin_expect ((p = aligned_alloc ((std::size_t)al, sz)) == 0,
-                          false))
+#if _GLIBCXX_HAVE_ALIGNED_ALLOC
+  /* C11: the value of size shall be an integral multiple of alignment.  */
+  if (std::size_t rem = sz % align)
+    sz += align - rem;
+#endif
+
+  while (__builtin_expect ((p = aligned_alloc (align, sz)) == 0, false))
     {
       new_handler handler = std::get_new_handler ();
       if (! handler)