[patch] aligned_alloc: conform to C17

Paul Eggert eggert@cs.ucla.edu
Fri Mar 17 06:33:50 GMT 2023


On 2023-03-16 17:00, DJ Delorie wrote:
> Paul Eggert <eggert@cs.ucla.edu> writes:
>> powerof2 (0) == 1, unfortunately....
> _mid_memalign enforces a minimum alignment so this isn't an issue, other
> than the pedantry of whether 0 is an iso-valid alignment.

Yes, it's the pedantry I was worried about. (That's the main point of 
the patch anyway - namely, conforming to the standard.)


> As for "non-negative alignment" I assume they mean "passing a negative
> number as `alignment' to aligned_alloc()", not referring to the power of
> two itself.

As you mention the C17 wording is not entirely clear, but it's 
implausible that the standardizers intended 0 to be a valid alignment: 
if they had intended that they would have said so clearly.

There's also a commensense argument: a pointer value aligned to A is in 
some sense a multiple of A. But the only multiple of zero is zero. So 
specifying an alignment of zero makes no intuitive sense, except perhaps 
for the all-bits-zero pointer.


> I suspect fixing the macro might be more generally useful than fixing
> this edge case

Although I haven't looked at this in detail, I suspect that changing the 
macro will merely slow other code down slightly. It might be better to 
leave the macro alone (perhaps changing its comment), and to fix just 
aligned_alloc.



More information about the Libc-alpha mailing list