Skip to content

Commit

Permalink
wrapper: Pin blob source insted of unnecessarily RC'ing it
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
MarijnS95 committed Nov 25, 2020
1 parent 4a3a9ac commit b3d4ae5
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -135,7 +135,7 @@ struct DxcIncludeHandlerWrapperVtbl {
struct DxcIncludeHandlerWrapper<'a> {
vtable: Box<DxcIncludeHandlerWrapperVtbl>,
handler: Box<dyn DxcIncludeHandler>,
pinned: Vec<Rc<String>>,
pinned: Vec<Pin<String>>,
library: &'a DxcLibrary,
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b3d4ae5

Please sign in to comment.