Skip to content

Commit f96bd54

Browse files
addaleaxBridgeAR
authored andcommitted
fs: simplify FSReqBase slightly
Replace the `data_` member of `FSReqBase` by a boolean flag, because the pointer that `data()` returns is already available through the `buffer_` member, and set the default size for not requiring an extra allocation to a more reasonable value, as the input is usually something like a file path. PR-URL: #19174 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent f3257dd commit f96bd54

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/node_file.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class FSReqBase : public ReqWrap<uv_fs_t> {
3939
encoding_ = encoding;
4040

4141
if (data != nullptr) {
42-
CHECK_EQ(data_, nullptr);
42+
CHECK(!has_data_);
4343
buffer_.AllocateSufficientStorage(len + 1);
4444
buffer_.SetLengthAndZeroTerminate(len);
4545
memcpy(*buffer_, data, len);
46-
data_ = *buffer_;
46+
has_data_ = true;
4747
}
4848
}
4949

@@ -54,17 +54,19 @@ class FSReqBase : public ReqWrap<uv_fs_t> {
5454
virtual void SetReturnValue(const FunctionCallbackInfo<Value>& args) = 0;
5555

5656
const char* syscall() const { return syscall_; }
57-
const char* data() const { return data_; }
57+
const char* data() const { return has_data_ ? *buffer_ : nullptr; }
5858
enum encoding encoding() const { return encoding_; }
5959

6060
size_t self_size() const override { return sizeof(*this); }
6161

6262
private:
6363
enum encoding encoding_ = UTF8;
64+
bool has_data_ = false;
6465
const char* syscall_ = nullptr;
6566

66-
const char* data_ = nullptr;
67-
MaybeStackBuffer<char> buffer_;
67+
// Typically, the content of buffer_ is something like a file name, so
68+
// something around 64 bytes should be enough.
69+
MaybeStackBuffer<char, 64> buffer_;
6870

6971
DISALLOW_COPY_AND_ASSIGN(FSReqBase);
7072
};

0 commit comments

Comments
 (0)