linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2 1/2] lib/string: add memrchr function
@ 2019-02-14  6:02 Xiang Xiao
  2019-02-14  6:02 ` [PATCH V2 2/2] rpmsg: add syslog redirection driver Xiang Xiao
  2019-02-14 12:53 ` [PATCH V2 1/2] lib/string: add memrchr function Andy Shevchenko
  0 siblings, 2 replies; 8+ messages in thread
From: Xiang Xiao @ 2019-02-14  6:02 UTC (permalink / raw)
  To: rdunlap, gregkh, alexander.shishkin, andriy.shevchenko, ohad,
	bjorn.andersson, wendy.liang, arnaud.pouliquen, kumar.gala,
	linux-remoteproc, linux-kernel
  Cc: 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.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
---
 include/linux/string.h |  3 +++
 lib/string.c           | 23 +++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 7927b87..915c617 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -167,6 +167,9 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+#ifndef __HAVE_ARCH_MEMRCHR
+void *memrchr(const void *s, int c, size_t n);
+#endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
 
diff --git a/lib/string.c b/lib/string.c
index 38e4ca0..595e0b3 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -964,6 +964,29 @@ void *memchr(const void *s, int c, size_t n)
 EXPORT_SYMBOL(memchr);
 #endif
 
+#ifndef __HAVE_ARCH_MEMRCHR
+/**
+ * memrchr - Find the last character in an area of memory.
+ * @s: The memory area
+ * @c: The byte to search for
+ * @n: The size of the area.
+ *
+ * Return: 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);
+#endif
+
 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
 {
 	while (bytes) {
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-02-16 16:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-14  6:02 [PATCH V2 1/2] lib/string: add memrchr function Xiang Xiao
2019-02-14  6:02 ` [PATCH V2 2/2] rpmsg: add syslog redirection driver Xiang Xiao
2019-02-14 13:09   ` Andy Shevchenko
2019-02-14 13:11     ` Andy Shevchenko
2019-02-14 16:31     ` xiang xiao
2019-02-15 15:06       ` Andy Shevchenko
2019-02-16 16:52         ` xiang xiao
2019-02-14 12:53 ` [PATCH V2 1/2] lib/string: add memrchr function Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).