11
11
#include " src/stdlib/abs_utils.h"
12
12
#include " src/string/memcpy.h"
13
13
#include < stddef.h> // size_t, ptrdiff_t
14
- #include < unistd.h> // ssize_t
15
14
16
15
namespace __llvm_libc {
17
16
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)
22
26
dest_m[offset] = src_m[offset];
23
27
}
24
28
25
29
LLVM_LIBC_FUNCTION (void *, memmove,
26
30
(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);
29
33
30
34
// If the distance between src_c and dest_c is equal to or greater
31
35
// than count (integer_abs(src_c - dest_c) >= count), they would not overlap.
@@ -50,11 +54,11 @@ LLVM_LIBC_FUNCTION(void *, memmove,
50
54
// src_c : [___abcde_] [_abcde___]
51
55
// dest_c: [_abc--___] [___--cde_]
52
56
53
- // TODO: Optimize `move_byte (...)` function .
57
+ // TODO: Optimize `move_byte_xxx (...)` functions .
54
58
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);
56
60
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);
58
62
return dest;
59
63
}
60
64
0 commit comments