0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Avoid memory leak (#1265)

This commit is contained in:
F001 2018-12-04 11:07:34 +08:00 committed by Ryan Dahl
parent f6c841a6cd
commit 9e839b7e23
2 changed files with 15 additions and 18 deletions

View file

@ -190,15 +190,14 @@ impl Isolate {
pub fn respond(&mut self, req_id: i32, buf: Buf) {
self.state.metrics_op_completed(buf.len());
// TODO(zero-copy) Use Buf::leak(buf) to leak the heap allocated buf. And
// don't do the memcpy in ImportBuf() (in libdeno/binding.cc)
// deno_respond will memcpy the buf into V8's heap,
// so borrowing a reference here is sufficient.
unsafe {
libdeno::deno_respond(
self.libdeno_isolate,
self.as_void_ptr(),
req_id,
buf.into(),
buf.as_ref().into(),
)
}
}
@ -271,20 +270,6 @@ impl Drop for Isolate {
}
}
/// Converts Rust Buf to libdeno `deno_buf`.
impl From<Buf> for libdeno::deno_buf {
fn from(x: Buf) -> Self {
let len = x.len();
let ptr = Box::into_raw(x);
Self {
alloc_ptr: std::ptr::null_mut(),
alloc_len: 0,
data_ptr: ptr as *mut u8,
data_len: len,
}
}
}
// Dereferences the C pointer into the Rust Isolate object.
extern "C" fn pre_dispatch(
user_data: *mut c_void,

View file

@ -29,6 +29,18 @@ impl deno_buf {
}
}
/// Converts Rust &Buf to libdeno `deno_buf`.
impl<'a> From<&'a [u8]> for deno_buf {
fn from(x: &'a [u8]) -> Self {
Self {
alloc_ptr: std::ptr::null_mut(),
alloc_len: 0,
data_ptr: x.as_ref().as_ptr() as *mut u8,
data_len: x.len(),
}
}
}
type DenoRecvCb = unsafe extern "C" fn(
user_data: *mut c_void,
req_id: i32,