[libc-commits] [PATCH] D94760: [libc][NFC] remove dependency on non standard ssize_t

Guillaume Chatelet via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Jan 15 03:10:42 PST 2021


gchatelet created this revision.
gchatelet added reviewers: sivachandra, cheng.w.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added a project: libc-project.
gchatelet requested review of this revision.

`ssize_t` is from POSIX and is not standard unfortunately.
Rewritting the code so it doesn't depend on it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94760

Files:
  libc/src/string/memmove.cpp


Index: libc/src/string/memmove.cpp
===================================================================
--- libc/src/string/memmove.cpp
+++ libc/src/string/memmove.cpp
@@ -11,14 +11,19 @@
 #include "src/stdlib/abs_utils.h"
 #include "src/string/memcpy.h"
 #include <stddef.h> // size_t, ptrdiff_t
-#include <unistd.h> // ssize_t
 
 namespace __llvm_libc {
 
-// src_m and dest_m might be the beginning or end.
-static inline void move_byte(unsigned char *dest_m, const unsigned char *src_m,
-                             size_t count, ssize_t direction) {
-  for (ssize_t offset = 0; count; --count, offset += direction)
+static inline void move_byte_forward(unsigned char *dest_m,
+                                     const unsigned char *src_m, size_t count) {
+  for (size_t offset = 0; count; --count, ++offset)
+    dest_m[offset] = src_m[offset];
+}
+
+static inline void move_byte_backward(unsigned char *dest_m,
+                                      const unsigned char *src_m,
+                                      size_t count) {
+  for (size_t offset = count - 1; count; --count, --offset)
     dest_m[offset] = src_m[offset];
 }
 
@@ -50,11 +55,11 @@
   // src_c : [___abcde_]  [_abcde___]
   // dest_c: [_abc--___]  [___--cde_]
 
-  // TODO: Optimize `move_byte(...)` function.
+  // TODO: Optimize `move_byte_xxx(...)` functions.
   if (dest_c < src_c)
-    move_byte(dest_c, src_c, count, /*pointer add*/ 1);
+    move_byte_forward(dest_c, src_c, count);
   if (dest_c > src_c)
-    move_byte(dest_c + count - 1, src_c + count - 1, count, /*pointer add*/ -1);
+    move_byte_backward(dest_c, src_c, count);
   return dest;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94760.316890.patch
Type: text/x-patch
Size: 1645 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210115/5d8ed9e6/attachment-0001.bin>


More information about the libc-commits mailing list