src: clean up FSReqWrap
Move the 'free FSReqWrap data?' logic into the class itself.
This commit is contained in:
parent
ffc5d83568
commit
d2b80b8a60
@ -63,18 +63,22 @@ using v8::Value;
|
|||||||
|
|
||||||
class FSReqWrap: public ReqWrap<uv_fs_t> {
|
class FSReqWrap: public ReqWrap<uv_fs_t> {
|
||||||
public:
|
public:
|
||||||
explicit FSReqWrap(const char* syscall)
|
explicit FSReqWrap(const char* syscall, char* data = NULL)
|
||||||
: must_free_(false)
|
: syscall_(syscall)
|
||||||
, data_(NULL)
|
, data_(data) {
|
||||||
, syscall_(syscall) {
|
}
|
||||||
|
|
||||||
|
void ReleaseEarly() {
|
||||||
|
if (data_ == NULL) return;
|
||||||
|
delete[] data_;
|
||||||
|
data_ = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* syscall() { return syscall_; }
|
const char* syscall() { return syscall_; }
|
||||||
bool must_free_; // request is responsible for free'ing memory oncomplete
|
|
||||||
char* data_;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* syscall_;
|
const char* syscall_;
|
||||||
|
char* data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -102,10 +106,7 @@ static void After(uv_fs_t *req) {
|
|||||||
|
|
||||||
FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data);
|
FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data);
|
||||||
assert(&req_wrap->req_ == req);
|
assert(&req_wrap->req_ == req);
|
||||||
|
req_wrap->ReleaseEarly(); // Free memory that's no longer used now.
|
||||||
// check if data needs to be cleaned
|
|
||||||
if (req_wrap->must_free_ == true)
|
|
||||||
delete[] req_wrap->data_;
|
|
||||||
|
|
||||||
// there is always at least one argument. "error"
|
// there is always at least one argument. "error"
|
||||||
int argc = 1;
|
int argc = 1;
|
||||||
@ -729,7 +730,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
char* buf = NULL;
|
char* buf = NULL;
|
||||||
int64_t pos;
|
int64_t pos;
|
||||||
size_t len;
|
size_t len;
|
||||||
bool must_free_ = false;
|
bool must_free = false;
|
||||||
|
|
||||||
// will assign buf and len if string was external
|
// will assign buf and len if string was external
|
||||||
if (!StringBytes::GetExternalParts(string,
|
if (!StringBytes::GetExternalParts(string,
|
||||||
@ -741,33 +742,35 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
// StorageSize may return too large a char, so correct the actual length
|
// StorageSize may return too large a char, so correct the actual length
|
||||||
// by the write size
|
// by the write size
|
||||||
len = StringBytes::Write(buf, len, args[1], enc);
|
len = StringBytes::Write(buf, len, args[1], enc);
|
||||||
must_free_ = true;
|
must_free = true;
|
||||||
}
|
}
|
||||||
pos = GET_OFFSET(args[2]);
|
pos = GET_OFFSET(args[2]);
|
||||||
cb = args[4];
|
cb = args[4];
|
||||||
|
|
||||||
if (cb->IsFunction()) {
|
if (!cb->IsFunction()) {
|
||||||
FSReqWrap* req_wrap = new FSReqWrap("write");
|
SYNC_CALL(write, NULL, fd, buf, len, pos)
|
||||||
int err = uv_fs_write(uv_default_loop(), &req_wrap->req_,
|
if (must_free) delete[] buf;
|
||||||
fd, buf, len, pos, After);
|
return args.GetReturnValue().Set(SYNC_RESULT);
|
||||||
req_wrap->object()->Set(oncomplete_sym, cb);
|
|
||||||
req_wrap->must_free_ = must_free_;
|
|
||||||
req_wrap->Dispatched();
|
|
||||||
req_wrap->data_ = buf;
|
|
||||||
if (err < 0) {
|
|
||||||
uv_fs_t* req = &req_wrap->req_;
|
|
||||||
req->result = err;
|
|
||||||
req->path = NULL;
|
|
||||||
After(req);
|
|
||||||
}
|
|
||||||
return args.GetReturnValue().Set(req_wrap->persistent());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SYNC_CALL(write, NULL, fd, buf, len, pos)
|
FSReqWrap* req_wrap = new FSReqWrap("write", must_free ? buf : NULL);
|
||||||
args.GetReturnValue().Set(SYNC_RESULT);
|
int err = uv_fs_write(uv_default_loop(),
|
||||||
|
&req_wrap->req_,
|
||||||
|
fd,
|
||||||
|
buf,
|
||||||
|
len,
|
||||||
|
pos,
|
||||||
|
After);
|
||||||
|
req_wrap->object()->Set(oncomplete_sym, cb);
|
||||||
|
req_wrap->Dispatched();
|
||||||
|
if (err < 0) {
|
||||||
|
uv_fs_t* req = &req_wrap->req_;
|
||||||
|
req->result = err;
|
||||||
|
req->path = NULL;
|
||||||
|
After(req);
|
||||||
|
}
|
||||||
|
|
||||||
if (must_free_)
|
return args.GetReturnValue().Set(req_wrap->persistent());
|
||||||
delete[] buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user