RFA (libstdc++): PATCH to implement C++17 over-aligned new

Jonathan Wakely jwakely@redhat.com
Fri Sep 16 11:12:00 GMT 2016


On 16/09/16 11:37 +0200, Marc Glisse wrote:
>On Fri, 16 Sep 2016, Jonathan Wakely wrote:
>
>>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).
>
>+  // The value of alignment shall be a power of two multiple of sizeof(void *).
>+  if (al < sizeof(void*))
>+    al = sizeof(void*);
>
>The code doesn't exactly match the comment. I can't find the 
>precondition in the standard that says operator new can only be called 
>on a power of 2... (maybe we can add it if it is really missing?)

[basic.align] says "Every alignment value shall be a non-negative
integral power of two." So asking operator new for any other value
doesn't make sense, but I can't find a restriction on doing so.

I was assuming we only need to ensure it's possible to use valid
alignments such as align_val_t(2) which are not valid arguments to
posix_memalign. For other values such as align_val_t(15) I was
assuming it's OK for posix_memalign to fail, so we throw bad_alloc.

If that's not the case then we need to round up all alignments that
aren't power of two multiples of sizeof(void*). I'd like to avoid
that.

>>Would using __builtin_expect (sz == 0, false) make sense?  Surely it's
>>rare to try to allocate zero bytes.
>
>https://gcc.gnu.org/ml/libstdc++/2014-03/msg00001.html
>
>gcc already guesses that a test like sz == 0 is usually false (not 
>with as large a probability as if you use __builtin_expect, but enough 
>that the generated code is unlikely to differ). But adding 
>__builtin_expect cannot hurt...
>
>Is the division (by a non-constant denominator) really necessary? 

Probably not, but I've asked the committee for clarification what this
function should do when called with an invalid alignment.

>Since align has to be a power of 2, x % align should be the same as x 
>& (align - 1), for instance.

Thanks, if it's UB to call it with alignments that aren't a power of
two then we can do that.

>I guess people interested in performance will do for aligned new the 
>same as for the old new: provide an inline version that skips all the 
>overhead to forward directly to malloc/aligned_alloc (and avoid 
>questionable calls in their code).
>
>-- 
>Marc Glisse



More information about the Gcc-patches mailing list