Skip to content

Commit d2b80b8

Browse files
committed
src: clean up FSReqWrap
Move the 'free FSReqWrap data?' logic into the class itself.
1 parent ffc5d83 commit d2b80b8

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

src/node_file.cc

+34-31
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@ using v8::Value;
6363

6464
class FSReqWrap: public ReqWrap<uv_fs_t> {
6565
public:
66-
explicit FSReqWrap(const char* syscall)
67-
: must_free_(false)
68-
, data_(NULL)
69-
, syscall_(syscall) {
66+
explicit FSReqWrap(const char* syscall, char* data = NULL)
67+
: syscall_(syscall)
68+
, data_(data) {
69+
}
70+
71+
void ReleaseEarly() {
72+
if (data_ == NULL) return;
73+
delete[] data_;
74+
data_ = NULL;
7075
}
7176

7277
const char* syscall() { return syscall_; }
73-
bool must_free_; // request is responsible for free'ing memory oncomplete
74-
char* data_;
7578

7679
private:
7780
const char* syscall_;
81+
char* data_;
7882
};
7983

8084

@@ -102,10 +106,7 @@ static void After(uv_fs_t *req) {
102106

103107
FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data);
104108
assert(&req_wrap->req_ == req);
105-
106-
// check if data needs to be cleaned
107-
if (req_wrap->must_free_ == true)
108-
delete[] req_wrap->data_;
109+
req_wrap->ReleaseEarly(); // Free memory that's no longer used now.
109110

110111
// there is always at least one argument. "error"
111112
int argc = 1;
@@ -729,7 +730,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
729730
char* buf = NULL;
730731
int64_t pos;
731732
size_t len;
732-
bool must_free_ = false;
733+
bool must_free = false;
733734

734735
// will assign buf and len if string was external
735736
if (!StringBytes::GetExternalParts(string,
@@ -741,33 +742,35 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
741742
// StorageSize may return too large a char, so correct the actual length
742743
// by the write size
743744
len = StringBytes::Write(buf, len, args[1], enc);
744-
must_free_ = true;
745+
must_free = true;
745746
}
746747
pos = GET_OFFSET(args[2]);
747748
cb = args[4];
748749

749-
if (cb->IsFunction()) {
750-
FSReqWrap* req_wrap = new FSReqWrap("write");
751-
int err = uv_fs_write(uv_default_loop(), &req_wrap->req_,
752-
fd, buf, len, pos, After);
753-
req_wrap->object()->Set(oncomplete_sym, cb);
754-
req_wrap->must_free_ = must_free_;
755-
req_wrap->Dispatched();
756-
req_wrap->data_ = buf;
757-
if (err < 0) {
758-
uv_fs_t* req = &req_wrap->req_;
759-
req->result = err;
760-
req->path = NULL;
761-
After(req);
762-
}
763-
return args.GetReturnValue().Set(req_wrap->persistent());
750+
if (!cb->IsFunction()) {
751+
SYNC_CALL(write, NULL, fd, buf, len, pos)
752+
if (must_free) delete[] buf;
753+
return args.GetReturnValue().Set(SYNC_RESULT);
764754
}
765755

766-
SYNC_CALL(write, NULL, fd, buf, len, pos)
767-
args.GetReturnValue().Set(SYNC_RESULT);
756+
FSReqWrap* req_wrap = new FSReqWrap("write", must_free ? buf : NULL);
757+
int err = uv_fs_write(uv_default_loop(),
758+
&req_wrap->req_,
759+
fd,
760+
buf,
761+
len,
762+
pos,
763+
After);
764+
req_wrap->object()->Set(oncomplete_sym, cb);
765+
req_wrap->Dispatched();
766+
if (err < 0) {
767+
uv_fs_t* req = &req_wrap->req_;
768+
req->result = err;
769+
req->path = NULL;
770+
After(req);
771+
}
768772

769-
if (must_free_)
770-
delete[] buf;
773+
return args.GetReturnValue().Set(req_wrap->persistent());
771774
}
772775

773776

0 commit comments

Comments
 (0)