Skip to content

Commit bbece4b

Browse files
authored
[libc] replace for loops with a call to memcpy in File (#165219)
Addresses `TODO`s in file.cpp by replacing data copies via for loops with calls to inline_memcpy. Signed-off-by: Shreeyash Pandey <[email protected]>
1 parent 05e94c9 commit bbece4b

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

libc/src/__support/File/file.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/__support/CPP/span.h"
1616
#include "src/__support/libc_errno.h" // For error macros
1717
#include "src/__support/macros/config.h"
18+
#include "src/string/memory_utils/inline_memcpy.h"
1819

1920
namespace LIBC_NAMESPACE_DECL {
2021

@@ -85,9 +86,7 @@ FileIOResult File::write_unlocked_fbf(const uint8_t *data, size_t len) {
8586
cpp::span<uint8_t> bufref(static_cast<uint8_t *>(buf), bufsize);
8687

8788
// Copy the first piece into the buffer.
88-
// TODO: Replace the for loop below with a call to internal memcpy.
89-
for (size_t i = 0; i < primary.size(); ++i)
90-
bufref[pos + i] = primary[i];
89+
inline_memcpy(bufref.data() + pos, primary.data(), primary.size());
9190
pos += primary.size();
9291

9392
// If there is no remainder, we can return early, since the first piece has
@@ -115,9 +114,7 @@ FileIOResult File::write_unlocked_fbf(const uint8_t *data, size_t len) {
115114
// know that if the second piece has data in it then the buffer has been
116115
// flushed, meaning that pos is always 0.
117116
if (remainder.size() < bufsize) {
118-
// TODO: Replace the for loop below with a call to internal memcpy.
119-
for (size_t i = 0; i < remainder.size(); ++i)
120-
bufref[i] = remainder[i];
117+
inline_memcpy(bufref.data(), remainder.data(), remainder.size());
121118
pos = remainder.size();
122119
} else {
123120

@@ -209,9 +206,7 @@ size_t File::copy_data_from_buf(uint8_t *data, size_t len) {
209206
// available_data is never a wrapped around value.
210207
size_t available_data = read_limit - pos;
211208
if (len <= available_data) {
212-
// TODO: Replace the for loop below with a call to internal memcpy.
213-
for (size_t i = 0; i < len; ++i)
214-
dataref[i] = bufref[i + pos];
209+
inline_memcpy(dataref.data(), bufref.data() + pos, len);
215210
pos += len;
216211
return len;
217212
}
@@ -255,8 +250,7 @@ FileIOResult File::read_unlocked_fbf(uint8_t *data, size_t len) {
255250
size_t fetched_size = result.value;
256251
read_limit += fetched_size;
257252
size_t transfer_size = fetched_size >= to_fetch ? to_fetch : fetched_size;
258-
for (size_t i = 0; i < transfer_size; ++i)
259-
dataref[i] = buf[i];
253+
inline_memcpy(dataref.data(), buf, transfer_size);
260254
pos += transfer_size;
261255
if (result.has_error() || fetched_size < to_fetch) {
262256
if (!result.has_error())

0 commit comments

Comments
 (0)