From 8cace3f05003c6cb4d38c36890055f03151c2ad4 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 16 Oct 2026 02:39:56 +0800 Subject: Remove old code Signed-off-by: Richard Qin --- lib/Build.mk | 49 -- lib/cxx_abi.cc | 37 -- lib/fdt.c | 2 - lib/fdt_addresses.c | 2 - lib/fdt_empty_tree.c | 2 - lib/fdt_ro.c | 2 - lib/fdt_rw.c | 2 - lib/fdt_strerror.c | 2 - lib/fdt_sw.c | 2 - lib/fdt_wip.c | 2 - lib/klog.cc | 574 ------------------ lib/kobject.cc | 82 --- lib/kprintf.cc | 91 --- lib/ktl/LICENSE | 21 - lib/ktl/README.lume | 8 - lib/ktl/README.md | 36 -- lib/ktl/include-after/errno.h | 13 - lib/ktl/include-after/wchar.h | 30 - lib/ktl/include/__chrono/system_clock.h | 15 - lib/ktl/include/__config | 53 -- lib/ktl/include/__config_site | 81 --- lib/ktl/include/__configuration/platform.h | 23 - lib/ktl/include/__external_threading | 35 -- lib/ktl/include/climits | 15 - lib/ktl/include/cmath | 53 -- lib/ktl/include/cstdint | 18 - lib/ktl/include/cstdio | 21 - lib/ktl/include/cstdlib | 30 - lib/ktl/include/cwchar | 62 -- lib/ktl/include/ktl/algorithm.h | 116 ---- lib/ktl/include/ktl/array.h | 19 - lib/ktl/include/ktl/atomic.h | 40 -- lib/ktl/include/ktl/bit.h | 30 - lib/ktl/include/ktl/byte.h | 18 - lib/ktl/include/ktl/concepts.h | 46 -- lib/ktl/include/ktl/declval.h | 18 - lib/ktl/include/ktl/enforce.h | 37 -- lib/ktl/include/ktl/forward.h | 18 - lib/ktl/include/ktl/functional.h | 19 - lib/ktl/include/ktl/initializer_list.h | 18 - lib/ktl/include/ktl/inplace_vector.h | 18 - lib/ktl/include/ktl/iterator.h | 38 -- lib/ktl/include/ktl/limits.h | 18 - lib/ktl/include/ktl/memory.h | 41 -- lib/ktl/include/ktl/new.h | 25 - lib/ktl/include/ktl/numeric.h | 20 - lib/ktl/include/ktl/optional.h | 23 - lib/ktl/include/ktl/ranges.h | 25 - lib/ktl/include/ktl/ref.h | 19 - lib/ktl/include/ktl/source_location.h | 18 - lib/ktl/include/ktl/span.h | 26 - lib/ktl/include/ktl/string_view.h | 22 - lib/ktl/include/ktl/tuple.h | 30 - lib/ktl/include/ktl/type_traits.h | 165 ------ lib/ktl/include/ktl/unique_ptr.h | 37 -- lib/ktl/include/ktl/utility.h | 41 -- lib/ktl/include/ktl/variant.h | 31 - lib/ktl/include/limits | 110 ---- lib/ktl/include/time.h | 39 -- lib/ktl/include/version | 14 - lib/ktl/include/wchar.h | 23 - lib/ktl/popcount.cc | 135 ----- lib/ktl/stub-new.cc | 25 - lib/libc/LICENSE | 21 - lib/libc/README.lume | 10 - lib/libc/include-after/ctype.h | 33 -- lib/libc/include-after/limits.h | 7 - lib/libc/include/assert.h | 20 - lib/libc/include/inttypes.h | 117 ---- lib/libc/include/multi-file.h | 50 -- lib/libc/include/stdint.h | 278 --------- lib/libc/include/stdio.h | 102 ---- lib/libc/include/stdlib.h | 36 -- lib/libc/include/string-file.h | 79 --- lib/libc/include/string.h | 97 --- lib/printf.c | 914 ----------------------------- lib/printf.h | 129 ---- lib/random.cc | 152 ----- lib/rbtree.cc | 596 ------------------- lib/string/COPYRIGHT | 193 ------ lib/string/aarch64/memcpy.S | 186 ------ lib/string/aarch64/memset.S | 115 ---- lib/string/arm/__aeabi_memcpy.s | 45 -- lib/string/arm/__aeabi_memset.s | 31 - lib/string/arm/memcpy.S | 479 --------------- lib/string/bcmp.c | 11 - lib/string/bcopy.c | 11 - lib/string/bzero.c | 11 - lib/string/explicit_bzero.c | 11 - lib/string/i386/memcpy.s | 32 - lib/string/i386/memmove.s | 22 - lib/string/i386/memset.s | 76 --- lib/string/index.c | 11 - lib/string/memccpy.c | 37 -- lib/string/memchr.c | 30 - lib/string/memcmp.c | 11 - lib/string/memcpy.c | 127 ---- lib/string/memmem.c | 152 ----- lib/string/memmove.c | 45 -- lib/string/mempcpy.c | 10 - lib/string/memrchr.c | 14 - lib/string/memset.c | 93 --- lib/string/rindex.c | 11 - lib/string/stpcpy.c | 32 - lib/string/stpncpy.c | 35 -- lib/string/strcat.c | 10 - lib/string/strchr.c | 10 - lib/string/strchrnul.c | 31 - lib/string/strcmp.c | 10 - lib/string/strcpy.c | 10 - lib/string/strcspn.c | 20 - lib/string/strlcat.c | 12 - lib/string/strlcpy.c | 37 -- lib/string/strlen.c | 25 - lib/string/strncat.c | 13 - lib/string/strncmp.c | 12 - lib/string/strncpy.c | 10 - lib/string/strnlen.c | 10 - lib/string/strpbrk.c | 10 - lib/string/strrchr.c | 9 - lib/string/strsep.c | 16 - lib/string/strspn.c | 23 - lib/string/strstr.c | 157 ----- lib/string/strtok.c | 16 - lib/string/strtok_r.c | 15 - lib/string/x86_64/memcpy.s | 25 - lib/string/x86_64/memmove.s | 16 - lib/string/x86_64/memset.s | 72 --- 128 files changed, 7726 deletions(-) delete mode 100644 lib/Build.mk delete mode 100644 lib/cxx_abi.cc delete mode 100644 lib/fdt.c delete mode 100644 lib/fdt_addresses.c delete mode 100644 lib/fdt_empty_tree.c delete mode 100644 lib/fdt_ro.c delete mode 100644 lib/fdt_rw.c delete mode 100644 lib/fdt_strerror.c delete mode 100644 lib/fdt_sw.c delete mode 100644 lib/fdt_wip.c delete mode 100644 lib/klog.cc delete mode 100644 lib/kobject.cc delete mode 100644 lib/kprintf.cc delete mode 100644 lib/ktl/LICENSE delete mode 100644 lib/ktl/README.lume delete mode 100644 lib/ktl/README.md delete mode 100644 lib/ktl/include-after/errno.h delete mode 100644 lib/ktl/include-after/wchar.h delete mode 100644 lib/ktl/include/__chrono/system_clock.h delete mode 100644 lib/ktl/include/__config delete mode 100644 lib/ktl/include/__config_site delete mode 100644 lib/ktl/include/__configuration/platform.h delete mode 100644 lib/ktl/include/__external_threading delete mode 100644 lib/ktl/include/climits delete mode 100644 lib/ktl/include/cmath delete mode 100644 lib/ktl/include/cstdint delete mode 100644 lib/ktl/include/cstdio delete mode 100644 lib/ktl/include/cstdlib delete mode 100644 lib/ktl/include/cwchar delete mode 100644 lib/ktl/include/ktl/algorithm.h delete mode 100644 lib/ktl/include/ktl/array.h delete mode 100644 lib/ktl/include/ktl/atomic.h delete mode 100644 lib/ktl/include/ktl/bit.h delete mode 100644 lib/ktl/include/ktl/byte.h delete mode 100644 lib/ktl/include/ktl/concepts.h delete mode 100644 lib/ktl/include/ktl/declval.h delete mode 100644 lib/ktl/include/ktl/enforce.h delete mode 100644 lib/ktl/include/ktl/forward.h delete mode 100644 lib/ktl/include/ktl/functional.h delete mode 100644 lib/ktl/include/ktl/initializer_list.h delete mode 100644 lib/ktl/include/ktl/inplace_vector.h delete mode 100644 lib/ktl/include/ktl/iterator.h delete mode 100644 lib/ktl/include/ktl/limits.h delete mode 100644 lib/ktl/include/ktl/memory.h delete mode 100644 lib/ktl/include/ktl/new.h delete mode 100644 lib/ktl/include/ktl/numeric.h delete mode 100644 lib/ktl/include/ktl/optional.h delete mode 100644 lib/ktl/include/ktl/ranges.h delete mode 100644 lib/ktl/include/ktl/ref.h delete mode 100644 lib/ktl/include/ktl/source_location.h delete mode 100644 lib/ktl/include/ktl/span.h delete mode 100644 lib/ktl/include/ktl/string_view.h delete mode 100644 lib/ktl/include/ktl/tuple.h delete mode 100644 lib/ktl/include/ktl/type_traits.h delete mode 100644 lib/ktl/include/ktl/unique_ptr.h delete mode 100644 lib/ktl/include/ktl/utility.h delete mode 100644 lib/ktl/include/ktl/variant.h delete mode 100644 lib/ktl/include/limits delete mode 100644 lib/ktl/include/time.h delete mode 100644 lib/ktl/include/version delete mode 100644 lib/ktl/include/wchar.h delete mode 100644 lib/ktl/popcount.cc delete mode 100644 lib/ktl/stub-new.cc delete mode 100644 lib/libc/LICENSE delete mode 100644 lib/libc/README.lume delete mode 100644 lib/libc/include-after/ctype.h delete mode 100644 lib/libc/include-after/limits.h delete mode 100644 lib/libc/include/assert.h delete mode 100644 lib/libc/include/inttypes.h delete mode 100644 lib/libc/include/multi-file.h delete mode 100644 lib/libc/include/stdint.h delete mode 100644 lib/libc/include/stdio.h delete mode 100644 lib/libc/include/stdlib.h delete mode 100644 lib/libc/include/string-file.h delete mode 100644 lib/libc/include/string.h delete mode 100644 lib/printf.c delete mode 100644 lib/printf.h delete mode 100644 lib/random.cc delete mode 100644 lib/rbtree.cc delete mode 100644 lib/string/COPYRIGHT delete mode 100644 lib/string/aarch64/memcpy.S delete mode 100644 lib/string/aarch64/memset.S delete mode 100644 lib/string/arm/__aeabi_memcpy.s delete mode 100644 lib/string/arm/__aeabi_memset.s delete mode 100644 lib/string/arm/memcpy.S delete mode 100644 lib/string/bcmp.c delete mode 100644 lib/string/bcopy.c delete mode 100644 lib/string/bzero.c delete mode 100644 lib/string/explicit_bzero.c delete mode 100644 lib/string/i386/memcpy.s delete mode 100644 lib/string/i386/memmove.s delete mode 100644 lib/string/i386/memset.s delete mode 100644 lib/string/index.c delete mode 100644 lib/string/memccpy.c delete mode 100644 lib/string/memchr.c delete mode 100644 lib/string/memcmp.c delete mode 100644 lib/string/memcpy.c delete mode 100644 lib/string/memmem.c delete mode 100644 lib/string/memmove.c delete mode 100644 lib/string/mempcpy.c delete mode 100644 lib/string/memrchr.c delete mode 100644 lib/string/memset.c delete mode 100644 lib/string/rindex.c delete mode 100644 lib/string/stpcpy.c delete mode 100644 lib/string/stpncpy.c delete mode 100644 lib/string/strcat.c delete mode 100644 lib/string/strchr.c delete mode 100644 lib/string/strchrnul.c delete mode 100644 lib/string/strcmp.c delete mode 100644 lib/string/strcpy.c delete mode 100644 lib/string/strcspn.c delete mode 100644 lib/string/strlcat.c delete mode 100644 lib/string/strlcpy.c delete mode 100644 lib/string/strlen.c delete mode 100644 lib/string/strncat.c delete mode 100644 lib/string/strncmp.c delete mode 100644 lib/string/strncpy.c delete mode 100644 lib/string/strnlen.c delete mode 100644 lib/string/strpbrk.c delete mode 100644 lib/string/strrchr.c delete mode 100644 lib/string/strsep.c delete mode 100644 lib/string/strspn.c delete mode 100644 lib/string/strstr.c delete mode 100644 lib/string/strtok.c delete mode 100644 lib/string/strtok_r.c delete mode 100644 lib/string/x86_64/memcpy.s delete mode 100644 lib/string/x86_64/memmove.s delete mode 100644 lib/string/x86_64/memset.s (limited to 'lib') diff --git a/lib/Build.mk b/lib/Build.mk deleted file mode 100644 index 2d5a095..0000000 --- a/lib/Build.mk +++ /dev/null @@ -1,49 +0,0 @@ -targets += lib/ktl/headers lib/libc/headers lib/ktl lib/runtime lib/string \ - lib/libfdt lib/core - -target.lib/ktl/headers.kind := header-library -target.lib/ktl/headers.public-includes := lib/ktl/include -target.lib/ktl/headers.public-after-includes := lib/ktl/include-after - -target.lib/libc/headers.kind := header-library -target.lib/libc/headers.public-includes := lib/libc/include -target.lib/libc/headers.public-after-includes := lib/libc/include-after -target.lib/libc/headers.deps := lib/ktl/headers - -target.lib/ktl.kind := source-set -target.lib/ktl.sources := lib/ktl/popcount.cc lib/ktl/stub-new.cc -target.lib/ktl.deps := lib/ktl/headers lib/libc/headers - -target.lib/runtime.kind := source-set -target.lib/runtime.sources := \ - lib/cxx_abi.cc lib/rbtree.cc lib/kobject.cc lib/kprintf.cc lib/klog.cc \ - lib/random.cc lib/printf.c - -target.lib/string.kind := static-library -target.lib/string.sources := $(sort $(wildcard lib/string/*.c)) - -# Architecture assembly replaces the matching generic C implementation. -string-arch.riscv := -string-arch.x86_64 := x86_64 -string-arch.aarch64 := aarch64 -string-arch.i386 := i386 -string-arch.arm := arm -string-arch := $(string-arch.$(ARCH)) -ifneq ($(string-arch),) -string-asm := $(sort $(wildcard lib/string/$(string-arch)/*.S) \ - $(wildcard lib/string/$(string-arch)/*.s)) -string-asm-names := $(sort $(basename $(notdir $(string-asm)))) -target.lib/string.sources := $(filter-out \ - $(foreach name,$(string-asm-names),lib/string/$(name).c), \ - $(target.lib/string.sources)) $(string-asm) -endif - -target.lib/libfdt.kind := static-library -target.lib/libfdt.sources := \ - lib/fdt.c lib/fdt_ro.c lib/fdt_wip.c lib/fdt_rw.c lib/fdt_sw.c \ - lib/fdt_strerror.c lib/fdt_empty_tree.c lib/fdt_addresses.c -target.lib/libfdt.private-includes := scripts/dtc/libfdt -target.lib/libfdt.cflags := -Wno-parentheses -Wno-sign-compare - -target.lib/core.kind := header-library -target.lib/core.deps := lib/ktl lib/libc/headers lib/runtime lib/string lib/libfdt diff --git a/lib/cxx_abi.cc b/lib/cxx_abi.cc deleted file mode 100644 index 872b4a2..0000000 --- a/lib/cxx_abi.cc +++ /dev/null @@ -1,37 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* Copyright (C) 2026 Richard Qin */ - -/* - * cxx_abi.cc — Minimal C++ ABI stubs for freestanding kernel - * - * Provides symbols that the compiler/linker expects from the C++ - * runtime library. All error paths are fatal (kernel_panic). - * - * Reference: docs/specs/cpp_runtime.md - */ - -#include - -/* Pure virtual function call trap. - * Generated by the compiler in vtable entries for pure virtual methods. - * Reaching this means a derived class failed to implement a pure virtual. */ -extern "C" void __cxa_pure_virtual() -{ - kernel_panic("pure virtual function call"); -} - -/* Global destructor registration. - * The kernel does not support global object destruction — all global - * objects use Placement New with explicit lifetime management. - * If the compiler emits a call to this, it indicates a code violation. */ -extern "C" int __cxa_atexit(void (*)(void*), void*, void*) -{ - kernel_panic("__cxa_atexit called in kernel"); - return 0; // unreachable -} - -/* DSO handle — required by __cxa_atexit ABI. - * The kernel is statically linked; this symbol just needs to exist. */ -extern "C" { -void* __dso_handle __attribute__((visibility("hidden"))) = nullptr; -} diff --git a/lib/fdt.c b/lib/fdt.c deleted file mode 100644 index 1b20383..0000000 --- a/lib/fdt.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt.c" diff --git a/lib/fdt_addresses.c b/lib/fdt_addresses.c deleted file mode 100644 index d58f896..0000000 --- a/lib/fdt_addresses.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_addresses.c" diff --git a/lib/fdt_empty_tree.c b/lib/fdt_empty_tree.c deleted file mode 100644 index 37f8ef8..0000000 --- a/lib/fdt_empty_tree.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_empty_tree.c" diff --git a/lib/fdt_ro.c b/lib/fdt_ro.c deleted file mode 100644 index 3599d49..0000000 --- a/lib/fdt_ro.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_ro.c" diff --git a/lib/fdt_rw.c b/lib/fdt_rw.c deleted file mode 100644 index 65d7a71..0000000 --- a/lib/fdt_rw.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_rw.c" diff --git a/lib/fdt_strerror.c b/lib/fdt_strerror.c deleted file mode 100644 index dae35ea..0000000 --- a/lib/fdt_strerror.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_strerror.c" diff --git a/lib/fdt_sw.c b/lib/fdt_sw.c deleted file mode 100644 index d04f220..0000000 --- a/lib/fdt_sw.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_sw.c" diff --git a/lib/fdt_wip.c b/lib/fdt_wip.c deleted file mode 100644 index ebfb3c1..0000000 --- a/lib/fdt_wip.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include "../scripts/dtc/libfdt/fdt_wip.c" diff --git a/lib/klog.cc b/lib/klog.cc deleted file mode 100644 index 86c67f8..0000000 --- a/lib/klog.cc +++ /dev/null @@ -1,574 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* Copyright (C) 2026 Richard Qin */ - -/* - * klog.cc — Kernel logging subsystem implementation - * - * Implements the ring buffer, spinlock-protected writes, log level - * filtering, and synchronous console output. - * - * Phase 2 TODO: async flush via kernel thread (see docs/specs/klog.md) - */ - -#include -#include -#include -#include -#include -#include - -/* Forward declaration — mpaland/printf */ -extern "C" int vsnprintf_(char* buffer, size_t count, - const char* format, __builtin_va_list va); - -/* ======================================================================== - * Ring Buffer State - * ======================================================================== */ - -struct KlogEntryHdr { - uint32 seq; - uint32 len; - uint64 ts; - uint16 cpu; - uint8 level; - uint8 subsys; - uint16 reserved; -}; - -static struct { - char data[KLOG_BUF_SIZE]; - uint32 head; /* Next write position */ - uint32 tail; /* Next read position (for /proc/kmsg) */ - uint32 console_tail; /* Next position to flush to console */ - uint32 dropped; - lume::atomic seq; - uint64 subsys_mask; - Spinlock lock; - KlogLevel console_level; /* Messages <= this level go to UART */ - bool initialized; - bool panic_mode; -} g_klog; - -/* ======================================================================== - * Level name table - * ======================================================================== */ - -static const char* klog_level_name(KlogLevel level) -{ - static const char* names[] = { - "EMERG", "ALERT", "CRIT", "ERR", - "WARN", "NOTICE", "INFO", "DEBUG" - }; - if (level <= KLOG_DEBUG) { - return names[level]; - } - return "?"; -} - -static const char* klog_subsys_name(KlogSubsys subsys) -{ - static const char* names[] = { - "GEN", - "SCHED", - "TRAP", - "IRQ", - "TIMER", - "VMM", - "PMM", - "SLAB", - "FS", - "DRV", - "TEST", - }; - if (subsys < KLOG_SUBSYS_MAX) { - return names[subsys]; - } - return "?"; -} - -static const char* parse_kern_level(const char* fmt, KlogLevel* level) -{ - if (!fmt || !level) { - return fmt; - } - - /* Linux-style "\001n" prefix (KERN_SOH) */ - if (fmt[0] == '\001' && fmt[1] >= '0' && fmt[1] <= '7') { - *level = static_cast(fmt[1] - '0'); - return fmt + 2; - } - - /* Alternate "" prefix */ - if (fmt[0] == '<' && fmt[1] >= '0' && fmt[1] <= '7' && fmt[2] == '>') { - *level = static_cast(fmt[1] - '0'); - return fmt + 3; - } - - return fmt; -} - -/* ======================================================================== - * Ring Buffer Operations - * ======================================================================== */ - -/* Write a byte to the ring buffer. Overwrites oldest data on overflow. */ -static inline uint32 min_u32(uint32 a, uint32 b) -{ - return (a < b) ? a : b; -} - -static void ring_write_bytes(const void* src, uint32 len) -{ - const char* p = reinterpret_cast(src); - for (uint32 i = 0; i < len; ++i) { - g_klog.data[(g_klog.head + i) & (KLOG_BUF_SIZE - 1)] = p[i]; - } - g_klog.head += len; -} - -static void ring_read_bytes(uint32 pos, void* dst, uint32 len) -{ - char* out = reinterpret_cast(dst); - for (uint32 i = 0; i < len; ++i) { - out[i] = g_klog.data[(pos + i) & (KLOG_BUF_SIZE - 1)]; - } -} - -static void ring_drop_oldest(uint32 needed) -{ - uint32 min_tail = min_u32(g_klog.tail, g_klog.console_tail); - - while (KLOG_BUF_SIZE - (g_klog.head - min_tail) < needed) { - KlogEntryHdr hdr; - ring_read_bytes(min_tail, &hdr, sizeof(hdr)); - - uint32 entry = sizeof(hdr) + hdr.len; - if (entry == 0 || entry > KLOG_BUF_SIZE) { - min_tail++; - g_klog.dropped++; - continue; - } - min_tail += entry; - g_klog.dropped++; - } - - if (g_klog.tail < min_tail) { - g_klog.tail = min_tail; - } - if (g_klog.console_tail < min_tail) { - g_klog.console_tail = min_tail; - } -} - -/* ======================================================================== - * Console Output (synchronous) - * ======================================================================== */ - -static void console_emit(const char* s, int len) -{ - console_write_all(s, len); -} - -static int klog_format_line(char* out, int out_size, - const KlogEntryHdr& hdr, - const char* msg, int msg_len) -{ - const char* lvl = klog_level_name(static_cast(hdr.level)); - const char* subsys = klog_subsys_name(static_cast(hdr.subsys)); - - int prefix = ksnprintf(out, out_size, - "[%s][cpu%u][%llu][%s] ", - lvl, (uint32)hdr.cpu, - (unsigned long long)hdr.ts, subsys); - if (prefix < 0) { - return 0; - } - - int total = prefix; - int avail = out_size - 1 - total; - if (avail > 0) { - int to_copy = (msg_len < avail) ? msg_len : avail; - for (int i = 0; i < to_copy; ++i) { - out[total + i] = msg[i]; - } - total += to_copy; - } - - if (total > 0 && out[total - 1] != '\n' && total < out_size - 1) { - out[total++] = '\n'; - } - - out[total] = '\0'; - return total; -} - -static int klog_read_internal(char* buf, int size, uint32* tail_ptr, bool consume) -{ - if (size <= 0) { - return 0; - } - - int written = 0; - uint32 pos = *tail_ptr; - - while (pos != g_klog.head) { - KlogEntryHdr hdr; - ring_read_bytes(pos, &hdr, sizeof(hdr)); - uint32 raw_len = hdr.len; - if (raw_len > KLOG_BUF_SIZE) { - break; - } - uint32 copy_len = raw_len; - if (copy_len > KLOG_LINE_MAX) { - copy_len = KLOG_LINE_MAX; - } - - char msg[KLOG_LINE_MAX + 1]; - ring_read_bytes(pos + sizeof(hdr), msg, copy_len); - msg[copy_len] = '\0'; - - char line[KLOG_LINE_MAX + 64]; - int line_len = klog_format_line(line, sizeof(line), hdr, msg, - static_cast(copy_len)); - - if (line_len <= 0 || written + line_len > size) { - break; - } - - for (int i = 0; i < line_len; ++i) { - buf[written + i] = line[i]; - } - written += line_len; - - pos += sizeof(hdr) + raw_len; - } - - if (consume) { - *tail_ptr = pos; - } - - return written; -} - -static void klog_vex(KlogLevel level, KlogSubsys subsys, - const char* fmt, __builtin_va_list va); - -/* ======================================================================== - * Public API - * ======================================================================== */ - -void klog_init(void) -{ - g_klog.head = 0; - g_klog.tail = 0; - g_klog.console_tail = 0; - g_klog.dropped = 0; - g_klog.seq.store(0, __ATOMIC_RELAXED); - g_klog.subsys_mask = (1ULL << KLOG_SUBSYS_MAX) - 1; - g_klog.console_level = static_cast(KLOG_DEFAULT_LEVEL); - g_klog.initialized = true; - g_klog.panic_mode = false; - g_klog.lock.init("klog"); -} - -void klog(KlogLevel level, const char* fmt, ...) -{ - __builtin_va_list va; - __builtin_va_start(va, fmt); - klog_vex(level, KLOG_SUBSYS_GENERIC, fmt, va); - __builtin_va_end(va); -} - -void klog_ex(KlogLevel level, KlogSubsys subsys, const char* fmt, ...) -{ - __builtin_va_list va; - __builtin_va_start(va, fmt); - klog_vex(level, subsys, fmt, va); - __builtin_va_end(va); -} - -static void klog_vex(KlogLevel level, KlogSubsys subsys, - const char* fmt, __builtin_va_list va) -{ - if (subsys >= KLOG_SUBSYS_MAX) { - subsys = KLOG_SUBSYS_GENERIC; - } - - if (g_klog.initialized && ((g_klog.subsys_mask & (1ULL << subsys)) == 0)) { - return; - } - - char msg[KLOG_LINE_MAX + 1]; - int msg_len = vsnprintf_(msg, KLOG_LINE_MAX, fmt, va); - - if (msg_len < 0) { - return; - } - if (msg_len > KLOG_LINE_MAX) { - msg_len = KLOG_LINE_MAX; - } - msg[msg_len] = '\0'; - - KlogEntryHdr hdr; - hdr.seq = g_klog.seq.fetch_add(1, __ATOMIC_RELAXED); - hdr.len = static_cast(msg_len); - hdr.ts = arch::cpu::read_time(); - hdr.cpu = static_cast(arch::cpu::id()); - hdr.level = static_cast(level); - hdr.subsys = static_cast(subsys); - hdr.reserved = 0; - - bool do_console = (!g_klog.initialized || level <= g_klog.console_level); - - if (g_klog.initialized && !g_klog.panic_mode) { - LockGuard guard(g_klog.lock); - ring_drop_oldest(sizeof(hdr) + hdr.len); - ring_write_bytes(&hdr, sizeof(hdr)); - ring_write_bytes(msg, hdr.len); - if (do_console) { - g_klog.console_tail = g_klog.head; - /* Emit to UART while holding the lock to prevent - * interleaved output from concurrent CPUs. */ - char line[KLOG_LINE_MAX + 64]; - int line_len = klog_format_line(line, sizeof(line), - hdr, msg, hdr.len); - if (line_len > 0) { - console_emit(line, line_len); - } - } - } else if (do_console) { - /* Pre-init or panic mode: emit without lock */ - char line[KLOG_LINE_MAX + 64]; - int line_len = klog_format_line(line, sizeof(line), - hdr, msg, hdr.len); - if (line_len > 0) { - console_emit(line, line_len); - } - } -} - -int vprintk(const char* fmt, __builtin_va_list va) -{ - KlogLevel level = KLOG_INFO; - const char* real_fmt = parse_kern_level(fmt, &level); - - __builtin_va_list va_copy; - __builtin_va_copy(va_copy, va); - - char tmp[KLOG_LINE_MAX + 1]; - int len = vsnprintf_(tmp, KLOG_LINE_MAX, real_fmt, va_copy); - __builtin_va_end(va_copy); - - klog_vex(level, KLOG_SUBSYS_GENERIC, real_fmt, va); - - if (len < 0) { - return 0; - } - return len; -} - -int printk(const char* fmt, ...) -{ - __builtin_va_list va; - __builtin_va_start(va, fmt); - int ret = vprintk(fmt, va); - __builtin_va_end(va); - return ret; -} - -void klog_set_level(KlogLevel level) -{ - g_klog.console_level = level; -} - -void klog_set_subsys_mask(uint64 mask) -{ - g_klog.subsys_mask = mask; -} - -void klog_enable_subsys(KlogSubsys subsys) -{ - if (subsys < KLOG_SUBSYS_MAX) { - g_klog.subsys_mask |= (1ULL << subsys); - } -} - -void klog_disable_subsys(KlogSubsys subsys) -{ - if (subsys < KLOG_SUBSYS_MAX) { - g_klog.subsys_mask &= ~(1ULL << subsys); - } -} - -int klog_read(char* buf, int size) -{ - if (!g_klog.initialized || size <= 0) { - return 0; - } - - LockGuard guard(g_klog.lock); - return klog_read_internal(buf, size, &g_klog.tail, false); -} - -int klog_read_consume(char* buf, int size) -{ - if (!g_klog.initialized || size <= 0) { - return 0; - } - - LockGuard guard(g_klog.lock); - return klog_read_internal(buf, size, &g_klog.tail, true); -} - -void klog_flush(void) -{ - if (!g_klog.initialized) { - return; - } - - LockGuard guard(g_klog.lock); - - uint32 pos = g_klog.console_tail; - while (pos != g_klog.head) { - KlogEntryHdr hdr; - ring_read_bytes(pos, &hdr, sizeof(hdr)); - uint32 raw_len = hdr.len; - if (raw_len > KLOG_BUF_SIZE) { - break; - } - uint32 copy_len = raw_len; - if (copy_len > KLOG_LINE_MAX) { - copy_len = KLOG_LINE_MAX; - } - - char msg[KLOG_LINE_MAX + 1]; - ring_read_bytes(pos + sizeof(hdr), msg, copy_len); - msg[copy_len] = '\0'; - - char line[KLOG_LINE_MAX + 64]; - int line_len = klog_format_line(line, sizeof(line), hdr, msg, - static_cast(copy_len)); - if (line_len > 0) { - console_emit(line, line_len); - } - - pos += sizeof(hdr) + raw_len; - } - - g_klog.console_tail = pos; -} - -bool klog_ratelimit(KlogRateLimit* rl, uint64 now) -{ - if (!rl) { - return true; - } - - if (rl->interval == 0 || rl->burst == 0) { - return true; - } - - if (now - rl->last_ts >= rl->interval) { - rl->last_ts = now; - rl->printed = 0; - rl->suppressed = 0; - } - - if (rl->printed < rl->burst) { - rl->printed++; - return true; - } - - rl->suppressed++; - return false; -} - -void klog_stats(KlogStats* out) -{ - if (!out) { - return; - } - - out->head = g_klog.head; - out->tail = g_klog.tail; - out->console_tail = g_klog.console_tail; - out->dropped = g_klog.dropped; - out->seq = g_klog.seq.load(__ATOMIC_RELAXED); -} - -/* ======================================================================== - * Kernel Panic — Fatal Error Handler - * - * Dumps all critical S-mode CSRs and the stack pointer to the console, - * then halts the CPU. Uses kprintf (NOT klog) to avoid deadlocking - * if the panic was triggered while holding the klog spinlock. - * ======================================================================== */ - -extern "C" [[noreturn]] void kernel_panic(const char* msg, const char* detail) { - /* Disable interrupts immediately */ - arch::cpu::intr_off(); - - g_klog.panic_mode = true; - - /* ---- Banner ---- */ - kprintf("\n\n"); - kprintf("========================================\n"); - kprintf(" *** KERNEL PANIC ***\n"); - kprintf("========================================\n"); - - if (msg != nullptr) { - kprintf(" Reason : %s", msg); - if (detail != nullptr) { - kprintf(": %s", detail); - } - kprintf("\n"); - } - - /* ---- Read S-mode CSRs ---- */ - uint64 sepc, scause, stval, sstatus, satp, stvec, sscratch, sp_val; - - __asm__ volatile("csrr %0, sepc" : "=r"(sepc)); - __asm__ volatile("csrr %0, scause" : "=r"(scause)); - __asm__ volatile("csrr %0, stval" : "=r"(stval)); - __asm__ volatile("csrr %0, sstatus" : "=r"(sstatus)); - __asm__ volatile("csrr %0, satp" : "=r"(satp)); - __asm__ volatile("csrr %0, stvec" : "=r"(stvec)); - __asm__ volatile("csrr %0, sscratch" : "=r"(sscratch)); - __asm__ volatile("mv %0, sp" : "=r"(sp_val)); - - kprintf("\n--- S-mode CSR Dump ---\n"); - kprintf(" sepc = 0x%016llx\n", sepc); - kprintf(" scause = 0x%016llx", scause); - - /* Decode scause for convenience */ - bool is_interrupt = (scause >> 63) != 0; - uint64 cause_code = scause & 0x7FFFFFFFFFFFFFFFULL; - if (is_interrupt) { - kprintf(" (interrupt, code=%llu)\n", cause_code); - } else { - kprintf(" (exception, code=%llu)\n", cause_code); - } - - kprintf(" stval = 0x%016llx\n", stval); - kprintf(" sstatus = 0x%016llx", sstatus); - kprintf(" [SIE=%llu SPP=%llu SPIE=%llu]\n", - (sstatus >> 1) & 1, /* SIE */ - (sstatus >> 8) & 1, /* SPP */ - (sstatus >> 5) & 1); /* SPIE */ - kprintf(" satp = 0x%016llx", satp); - uint64 satp_mode = satp >> 60; - kprintf(" [mode=%llu]\n", satp_mode); - kprintf(" stvec = 0x%016llx\n", stvec); - kprintf(" sscratch = 0x%016llx\n", sscratch); - kprintf(" sp = 0x%016llx\n", sp_val); - - kprintf(" cpu_id = %llu\n", arch::cpu::id()); - kprintf(" time = %llu\n", arch::cpu::read_time()); - - kprintf("========================================\n\n"); - - /* Halt — spin forever with WFI to save power */ - while (true) { - arch::cpu::halt_until_interrupt(); - } -} diff --git a/lib/kobject.cc b/lib/kobject.cc deleted file mode 100644 index 8e8329e..0000000 --- a/lib/kobject.cc +++ /dev/null @@ -1,82 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* Copyright (C) 2026 Richard Qin */ - -/* - * kobject — implementation. - * - * Lives in lib/ (mirroring Linux's lib/kobject.c) because kobject is a - * generic primitive, not tied to the driver model. - * - * Reference: docs/specs/storage_stack.md §4.2 - */ - -#include -#include -#include - -void kobject_init(kobject* k, const kobj_type* type) -{ - if (k == nullptr) { - return; - } - k->name = nullptr; - k->parent = nullptr; - k->ktype = type; - k->state_in_sysfs = false; - k->entry.init(); - k->children.init(); - k->lock.init("kobject"); - kref_init(&k->refcount); -} - -int kobject_add(kobject* k, kobject* parent, const char* name) -{ - if (k == nullptr) { - return -1; - } - k->name = name; - if (parent != nullptr) { - kobject_get(parent); - k->parent = parent; - LockGuard g(parent->lock); - list_add_tail(&k->entry, &parent->children); - } - return 0; -} - -void kobject_del(kobject* k) -{ - if (k == nullptr || k->parent == nullptr) { - return; - } - kobject* parent = k->parent; - { - LockGuard g(parent->lock); - list_del_init(&k->entry); - } - k->parent = nullptr; - kobject_put(parent); -} - -void kobject_get(kobject* k) -{ - if (k != nullptr) { - kref_get(&k->refcount); - } -} - -static void kobject_release(kref* r) -{ - kobject* k = container_of(r, kobject, refcount); - if (k->ktype != nullptr && k->ktype->release != nullptr) { - k->ktype->release(k); - } -} - -void kobject_put(kobject* k) -{ - if (k == nullptr) { - return; - } - kref_put(&k->refcount, kobject_release); -} diff --git a/lib/kprintf.cc b/lib/kprintf.cc deleted file mode 100644 index a98de71..0000000 --- a/lib/kprintf.cc +++ /dev/null @@ -1,91 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* Copyright (C) 2026 Richard Qin */ - -/* - * kprintf.cc — Kernel formatted output - * - * Uses mpaland/printf's vsnprintf_ as the core formatting engine. - * - * Architecture: - * vsnprintf_() ← pure formatting, writes to buffer - * ↓ - * kprintf() ← format + emit via console_write_all() - * kvprintf() ← va_list variant - * ksnprintf() ← format into caller's buffer - * - * Output path: - * Pre-registration: console_write_all() → early_putc() fallback - * Post-registration: console_write_all() → struct console chain - */ - -#include -#include -#include -#include -#include "printf.h" - -/* ------------------------------------------------------------------ */ -/* Console spinlock (raw, no scheduler dependency — safe in panic) */ -/* ------------------------------------------------------------------ */ - -static lume::atomic g_console_lock{0}; - -static inline void console_lock_acquire() -{ - while (g_console_lock.exchange(1, __ATOMIC_ACQUIRE) != 0) { - /* spin */ - } -} - -static inline void console_lock_release() -{ - g_console_lock.store(0, __ATOMIC_RELEASE); -} - -/* ------------------------------------------------------------------ */ -/* _putchar: required by mpaland/printf for printf_() / vprintf_() */ -/* ------------------------------------------------------------------ */ - -void _putchar(char character) -{ - early_putc(character); -} - -/* ------------------------------------------------------------------ */ -/* Public API */ -/* ------------------------------------------------------------------ */ - -extern "C" int kprintf(const char* fmt, ...) -{ - char buf[256]; - va_list va; - va_start(va, fmt); - int len = vsnprintf_(buf, sizeof(buf), fmt, va); - va_end(va); - - /* Emit to console under lock */ - console_lock_acquire(); - console_write_all(buf, len); - console_lock_release(); - return len; -} - -extern "C" int kvprintf(const char* fmt, va_list va) -{ - char buf[256]; - int len = vsnprintf_(buf, sizeof(buf), fmt, va); - - console_lock_acquire(); - console_write_all(buf, len); - console_lock_release(); - return len; -} - -extern "C" int ksnprintf(char* buf, int count, const char* fmt, ...) -{ - va_list va; - va_start(va, fmt); - int ret = vsnprintf_(buf, (size_t)count, fmt, va); - va_end(va); - return ret; -} diff --git a/lib/ktl/LICENSE b/lib/ktl/LICENSE deleted file mode 100644 index 8580f02..0000000 --- a/lib/ktl/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -Copyright 2016 The Fuchsia Authors -Copyright (c) 2008-2015 Travis Geiselbrecht - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files -(the "Software"), to deal in the Software without restriction, -including without limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of the Software, -and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/ktl/README.lume b/lib/ktl/README.lume deleted file mode 100644 index 7e7c8a4..0000000 --- a/lib/ktl/README.lume +++ /dev/null @@ -1,8 +0,0 @@ -KTL configuration overlay imported from Zircon - -Source: zircon/kernel/lib/ktl -Fuchsia revision: f8d670ab4bbb71bfb4d569893ba12fbb8a83bacd - -The complete KTL header overlay and its two implementation files, popcount.cc -and stub-new.cc, are imported. The KTL target is built together with the -Zircon kernel libc header layer and the version-matched libc++ snapshot. diff --git a/lib/ktl/README.md b/lib/ktl/README.md deleted file mode 100644 index 667c678..0000000 --- a/lib/ktl/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# Kernel Template Library - -The Kernel Template Library is a curated subset of the C++ standard library API -using the `ktl` namespace rather than the `std` namespace. When a specific -interface is deemed useful, safe, and appropriate for use in the kernel, it can -be added here. No `std` APIs are ever used directly in the kernel, only `ktl`. - -The API is a strict subset of the standard C++ library API aside from the -namespace. The header file names are mostly directly mapped as well: - - -> - -There can be exceptions. For example, is a very broad collection of -APIs and the ktl subset is more focused. So instead of there -are specific headers for the standard APIs from that are included, such -as for `ktl::unique_ptr` (`std::unique_ptr` in ). - -The implementation here simply leverages the libc++ implementation and defines -`using` aliases in the `ktl` namespace. It's expected that ktl will only ever -provide "header only" APIs. - -## ktl enforcement - -The [``](include/ktl/enforce.h) header file should be used in -most `.cc` files (but not `.h`) files meant only for the kernel environment. -That is, in `.cc` files that use the `` headers or use kernel-only -headers that use them. - -This file must appear last in the `#include` list and `clang-format` will -arrange that automatically. - -After this `#include` any use of the `std` identifier in C++ code will cause a -compile-time error. When such errors are encountered, the kernel code should -be checked for use of C++ standard library headers (with no `.h` in the name, -like `, etc.) that should be replaced with `` headers and for -`std::` names that should be replaced with `ktl::` names. diff --git a/lib/ktl/include-after/errno.h b/lib/ktl/include-after/errno.h deleted file mode 100644 index 52edc9c..0000000 --- a/lib/ktl/include-after/errno.h +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_ERRNO_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_ERRNO_H_ - -// The kernel doesn't want this file but some libc++ headers we need -// wind up including it. - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_ERRNO_H_ diff --git a/lib/ktl/include-after/wchar.h b/lib/ktl/include-after/wchar.h deleted file mode 100644 index 41d11ae..0000000 --- a/lib/ktl/include-after/wchar.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_AFTER_WCHAR_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_AFTER_WCHAR_H_ - -#include - -// The kernel doesn't want this file but some libc++ headers we need wind up -// including it and they need these declarations. -// -// **NOTE** This file is include-after because libc++ headers get it via -// #include_next so as to get the "system" rather than libc++'s -// own wrapper. - -typedef struct { -} mbstate_t; - -typedef unsigned int wint_t; -#define WEOF (~wint_t{0}) - -// refers to these in code that's never actually used in ktl. -wchar_t* wmemcpy(wchar_t* __restrict, const wchar_t* __restrict, size_t); -wchar_t* wmemmove(wchar_t*, const wchar_t*, size_t); -wchar_t* wmemset(wchar_t*, wchar_t, size_t); - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_AFTER_WCHAR_H_ diff --git a/lib/ktl/include/__chrono/system_clock.h b/lib/ktl/include/__chrono/system_clock.h deleted file mode 100644 index 5d39243..0000000 --- a/lib/ktl/include/__chrono/system_clock.h +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2023 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE___CHRONO_SYSTEM_CLOCK_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE___CHRONO_SYSTEM_CLOCK_H_ - -// libc++'s includes this file via various internal headers. -// The declarations it provides aren't needed for the kernel and rely -// on other things stubbed out by the kernel. So this placeholder file -// just prevents compiling the libc++ code that doesn't work. - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE___CHRONO_SYSTEM_CLOCK_H_ diff --git a/lib/ktl/include/__config b/lib/ktl/include/__config deleted file mode 100644 index e04573e..0000000 --- a/lib/ktl/include/__config +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2022 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIG_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIG_ - -// The libc++ internal header <__config> unconditionally responds to some -// predefines set in the EFI environment. So this header wraps it and then -// undoes the damage that our __config_site header can't prevent. - -#include_next <__config> - -#undef _LIBCPP_WIN32API -#undef _LIBCPP_MSVCRT_LIKE -#undef _LIBCPP_MSVCRT -#undef _LIBCPP_HAS_OPEN_WITH_WCHAR - -// This prevents libc++ from doing fallback to spin locks and the like for -// cases where is_always_lock_free is not true. In the kernel, we don't want -// any behind-the-scenes help like that, we want to get link errors from -// libcalls if atomics can't be lowered to real hardware instructions. -#undef _LIBCPP_ATOMIC_ONLY_USE_BUILTINS - -// These are used by <__configuration/availability.h> to decide when headers -// should expect things to be defined in the library. For the kernel, there is -// no library and so everything must be defined in headers. -#undef _LIBCPP_INTRODUCED_IN_LLVM_21 -#define _LIBCPP_INTRODUCED_IN_LLVM_21 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_20 -#define _LIBCPP_INTRODUCED_IN_LLVM_20 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_19 -#define _LIBCPP_INTRODUCED_IN_LLVM_19 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_18 -#define _LIBCPP_INTRODUCED_IN_LLVM_18 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_16 -#define _LIBCPP_INTRODUCED_IN_LLVM_16 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_15 -#define _LIBCPP_INTRODUCED_IN_LLVM_15 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_14 -#define _LIBCPP_INTRODUCED_IN_LLVM_14 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_12 -#define _LIBCPP_INTRODUCED_IN_LLVM_12 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_11 -#define _LIBCPP_INTRODUCED_IN_LLVM_11 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_9 -#define _LIBCPP_INTRODUCED_IN_LLVM_9 0 -#undef _LIBCPP_INTRODUCED_IN_LLVM_4 -#define _LIBCPP_INTRODUCED_IN_LLVM_4 0 - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIG_ diff --git a/lib/ktl/include/__config_site b/lib/ktl/include/__config_site deleted file mode 100644 index 4d2236b..0000000 --- a/lib/ktl/include/__config_site +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIG_SITE_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIG_SITE_ - -// The libc++ internal header <__config> does #include <__config_site> to get -// definitions that vary by operating system, machine, or configuration. The -// toolchain's libc++ installation provides include/$triple/.../__config_site -// files for each $cpu-fuchsia appropriate for normal userland. But the ktl -// abuse of libc++ is different from public userland libc++, so provide special -// kernel-appropriate definitions here. This file will be in the include path -// ahead of any toolchain-supplied header. Also, some special bare-metal build -// environments such as phys32 or efi that reuse ktl and kernel libc code may -// use settings that cause the compiler to change the $triple it injects into -// the include path to one for which no runtimes are built and so there is no -// __config_site file to be found at all. - -// These two are what <__config_site> for Fuchsia userland defines. -#define _LIBCPP_ABI_VERSION 2 -#define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS 1 - -// This has to be defined to some `__*` string but what doesn't really matter. -#define _LIBCPP_ABI_NAMESPACE __ktl - -// When compiling in the UEFI environment, the predefines look like Windows -// but there is no Windows runtime environment to use. -#define _LIBCPP_NO_VCRUNTIME 1 - -// This tells the headers not to use `extern template` declarations that -// expect specific instantiations/specializations to come from the libc++ -// library rather than the headers. In the kernel, only the headers are -// ever going to be available. -#define _LIBCPP_DISABLE_EXTERN_TEMPLATE 1 - -// This tells not to use threading APIs. -#define _LIBCPP_HAS_NO_PLATFORM_WAIT 1 - -// This tells et al to use <__external_threading> instead -// of expecting either POSIX or C11 threading APIs. -#define _LIBCPP_HAS_THREAD_API_EXTERNAL 1 -#define _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL 1 - -// <__config> requires that these be set if _LIBCPP_HAS_THREAD_API_EXTERNAL is. -#define _LIBCPP_HAS_THREADS 1 -#define _LIBCPP_HAS_MONOTONIC_CLOCK 1 - -// This tells not to use `aligned_alloc` from . -#define _LIBCPP_HAS_NO_C11_ALIGNED_ALLOC 1 - -// This tells various headers to be strict about what includes what. -#define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES 1 - -// This tells libc++ not to expect full locale APIs in some places. -#define _LIBCPP_HAS_NO_LOCALIZATION 1 - -// This is unfortunately required even though the kernel does not use wide -// characters. -#define _LIBCPP_HAS_WIDE_CHARACTERS 1 - -// This tells pstl headers to use serial algorithms and not try to -// do any multithreading. -#define _LIBCPP_PSTL_CPU_BACKEND_SERIAL 1 - -// This will probably never be used in code that ktl actually uses. -// But define it for completeness. -#define _LIBCPP_INSTRUMENTED_WITH_ASAN __has_feature(address_sanitizer) - -// TODO(https://fxbug.dev/42082322): make the hardening mode selectable through build -// configurations. -// This sets the default to tell libc++ to not enable assertions to prevent -// undefined behavior, but may be overridden if _LIBCPP_HARDENING_MODE is -// defined elsewhere. -#define _LIBCPP_HARDENING_MODE_DEFAULT _LIBCPP_HARDENING_MODE_NONE - -#define _LIBCPP_ASSERTION_SEMANTIC_DEFAULT _LIBCPP_ASSERTION_SEMANTIC_HARDENING_DEPENDENT - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIG_SITE_ diff --git a/lib/ktl/include/__configuration/platform.h b/lib/ktl/include/__configuration/platform.h deleted file mode 100644 index b15e1e0..0000000 --- a/lib/ktl/include/__configuration/platform.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2025 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIGURATION_PLATFORM_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIGURATION_PLATFORM_H_ - -// Other libc++ headers use <__configuration/platform.h> but libc++'s -// <__configuration/platform.h> does not yet recognize the `__PE_COFF__` -// predefine. Until we support this new predefine upstream, -// we must define _LIBCPP_OBJECT_FORMAT_COFF for UEFI targets here. -// TODO(https://fxbug.dev/435771251) -- Support `__PE_COFF__` predefine -// upstream. - -#if defined(__PE_COFF__) -#define _LIBCPP_OBJECT_FORMAT_COFF 2 // Tell libc++ this is PE-COFF. -#endif // __PE_COFF__ - -#include_next <__configuration/platform.h> - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE___CONFIGURATION_PLATFORM_H_ diff --git a/lib/ktl/include/__external_threading b/lib/ktl/include/__external_threading deleted file mode 100644 index 9cec903..0000000 --- a/lib/ktl/include/__external_threading +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE___EXTERNAL_THREADING -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE___EXTERNAL_THREADING - -// libc++'s includes this file via <__threading_support>. It needs -// these types and functions declared, but only for use in interfaces that -// aren't supported in the kernel, so stub declarations are all they need. - -_LIBCPP_BEGIN_NAMESPACE_STD - -struct __libcpp_timespec_t { - long int tv_sec, tv_nsec; -}; - -using __libcpp_thread_id = int; - -bool __libcpp_thread_id_equal(__libcpp_thread_id, __libcpp_thread_id); -bool __libcpp_thread_id_less(__libcpp_thread_id, __libcpp_thread_id); -__libcpp_thread_id __libcpp_thread_get_current_id(); - -void __libcpp_thread_sleep_for(chrono::nanoseconds); -void __libcpp_thread_yield(); - -// This macro is used unconditionally in <__threading_support> but defined -// there only conditionally on when <__external_threading> is not used. -#define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY - -_LIBCPP_END_NAMESPACE_STD - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE___EXTERNAL_THREADING diff --git a/lib/ktl/include/climits b/lib/ktl/include/climits deleted file mode 100644 index 86e537d..0000000 --- a/lib/ktl/include/climits +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2022 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#pragma once - -// Other libc++ headers use but libc++'s complains if it -// thinks is not its own wrapper, which we do not need. We always -// get the kernel's before the libc++ with the way the include paths -// are set up, which is necessary because of other headers where preempting -// libc++'s header is unavoidable for the kernel environment. - -#include diff --git a/lib/ktl/include/cmath b/lib/ktl/include/cmath deleted file mode 100644 index 13b8e6b..0000000 --- a/lib/ktl/include/cmath +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_CMATH_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_CMATH_ - -// The kernel doesn't want this file but some libc++ headers we need -// wind up including it. They don't actually most of the math -// declarations except under #if conditions that aren't true. - -#include <__config> -#include - -_LIBCPP_BEGIN_NAMESPACE_STD - -// These are used in templates that won't ever be instantiated in kernel code. - -template -constexpr bool isinf(T&& x) { - static_assert(!is_same_v, "must not be instantiated in kernel code"); - return false; -} - -template -constexpr bool isnan(T&& x) { - static_assert(!is_same_v, "must not be instantiated in kernel code"); - return false; -} - -template -constexpr bool signbit(T&& x) { - static_assert(!is_same_v, "must not be instantiated in kernel code"); - return false; -} - -template -constexpr T frexp(T&& x, int* exponent) { - static_assert(!is_same_v, "must not be instantiated in kernel code"); - return x; -} - -template -constexpr auto ceil(T&& x) { - static_assert(!is_same_v, "must not be instantiated in kernel code"); - return x; -} - -_LIBCPP_END_NAMESPACE_STD - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_CMATH_ diff --git a/lib/ktl/include/cstdint b/lib/ktl/include/cstdint deleted file mode 100644 index 0ab922c..0000000 --- a/lib/ktl/include/cstdint +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2022 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#pragma once - -// Other libc++ headers use but libc++'s complains if it -// thinks is not its own wrapper, so we define _LIBCPP_STDINT_H. We -// always get the real before the libc++ with the way the include -// paths are set up, which is necessary because of other headers where -// preempting libc++'s header is unavoidable for the kernel environment. - -#include - -#define _LIBCPP_STDINT_H -#include_next diff --git a/lib/ktl/include/cstdio b/lib/ktl/include/cstdio deleted file mode 100644 index 349e736..0000000 --- a/lib/ktl/include/cstdio +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_CSTDIO_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_CSTDIO_ - -// The kernel doesn't want this file but some libc++ headers we need -// wind up including it and they need these declarations. -// -// Note this specifically avoids because libc++ doesn't need that -// but indirectly reaches and the kernel's -// uses so there would be a circularity. - -#include - -#define EOF (-1) - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_CSTDIO_ diff --git a/lib/ktl/include/cstdlib b/lib/ktl/include/cstdlib deleted file mode 100644 index 25ad762..0000000 --- a/lib/ktl/include/cstdlib +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#pragma once - -// This is used by libc++'s just for std::abort, ::posix_memalign, and -// ::free. - -#include - -#include <__config> - -#define _LIBCPP_UNREACHABLE() __builtin_unreachable() - -// This is used by the __libcpp_aligned_alloc inline. Since it always fails -// that inline will trivially always return nullptr. -inline int posix_memalign(void** ptr, size_t align, size_t size) { return 1; } - -// This is used by the __libcpp_aligned_free inline, which won't really ever be -// called. The kernel does have a real free declared elsewhere. -void free(void* ptr); - -_LIBCPP_BEGIN_NAMESPACE_STD - -using ::abort; - -_LIBCPP_END_NAMESPACE_STD diff --git a/lib/ktl/include/cwchar b/lib/ktl/include/cwchar deleted file mode 100644 index 4f24c73..0000000 --- a/lib/ktl/include/cwchar +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_CWCHAR -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_CWCHAR - -// The kernel doesn't want this file but some libc++ headers we need -// wind up including it and they need these declarations. - -#include - -#include <__config> - -_LIBCPP_BEGIN_NAMESPACE_STD - -using ::wmemcpy; -using ::wmemmove; -using ::wmemset; - -// These are called by libc++ header code. The first two should never actually -// be called by anything that really gets instantiated in kernel code since -// wchar_t is never used. However, __constexpr_wmemchr is used by places like -// std::find not just for wchar_t per se but for any trivial type of the same -// size, e.g. uint32_t. So its code must be correct. The other two are filled -// out just in case of an unexpected use. - -constexpr size_t __constexpr_wcslen(const wchar_t* str) { - size_t len = 0; - while (*str++ != L'\0') { - ++len; - } - return len; -} - -constexpr int __constexpr_wmemcmp(const wchar_t* lhs, const wchar_t* rhs, size_t count) { - while (count-- > 0) { - if (*lhs != *rhs) { - return static_cast(*lhs - *rhs); - } - ++lhs; - ++rhs; - } - return 0; -} - -template -constexpr Tp* __constexpr_wmemchr(Tp* str, Up wc, size_t count) { - while (count-- > 0) { - if (*str == wc) { - return str; - } - ++str; - } - return nullptr; -} - -_LIBCPP_END_NAMESPACE_STD - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_CWCHAR diff --git a/lib/ktl/include/ktl/algorithm.h b/lib/ktl/include/ktl/algorithm.h deleted file mode 100644 index 7b7baed..0000000 --- a/lib/ktl/include/ktl/algorithm.h +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ALGORITHM_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ALGORITHM_H_ - -#include - -namespace ktl { - -// "Non-modifying sequence operations" -using std::adjacent_find; -using std::all_of; -using std::any_of; -using std::count; -using std::count_if; -using std::find; -using std::find_end; -using std::find_first_of; -using std::find_if; -using std::find_if_not; -using std::for_each; -using std::for_each_n; -using std::none_of; -using std::search; -using std::search_n; - -namespace ranges { -using std::ranges::adjacent_find; -using std::ranges::all_of; -using std::ranges::any_of; -using std::ranges::count; -using std::ranges::count_if; -using std::ranges::find; -using std::ranges::find_end; -using std::ranges::find_first_of; -using std::ranges::find_if; -using std::ranges::find_if_not; -using std::ranges::for_each; -using std::ranges::for_each_n; -using std::ranges::none_of; -using std::ranges::search; -using std::ranges::search_n; -} // namespace ranges - -// "Modifying sequence operations" (subset) -using std::copy; -using std::copy_if; -using std::fill; -using std::fill_n; -using std::reverse; -using std::shuffle; -using std::transform; - -namespace ranges { -using std::ranges::copy; -using std::ranges::copy_if; -using std::ranges::fill; -using std::ranges::fill_n; -using std::ranges::reverse; -using std::ranges::shuffle; -using std::ranges::transform; -} // namespace ranges - -// "Sorting operations" (subset) -using std::is_sorted; -using std::is_sorted_until; -using std::sort; -using std::stable_sort; - -namespace ranges { -using std::ranges::is_sorted; -using std::ranges::is_sorted_until; -using std::ranges::sort; -using std::ranges::stable_sort; -} // namespace ranges - -// "Binary search operations (on sorted ranges)" -using std::binary_search; -using std::equal_range; -using std::lower_bound; -using std::upper_bound; - -namespace ranges { -using std::ranges::binary_search; -using std::ranges::equal_range; -using std::ranges::lower_bound; -using std::ranges::upper_bound; -} // namespace ranges - -// "Minimum/maximum operations" -using std::clamp; -using std::equal; -using std::max; -using std::max_element; -using std::min; -using std::min_element; -using std::minmax; -using std::minmax_element; - -namespace ranges { -using std::ranges::clamp; -using std::ranges::max; -using std::ranges::max_element; -using std::ranges::min; -using std::ranges::min_element; -using std::ranges::minmax; -using std::ranges::minmax_element; -} // namespace ranges - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ALGORITHM_H_ diff --git a/lib/ktl/include/ktl/array.h b/lib/ktl/include/ktl/array.h deleted file mode 100644 index 90d445a..0000000 --- a/lib/ktl/include/ktl/array.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2019 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ARRAY_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ARRAY_H_ - -#include - -namespace ktl { - -using std::array; -using std::to_array; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ARRAY_H_ diff --git a/lib/ktl/include/ktl/atomic.h b/lib/ktl/include/ktl/atomic.h deleted file mode 100644 index 017db6b..0000000 --- a/lib/ktl/include/ktl/atomic.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2019 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ATOMIC_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ATOMIC_H_ - -#include - -// On arm64 and x86-64, lock-free 128-bit (16-byte) atomics are available. -// But riscv64 does not support them. -#ifdef __riscv -#define HAVE_ATOMIC_128 0 -#else -#define HAVE_ATOMIC_128 1 -#endif - -namespace ktl { - -using std::atomic; -using std::atomic_init; -using std::atomic_ref; - -using std::memory_order; - -using std::memory_order_acq_rel; -using std::memory_order_acquire; -using std::memory_order_consume; -using std::memory_order_relaxed; -using std::memory_order_release; -using std::memory_order_seq_cst; - -using std::atomic_signal_fence; -using std::atomic_thread_fence; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ATOMIC_H_ diff --git a/lib/ktl/include/ktl/bit.h b/lib/ktl/include/ktl/bit.h deleted file mode 100644 index c3ccb7e..0000000 --- a/lib/ktl/include/ktl/bit.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_BIT_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_BIT_H_ - -#include - -namespace ktl { - -using std::bit_cast; -using std::bit_ceil; -using std::bit_floor; -using std::bit_width; -using std::countl_one; -using std::countl_zero; -using std::countr_one; -using std::countr_zero; -using std::endian; -using std::has_single_bit; -using std::popcount; -using std::rotl; -using std::rotr; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_BIT_H_ diff --git a/lib/ktl/include/ktl/byte.h b/lib/ktl/include/ktl/byte.h deleted file mode 100644 index 26b1294..0000000 --- a/lib/ktl/include/ktl/byte.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_BYTE_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_BYTE_H_ - -#include - -namespace ktl { - -using std::byte; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_BYTE_H_ diff --git a/lib/ktl/include/ktl/concepts.h b/lib/ktl/include/ktl/concepts.h deleted file mode 100644 index 93b3f62..0000000 --- a/lib/ktl/include/ktl/concepts.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2025 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_CONCEPTS_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_CONCEPTS_H_ - -#include - -namespace ktl { - -using std::assignable_from; -using std::common_reference_with; -using std::common_with; -using std::constructible_from; -using std::copy_constructible; -using std::copyable; -using std::default_initializable; -using std::derived_from; -using std::destructible; -using std::equality_comparable; -using std::equality_comparable_with; -using std::equivalence_relation; -using std::integral; -using std::invocable; -using std::movable; -using std::move_constructible; -using std::predicate; -using std::regular; -using std::regular_invocable; -using std::relation; -using std::same_as; -using std::semiregular; -using std::signed_integral; -using std::strict_weak_order; -using std::swappable; -using std::swappable_with; -using std::totally_ordered; -using std::totally_ordered_with; -using std::unsigned_integral; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_CONCEPTS_H_ diff --git a/lib/ktl/include/ktl/declval.h b/lib/ktl/include/ktl/declval.h deleted file mode 100644 index 925fe9c..0000000 --- a/lib/ktl/include/ktl/declval.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_DECLVAL_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_DECLVAL_H_ - -#include - -namespace ktl { - -using std::declval; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_DECLVAL_H_ diff --git a/lib/ktl/include/ktl/enforce.h b/lib/ktl/include/ktl/enforce.h deleted file mode 100644 index 9c948d2..0000000 --- a/lib/ktl/include/ktl/enforce.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ENFORCE_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ENFORCE_H_ - -// `#include ` should appear last in the include list of any -// kernel .cc file that uses `ktl` headers. Since many other kernel headers -// use ktl headers and this must appear after *all* uses of any ktl headers, -// whether direct or indirect, it's a good idea to just make sure that -// `#include ` in each .cc file of kernel-only code (i.e. code -// that might ever use ktl directly). -// -// The //.clang-format file has a rule to ensure this file is placed last and -// separate from the other #include lines, so just adding the `#include` line -// to each source file should make sure its placement stays correct. - -// This directive tells the preprocessor that the identifier `std` cannot -// appear in any context (except the expansion of a macro already defined -// before now). This ensures that any stray use of std::name in kernel code -// can get caught at compile time so people remember to use ktl::name instead -// and ensure that what they're using is in the approved ktl subset of std. -// -// It also affects harmless use of `std` as a local variable name or whatever, -// but we can live without kernel code being able to use that one name. -#pragma GCC poison std - -// Also poison the stdcompat namespaces, as the same rules for ktl wrappers -// apply to those as std proper. -#pragma GCC poison cpp17 -#pragma GCC poison cpp20 -#pragma GCC poison cpp23 - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ENFORCE_H_ diff --git a/lib/ktl/include/ktl/forward.h b/lib/ktl/include/ktl/forward.h deleted file mode 100644 index 62874e6..0000000 --- a/lib/ktl/include/ktl/forward.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2018 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_INCLUDE_KTL_FORWARD_H_ -#define ZIRCON_KERNEL_INCLUDE_KTL_FORWARD_H_ - -#include - -namespace ktl { - -using std::forward; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_INCLUDE_KTL_FORWARD_H_ diff --git a/lib/ktl/include/ktl/functional.h b/lib/ktl/include/ktl/functional.h deleted file mode 100644 index 026f4cf..0000000 --- a/lib/ktl/include/ktl/functional.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2023 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_FUNCTIONAL_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_FUNCTIONAL_H_ - -#include - -namespace ktl { - -using std::bind_front; -using std::invoke; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_FUNCTIONAL_H_ diff --git a/lib/ktl/include/ktl/initializer_list.h b/lib/ktl/include/ktl/initializer_list.h deleted file mode 100644 index 3a69abb..0000000 --- a/lib/ktl/include/ktl/initializer_list.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_INITIALIZER_LIST_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_INITIALIZER_LIST_H_ - -#include - -namespace ktl { - -using std::initializer_list; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_INITIALIZER_LIST_H_ diff --git a/lib/ktl/include/ktl/inplace_vector.h b/lib/ktl/include/ktl/inplace_vector.h deleted file mode 100644 index 4eaf92b..0000000 --- a/lib/ktl/include/ktl/inplace_vector.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2025 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_INPLACE_VECTOR_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_INPLACE_VECTOR_H_ - -#include - -namespace ktl { - -using cpp26::inplace_vector; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_INPLACE_VECTOR_H_ diff --git a/lib/ktl/include/ktl/iterator.h b/lib/ktl/include/ktl/iterator.h deleted file mode 100644 index 58d92be..0000000 --- a/lib/ktl/include/ktl/iterator.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ITERATOR_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ITERATOR_H_ - -#include - -namespace ktl { - -// This includes only the "iterator operations" and "range access" subsets -// of the API. These are useful for treating all sorts of -// container types, raw arrays, pointers, and {...} literal lists uniformly. -// It leaves out iterator_traits and the "iterator adaptors" subset, which -// have not yet seemed desirable to employ in kernel code. - -using std::advance; -using std::begin; -using std::cbegin; -using std::cend; -using std::crbegin; -using std::crend; -using std::data; -using std::distance; -using std::empty; -using std::end; -using std::next; -using std::prev; -using std::rbegin; -using std::rend; -using std::size; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_ITERATOR_H_ diff --git a/lib/ktl/include/ktl/limits.h b/lib/ktl/include/ktl/limits.h deleted file mode 100644 index 8f6adf4..0000000 --- a/lib/ktl/include/ktl/limits.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2019 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_INCLUDE_KTL_LIMITS_H_ -#define ZIRCON_KERNEL_INCLUDE_KTL_LIMITS_H_ - -#include - -namespace ktl { - -using std::numeric_limits; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_INCLUDE_KTL_LIMITS_H_ diff --git a/lib/ktl/include/ktl/memory.h b/lib/ktl/include/ktl/memory.h deleted file mode 100644 index caf8d47..0000000 --- a/lib/ktl/include/ktl/memory.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2024 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_MEMORY_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_MEMORY_H_ - -#include - -namespace ktl { - -using std::addressof; -using std::to_address; - -using std::align; -using std::assume_aligned; - -using std::default_delete; - -using std::pointer_traits; - -using std::construct_at; -using std::destroy; -using std::destroy_at; -using std::destroy_n; -using std::uninitialized_copy; -using std::uninitialized_copy_n; -using std::uninitialized_default_construct; -using std::uninitialized_default_construct_n; -using std::uninitialized_fill; -using std::uninitialized_fill_n; -using std::uninitialized_move; -using std::uninitialized_move_n; -using std::uninitialized_value_construct; -using std::uninitialized_value_construct_n; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_MEMORY_H_ diff --git a/lib/ktl/include/ktl/new.h b/lib/ktl/include/ktl/new.h deleted file mode 100644 index 12def96..0000000 --- a/lib/ktl/include/ktl/new.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2026 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_NEW_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_NEW_H_ - -#include - -namespace ktl { - -using std::align_val_t; - -using std::destroying_delete; -using std::destroying_delete_t; -using std::nothrow; -using std::nothrow_t; - -using std::launder; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_NEW_H_ diff --git a/lib/ktl/include/ktl/numeric.h b/lib/ktl/include/ktl/numeric.h deleted file mode 100644 index 18cad1f..0000000 --- a/lib/ktl/include/ktl/numeric.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2023 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_NUMERIC_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_NUMERIC_H_ - -#include - -namespace ktl { - -// "Numeric operations" -using std::accumulate; -using std::reduce; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_NUMERIC_H_ diff --git a/lib/ktl/include/ktl/optional.h b/lib/ktl/include/ktl/optional.h deleted file mode 100644 index e08451d..0000000 --- a/lib/ktl/include/ktl/optional.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_OPTIONAL_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_OPTIONAL_H_ - -#include - -namespace ktl { - -using std::in_place; -using std::in_place_t; -using std::make_optional; -using std::nullopt; -using std::nullopt_t; -using std::optional; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_OPTIONAL_H_ diff --git a/lib/ktl/include/ktl/ranges.h b/lib/ktl/include/ktl/ranges.h deleted file mode 100644 index 8e3ee09..0000000 --- a/lib/ktl/include/ktl/ranges.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2025 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_RANGES_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_RANGES_H_ - -#include - -namespace ktl { - -// This can't be a namespace alias like `views` because also -// adds things to ktl::ranges and redundant namespace aliases aren't allowed. -namespace ranges { -using namespace std::ranges; -} // namespace ranges - -// Only this header defines this namespace, so a simple namespace alias works. -namespace views = std::views; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_RANGES_H_ diff --git a/lib/ktl/include/ktl/ref.h b/lib/ktl/include/ktl/ref.h deleted file mode 100644 index fdaddc4..0000000 --- a/lib/ktl/include/ktl/ref.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2023 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_REF_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_REF_H_ - -#include - -namespace ktl { - -using std::cref; -using std::ref; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_REF_H_ diff --git a/lib/ktl/include/ktl/source_location.h b/lib/ktl/include/ktl/source_location.h deleted file mode 100644 index 121668d..0000000 --- a/lib/ktl/include/ktl/source_location.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2025 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_SOURCE_LOCATION_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_SOURCE_LOCATION_H_ - -#include - -namespace ktl { - -using std::source_location; - -} - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_SOURCE_LOCATION_H_ diff --git a/lib/ktl/include/ktl/span.h b/lib/ktl/include/ktl/span.h deleted file mode 100644 index deb1df3..0000000 --- a/lib/ktl/include/ktl/span.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_SPAN_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_SPAN_H_ - -#include - -namespace ktl { - -using std::span; - -using std::as_bytes; -using std::as_writable_bytes; - -using std::dynamic_extent; - -using std::data; -using std::size; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_SPAN_H_ diff --git a/lib/ktl/include/ktl/string_view.h b/lib/ktl/include/ktl/string_view.h deleted file mode 100644 index 9ba949f..0000000 --- a/lib/ktl/include/ktl/string_view.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_STRING_VIEW_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_STRING_VIEW_H_ - -#include - -namespace ktl { - -using std::basic_string_view; -using std::string_view; - -} // namespace ktl - -// Just including enables "foo"sv literals. -using namespace std::literals::string_view_literals; - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_STRING_VIEW_H_ diff --git a/lib/ktl/include/ktl/tuple.h b/lib/ktl/include/ktl/tuple.h deleted file mode 100644 index b00c7e0..0000000 --- a/lib/ktl/include/ktl/tuple.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2024 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_TUPLE_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_TUPLE_H_ - -#include - -namespace ktl { - -using std::apply; -using std::forward_as_tuple; -using std::get; -using std::ignore; -using std::make_from_tuple; -using std::make_tuple; -using std::tie; -using std::tuple; -using std::tuple_cat; -using std::tuple_element; -using std::tuple_element_t; -using std::tuple_size; -using std::tuple_size_v; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_TUPLE_H_ diff --git a/lib/ktl/include/ktl/type_traits.h b/lib/ktl/include/ktl/type_traits.h deleted file mode 100644 index fc0a2f0..0000000 --- a/lib/ktl/include/ktl/type_traits.h +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright 2019 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_TYPE_TRAITS_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_TYPE_TRAITS_H_ - -#include - -namespace ktl { - -using std::add_const; -using std::add_const_t; - -using std::is_array; -using std::is_array_v; -using std::is_bounded_array; -using std::is_bounded_array_v; -using std::is_unbounded_array; -using std::is_unbounded_array_v; - -using std::false_type; -using std::true_type; - -using std::conjunction; -using std::conjunction_v; - -using std::disjunction; -using std::disjunction_v; - -using std::conditional; -using std::conditional_t; - -using std::decay; -using std::decay_t; - -using std::void_t; - -using std::enable_if; -using std::enable_if_t; - -using std::has_unique_object_representations; -using std::has_unique_object_representations_v; - -using std::is_base_of; -using std::is_base_of_v; - -using std::is_const; -using std::is_const_v; - -using std::is_constructible; -using std::is_constructible_v; - -using std::is_copy_constructible; -using std::is_copy_constructible_v; - -using std::is_copy_assignable; -using std::is_copy_assignable_v; - -using std::is_default_constructible; -using std::is_default_constructible_v; - -using std::is_class; -using std::is_class_v; - -using std::is_enum; -using std::is_enum_v; - -using std::is_floating_point; -using std::is_floating_point_v; - -using std::is_function; -using std::is_function_v; - -using std::is_integral; -using std::is_integral_v; - -using std::invoke_result_t; -using std::is_invocable; -using std::is_invocable_r; -using std::is_invocable_r_v; -using std::is_invocable_v; - -using std::is_lvalue_reference; -using std::is_lvalue_reference_v; - -using std::is_move_constructible; -using std::is_move_constructible_v; - -using std::is_move_assignable; -using std::is_move_assignable_v; - -using std::is_pod; -using std::is_pod_v; - -using std::is_same; -using std::is_same_v; - -using std::is_signed; -using std::is_signed_v; - -using std::is_standard_layout; -using std::is_standard_layout_v; - -using std::is_empty; -using std::is_empty_v; - -using std::is_trivial; -using std::is_trivial_v; - -using std::is_trivially_copy_assignable; -using std::is_trivially_copy_assignable_v; - -using std::is_trivially_copy_constructible; -using std::is_trivially_copy_constructible_v; - -using std::is_trivially_constructible; -using std::is_trivially_constructible_v; - -using std::is_trivially_destructible; -using std::is_trivially_destructible_v; - -using std::is_trivially_move_assignable; -using std::is_trivially_move_assignable_v; - -using std::is_trivially_move_constructible; -using std::is_trivially_move_constructible_v; - -using std::is_member_object_pointer; -using std::is_member_object_pointer_v; - -using std::is_unsigned; -using std::is_unsigned_v; - -using std::is_void; -using std::is_void_v; - -using std::make_signed; -using std::make_signed_t; -using std::make_unsigned; -using std::make_unsigned_t; - -using std::remove_all_extents; -using std::remove_all_extents_t; - -using std::remove_const; -using std::remove_const_t; - -using std::remove_extent; -using std::remove_extent_t; - -using std::remove_pointer; -using std::remove_pointer_t; - -using std::remove_reference; -using std::remove_reference_t; - -using std::underlying_type; -using std::underlying_type_t; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_TYPE_TRAITS_H_ diff --git a/lib/ktl/include/ktl/unique_ptr.h b/lib/ktl/include/ktl/unique_ptr.h deleted file mode 100644 index e2d2f39..0000000 --- a/lib/ktl/include/ktl/unique_ptr.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_INCLUDE_KTL_UNIQUE_PTR_H_ -#define ZIRCON_KERNEL_INCLUDE_KTL_UNIQUE_PTR_H_ - -#include -#include - -#include -#include - -namespace ktl { - -using std::align_val_t; -using std::unique_ptr; - -template -ktl::enable_if_t, unique_ptr> make_unique(fbl::AllocChecker* ac, - Args&&... args) { - return unique_ptr(new (ac) T(std::forward(args)...)); -} - -// For an unbounded array of given size, each element is value-initialized. -// This is different from plain `new (ac) T[n]`, which performs default-initialization. -template -ktl::enable_if_t, unique_ptr> make_unique(fbl::AllocChecker* ac, - size_t n) { - return unique_ptr(new (ac) ktl::remove_extent_t[n]()); -} - -} // namespace ktl - -#endif // ZIRCON_KERNEL_INCLUDE_KTL_UNIQUE_PTR_H_ diff --git a/lib/ktl/include/ktl/utility.h b/lib/ktl/include/ktl/utility.h deleted file mode 100644 index 1e699e2..0000000 --- a/lib/ktl/include/ktl/utility.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2018 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_UTILITY_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_UTILITY_H_ - -#include - -namespace ktl { - -using std::declval; -using std::exchange; -using std::forward; -using std::move; -using std::swap; - -using std::in_place; -using std::in_place_index; -using std::in_place_index_t; -using std::in_place_t; - -using std::index_sequence; -using std::index_sequence_for; -using std::integer_sequence; -using std::make_index_sequence; -using std::make_integer_sequence; - -using std::get; -using std::make_pair; -using std::pair; -using std::piecewise_construct; -using std::piecewise_construct_t; - -using std::ignore; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_UTILITY_H_ diff --git a/lib/ktl/include/ktl/variant.h b/lib/ktl/include/ktl/variant.h deleted file mode 100644 index 68ef362..0000000 --- a/lib/ktl/include/ktl/variant.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_VARIANT_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_VARIANT_H_ - -#include -#include - -namespace ktl { - -using std::get; -using std::get_if; -using std::holds_alternative; -using std::in_place_type; -using std::in_place_type_t; -using std::monostate; -using std::variant; -using std::variant_alternative; -using std::variant_alternative_t; -using std::variant_npos; -using std::variant_size; -using std::variant_size_v; -using std::visit; - -} // namespace ktl - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_KTL_VARIANT_H_ diff --git a/lib/ktl/include/limits b/lib/ktl/include/limits deleted file mode 100644 index d8ea37d..0000000 --- a/lib/ktl/include/limits +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2018 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#pragma once - -// TODO(https://fxbug.dev/42106615): The libc++ defines specializations for -// floating-point types, which in GCC is incompatible with the command-line -// switches used for the kernel. So this header fakes out the other libc++ -// headers with a std::numeric_limits that is close enough. - -#include - -namespace std { - -template -class numeric_limits {}; - -#define SPECIALIZE_INT(type, sign, modulo, digits_, min_, max_) \ - template <> \ - class numeric_limits { \ - public: \ - static constexpr bool is_specialized = true; \ - static constexpr bool is_signed = (sign); \ - static constexpr bool is_integer = true; \ - static constexpr bool is_exact = true; \ - static constexpr bool has_infinity = false; \ - static constexpr bool has_quiet_NaN = false; \ - static constexpr bool has_signaling_NaN = false; \ - static constexpr bool has_denorm = false; \ - static constexpr bool has_denorm_loss = false; \ - static constexpr bool round_style = false; \ - static constexpr bool is_iec559 = false; \ - static constexpr bool is_bounded = true; \ - static constexpr bool is_modulo = (modulo); \ - static constexpr int digits = (digits_); \ - static constexpr int digits10 = (digits_) * 3 / 10; \ - static constexpr int max_digits10 = 0; \ - static constexpr int radix = 2; \ - static constexpr int min_exponent = 0; \ - static constexpr int min_exponent10 = 0; \ - static constexpr int max_exponent = 0; \ - static constexpr int max_exponent10 = 0; \ - static constexpr bool traps = false; \ - static constexpr bool tinyness_before = false; \ - static constexpr type min() { \ - return (min_); \ - } \ - static constexpr type lowest() { \ - return (min_); \ - } \ - static constexpr type max() { \ - return (max_); \ - } \ - static constexpr type epsilon() { \ - return 0; \ - } \ - static constexpr type round_error() { \ - return 0; \ - } \ - static constexpr type infinity() { \ - return 0; \ - } \ - static constexpr type quiet_NaN() { \ - return 0; \ - } \ - static constexpr type signaling_NaN() { \ - return 0; \ - } \ - static constexpr type denorm_min() { \ - return 0; \ - } \ - } - -#define SPECIALIZE_SIGNED(type, min, max) \ - SPECIALIZE_INT(type, true, false, CHAR_BIT * sizeof(type) - 1, min, max) - -#define SPECIALIZE_UNSIGNED(type, min, max) \ - SPECIALIZE_INT(type, false, true, CHAR_BIT * sizeof(type), min, max) - -SPECIALIZE_INT(bool, false, true, 1, false, true); -#if CHAR_MIN == 0 -SPECIALIZE_UNSIGNED(char, CHAR_MIN, CHAR_MAX); -#else -SPECIALIZE_SIGNED(char, CHAR_MIN, CHAR_MAX); -#endif -SPECIALIZE_SIGNED(signed char, SCHAR_MIN, SCHAR_MAX); -SPECIALIZE_UNSIGNED(unsigned char, 0, UCHAR_MAX); - -SPECIALIZE_SIGNED(short, SHRT_MIN, SHRT_MAX); -SPECIALIZE_UNSIGNED(unsigned short, 0, USHRT_MAX); -SPECIALIZE_SIGNED(int, INT_MIN, INT_MAX); -SPECIALIZE_UNSIGNED(unsigned int, 0, UINT_MAX); -SPECIALIZE_SIGNED(long, LONG_MIN, LONG_MAX); -SPECIALIZE_UNSIGNED(unsigned long, 0, ULONG_MAX); -SPECIALIZE_SIGNED(long long, (-__LONG_LONG_MAX__ - 1LL), __LONG_LONG_MAX__); -SPECIALIZE_UNSIGNED(unsigned long long, 0, (__LONG_LONG_MAX__ * 2ULL + 1ULL)); - -#ifdef __SIZEOF_INT128__ -SPECIALIZE_SIGNED(__int128_t, (__int128_t{1} << 127), (~__uint128_t{0} ^ (__int128_t{1} << 127))); -SPECIALIZE_UNSIGNED(__uint128_t, 0, (~__uint128_t{0})); -#endif - -#undef SPECIALIZE_SIGNED -#undef SPECIALIZE_UNSIGNED -#undef SPECIALIZE_INT - -} // namespace std diff --git a/lib/ktl/include/time.h b/lib/ktl/include/time.h deleted file mode 100644 index 138801f..0000000 --- a/lib/ktl/include/time.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_TIME_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_TIME_H_ - -#include - -// uses these when __has_include(). -typedef long int time_t; -struct timespec { - time_t tv_sec; - long int tv_nsec; -}; - -// The kernel doesn't want this file but some libc++ headers we need -// wind up including it and they need these declarations. - -#ifdef __cplusplus - -using clock_t = long int; -struct tm {}; -char* asctime(tm*) noexcept; -clock_t clock() noexcept; -char* ctime(const time_t*) noexcept; -double difftime(time_t, time_t) noexcept; -tm* gmtime(const time_t*) noexcept; -tm* localtime(const time_t*) noexcept; -time_t mktime(tm*) noexcept; -size_t strftime(char*, size_t, const char*, const tm*) noexcept; -time_t time(time_t*) noexcept; -int timespec_get(timespec* ts, int) noexcept; - -#endif // __cplusplus - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_TIME_H_ diff --git a/lib/ktl/include/version b/lib/ktl/include/version deleted file mode 100644 index ba04749..0000000 --- a/lib/ktl/include/version +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2024 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#pragma once - -#include_next - -// We want to be able to implement specialization for std::formatter in common libraries for kernel -// and userspace. By undefing it here, we allow those libraries to avoid exposing that code to the -// kernel where it's not supported. -#undef __cpp_lib_format diff --git a/lib/ktl/include/wchar.h b/lib/ktl/include/wchar.h deleted file mode 100644 index b3f2ccb..0000000 --- a/lib/ktl/include/wchar.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2023 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_WCHAR_H_ -#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_WCHAR_H_ - -// This file is included by and the like that are used from other -// libc++ headers that are wrapped by ktl. This file exists to preempt those -// inclusions so that libc++'s wrapper header won't be used, since -// it's not compatible with the kernel environment. However, other libc++ -// headers also use `#include_next ` to get to "the system libc" -// version of the header. To satisfy those, include-after/wchar.h provides the -// actual declarations needed from the standard C API. So this -// header just defers to that one, but must exist separately in this directory -// so that it preempt's libc++'s wrapper. This can't itself use #include_next -// because that would reach libc++'s wrapper before the include-after file. - -#include "../include-after/wchar.h" - -#endif // ZIRCON_KERNEL_LIB_KTL_INCLUDE_WCHAR_H_ diff --git a/lib/ktl/popcount.cc b/lib/ktl/popcount.cc deleted file mode 100644 index 07b7888..0000000 --- a/lib/ktl/popcount.cc +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright 2019 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#include - -// When the compiler doesn't inline __builtin_popcount{,l}, it calls these. -extern "C" int __popcountsi2(uint32_t val); -extern "C" int __popcountdi2(uint64_t val); -// If the compiler doesn't inline __builtin_popcount % 2 it calls this. -extern "C" int __paritydi2(uint64_t val); - -extern "C" int __popcountsi2(uint32_t val) { - // Implement a log2(bits) popcount, with a few tricks to save some - // instructions. - // - // The general idea here is to simply add up the bits in the word in - // parallel, storing intermediate results in different bit positions in - // the word as we go, and eventually arriving at a sum which is the - // popcount of the word. - // - // For example, consider an 8 bit integer written as - // - // b7 b6 b5 b4 b3 b2 b1 b0 - // - // Each of the bits in the word represent a partial sum that, when added - // together, produce our result. We can compute... - // - // ( - // b7 b6 b5 b4 b3 b2 b1 b0 & - // 0 1 0 1 0 1 0 1 = - // 0 b6 0 b4 0 b2 0 b0 - // ) + ( - // b7 b6 b5 b4 b3 b2 b1 b0 >> 1 = - // 0 b7 b6 b5 b4 b3 b2 b1 & - // 0 1 0 1 0 1 0 1 = - // 0 b7 0 b5 0 b3 0 b1 - // ) = - // b7+b6 b5+b4 b3+b2 b1+b0 - // - // And now we have 4 partial sums, each of which takes two bits of - // storage instead of one. Repeating this process with appropriate - // masks and shifts will eventually give us the answer we are looking - // for. After the next step, we will have partial sums which require 3 - // bits of storage, but aligned to 4 bit boundaries; so... - // - // 0 (b7+b6+b5+b4) 0 (b3+b2+b1+b0) - // - // After the last step, we will get the final sum stored in 4 bits, with - // guaranteed 0s for the upper bits. - // aligned to ... - // - // 0 0 0 0 (b7+b6+b5+b4+b3+b2+b1+b0) - // - // While this is the general idea, it turns out that we only need to - // follow this exact process during step #2. For each of the other - // steps (there will be 5 for a 32 bit integer) we can shave a few - // cycles by taking advantage of some of the particular properties of - // each step. See the comments below. - constexpr uint32_t mask1 = 0x55555555; - constexpr uint32_t mask2 = 0x33333333; - constexpr uint32_t mask3 = 0x0F0F0F0F; - constexpr uint32_t mult1 = 0x01010101; - - // Step 1: - // While we could compute (val & mask) + ((val >> 1) & mask), we can - // actually save one of the mask operations by subtracting instead of - // adding. Consider the following truth table... - // - // In | >>1 | & | - | - // X00 | XX0 | X00 | X00 | - // X01 | XX0 | X00 | X01 | - // X10 | XX1 | X01 | X01 | - // X11 | XX1 | X01 | X10 | - // - val = val - ((val >> 1) & mask1); - - // Step 2: - // This is simply the operation described in the overview, adding a - // bunch of 2 bit partials to produce a 3 bit result, but which is - // aligned on 4 bit boundaries (where the MSB of each 4 bit nibble is - // guaranteed to be 0) - val = (val & mask2) + ((val >> 2) & mask2); - - // Step 3: - // Again, we can save a mask operation here. This time, it is because - // the we know that all bit positions ((i * 4) + 3) are guaranteed to be - // zero. We can simply shift by 4 and add the partial sums. Any - // overflow will go into the ((i * 4) + 3) position and not interfere - // with any of the other partial sums. - val = (val + (val >> 4)) & mask3; - - // Step 4 + 5: - // Finally, we can combine steps 4 and 5 using a multiply. We have 4 - // remaining partial sums, each of which is contained in 4 bits and - // aligned to 8 bit boundaries (with 4 bits of zero in-between each - // sum). Now, we have enough space that if we could perform all 4 sums - // at once, we know that the result would fit in (at most) 6 bits, which - // easily fits in our 8 bits of space. - // - // At its heart, multiplying is the equivalent of repeated shifting, and - // conditional adding. IOW - if I multiply by 0101b, it is basically - // the same as saying "shift X left by 0, then add that to shift X left - // by 2" (because bits 0 and bits 2 are the only bits set). - // - // So, we have, 0x0A0B0C0D, and want to compute (A+B+C+D). Multiplying - // our register by 0x01010101 basically does this for us. We are - // summing our register shifted left by 0 (A), 8 (B), 16 (C) and 24 (D), - // which puts A+B+C+D into the upper 8 bits of the register. The lower - // 24 bits are junk, but we know (because of the 0s which separate A-D) - // that this junk is not going to overflow into our result. All we need - // to do is right shift by 24, and we are done. - return (val * mult1) >> 24; -} - -extern "C" int __popcountdi2(uint64_t val) { - // See notes above for how this works. The 64 bit version of this is - // identical to the 32 bit version, the only difference is that during - // the final stage, we use the multiply trick to combine steps 4, 5, and - // 6 to sum all 8 4-bit partial sums at once, fitting the result into a - // 7 bit value. - constexpr uint64_t mask1 = 0x5555555555555555; - constexpr uint64_t mask2 = 0x3333333333333333; - constexpr uint64_t mask3 = 0x0F0F0F0F0F0F0F0F; - constexpr uint64_t mult1 = 0x0101010101010101; - - val = val - ((val >> 1) & mask1); - val = (val & mask2) + ((val >> 2) & mask2); - val = (val + (val >> 4)) & mask3; - return static_cast((val * mult1) >> 56); -} - -extern "C" int __paritydi2(uint64_t val) { return __popcountdi2(val) % 2; } diff --git a/lib/ktl/stub-new.cc b/lib/ktl/stub-new.cc deleted file mode 100644 index 919d941..0000000 --- a/lib/ktl/stub-new.cc +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#include - -// std::stable_sort (used as ktl::stable_sort) uses std::get_temporary_buffer, -// which uses operator new to request a temporary buffer but then works fine -// if it can't get one. There is no "default" memory allocation in the -// kernel, so just provide a stub operator new that always fails so -// std::stable_sort falls back to its no-allocation algorithm. - -namespace std { - -const nothrow_t nothrow; - -} // namespace std - -void* operator new(std::size_t, const std::nothrow_t&) noexcept { return nullptr; } - -void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept { - return nullptr; -} diff --git a/lib/libc/LICENSE b/lib/libc/LICENSE deleted file mode 100644 index 8580f02..0000000 --- a/lib/libc/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -Copyright 2016 The Fuchsia Authors -Copyright (c) 2008-2015 Travis Geiselbrecht - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files -(the "Software"), to deal in the Software without restriction, -including without limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of the Software, -and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/libc/README.lume b/lib/libc/README.lume deleted file mode 100644 index 8d50752..0000000 --- a/lib/libc/README.lume +++ /dev/null @@ -1,10 +0,0 @@ -Zircon kernel libc header layer - -Source: zircon/kernel/lib/libc/include and include-after -Fuchsia revision: f8d670ab4bbb71bfb4d569893ba12fbb8a83bacd - -This first import contains the complete public header layer. The libc -implementation sources are intentionally not imported yet because abort, -panic, heap, console output, and C++ ABI providers belong to later foundation -batches. Existing LumeOS C implementations continue to provide the symbols -used by the current kernel. diff --git a/lib/libc/include-after/ctype.h b/lib/libc/include-after/ctype.h deleted file mode 100644 index ff2fcfe..0000000 --- a/lib/libc/include-after/ctype.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2016 The Fuchsia Authors -// Copyright (c) 2008 Travis Geiselbrecht -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_CTYPE_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_CTYPE_H_ - -#include - -__BEGIN_CDECLS - -int isalnum(int c); -int isalpha(int c); -int isblank(int c); -int iscntrl(int c); -int isdigit(int c); -int isgraph(int c); -int islower(int c); -int isprint(int c); -int ispunct(int c); -int isspace(int c); -int isupper(int c); -int isxdigit(int c); - -int tolower(int c); -int toupper(int c); - -__END_CDECLS - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_CTYPE_H_ diff --git a/lib/libc/include-after/limits.h b/lib/libc/include-after/limits.h deleted file mode 100644 index f946def..0000000 --- a/lib/libc/include-after/limits.h +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -// Empty file for toolchain's #include_next to find. diff --git a/lib/libc/include/assert.h b/lib/libc/include/assert.h deleted file mode 100644 index 8b6252e..0000000 --- a/lib/libc/include/assert.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#pragma once - -#include - -#ifdef NDEBUG -#define assert(expr) ((void)0) -#else -#define assert(expr) ZX_ASSERT(expr) -#endif - -// make sure static_assert() is defined, even in C -#if !defined(__cplusplus) && !defined(static_assert) -#define static_assert(e, msg) _Static_assert(e, msg) -#endif diff --git a/lib/libc/include/inttypes.h b/lib/libc/include/inttypes.h deleted file mode 100644 index b19e0f5..0000000 --- a/lib/libc/include/inttypes.h +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2016 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_INTTYPES_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_INTTYPES_H_ - -// This (or the compiler) defines all the ___FMT__ macros. -#include - -#define PRId8 __INT8_FMTd__ -#define PRId16 __INT16_FMTd__ -#define PRId32 __INT32_FMTd__ -#define PRId64 __INT64_FMTd__ - -#define PRIdLEAST8 __INT_LEAST8_FMTd__ -#define PRIdLEAST16 __INT_LEAST16_FMTd__ -#define PRIdLEAST32 __INT_LEAST32_FMTd__ -#define PRIdLEAST64 __INT_LEAST64_FMTd__ - -#define PRIdFAST8 __INT_FAST8_FMTd__ -#define PRIdFAST16 __INT_FAST16_FMTd__ -#define PRIdFAST32 __INT_FAST32_FMTd__ -#define PRIdFAST64 __INT_FAST64_FMTd__ - -#define PRIi8 __INT8_FMTi__ -#define PRIi16 __INT16_FMTi__ -#define PRIi32 __INT32_FMTi__ -#define PRIi64 __INT64_FMTi__ - -#define PRIiLEAST8 __INT_LEAST8_FMTi__ -#define PRIiLEAST16 __INT_LEAST16_FMTi__ -#define PRIiLEAST32 __INT_LEAST32_FMTi__ -#define PRIiLEAST64 __INT_LEAST64_FMTi__ - -#define PRIiFAST8 __INT_FAST8_FMTi__ -#define PRIiFAST16 __INT_FAST16_FMTi__ -#define PRIiFAST32 __INT_FAST32_FMTi__ -#define PRIiFAST64 __INT_FAST64_FMTi__ - -#define PRIo8 __UINT8_FMTo__ -#define PRIo16 __UINT16_FMTo__ -#define PRIo32 __UINT32_FMTo__ -#define PRIo64 __UINT64_FMTo__ - -#define PRIoLEAST8 __UINT_LEAST8_FMTo__ -#define PRIoLEAST16 __UINT_LEAST16_FMTo__ -#define PRIoLEAST32 __UINT_LEAST32_FMTo__ -#define PRIoLEAST64 __UINT_LEAST64_FMTo__ - -#define PRIoFAST8 __UINT_FAST8_FMTo__ -#define PRIoFAST16 __UINT_FAST16_FMTo__ -#define PRIoFAST32 __UINT_FAST32_FMTo__ -#define PRIoFAST64 __UINT_FAST64_FMTo__ - -#define PRIu8 __UINT8_FMTu__ -#define PRIu16 __UINT16_FMTu__ -#define PRIu32 __UINT32_FMTu__ -#define PRIu64 __UINT64_FMTu__ - -#define PRIuLEAST8 __UINT_LEAST8_FMTu__ -#define PRIuLEAST16 __UINT_LEAST16_FMTu__ -#define PRIuLEAST32 __UINT_LEAST32_FMTu__ -#define PRIuLEAST64 __UINT_LEAST64_FMTu__ - -#define PRIuFAST8 __UINT_FAST8_FMTu__ -#define PRIuFAST16 __UINT_FAST16_FMTu__ -#define PRIuFAST32 __UINT_FAST32_FMTu__ -#define PRIuFAST64 __UINT_FAST64_FMTu__ - -#define PRIx8 __UINT8_FMTx__ -#define PRIx16 __UINT16_FMTx__ -#define PRIx32 __UINT32_FMTx__ -#define PRIx64 __UINT64_FMTx__ - -#define PRIxLEAST8 __UINT_LEAST8_FMTx__ -#define PRIxLEAST16 __UINT_LEAST16_FMTx__ -#define PRIxLEAST32 __UINT_LEAST32_FMTx__ -#define PRIxLEAST64 __UINT_LEAST64_FMTx__ - -#define PRIxFAST8 __UINT_FAST8_FMTx__ -#define PRIxFAST16 __UINT_FAST16_FMTx__ -#define PRIxFAST32 __UINT_FAST32_FMTx__ -#define PRIxFAST64 __UINT_FAST64_FMTx__ - -#define PRIX8 __UINT8_FMTX__ -#define PRIX16 __UINT16_FMTX__ -#define PRIX32 __UINT32_FMTX__ -#define PRIX64 __UINT64_FMTX__ - -#define PRIXLEAST8 __UINT_LEAST8_FMTX__ -#define PRIXLEAST16 __UINT_LEAST16_FMTX__ -#define PRIXLEAST32 __UINT_LEAST32_FMTX__ -#define PRIXLEAST64 __UINT_LEAST64_FMTX__ - -#define PRIXFAST8 __UINT_FAST8_FMTX__ -#define PRIXFAST16 __UINT_FAST16_FMTX__ -#define PRIXFAST32 __UINT_FAST32_FMTX__ -#define PRIXFAST64 __UINT_FAST64_FMTX__ - -#define PRIdMAX __INTMAX_FMTd__ -#define PRIiMAX __INTMAX_FMTi__ -#define PRIoMAX __UINTMAX_FMTo__ -#define PRIuMAX __UINTMAX_FMTu__ -#define PRIxMAX __UINTMAX_FMTx__ -#define PRIXMAX __UINTMAX_FMTX__ - -#define PRIdPTR __INTPTR_FMTd__ -#define PRIiPTR __INTPTR_FMTi__ -#define PRIoPTR __UINTPTR_FMTo__ -#define PRIuPTR __UINTPTR_FMTu__ -#define PRIxPTR __UINTPTR_FMTx__ -#define PRIXPTR __UINTPTR_FMTX__ - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_INTTYPES_H_ diff --git a/lib/libc/include/multi-file.h b/lib/libc/include/multi-file.h deleted file mode 100644 index a04a031..0000000 --- a/lib/libc/include/multi-file.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2022 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_MULTI_FILE_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_MULTI_FILE_H_ - -#include - -#include -#include -#include -#include - -// MultiFile broadcasts Write calls across multiple FILE* pointers. Each -// pointer is ignored if it's null. MultiFile::Write always returns success, -// ignoring any failures or short writes from the underlying FILE objects. -template -class MultiFile : public FILE { - public: - using FileArray = ktl::array; - - constexpr MultiFile() : FILE{this} {} - - constexpr explicit MultiFile(const FileArray& files) : FILE{this}, files_(files) {} - - constexpr MultiFile(const MultiFile& other) : MultiFile(other.files_) {} - - FileArray& files() { return files_; } - const FileArray& files() const { return files_; } - - int Write(ktl::string_view str) { - for (FILE* file : files_) { - if (file) { - file->Write(str); - } - } - return static_cast(str.size()); - } - - private: - FileArray files_ = {}; -}; - -template -MultiFile(const ktl::array&) -> MultiFile; - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_MULTI_FILE_H_ diff --git a/lib/libc/include/stdint.h b/lib/libc/include/stdint.h deleted file mode 100644 index 3cbdfeb..0000000 --- a/lib/libc/include/stdint.h +++ /dev/null @@ -1,278 +0,0 @@ -// Copyright 2016 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDINT_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDINT_H_ - -// The compiler predefines macros ___TYPE__, ___MAX__, -// and ___C for the various types. All we have to do here is -// define the public names based on those macros. - -// Clang predefines macros ___FMT__ for each type, -// with being i and for signed types, and o, u, x, and X -// for unsigned types. That lets do its work without -// any special knowledge of what the underlying types are. -// Unfortunately, GCC does not define these macros. To keep all -// knowledge of the compiler's choices isolated to this file, define -// them here for GCC so that can use them unconditionally. -#ifndef __INTMAX_FMTd__ - -#define __INT8_FMT_MODIFIER__ "hh" -#define __INT16_FMT_MODIFIER__ "h" -#ifdef _LP64 -#define __INT32_FMT_MODIFIER__ "" -#else -#define __INT32_FMT_MODIFIER__ "l" -#endif - -#define __INT_LEAST8_FMT_MODIFIER__ __INT8_FMT_MODIFIER__ -#define __INT_LEAST16_FMT_MODIFIER__ __INT16_FMT_MODIFIER__ -#define __INT_LEAST32_FMT_MODIFIER__ __INT32_FMT_MODIFIER__ -#define __INT_LEAST64_FMT_MODIFIER__ __INT64_FMT_MODIFIER__ - -// The *-elf and arm-eabi GCC targets use 'int' for the fast{8,16,32} -// types. On LP64 systems, 'long' is used for the fast64 type, and -// 'long long' on non-LP64 systems. -#define __INT_FAST8_FMT_MODIFIER__ "" -#define __INT_FAST16_FMT_MODIFIER__ "" -#define __INT_FAST32_FMT_MODIFIER__ "" -#if _LP64 -#define __INT_FAST64_FMT_MODIFIER__ "l" -#else -#define __INT_FAST64_FMT_MODIFIER__ "ll" -#endif - -// On machines where 'long' types are 64 bits, the compiler defines -// __INT64_TYPE__ et al using 'long', not 'long long', though both are -// 64-bit types. -#ifdef _LP64 -#define __INT64_FMT_MODIFIER__ "l" -#define __INTPTR_FMT_MODIFIER__ "l" -#else -#define __INT64_FMT_MODIFIER__ "ll" -#define __INTPTR_FMT_MODIFIER__ "" -#endif - -#define __INTMAX_FMT_MODIFIER__ __INT64_FMT_MODIFIER__ - -#define __INTMAX_FMTd__ __INTMAX_FMT_MODIFIER__ "d" -#define __INTMAX_FMTi__ __INTMAX_FMT_MODIFIER__ "i" -#define __UINTMAX_FMTo__ __INTMAX_FMT_MODIFIER__ "o" -#define __UINTMAX_FMTu__ __INTMAX_FMT_MODIFIER__ "u" -#define __UINTMAX_FMTx__ __INTMAX_FMT_MODIFIER__ "x" -#define __UINTMAX_FMTX__ __INTMAX_FMT_MODIFIER__ "X" -#define __INTPTR_FMTd__ __INTPTR_FMT_MODIFIER__ "d" -#define __INTPTR_FMTi__ __INTPTR_FMT_MODIFIER__ "i" -#define __UINTPTR_FMTo__ __INTPTR_FMT_MODIFIER__ "o" -#define __UINTPTR_FMTu__ __INTPTR_FMT_MODIFIER__ "u" -#define __UINTPTR_FMTx__ __INTPTR_FMT_MODIFIER__ "x" -#define __UINTPTR_FMTX__ __INTPTR_FMT_MODIFIER__ "X" -#define __INT8_FMTd__ __INT8_FMT_MODIFIER__ "d" -#define __INT8_FMTi__ __INT8_FMT_MODIFIER__ "i" -#define __INT16_FMTd__ __INT16_FMT_MODIFIER__ "d" -#define __INT16_FMTi__ __INT16_FMT_MODIFIER__ "i" -#define __INT32_FMTd__ __INT32_FMT_MODIFIER__ "d" -#define __INT32_FMTi__ __INT32_FMT_MODIFIER__ "i" -#define __INT64_FMTd__ __INT64_FMT_MODIFIER__ "d" -#define __INT64_FMTi__ __INT64_FMT_MODIFIER__ "i" -#define __UINT8_FMTo__ __INT8_FMT_MODIFIER__ "o" -#define __UINT8_FMTu__ __INT8_FMT_MODIFIER__ "u" -#define __UINT8_FMTx__ __INT8_FMT_MODIFIER__ "x" -#define __UINT8_FMTX__ __INT8_FMT_MODIFIER__ "X" -#define __UINT16_FMTo__ __INT16_FMT_MODIFIER__ "o" -#define __UINT16_FMTu__ __INT16_FMT_MODIFIER__ "u" -#define __UINT16_FMTx__ __INT16_FMT_MODIFIER__ "x" -#define __UINT16_FMTX__ __INT16_FMT_MODIFIER__ "X" -#define __UINT32_FMTo__ __INT32_FMT_MODIFIER__ "o" -#define __UINT32_FMTu__ __INT32_FMT_MODIFIER__ "u" -#define __UINT32_FMTx__ __INT32_FMT_MODIFIER__ "x" -#define __UINT32_FMTX__ __INT32_FMT_MODIFIER__ "X" -#define __UINT64_FMTo__ __INT64_FMT_MODIFIER__ "o" -#define __UINT64_FMTu__ __INT64_FMT_MODIFIER__ "u" -#define __UINT64_FMTx__ __INT64_FMT_MODIFIER__ "x" -#define __UINT64_FMTX__ __INT64_FMT_MODIFIER__ "X" -#define __INT_LEAST8_FMTd__ __INT_LEAST8_FMT_MODIFIER__ "d" -#define __INT_LEAST8_FMTi__ __INT_LEAST8_FMT_MODIFIER__ "i" -#define __UINT_LEAST8_FMTo__ __INT_LEAST8_FMT_MODIFIER__ "o" -#define __UINT_LEAST8_FMTu__ __INT_LEAST8_FMT_MODIFIER__ "u" -#define __UINT_LEAST8_FMTx__ __INT_LEAST8_FMT_MODIFIER__ "x" -#define __UINT_LEAST8_FMTX__ __INT_LEAST8_FMT_MODIFIER__ "X" -#define __INT_LEAST16_FMTd__ __INT_LEAST16_FMT_MODIFIER__ "d" -#define __INT_LEAST16_FMTi__ __INT_LEAST16_FMT_MODIFIER__ "i" -#define __UINT_LEAST16_FMTo__ __INT_LEAST16_FMT_MODIFIER__ "o" -#define __UINT_LEAST16_FMTu__ __INT_LEAST16_FMT_MODIFIER__ "u" -#define __UINT_LEAST16_FMTx__ __INT_LEAST16_FMT_MODIFIER__ "x" -#define __UINT_LEAST16_FMTX__ __INT_LEAST16_FMT_MODIFIER__ "X" -#define __INT_LEAST32_FMTd__ __INT_LEAST32_FMT_MODIFIER__ "d" -#define __INT_LEAST32_FMTi__ __INT_LEAST32_FMT_MODIFIER__ "i" -#define __UINT_LEAST32_FMTo__ __INT_LEAST32_FMT_MODIFIER__ "o" -#define __UINT_LEAST32_FMTu__ __INT_LEAST32_FMT_MODIFIER__ "u" -#define __UINT_LEAST32_FMTx__ __INT_LEAST32_FMT_MODIFIER__ "x" -#define __UINT_LEAST32_FMTX__ __INT_LEAST32_FMT_MODIFIER__ "X" -#define __INT_LEAST64_FMTd__ __INT_LEAST64_FMT_MODIFIER__ "d" -#define __INT_LEAST64_FMTi__ __INT_LEAST64_FMT_MODIFIER__ "i" -#define __UINT_LEAST64_FMTo__ __INT_LEAST64_FMT_MODIFIER__ "o" -#define __UINT_LEAST64_FMTu__ __INT_LEAST64_FMT_MODIFIER__ "u" -#define __UINT_LEAST64_FMTx__ __INT_LEAST64_FMT_MODIFIER__ "x" -#define __UINT_LEAST64_FMTX__ __INT_LEAST64_FMT_MODIFIER__ "X" -#define __INT_FAST8_FMTd__ __INT_FAST8_FMT_MODIFIER__ "d" -#define __INT_FAST8_FMTi__ __INT_FAST8_FMT_MODIFIER__ "i" -#define __UINT_FAST8_FMTo__ __INT_FAST8_FMT_MODIFIER__ "o" -#define __UINT_FAST8_FMTu__ __INT_FAST8_FMT_MODIFIER__ "u" -#define __UINT_FAST8_FMTx__ __INT_FAST8_FMT_MODIFIER__ "x" -#define __UINT_FAST8_FMTX__ __INT_FAST8_FMT_MODIFIER__ "X" -#define __INT_FAST16_FMTd__ __INT_FAST16_FMT_MODIFIER__ "d" -#define __INT_FAST16_FMTi__ __INT_FAST16_FMT_MODIFIER__ "i" -#define __UINT_FAST16_FMTo__ __INT_FAST16_FMT_MODIFIER__ "o" -#define __UINT_FAST16_FMTu__ __INT_FAST16_FMT_MODIFIER__ "u" -#define __UINT_FAST16_FMTx__ __INT_FAST16_FMT_MODIFIER__ "x" -#define __UINT_FAST16_FMTX__ __INT_FAST16_FMT_MODIFIER__ "X" -#define __INT_FAST32_FMTd__ __INT_FAST32_FMT_MODIFIER__ "d" -#define __INT_FAST32_FMTi__ __INT_FAST32_FMT_MODIFIER__ "i" -#define __UINT_FAST32_FMTo__ __INT_FAST32_FMT_MODIFIER__ "o" -#define __UINT_FAST32_FMTu__ __INT_FAST32_FMT_MODIFIER__ "u" -#define __UINT_FAST32_FMTx__ __INT_FAST32_FMT_MODIFIER__ "x" -#define __UINT_FAST32_FMTX__ __INT_FAST32_FMT_MODIFIER__ "X" -#define __INT_FAST64_FMTd__ __INT_FAST64_FMT_MODIFIER__ "d" -#define __INT_FAST64_FMTi__ __INT_FAST64_FMT_MODIFIER__ "i" -#define __UINT_FAST64_FMTo__ __INT_FAST64_FMT_MODIFIER__ "o" -#define __UINT_FAST64_FMTu__ __INT_FAST64_FMT_MODIFIER__ "u" -#define __UINT_FAST64_FMTx__ __INT_FAST64_FMT_MODIFIER__ "x" -#define __UINT_FAST64_FMTX__ __INT_FAST64_FMT_MODIFIER__ "X" - -#endif - -typedef __UINT8_TYPE__ uint8_t; -typedef __UINT16_TYPE__ uint16_t; -typedef __UINT32_TYPE__ uint32_t; -typedef __UINT64_TYPE__ uint64_t; -typedef __INT8_TYPE__ int8_t; -typedef __INT16_TYPE__ int16_t; -typedef __INT32_TYPE__ int32_t; -typedef __INT64_TYPE__ int64_t; - -#define UINT8_MAX __UINT8_MAX__ -#define UINT16_MAX __UINT16_MAX__ -#define UINT32_MAX __UINT32_MAX__ -#define UINT64_MAX __UINT64_MAX__ - -#define INT8_MAX __INT8_MAX__ -#define INT16_MAX __INT16_MAX__ -#define INT32_MAX __INT32_MAX__ -#define INT64_MAX __INT64_MAX__ - -#define INT8_MIN (-INT8_MAX - 1) -#define INT16_MIN (-INT16_MAX - 1) -#define INT32_MIN (-INT32_MAX - 1) -#define INT64_MIN (-INT64_MAX - 1) - -typedef __UINT_LEAST8_TYPE__ uint_least8_t; -typedef __UINT_LEAST16_TYPE__ uint_least16_t; -typedef __UINT_LEAST32_TYPE__ uint_least32_t; -typedef __UINT_LEAST64_TYPE__ uint_least64_t; -typedef __INT_LEAST8_TYPE__ int_least8_t; -typedef __INT_LEAST16_TYPE__ int_least16_t; -typedef __INT_LEAST32_TYPE__ int_least32_t; -typedef __INT_LEAST64_TYPE__ int_least64_t; - -#define UINT_LEAST8_MAX __UINT_LEAST8_MAX__ -#define UINT_LEAST16_MAX __UINT_LEAST16_MAX__ -#define UINT_LEAST32_MAX __UINT_LEAST32_MAX__ -#define UINT_LEAST64_MAX __UINT_LEAST64_MAX__ - -#define INT_LEAST8_MAX __INT_LEAST8_MAX__ -#define INT_LEAST16_MAX __INT_LEAST16_MAX__ -#define INT_LEAST32_MAX __INT_LEAST32_MAX__ -#define INT_LEAST64_MAX __INT_LEAST64_MAX__ - -#define INT_LEAST8_MIN (-INT_LEAST8_MAX - 1) -#define INT_LEAST16_MIN (-INT_LEAST16_MAX - 1) -#define INT_LEAST32_MIN (-INT_LEAST32_MAX - 1) -#define INT_LEAST64_MIN (-INT_LEAST64_MAX - 1) - -typedef __UINT_FAST8_TYPE__ uint_fast8_t; -typedef __UINT_FAST16_TYPE__ uint_fast16_t; -typedef __UINT_FAST32_TYPE__ uint_fast32_t; -typedef __UINT_FAST64_TYPE__ uint_fast64_t; -typedef __INT_FAST8_TYPE__ int_fast8_t; -typedef __INT_FAST16_TYPE__ int_fast16_t; -typedef __INT_FAST32_TYPE__ int_fast32_t; -typedef __INT_FAST64_TYPE__ int_fast64_t; - -#define UINT_FAST8_MAX __UINT_FAST8_MAX__ -#define UINT_FAST16_MAX __UINT_FAST16_MAX__ -#define UINT_FAST32_MAX __UINT_FAST32_MAX__ -#define UINT_FAST64_MAX __UINT_FAST64_MAX__ - -#define INT_FAST8_MAX __INT_FAST8_MAX__ -#define INT_FAST16_MAX __INT_FAST16_MAX__ -#define INT_FAST32_MAX __INT_FAST32_MAX__ -#define INT_FAST64_MAX __INT_FAST64_MAX__ - -#define INT_FAST8_MIN (-INT_FAST8_MAX - 1) -#define INT_FAST16_MIN (-INT_FAST16_MAX - 1) -#define INT_FAST32_MIN (-INT_FAST32_MAX - 1) -#define INT_FAST64_MIN (-INT_FAST64_MAX - 1) - -typedef __INTPTR_TYPE__ intptr_t; -typedef __UINTPTR_TYPE__ uintptr_t; - -#define INTPTR_MAX __INTPTR_MAX__ -#define INTPTR_MIN (-INTPTR_MAX - 1) -#define UINTPTR_MAX __UINTPTR_MAX__ - -typedef __INTMAX_TYPE__ intmax_t; -typedef __UINTMAX_TYPE__ uintmax_t; - -#define INTMAX_MAX __INTMAX_MAX__ -#define INTMAX_MIN (-INTMAX_MAX - 1) -#define UINTMAX_MAX __UINTMAX_MAX__ - -#define SIZE_MAX __SIZE_MAX__ - -// GCC defines __INT_C macros which append the correct suffix -// to an (un)signed integer literal, with being one of 8, 16, 32, 64 -// and MAX, and type being U for unsigned types. The standard stdint.h macros -// with the same names without the leading __ could be implemented in terms of -// these macros. -// -// Clang predefines macros __INT_C_SUFFIX__ which expand to the -// suffix itself. We define the standard stdint.h macros in terms of the GCC -// variants and for Clang we define their equivalents. -#ifndef __INTMAX_C - -#define __int_c_join(a, b) a##b -#define __int_c(v, suffix) __int_c_join(v, suffix) - -#define __INT8_C(c) __int_c(c, __INT8_C_SUFFIX__) -#define __INT16_C(c) __int_c(c, __INT16_C_SUFFIX__) -#define __INT32_C(c) __int_c(c, __INT32_C_SUFFIX__) -#define __INT64_C(c) __int_c(c, __INT64_C_SUFFIX__) - -#define __UINT8_C(c) __int_c(c, __UINT8_C_SUFFIX__) -#define __UINT16_C(c) __int_c(c, __UINT16_C_SUFFIX__) -#define __UINT32_C(c) __int_c(c, __UINT32_C_SUFFIX__) -#define __UINT64_C(c) __int_c(c, __UINT64_C_SUFFIX__) - -#define __INTMAX_C(c) __int_c(c, __INTMAX_C_SUFFIX__) -#define __UINTMAX_C(c) __int_c(c, __UINTMAX_C_SUFFIX__) - -#endif - -#define INT8_C(c) __INT8_C(c) -#define INT16_C(c) __INT16_C(c) -#define INT32_C(c) __INT32_C(c) -#define INT64_C(c) __INT64_C(c) - -#define UINT8_C(c) __UINT8_C(c) -#define UINT16_C(c) __UINT16_C(c) -#define UINT32_C(c) __UINT32_C(c) -#define UINT64_C(c) __UINT64_C(c) - -#define INTMAX_C(c) __INTMAX_C(c) -#define UINTMAX_C(c) __UINTMAX_C(c) - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDINT_H_ diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h deleted file mode 100644 index ae5a84b..0000000 --- a/lib/libc/include/stdio.h +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2020 The Fuchsia Authors -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDIO_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDIO_H_ - -#include -#include -#include - -// All anybody really wants from stdio is printf. - -#ifdef __cplusplus -#include - -class FILE { - public: - // This is basically equivalent to having a virtual Write function with - // subclasses providing their own data members in lieu of ptr. But it's - // simpler and avoids a vtable that might need address fixup at load time - // (and the double indirection for a single-entry vtable--at the cost of - // double indirection for the ptr data in a callback that uses it). - - using Callback = int(void*, ktl::string_view); - - FILE() = default; - - constexpr FILE(Callback* write, void* ptr) : write_(write), ptr_(ptr) {} - - template - explicit FILE(T* writer) - : write_([](void* ptr, ktl::string_view s) { return static_cast(ptr)->Write(s); }), - ptr_(writer) {} - - // This is what fprintf calls to do output. - int Write(ktl::string_view s) { return write_(ptr_, s); } - - constexpr explicit operator bool() const { return write_; } - - constexpr bool operator==(const FILE& other) const { - return write_ == other.write_ && ptr_ == other.ptr_; - } - - constexpr bool operator!=(const FILE& other) const { return !(*this == other); } - - // This is not defined by libc itself. The kernel defines it to point at - // the default console output mechanism. - static FILE stdout_; - - private: - Callback* write_ = nullptr; - void* ptr_ = nullptr; -}; - -#define stdout (&FILE::stdout_) - -inline int fputc(int c, FILE* f) { - const unsigned char uc = static_cast(c); - return f->Write({reinterpret_cast(&uc), 1}) == 1 ? uc : -1; -} - -inline int putc(int c, FILE* f) { return fputc(c, f); } - -inline int putchar(int c) { return fputc(c, stdout); } - -inline int fputs(const char* s, FILE* f) { - ktl::string_view str(s); - return f->Write(str) == static_cast(str.size()) ? 0 : -1; -} - -inline int puts(const char* s) { return fputs(s, stdout) == 0 ? putchar('\n') : -1; } - -#else // !__cplusplus - -// C users just need the function declarations. -typedef struct _FILE_is_opaque FILE; - -#endif // __cplusplus - -__BEGIN_CDECLS - -int printf(const char*, ...) __PRINTFLIKE(1, 2); -int fprintf(FILE*, const char*, ...) __PRINTFLIKE(2, 3); -int snprintf(char* buf, size_t len, const char*, ...) __PRINTFLIKE(3, 4); - -int vprintf(const char*, va_list); -int vfprintf(FILE*, const char*, va_list); -int vsnprintf(char* buf, size_t len, const char*, va_list); - -__END_CDECLS - -#if DISABLE_DEBUG_OUTPUT -// The declarations stand so these can be used without parens to get -// the real functions (e.g. &printf or (printf)(...)). -#define printf(...) -#define vprintf(fmt, args) -#endif - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDIO_H_ diff --git a/lib/libc/include/stdlib.h b/lib/libc/include/stdlib.h deleted file mode 100644 index 45c5a21..0000000 --- a/lib/libc/include/stdlib.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2016 The Fuchsia Authors -// Copyright (c) 2008-2014 Travis Geiselbrecht -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDLIB_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDLIB_H_ - -#include -#include -#include - -#ifdef _KERNEL -#include // lib/heap provides malloc/free definitions. -#endif - -__BEGIN_CDECLS - -long strtol(const char *nptr, char **endptr, int base); -unsigned long int strtoul(const char *nptr, char **endptr, int base); - -void abort(void) __attribute__((noreturn)); - -#define RAND_MAX (0x7fffffff) - -int rand(void); -void srand(unsigned int seed); - -// Note: POSIX.1 specifies unsigned int, but we use uintptr_t instead. -int rand_r(uintptr_t *seed); - -__END_CDECLS - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STDLIB_H_ diff --git a/lib/libc/include/string-file.h b/lib/libc/include/string-file.h deleted file mode 100644 index 1632933..0000000 --- a/lib/libc/include/string-file.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2021 The Fuchsia Authors -// Copyright (c) 2008 Travis Geiselbrecht -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STRING_FILE_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STRING_FILE_H_ - -#ifdef __cplusplus -#include - -#include -#include -#include - -// FILE* wrapper over a string. -class StringFile : public FILE { - public: - StringFile() = delete; - StringFile(const StringFile &) = delete; - StringFile operator=(const StringFile &) = delete; - - explicit StringFile(ktl::span s) : FILE(this), buffer_(s) {} - - ~StringFile() = default; - - // Adds a null character at the end of the current buffer and returns a view - // into the written section. - ktl::span take() &&; - - // Returns |str.size()| and writes as much of |str| as it would fit in the - // buffer [offest_, size - 1) while reserving the last byte for null - // character. - int Write(ktl::string_view str); - - // Returns a region representing the currently used portion of the buffer. - ktl::span used_region() const { - return (offset_ > 0) ? ktl::span{buffer_.data(), offset_} : ktl::span{}; - } - - // Returns a region representing the remaining unused space in the buffer, - // _not_ including the space reserved for the final null character. - ktl::span available_region() const { - const size_t available = ktl::max(buffer_.size() - offset_, 1) - 1; - return (available > 0) ? ktl::span{buffer_.data() + offset_, available} - : ktl::span{}; - } - - // Skip up to |amt| bytes in the buffer, advancing the write pointer as if we - // had written data, but without actually changing the buffer. - void Skip(size_t amt) { - amt = ktl::min(amt, available_region().size()); - offset_ += amt; - } - - // Provide two ways for users to convert the currently used buffer into a - // string_view. Given a string file |S|, users may say either: - // - // 1) S.as_string_view(); // or - // 2) static_cast(S); - // - // to perform the conversion. - // - ktl::string_view as_string_view() const { - return (offset_ > 0) ? ktl::string_view{buffer_.data(), offset_} : ktl::string_view{}; - } - - explicit operator ktl::string_view() const { return this->as_string_view(); } - - private: - ktl::span buffer_; - size_t offset_ = 0; -}; - -#endif // __cplusplus - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STRING_FILE_H_ diff --git a/lib/libc/include/string.h b/lib/libc/include/string.h deleted file mode 100644 index 3b55ccb..0000000 --- a/lib/libc/include/string.h +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2016 The Fuchsia Authors -// Copyright (c) 2008 Travis Geiselbrecht -// -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file or at -// https://opensource.org/licenses/MIT - -#ifndef ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STRING_H_ -#define ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STRING_H_ - -// Other libc++ headers use but libc++'s complains if it -// thinks is not its own wrapper, which we do not need. We always -// get the kernel's before the libc++ with the way the include paths -// are set up, which is necessary because of other headers where preempting -// libc++'s header is unavoidable for the kernel environment. So just we -// define the macro that checks for to keep it happy. -#define _LIBCPP_STRING_H - -#include -#include - -__BEGIN_CDECLS - -int memcmp(const void*, const void*, size_t) __PURE; -void* memcpy(void*, const void*, size_t); -void* memmove(void*, const void*, size_t); -void* memset(void*, int, size_t); - -char* strcat(char*, const char*); -int strcmp(const char*, const char*) __PURE; -char* strcpy(char*, const char*); -size_t strlen(const char*) __PURE; -char* strncat(char*, const char*, size_t); -int strncmp(const char*, const char*, size_t) __PURE; -char* strncpy(char*, const char*, size_t); -size_t strspn(const char*, const char*) __PURE; -size_t strcspn(const char* s, const char*) __PURE; -char* strtok(char*, const char*); -int strcoll(const char* s1, const char* s2) __PURE; -size_t strxfrm(char* dest, const char* src, size_t n) __PURE; - -// Not actually defined in the kernel, but expects the declaration. -char* strerror(int); - -// Nonstandard. -size_t strlcat(char*, const char*, size_t); -size_t strlcpy(char*, const char*, size_t); -int strncasecmp(const char*, const char*, size_t) __PURE; -int strnicmp(const char*, const char*, size_t) __PURE; -size_t strnlen(const char* s, size_t count) __PURE; - -// Used by address sanitizer. -__typeof(memcpy) __unsanitized_memcpy; -__typeof(memmove) __unsanitized_memmove; -__typeof(memset) __unsanitized_memset; - -__END_CDECLS - -// C++ wants overloads for const and non-const versions of some functions. -#ifdef __cplusplus - -const char* strchr(const char*, int) __asm__("strchr") __PURE; -inline char* strchr(char* s, int c) { - return const_cast(strchr(const_cast(s), c)); -} - -const char* strpbrk(const char*, const char*) __asm__("strpbrk") __PURE; -inline char* strpbrk(char* s1, const char* s2) { - return const_cast(strpbrk(const_cast(s1), s2)); -} - -const char* strrchr(const char*, int) __asm__("strrchr") __PURE; -inline char* strrchr(char* s, int c) { - return const_cast(strrchr(const_cast(s), c)); -} - -const void* memchr(const void*, int, size_t) __asm__("memchr") __PURE; -inline void* memchr(void* s, int c, size_t n) { - return const_cast(memchr(const_cast(s), c, n)); -} - -const char* strstr(const char*, const char*) __asm__("strstr") __PURE; -inline char* strstr(char* s1, const char* s2) { - return const_cast(strstr(const_cast(s1), s2)); -} - -#else - -void* memchr(const void*, int, size_t); -char* strchr(const char*, int) __PURE; -char* strpbrk(const char*, const char*) __PURE; -char* strrchr(const char*, int) __PURE; -char* strstr(const char*, const char*) __PURE; - -#endif // __cplusplus - -#endif // ZIRCON_KERNEL_LIB_LIBC_INCLUDE_STRING_H_ diff --git a/lib/printf.c b/lib/printf.c deleted file mode 100644 index 8a700ad..0000000 --- a/lib/printf.c +++ /dev/null @@ -1,914 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2019, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on -// embedded systems with a very limited resources. These routines are thread -// safe and reentrant! -// Use this instead of the bloated standard/newlib printf cause these use -// malloc for printf (and may not be thread safe). -// -/////////////////////////////////////////////////////////////////////////////// - -#include -#include - -#include "printf.h" - - -// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the -// printf_config.h header file -// default: undefined -#ifdef PRINTF_INCLUDE_CONFIG_H -#include "printf_config.h" -#endif - - -// 'ntoa' conversion buffer size, this must be big enough to hold one converted -// numeric number including padded zeros (dynamically created on stack) -// default: 32 byte -#ifndef PRINTF_NTOA_BUFFER_SIZE -#define PRINTF_NTOA_BUFFER_SIZE 32U -#endif - -// 'ftoa' conversion buffer size, this must be big enough to hold one converted -// float number including padded zeros (dynamically created on stack) -// default: 32 byte -#ifndef PRINTF_FTOA_BUFFER_SIZE -#define PRINTF_FTOA_BUFFER_SIZE 32U -#endif - -// support for the floating point type (%f) -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_FLOAT -#define PRINTF_SUPPORT_FLOAT -#endif - -// support for exponential floating point notation (%e/%g) -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL -#define PRINTF_SUPPORT_EXPONENTIAL -#endif - -// define the default floating point precision -// default: 6 digits -#ifndef PRINTF_DEFAULT_FLOAT_PRECISION -#define PRINTF_DEFAULT_FLOAT_PRECISION 6U -#endif - -// define the largest float suitable to print with %f -// default: 1e9 -#ifndef PRINTF_MAX_FLOAT -#define PRINTF_MAX_FLOAT 1e9 -#endif - -// support for the long long types (%llu or %p) -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG -#define PRINTF_SUPPORT_LONG_LONG -#endif - -// support for the ptrdiff_t type (%t) -// ptrdiff_t is normally defined in as long or long long type -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T -#define PRINTF_SUPPORT_PTRDIFF_T -#endif - -/////////////////////////////////////////////////////////////////////////////// - -// internal flag definitions -#define FLAGS_ZEROPAD (1U << 0U) -#define FLAGS_LEFT (1U << 1U) -#define FLAGS_PLUS (1U << 2U) -#define FLAGS_SPACE (1U << 3U) -#define FLAGS_HASH (1U << 4U) -#define FLAGS_UPPERCASE (1U << 5U) -#define FLAGS_CHAR (1U << 6U) -#define FLAGS_SHORT (1U << 7U) -#define FLAGS_LONG (1U << 8U) -#define FLAGS_LONG_LONG (1U << 9U) -#define FLAGS_PRECISION (1U << 10U) -#define FLAGS_ADAPT_EXP (1U << 11U) - - -// import float.h for DBL_MAX -#if defined(PRINTF_SUPPORT_FLOAT) -#include -#endif - - -// output function type -typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen); - - -// wrapper (used as buffer) for output function type -typedef struct { - void (*fct)(char character, void* arg); - void* arg; -} out_fct_wrap_type; - - -// internal buffer output -static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) -{ - if (idx < maxlen) { - ((char*)buffer)[idx] = character; - } -} - - -// internal null output -static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)character; (void)buffer; (void)idx; (void)maxlen; -} - - -// internal _putchar wrapper -static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)buffer; (void)idx; (void)maxlen; - if (character) { - _putchar(character); - } -} - - -// internal output function wrapper -static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)idx; (void)maxlen; - if (character) { - // buffer is the output fct pointer - ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg); - } -} - - -// internal secure strlen -// \return The length of the string (excluding the terminating 0) limited by 'maxsize' -static inline unsigned int _strnlen_s(const char* str, size_t maxsize) -{ - const char* s; - for (s = str; *s && maxsize--; ++s); - return (unsigned int)(s - str); -} - - -// internal test if char is a digit (0-9) -// \return true if char is a digit -static inline bool _is_digit(char ch) -{ - return (ch >= '0') && (ch <= '9'); -} - - -// internal ASCII string to unsigned int conversion -static unsigned int _atoi(const char** str) -{ - unsigned int i = 0U; - while (_is_digit(**str)) { - i = i * 10U + (unsigned int)(*((*str)++) - '0'); - } - return i; -} - - -// output the specified string in reverse, taking care of any zero-padding -static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags) -{ - const size_t start_idx = idx; - - // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - for (size_t i = len; i < width; i++) { - out(' ', buffer, idx++, maxlen); - } - } - - // reverse string - while (len) { - out(buf[--len], buffer, idx++, maxlen); - } - - // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while (idx - start_idx < width) { - out(' ', buffer, idx++, maxlen); - } - } - - return idx; -} - - -// internal itoa format -static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) -{ - // pad leading zeros - if (!(flags & FLAGS_LEFT)) { - if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - // handle hash - if (flags & FLAGS_HASH) { - if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { - len--; - if (len && (base == 16U)) { - len--; - } - } - if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = 'x'; - } - else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = 'X'; - } - else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = 'b'; - } - if (len < PRINTF_NTOA_BUFFER_SIZE) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_NTOA_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } - else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } - else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); -} - - -// internal itoa for 'long' type -static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags) -{ - char buf[PRINTF_NTOA_BUFFER_SIZE]; - size_t len = 0U; - - // no hash for 0 values - if (!value) { - flags &= ~FLAGS_HASH; - } - - // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || value) { - do { - const char digit = (char)(value % base); - buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; - value /= base; - } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); - } - - return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); -} - - -// internal itoa for 'long long' type -#if defined(PRINTF_SUPPORT_LONG_LONG) -static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) -{ - char buf[PRINTF_NTOA_BUFFER_SIZE]; - size_t len = 0U; - - // no hash for 0 values - if (!value) { - flags &= ~FLAGS_HASH; - } - - // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || value) { - do { - const char digit = (char)(value % base); - buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; - value /= base; - } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); - } - - return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); -} -#endif // PRINTF_SUPPORT_LONG_LONG - - -#if defined(PRINTF_SUPPORT_FLOAT) - -#if defined(PRINTF_SUPPORT_EXPONENTIAL) -// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT -static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags); -#endif - - -// internal ftoa for fixed decimal floating point -static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) -{ - char buf[PRINTF_FTOA_BUFFER_SIZE]; - size_t len = 0U; - double diff = 0.0; - - // powers of 10 - static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; - - // test for special values - if (value != value) - return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags); - if (value < -DBL_MAX) - return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags); - if (value > DBL_MAX) - return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags); - - // test for very large values - // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad - if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) { -#if defined(PRINTF_SUPPORT_EXPONENTIAL) - return _etoa(out, buffer, idx, maxlen, value, prec, width, flags); -#else - return 0U; -#endif - } - - // test for negative - bool negative = false; - if (value < 0) { - negative = true; - value = 0 - value; - } - - // set default precision, if not set explicitly - if (!(flags & FLAGS_PRECISION)) { - prec = PRINTF_DEFAULT_FLOAT_PRECISION; - } - // limit precision to 9, cause a prec >= 10 can lead to overflow errors - while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) { - buf[len++] = '0'; - prec--; - } - - int whole = (int)value; - double tmp = (value - whole) * pow10[prec]; - unsigned long frac = (unsigned long)tmp; - diff = tmp - frac; - - if (diff > 0.5) { - ++frac; - // handle rollover, e.g. case 0.99 with prec 1 is 1.0 - if (frac >= pow10[prec]) { - frac = 0; - ++whole; - } - } - else if (diff < 0.5) { - } - else if ((frac == 0U) || (frac & 1U)) { - // if halfway, round up if odd OR if last digit is 0 - ++frac; - } - - if (prec == 0U) { - diff = value - (double)whole; - if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) { - // exactly 0.5 and ODD, then round up - // 1.5 -> 2, but 2.5 -> 2 - ++whole; - } - } - else { - unsigned int count = prec; - // now do fractional part, as an unsigned number - while (len < PRINTF_FTOA_BUFFER_SIZE) { - --count; - buf[len++] = (char)(48U + (frac % 10U)); - if (!(frac /= 10U)) { - break; - } - } - // add extra 0s - while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) { - buf[len++] = '0'; - } - if (len < PRINTF_FTOA_BUFFER_SIZE) { - // add decimal - buf[len++] = '.'; - } - } - - // do whole part, number is reversed - while (len < PRINTF_FTOA_BUFFER_SIZE) { - buf[len++] = (char)(48 + (whole % 10)); - if (!(whole /= 10)) { - break; - } - } - - // pad leading zeros - if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { - if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_FTOA_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } - else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } - else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); -} - - -#if defined(PRINTF_SUPPORT_EXPONENTIAL) -// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse -static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) -{ - // check for NaN and special values - if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) { - return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags); - } - - // determine the sign - const bool negative = value < 0; - if (negative) { - value = -value; - } - - // default precision - if (!(flags & FLAGS_PRECISION)) { - prec = PRINTF_DEFAULT_FLOAT_PRECISION; - } - - // determine the decimal exponent - // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) - union { - uint64_t U; - double F; - } conv; - - conv.F = value; - int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2 - conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2) - // now approximate log10 from the log2 integer part and an expansion of ln around 1.5 - int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168); - // now we want to compute 10^expval but we want to be sure it won't overflow - exp2 = (int)(expval * 3.321928094887362 + 0.5); - const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453; - const double z2 = z * z; - conv.U = (uint64_t)(exp2 + 1023) << 52U; - // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex - conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); - // correct for rounding errors - if (value < conv.F) { - expval--; - conv.F /= 10; - } - - // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters - unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U; - - // in "%g" mode, "prec" is the number of *significant figures* not decimals - if (flags & FLAGS_ADAPT_EXP) { - // do we want to fall-back to "%f" mode? - if ((value >= 1e-4) && (value < 1e6)) { - if ((int)prec > expval) { - prec = (unsigned)((int)prec - expval - 1); - } - else { - prec = 0; - } - flags |= FLAGS_PRECISION; // make sure _ftoa respects precision - // no characters in exponent - minwidth = 0U; - expval = 0; - } - else { - // we use one sigfig for the whole part - if ((prec > 0) && (flags & FLAGS_PRECISION)) { - --prec; - } - } - } - - // will everything fit? - unsigned int fwidth = width; - if (width > minwidth) { - // we didn't fall-back so subtract the characters required for the exponent - fwidth -= minwidth; - } else { - // not enough characters, so go back to default sizing - fwidth = 0U; - } - if ((flags & FLAGS_LEFT) && minwidth) { - // if we're padding on the right, DON'T pad the floating part - fwidth = 0U; - } - - // rescale the float value - if (expval) { - value /= conv.F; - } - - // output the floating part - const size_t start_idx = idx; - idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP); - - // output the exponent part - if (minwidth) { - // output the exponential symbol - out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen); - // output the exponent value - idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS); - // might need to right-pad spaces - if (flags & FLAGS_LEFT) { - while (idx - start_idx < width) out(' ', buffer, idx++, maxlen); - } - } - return idx; -} -#endif // PRINTF_SUPPORT_EXPONENTIAL -#endif // PRINTF_SUPPORT_FLOAT - - -// internal vsnprintf -static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va) -{ - unsigned int flags, width, precision, n; - size_t idx = 0U; - - if (!buffer) { - // use null output function - out = _out_null; - } - - while (*format) - { - // format specifier? %[flags][width][.precision][length] - if (*format != '%') { - // no - out(*format, buffer, idx++, maxlen); - format++; - continue; - } - else { - // yes, evaluate it - format++; - } - - // evaluate flags - flags = 0U; - do { - switch (*format) { - case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; - case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; - case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; - case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; - case '#': flags |= FLAGS_HASH; format++; n = 1U; break; - default : n = 0U; break; - } - } while (n); - - // evaluate width field - width = 0U; - if (_is_digit(*format)) { - width = _atoi(&format); - } - else if (*format == '*') { - const int w = va_arg(va, int); - if (w < 0) { - flags |= FLAGS_LEFT; // reverse padding - width = (unsigned int)-w; - } - else { - width = (unsigned int)w; - } - format++; - } - - // evaluate precision field - precision = 0U; - if (*format == '.') { - flags |= FLAGS_PRECISION; - format++; - if (_is_digit(*format)) { - precision = _atoi(&format); - } - else if (*format == '*') { - const int prec = (int)va_arg(va, int); - precision = prec > 0 ? (unsigned int)prec : 0U; - format++; - } - } - - // evaluate length field - switch (*format) { - case 'l' : - flags |= FLAGS_LONG; - format++; - if (*format == 'l') { - flags |= FLAGS_LONG_LONG; - format++; - } - break; - case 'h' : - flags |= FLAGS_SHORT; - format++; - if (*format == 'h') { - flags |= FLAGS_CHAR; - format++; - } - break; -#if defined(PRINTF_SUPPORT_PTRDIFF_T) - case 't' : - flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - format++; - break; -#endif - case 'j' : - flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - format++; - break; - case 'z' : - flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - format++; - break; - default : - break; - } - - // evaluate specifier - switch (*format) { - case 'd' : - case 'i' : - case 'u' : - case 'x' : - case 'X' : - case 'o' : - case 'b' : { - // set the base - unsigned int base; - if (*format == 'x' || *format == 'X') { - base = 16U; - } - else if (*format == 'o') { - base = 8U; - } - else if (*format == 'b') { - base = 2U; - } - else { - base = 10U; - flags &= ~FLAGS_HASH; // no hash for dec format - } - // uppercase - if (*format == 'X') { - flags |= FLAGS_UPPERCASE; - } - - // no plus or space flag for u, x, X, o, b - if ((*format != 'i') && (*format != 'd')) { - flags &= ~(FLAGS_PLUS | FLAGS_SPACE); - } - - // ignore '0' flag when precision is given - if (flags & FLAGS_PRECISION) { - flags &= ~FLAGS_ZEROPAD; - } - - // convert the integer - if ((*format == 'i') || (*format == 'd')) { - // signed - if (flags & FLAGS_LONG_LONG) { -#if defined(PRINTF_SUPPORT_LONG_LONG) - const long long value = va_arg(va, long long); - idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); -#endif - } - else if (flags & FLAGS_LONG) { - const long value = va_arg(va, long); - idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); - } - else { - const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int); - idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); - } - } - else { - // unsigned - if (flags & FLAGS_LONG_LONG) { -#if defined(PRINTF_SUPPORT_LONG_LONG) - idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags); -#endif - } - else if (flags & FLAGS_LONG) { - idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags); - } - else { - const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int); - idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); - } - } - format++; - break; - } -#if defined(PRINTF_SUPPORT_FLOAT) - case 'f' : - case 'F' : - if (*format == 'F') flags |= FLAGS_UPPERCASE; - idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); - format++; - break; -#if defined(PRINTF_SUPPORT_EXPONENTIAL) - case 'e': - case 'E': - case 'g': - case 'G': - if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP; - if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE; - idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); - format++; - break; -#endif // PRINTF_SUPPORT_EXPONENTIAL -#endif // PRINTF_SUPPORT_FLOAT - case 'c' : { - unsigned int l = 1U; - // pre padding - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - // char output - out((char)va_arg(va, int), buffer, idx++, maxlen); - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - format++; - break; - } - - case 's' : { - const char* p = va_arg(va, char*); - unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1); - // pre padding - if (flags & FLAGS_PRECISION) { - l = (l < precision ? l : precision); - } - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - // string output - while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { - out(*(p++), buffer, idx++, maxlen); - } - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - format++; - break; - } - - case 'p' : { - width = sizeof(void*) * 2U; - flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; -#if defined(PRINTF_SUPPORT_LONG_LONG) - const bool is_ll = sizeof(uintptr_t) == sizeof(long long); - if (is_ll) { - idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags); - } - else { -#endif - idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags); -#if defined(PRINTF_SUPPORT_LONG_LONG) - } -#endif - format++; - break; - } - - case '%' : - out('%', buffer, idx++, maxlen); - format++; - break; - - default : - out(*format, buffer, idx++, maxlen); - format++; - break; - } - } - - // termination - out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen); - - // return written chars without terminating \0 - return (int)idx; -} - - -/////////////////////////////////////////////////////////////////////////////// - -int printf_(const char* format, ...) -{ - va_list va; - va_start(va, format); - char buffer[1]; - const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); - va_end(va); - return ret; -} - - -int sprintf_(char* buffer, const char* format, ...) -{ - va_list va; - va_start(va, format); - const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va); - va_end(va); - return ret; -} - - -int snprintf_(char* buffer, size_t count, const char* format, ...) -{ - va_list va; - va_start(va, format); - const int ret = _vsnprintf(_out_buffer, buffer, count, format, va); - va_end(va); - return ret; -} - - -int vprintf_(const char* format, va_list va) -{ - char buffer[1]; - return _vsnprintf(_out_char, buffer, (size_t)-1, format, va); -} - - -int vsnprintf_(char* buffer, size_t count, const char* format, va_list va) -{ - return _vsnprintf(_out_buffer, buffer, count, format, va); -} - - -int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...) -{ - va_list va; - va_start(va, format); - const out_fct_wrap_type out_fct_wrap = { out, arg }; - const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va); - va_end(va); - return ret; -} diff --git a/lib/printf.h b/lib/printf.h deleted file mode 100644 index 8aa23cf..0000000 --- a/lib/printf.h +++ /dev/null @@ -1,129 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2019, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on -// embedded systems with a very limited resources. -// Use this instead of bloated standard/newlib printf. -// These routines are thread safe and reentrant. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef _PRINTF_H_ -#define _PRINTF_H_ - -#include - -/* LumeOS: avoid stddef.h — define types from compiler builtins directly. - * This works in both C and C++ and matches our types.h definition. */ -#ifndef _SIZE_T_DEFINED -#define _SIZE_T_DEFINED -typedef __SIZE_TYPE__ size_t; -typedef __PTRDIFF_TYPE__ ptrdiff_t; -typedef __INTMAX_TYPE__ intmax_t; -#endif - -/* LumeOS: kernel does not need floating-point formatted output */ -#define PRINTF_DISABLE_SUPPORT_FLOAT -#define PRINTF_DISABLE_SUPPORT_EXPONENTIAL - - -#ifdef __cplusplus -extern "C" { -#endif - - -/** - * Output a character to a custom device like UART, used by the printf() function - * This function is declared here only. You have to write your custom implementation somewhere - * \param character Character to output - */ -void _putchar(char character); - - -/** - * Tiny printf implementation - * You have to implement _putchar if you use printf() - * To avoid conflicts with the regular printf() API it is overridden by macro defines - * and internal underscore-appended functions like printf_() are used - * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character - */ -#define printf printf_ -int printf_(const char* format, ...); - - -/** - * Tiny sprintf implementation - * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD! - * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output! - * \param format A string that specifies the format of the output - * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character - */ -#define sprintf sprintf_ -int sprintf_(char* buffer, const char* format, ...); - - -/** - * Tiny snprintf/vsnprintf implementation - * \param buffer A pointer to the buffer where to store the formatted string - * \param count The maximum number of characters to store in the buffer, including a terminating null character - * \param format A string that specifies the format of the output - * \param va A value identifying a variable arguments list - * \return The number of characters that COULD have been written into the buffer, not counting the terminating - * null character. A value equal or larger than count indicates truncation. Only when the returned value - * is non-negative and less than count, the string has been completely written. - */ -#define snprintf snprintf_ -#define vsnprintf vsnprintf_ -int snprintf_(char* buffer, size_t count, const char* format, ...); -int vsnprintf_(char* buffer, size_t count, const char* format, va_list va); - - -/** - * Tiny vprintf implementation - * \param format A string that specifies the format of the output - * \param va A value identifying a variable arguments list - * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character - */ -#define vprintf vprintf_ -int vprintf_(const char* format, va_list va); - - -/** - * printf with output function - * You may use this as dynamic alternative to printf() with its fixed _putchar() output - * \param out An output function which takes one character and an argument pointer - * \param arg An argument pointer for user data passed to output function - * \param format A string that specifies the format of the output - * \return The number of characters that are sent to the output function, not counting the terminating null character - */ -int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...); - - -#ifdef __cplusplus -} -#endif - - -#endif // _PRINTF_H_ diff --git a/lib/random.cc b/lib/random.cc deleted file mode 100644 index 428c75c..0000000 --- a/lib/random.cc +++ /dev/null @@ -1,152 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* Copyright (C) 2026 Richard Qin */ - -#include -#include -#include - -#include - -/* - * Early Boot PRNG (xoshiro256**) - * State size: 256 bits (4 * 64-bit words) - * Extremely fast, excellent statistical properties. - */ - -static uint64 g_state[4]; -static bool g_initialized = false; -static uint64 g_init_seed = 0; -static RandomSeedSource g_seed_source = RandomSeedSource::Unknown; - -static inline uint64 rotl(const uint64 x, int k) { - return (x << k) | (x >> (64 - k)); -} - -/* SplitMix64 used to initialize xoshiro state from a single 64-bit seed */ -static uint64 splitmix64(uint64& state) { - uint64 z = (state += 0x9e3779b97f4a7c15ULL); - z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; - z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; - return z ^ (z >> 31); -} - -void random_early_init(uint64 seed) -{ - if (g_initialized) return; - - RandomSeedSource source = RandomSeedSource::Unknown; - - /* If seed is 0 (not provided by bootloader), try hardware */ - if (seed == 0) { - uint64 hw_val; - if (arch::get_hw_random(hw_val)) { - seed = hw_val; - source = RandomSeedSource::HwRandom; - } else { - /* Fallback: use boot cycles mixed with a constant */ - seed = arch::get_cycles() ^ 0xdeadbeefbadc0ffeULL; - source = RandomSeedSource::CycleFallback; - } - } else { - source = RandomSeedSource::FdtProvided; - } - - uint64 sm_state = seed; - g_state[0] = splitmix64(sm_state); - g_state[1] = splitmix64(sm_state); - g_state[2] = splitmix64(sm_state); - g_state[3] = splitmix64(sm_state); - - g_init_seed = seed; - g_seed_source = source; - g_initialized = true; - kprintf("[random] early PRNG initialized, seed=0x%llx, source=%s\n", - seed, random_seed_source_name(source)); -} - -uint64 get_random_u64() -{ - if (!g_initialized) { - random_early_init(0); - } - - const uint64 result = rotl(g_state[1] * 5, 7) * 9; - - const uint64 t = g_state[1] << 17; - - g_state[2] ^= g_state[0]; - g_state[3] ^= g_state[1]; - g_state[1] ^= g_state[2]; - g_state[0] ^= g_state[3]; - - g_state[2] ^= t; - g_state[3] = rotl(g_state[3], 45); - - /* Mix with current cycles for slight additional unpredictability */ - return result ^ arch::get_cycles(); -} - -uint32 get_random_u32() -{ - return static_cast(get_random_u64() >> 32); -} - -void get_random_bytes(void* buf, size_t len) -{ - uint8* p = static_cast(buf); - while (len >= 8) { - uint64 val = get_random_u64(); - for (int i = 0; i < 8; i++) { - p[i] = (val >> (i * 8)) & 0xFF; - } - p += 8; - len -= 8; - } - if (len > 0) { - uint64 val = get_random_u64(); - for (size_t i = 0; i < len; i++) { - p[i] = (val >> (i * 8)) & 0xFF; - } - } -} - -void add_device_randomness(const void* buf, size_t len) -{ - /* - * For now, just mix into the xoshiro state using a simple XOR. - * In the future, this will feed the CSPRNG entropy pool. - */ - const uint8* p = static_cast(buf); - uint64 mix = arch::get_cycles(); - for (size_t i = 0; i < len; i++) { - mix = (mix << 5) ^ (mix >> 59) ^ p[i]; - } - - g_state[0] ^= mix; - g_state[1] ^= arch::get_cycles(); -} - -RandomSeedSource random_early_seed_source() -{ - return g_seed_source; -} - -uint64 random_early_seed_value() -{ - return g_init_seed; -} - -const char* random_seed_source_name(RandomSeedSource source) -{ - switch (source) { - case RandomSeedSource::FdtProvided: - return "fdt"; - case RandomSeedSource::HwRandom: - return "hw"; - case RandomSeedSource::CycleFallback: - return "cycle"; - case RandomSeedSource::Unknown: - default: - return "unknown"; - } -} diff --git a/lib/rbtree.cc b/lib/rbtree.cc deleted file mode 100644 index 6718a1a..0000000 --- a/lib/rbtree.cc +++ /dev/null @@ -1,596 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - Red Black Trees - (C) 1999 Andrea Arcangeli - (C) 2002 David Woodhouse - (C) 2012 Michel Lespinasse - - - linux/lib/rbtree.c -*/ - -#include - - -/* - * red-black trees properties: https://en.wikipedia.org/wiki/Rbtree - * - * 1) A node is either red or black - * 2) The root is black - * 3) All leaves (nullptr) are black - * 4) Both children of every red node are black - * 5) Every simple path from root to leaves contains the same number - * of black nodes. - * - * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two - * consecutive red nodes in a path and every red node is therefore followed by - * a black. So if B is the number of black nodes on every simple path (as per - * 5), then the longest possible path due to 4 is 2B. - * - * We shall indicate color with case, where black nodes are uppercase and red - * nodes will be lowercase. Unknown color nodes shall be drawn as red within - * parentheses and have some accompanying text comment. - */ - -/* - * Notes on lockless lookups: - * - * All stores to the tree structure (rb_left and rb_right) must be done using - * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the - * tree structure as seen in program order. - * - * These two requirements will allow lockless iteration of the tree -- not - * correct iteration mind you, tree rotations are not atomic so a lookup might - * miss entire subtrees. - * - * But they do guarantee that any such traversal will only see valid elements - * and that it will indeed complete -- does not get stuck in a loop. - * - * It also guarantees that if the lookup returns an element it is the 'correct' - * one. But not returning an element does _NOT_ mean it's not present. - * - * NOTE: - * - * Stores to __rb_parent_color are not important for simple lookups so those - * are left undone as of now. Nor did I check for loops involving parent - * pointers. - */ - -static inline void rb_set_black(struct rb_node *rb) -{ - rb->__rb_parent_color += RB_BLACK; -} - -static inline struct rb_node *rb_red_parent(struct rb_node *red) -{ - return (struct rb_node *)red->__rb_parent_color; -} - -/* - * Helper function for rotations: - * - old's parent and color get assigned to _new - * - old gets assigned _new as a parent and 'color' as a color. - */ -static inline void -__rb_rotate_set_parents(struct rb_node *old, struct rb_node *_new, - struct rb_root *root, int color) -{ - struct rb_node *parent = rb_parent(old); - _new->__rb_parent_color = old->__rb_parent_color; - rb_set_parent_color(old, _new, color); - __rb_change_child(old, _new, parent, root); -} - -static __always_inline void -__rb_insert(struct rb_node *node, struct rb_root *root, - void (*augment_rotate)(struct rb_node *old, struct rb_node *_new)) -{ - struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; - - while (true) { - /* - * Loop invariant: node is red. - */ - if (unlikely(!parent)) { - /* - * The inserted node is root. Either this is the - * first node, or we recursed at Case 1 below and - * are no longer violating 4). - */ - rb_set_parent_color(node, nullptr, RB_BLACK); - break; - } - - /* - * If there is a black parent, we are done. - * Otherwise, take some corrective action as, - * per 4), we don't want a red root or two - * consecutive red nodes. - */ - if(rb_is_black(parent)) - break; - - gparent = rb_red_parent(parent); - - tmp = gparent->rb_right; - if (parent != tmp) { /* parent == gparent->rb_left */ - if (tmp && rb_is_red(tmp)) { - /* - * Case 1 - node's uncle is red (color flips). - * - * G g - * / \ / \ - * p u --> P U - * / / - * n n - * - * However, since g's parent might be red, and - * 4) does not allow this, we need to recurse - * at g. - */ - rb_set_parent_color(tmp, gparent, RB_BLACK); - rb_set_parent_color(parent, gparent, RB_BLACK); - node = gparent; - parent = rb_parent(node); - rb_set_parent_color(node, parent, RB_RED); - continue; - } - - tmp = parent->rb_right; - if (node == tmp) { - /* - * Case 2 - node's uncle is black and node is - * the parent's right child (left rotate at parent). - * - * G G - * / \ / \ - * p U --> n U - * \ / - * n p - * - * This still leaves us in violation of 4), the - * continuation into Case 3 will fix that. - */ - tmp = node->rb_left; - WRITE_ONCE(parent->rb_right, tmp); - WRITE_ONCE(node->rb_left, parent); - if (tmp) - rb_set_parent_color(tmp, parent, - RB_BLACK); - rb_set_parent_color(parent, node, RB_RED); - augment_rotate(parent, node); - parent = node; - tmp = node->rb_right; - } - - /* - * Case 3 - node's uncle is black and node is - * the parent's left child (right rotate at gparent). - * - * G P - * / \ / \ - * p U --> n g - * / \ - * n U - */ - WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */ - WRITE_ONCE(parent->rb_right, gparent); - if (tmp) - rb_set_parent_color(tmp, gparent, RB_BLACK); - __rb_rotate_set_parents(gparent, parent, root, RB_RED); - augment_rotate(gparent, parent); - break; - } else { - tmp = gparent->rb_left; - if (tmp && rb_is_red(tmp)) { - /* Case 1 - color flips */ - rb_set_parent_color(tmp, gparent, RB_BLACK); - rb_set_parent_color(parent, gparent, RB_BLACK); - node = gparent; - parent = rb_parent(node); - rb_set_parent_color(node, parent, RB_RED); - continue; - } - - tmp = parent->rb_left; - if (node == tmp) { - /* Case 2 - right rotate at parent */ - tmp = node->rb_right; - WRITE_ONCE(parent->rb_left, tmp); - WRITE_ONCE(node->rb_right, parent); - if (tmp) - rb_set_parent_color(tmp, parent, - RB_BLACK); - rb_set_parent_color(parent, node, RB_RED); - augment_rotate(parent, node); - parent = node; - tmp = node->rb_left; - } - - /* Case 3 - left rotate at gparent */ - WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */ - WRITE_ONCE(parent->rb_left, gparent); - if (tmp) - rb_set_parent_color(tmp, gparent, RB_BLACK); - __rb_rotate_set_parents(gparent, parent, root, RB_RED); - augment_rotate(gparent, parent); - break; - } - } -} - -/* - * Inline version for rb_erase() use - we want to be able to inline - * and eliminate the dummy_rotate callback there - */ -static __always_inline void -____rb_erase_color(struct rb_node *parent, struct rb_root *root, - void (*augment_rotate)(struct rb_node *old, struct rb_node *_new)) -{ - struct rb_node *node = nullptr, *sibling, *tmp1, *tmp2; - - while (true) { - /* - * Loop invariants: - * - node is black (or nullptr on first iteration) - * - node is not the root (parent is not nullptr) - * - All leaf paths going through parent and node have a - * black node count that is 1 lower than other leaf paths. - */ - sibling = parent->rb_right; - if (node != sibling) { /* node == parent->rb_left */ - if (rb_is_red(sibling)) { - /* - * Case 1 - left rotate at parent - * - * P S - * / \ / \ - * N s --> p Sr - * / \ / \ - * Sl Sr N Sl - */ - tmp1 = sibling->rb_left; - WRITE_ONCE(parent->rb_right, tmp1); - WRITE_ONCE(sibling->rb_left, parent); - rb_set_parent_color(tmp1, parent, RB_BLACK); - __rb_rotate_set_parents(parent, sibling, root, - RB_RED); - augment_rotate(parent, sibling); - sibling = tmp1; - } - tmp1 = sibling->rb_right; - if (!tmp1 || rb_is_black(tmp1)) { - tmp2 = sibling->rb_left; - if (!tmp2 || rb_is_black(tmp2)) { - /* - * Case 2 - sibling color flip - * (p could be either color here) - * - * (p) (p) - * / \ / \ - * N S --> N s - * / \ / \ - * Sl Sr Sl Sr - * - * This leaves us violating 5) which - * can be fixed by flipping p to black - * if it was red, or by recursing at p. - * p is red when coming from Case 1. - */ - rb_set_parent_color(sibling, parent, - RB_RED); - if (rb_is_red(parent)) - rb_set_black(parent); - else { - node = parent; - parent = rb_parent(node); - if (parent) - continue; - } - break; - } - /* - * Case 3 - right rotate at sibling - * (p could be either color here) - * - * (p) (p) - * / \ / \ - * N S --> N sl - * / \ \ - * sl Sr S - * \ - * Sr - * - * Note: p might be red, and then both - * p and sl are red after rotation(which - * breaks property 4). This is fixed in - * Case 4 (in __rb_rotate_set_parents() - * which set sl the color of p - * and set p RB_BLACK) - * - * (p) (sl) - * / \ / \ - * N sl --> P S - * \ / \ - * S N Sr - * \ - * Sr - */ - tmp1 = tmp2->rb_right; - WRITE_ONCE(sibling->rb_left, tmp1); - WRITE_ONCE(tmp2->rb_right, sibling); - WRITE_ONCE(parent->rb_right, tmp2); - if (tmp1) - rb_set_parent_color(tmp1, sibling, - RB_BLACK); - augment_rotate(sibling, tmp2); - tmp1 = sibling; - sibling = tmp2; - } - /* - * Case 4 - left rotate at parent + color flips - * (p and sl could be either color here. - * After rotation, p becomes black, s acquires - * p's color, and sl keeps its color) - * - * (p) (s) - * / \ / \ - * N S --> P Sr - * / \ / \ - * (sl) sr N (sl) - */ - tmp2 = sibling->rb_left; - WRITE_ONCE(parent->rb_right, tmp2); - WRITE_ONCE(sibling->rb_left, parent); - rb_set_parent_color(tmp1, sibling, RB_BLACK); - if (tmp2) - rb_set_parent(tmp2, parent); - __rb_rotate_set_parents(parent, sibling, root, - RB_BLACK); - augment_rotate(parent, sibling); - break; - } else { - sibling = parent->rb_left; - if (rb_is_red(sibling)) { - /* Case 1 - right rotate at parent */ - tmp1 = sibling->rb_right; - WRITE_ONCE(parent->rb_left, tmp1); - WRITE_ONCE(sibling->rb_right, parent); - rb_set_parent_color(tmp1, parent, RB_BLACK); - __rb_rotate_set_parents(parent, sibling, root, - RB_RED); - augment_rotate(parent, sibling); - sibling = tmp1; - } - tmp1 = sibling->rb_left; - if (!tmp1 || rb_is_black(tmp1)) { - tmp2 = sibling->rb_right; - if (!tmp2 || rb_is_black(tmp2)) { - /* Case 2 - sibling color flip */ - rb_set_parent_color(sibling, parent, - RB_RED); - if (rb_is_red(parent)) - rb_set_black(parent); - else { - node = parent; - parent = rb_parent(node); - if (parent) - continue; - } - break; - } - /* Case 3 - left rotate at sibling */ - tmp1 = tmp2->rb_left; - WRITE_ONCE(sibling->rb_right, tmp1); - WRITE_ONCE(tmp2->rb_left, sibling); - WRITE_ONCE(parent->rb_left, tmp2); - if (tmp1) - rb_set_parent_color(tmp1, sibling, - RB_BLACK); - augment_rotate(sibling, tmp2); - tmp1 = sibling; - sibling = tmp2; - } - /* Case 4 - right rotate at parent + color flips */ - tmp2 = sibling->rb_right; - WRITE_ONCE(parent->rb_left, tmp2); - WRITE_ONCE(sibling->rb_right, parent); - rb_set_parent_color(tmp1, sibling, RB_BLACK); - if (tmp2) - rb_set_parent(tmp2, parent); - __rb_rotate_set_parents(parent, sibling, root, - RB_BLACK); - augment_rotate(parent, sibling); - break; - } - } -} - -/* Non-inline version for rb_erase_augmented() use */ -void __rb_erase_color(struct rb_node *parent, struct rb_root *root, - void (*augment_rotate)(struct rb_node *old, struct rb_node *_new)) -{ - ____rb_erase_color(parent, root, augment_rotate); -} - -/* - * Non-augmented rbtree manipulation functions. - * - * We use dummy augmented callbacks here, and have the compiler optimize them - * out of the rb_insert_color() and rb_erase() function definitions. - */ - -static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {} -static inline void dummy_copy(struct rb_node *old, struct rb_node *_new) {} -static inline void dummy_rotate(struct rb_node *old, struct rb_node *_new) {} - -static const struct rb_augment_callbacks dummy_callbacks = { - .propagate = dummy_propagate, - .copy = dummy_copy, - .rotate = dummy_rotate -}; - -void rb_insert_color(struct rb_node *node, struct rb_root *root) -{ - __rb_insert(node, root, dummy_rotate); -} - -void rb_erase(struct rb_node *node, struct rb_root *root) -{ - struct rb_node *rebalance; - rebalance = __rb_erase_augmented(node, root, &dummy_callbacks); - if (rebalance) - ____rb_erase_color(rebalance, root, dummy_rotate); -} - - -/* - * Augmented rbtree manipulation functions. - * - * This instantiates the same __always_inline functions as in the non-augmented - * case, but this time with user-defined callbacks. - */ - -void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, - void (*augment_rotate)(struct rb_node *old, struct rb_node *_new)) -{ - __rb_insert(node, root, augment_rotate); -} - -struct rb_node *rb_next(const struct rb_node *node) -{ - struct rb_node *parent; - - if (RB_EMPTY_NODE(node)) - return nullptr; - - /* - * If we have a right-hand child, go down and then left as far - * as we can. - */ - if (node->rb_right) { - node = node->rb_right; - while (node->rb_left) - node = node->rb_left; - return (struct rb_node *)node; - } - - /* - * No right-hand children. Everything down and left is smaller than us, - * so any 'next' node must be in the general direction of our parent. - * Go up the tree; any time the ancestor is a right-hand child of its - * parent, keep going up. First time it's a left-hand child of its - * parent, said parent is our 'next' node. - */ - while ((parent = rb_parent(node)) && node == parent->rb_right) - node = parent; - - return parent; -} - -struct rb_node *rb_prev(const struct rb_node *node) -{ - struct rb_node *parent; - - if (RB_EMPTY_NODE(node)) - return nullptr; - - /* - * If we have a left-hand child, go down and then right as far - * as we can. - */ - if (node->rb_left) { - node = node->rb_left; - while (node->rb_right) - node = node->rb_right; - return (struct rb_node *)node; - } - - /* - * No left-hand children. Go up till we find an ancestor which - * is a right-hand child of its parent. - */ - while ((parent = rb_parent(node)) && node == parent->rb_left) - node = parent; - - return parent; -} - -void rb_replace_node(struct rb_node *victim, struct rb_node *_new, - struct rb_root *root) -{ - struct rb_node *parent = rb_parent(victim); - - /* Copy the pointers/colour from the victim to the replacement */ - *_new = *victim; - - /* Set the surrounding nodes to point to the replacement */ - if (victim->rb_left) - rb_set_parent(victim->rb_left, _new); - if (victim->rb_right) - rb_set_parent(victim->rb_right, _new); - __rb_change_child(victim, _new, parent, root); -} - - -static struct rb_node *rb_left_deepest_node(const struct rb_node *node) -{ - for (;;) { - if (node->rb_left) - node = node->rb_left; - else if (node->rb_right) - node = node->rb_right; - else - return (struct rb_node *)node; - } -} - -struct rb_node *rb_next_postorder(const struct rb_node *node) -{ - const struct rb_node *parent; - if (!node) - return nullptr; - parent = rb_parent(node); - - /* If we're sitting on node, we've already seen our children */ - if (parent && node == parent->rb_left && parent->rb_right) { - /* If we are the parent's left node, go to the parent's right - * node then all the way down to the left */ - return rb_left_deepest_node(parent->rb_right); - } else - /* Otherwise we are the parent's right node, and the parent - * should be next */ - return (struct rb_node *)parent; -} - -struct rb_node *rb_first_postorder(const struct rb_root *root) -{ - if (!root->rb_node) - return nullptr; - - return rb_left_deepest_node(root->rb_node); -} - -struct rb_node *rb_first(const struct rb_root *root) -{ - struct rb_node *n; - - n = root->rb_node; - if (!n) - return nullptr; - while (n->rb_left) - n = n->rb_left; - return n; -} - -struct rb_node *rb_last(const struct rb_root *root) -{ - struct rb_node *n; - - n = root->rb_node; - if (!n) - return nullptr; - while (n->rb_right) - n = n->rb_right; - return n; -} diff --git a/lib/string/COPYRIGHT b/lib/string/COPYRIGHT deleted file mode 100644 index 2f15edc..0000000 --- a/lib/string/COPYRIGHT +++ /dev/null @@ -1,193 +0,0 @@ -musl as a whole is licensed under the following standard MIT license: - ----------------------------------------------------------------------- -Copyright © 2005-2020 Rich Felker, et al. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ----------------------------------------------------------------------- - -Authors/contributors include: - -A. Wilcox -Ada Worcester -Alex Dowad -Alex Suykov -Alexander Monakov -Andre McCurdy -Andrew Kelley -Anthony G. Basile -Aric Belsito -Arvid Picciani -Bartosz Brachaczek -Benjamin Peterson -Bobby Bingham -Boris Brezillon -Brent Cook -Chris Spiegel -Clément Vasseur -Daniel Micay -Daniel Sabogal -Daurnimator -David Carlier -David Edelsohn -Denys Vlasenko -Dmitry Ivanov -Dmitry V. Levin -Drew DeVault -Emil Renner Berthing -Fangrui Song -Felix Fietkau -Felix Janda -Gianluca Anzolin -Hauke Mehrtens -He X -Hiltjo Posthuma -Isaac Dunham -Jaydeep Patil -Jens Gustedt -Jeremy Huntwork -Jo-Philipp Wich -Joakim Sindholt -John Spencer -Julien Ramseier -Justin Cormack -Kaarle Ritvanen -Khem Raj -Kylie McClain -Leah Neukirchen -Luca Barbato -Luka Perkov -Lynn Ochs -M Farkas-Dyck (Strake) -Mahesh Bodapati -Markus Wichmann -Masanori Ogino -Michael Clark -Michael Forney -Mikhail Kremnyov -Natanael Copa -Nicholas J. Kain -orc -Pascal Cuoq -Patrick Oppenlander -Petr Hosek -Petr Skocik -Pierre Carrier -Reini Urban -Rich Felker -Richard Pennington -Ryan Fairfax -Samuel Holland -Segev Finer -Shiz -sin -Solar Designer -Stefan Kristiansson -Stefan O'Rear -Szabolcs Nagy -Timo Teräs -Trutz Behn -Will Dietz -William Haddon -William Pitcock - -Portions of this software are derived from third-party works licensed -under terms compatible with the above MIT license: - -The TRE regular expression implementation (src/regex/reg* and -src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed -under a 2-clause BSD license (license text in the source files). The -included version has been heavily modified by Rich Felker in 2012, in -the interests of size, simplicity, and namespace cleanliness. - -Much of the math library code (src/math/* and src/complex/*) is -Copyright © 1993,2004 Sun Microsystems or -Copyright © 2003-2011 David Schultz or -Copyright © 2003-2009 Steven G. Kargl or -Copyright © 2003-2009 Bruce D. Evans or -Copyright © 2008 Stephen L. Moshier or -Copyright © 2017-2018 Arm Limited -and labelled as such in comments in the individual source files. All -have been licensed under extremely permissive terms. - -The ARM memcpy code (src/string/arm/memcpy.S) is Copyright © 2008 -The Android Open Source Project and is licensed under a two-clause BSD -license. It was taken from Bionic libc, used on Android. - -The AArch64 memcpy and memset code (src/string/aarch64/*) are -Copyright © 1999-2019, Arm Limited. - -The implementation of DES for crypt (src/crypt/crypt_des.c) is -Copyright © 1994 David Burren. It is licensed under a BSD license. - -The implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was -originally written by Solar Designer and placed into the public -domain. The code also comes with a fallback permissive license for use -in jurisdictions that may not recognize the public domain. - -The smoothsort implementation (src/stdlib/qsort.c) is Copyright © 2011 -Lynn Ochs and is licensed under an MIT-style license. - -The x86_64 port was written by Nicholas J. Kain and is licensed under -the standard MIT terms. - -The mips and microblaze ports were originally written by Richard -Pennington for use in the ellcc project. The original code was adapted -by Rich Felker for build system and code conventions during upstream -integration. It is licensed under the standard MIT terms. - -The mips64 port was contributed by Imagination Technologies and is -licensed under the standard MIT terms. - -The powerpc port was also originally written by Richard Pennington, -and later supplemented and integrated by John Spencer. It is licensed -under the standard MIT terms. - -All other files which have no copyright comments are original works -produced specifically for use as part of this library, written either -by Rich Felker, the main author of the library, or by one or more -contibutors listed above. Details on authorship of individual files -can be found in the git version control history of the project. The -omission of copyright and license comments in each file is in the -interest of source tree size. - -In addition, permission is hereby granted for all public header files -(include/* and arch/*/bits/*) and crt files intended to be linked into -applications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit -the copyright notice and permission notice otherwise required by the -license, and to use these files without any requirement of -attribution. These files include substantial contributions from: - -Bobby Bingham -John Spencer -Nicholas J. Kain -Rich Felker -Richard Pennington -Stefan Kristiansson -Szabolcs Nagy - -all of whom have explicitly granted such permission. - -This file previously contained text expressing a belief that most of -the files covered by the above exception were sufficiently trivial not -to be subject to copyright, resulting in confusion over whether it -negated the permissions granted in the license. In the spirit of -permissive licensing, and of not having licensing issues being an -obstacle to adoption, that text has been removed. diff --git a/lib/string/aarch64/memcpy.S b/lib/string/aarch64/memcpy.S deleted file mode 100644 index 48bb8a8..0000000 --- a/lib/string/aarch64/memcpy.S +++ /dev/null @@ -1,186 +0,0 @@ -/* - * memcpy - copy memory area - * - * Copyright (c) 2012-2020, Arm Limited. - * SPDX-License-Identifier: MIT - */ - -/* Assumptions: - * - * ARMv8-a, AArch64, unaligned accesses. - * - */ - -#define dstin x0 -#define src x1 -#define count x2 -#define dst x3 -#define srcend x4 -#define dstend x5 -#define A_l x6 -#define A_lw w6 -#define A_h x7 -#define B_l x8 -#define B_lw w8 -#define B_h x9 -#define C_l x10 -#define C_lw w10 -#define C_h x11 -#define D_l x12 -#define D_h x13 -#define E_l x14 -#define E_h x15 -#define F_l x16 -#define F_h x17 -#define G_l count -#define G_h dst -#define H_l src -#define H_h srcend -#define tmp1 x14 - -/* This implementation of memcpy uses unaligned accesses and branchless - sequences to keep the code small, simple and improve performance. - - Copies are split into 3 main cases: small copies of up to 32 bytes, medium - copies of up to 128 bytes, and large copies. The overhead of the overlap - check is negligible since it is only required for large copies. - - Large copies use a software pipelined loop processing 64 bytes per iteration. - The destination pointer is 16-byte aligned to minimize unaligned accesses. - The loop tail is handled by always copying 64 bytes from the end. -*/ - -.global memcpy -.type memcpy,%function -memcpy: - add srcend, src, count - add dstend, dstin, count - cmp count, 128 - b.hi .Lcopy_long - cmp count, 32 - b.hi .Lcopy32_128 - - /* Small copies: 0..32 bytes. */ - cmp count, 16 - b.lo .Lcopy16 - ldp A_l, A_h, [src] - ldp D_l, D_h, [srcend, -16] - stp A_l, A_h, [dstin] - stp D_l, D_h, [dstend, -16] - ret - - /* Copy 8-15 bytes. */ -.Lcopy16: - tbz count, 3, .Lcopy8 - ldr A_l, [src] - ldr A_h, [srcend, -8] - str A_l, [dstin] - str A_h, [dstend, -8] - ret - - .p2align 3 - /* Copy 4-7 bytes. */ -.Lcopy8: - tbz count, 2, .Lcopy4 - ldr A_lw, [src] - ldr B_lw, [srcend, -4] - str A_lw, [dstin] - str B_lw, [dstend, -4] - ret - - /* Copy 0..3 bytes using a branchless sequence. */ -.Lcopy4: - cbz count, .Lcopy0 - lsr tmp1, count, 1 - ldrb A_lw, [src] - ldrb C_lw, [srcend, -1] - ldrb B_lw, [src, tmp1] - strb A_lw, [dstin] - strb B_lw, [dstin, tmp1] - strb C_lw, [dstend, -1] -.Lcopy0: - ret - - .p2align 4 - /* Medium copies: 33..128 bytes. */ -.Lcopy32_128: - ldp A_l, A_h, [src] - ldp B_l, B_h, [src, 16] - ldp C_l, C_h, [srcend, -32] - ldp D_l, D_h, [srcend, -16] - cmp count, 64 - b.hi .Lcopy128 - stp A_l, A_h, [dstin] - stp B_l, B_h, [dstin, 16] - stp C_l, C_h, [dstend, -32] - stp D_l, D_h, [dstend, -16] - ret - - .p2align 4 - /* Copy 65..128 bytes. */ -.Lcopy128: - ldp E_l, E_h, [src, 32] - ldp F_l, F_h, [src, 48] - cmp count, 96 - b.ls .Lcopy96 - ldp G_l, G_h, [srcend, -64] - ldp H_l, H_h, [srcend, -48] - stp G_l, G_h, [dstend, -64] - stp H_l, H_h, [dstend, -48] -.Lcopy96: - stp A_l, A_h, [dstin] - stp B_l, B_h, [dstin, 16] - stp E_l, E_h, [dstin, 32] - stp F_l, F_h, [dstin, 48] - stp C_l, C_h, [dstend, -32] - stp D_l, D_h, [dstend, -16] - ret - - .p2align 4 - /* Copy more than 128 bytes. */ -.Lcopy_long: - - /* Copy 16 bytes and then align dst to 16-byte alignment. */ - - ldp D_l, D_h, [src] - and tmp1, dstin, 15 - bic dst, dstin, 15 - sub src, src, tmp1 - add count, count, tmp1 /* Count is now 16 too large. */ - ldp A_l, A_h, [src, 16] - stp D_l, D_h, [dstin] - ldp B_l, B_h, [src, 32] - ldp C_l, C_h, [src, 48] - ldp D_l, D_h, [src, 64]! - subs count, count, 128 + 16 /* Test and readjust count. */ - b.ls .Lcopy64_from_end - -.Lloop64: - stp A_l, A_h, [dst, 16] - ldp A_l, A_h, [src, 16] - stp B_l, B_h, [dst, 32] - ldp B_l, B_h, [src, 32] - stp C_l, C_h, [dst, 48] - ldp C_l, C_h, [src, 48] - stp D_l, D_h, [dst, 64]! - ldp D_l, D_h, [src, 64]! - subs count, count, 64 - b.hi .Lloop64 - - /* Write the last iteration and copy 64 bytes from the end. */ -.Lcopy64_from_end: - ldp E_l, E_h, [srcend, -64] - stp A_l, A_h, [dst, 16] - ldp A_l, A_h, [srcend, -48] - stp B_l, B_h, [dst, 32] - ldp B_l, B_h, [srcend, -32] - stp C_l, C_h, [dst, 48] - ldp C_l, C_h, [srcend, -16] - stp D_l, D_h, [dst, 64] - stp E_l, E_h, [dstend, -64] - stp A_l, A_h, [dstend, -48] - stp B_l, B_h, [dstend, -32] - stp C_l, C_h, [dstend, -16] - ret - -.size memcpy,.-memcpy diff --git a/lib/string/aarch64/memset.S b/lib/string/aarch64/memset.S deleted file mode 100644 index f0d29b7..0000000 --- a/lib/string/aarch64/memset.S +++ /dev/null @@ -1,115 +0,0 @@ -/* - * memset - fill memory with a constant byte - * - * Copyright (c) 2012-2020, Arm Limited. - * SPDX-License-Identifier: MIT - */ - -/* Assumptions: - * - * ARMv8-a, AArch64, Advanced SIMD, unaligned accesses. - * - */ - -#define dstin x0 -#define val x1 -#define valw w1 -#define count x2 -#define dst x3 -#define dstend x4 -#define zva_val x5 - -.global memset -.type memset,%function -memset: - - dup v0.16B, valw - add dstend, dstin, count - - cmp count, 96 - b.hi .Lset_long - cmp count, 16 - b.hs .Lset_medium - mov val, v0.D[0] - - /* Set 0..15 bytes. */ - tbz count, 3, 1f - str val, [dstin] - str val, [dstend, -8] - ret - nop -1: tbz count, 2, 2f - str valw, [dstin] - str valw, [dstend, -4] - ret -2: cbz count, 3f - strb valw, [dstin] - tbz count, 1, 3f - strh valw, [dstend, -2] -3: ret - - /* Set 17..96 bytes. */ -.Lset_medium: - str q0, [dstin] - tbnz count, 6, .Lset96 - str q0, [dstend, -16] - tbz count, 5, 1f - str q0, [dstin, 16] - str q0, [dstend, -32] -1: ret - - .p2align 4 - /* Set 64..96 bytes. Write 64 bytes from the start and - 32 bytes from the end. */ -.Lset96: - str q0, [dstin, 16] - stp q0, q0, [dstin, 32] - stp q0, q0, [dstend, -32] - ret - - .p2align 4 -.Lset_long: - and valw, valw, 255 - bic dst, dstin, 15 - str q0, [dstin] - cmp count, 160 - ccmp valw, 0, 0, hs - b.ne .Lno_zva - -#ifndef SKIP_ZVA_CHECK - mrs zva_val, dczid_el0 - and zva_val, zva_val, 31 - cmp zva_val, 4 /* ZVA size is 64 bytes. */ - b.ne .Lno_zva -#endif - str q0, [dst, 16] - stp q0, q0, [dst, 32] - bic dst, dst, 63 - sub count, dstend, dst /* Count is now 64 too large. */ - sub count, count, 128 /* Adjust count and bias for loop. */ - - .p2align 4 -.Lzva_loop: - add dst, dst, 64 - dc zva, dst - subs count, count, 64 - b.hi .Lzva_loop - stp q0, q0, [dstend, -64] - stp q0, q0, [dstend, -32] - ret - -.Lno_zva: - sub count, dstend, dst /* Count is 16 too large. */ - sub dst, dst, 16 /* Dst is biased by -32. */ - sub count, count, 64 + 16 /* Adjust count and bias for loop. */ -.Lno_zva_loop: - stp q0, q0, [dst, 32] - stp q0, q0, [dst, 64]! - subs count, count, 64 - b.hi .Lno_zva_loop - stp q0, q0, [dstend, -64] - stp q0, q0, [dstend, -32] - ret - -.size memset,.-memset - diff --git a/lib/string/arm/__aeabi_memcpy.s b/lib/string/arm/__aeabi_memcpy.s deleted file mode 100644 index 3a527e4..0000000 --- a/lib/string/arm/__aeabi_memcpy.s +++ /dev/null @@ -1,45 +0,0 @@ -.syntax unified - -.global __aeabi_memcpy8 -.global __aeabi_memcpy4 -.global __aeabi_memcpy -.global __aeabi_memmove8 -.global __aeabi_memmove4 -.global __aeabi_memmove - -.type __aeabi_memcpy8,%function -.type __aeabi_memcpy4,%function -.type __aeabi_memcpy,%function -.type __aeabi_memmove8,%function -.type __aeabi_memmove4,%function -.type __aeabi_memmove,%function - -__aeabi_memmove8: -__aeabi_memmove4: -__aeabi_memmove: - cmp r0, r1 - bls 3f - cmp r2, #0 - beq 2f - adds r0, r0, r2 - adds r2, r1, r2 -1: subs r2, r2, #1 - ldrb r3, [r2] - subs r0, r0, #1 - strb r3, [r0] - cmp r1, r2 - bne 1b -2: bx lr -__aeabi_memcpy8: -__aeabi_memcpy4: -__aeabi_memcpy: -3: cmp r2, #0 - beq 2f - adds r2, r1, r2 -1: ldrb r3, [r1] - adds r1, r1, #1 - strb r3, [r0] - adds r0, r0, #1 - cmp r1, r2 - bne 1b -2: bx lr diff --git a/lib/string/arm/__aeabi_memset.s b/lib/string/arm/__aeabi_memset.s deleted file mode 100644 index f9f6058..0000000 --- a/lib/string/arm/__aeabi_memset.s +++ /dev/null @@ -1,31 +0,0 @@ -.syntax unified - -.global __aeabi_memclr8 -.global __aeabi_memclr4 -.global __aeabi_memclr -.global __aeabi_memset8 -.global __aeabi_memset4 -.global __aeabi_memset - -.type __aeabi_memclr8,%function -.type __aeabi_memclr4,%function -.type __aeabi_memclr,%function -.type __aeabi_memset8,%function -.type __aeabi_memset4,%function -.type __aeabi_memset,%function - -__aeabi_memclr8: -__aeabi_memclr4: -__aeabi_memclr: - movs r2, #0 -__aeabi_memset8: -__aeabi_memset4: -__aeabi_memset: - cmp r1, #0 - beq 2f - adds r1, r0, r1 -1: strb r2, [r0] - adds r0, r0, #1 - cmp r1, r0 - bne 1b -2: bx lr diff --git a/lib/string/arm/memcpy.S b/lib/string/arm/memcpy.S deleted file mode 100644 index 869e344..0000000 --- a/lib/string/arm/memcpy.S +++ /dev/null @@ -1,479 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - - -/* - * Optimized memcpy() for ARM. - * - * note that memcpy() always returns the destination pointer, - * so we have to preserve R0. - */ - -/* - * This file has been modified from the original for use in musl libc. - * The main changes are: addition of .type memcpy,%function to make the - * code safely callable from thumb mode, adjusting the return - * instructions to be compatible with pre-thumb ARM cpus, removal of - * prefetch code that is not compatible with older cpus and support for - * building as thumb 2 and big-endian. - */ - -.syntax unified - -.global memcpy -.type memcpy,%function -memcpy: - /* The stack must always be 64-bits aligned to be compliant with the - * ARM ABI. Since we have to save R0, we might as well save R4 - * which we can use for better pipelining of the reads below - */ - .fnstart - .save {r0, r4, lr} - stmfd sp!, {r0, r4, lr} - /* Making room for r5-r11 which will be spilled later */ - .pad #28 - sub sp, sp, #28 - - /* it simplifies things to take care of len<4 early */ - cmp r2, #4 - blo copy_last_3_and_return - - /* compute the offset to align the source - * offset = (4-(src&3))&3 = -src & 3 - */ - rsb r3, r1, #0 - ands r3, r3, #3 - beq src_aligned - - /* align source to 32 bits. We need to insert 2 instructions between - * a ldr[b|h] and str[b|h] because byte and half-word instructions - * stall 2 cycles. - */ - movs r12, r3, lsl #31 - sub r2, r2, r3 /* we know that r3 <= r2 because r2 >= 4 */ - ldrbmi r3, [r1], #1 - ldrbcs r4, [r1], #1 - ldrbcs r12,[r1], #1 - strbmi r3, [r0], #1 - strbcs r4, [r0], #1 - strbcs r12,[r0], #1 - -src_aligned: - - /* see if src and dst are aligned together (congruent) */ - eor r12, r0, r1 - tst r12, #3 - bne non_congruent - - /* Use post-incriment mode for stm to spill r5-r11 to reserved stack - * frame. Don't update sp. - */ - stmea sp, {r5-r11} - - /* align the destination to a cache-line */ - rsb r3, r0, #0 - ands r3, r3, #0x1C - beq congruent_aligned32 - cmp r3, r2 - andhi r3, r2, #0x1C - - /* conditionnaly copies 0 to 7 words (length in r3) */ - movs r12, r3, lsl #28 - ldmcs r1!, {r4, r5, r6, r7} /* 16 bytes */ - ldmmi r1!, {r8, r9} /* 8 bytes */ - stmcs r0!, {r4, r5, r6, r7} - stmmi r0!, {r8, r9} - tst r3, #0x4 - ldrne r10,[r1], #4 /* 4 bytes */ - strne r10,[r0], #4 - sub r2, r2, r3 - -congruent_aligned32: - /* - * here source is aligned to 32 bytes. - */ - -cached_aligned32: - subs r2, r2, #32 - blo less_than_32_left - - /* - * We preload a cache-line up to 64 bytes ahead. On the 926, this will - * stall only until the requested world is fetched, but the linefill - * continues in the the background. - * While the linefill is going, we write our previous cache-line - * into the write-buffer (which should have some free space). - * When the linefill is done, the writebuffer will - * start dumping its content into memory - * - * While all this is going, we then load a full cache line into - * 8 registers, this cache line should be in the cache by now - * (or partly in the cache). - * - * This code should work well regardless of the source/dest alignment. - * - */ - - /* Align the preload register to a cache-line because the cpu does - * "critical word first" (the first word requested is loaded first). - */ - @ bic r12, r1, #0x1F - @ add r12, r12, #64 - -1: ldmia r1!, { r4-r11 } - subs r2, r2, #32 - - /* - * NOTE: if r12 is more than 64 ahead of r1, the following ldrhi - * for ARM9 preload will not be safely guarded by the preceding subs. - * When it is safely guarded the only possibility to have SIGSEGV here - * is because the caller overstates the length. - */ - @ ldrhi r3, [r12], #32 /* cheap ARM9 preload */ - stmia r0!, { r4-r11 } - bhs 1b - - add r2, r2, #32 - -less_than_32_left: - /* - * less than 32 bytes left at this point (length in r2) - */ - - /* skip all this if there is nothing to do, which should - * be a common case (if not executed the code below takes - * about 16 cycles) - */ - tst r2, #0x1F - beq 1f - - /* conditionnaly copies 0 to 31 bytes */ - movs r12, r2, lsl #28 - ldmcs r1!, {r4, r5, r6, r7} /* 16 bytes */ - ldmmi r1!, {r8, r9} /* 8 bytes */ - stmcs r0!, {r4, r5, r6, r7} - stmmi r0!, {r8, r9} - movs r12, r2, lsl #30 - ldrcs r3, [r1], #4 /* 4 bytes */ - ldrhmi r4, [r1], #2 /* 2 bytes */ - strcs r3, [r0], #4 - strhmi r4, [r0], #2 - tst r2, #0x1 - ldrbne r3, [r1] /* last byte */ - strbne r3, [r0] - - /* we're done! restore everything and return */ -1: ldmfd sp!, {r5-r11} - ldmfd sp!, {r0, r4, lr} - bx lr - - /********************************************************************/ - -non_congruent: - /* - * here source is aligned to 4 bytes - * but destination is not. - * - * in the code below r2 is the number of bytes read - * (the number of bytes written is always smaller, because we have - * partial words in the shift queue) - */ - cmp r2, #4 - blo copy_last_3_and_return - - /* Use post-incriment mode for stm to spill r5-r11 to reserved stack - * frame. Don't update sp. - */ - stmea sp, {r5-r11} - - /* compute shifts needed to align src to dest */ - rsb r5, r0, #0 - and r5, r5, #3 /* r5 = # bytes in partial words */ - mov r12, r5, lsl #3 /* r12 = right */ - rsb lr, r12, #32 /* lr = left */ - - /* read the first word */ - ldr r3, [r1], #4 - sub r2, r2, #4 - - /* write a partial word (0 to 3 bytes), such that destination - * becomes aligned to 32 bits (r5 = nb of words to copy for alignment) - */ - movs r5, r5, lsl #31 - -#if __ARMEB__ - movmi r3, r3, ror #24 - strbmi r3, [r0], #1 - movcs r3, r3, ror #24 - strbcs r3, [r0], #1 - movcs r3, r3, ror #24 - strbcs r3, [r0], #1 -#else - strbmi r3, [r0], #1 - movmi r3, r3, lsr #8 - strbcs r3, [r0], #1 - movcs r3, r3, lsr #8 - strbcs r3, [r0], #1 - movcs r3, r3, lsr #8 -#endif - - cmp r2, #4 - blo partial_word_tail - -#if __ARMEB__ - mov r3, r3, lsr r12 - mov r3, r3, lsl r12 -#endif - - /* Align destination to 32 bytes (cache line boundary) */ -1: tst r0, #0x1c - beq 2f - ldr r5, [r1], #4 - sub r2, r2, #4 -#if __ARMEB__ - mov r4, r5, lsr lr - orr r4, r4, r3 - mov r3, r5, lsl r12 -#else - mov r4, r5, lsl lr - orr r4, r4, r3 - mov r3, r5, lsr r12 -#endif - str r4, [r0], #4 - cmp r2, #4 - bhs 1b - blo partial_word_tail - - /* copy 32 bytes at a time */ -2: subs r2, r2, #32 - blo less_than_thirtytwo - - /* Use immediate mode for the shifts, because there is an extra cycle - * for register shifts, which could account for up to 50% of - * performance hit. - */ - - cmp r12, #24 - beq loop24 - cmp r12, #8 - beq loop8 - -loop16: - ldr r12, [r1], #4 -1: mov r4, r12 - ldmia r1!, { r5,r6,r7, r8,r9,r10,r11} - subs r2, r2, #32 - ldrhs r12, [r1], #4 -#if __ARMEB__ - orr r3, r3, r4, lsr #16 - mov r4, r4, lsl #16 - orr r4, r4, r5, lsr #16 - mov r5, r5, lsl #16 - orr r5, r5, r6, lsr #16 - mov r6, r6, lsl #16 - orr r6, r6, r7, lsr #16 - mov r7, r7, lsl #16 - orr r7, r7, r8, lsr #16 - mov r8, r8, lsl #16 - orr r8, r8, r9, lsr #16 - mov r9, r9, lsl #16 - orr r9, r9, r10, lsr #16 - mov r10, r10, lsl #16 - orr r10, r10, r11, lsr #16 - stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10} - mov r3, r11, lsl #16 -#else - orr r3, r3, r4, lsl #16 - mov r4, r4, lsr #16 - orr r4, r4, r5, lsl #16 - mov r5, r5, lsr #16 - orr r5, r5, r6, lsl #16 - mov r6, r6, lsr #16 - orr r6, r6, r7, lsl #16 - mov r7, r7, lsr #16 - orr r7, r7, r8, lsl #16 - mov r8, r8, lsr #16 - orr r8, r8, r9, lsl #16 - mov r9, r9, lsr #16 - orr r9, r9, r10, lsl #16 - mov r10, r10, lsr #16 - orr r10, r10, r11, lsl #16 - stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10} - mov r3, r11, lsr #16 -#endif - bhs 1b - b less_than_thirtytwo - -loop8: - ldr r12, [r1], #4 -1: mov r4, r12 - ldmia r1!, { r5,r6,r7, r8,r9,r10,r11} - subs r2, r2, #32 - ldrhs r12, [r1], #4 -#if __ARMEB__ - orr r3, r3, r4, lsr #24 - mov r4, r4, lsl #8 - orr r4, r4, r5, lsr #24 - mov r5, r5, lsl #8 - orr r5, r5, r6, lsr #24 - mov r6, r6, lsl #8 - orr r6, r6, r7, lsr #24 - mov r7, r7, lsl #8 - orr r7, r7, r8, lsr #24 - mov r8, r8, lsl #8 - orr r8, r8, r9, lsr #24 - mov r9, r9, lsl #8 - orr r9, r9, r10, lsr #24 - mov r10, r10, lsl #8 - orr r10, r10, r11, lsr #24 - stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10} - mov r3, r11, lsl #8 -#else - orr r3, r3, r4, lsl #24 - mov r4, r4, lsr #8 - orr r4, r4, r5, lsl #24 - mov r5, r5, lsr #8 - orr r5, r5, r6, lsl #24 - mov r6, r6, lsr #8 - orr r6, r6, r7, lsl #24 - mov r7, r7, lsr #8 - orr r7, r7, r8, lsl #24 - mov r8, r8, lsr #8 - orr r8, r8, r9, lsl #24 - mov r9, r9, lsr #8 - orr r9, r9, r10, lsl #24 - mov r10, r10, lsr #8 - orr r10, r10, r11, lsl #24 - stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10} - mov r3, r11, lsr #8 -#endif - bhs 1b - b less_than_thirtytwo - -loop24: - ldr r12, [r1], #4 -1: mov r4, r12 - ldmia r1!, { r5,r6,r7, r8,r9,r10,r11} - subs r2, r2, #32 - ldrhs r12, [r1], #4 -#if __ARMEB__ - orr r3, r3, r4, lsr #8 - mov r4, r4, lsl #24 - orr r4, r4, r5, lsr #8 - mov r5, r5, lsl #24 - orr r5, r5, r6, lsr #8 - mov r6, r6, lsl #24 - orr r6, r6, r7, lsr #8 - mov r7, r7, lsl #24 - orr r7, r7, r8, lsr #8 - mov r8, r8, lsl #24 - orr r8, r8, r9, lsr #8 - mov r9, r9, lsl #24 - orr r9, r9, r10, lsr #8 - mov r10, r10, lsl #24 - orr r10, r10, r11, lsr #8 - stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10} - mov r3, r11, lsl #24 -#else - orr r3, r3, r4, lsl #8 - mov r4, r4, lsr #24 - orr r4, r4, r5, lsl #8 - mov r5, r5, lsr #24 - orr r5, r5, r6, lsl #8 - mov r6, r6, lsr #24 - orr r6, r6, r7, lsl #8 - mov r7, r7, lsr #24 - orr r7, r7, r8, lsl #8 - mov r8, r8, lsr #24 - orr r8, r8, r9, lsl #8 - mov r9, r9, lsr #24 - orr r9, r9, r10, lsl #8 - mov r10, r10, lsr #24 - orr r10, r10, r11, lsl #8 - stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10} - mov r3, r11, lsr #24 -#endif - bhs 1b - -less_than_thirtytwo: - /* copy the last 0 to 31 bytes of the source */ - rsb r12, lr, #32 /* we corrupted r12, recompute it */ - add r2, r2, #32 - cmp r2, #4 - blo partial_word_tail - -1: ldr r5, [r1], #4 - sub r2, r2, #4 -#if __ARMEB__ - mov r4, r5, lsr lr - orr r4, r4, r3 - mov r3, r5, lsl r12 -#else - mov r4, r5, lsl lr - orr r4, r4, r3 - mov r3, r5, lsr r12 -#endif - str r4, [r0], #4 - cmp r2, #4 - bhs 1b - -partial_word_tail: - /* we have a partial word in the input buffer */ - movs r5, lr, lsl #(31-3) -#if __ARMEB__ - movmi r3, r3, ror #24 - strbmi r3, [r0], #1 - movcs r3, r3, ror #24 - strbcs r3, [r0], #1 - movcs r3, r3, ror #24 - strbcs r3, [r0], #1 -#else - strbmi r3, [r0], #1 - movmi r3, r3, lsr #8 - strbcs r3, [r0], #1 - movcs r3, r3, lsr #8 - strbcs r3, [r0], #1 -#endif - - /* Refill spilled registers from the stack. Don't update sp. */ - ldmfd sp, {r5-r11} - -copy_last_3_and_return: - movs r2, r2, lsl #31 /* copy remaining 0, 1, 2 or 3 bytes */ - ldrbmi r2, [r1], #1 - ldrbcs r3, [r1], #1 - ldrbcs r12,[r1] - strbmi r2, [r0], #1 - strbcs r3, [r0], #1 - strbcs r12,[r0] - - /* we're done! restore sp and spilled registers and return */ - add sp, sp, #28 - ldmfd sp!, {r0, r4, lr} - bx lr - diff --git a/lib/string/bcmp.c b/lib/string/bcmp.c deleted file mode 100644 index d4c093f..0000000 --- a/lib/string/bcmp.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include -#include - -int bcmp(const void *s1, const void *s2, size_t n) -{ - return memcmp(s1, s2, n); -} diff --git a/lib/string/bcopy.c b/lib/string/bcopy.c deleted file mode 100644 index 25e98a2..0000000 --- a/lib/string/bcopy.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include -#include - -void bcopy(const void *s1, void *s2, size_t n) -{ - memmove(s2, s1, n); -} diff --git a/lib/string/bzero.c b/lib/string/bzero.c deleted file mode 100644 index 08f1398..0000000 --- a/lib/string/bzero.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include -#include - -void bzero(void *s, size_t n) -{ - memset(s, 0, n); -} diff --git a/lib/string/explicit_bzero.c b/lib/string/explicit_bzero.c deleted file mode 100644 index c94f25f..0000000 --- a/lib/string/explicit_bzero.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include - -void explicit_bzero(void *d, size_t n) -{ - d = memset(d, 0, n); - __asm__ __volatile__ ("" : : "r"(d) : "memory"); -} diff --git a/lib/string/i386/memcpy.s b/lib/string/i386/memcpy.s deleted file mode 100644 index 0608dd8..0000000 --- a/lib/string/i386/memcpy.s +++ /dev/null @@ -1,32 +0,0 @@ -.global memcpy -.global __memcpy_fwd -.hidden __memcpy_fwd -.type memcpy,@function -memcpy: -__memcpy_fwd: - push %esi - push %edi - mov 12(%esp),%edi - mov 16(%esp),%esi - mov 20(%esp),%ecx - mov %edi,%eax - cmp $4,%ecx - jc 1f - test $3,%edi - jz 1f -2: movsb - dec %ecx - test $3,%edi - jnz 2b -1: mov %ecx,%edx - shr $2,%ecx - rep - movsl - and $3,%edx - jz 1f -2: movsb - dec %edx - jnz 2b -1: pop %edi - pop %esi - ret diff --git a/lib/string/i386/memmove.s b/lib/string/i386/memmove.s deleted file mode 100644 index 2a6a504..0000000 --- a/lib/string/i386/memmove.s +++ /dev/null @@ -1,22 +0,0 @@ -.global memmove -.type memmove,@function -memmove: - mov 4(%esp),%eax - sub 8(%esp),%eax - cmp 12(%esp),%eax -.hidden __memcpy_fwd - jae __memcpy_fwd - push %esi - push %edi - mov 12(%esp),%edi - mov 16(%esp),%esi - mov 20(%esp),%ecx - lea -1(%edi,%ecx),%edi - lea -1(%esi,%ecx),%esi - std - rep movsb - cld - lea 1(%edi),%eax - pop %edi - pop %esi - ret diff --git a/lib/string/i386/memset.s b/lib/string/i386/memset.s deleted file mode 100644 index d00422c..0000000 --- a/lib/string/i386/memset.s +++ /dev/null @@ -1,76 +0,0 @@ -.global memset -.type memset,@function -memset: - mov 12(%esp),%ecx - cmp $62,%ecx - ja 2f - - mov 8(%esp),%dl - mov 4(%esp),%eax - test %ecx,%ecx - jz 1f - - mov %dl,%dh - - mov %dl,(%eax) - mov %dl,-1(%eax,%ecx) - cmp $2,%ecx - jbe 1f - - mov %dx,1(%eax) - mov %dx,(-1-2)(%eax,%ecx) - cmp $6,%ecx - jbe 1f - - shl $16,%edx - mov 8(%esp),%dl - mov 8(%esp),%dh - - mov %edx,(1+2)(%eax) - mov %edx,(-1-2-4)(%eax,%ecx) - cmp $14,%ecx - jbe 1f - - mov %edx,(1+2+4)(%eax) - mov %edx,(1+2+4+4)(%eax) - mov %edx,(-1-2-4-8)(%eax,%ecx) - mov %edx,(-1-2-4-4)(%eax,%ecx) - cmp $30,%ecx - jbe 1f - - mov %edx,(1+2+4+8)(%eax) - mov %edx,(1+2+4+8+4)(%eax) - mov %edx,(1+2+4+8+8)(%eax) - mov %edx,(1+2+4+8+12)(%eax) - mov %edx,(-1-2-4-8-16)(%eax,%ecx) - mov %edx,(-1-2-4-8-12)(%eax,%ecx) - mov %edx,(-1-2-4-8-8)(%eax,%ecx) - mov %edx,(-1-2-4-8-4)(%eax,%ecx) - -1: ret - -2: movzbl 8(%esp),%eax - mov %edi,12(%esp) - imul $0x1010101,%eax - mov 4(%esp),%edi - test $15,%edi - mov %eax,-4(%edi,%ecx) - jnz 2f - -1: shr $2, %ecx - rep - stosl - mov 4(%esp),%eax - mov 12(%esp),%edi - ret - -2: xor %edx,%edx - sub %edi,%edx - and $15,%edx - mov %eax,(%edi) - mov %eax,4(%edi) - mov %eax,8(%edi) - mov %eax,12(%edi) - sub %edx,%ecx - add %edx,%edi - jmp 1b diff --git a/lib/string/index.c b/lib/string/index.c deleted file mode 100644 index 8f6894c..0000000 --- a/lib/string/index.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include -#include - -char *index(const char *s, int c) -{ - return strchr(s, c); -} diff --git a/lib/string/memccpy.c b/lib/string/memccpy.c deleted file mode 100644 index 99f146f..0000000 --- a/lib/string/memccpy.c +++ /dev/null @@ -1,37 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -#define ALIGN (sizeof(size_t)-1) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n) -{ - unsigned char *d = dest; - const unsigned char *s = src; - - c = (unsigned char)c; -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - word *wd; - const word *ws; - if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { - for (; ((uintptr_t)s & ALIGN) && n && (*d=*s)!=c; n--, s++, d++); - if ((uintptr_t)s & ALIGN) goto tail; - size_t k = ONES * c; - wd=(void *)d; ws=(const void *)s; - for (; n>=sizeof(size_t) && !HASZERO(*ws^k); - n-=sizeof(size_t), ws++, wd++) *wd = *ws; - d=(void *)wd; s=(const void *)ws; - } -#endif - for (; n && (*d=*s)!=c; n--, s++, d++); -tail: - if (n) return d+1; - return 0; -} diff --git a/lib/string/memchr.c b/lib/string/memchr.c deleted file mode 100644 index d0f670f..0000000 --- a/lib/string/memchr.c +++ /dev/null @@ -1,30 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -#define SS (sizeof(size_t)) -#define ALIGN (sizeof(size_t)-1) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -void *memchr(const void *src, int c, size_t n) -{ - const unsigned char *s = src; - c = (unsigned char)c; -#ifdef __GNUC__ - for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--); - if (n && *s != c) { - typedef size_t __attribute__((__may_alias__)) word; - const word *w; - size_t k = ONES * c; - for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS); - s = (const void *)w; - } -#endif - for (; n && *s != c; s++, n--); - return n ? (void *)s : 0; -} diff --git a/lib/string/memcmp.c b/lib/string/memcmp.c deleted file mode 100644 index 15606f4..0000000 --- a/lib/string/memcmp.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -int memcmp(const void *vl, const void *vr, size_t n) -{ - const unsigned char *l=vl, *r=vr; - for (; n && *l == *r; n--, l++, r++); - return n ? *l-*r : 0; -} diff --git a/lib/string/memcpy.c b/lib/string/memcpy.c deleted file mode 100644 index de26a00..0000000 --- a/lib/string/memcpy.c +++ /dev/null @@ -1,127 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -void *memcpy(void *restrict dest, const void *restrict src, size_t n) -{ - unsigned char *d = dest; - const unsigned char *s = src; - -#ifdef __GNUC__ - -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define LS >> -#define RS << -#else -#define LS << -#define RS >> -#endif - - typedef uint32_t __attribute__((__may_alias__)) u32; - uint32_t w, x; - - for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++; - - if ((uintptr_t)d % 4 == 0) { - for (; n>=16; s+=16, d+=16, n-=16) { - *(u32 *)(d+0) = *(u32 *)(s+0); - *(u32 *)(d+4) = *(u32 *)(s+4); - *(u32 *)(d+8) = *(u32 *)(s+8); - *(u32 *)(d+12) = *(u32 *)(s+12); - } - if (n&8) { - *(u32 *)(d+0) = *(u32 *)(s+0); - *(u32 *)(d+4) = *(u32 *)(s+4); - d += 8; s += 8; - } - if (n&4) { - *(u32 *)(d+0) = *(u32 *)(s+0); - d += 4; s += 4; - } - if (n&2) { - *d++ = *s++; *d++ = *s++; - } - if (n&1) { - *d = *s; - } - return dest; - } - - if (n >= 32) switch ((uintptr_t)d % 4) { - case 1: - w = *(u32 *)s; - *d++ = *s++; - *d++ = *s++; - *d++ = *s++; - n -= 3; - for (; n>=17; s+=16, d+=16, n-=16) { - x = *(u32 *)(s+1); - *(u32 *)(d+0) = (w LS 24) | (x RS 8); - w = *(u32 *)(s+5); - *(u32 *)(d+4) = (x LS 24) | (w RS 8); - x = *(u32 *)(s+9); - *(u32 *)(d+8) = (w LS 24) | (x RS 8); - w = *(u32 *)(s+13); - *(u32 *)(d+12) = (x LS 24) | (w RS 8); - } - break; - case 2: - w = *(u32 *)s; - *d++ = *s++; - *d++ = *s++; - n -= 2; - for (; n>=18; s+=16, d+=16, n-=16) { - x = *(u32 *)(s+2); - *(u32 *)(d+0) = (w LS 16) | (x RS 16); - w = *(u32 *)(s+6); - *(u32 *)(d+4) = (x LS 16) | (w RS 16); - x = *(u32 *)(s+10); - *(u32 *)(d+8) = (w LS 16) | (x RS 16); - w = *(u32 *)(s+14); - *(u32 *)(d+12) = (x LS 16) | (w RS 16); - } - break; - case 3: - w = *(u32 *)s; - *d++ = *s++; - n -= 1; - for (; n>=19; s+=16, d+=16, n-=16) { - x = *(u32 *)(s+3); - *(u32 *)(d+0) = (w LS 8) | (x RS 24); - w = *(u32 *)(s+7); - *(u32 *)(d+4) = (x LS 8) | (w RS 24); - x = *(u32 *)(s+11); - *(u32 *)(d+8) = (w LS 8) | (x RS 24); - w = *(u32 *)(s+15); - *(u32 *)(d+12) = (x LS 8) | (w RS 24); - } - break; - } - if (n&16) { - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - } - if (n&8) { - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - } - if (n&4) { - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - } - if (n&2) { - *d++ = *s++; *d++ = *s++; - } - if (n&1) { - *d = *s; - } - return dest; -#endif - - for (; n; n--) *d++ = *s++; - return dest; -} diff --git a/lib/string/memmem.c b/lib/string/memmem.c deleted file mode 100644 index cfd23bb..0000000 --- a/lib/string/memmem.c +++ /dev/null @@ -1,152 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _GNU_SOURCE -#include -#include - -static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) -{ - uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; - for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++) - if (hw == nw) return (char *)h-2; - return hw == nw ? (char *)h-2 : 0; -} - -static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8; - uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8; - for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8) - if (hw == nw) return (char *)h-3; - return hw == nw ? (char *)h-3 : 0; -} - -static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; - uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; - for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++) - if (hw == nw) return (char *)h-4; - return hw == nw ? (char *)h-4 : 0; -} - -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) - -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) - -static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l) -{ - size_t i, ip, jp, k, p, ms, p0, mem, mem0; - size_t byteset[32 / sizeof(size_t)] = { 0 }; - size_t shift[256]; - - /* Computing length of needle and fill shift table */ - for (i=0; i n[jp+k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - ms = ip; - p0 = p; - - /* And with the opposite comparison */ - ip = -1; jp = 0; k = p = 1; - while (jp+k ms+1) ms = ip; - else p = p0; - - /* Periodic needle? */ - if (memcmp(n, n+p, ms+1)) { - mem0 = 0; - p = MAX(ms, l-ms-1) + 1; - } else mem0 = l-p; - mem = 0; - - /* Search loop */ - for (;;) { - /* If remainder of haystack is shorter than needle, done */ - if (z-h < l) return 0; - - /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l-1], &)) { - k = l-shift[h[l-1]]; - if (k) { - if (k < mem) k = mem; - h += k; - mem = 0; - continue; - } - } else { - h += l; - mem = 0; - continue; - } - - /* Compare right half */ - for (k=MAX(ms+1,mem); kmem && n[k-1] == h[k-1]; k--); - if (k <= mem) return (char *)h; - h += p; - mem = mem0; - } -} - -void *memmem(const void *h0, size_t k, const void *n0, size_t l) -{ - const unsigned char *h = h0, *n = n0; - - /* Return immediately on empty needle */ - if (!l) return (void *)h; - - /* Return immediately when needle is longer than haystack */ - if (k -#include - -#ifdef __GNUC__ -typedef __attribute__((__may_alias__)) size_t WT; -#define WS (sizeof(WT)) -#endif - -void *memmove(void *dest, const void *src, size_t n) -{ - char *d = dest; - const char *s = src; - - if (d==s) return d; - if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n); - - if (d=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s; - } -#endif - for (; n; n--) *d++ = *s++; - } else { -#ifdef __GNUC__ - if ((uintptr_t)s % WS == (uintptr_t)d % WS) { - while ((uintptr_t)(d+n) % WS) { - if (!n--) return dest; - d[n] = s[n]; - } - while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n); - } -#endif - while (n) n--, d[n] = s[n]; - } - - return dest; -} diff --git a/lib/string/mempcpy.c b/lib/string/mempcpy.c deleted file mode 100644 index 1f33e45..0000000 --- a/lib/string/mempcpy.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _GNU_SOURCE -#include - -void *mempcpy(void *dest, const void *src, size_t n) -{ - return (char *)memcpy(dest, src, n) + n; -} diff --git a/lib/string/memrchr.c b/lib/string/memrchr.c deleted file mode 100644 index 8d5c742..0000000 --- a/lib/string/memrchr.c +++ /dev/null @@ -1,14 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -void *__memrchr(const void *m, int c, size_t n) -{ - const unsigned char *s = m; - c = (unsigned char)c; - while (n--) if (s[n]==c) return (void *)(s+n); - return 0; -} - -weak_alias(__memrchr, memrchr); diff --git a/lib/string/memset.c b/lib/string/memset.c deleted file mode 100644 index e780c71..0000000 --- a/lib/string/memset.c +++ /dev/null @@ -1,93 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include - -void *memset(void *dest, int c, size_t n) -{ - unsigned char *s = dest; - size_t k; - - /* Fill head and tail with minimal branching. Each - * conditional ensures that all the subsequently used - * offsets are well-defined and in the dest region. */ - - if (!n) return dest; - s[0] = c; - s[n-1] = c; - if (n <= 2) return dest; - s[1] = c; - s[2] = c; - s[n-2] = c; - s[n-3] = c; - if (n <= 6) return dest; - s[3] = c; - s[n-4] = c; - if (n <= 8) return dest; - - /* Advance pointer to align it at a 4-byte boundary, - * and truncate n to a multiple of 4. The previous code - * already took care of any head/tail that get cut off - * by the alignment. */ - - k = -(uintptr_t)s & 3; - s += k; - n -= k; - n &= -4; - -#ifdef __GNUC__ - typedef uint32_t __attribute__((__may_alias__)) u32; - typedef uint64_t __attribute__((__may_alias__)) u64; - - u32 c32 = ((u32)-1)/255 * (unsigned char)c; - - /* In preparation to copy 32 bytes at a time, aligned on - * an 8-byte bounary, fill head/tail up to 28 bytes each. - * As in the initial byte-based head/tail fill, each - * conditional below ensures that the subsequent offsets - * are valid (e.g. !(n<=24) implies n>=28). */ - - *(u32 *)(s+0) = c32; - *(u32 *)(s+n-4) = c32; - if (n <= 8) return dest; - *(u32 *)(s+4) = c32; - *(u32 *)(s+8) = c32; - *(u32 *)(s+n-12) = c32; - *(u32 *)(s+n-8) = c32; - if (n <= 24) return dest; - *(u32 *)(s+12) = c32; - *(u32 *)(s+16) = c32; - *(u32 *)(s+20) = c32; - *(u32 *)(s+24) = c32; - *(u32 *)(s+n-28) = c32; - *(u32 *)(s+n-24) = c32; - *(u32 *)(s+n-20) = c32; - *(u32 *)(s+n-16) = c32; - - /* Align to a multiple of 8 so we can fill 64 bits at a time, - * and avoid writing the same bytes twice as much as is - * practical without introducing additional branching. */ - - k = 24 + ((uintptr_t)s & 4); - s += k; - n -= k; - - /* If this loop is reached, 28 tail bytes have already been - * filled, so any remainder when n drops below 32 can be - * safely ignored. */ - - u64 c64 = c32 | ((u64)c32 << 32); - for (; n >= 32; n-=32, s+=32) { - *(u64 *)(s+0) = c64; - *(u64 *)(s+8) = c64; - *(u64 *)(s+16) = c64; - *(u64 *)(s+24) = c64; - } -#else - /* Pure C fallback with no aliasing violations. */ - for (; n; n--, s++) *s = c; -#endif - - return dest; -} diff --git a/lib/string/rindex.c b/lib/string/rindex.c deleted file mode 100644 index 897e19e..0000000 --- a/lib/string/rindex.c +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include -#include - -char *rindex(const char *s, int c) -{ - return strrchr(s, c); -} diff --git a/lib/string/stpcpy.c b/lib/string/stpcpy.c deleted file mode 100644 index 0c1f1b4..0000000 --- a/lib/string/stpcpy.c +++ /dev/null @@ -1,32 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -#define ALIGN (sizeof(size_t)) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -char *__stpcpy(char *restrict d, const char *restrict s) -{ -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - word *wd; - const word *ws; - if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) { - for (; (uintptr_t)s % ALIGN; s++, d++) - if (!(*d=*s)) return d; - wd=(void *)d; ws=(const void *)s; - for (; !HASZERO(*ws); *wd++ = *ws++); - d=(void *)wd; s=(const void *)ws; - } -#endif - for (; (*d=*s); s++, d++); - - return d; -} - -weak_alias(__stpcpy, stpcpy); diff --git a/lib/string/stpncpy.c b/lib/string/stpncpy.c deleted file mode 100644 index ca2ef78..0000000 --- a/lib/string/stpncpy.c +++ /dev/null @@ -1,35 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -#define ALIGN (sizeof(size_t)-1) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -char *__stpncpy(char *restrict d, const char *restrict s, size_t n) -{ -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - word *wd; - const word *ws; - if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { - for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); - if (!n || !*s) goto tail; - wd=(void *)d; ws=(const void *)s; - for (; n>=sizeof(size_t) && !HASZERO(*ws); - n-=sizeof(size_t), ws++, wd++) *wd = *ws; - d=(void *)wd; s=(const void *)ws; - } -#endif - for (; n && (*d=*s); n--, s++, d++); -tail: - memset(d, 0, n); - return d; -} - -weak_alias(__stpncpy, stpncpy); - diff --git a/lib/string/strcat.c b/lib/string/strcat.c deleted file mode 100644 index 723c2ff..0000000 --- a/lib/string/strcat.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strcat(char *restrict dest, const char *restrict src) -{ - strcpy(dest + strlen(dest), src); - return dest; -} diff --git a/lib/string/strchr.c b/lib/string/strchr.c deleted file mode 100644 index ddeca01..0000000 --- a/lib/string/strchr.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strchr(const char *s, int c) -{ - char *r = __strchrnul(s, c); - return *(unsigned char *)r == (unsigned char)c ? r : 0; -} diff --git a/lib/string/strchrnul.c b/lib/string/strchrnul.c deleted file mode 100644 index 6fc86d1..0000000 --- a/lib/string/strchrnul.c +++ /dev/null @@ -1,31 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -#define ALIGN (sizeof(size_t)) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -char *__strchrnul(const char *s, int c) -{ - c = (unsigned char)c; - if (!c) return (char *)s + strlen(s); - -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - const word *w; - for (; (uintptr_t)s % ALIGN; s++) - if (!*s || *(unsigned char *)s == c) return (char *)s; - size_t k = ONES * c; - for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); - s = (void *)w; -#endif - for (; *s && *(unsigned char *)s != c; s++); - return (char *)s; -} - -weak_alias(__strchrnul, strchrnul); diff --git a/lib/string/strcmp.c b/lib/string/strcmp.c deleted file mode 100644 index afcb1b7..0000000 --- a/lib/string/strcmp.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -int strcmp(const char *l, const char *r) -{ - for (; *l==*r && *l; l++, r++); - return *(unsigned char *)l - *(unsigned char *)r; -} diff --git a/lib/string/strcpy.c b/lib/string/strcpy.c deleted file mode 100644 index a761577..0000000 --- a/lib/string/strcpy.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strcpy(char *restrict dest, const char *restrict src) -{ - __stpcpy(dest, src); - return dest; -} diff --git a/lib/string/strcspn.c b/lib/string/strcspn.c deleted file mode 100644 index ba55fe6..0000000 --- a/lib/string/strcspn.c +++ /dev/null @@ -1,20 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) - -size_t strcspn(const char *s, const char *c) -{ - const char *a = s; - size_t byteset[32/sizeof(size_t)]; - - if (!c[0] || !c[1]) return __strchrnul(s, *c)-a; - - memset(byteset, 0, sizeof byteset); - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); - for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++); - return s-a; -} diff --git a/lib/string/strlcat.c b/lib/string/strlcat.c deleted file mode 100644 index a61821d..0000000 --- a/lib/string/strlcat.c +++ /dev/null @@ -1,12 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include - -size_t strlcat(char *d, const char *s, size_t n) -{ - size_t l = strnlen(d, n); - if (l == n) return l + strlen(s); - return l + strlcpy(d+l, s, n-l); -} diff --git a/lib/string/strlcpy.c b/lib/string/strlcpy.c deleted file mode 100644 index c2a5142..0000000 --- a/lib/string/strlcpy.c +++ /dev/null @@ -1,37 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _BSD_SOURCE -#include -#include -#include - -#define ALIGN (sizeof(size_t)-1) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -size_t strlcpy(char *d, const char *s, size_t n) -{ - char *d0 = d; - size_t *wd; - - if (!n--) goto finish; -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - const word *ws; - if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { - for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); - if (n && *s) { - wd=(void *)d; ws=(const void *)s; - for (; n>=sizeof(size_t) && !HASZERO(*ws); - n-=sizeof(size_t), ws++, wd++) *wd = *ws; - d=(void *)wd; s=(const void *)ws; - } - } -#endif - for (; n && (*d=*s); n--, s++, d++); - *d = 0; -finish: - return d-d0 + strlen(s); -} diff --git a/lib/string/strlen.c b/lib/string/strlen.c deleted file mode 100644 index 76e14f2..0000000 --- a/lib/string/strlen.c +++ /dev/null @@ -1,25 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include -#include - -#define ALIGN (sizeof(size_t)) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -size_t strlen(const char *s) -{ - const char *a = s; -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - const word *w; - for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a; - for (w = (const void *)s; !HASZERO(*w); w++); - s = (const void *)w; -#endif - for (; *s; s++); - return s-a; -} diff --git a/lib/string/strncat.c b/lib/string/strncat.c deleted file mode 100644 index a144e37..0000000 --- a/lib/string/strncat.c +++ /dev/null @@ -1,13 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strncat(char *restrict d, const char *restrict s, size_t n) -{ - char *a = d; - d += strlen(d); - while (n && *s) n--, *d++ = *s++; - *d++ = 0; - return a; -} diff --git a/lib/string/strncmp.c b/lib/string/strncmp.c deleted file mode 100644 index b98473b..0000000 --- a/lib/string/strncmp.c +++ /dev/null @@ -1,12 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -int strncmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l=(void *)_l, *r=(void *)_r; - if (!n--) return 0; - for (; *l && *r && n && *l == *r ; l++, r++, n--); - return *l - *r; -} diff --git a/lib/string/strncpy.c b/lib/string/strncpy.c deleted file mode 100644 index a828465..0000000 --- a/lib/string/strncpy.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strncpy(char *restrict d, const char *restrict s, size_t n) -{ - __stpncpy(d, s, n); - return d; -} diff --git a/lib/string/strnlen.c b/lib/string/strnlen.c deleted file mode 100644 index 8931c52..0000000 --- a/lib/string/strnlen.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -size_t strnlen(const char *s, size_t n) -{ - const char *p = memchr(s, 0, n); - return p ? p-s : n; -} diff --git a/lib/string/strpbrk.c b/lib/string/strpbrk.c deleted file mode 100644 index 82fbc92..0000000 --- a/lib/string/strpbrk.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strpbrk(const char *s, const char *b) -{ - s += strcspn(s, b); - return *s ? (char *)s : 0; -} diff --git a/lib/string/strrchr.c b/lib/string/strrchr.c deleted file mode 100644 index a45b459..0000000 --- a/lib/string/strrchr.c +++ /dev/null @@ -1,9 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strrchr(const char *s, int c) -{ - return __memrchr(s, c, strlen(s) + 1); -} diff --git a/lib/string/strsep.c b/lib/string/strsep.c deleted file mode 100644 index 2d513f4..0000000 --- a/lib/string/strsep.c +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#define _GNU_SOURCE -#include - -char *strsep(char **str, const char *sep) -{ - char *s = *str, *end; - if (!s) return NULL; - end = s + strcspn(s, sep); - if (*end) *end++ = 0; - else end = 0; - *str = end; - return s; -} diff --git a/lib/string/strspn.c b/lib/string/strspn.c deleted file mode 100644 index f27710d..0000000 --- a/lib/string/strspn.c +++ /dev/null @@ -1,23 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) - -size_t strspn(const char *s, const char *c) -{ - const char *a = s; - size_t byteset[32/sizeof(size_t)] = { 0 }; - - if (!c[0]) return 0; - if (!c[1]) { - for (; *s == *c; s++); - return s-a; - } - - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); - for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++); - return s-a; -} diff --git a/lib/string/strstr.c b/lib/string/strstr.c deleted file mode 100644 index 07e2982..0000000 --- a/lib/string/strstr.c +++ /dev/null @@ -1,157 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include -#include - -static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; - for (h++; *h && hw != nw; hw = hw<<8 | *++h); - return *h ? (char *)h-1 : 0; -} - -static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8; - uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8; - for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); - return *h ? (char *)h-2 : 0; -} - -static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; - uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; - for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); - return *h ? (char *)h-3 : 0; -} - -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) - -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) - -static char *twoway_strstr(const unsigned char *h, const unsigned char *n) -{ - const unsigned char *z; - size_t l, ip, jp, k, p, ms, p0, mem, mem0; - size_t byteset[32 / sizeof(size_t)] = { 0 }; - size_t shift[256]; - - /* Computing length of needle and fill shift table */ - for (l=0; n[l] && h[l]; l++) - BITOP(byteset, n[l], |=), shift[n[l]] = l+1; - if (n[l]) return 0; /* hit the end of h */ - - /* Compute maximal suffix */ - ip = -1; jp = 0; k = p = 1; - while (jp+k n[jp+k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - ms = ip; - p0 = p; - - /* And with the opposite comparison */ - ip = -1; jp = 0; k = p = 1; - while (jp+k ms+1) ms = ip; - else p = p0; - - /* Periodic needle? */ - if (memcmp(n, n+p, ms+1)) { - mem0 = 0; - p = MAX(ms, l-ms-1) + 1; - } else mem0 = l-p; - mem = 0; - - /* Initialize incremental end-of-haystack pointer */ - z = h; - - /* Search loop */ - for (;;) { - /* Update incremental end-of-haystack pointer */ - if (z-h < l) { - /* Fast estimate for MAX(l,63) */ - size_t grow = l | 63; - const unsigned char *z2 = memchr(z, 0, grow); - if (z2) { - z = z2; - if (z-h < l) return 0; - } else z += grow; - } - - /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l-1], &)) { - k = l-shift[h[l-1]]; - if (k) { - if (k < mem) k = mem; - h += k; - mem = 0; - continue; - } - } else { - h += l; - mem = 0; - continue; - } - - /* Compare right half */ - for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); - if (n[k]) { - h += k-ms; - mem = 0; - continue; - } - /* Compare left half */ - for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); - if (k <= mem) return (char *)h; - h += p; - mem = mem0; - } -} - -char *strstr(const char *h, const char *n) -{ - /* Return immediately on empty needle */ - if (!n[0]) return (char *)h; - - /* Use faster algorithms for short needles */ - h = strchr(h, *n); - if (!h || !n[1]) return (char *)h; - if (!h[1]) return 0; - if (!n[2]) return twobyte_strstr((void *)h, (void *)n); - if (!h[2]) return 0; - if (!n[3]) return threebyte_strstr((void *)h, (void *)n); - if (!h[3]) return 0; - if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); - - return twoway_strstr((void *)h, (void *)n); -} diff --git a/lib/string/strtok.c b/lib/string/strtok.c deleted file mode 100644 index 86d9c97..0000000 --- a/lib/string/strtok.c +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strtok(char *restrict s, const char *restrict sep) -{ - static char *p; - if (!s && !(s = p)) return NULL; - s += strspn(s, sep); - if (!*s) return p = 0; - p = s + strcspn(s, sep); - if (*p) *p++ = 0; - else p = 0; - return s; -} diff --git a/lib/string/strtok_r.c b/lib/string/strtok_r.c deleted file mode 100644 index a702703..0000000 --- a/lib/string/strtok_r.c +++ /dev/null @@ -1,15 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Derived from musl libc (Copyright © 2005-2020 Rich Felker, et al.) */ - -#include - -char *strtok_r(char *restrict s, const char *restrict sep, char **restrict p) -{ - if (!s && !(s = *p)) return NULL; - s += strspn(s, sep); - if (!*s) return *p = 0; - *p = s + strcspn(s, sep); - if (**p) *(*p)++ = 0; - else *p = 0; - return s; -} diff --git a/lib/string/x86_64/memcpy.s b/lib/string/x86_64/memcpy.s deleted file mode 100644 index 3d960ef..0000000 --- a/lib/string/x86_64/memcpy.s +++ /dev/null @@ -1,25 +0,0 @@ -.global memcpy -.global __memcpy_fwd -.hidden __memcpy_fwd -.type memcpy,@function -memcpy: -__memcpy_fwd: - mov %rdi,%rax - cmp $8,%rdx - jc 1f - test $7,%edi - jz 1f -2: movsb - dec %rdx - test $7,%edi - jnz 2b -1: mov %rdx,%rcx - shr $3,%rcx - rep - movsq - and $7,%edx - jz 1f -2: movsb - dec %edx - jnz 2b -1: ret diff --git a/lib/string/x86_64/memmove.s b/lib/string/x86_64/memmove.s deleted file mode 100644 index 172c025..0000000 --- a/lib/string/x86_64/memmove.s +++ /dev/null @@ -1,16 +0,0 @@ -.global memmove -.type memmove,@function -memmove: - mov %rdi,%rax - sub %rsi,%rax - cmp %rdx,%rax -.hidden __memcpy_fwd - jae __memcpy_fwd - mov %rdx,%rcx - lea -1(%rdi,%rdx),%rdi - lea -1(%rsi,%rdx),%rsi - std - rep movsb - cld - lea 1(%rdi),%rax - ret diff --git a/lib/string/x86_64/memset.s b/lib/string/x86_64/memset.s deleted file mode 100644 index 2d3f5e5..0000000 --- a/lib/string/x86_64/memset.s +++ /dev/null @@ -1,72 +0,0 @@ -.global memset -.type memset,@function -memset: - movzbq %sil,%rax - mov $0x101010101010101,%r8 - imul %r8,%rax - - cmp $126,%rdx - ja 2f - - test %edx,%edx - jz 1f - - mov %sil,(%rdi) - mov %sil,-1(%rdi,%rdx) - cmp $2,%edx - jbe 1f - - mov %ax,1(%rdi) - mov %ax,(-1-2)(%rdi,%rdx) - cmp $6,%edx - jbe 1f - - mov %eax,(1+2)(%rdi) - mov %eax,(-1-2-4)(%rdi,%rdx) - cmp $14,%edx - jbe 1f - - mov %rax,(1+2+4)(%rdi) - mov %rax,(-1-2-4-8)(%rdi,%rdx) - cmp $30,%edx - jbe 1f - - mov %rax,(1+2+4+8)(%rdi) - mov %rax,(1+2+4+8+8)(%rdi) - mov %rax,(-1-2-4-8-16)(%rdi,%rdx) - mov %rax,(-1-2-4-8-8)(%rdi,%rdx) - cmp $62,%edx - jbe 1f - - mov %rax,(1+2+4+8+16)(%rdi) - mov %rax,(1+2+4+8+16+8)(%rdi) - mov %rax,(1+2+4+8+16+16)(%rdi) - mov %rax,(1+2+4+8+16+24)(%rdi) - mov %rax,(-1-2-4-8-16-32)(%rdi,%rdx) - mov %rax,(-1-2-4-8-16-24)(%rdi,%rdx) - mov %rax,(-1-2-4-8-16-16)(%rdi,%rdx) - mov %rax,(-1-2-4-8-16-8)(%rdi,%rdx) - -1: mov %rdi,%rax - ret - -2: test $15,%edi - mov %rdi,%r8 - mov %rax,-8(%rdi,%rdx) - mov %rdx,%rcx - jnz 2f - -1: shr $3,%rcx - rep - stosq - mov %r8,%rax - ret - -2: xor %edx,%edx - sub %edi,%edx - and $15,%edx - mov %rax,(%rdi) - mov %rax,8(%rdi) - sub %rdx,%rcx - add %rdx,%rdi - jmp 1b -- cgit v1.2.3