From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 56567 invoked by alias); 16 Sep 2016 09:04:41 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 56542 invoked by uid 89); 16 Sep 2016 09:04:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Increase X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 16 Sep 2016 09:04:30 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E56BEC05B1E9; Fri, 16 Sep 2016 09:04:28 +0000 (UTC) Received: from localhost (ovpn-116-87.ams2.redhat.com [10.36.116.87]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8G94RDi006670; Fri, 16 Sep 2016 05:04:28 -0400 Date: Fri, 16 Sep 2016 09:14:00 -0000 From: Jonathan Wakely To: Rainer Orth Cc: Jason Merrill , Christophe Lyon , Andreas Schwab , "libstdc++@gcc.gnu.org" , gcc-patches List Subject: Re: RFA (libstdc++): PATCH to implement C++17 over-aligned new Message-ID: <20160916090427.GM17376@redhat.com> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="gBBFr7Ir9EOA20Yy" Content-Disposition: inline In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.0 (2016-08-17) X-SW-Source: 2016-09/txt/msg01020.txt.bz2 --gBBFr7Ir9EOA20Yy Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-length: 684 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. --gBBFr7Ir9EOA20Yy Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" Content-length: 1623 commit 216b9547230295e615bab86aaede65554f63e57d Author: Jonathan Wakely 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) --gBBFr7Ir9EOA20Yy--