[v12,12/31] string: Improve generic strnlen with memchr
Commit Message
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
@@ -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"
@@ -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>
@@ -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