tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: aligned_alloc c11 function

> On 5 Nov 2015, at 21:35 , Robert Elz <kre%munnari.OZ.AU@localhost> wrote:
>    Date:        Thu, 5 Nov 2015 18:24:36 -0800
>    From:        Dennis Ferguson <>
>    Message-ID:  <>
>  | That said, I do think this should be done your way for functional reasons.
>  | Your code does what I think is the right thing in the case where both size
>  | and alignment are zero-valued, but the while() loop code probably doesn't.
> 0 is not a power of two, and hence is invalid as the alignment,
> according to the proposed interface spec.   It also makes no sense.

Yes, I realized I’d goofed that about 3 minutes after I’d posted, but
I’d already turned off the computer and was too sleepy to turn it back
on.  I’d fixated on the idea that

	(x & (x - 1)) == 0

is true only if `x’ is a power of 2 but that’s clearly wrong.  The test
for a power of 2 needs to be

	(x & (x - 1)) == 0 && x != 0

> What's more, the code already checks for that, and returns EINVAL if
> alignment == 0, so the while loop doesn’t need to worry about that case.

Ah, the code I was looking at didn’t do that and failed when both
alignment == 0 and size == 0.  It was that bug that made me think the
while() loop was a problem, but that isn’t what needed fixing.  I
assume the explicit check for alignment == 0 got added to the if() when
I wasn’t paying attention?

Dennis Ferguson

Home | Main Index | Thread Index | Old Index