aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/memalignment.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdlib/memalignment.c')
-rw-r--r--lib/libc/stdlib/memalignment.c28
1 files changed, 28 insertions, 0 deletions
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 <fuz@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+
+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);
+}