Use memcpy rather than copying bytes one by one, for there might be large
size structs to move.
Details
- Reviewers
gchatelet sivachandra - Commits
- rG2423ec583761: [libc] Add memmove implementation.
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Sorry for the delay. And yes, you did the right thing, @gchatelet is the best reviewer for this. Overall, this LGTM, but I prefer @gchatelet to approve it.
My apologies for the late reply. I've made a bunch of comments.
libc/src/string/memmove.cpp | ||
---|---|---|
22 | assert(count <= SSIZE_MAX); @sivachandra I don't remember if we allow asserts in the library? | |
23 | nit: subscript operator is clearer IMHO dest_m[offset] = src_m[offset] | |
38 | Technically the pointer difference type is ptrdiff_t, you may want to rewrite this as: #include "src/stdlib/abs_utils.h" // __llvm_libc::integer_abs #include <cstdint> // ptrdiff_t, PTRDIFF_MAX ... assert(count <= PTRDIFF_MAX); if (__llvm_libc::integer_abs(src_c - dest_c) >= static_cast<ptrdiff_t>(count)) Also update the comment above. | |
53–56 | The code generated here seems suboptimal and quite large https://godbolt.org/z/fMnfv8. That said, we can keep this version for now and optimize later on. | |
libc/test/src/string/memmove_test.cpp | ||
43 | typo |
libc/src/string/memmove.cpp | ||
---|---|---|
26 | This line should be LLVM_LIBC_FUNCTION(void *, memmove, (void *dest, const void *src, size_t count)) { since we recently changed the macro that's used for external functions. |
LGTM, can you please wait for @sivachandra answer about using assert in code before submitting? Thx!
libc/src/string/memmove.cpp | ||
---|---|---|
32 | remove or update |
Sorry I missed this earlier. But, I don't see any assert now. Was it removed?
I don't think there is a hard and fast rule about assert but we should try to avoid them in order not to couple parts of the libc. For error conditions, if we align with the standards' requirements and add tests for them, I think that is sufficient.
But of course, if there is real benefit got by using an assert, we can use it.
libc/src/string/memmove.cpp | ||
---|---|---|
15 | I think you should add a DEPS for this header in the CMake file? |
libc/src/string/memmove.cpp | ||
---|---|---|
15 | Newline? |
I think you should add a DEPS for this header in the CMake file?