diff mbox series

[v12,12/31] string: Improve generic strnlen with memchr

Message ID PAWPR08MB89820983A878A96098A138B783D79@PAWPR08MB8982.eurprd08.prod.outlook.com
State New
Headers show
Series None | expand

Commit Message

Wilco Dijkstra Feb. 3, 2023, 1:18 p.m. UTC
Hi Adhemerval,
 
 /* Find the length of S, but scan at most MAXLEN characters.  If no
    '\0' terminator is found in that many characters, return MAXLEN.  */
@@ -32,134 +27,12 @@
 size_t
 __strnlen (const char *str, size_t maxlen)
 {
+  const char *found = memchr (str, '\0', maxlen);
+  return found ? found - str : maxlen;
 }

OK. Indeed, using memchr is better by default since it is optimized on
many targets and it ensures the useful property that strnlen is at least
as fast as memchr.


 #ifndef STRNLEN
-libc_hidden_def (__strnlen)
 weak_alias (__strnlen, strnlen)
-#endif
+libc_hidden_def (__strnlen)
 libc_hidden_def (strnlen)
+#endif
diff mbox series

Patch

diff --git a/sysdeps/i386/i686/multiarch/strnlen-c.c b/sysdeps/i386/i686/multiarch/strnlen-c.c
index 351e939a93..beb0350d53 100644
--- a/sysdeps/i386/i686/multiarch/strnlen-c.c
+++ b/sysdeps/i386/i686/multiarch/strnlen-c.c
@@ -1,10 +1,10 @@ 
 #define STRNLEN  __strnlen_ia32
+#include <string/strnlen.c>
+
 #ifdef SHARED
-# undef libc_hidden_def
-# define libc_hidden_def(name)  \
-    __hidden_ver1 (__strnlen_ia32, __GI_strnlen, __strnlen_ia32); \
-    strong_alias (__strnlen_ia32, __strnlen_ia32_1); \
-    __hidden_ver1 (__strnlen_ia32_1, __GI___strnlen, __strnlen_ia32_1);
+/* Alias for internal symbol to avoid PLT generation, it redirects the
+   libc_hidden_def (__strnlen/strlen) to default implementation.  */
+__hidden_ver1 (__strnlen_ia32, __GI_strnlen, __strnlen_ia32);
+strong_alias (__strnlen_ia32, __strnlen_ia32_1);
+__hidden_ver1 (__strnlen_ia32_1, __GI___strnlen, __strnlen_ia32_1);
 #endif
-
-#include "string/strnlen.c"
diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
index 957b9b99e8..2ca1cd7181 100644
--- a/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
+++ b/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
@@ -17,12 +17,12 @@ 
    <https://www.gnu.org/licenses/>.  */
 
 #define STRNLEN  __strnlen_ppc
+#include <string/strnlen.c>
+
 #ifdef SHARED
-# undef libc_hidden_def
-# define libc_hidden_def(name)  \
-    __hidden_ver1 (__strnlen_ppc, __GI_strnlen, __strnlen_ppc); \
-    strong_alias (__strnlen_ppc, __strnlen_ppc_1); \
-    __hidden_ver1 (__strnlen_ppc_1, __GI___strnlen, __strnlen_ppc_1);
+/* Alias for internal symbol to avoid PLT generation, it redirects the
+   libc_hidden_def (__strnlen/strlen) to default implementation.  */
+__hidden_ver1 (__strnlen_ppc, __GI_strnlen, __strnlen_ppc); \
+strong_alias (__strnlen_ppc, __strnlen_ppc_1); \
+__hidden_ver1 (__strnlen_ppc_1, __GI___strnlen, __strnlen_ppc_1);
 #endif
-
-#include <string/strnlen.c>
diff --git a/sysdeps/s390/strnlen-c.c b/sysdeps/s390/strnlen-c.c
index 172fcc7caa..95156a0ff5 100644
--- a/sysdeps/s390/strnlen-c.c
+++ b/sysdeps/s390/strnlen-c.c
@@ -21,14 +21,16 @@ 
 #if HAVE_STRNLEN_C
 # if HAVE_STRNLEN_IFUNC
 #  define STRNLEN STRNLEN_C
+# endif
+
+# include <string/strnlen.c>
+
+# if HAVE_STRNLEN_IFUNC
 #  if defined SHARED && IS_IN (libc)
-#   undef libc_hidden_def
-#   define libc_hidden_def(name)					\
-  __hidden_ver1 (__strnlen_c, __GI_strnlen, __strnlen_c);	\
-  strong_alias (__strnlen_c, __strnlen_c_1);			\
-  __hidden_ver1 (__strnlen_c_1, __GI___strnlen, __strnlen_c_1);
+__hidden_ver1 (__strnlen_c, __GI_strnlen, __strnlen_c);
+strong_alias (__strnlen_c, __strnlen_c_1);
+__hidden_ver1 (__strnlen_c_1, __GI___strnlen, __strnlen_c_1);
 #  endif
 # endif
 
-# include <string/strnlen.c>
 #endif