public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
Cc: Jason Merrill <jason@redhat.com>,
	       Christophe Lyon <christophe.lyon@linaro.org>,
	       Andreas Schwab <schwab@linux-m68k.org>,
	       "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>,
	       gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: Re: RFA (libstdc++): PATCH to implement C++17 over-aligned new
Date: Fri, 16 Sep 2016 09:14:00 -0000	[thread overview]
Message-ID: <20160916090427.GM17376@redhat.com> (raw)
In-Reply-To: <yddintwmijq.fsf@CeBiTec.Uni-Bielefeld.DE>

[-- Attachment #1: Type: text/plain, Size: 684 bytes --]

On 16/09/16 09:04 +0200, Rainer Orth wrote:
>Hi Jason,
>
>> OK, one more:
>
>this works just fine on both sparc-sun-solaris2.12 and
>i386-pc-solaris2.12.
>
>Once Jonathan's patch to heed aligned_alloc's requirement on size being
>a multiple of alignment is in, all is fine on Solaris.

I've got a slightly different fix now.

We only need to make the size a multiple of alignment for
aligned_alloc, however for posix_memalign we need to ensure the
alignment is a multiple of sizeof(void*).

I'm testing this now (but only on x86_64 GNU/Linux where it wasn't
failing anyway).

Would using __builtin_expect (sz == 0, false) make sense?  Surely it's
rare to try to allocate zero bytes.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 1623 bytes --]

commit 216b9547230295e615bab86aaede65554f63e57d
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 16 09:54:51 2016 +0100

    Adjust arguments to aligned_alloc or posix_memalign
    
    	* 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.

diff --git a/libstdc++-v3/libsupc++/new_opa.cc b/libstdc++-v3/libsupc++/new_opa.cc
index 6ff5421..9c859c1 100644
--- a/libstdc++-v3/libsupc++/new_opa.cc
+++ b/libstdc++-v3/libsupc++/new_opa.cc
@@ -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)

  parent reply	other threads:[~2016-09-16  9:04 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-08  7:10 Jason Merrill
2016-09-08  8:32 ` Marc Glisse
2016-09-08 11:18   ` Jonathan Wakely
2016-09-09 21:40     ` Jason Merrill
2016-09-10  7:03       ` Christophe Lyon
2016-09-10 10:14         ` Marc Glisse
2016-09-10 10:35           ` Jonathan Wakely
2016-09-11  9:14         ` Christophe Lyon
2016-09-11  9:55           ` Jonathan Wakely
2016-09-11  9:56             ` Jonathan Wakely
2016-09-11 10:20             ` Christophe Lyon
2016-09-11 12:09               ` Jonathan Wakely
2016-09-13 13:04         ` Szabolcs Nagy
2016-09-10 10:14       ` Jonathan Wakely
2016-09-11  7:09       ` Andreas Schwab
2016-09-12 21:13         ` Jason Merrill
2016-09-13  8:41           ` Christophe Lyon
2016-09-13 12:37           ` Andreas Schwab
2016-09-13 12:54             ` Jason Merrill
2016-09-13 13:18               ` Andreas Schwab
2016-09-13 13:21                 ` Jason Merrill
2016-09-14 12:13                   ` Andreas Schwab
2016-09-14 16:11                     ` Christophe Lyon
2016-09-14 16:37                       ` Jason Merrill
2016-09-15 10:00                         ` Rainer Orth
2016-09-15 12:23                           ` Christophe Lyon
2016-09-15 20:09                             ` Jason Merrill
2016-09-16  7:12                               ` Rainer Orth
2016-09-16  8:15                                 ` Christophe Lyon
2016-09-16  9:14                                 ` Jonathan Wakely [this message]
2016-09-16  9:51                                   ` Marc Glisse
2016-09-16 11:12                                     ` Jonathan Wakely
2016-09-16 13:13                                       ` Jonathan Wakely
2016-09-16 13:17                                         ` Rainer Orth
2016-09-16 18:19                                         ` Jonathan Wakely
2016-09-12 14:15       ` Rainer Orth
2016-09-12 16:19         ` Jonathan Wakely
2016-09-12 18:57           ` Jason Merrill
2016-09-14 12:11           ` Rainer Orth
2016-09-08 11:00 ` Jonathan Wakely
2017-11-24 14:26 ` Marc Glisse
2017-11-29 21:23   ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160916090427.GM17376@redhat.com \
    --to=jwakely@redhat.com \
    --cc=christophe.lyon@linaro.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ro@CeBiTec.Uni-Bielefeld.DE \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).