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

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?)

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


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? Since align has to be a power of 2, x % align should be the same as x & (align - 1), for instance.

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

Reply via email to