Skip to content

Commit

Permalink
fs: simplify FSReqBase slightly
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax authored and BridgeAR committed Mar 11, 2018
1 parent f3257dd commit f96bd54
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class FSReqBase : public ReqWrap<uv_fs_t> {
encoding_ = encoding;

if (data != nullptr) {
CHECK_EQ(data_, nullptr);
CHECK(!has_data_);
buffer_.AllocateSufficientStorage(len + 1);
buffer_.SetLengthAndZeroTerminate(len);
memcpy(*buffer_, data, len);
data_ = *buffer_;
has_data_ = true;
}
}

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

const char* syscall() const { return syscall_; }
const char* data() const { return data_; }
const char* data() const { return has_data_ ? *buffer_ : nullptr; }
enum encoding encoding() const { return encoding_; }

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

private:
enum encoding encoding_ = UTF8;
bool has_data_ = false;
const char* syscall_ = nullptr;

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

DISALLOW_COPY_AND_ASSIGN(FSReqBase);
};
Expand Down

0 comments on commit f96bd54

Please sign in to comment.