From nobody Fri Dec 12 11:59:04 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dSSfc1pwSz6Knh1 for ; Fri, 12 Dec 2025 11:59:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dSSfc0p6Kz3FnR for ; Fri, 12 Dec 2025 11:59:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765540744; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=y3vZJKcYgZXMSEzQEFcIcXTD1lMTjWSOjP6RsKFdCgk=; b=vJ3vhkqXbPF0ZPAr++EQh6A/QjymbNUlsZBbf7siS8m5rEhLK24NMhivC+TSjRt8iSlTxL R4MhykPAngNyockfpTnJ8CX+BMddLseHuJO/Sts5OijF5aYunT91FHDn4VgbyqmnHoAfcg e80VF7YfCcNWQQoYQwdY01G6AJkP5+HCq1stRNzk/ZC0a3DunrDde87FDOXfHeyC5m39l3 dgl99pnCKHAZwV/SRZ4F3dkLJEp2TuoEhvXr5XWt1fJaxI3UMnyCoC3vaMwT86NLPG+9Ju U9oeQfNCycSfZnnd0/JNLxzM4CyAcFMvt7NgtVKPuMorGAkbSP/KTaNiNzN5GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765540744; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=y3vZJKcYgZXMSEzQEFcIcXTD1lMTjWSOjP6RsKFdCgk=; b=wF7ZMGna4/JVw3AM88Ac6ov02DooaRz+AuuqByUY5fPXcrj14cLxQg+pQjH7a7gnLbnI+7 EUM3JKna0EY7dKcTCpFaYP7vjR7cDVFNbNDA3+us6YnOD7nIFTmqd6KQ3rgJD4P2h9H04o lkdAdMAH4TkaC7pr0C/4Wi4zBsHLatGsEpbnSHu+DYivSvh3Oa1Id1ySCjwFWP9Wvp1Kh5 7IyojTZwze5LOYV54qYnIQkpxHmIqN2Z49RDmrbG1cBPkoGhzDyrsKDyVXL1KCRfYBJMIO txCL3/ySctEelcmECDwX+wqCYd+2uHpM8pbpxN/EMm5RjOEhv68nXJ1ezG8zPw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1765540744; a=rsa-sha256; cv=none; b=KRjuQU2Cf2J2yEytzHbdiL7glIGGtRdq4XQhE2nZDSfB6nRxDVmurttQlrrqzZrs4vali4 mCVCTGvXpf3mQVa5LJyMAxN9/JOnP77Ne29ITGBsUerdilKgUQYRjE8AkL91x//FfhpCfo GLGpbhJ4y+f7H2/KKsy/WvHQOs73uHzAO7NqQ9g9iSI9eg80kUNJkXAkpdIB5agXLJo88C rL+KMJAS6khXndu25kfWSCMSEdbV9cJTxMcoaVvRwx5SZGtoQXWxhr+IsXdc8yw7iFvKwp +DXnZa5E5Phc8o0HeUFeR7g1DibNZ6Sxc80plcLIuc5GoiZUsulLYJqnhN1rDA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dSSfc0HV1zjK for ; Fri, 12 Dec 2025 11:59:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id bb68 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 12 Dec 2025 11:59:04 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Robert Clausecker Subject: git: 24ea81047de7 - stable/15 - lib/libc: implement C23 memalignment() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 24ea81047de7c75fd57c54deda0b36c3225c367b Auto-Submitted: auto-generated Date: Fri, 12 Dec 2025 11:59:04 +0000 Message-Id: <693c0388.bb68.5e9fe38f@gitrepo.freebsd.org> The branch stable/15 has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=24ea81047de7c75fd57c54deda0b36c3225c367b commit 24ea81047de7c75fd57c54deda0b36c3225c367b Author: Robert Clausecker AuthorDate: 2025-11-10 17:54:41 +0000 Commit: Robert Clausecker CommitDate: 2025-12-12 10:32:58 +0000 lib/libc: implement C23 memalignment() This new function computes the alignment of a pointer. It is part of ISO/IEC 9899:2024, the new C standard. 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. Reviewed by: kib, imp, ziaee (manpages), pauamma@gundo.com Approved by: markj (mentor) MFC after: 1 month Relnotes: yes Differential Revision: https://reviews.freebsd.org/D53673 (cherry picked from commit 6c57e368eb1777f6097158eeca2fcc175d068dba) --- include/stdlib.h | 8 +++++++ lib/libc/stdlib/Makefile.inc | 4 ++-- lib/libc/stdlib/Symbol.map | 1 + lib/libc/stdlib/memalignment.3 | 53 ++++++++++++++++++++++++++++++++++++++++++ lib/libc/stdlib/memalignment.c | 28 ++++++++++++++++++++++ 5 files changed, 92 insertions(+), 2 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index 784cb63bfc5b..3e54b5feb6de 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -167,6 +167,14 @@ int at_quick_exit(void (*)(void)) __noexcept; _Noreturn void quick_exit(int) /* __noexcept -- not ready ABI issues? */; #endif /* __ISO_C_VISIBLE >= 2011 */ + +/* + * C23 extensions + */ +#if __ISO_C_VISIBLE >= 2023 +size_t memalignment(const void *) __pure2; +#endif /* __ISO_C_VISIBLE >= 2023 */ + /* * Extensions made by POSIX relative to C. */ diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index e7b9955b9646..b878a7625e9f 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -7,7 +7,7 @@ MISRCS+=C99_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \ div.c exit.c getenv.c getopt.c getopt_long.c \ getsubopt.c hcreate.c hcreate_r.c hdestroy_r.c heapsort.c heapsort_b.c \ hsearch_r.c imaxabs.c imaxdiv.c \ - insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \ + insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c memalignment.c \ merge.c mergesort_b.c ptsname.c qsort.c qsort_r.c qsort_r_compat.c \ qsort_s.c quick_exit.c radixsort.c rand.c \ random.c reallocarray.c reallocf.c realpath.c recallocarray.c remque.c \ @@ -35,7 +35,7 @@ MAN+= a64l.3 abort.3 abs.3 atexit.3 atof.3 \ atoi.3 atol.3 at_quick_exit.3 bsearch.3 \ div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \ hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \ - lsearch.3 memory.3 ptsname.3 qsort.3 \ + lsearch.3 memalignment.3 memory.3 ptsname.3 qsort.3 \ quick_exit.3 \ radixsort.3 rand.3 random.3 reallocarray.3 reallocf.3 realpath.3 \ set_constraint_handler_s.3 \ diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map index 53d71bcafb7d..8b7e97c3cbdc 100644 --- a/lib/libc/stdlib/Symbol.map +++ b/lib/libc/stdlib/Symbol.map @@ -132,6 +132,7 @@ FBSD_1.8 { }; FBSD_1.9 { + memalignment; recallocarray; }; diff --git a/lib/libc/stdlib/memalignment.3 b/lib/libc/stdlib/memalignment.3 new file mode 100644 index 000000000000..4a2269a82c81 --- /dev/null +++ b/lib/libc/stdlib/memalignment.3 @@ -0,0 +1,53 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 10, 2025 +.Dt MEMALIGNMENT 3 +.Os +.Sh NAME +.Nm memalignment +.Nd find the memory alignment of an object +.Sh SYNOPSIS +.Lb libc +.In stdlib.h +.Ft size_t +.Fn memalignment "const void *ptr" +.Sh DESCRIPTION +The +.Fn memalignment +function determines the alignment of the object pointed to by +.Fa ptr . +This alignment is a power of\~2, and may be larger than the range +supported by the +.Sy alignof +operator. +The value returned can be compared to the result of +.Sy alignof , +and if it is greater or equal, the alignment requirement of the operand +is satisfied. +.Sh RETURN VALUES +Returns the alignment of +.Fa ptr +as a power of\~2. +If +.Fa ptr +is a null pointer, an alignment of zero is returned. +An alignment of zero indicates that the tested pointer cannot be used to +access an object of any type. +.Sh SEE ALSO +.Xr aligned_alloc 3 , +.Xr posix_memalign 3 +.Sh STANDARDS +The +.Fn memalignment +function conforms to +.St -isoC-2023 . +.Sh HISTORY +The +.Fn memalignment +function was added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdlib/memalignment.c b/lib/libc/stdlib/memalignment.c new file mode 100644 index 000000000000..771ddc2f5253 --- /dev/null +++ b/lib/libc/stdlib/memalignment.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Robert Clausecker + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +size_t +memalignment(const void *p) +{ + uintptr_t align; + + if (p == NULL) + return (0); + + align = (uintptr_t)p; + align &= -align; + +#if UINTPTR_MAX > SIZE_MAX + /* if alignment overflows size_t, return maximum possible */ + if (align > SIZE_MAX) + align = SIZE_MAX - SIZE_MAX/2; +#endif + + return (align); +}