From nobody Wed Nov 12 12:24:49 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 4d62f9486bz6GvRN; Wed, 12 Nov 2025 12:24:49 +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 4d62f93d0Rz3dP8; Wed, 12 Nov 2025 12:24:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1762950289; 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=W5wI7qdZGX6XfjGUDSlY95aiiOEsRFA3ZJXpZ89Qmjk=; b=XXBxm3aVYP8LXm2CMSiJ+kc8OASYINI9WNEhJQZ/6qjBLStd/+Oj7435Q2VL2Lg3TnDg1m pQLSyNhVv/FPhEcxmmiuaX5tSo5y6adN+l14Y6LeEE2sVl1LFcgDVmUtFlvdQXFEwdhAra uBtjd6oV7vLnDl9AVRROYAm/BIrMmt0oEQE+DwMaDyaQ1NY/rnqmBQQl2dyW7Rf0Zr84pO C6xFr1NaMRLebEO/m12ELfjxoeIQoySJnoJbAHTLuXhe46w0Z5hAp23GzaB4V7zBD/s8sN yZR8GwPCjexUZnFzr1UvoqlkvU7M4YY1dMzTaBW8KVLKNxa7IyIrSkN77ex9Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1762950289; 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=W5wI7qdZGX6XfjGUDSlY95aiiOEsRFA3ZJXpZ89Qmjk=; b=dGahMlV+JY6KWNBFNMuaAkJiA19b3sUnZPQtEEVbCO8zY19guzhuX8wf1Nm2C33jkLvkck +e/hwoVNOqMTPK+fj+tUGKf9T8qNtu4uZSVmz4OnISBHJssTH5PZQ0BLa5z4xuBVrrnbfw hZPjMd1kT6tGQrSr+sIduvMMzuBhPyX5cMFQzmvPV5WZNZmU+GfGQBtxqzZu7CN4GL4D6j cd8TBfu5g80wJE46X6anO/G9qbD6hfgy7uP+7l4iVk34wCrb/1hqgmUdRDHTjhPgXeKtpo J06FYFcZrGw08l5W6dZrVrKSWi78WOm2YO0sUy21yMZvbwE990EiO3lHu6mh1w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1762950289; a=rsa-sha256; cv=none; b=ww/ofyllrrdgYBMEGhGSOjKCT7nh+rOBJeD2888kcllA2iq5ng/5BsjUjfPXgh/bc/iHCj /aQ877g1ONhSQDXaGdCzMFi3Slx1A5uJrrcNeq8HgIEAZEFpXlTZa3Jr8slUw25koomQrP asyvVJgDgJJKWh6kzBp8fVYTIyKiqHpq85tMn1Fg/soXifXZE0c8VV6cOQVhwmPeg8qgwF aRtgyw9jrGYzq+VahrVP0dVhaKtmhmR64v8qXWWzq0RZJHxRec5DCdxmL+/19nyODJsYkQ jBiTa+mx6DpkcxbiLiBgEYSfl5MAP0nerp+I4au4/9vzjkVF48xcjPTnPw5HCg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4d62f93D87zbP4; Wed, 12 Nov 2025 12:24:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 5ACCOnIW079539; Wed, 12 Nov 2025 12:24:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 5ACCOnIv079536; Wed, 12 Nov 2025 12:24:49 GMT (envelope-from git) Date: Wed, 12 Nov 2025 12:24:49 GMT Message-Id: <202511121224.5ACCOnIv079536@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 6c57e368eb17 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 6c57e368eb1777f6097158eeca2fcc175d068dba Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=6c57e368eb1777f6097158eeca2fcc175d068dba commit 6c57e368eb1777f6097158eeca2fcc175d068dba Author: Robert Clausecker AuthorDate: 2025-11-10 17:54:41 +0000 Commit: Robert Clausecker CommitDate: 2025-11-12 12:23:09 +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 --- 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); +}