[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220322102115.186179-9-ammarfaizi2@gnuweeb.org>
Date: Tue, 22 Mar 2022 17:21:15 +0700
From: Ammar Faizi <ammarfaizi2@...weeb.org>
To: Willy Tarreau <w@....eu>
Cc: "Paul E. McKenney" <paulmck@...nel.org>,
Alviro Iskandar Setiawan <alviro.iskandar@...weeb.org>,
Nugraha <richiisei@...il.com>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
GNU/Weeb Mailing List <gwml@...r.gnuweeb.org>,
Ammar Faizi <ammarfaizi2@...weeb.org>
Subject: [RFC PATCH v2 8/8] tools/include/string: Implement `strdup()` and `strndup()`
These functions are currently only available on architectures that have
my_syscall6() macro implemented. Since these functions use malloc(),
malloc() uses mmap(), mmap() depends on my_syscall6() macro.
On architectures that don't support my_syscall6(), these function will
always return NULL with errno set to ENOSYS.
Signed-off-by: Ammar Faizi <ammarfaizi2@...weeb.org>
---
@@ Changelog:
Link RFC v1: https://lore.kernel.org/lkml/20220320093750.159991-7-ammarfaizi2@gnuweeb.org/
RFC v1 -> RFC v2:
- Update strdup and strndup implementation, use strlen and strnlen to get
the string length first (comment from Willy and Alviro).
- Fix the subject line prefix, it was "tools/include/string: ", it should be
"tools/nolibc/string: ".
- Update the commit message.
---
tools/include/nolibc/string.h | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index 1426eefc1ef2..bcc76f89199e 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -9,6 +9,8 @@
#include "std.h"
+static void *malloc(size_t len);
+
/*
* As much as possible, please keep functions alphabetically sorted.
*/
@@ -147,6 +149,36 @@ size_t strnlen(const char *str, size_t maxlen)
return len;
}
+static __attribute__((unused))
+char *strdup(const char *str)
+{
+ size_t len;
+ char *ret;
+
+ len = strlen(str);
+ ret = malloc(len + 1);
+ if (__builtin_expect(ret != NULL, 1))
+ memcpy(ret, str, len + 1);
+
+ return ret;
+}
+
+static __attribute__((unused))
+char *strndup(const char *str, size_t maxlen)
+{
+ size_t len;
+ char *ret;
+
+ len = strnlen(str, maxlen);
+ ret = malloc(len + 1);
+ if (__builtin_expect(ret != NULL, 1)) {
+ memcpy(ret, str, len);
+ ret[len] = '\0';
+ }
+
+ return ret;
+}
+
static __attribute__((unused))
size_t strlcat(char *dst, const char *src, size_t size)
{
--
Ammar Faizi
Powered by blists - more mailing lists