@@ -63,18 +63,22 @@ using v8::Value;
63
63
64
64
class FSReqWrap: public ReqWrap<uv_fs_t> {
65
65
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 ;
70
75
}
71
76
72
77
const char * syscall () { return syscall_; }
73
- bool must_free_; // request is responsible for free'ing memory oncomplete
74
- char * data_;
75
78
76
79
private:
77
80
const char * syscall_;
81
+ char * data_;
78
82
};
79
83
80
84
@@ -102,10 +106,7 @@ static void After(uv_fs_t *req) {
102
106
103
107
FSReqWrap* req_wrap = static_cast <FSReqWrap*>(req->data );
104
108
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.
109
110
110
111
// there is always at least one argument. "error"
111
112
int argc = 1 ;
@@ -729,7 +730,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
729
730
char * buf = NULL ;
730
731
int64_t pos;
731
732
size_t len;
732
- bool must_free_ = false ;
733
+ bool must_free = false ;
733
734
734
735
// will assign buf and len if string was external
735
736
if (!StringBytes::GetExternalParts (string,
@@ -741,33 +742,35 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
741
742
// StorageSize may return too large a char, so correct the actual length
742
743
// by the write size
743
744
len = StringBytes::Write (buf, len, args[1 ], enc);
744
- must_free_ = true ;
745
+ must_free = true ;
745
746
}
746
747
pos = GET_OFFSET (args[2 ]);
747
748
cb = args[4 ];
748
749
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);
764
754
}
765
755
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
+ }
768
772
769
- if (must_free_)
770
- delete[] buf;
773
+ return args.GetReturnValue ().Set (req_wrap->persistent ());
771
774
}
772
775
773
776
0 commit comments