public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-5361] libgomp: Fix up aligned_alloc arguments [PR102838]
Date: Thu, 18 Nov 2021 08:10:16 +0000 (GMT)	[thread overview]
Message-ID: <20211118081016.B39E33858C2C@sourceware.org> (raw)

https://gcc.gnu.org/g:7a2aa63fad06a72d9770b08491f1a7809eac7c50

commit r12-5361-g7a2aa63fad06a72d9770b08491f1a7809eac7c50
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Nov 18 09:07:31 2021 +0100

    libgomp: Fix up aligned_alloc arguments [PR102838]
    
    C says that aligned_alloc size must be an integral multiple of alignment.
    While glibc doesn't care about it, apparently Solaris does.
    So, this patch decreases the priority of aligned_alloc among the other
    variants because it needs more work and can waste more memory and rounds
    up the size to multiple of alignment.
    
    2021-11-18  Jakub Jelinek  <jakub@redhat.com>
    
            PR libgomp/102838
            * alloc.c (gomp_aligned_alloc): Prefer _aligned_alloc over
            memalign over posix_memalign over aligned_alloc over fallback
            with malloc instead of aligned_alloc over _aligned_alloc over
            posix_memalign over memalign over fallback with malloc.  For
            aligned_alloc, round up size up to multiple of al.

Diff:
---
 libgomp/alloc.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/libgomp/alloc.c b/libgomp/alloc.c
index 6ff9cb9ffd2..3109b86a4d1 100644
--- a/libgomp/alloc.c
+++ b/libgomp/alloc.c
@@ -65,18 +65,24 @@ gomp_aligned_alloc (size_t al, size_t size)
   void *ret;
   if (al < sizeof (void *))
     al = sizeof (void *);
-#ifdef HAVE_ALIGNED_ALLOC
-  ret = aligned_alloc (al, size);
-#elif defined(HAVE__ALIGNED_MALLOC)
+#ifdef HAVE__ALIGNED_MALLOC
   ret = _aligned_malloc (size, al);
-#elif defined(HAVE_POSIX_MEMALIGN)
-  if (posix_memalign (&ret, al, size) != 0)
-    ret = NULL;
 #elif defined(HAVE_MEMALIGN)
   {
     extern void *memalign (size_t, size_t);
     ret = memalign (al, size);
   }
+#elif defined(HAVE_POSIX_MEMALIGN)
+  if (posix_memalign (&ret, al, size) != 0)
+    ret = NULL;
+#lif defined(HAVE_ALIGNED_ALLOC)
+  {
+    size_t sz = (size + al - 1) & ~(al - 1);
+    if (__builtin_expect (sz >= size, 1))
+      ret = aligned_alloc (al, sz);
+    else
+      ret = NULL;
+  }
 #else
   ret = NULL;
   if ((al & (al - 1)) == 0 && size)


                 reply	other threads:[~2021-11-18  8:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211118081016.B39E33858C2C@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.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).