diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt --- a/libc/src/string/CMakeLists.txt +++ b/libc/src/string/CMakeLists.txt @@ -24,7 +24,7 @@ HDRS mempcpy.h DEPENDS - libc.src.string.memcpy + .memory_utils.memcpy_implementation ) add_entrypoint_object( @@ -45,7 +45,7 @@ memmove.h DEPENDS libc.src.__support.integer_operations - libc.src.string.memcpy + .memory_utils.memcpy_implementation ) add_entrypoint_object( @@ -74,7 +74,7 @@ HDRS stpncpy.h DEPENDS - .bzero + .memory_utils.memset_implementation ) add_entrypoint_object( @@ -111,7 +111,7 @@ HDRS strcpy.h DEPENDS - .memcpy + .memory_utils.memcpy_implementation .string_utils ) @@ -132,7 +132,7 @@ HDRS strdup.h DEPENDS - .memcpy + .memory_utils.memcpy_implementation .string_utils libc.include.stdlib ) @@ -181,7 +181,7 @@ HDRS strndup.h DEPENDS - .memcpy + .memory_utils.memcpy_implementation .string_utils libc.include.stdlib ) @@ -317,7 +317,7 @@ SRCS ${LIBC_SOURCE_DIR}/src/string/bzero.cpp HDRS ${LIBC_SOURCE_DIR}/src/string/bzero.h DEPENDS - .memory_utils.memory_utils + .memory_utils.memset_implementation libc.include.string COMPILE_OPTIONS -fno-builtin-bzero @@ -346,7 +346,7 @@ SRCS ${LIBC_SOURCE_DIR}/src/string/memcmp.cpp HDRS ${LIBC_SOURCE_DIR}/src/string/memcmp.h DEPENDS - .memory_utils.memory_utils + .memory_utils.memcmp_implementation libc.include.string COMPILE_OPTIONS -fno-builtin-memcmp @@ -378,7 +378,7 @@ SRCS ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp HDRS ${LIBC_SOURCE_DIR}/src/string/memcpy.h DEPENDS - .memory_utils.memory_utils + .memory_utils.memcpy_implementation libc.include.string COMPILE_OPTIONS -fno-builtin-memcpy @@ -413,7 +413,7 @@ SRCS ${LIBC_SOURCE_DIR}/src/string/memset.cpp HDRS ${LIBC_SOURCE_DIR}/src/string/memset.h DEPENDS - .memory_utils.memory_utils + .memory_utils.memset_implementation libc.include.string COMPILE_OPTIONS -fno-builtin-memset diff --git a/libc/src/string/memmove.cpp b/libc/src/string/memmove.cpp --- a/libc/src/string/memmove.cpp +++ b/libc/src/string/memmove.cpp @@ -10,7 +10,7 @@ #include "src/__support/common.h" #include "src/__support/integer_operations.h" -#include "src/string/memcpy.h" +#include "src/string/memory_utils/memcpy_implementations.h" #include // size_t, ptrdiff_t namespace __llvm_libc { @@ -40,8 +40,11 @@ // dest_c:[_____yz_] [___yz___] [__yz____] // Call `memcpy` if `src_c` and `dest_c` do not overlap. - if (__llvm_libc::integerAbs(src_c - dest_c) >= static_cast(count)) - return __llvm_libc::memcpy(dest_c, src_c, count); + if (__llvm_libc::integerAbs(src_c - dest_c) >= + static_cast(count)) { + inline_memcpy(dest_c, src_c, count); + return dest_c; + } // Overlapping cases. // If `dest_c` starts before `src_c` (dest_c < src_c), copy diff --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt --- a/libc/src/string/memory_utils/CMakeLists.txt +++ b/libc/src/string/memory_utils/CMakeLists.txt @@ -4,3 +4,27 @@ utils.h elements.h ) + +add_header_library( + memcpy_implementation + HDRS + memcpy_implementations.h + DEPS + .memory_utils +) + +add_header_library( + memcmp_implementation + HDRS + memcmp_implementations.h + DEPS + .memory_utils +) + +add_header_library( + memset_implementation + HDRS + memset_implementations.h + DEPS + .memory_utils +) diff --git a/libc/src/string/mempcpy.cpp b/libc/src/string/mempcpy.cpp --- a/libc/src/string/mempcpy.cpp +++ b/libc/src/string/mempcpy.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/string/mempcpy.h" -#include "src/string/memcpy.h" +#include "src/string/memory_utils/memcpy_implementations.h" #include "src/__support/common.h" #include // For size_t. @@ -17,10 +17,11 @@ LLVM_LIBC_FUNCTION(void *, mempcpy, (void *__restrict dest, const void *__restrict src, size_t count)) { - void *result = __llvm_libc::memcpy(dest, src, count); - return result == nullptr - ? result - : static_cast(static_cast(result) + count); + if (dest == nullptr) + return nullptr; + char *result = reinterpret_cast(dest); + inline_memcpy(result, reinterpret_cast(src), count); + return static_cast(result + count); } } // namespace __llvm_libc diff --git a/libc/src/string/stpncpy.cpp b/libc/src/string/stpncpy.cpp --- a/libc/src/string/stpncpy.cpp +++ b/libc/src/string/stpncpy.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/string/stpncpy.h" -#include "src/string/bzero.h" +#include "src/string/memory_utils/memset_implementations.h" #include "src/__support/common.h" @@ -22,7 +22,7 @@ dest[i] = src[i]; // When n>strlen(src), n-strlen(src) \0 are appended. if (n > i) - __llvm_libc::bzero(dest + i, n - i); + inline_memset(dest + i, 0, n - i); return dest + i; } diff --git a/libc/src/string/strcpy.cpp b/libc/src/string/strcpy.cpp --- a/libc/src/string/strcpy.cpp +++ b/libc/src/string/strcpy.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/string/strcpy.h" -#include "src/string/memcpy.h" +#include "src/string/memory_utils/memcpy_implementations.h" #include "src/string/string_utils.h" #include "src/__support/common.h" @@ -18,7 +18,7 @@ LLVM_LIBC_FUNCTION(char *, strcpy, (char *__restrict dest, const char *__restrict src)) { size_t size = internal::string_length(src) + 1; - char *result = reinterpret_cast(__llvm_libc::memcpy(dest, src, size)); + inline_memcpy(dest, src, size); // In many libc uses, we do not want memcpy to be instrumented. Hence, // we mark the destination as initialized. @@ -27,9 +27,9 @@ // generate calls to memcpy. If the sanitizer business logic ends up with a // compiler generated call to memcpy which is instrumented, then it will // break the sanitizers. - SANITIZER_MEMORY_INITIALIZED(result, size); + SANITIZER_MEMORY_INITIALIZED(dst, size); - return result; + return dest; } } // namespace __llvm_libc diff --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp --- a/libc/src/string/strdup.cpp +++ b/libc/src/string/strdup.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/string/strdup.h" -#include "src/string/memcpy.h" +#include "src/string/memory_utils/memcpy_implementations.h" #include "src/string/string_utils.h" #include "src/__support/common.h" @@ -25,8 +25,8 @@ if (dest == nullptr) { return nullptr; } - char *result = reinterpret_cast(__llvm_libc::memcpy(dest, src, len)); - return result; + inline_memcpy(dest, src, len); + return dest; } } // namespace __llvm_libc diff --git a/libc/src/string/strndup.cpp b/libc/src/string/strndup.cpp --- a/libc/src/string/strndup.cpp +++ b/libc/src/string/strndup.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/string/strndup.h" -#include "src/string/memcpy.h" +#include "src/string/memory_utils/memcpy_implementations.h" #include "src/string/string_utils.h" #include "src/__support/common.h" @@ -26,10 +26,9 @@ char *dest = reinterpret_cast(::malloc(len + 1)); // NOLINT if (dest == nullptr) return nullptr; - char *result = - reinterpret_cast(__llvm_libc::memcpy(dest, src, len + 1)); - result[len] = '\0'; - return result; + inline_memcpy(dest, src, len + 1); + dest[len] = '\0'; + return dest; } } // namespace __llvm_libc