[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210702123153.14093-3-mcroce@linux.microsoft.com>
Date: Fri, 2 Jul 2021 14:31:52 +0200
From: Matteo Croce <mcroce@...ux.microsoft.com>
To: linux-kernel@...r.kernel.org, Nick Kossifidis <mick@....forth.gr>,
Guo Ren <guoren@...nel.org>,
Christoph Hellwig <hch@...radead.org>,
David Laight <David.Laight@...lab.com>,
Palmer Dabbelt <palmer@...belt.com>,
Emil Renner Berthing <kernel@...il.dk>,
Drew Fustini <drew@...gleboard.org>
Cc: linux-arch@...r.kernel.org,
Andrew Morton <akpm@...ux-foundation.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
linux-riscv@...ts.infradead.org
Subject: [PATCH v2 2/3] lib/string: optimized memmove
From: Matteo Croce <mcroce@...rosoft.com>
When the destination buffer is before the source one, or when the
buffers doesn't overlap, it's safe to use memcpy() instead, which is
optimized to use a bigger data size possible.
This "optimization" only covers a common case. In future, proper code
which does the same thing as memcpy() does but backwards can be done.
Signed-off-by: Matteo Croce <mcroce@...rosoft.com>
---
lib/string.c | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
diff --git a/lib/string.c b/lib/string.c
index caeef4264c43..108b83c34cec 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -975,19 +975,13 @@ EXPORT_SYMBOL(memcpy);
*/
void *memmove(void *dest, const void *src, size_t count)
{
- char *tmp;
- const char *s;
+ if (dest < src || src + count <= dest)
+ return memcpy(dest, src, count);
+
+ if (dest > src) {
+ const char *s = src + count;
+ char *tmp = dest + count;
- if (dest <= src) {
- tmp = dest;
- s = src;
- while (count--)
- *tmp++ = *s++;
- } else {
- tmp = dest;
- tmp += count;
- s = src;
- s += count;
while (count--)
*--tmp = *--s;
}
--
2.31.1
Powered by blists - more mailing lists