-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Support _IONBF buffering for read_unlocked #120677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add the functions read_unlocked_nbf and read_unlocked_fbf.
@llvm/pr-subscribers-libc Author: Jack Huang (jackhong12) ChangesSupport _IONBF buffering for read_unlocked. Add the functions read_unlocked_nbf() and read_unlocked_fbf(). Fixes: #120155 Full diff: https://github.com/llvm/llvm-project/pull/120677.diff 2 Files Affected:
diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp
index 972249fef96bcf..cb8e1dff9fe96e 100644
--- a/libc/src/__support/File/file.cpp
+++ b/libc/src/__support/File/file.cpp
@@ -190,6 +190,17 @@ FileIOResult File::read_unlocked(void *data, size_t len) {
prev_op = FileOp::READ;
+ if (bufmode == _IONBF) { // unbuffered.
+ return read_unlocked_nbf(static_cast<uint8_t *>(data), len);
+ } else if (bufmode == _IOFBF) { // fully buffered
+ return read_unlocked_fbf(static_cast<uint8_t *>(data), len);
+ } else /*if (bufmode == _IOLBF) */ { // line buffered
+ // There is no line buffered mode for read. Use fully buffer instead.
+ return read_unlocked_fbf(static_cast<uint8_t *>(data), len);
+ }
+}
+
+FileIOResult File::read_unlocked_fbf(uint8_t *data, size_t len) {
cpp::span<uint8_t> bufref(static_cast<uint8_t *>(buf), bufsize);
cpp::span<uint8_t> dataref(static_cast<uint8_t *>(data), len);
@@ -245,6 +256,18 @@ FileIOResult File::read_unlocked(void *data, size_t len) {
return {transfer_size + available_data, result.error};
}
+FileIOResult File::read_unlocked_nbf(uint8_t *data, size_t len) {
+ auto result = platform_read(this, data, len);
+
+ if (result.has_error() || result < len) {
+ if (!result.has_error())
+ eof = true;
+ else
+ err = true;
+ }
+ return result;
+}
+
int File::ungetc_unlocked(int c) {
// There is no meaning to unget if:
// 1. You are trying to push back EOF.
diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index 42e1d11b4ab1a0..548f051a1b9f5c 100644
--- a/libc/src/__support/File/file.h
+++ b/libc/src/__support/File/file.h
@@ -280,6 +280,9 @@ class File {
FileIOResult write_unlocked_fbf(const uint8_t *data, size_t len);
FileIOResult write_unlocked_nbf(const uint8_t *data, size_t len);
+ FileIOResult read_unlocked_fbf(uint8_t *data, size_t len);
+ FileIOResult read_unlocked_nbf(uint8_t *data, size_t len);
+
constexpr void adjust_buf() {
if (read_allowed() && (buf == nullptr || bufsize == 0)) {
// We should allow atleast one ungetc operation.
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
For read_unlocked() with the _IONBUF mode, there is still an one-character buffer which is used to store the data inserted by ungetc(), so we always need to check the buffer first.
394dd06
to
26e848f
Compare
Sorry for the delays in code review, most of us have been off for the holidays in the US. @michaelrj-google should be back online this week to review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM, just a couple things to clean up before landing. Thanks for picking this up!
- The function copy_data_from_buf returns size_t. - Fix the spelling error. - Fix the incorrect data, error, in FileIOResult.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for doing this. Do you need me to merge this for you?
I can submit the code by myself. Let me check how to merge it. Thanks for your reviews! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/11402 Here is the relevant piece of the build log for the reference
|
Support _IONBF buffering for read_unlocked. Add the functions read_unlocked_nbf() and read_unlocked_fbf().
Fixes: #120155