Skip to contentSkip to conversation

lib/string: add memrchr function

PATCH Superseded Xiang Xiao 2 participants 9 messages Feb 12, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xiang Xiao

author
#0 lore raw

PATCH 1/1 lib/string: add memrchr function

Here is the detailed description for memrchr:
https://linux.die.net/man/3/memrchr

---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
include/linux/string.h +1 -0
index 7927b87..f380f4b 100644
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
 
lib/string.c +21 -0
index 38e4ca0..92914f6 100644
@@ -964,6 +964,27 @@ void *memchr(const void *s, int c, size_t n)
 EXPORT_SYMBOL(memchr);
 #endif
 
+/**
+ * memrchr - Find a character in an area of memory.
+ * @s: The memory area
+ * @c: The byte to search for
+ * @n: The size of the area.
+ *
+ * returns the address of the last occurrence of @c, or %NULL
+ * if @c is not found
+ */
+void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *p = s + n;
+
+	while (n-- != 0) {
+		if ((unsigned char)c == *--p)
+			return (void *)p;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL(memrchr);
+
 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
 {
 	while (bytes) {
Signature · 1 line
2.7.4
#1 lore raw
Quoted 2 lines · Xiang Xiao

Here is the detailed description for memrchr:
https://linux.die.net/man/3/memrchr

Please put it in the changelog, as web pages move and go away :(

Quoted 12 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
include/linux/string.h +0 -0
index 7927b87..f380f4b 100644

include/linux/string.h static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) · line 170

Quoted 4 lines
include/linux/string.h +1 -0
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);

Who is going to use this?

thanks,

greg k-h

author
#2 lore raw
Quoted 6 lines · Greg KH
Quoted 2 lines · Xiang Xiao

Here is the detailed description for memrchr:
https://linux.die.net/man/3/memrchr

Please put it in the changelog, as web pages move and go away :(

Sure.

Quoted 8 lines
Quoted 8 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
Quoted 11 lines

include/linux/string.h static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) · line 170

Quoted 9 lines
include/linux/string.h +1 -0
index 7927b87..f380f4b 100644
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
      memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);

Who is going to use this?

This patch will use memrchr:
https://github.com/thesofproject/linux/pull/177/commits/a0b7009fede5552dc98733f2996a8140bff62455
I am preparing to upstream it.

Quoted 4 lines

thanks,

greg k-h

#3 lore raw
Quoted 30 lines · xiang xiao
Quoted 6 lines · Greg KH
Quoted 2 lines · Xiang Xiao

Here is the detailed description for memrchr:
https://linux.die.net/man/3/memrchr

Please put it in the changelog, as web pages move and go away :(

Sure.

Quoted 8 lines
Quoted 8 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
Quoted 11 lines

include/linux/string.h static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) · line 170

Quoted 9 lines
include/linux/string.h +1 -0
index 7927b87..f380f4b 100644
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
      memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);

Who is going to use this?

Quoted 3 lines

This patch will use memrchr:
https://github.com/thesofproject/linux/pull/177/commits/a0b7009fede5552dc98733f2996a8140bff62455
I am preparing to upstream it.

Great, make it part of the patch series for your driver, no need to submit it separately.

thanks,

greg k-h

author
#4 lore raw
Quoted 32 lines · Greg KH
Quoted 30 lines · xiang xiao
Quoted 6 lines · Greg KH
Quoted 2 lines · Xiang Xiao

Here is the detailed description for memrchr:
https://linux.die.net/man/3/memrchr

Please put it in the changelog, as web pages move and go away :(

Sure.

Quoted 19 lines
Quoted 13 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
include/linux/string.h +0 -0
index 7927b87..f380f4b 100644

include/linux/string.h static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) · line 170

Quoted 4 lines
include/linux/string.h +1 -0
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
      memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);

Who is going to use this?

Quoted 7 lines
Quoted 3 lines

This patch will use memrchr:
https://github.com/thesofproject/linux/pull/177/commits/a0b7009fede5552dc98733f2996a8140bff62455
I am preparing to upstream it.

Great, make it part of the patch series for your driver, no need to submit it separately.

Ok, I will merge the depend patch into one series.

Quoted 3 lines

thanks,

greg k-h

author
#5 lore raw

PATCH v2 1/1 lib/string: add memrchr function

In reply to Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
include/linux/string.h +1 -0
index 7927b87..f380f4b 100644
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
 
lib/string.c +21 -0
index 38e4ca0..92914f6 100644
@@ -964,6 +964,27 @@ void *memchr(const void *s, int c, size_t n)
 EXPORT_SYMBOL(memchr);
 #endif
 
+/**
+ * memrchr - Find a character in an area of memory.
+ * @s: The memory area
+ * @c: The byte to search for
+ * @n: The size of the area.
+ *
+ * returns the address of the last occurrence of @c, or %NULL
+ * if @c is not found
+ */
+void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *p = s + n;
+
+	while (n-- != 0) {
+		if ((unsigned char)c == *--p)
+			return (void *)p;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL(memrchr);
+
 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
 {
 	while (bytes) {
Signature · 1 line
2.7.4
#6 lore raw
Quoted 12 lines · Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

Quoted 5 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

Don't add new apis without any users.

thanks,

greg k-h

#7 lore raw
In reply to Xiang Xiao
Quoted 23 lines · Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
include/linux/string.h +0 -0
index 7927b87..f380f4b 100644

include/linux/string.h static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) · line 172

Quoted 6 lines
include/linux/string.h +1 -0
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);

Also, if you really need this, why not also provide the arch-specific versions as well?

thanks,

greg k-h

author
#8 lore raw
Quoted 20 lines · Greg KH
Quoted 18 lines · Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
Quoted 15 lines

include/linux/string.h static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) · line 172

Quoted 11 lines
include/linux/string.h +1 -0
index 7927b87..f380f4b 100644
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
      memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);

Also, if you really need this, why not also provide the arch-specific versions as well?

Good point, I will make the arch-specific overwrite possible.

Quoted 3 lines

thanks,

greg k-h