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.
This commit is contained in:
Augustin Cavalier
2026-03-10 12:00:59 -04:00
parent ff861e0ea3
commit 69499b0e00
5 changed files with 22 additions and 0 deletions
+18
View File
@@ -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 <string.h>
#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 */
@@ -1,5 +1,6 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#undef memmove
#if defined(__GNUC__) && __GNUC__ >= 4 #if defined(__GNUC__) && __GNUC__ >= 4
typedef __attribute__((__may_alias__)) size_t WT; typedef __attribute__((__may_alias__)) size_t WT;
@@ -6,6 +6,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#undef memcpy
#define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1)) #define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1))
@@ -6,6 +6,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#undef memset
#define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1)) #define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1))
+1
View File
@@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#undef memcmp
#define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1)) #define MISALIGNMENT(PTR, TYPE) ((addr_t)(PTR) & (sizeof(TYPE) - 1))