From 9e839b7e2325e55147cc7e49ed9576cc000eaf63 Mon Sep 17 00:00:00 2001 From: F001 Date: Tue, 4 Dec 2018 11:07:34 +0800 Subject: [PATCH] Avoid memory leak (#1265) --- src/isolate.rs | 21 +++------------------ src/libdeno.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/isolate.rs b/src/isolate.rs index 6e4c421c7a..a9088e708f 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -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 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, diff --git a/src/libdeno.rs b/src/libdeno.rs index c831dc73a5..35278a7dab 100644 --- a/src/libdeno.rs +++ b/src/libdeno.rs @@ -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,