Skip to content

Commit

Permalink
Simplify memory handling for module
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Oct 1, 2020
1 parent 1a7d09a commit 4713366
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ pub fn validate<T: AsRef<[u8]>>(input: T) -> bool {
}

pub struct Module {
ptr: *mut sys::FizzyModule,
ptr: NonNull<sys::FizzyModule>,
}

impl Drop for Module {
fn drop(&mut self) {
// This is null if we took ownership with instantiate
if !self.ptr.is_null() {
unsafe { sys::fizzy_free_module(self.ptr) }
}
unsafe { sys::fizzy_free_module(self.ptr.as_ptr()) }
}
}

Expand All @@ -28,7 +25,9 @@ pub fn parse<T: AsRef<[u8]>>(input: &T) -> Option<Module> {
if ptr.is_null() {
return None;
}
Some(Module { ptr: ptr })
Some(Module {
ptr: unsafe { NonNull::new_unchecked(ptr) },
})
}

pub struct Instance {
Expand All @@ -44,12 +43,8 @@ impl Drop for Instance {
impl Module {
// TODO: support imported functions{
pub fn instantiate(self) -> Option<Instance> {
// An instance was already created.
if self.ptr.is_null() {
return None;
}
let ptr = unsafe { sys::fizzy_instantiate(self.ptr, std::ptr::null_mut(), 0) };
// Forget pointer because it has been consumed by instantiate (even if it failed).
let ptr = unsafe { sys::fizzy_instantiate(self.ptr.as_ptr(), std::ptr::null_mut(), 0) };
// Forget Module (and avoid calling drop) because it has been consumed by instantiate (even if it failed).
core::mem::forget(self);
if ptr.is_null() {
return None;
Expand Down

0 comments on commit 4713366

Please sign in to comment.