Page MenuHomeFreeBSD

lib/libc: implement C23 memalignment()
ClosedPublic

Authored by fuz on Mon, Nov 10, 5:57 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Nov 14, 1:30 PM
Unknown Object (File)
Fri, Nov 14, 1:30 PM
Unknown Object (File)
Fri, Nov 14, 1:30 PM
Unknown Object (File)
Fri, Nov 14, 1:30 PM
Unknown Object (File)
Fri, Nov 14, 1:30 PM
Unknown Object (File)
Fri, Nov 14, 10:46 AM
Unknown Object (File)
Thu, Nov 13, 8:43 AM
Unknown Object (File)
Wed, Nov 12, 1:56 AM

Details

Summary

This new function computes the alignment of a pointer.
If the pointer is a null pointer, null is returned.
I have tried to write an implementation that can cope
with traditional address-based architectures, even if
size_t and uintptr_t are of different length. Adjustments
may be needed for CHERI though.

A man page is provided, too. No unit test for now.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

fuz requested review of this revision.Mon, Nov 10, 5:57 PM
lib/libc/stdlib/memalignment.3
2

The hyphen was for a license parser that Warner abandoned years ago.

lib/libc/stdlib/memalignment.3
2

The hyphen follows style(9). Are you sure I should diverge from it?

lib/libc/stdlib/memalignment.c
20

Um, shouldn’t this be ~’ed? Whether as ~(align - 1) or ~align + 1 (or just -align in two’s complement).

fuz planned changes to this revision.Mon, Nov 10, 6:37 PM
fuz added inline comments.
lib/libc/stdlib/memalignment.c
20

Ah... brain fart time ...

  • correct alignment computation
lib/libc/stdlib/memalignment.3
2

We took the hyphen out of style(9) in February.

This looks good to my eye.

include/stdlib.h
174

I'm guessing this is too new for POSIX alignment, eh?

This revision is now accepted and ready to land.Mon, Nov 10, 8:10 PM
kib added inline comments.
lib/libc/stdlib/memalignment.c
20

This would deserve a comment that we expect 2-complement.

BTW, I think we must compile libc with -fwrapv, if we don't already.

include/stdlib.h
174

Yeah, issue 8 doesn't seem to have it.

lib/libc/stdlib/memalignment.c
20

uintptr_t is an unsigned type, so neither is necessary.

What may be possible is removing lines 15 and 16. I'll have to check if null pointers always become zero when cast to integer.

lib/libc/stdlib/memalignment.c
20

uintptr_t is an unsigned type, so neither is necessary.

What it means -a for unsigned then?

What may be possible is removing lines 15 and 16. I'll have to check if null pointers always become zero when cast to integer.

I did not said that -fwrapv is necessary for this commit, I said that we should do this for libc in general (and rtld/libsys/libtr but there I would expect it less significant).

lib/libc/stdlib/memalignment.c
20

What it means -a for unsigned then?

The standard just says (ISO/IEC 9899:2024 § 6.5.4.3 ¶ 3):

The result of the unary - operator is the negative of its (promoted) operand. The integer promotions

are performed on the operand, and the result has the promoted type.

The negative of an unsigned number is the same as subtracting that number from zero, which gives ~a + 1.

Please also have a look at D53657 and descendent commits if you have some time.

  • remove - from /*- and .\"- in memalignment.[3c] as per style(9)
This revision now requires review to proceed.Tue, Nov 11, 4:00 PM

Approved.

lib/libc/stdlib/memalignment.c
25
This revision is now accepted and ready to land.Tue, Nov 11, 7:19 PM
kib added inline comments.
include/stdlib.h
172

In fact, this is not extension but the part of the standard

pauamma_gundo.com added inline comments.
lib/libc/stdlib/memalignment.3
37
This revision was automatically updated to reflect the committed changes.