Skip to content

Commit e21ca1a

Browse files
michaelrj-googlerorth
authored andcommitted
[libc] clean up string_utils memory functions (llvm#143031)
The string_utils.h file previously included both memcpy and bzero. There were no uses of bzero, and only one use of memcpy which was replaced with __builtin_memcpy. Also fix strsep which was broken by this change, fix a useless assert of "sizeof(char) == sizeof(cpp::byte)", and update the bazel.
1 parent 3b59e90 commit e21ca1a

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

libc/src/string/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ add_header_library(
1515
HDRS
1616
string_utils.h
1717
DEPENDS
18-
.memory_utils.inline_bzero
19-
.memory_utils.inline_memcpy
2018
libc.hdr.types.size_t
21-
libc.include.stdlib
19+
libc.hdr.limits_macros
2220
libc.src.__support.CPP.bitset
2321
libc.src.__support.CPP.type_traits
2422
libc.src.__support.common

libc/src/string/string_utils.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
#ifndef LLVM_LIBC_SRC_STRING_STRING_UTILS_H
1515
#define LLVM_LIBC_SRC_STRING_STRING_UTILS_H
1616

17+
#include "hdr/limits_macros.h"
1718
#include "hdr/types/size_t.h"
1819
#include "src/__support/CPP/bitset.h"
1920
#include "src/__support/CPP/type_traits.h" // cpp::is_same_v
2021
#include "src/__support/macros/config.h"
2122
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
22-
#include "src/string/memory_utils/inline_bzero.h"
23-
#include "src/string/memory_utils/inline_memcpy.h"
2423

2524
namespace LIBC_NAMESPACE_DECL {
2625
namespace internal {
2726

2827
template <typename Word> LIBC_INLINE constexpr Word repeat_byte(Word byte) {
29-
constexpr size_t BITS_IN_BYTE = 8;
28+
static_assert(CHAR_BIT == 8, "repeat_byte assumes a byte is 8 bits.");
29+
constexpr size_t BITS_IN_BYTE = CHAR_BIT;
3030
constexpr size_t BYTE_MASK = 0xff;
3131
Word result = 0;
3232
byte = byte & BYTE_MASK;
@@ -189,8 +189,7 @@ LIBC_INLINE char *string_token(char *__restrict src,
189189
if (LIBC_UNLIKELY(src == nullptr && ((src = *saveptr) == nullptr)))
190190
return nullptr;
191191

192-
static_assert(sizeof(char) == sizeof(cpp::byte),
193-
"bitset of 256 assumes char is 8 bits");
192+
static_assert(CHAR_BIT == 8, "bitset of 256 assumes char is 8 bits");
194193
cpp::bitset<256> delimiter_set;
195194
for (; *delimiter_string != '\0'; ++delimiter_string)
196195
delimiter_set.set(static_cast<size_t>(*delimiter_string));
@@ -220,7 +219,7 @@ LIBC_INLINE size_t strlcpy(char *__restrict dst, const char *__restrict src,
220219
if (!size)
221220
return len;
222221
size_t n = len < size - 1 ? len : size - 1;
223-
inline_memcpy(dst, src, n);
222+
__builtin_memcpy(dst, src, n);
224223
dst[n] = '\0';
225224
return len;
226225
}

libc/src/string/strsep.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "src/__support/macros/null_check.h"
1313
#include "src/string/string_utils.h"
1414

15+
#include "src/__support/common.h"
16+
1517
namespace LIBC_NAMESPACE_DECL {
1618

1719
LLVM_LIBC_FUNCTION(char *, strsep,

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4188,9 +4188,10 @@ libc_support_library(
41884188
deps = [
41894189
":__support_common",
41904190
":__support_cpp_bitset",
4191+
":__support_cpp_type_traits",
41914192
":__support_macros_optimization",
4193+
":hdr_limits_macros",
41924194
":llvm_libc_types_size_t",
4193-
":string_memory_utils",
41944195
":types_size_t",
41954196
],
41964197
)

0 commit comments

Comments
 (0)