tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
RE: aligned_alloc c11 function
Hi,
One small thing.
I don't understand why we're doing this calculation as a while():
/*
* Adjust alignment to satisfy posix_memalign,
* larger alignments satisfy smaller alignments.
*/
while (alignment < sizeof (void *)) {
alignment <<= 1;
}
We know alignment is a power of two, therefore the result of this is a
constant and could be done as an if() with a
pre-computed constant:
if (alignment < MIN_ALIGNMENT)
alignment = MIN_ALIGNMENT;
where MIN_ALIGNMENT is the smallest power of 2 not less than sizeof(void*).
(On all real platforms, MIN_ALIGNMENT == sizeof(void *) but a purist might
want to use compile-time log2 macros to compute MIN_ALIGNMENT.)
I feel that writing a while() in this context is bad style as it implies
that some iterative computation is being done. It's not at all an iterative
calculation, in fact.
--Terry
> -----Original Message-----
> From: tech-userlevel-owner%NetBSD.org@localhost [mailto:tech-userlevel-
> owner%NetBSD.org@localhost] On Behalf Of Niclas Rosenvik
> Sent: Saturday, October 31, 2015 5:01
> To: tech-userlevel%netbsd.org@localhost
> Subject: Re: aligned_alloc c11 function
>
> Hi everyone, here is the second take on aligned_alloc, I have incorporated
> the changes suggested by Christos and Robert Elz.
>
> Ok to commit?
>
> Regards,
> Niclas Rosenvik
>
>
Home |
Main Index |
Thread Index |
Old Index