lkml.org 
[lkml]   [2015]   [Jun]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH] small update for strlen, strnlen, use less cpu instructions
very small update to strlen and strnlen that now use less cpu 
instructions by
using a counter avoiding the memory addresses substraction to find the
length
of the string,

Orestes Leal Rodriguez.
--- lib/string.c.orig 2015-06-15 23:59:32.768346193 -0400
+++ lib/string.c 2015-06-16 00:11:45.791381539 -0400
@@ -17,6 +17,10 @@
* * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
* Matthew Hawkins <matt@mh.dropbear.id.au>
* - Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <lukes357@gmail.com>
+ * - strlen, strnlen: by using a single counter we use less cpu instructions
+ * by avoiding substracting the memory addresses before return
*/

#include <linux/types.h>
@@ -401,11 +405,11 @@ EXPORT_SYMBOL(strim);
*/
size_t strlen(const char *s)
{
- const char *sc;
+ size_t sz = 0;

- for (sc = s; *sc != '\0'; ++sc)
- /* nothing */;
- return sc - s;
+ for (; *s++ != '\0'; sz++)
+ /*empty */;
+ return sz;
}
EXPORT_SYMBOL(strlen);
#endif
@@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen);
*/
size_t strnlen(const char *s, size_t count)
{
- const char *sc;
+ size_t sz = 0;

- for (sc = s; count-- && *sc != '\0'; ++sc)
- /* nothing */;
- return sc - s;
+ for (; count-- && *s++ != '\0'; sz++)
+ /* empty */;
+ return sz;
}
+
EXPORT_SYMBOL(strnlen);
#endif
\
 
 \ /
  Last update: 2015-06-16 11:21    [W:0.034 / U:0.096 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site