Skip to content

Commit e517dff

Browse files
committed
[libc][NFC] remove dependency on non standard ssize_t
`ssize_t` is from POSIX and is not standard unfortunately. Rewritting the code so it doesn't depend on it. Differential Revision: https://reviews.llvm.org/D94760
1 parent d4bb3ef commit e517dff

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

libc/src/string/memmove.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111
#include "src/stdlib/abs_utils.h"
1212
#include "src/string/memcpy.h"
1313
#include <stddef.h> // size_t, ptrdiff_t
14-
#include <unistd.h> // ssize_t
1514

1615
namespace __llvm_libc {
1716

18-
// src_m and dest_m might be the beginning or end.
19-
static inline void move_byte(unsigned char *dest_m, const unsigned char *src_m,
20-
size_t count, ssize_t direction) {
21-
for (ssize_t offset = 0; count; --count, offset += direction)
17+
static inline void move_byte_forward(char *dest_m, const char *src_m,
18+
size_t count) {
19+
for (size_t offset = 0; count; --count, ++offset)
20+
dest_m[offset] = src_m[offset];
21+
}
22+
23+
static inline void move_byte_backward(char *dest_m, const char *src_m,
24+
size_t count) {
25+
for (size_t offset = count - 1; count; --count, --offset)
2226
dest_m[offset] = src_m[offset];
2327
}
2428

2529
LLVM_LIBC_FUNCTION(void *, memmove,
2630
(void *dest, const void *src, size_t count)) {
27-
unsigned char *dest_c = reinterpret_cast<unsigned char *>(dest);
28-
const unsigned char *src_c = reinterpret_cast<const unsigned char *>(src);
31+
char *dest_c = reinterpret_cast<char *>(dest);
32+
const char *src_c = reinterpret_cast<const char *>(src);
2933

3034
// If the distance between src_c and dest_c is equal to or greater
3135
// than count (integer_abs(src_c - dest_c) >= count), they would not overlap.
@@ -50,11 +54,11 @@ LLVM_LIBC_FUNCTION(void *, memmove,
5054
// src_c : [___abcde_] [_abcde___]
5155
// dest_c: [_abc--___] [___--cde_]
5256

53-
// TODO: Optimize `move_byte(...)` function.
57+
// TODO: Optimize `move_byte_xxx(...)` functions.
5458
if (dest_c < src_c)
55-
move_byte(dest_c, src_c, count, /*pointer add*/ 1);
59+
move_byte_forward(dest_c, src_c, count);
5660
if (dest_c > src_c)
57-
move_byte(dest_c + count - 1, src_c + count - 1, count, /*pointer add*/ -1);
61+
move_byte_backward(dest_c, src_c, count);
5862
return dest;
5963
}
6064

0 commit comments

Comments
 (0)