From: "Paul E. McKenney" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected],
Willy Tarreau <[email protected]>,
"Paul E . McKenney" <[email protected]>
Subject: [PATCH nolibc 08/61] tools/nolibc/string: split the string functions into string.h
Date: Mon, 18 Apr 2022 17:41:32 -0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <20220419004219.GA3952301@paulmck-ThinkPad-P17-Gen-1>
From: Willy Tarreau <[email protected]>
The string manipulation functions (mem*, str*) are now found in
string.h. The file depends on almost nothing and will be
usable from other includes if needed. Maybe more functions could
be added.
Signed-off-by: Willy Tarreau <[email protected]>
Signed-off-by: Paul E. McKenney <[email protected]>
---
tools/include/nolibc/nolibc.h | 96 +-----------------------------
tools/include/nolibc/string.h | 107 ++++++++++++++++++++++++++++++++++
2 files changed, 108 insertions(+), 95 deletions(-)
create mode 100644 tools/include/nolibc/string.h
diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index ed909a8daa1a..b06bd5cb5651 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -88,99 +88,11 @@
#include "types.h"
#include "sys.h"
#include "stdlib.h"
+#include "string.h"
/* Used by programs to avoid std includes */
#define NOLIBC
-/* some size-optimized reimplementations of a few common str* and mem*
- * functions. They're marked static, except memcpy() and raise() which are used
- * by libgcc on ARM, so they are marked weak instead in order not to cause an
- * error when building a program made of multiple files (not recommended).
- */
-
-static __attribute__((unused))
-void *memmove(void *dst, const void *src, size_t len)
-{
- ssize_t pos = (dst <= src) ? -1 : (long)len;
- void *ret = dst;
-
- while (len--) {
- pos += (dst <= src) ? 1 : -1;
- ((char *)dst)[pos] = ((char *)src)[pos];
- }
- return ret;
-}
-
-static __attribute__((unused))
-void *memset(void *dst, int b, size_t len)
-{
- char *p = dst;
-
- while (len--)
- *(p++) = b;
- return dst;
-}
-
-static __attribute__((unused))
-int memcmp(const void *s1, const void *s2, size_t n)
-{
- size_t ofs = 0;
- char c1 = 0;
-
- while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) {
- ofs++;
- }
- return c1;
-}
-
-static __attribute__((unused))
-char *strcpy(char *dst, const char *src)
-{
- char *ret = dst;
-
- while ((*dst++ = *src++));
- return ret;
-}
-
-static __attribute__((unused))
-char *strchr(const char *s, int c)
-{
- while (*s) {
- if (*s == (char)c)
- return (char *)s;
- s++;
- }
- return NULL;
-}
-
-static __attribute__((unused))
-char *strrchr(const char *s, int c)
-{
- const char *ret = NULL;
-
- while (*s) {
- if (*s == (char)c)
- ret = s;
- s++;
- }
- return (char *)ret;
-}
-
-static __attribute__((unused))
-size_t nolibc_strlen(const char *str)
-{
- size_t len;
-
- for (len = 0; str[len]; len++);
- return len;
-}
-
-#define strlen(str) ({ \
- __builtin_constant_p((str)) ? \
- __builtin_strlen((str)) : \
- nolibc_strlen((str)); \
-})
-
static __attribute__((unused))
int isdigit(int c)
{
@@ -209,12 +121,6 @@ const char *ltoa(long in)
return pos + 1;
}
-__attribute__((weak,unused))
-void *memcpy(void *dst, const void *src, size_t len)
-{
- return memmove(dst, src, len);
-}
-
/* Here come a few helper functions */
static __attribute__((unused))
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
new file mode 100644
index 000000000000..8a23cda2d450
--- /dev/null
+++ b/tools/include/nolibc/string.h
@@ -0,0 +1,107 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * string function definitions for NOLIBC
+ * Copyright (C) 2017-2021 Willy Tarreau <[email protected]>
+ */
+
+#ifndef _NOLIBC_STRING_H
+#define _NOLIBC_STRING_H
+
+#include "std.h"
+
+/*
+ * As much as possible, please keep functions alphabetically sorted.
+ */
+
+static __attribute__((unused))
+int memcmp(const void *s1, const void *s2, size_t n)
+{
+ size_t ofs = 0;
+ char c1 = 0;
+
+ while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) {
+ ofs++;
+ }
+ return c1;
+}
+
+static __attribute__((unused))
+void *memmove(void *dst, const void *src, size_t len)
+{
+ ssize_t pos = (dst <= src) ? -1 : (long)len;
+ void *ret = dst;
+
+ while (len--) {
+ pos += (dst <= src) ? 1 : -1;
+ ((char *)dst)[pos] = ((char *)src)[pos];
+ }
+ return ret;
+}
+
+/* must be exported, as it's used by libgcc on ARM */
+__attribute__((weak,unused))
+void *memcpy(void *dst, const void *src, size_t len)
+{
+ return memmove(dst, src, len);
+}
+
+static __attribute__((unused))
+void *memset(void *dst, int b, size_t len)
+{
+ char *p = dst;
+
+ while (len--)
+ *(p++) = b;
+ return dst;
+}
+
+static __attribute__((unused))
+char *strchr(const char *s, int c)
+{
+ while (*s) {
+ if (*s == (char)c)
+ return (char *)s;
+ s++;
+ }
+ return NULL;
+}
+
+static __attribute__((unused))
+char *strcpy(char *dst, const char *src)
+{
+ char *ret = dst;
+
+ while ((*dst++ = *src++));
+ return ret;
+}
+
+/* this function is only used with arguments that are not constants */
+static __attribute__((unused))
+size_t nolibc_strlen(const char *str)
+{
+ size_t len;
+
+ for (len = 0; str[len]; len++);
+ return len;
+}
+
+#define strlen(str) ({ \
+ __builtin_constant_p((str)) ? \
+ __builtin_strlen((str)) : \
+ nolibc_strlen((str)); \
+})
+
+static __attribute__((unused))
+char *strrchr(const char *s, int c)
+{
+ const char *ret = NULL;
+
+ while (*s) {
+ if (*s == (char)c)
+ ret = s;
+ s++;
+ }
+ return (char *)ret;
+}
+
+#endif /* _NOLIBC_STRING_H */
--
2.31.1.189.g2e36527f23
next prev parent reply other threads:[~2022-04-19 0:42 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-19 0:42 [PATCH nolibc 0/61] nolibc updates for v5.19 Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 01/61] tools/nolibc: use pselect6 on RISCV Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 02/61] tools/nolibc: guard the main file against multiple inclusion Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 03/61] tools/nolibc/std: move the standard type definitions to std.h Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 04/61] tools/nolibc/types: split syscall-specific definitions into their own files Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 05/61] tools/nolibc/arch: split arch-specific code into individual files Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 06/61] tools/nolibc/sys: split the syscall definitions into their own file Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 07/61] tools/nolibc/stdlib: extract the stdlib-specific functions to " Paul E. McKenney
2022-04-19 0:41 ` Paul E. McKenney [this message]
2022-04-19 0:41 ` [PATCH nolibc 09/61] tools/nolibc/ctype: split the is* functions to ctype.h Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 10/61] tools/nolibc/ctype: add the missing is* functions Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 11/61] tools/nolibc/types: move the FD_* functions to macros in types.h Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 12/61] tools/nolibc/types: make FD_SETSIZE configurable Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 13/61] tools/nolibc/types: move makedev to types.h and make it a macro Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 14/61] tools/nolibc/stdlib: move ltoa() to stdlib.h Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 15/61] tools/nolibc/stdlib: replace the ltoa() function with more efficient ones Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 16/61] tools/nolibc/stdlib: add i64toa() and u64toa() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 17/61] tools/nolibc/stdlib: add utoh() and u64toh() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 18/61] tools/nolibc/stdio: add a minimal set of stdio functions Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 19/61] tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 20/61] tools/nolibc/stdio: add fwrite() to stdio Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 21/61] tools/nolibc/stdio: add a minimal [vf]printf() implementation Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 22/61] tools/nolibc/types: define EXIT_SUCCESS and EXIT_FAILURE Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 23/61] tools/nolibc/stdio: add perror() to report the errno value Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 24/61] tools/nolibc/sys: make open() take a vararg on the 3rd argument Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 25/61] tools/nolibc/stdlib: avoid a 64-bit shift in u64toh_r() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 26/61] tools/nolibc/stdlib: make raise() use the lower level syscalls only Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 27/61] tools/nolibc/sys: make getpgrp(), getpid(), gettid() not set errno Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 28/61] tools/nolibc/string: use unidirectional variants for memcpy() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 29/61] tools/nolibc/string: slightly simplify memmove() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 30/61] tools/nolibc/string: add strncpy() and strlcpy() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 31/61] tools/nolibc/string: add tiny versions of strncat() and strlcat() Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 32/61] tools/nolibc: move exported functions to their own section Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 33/61] tools/nolibc/arch: mark the _start symbol as weak Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 34/61] tools/nolibc/types: define PATH_MAX and MAXPATHLEN Paul E. McKenney
2022-04-19 0:41 ` [PATCH nolibc 35/61] tools/nolibc/string: export memset() and memmove() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 36/61] tools/nolibc/errno: extract errno.h from sys.h Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 37/61] tools/nolibc/unistd: extract msleep(), sleep(), tcsetpgrp() to unistd.h Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 38/61] tools/nolibc/unistd: add usleep() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 39/61] tools/nolibc/signal: move raise() to signal.h Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 40/61] tools/nolibc/time: create time.h with time() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 41/61] tools/nolibc: also mention how to build by just setting the include path Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 42/61] tools/nolibc/stdlib: implement abort() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 43/61] tools/nolibc/stdio: make printf(%s) accept NULL Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 44/61] tools/nolibc/stdlib: add a simple getenv() implementation Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 45/61] tools/nolibc/stdio: add support for '%p' to vfprintf() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 46/61] tools/nolibc/string: add strcmp() and strncmp() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 47/61] tools/nolibc/sys: add syscall definition for getppid() Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 48/61] tools/nolibc/types: add poll() and waitpid() flag definitions Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 49/61] tools/nolibc: add a makefile to install headers Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 50/61] tools/nolibc: add the nolibc subdir to the common Makefile Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 51/61] tools/nolibc/string: do not use __builtin_strlen() at -O0 Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 52/61] tools/nolibc/stdlib: only reference the external environ when inlined Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 53/61] tools/nolibc: x86-64: Update System V ABI document link Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 54/61] tools/nolibc: Replace `asm` with `__asm__` Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 55/61] tools/nolibc: Remove .global _start from the entry point code Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 56/61] tools/nolibc: i386: Implement syscall with 6 arguments Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 57/61] tools/nolibc/sys: Implement `mmap()` and `munmap()` Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 58/61] tools/nolibc/types: Implement `offsetof()` and `container_of()` macro Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 59/61] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()` Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 60/61] tools/nolibc/string: Implement `strnlen()` Paul E. McKenney
2022-04-19 0:42 ` [PATCH nolibc 61/61] tools/nolibc/string: Implement `strdup()` and `strndup()` Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox