[PATCH] libc: remove more string functions from libc/ in favor of the original musl ones

5 views
Skip to first unread message

Waldemar Kozaczuk

unread,
Jul 27, 2020, 5:41:53 PM7/27/20
to osv...@googlegroups.com, Waldemar Kozaczuk
Just like the last two other libc related patches, this patch
also eliminates 7 more source files from libc/string/
in favor of the equivalent ones in musl/src/string/.

In this case those 7 files differed only like in this example:
```
diff musl/src/string/strchrnul.c libc/string/strchrnul.c
10c10
< #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
---
> #define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
```
Sometimes it would also have extra difference of '__restrict'
vs "restrict" keyword in function signature which is explained in one
of the other patches.

The git history for many of those files (I have not checked each of the 7 ones)
indicates that they were imported "as-is" from musl 7 years
ago (for example see commit 5cd0168fc566e5f7263b04948b4df6695d8ae721).
But then, `cd musl && git log src/string/memchr.c` for example, shows
that no changes have ever been made to this file on musl side ever,
at least per history of our clone of musl (there are later modifications in
to this file in upstream musl but not in respect to the HASZERO macro).

It is not clear why the macro HASZERO() had been modified like
so in all these cases, but based on my understanding of the operator
precedence in C, adding extra parentheses around '(x)-ONES' should
not change the end result (the subtraction and negation should be evaluated first
anyway followed by bitwise and).

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>
---
Makefile | 16 ++++++++--------
libc/string/memccpy.c | 32 --------------------------------
libc/string/memchr.c | 24 ------------------------
libc/string/stpcpy.c | 29 -----------------------------
libc/string/stpncpy.c | 32 --------------------------------
libc/string/strchrnul.c | 27 ---------------------------
libc/string/strlcpy.c | 32 --------------------------------
libc/string/strlen.c | 19 -------------------
8 files changed, 8 insertions(+), 203 deletions(-)
delete mode 100644 libc/string/memccpy.c
delete mode 100644 libc/string/memchr.c
delete mode 100644 libc/string/stpcpy.c
delete mode 100644 libc/string/stpncpy.c
delete mode 100644 libc/string/strchrnul.c
delete mode 100644 libc/string/strlcpy.c
delete mode 100644 libc/string/strlen.c

diff --git a/Makefile b/Makefile
index 7c7c0489..ff0ca4d7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# O./libc/string/strdup.cSv makefile
+# OSv makefile
#
# Copyright (C) 2015 Cloudius Systems, Ltd.
# This work is open source software, licensed under the terms of the
@@ -1556,8 +1556,8 @@ musl += string/bcmp.o
musl += string/bcopy.o
musl += string/bzero.o
musl += string/index.o
-libc += string/memccpy.o
-libc += string/memchr.o
+musl += string/memccpy.o
+musl += string/memchr.o
musl += string/memcmp.o
libc += string/memcpy.o
musl += string/memmem.o
@@ -1569,15 +1569,15 @@ libc += string/memset.o
libc += string/__memset_chk.o
libc += string/rawmemchr.o
musl += string/rindex.o
-libc += string/stpcpy.o
+musl += string/stpcpy.o
libc += string/__stpcpy_chk.o
-libc += string/stpncpy.o
+musl += string/stpncpy.o
musl += string/strcasecmp.o
musl += string/strcasestr.o
musl += string/strcat.o
libc += string/__strcat_chk.o
musl += string/strchr.o
-libc += string/strchrnul.o
+musl += string/strchrnul.o
musl += string/strcmp.o
musl += string/strcpy.o
libc += string/__strcpy_chk.o
@@ -1585,8 +1585,8 @@ musl += string/strcspn.o
musl += string/strdup.o
libc += string/strerror_r.o
libc += string/strlcat.o
-libc += string/strlcpy.o
-libc += string/strlen.o
+musl += string/strlcpy.o
+musl += string/strlen.o
musl += string/strncasecmp.o
musl += string/strncat.o
libc += string/__strncat_chk.o
diff --git a/libc/string/memccpy.c b/libc/string/memccpy.c
deleted file mode 100644
index 774cc5aa..00000000
--- a/libc/string/memccpy.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-
-#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;
- size_t *wd, k;
- const size_t *ws;
-
- c = (unsigned char)c;
- 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;
- 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;
- }
- for (; n && (*d=*s)!=c; n--, s++, d++);
-tail:
- if (*s==c) return d+1;
- return 0;
-}
diff --git a/libc/string/memchr.c b/libc/string/memchr.c
deleted file mode 100644
index 32e73135..00000000
--- a/libc/string/memchr.c
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-
-#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;
- for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--);
- if (n && *s != c) {
- const size_t *w;
- size_t k = ONES * c;
- for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS);
- for (s = (const void *)w; n && *s != c; s++, n--);
- }
- return n ? (void *)s : 0;
-}
diff --git a/libc/string/stpcpy.c b/libc/string/stpcpy.c
deleted file mode 100644
index bb04564f..00000000
--- a/libc/string/stpcpy.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-#include "libc.h"
-
-#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)
-{
- size_t *wd;
- const size_t *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;
- }
- for (; (*d=*s); s++, d++);
-
- return d;
-}
-
-weak_alias(__stpcpy, stpcpy);
diff --git a/libc/string/stpncpy.c b/libc/string/stpncpy.c
deleted file mode 100644
index 003a54cb..00000000
--- a/libc/string/stpncpy.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-#include "libc.h"
-
-#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)
-{
- size_t *wd;
- const size_t *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;
- }
- for (; n && (*d=*s); n--, s++, d++);
-tail:
- memset(d, 0, n);
- return d;
-}
-
-weak_alias(__stpncpy, stpncpy);
-
diff --git a/libc/string/strchrnul.c b/libc/string/strchrnul.c
deleted file mode 100644
index b3db8942..00000000
--- a/libc/string/strchrnul.c
+++ /dev/null
@@ -1,27 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-#include "libc.h"
-
-#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)
-{
- size_t *w, k;
-
- c = (unsigned char)c;
- if (!c) return (char *)s + strlen(s);
-
- for (; (uintptr_t)s % ALIGN; s++)
- if (!*s || *(unsigned char *)s == c) return (char *)s;
- k = ONES * c;
- for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
- for (s = (void *)w; *s && *(unsigned char *)s != c; s++);
- return (char *)s;
-}
-
-weak_alias(__strchrnul, strchrnul);
diff --git a/libc/string/strlcpy.c b/libc/string/strlcpy.c
deleted file mode 100644
index 9108af40..00000000
--- a/libc/string/strlcpy.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-#include "libc.h"
-
-#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;
- const size_t *ws;
-
- if (!n--) goto finish;
- 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;
- }
- }
- for (; n && (*d=*s); n--, s++, d++);
- *d = 0;
-finish:
- return d-d0 + strlen(s);
-}
diff --git a/libc/string/strlen.c b/libc/string/strlen.c
deleted file mode 100644
index 23fb650c..00000000
--- a/libc/string/strlen.c
+++ /dev/null
@@ -1,19 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-
-#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;
- const size_t *w;
- for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
- for (w = (const void *)s; !HASZERO(*w); w++);
- for (s = (const void *)w; *s; s++);
- return s-a;
-}
--
2.25.1

Nadav Har'El

unread,
Jul 29, 2020, 4:49:09 AM7/29/20
to Waldemar Kozaczuk, Osv Dev
Yes. I suspect that at some point a compiler warning warned about the precedence *maybe*
being wrong, so the extra parentheses were added to shut this compiler up.
As I noted earlier, it's probably not *us* that did this - the files in libc/ were taken from musl
at some point which is *not* the same time point that the musl/ directory is at (it's not even
necessarily one time point - different people copied different files from musl at different times).
So it might have been a change that happened in the Musl code based. If you're really curious
you can go look at the Musl git history.

--
You received this message because you are subscribed to the Google Groups "OSv Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osv-dev/20200727214145.16737-1-jwkozaczuk%40gmail.com.

Commit Bot

unread,
Jul 29, 2020, 4:52:52 AM7/29/20
to osv...@googlegroups.com, Waldemar Kozaczuk
From: Waldemar Kozaczuk <jwkoz...@gmail.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

libc: remove more string functions from libc/ in favor of the original musl ones

Just like the last two other libc related patches, this patch
also eliminates 7 more source files from libc/string/
in favor of the equivalent ones in musl/src/string/.

In this case those 7 files differed only like in this example:
```
diff musl/src/string/strchrnul.c libc/string/strchrnul.c
10c10
< #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
Message-Id: <20200727214145.1...@gmail.com>

---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# O./libc/string/strdup.cSv makefile
+# OSv makefile
#
# Copyright (C) 2015 Cloudius Systems, Ltd.
# This work is open source software, licensed under the terms of the
@@ -1556,8 +1556,8 @@ musl += string/bcmp.o
musl += string/bcopy.o
musl += string/bzero.o
musl += string/index.o
-libc += string/memccpy.o
-libc += string/memchr.o
+musl += string/memccpy.o
+musl += string/memchr.o
musl += string/memcmp.o
libc += string/memcpy.o
musl += string/memmem.o
@@ -1569,24 +1569,24 @@ libc += string/memset.o
libc += string/__memset_chk.o
libc += string/rawmemchr.o
musl += string/rindex.o
-libc += string/stpcpy.o
+musl += string/stpcpy.o
libc += string/__stpcpy_chk.o
-libc += string/stpncpy.o
+musl += string/stpncpy.o
musl += string/strcasecmp.o
musl += string/strcasestr.o
musl += string/strcat.o
libc += string/__strcat_chk.o
musl += string/strchr.o
-libc += string/strchrnul.o
+musl += string/strchrnul.o
musl += string/strcmp.o
musl += string/strcpy.o
libc += string/__strcpy_chk.o
musl += string/strcspn.o
musl += string/strdup.o
libc += string/strerror_r.o
libc += string/strlcat.o
-libc += string/strlcpy.o
-libc += string/strlen.o
+musl += string/strlcpy.o
+musl += string/strlen.o
musl += string/strncasecmp.o
musl += string/strncat.o
libc += string/__strncat_chk.o
diff --git a/libc/string/memccpy.c b/libc/string/memccpy.c
--- a/libc/string/memccpy.c
+++ b/libc/string/memccpy.c
--- a/libc/string/memchr.c
+++ b/libc/string/memchr.c
--- a/libc/string/stpcpy.c
+++ b/libc/string/stpcpy.c
--- a/libc/string/stpncpy.c
+++ b/libc/string/stpncpy.c
--- a/libc/string/strchrnul.c
+++ b/libc/string/strchrnul.c
--- a/libc/string/strlcpy.c
+++ b/libc/string/strlcpy.c
--- a/libc/string/strlen.c
+++ b/libc/string/strlen.c
Reply all
Reply to author
Forward
0 new messages