From 69499b0e00641bfda09d94d78b0bd610b1a16f3f Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 10 Mar 2026 12:00:32 -0400 Subject: [PATCH] kernel: Use compiler-builtin memset, memcpy, memmove, memcmp. Since the kernel is built with -ffreestanding, and thus -fno-builtin, the compiler ignores the function names and doesn't perform optimizations (e.g. inlining for small values) that it otherwise could. So, here, we add a string.h that supplements the default, and uses #define to reinstate the builtins, and thus the optimizations. Linux and FreeBSD at least apparently do the same. A quick compile benchmark in a VM doesn't show much difference, maybe a slight decrease in sys time. --- headers/private/kernel/string.h | 18 ++++++++++++++++++ src/system/libroot/posix/musl/string/memmove.c | 1 + .../posix/string/arch/generic/generic_memcpy.c | 1 + .../posix/string/arch/generic/generic_memset.c | 1 + src/system/libroot/posix/string/memcmp.c | 1 + 5 files changed, 22 insertions(+) create mode 100644 headers/private/kernel/string.h diff --git a/headers/private/kernel/string.h b/headers/private/kernel/string.h new file mode 100644 index 0000000000..9af4e94157 --- /dev/null +++ b/headers/private/kernel/string.h @@ -0,0 +1,18 @@ +/* + * Copyright 2026, Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef KERNEL_STRING_H +#define KERNEL_STRING_H + + +#include_next + + +#define memset(DEST, V, LEN) __builtin_memset((DEST), (V), (LEN)) +#define memcpy(DEST, SRC, LEN) __builtin_memcpy((DEST), (SRC), (LEN)) +#define memmove(DEST, SRC, LEN) __builtin_memmove((DEST), (SRC), (LEN)) +#define memcmp(B1, B2, LEN) __builtin_memcmp((B1), (B2), (LEN)) + + +#endif /* KERNEL_STRING_H */ diff --git a/src/system/libroot/posix/musl/string/memmove.c b/src/system/libroot/posix/musl/string/memmove.c index 5e025fd9a2..636b4dc859 100644 --- a/src/system/libroot/posix/musl/string/memmove.c +++ b/src/system/libroot/posix/musl/string/memmove.c @@ -1,5 +1,6 @@ #include #include +#undef memmove #if defined(__GNUC__) && __GNUC__ >= 4 typedef __attribute__((__may_alias__)) size_t WT; diff --git a/src/system/libroot/posix/string/arch/generic/generic_memcpy.c b/src/system/libroot/posix/string/arch/generic/generic_memcpy.c index 61d8fabddc..5ef7977b0d 100644 --- a/src/system/libroot/posix/string/arch/generic/generic_memcpy.c +++ b/src/system/libroot/posix/string/arch/generic/generic_memcpy.c @@ -6,6 +6,7 @@ #include #include +#undef memcpy #define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1)) diff --git a/src/system/libroot/posix/string/arch/generic/generic_memset.c b/src/system/libroot/posix/string/arch/generic/generic_memset.c index 57874003d9..9896c2ce1c 100644 --- a/src/system/libroot/posix/string/arch/generic/generic_memset.c +++ b/src/system/libroot/posix/string/arch/generic/generic_memset.c @@ -6,6 +6,7 @@ #include #include +#undef memset #define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1)) diff --git a/src/system/libroot/posix/string/memcmp.c b/src/system/libroot/posix/string/memcmp.c index 71d0cf98e4..f732d3482b 100644 --- a/src/system/libroot/posix/string/memcmp.c +++ b/src/system/libroot/posix/string/memcmp.c @@ -7,6 +7,7 @@ #include #include +#undef memcmp #define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1))