From b3d4ae51f5e57e6d7e76815f87edd3064c27757d Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 25 Nov 2020 16:45:29 +0100 Subject: [PATCH] wrapper: Pin blob source insted of unnecessarily RC'ing it The refcounter here makes little sense as the string is only ever referenced by DxcIncludeHandlerWrapper. The count is never artificially inremented to represent implicit referencing from a native DxcBlob. Overall lifetime of the string is not altered: as soon as DxcIncludeHandlerWrapper goes out of scope the underlying string disappears as well. Furthermore, use Pin<> to hold the string at the same location in memory. In particular this Pin instance only gives us access to the deref of `String`, which is `&str` and does not allow destructive operations like resizing or reassignment that could potentially reallocate heap memory (hence moving the pointer). --- src/wrapper.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/wrapper.rs b/src/wrapper.rs index 8896204..e81c4c1 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -11,7 +11,7 @@ use com_rs::ComPtr; use libloading::{Library, Symbol}; use std::convert::Into; use std::ffi::c_void; -use std::rc::Rc; +use std::pin::Pin; #[macro_export] macro_rules! check_hr { @@ -135,7 +135,7 @@ struct DxcIncludeHandlerWrapperVtbl { struct DxcIncludeHandlerWrapper<'a> { vtable: Box, handler: Box, - pinned: Vec>, + pinned: Vec>, library: &'a DxcLibrary, } @@ -164,19 +164,18 @@ impl<'a> DxcIncludeHandlerWrapper<'a> { let source = unsafe { (*me).handler.load_source(filename) }; if let Some(source) = source { - let pinned_source = Rc::new(source); - + let source = Pin::new(source); let mut blob = unsafe { (*me) .library - .create_blob_with_encoding_from_str(pinned_source.as_str()) + .create_blob_with_encoding_from_str(&source) .unwrap() }; unsafe { blob.inner.add_ref(); *include_source = *blob.inner.as_mut_ptr(); - (*me).pinned.push(pinned_source); + (*me).pinned.push(source); } 0