git: 06a98fefd3d3 - main - nvi: Replace Clang-only __builtin_is_aligned with C code (#124)

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Wed, 27 Nov 2024 18:39:00 UTC
The branch main has been updated by brooks:

URL: https://cgit.FreeBSD.org/src/commit/?id=06a98fefd3d3ff42b7e7832af6c0736b98f167ac

commit 06a98fefd3d3ff42b7e7832af6c0736b98f167ac
Author:     Zhihao Yuan <lichray@gmail.com>
AuthorDate: 2024-11-27 18:30:46 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2024-11-27 18:35:10 +0000

    nvi: Replace Clang-only __builtin_is_aligned with C code (#124)
    
    We should use alignof in the future.
    
    Obtained from: https://github.com/lichray/nvi2/commit/25c4d7db4ea638a31ac458b733a3b67b0a0ff634
---
 contrib/nvi/common/log.c |  2 +-
 contrib/nvi/common/mem.h | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/contrib/nvi/common/log.c b/contrib/nvi/common/log.c
index d9b142b16d01..d1a1bc6f704a 100644
--- a/contrib/nvi/common/log.c
+++ b/contrib/nvi/common/log.c
@@ -709,7 +709,7 @@ apply_with(int (*db_func)(SCR *, recno_t, CHAR_T *, size_t), SCR *sp,
 	static size_t blen;
 	static u_char *bp;
 
-	if (!__builtin_is_aligned(p, sizeof(unsigned long))) {
+	if (!is_aligned(p, sizeof(unsigned long))) {
 		if (len > blen) {
 			blen = p2roundup(MAX(len, 512));
 			REALLOC(sp, bp, u_char *, blen);
diff --git a/contrib/nvi/common/mem.h b/contrib/nvi/common/mem.h
index d24ec0b50b09..0c83b70dec2f 100644
--- a/contrib/nvi/common/mem.h
+++ b/contrib/nvi/common/mem.h
@@ -212,6 +212,18 @@ p2roundup(size_t n)
 	return (n);
 }
 
+/*
+ * is_aligned --
+ *      Determine whether the program can safely read an object with an
+ *      alignment requirement from ptr.
+ *
+ * See also: https://clang.llvm.org/docs/LanguageExtensions.html#alignment-builtins
+ */
+static __inline int
+is_aligned(void *ptr, size_t alignment) {
+	return ((uintptr_t)ptr % alignment) == 0;
+}
+
 /* Additional TAILQ helper. */
 #define TAILQ_ENTRY_ISVALID(elm, field)					\
 	((elm)->field.tqe_prev != NULL)