tech-userlevel archive

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

Re: aligned_alloc c11 function



This is all silly - the while loop is perfectly fine, it is clear,
works, and in practice, costs exactly the same as the if version at
runtime - that's because for the architectures NetBSD runs on, 
regular malloc() always returns mem aligned at least sizeof(int), so
no-one is going to use alligned_allow() for that or smaller alignments.
That's 4 2 or 1.  The only other relevant power of 2 is 8, which is
not < sizeof(void *) on anything supported, so the test in the while
will generally always be false, and the <<= 1 stuff will normally never
get executed.   It is there for correctness, not to normally do anything.

But on the off chance that someone ports NetBSD to something where
sizeof(void *) == 30 or something weird like that, the code will work
properly if something needs 8 or 16 byte alignment.

Sure, the if version can also work, but as this discussion has shown,
working out just how to get it right, all the time, is messy, which means
less likely to be understood into the future.

Just do it the simple, obviously correct way, regardless of whether you
think using "while" where "if" would work is somehow unclean, and
where computing what could be a compile time constant looks like it might
cost more - it doesn't.

If this was somewhere where performance was critical, and it was actually
going to make a difference, I'd consider some complicated preprocessor
mess to optimise it by hand.   It isn't.

kre



Home | Main Index | Thread Index | Old Index