Skip to content

Commit

Permalink
Implements heap de-allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Oct 19, 2024
1 parent a5e7321 commit f4a0a76
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions macros/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ fn render_class(item: Class) -> syn::Result<TokenStream> {
fn alloc() -> *mut () {
unsafe { ::cppbind::new(#size) }
}

unsafe fn dealloc(this: *mut ()) {
::cppbind::delete(this, #size);
}
}

impl ::cppbind::Memory for &mut #mem {
Expand Down
16 changes: 12 additions & 4 deletions macros/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl<'a> Symbol<'a> {
Segment::Ctor => n.push_str("C1"),
Segment::Dtor => n.push_str("D1"),
Segment::New => n.push_str("nw"),
Segment::Delete => n.push_str("dl"),
}
};

Expand All @@ -62,12 +63,17 @@ impl<'a> Symbol<'a> {
}

// Build signature.
fn push_type(n: &mut String, t: &Type) {
match t {
Type::Void => n.push('v'),
Type::Ulong => n.push('m'),
Type::Ptr(v) => push_type(n, &v),
}
}

if let Some(s) = &self.sig {
for p in &s.params {
match p {
Type::Void => name.push('v'),
Type::Ulong => name.push('m'),
}
push_type(&mut name, p);
}
}

Expand All @@ -90,6 +96,7 @@ pub enum Segment<'a> {
Ctor,
Dtor,
New,
Delete,
}

/// Argument of a template instantiation.
Expand All @@ -115,6 +122,7 @@ impl Signature {
pub enum Type {
Void,
Ulong,
Ptr(Box<Self>),
}

/// Represents an error when [`Symbol`] fails to parse from a mangled name.
Expand Down
2 changes: 2 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
unsafe extern "C-unwind" {
#[link_name = "\u{1}_Znwm"]
pub fn new(len: usize) -> *mut ();
#[link_name = "\u{1}_ZdlPvm"]
pub fn delete(ptr: *mut (), len: usize);
}
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ pub use cppbind_macros::*;
mod ffi;

/// Memory of a C++ class that live on a heap.
pub struct Heap<T>(*mut T);
pub struct Heap<T: HeapAlloc>(*mut T);

impl<T: HeapAlloc> Heap<T> {
pub fn new() -> Self {
Self(T::alloc().cast())
}
}

impl<T: HeapAlloc> Drop for Heap<T> {
fn drop(&mut self) {
unsafe { T::dealloc(self.0.cast()) };
}
}

impl<T: HeapAlloc> Memory for Heap<T> {
type Class = T::Class;

Expand All @@ -20,8 +26,8 @@ impl<T: HeapAlloc> Memory for Heap<T> {
}
}

unsafe impl<T: Send> Send for Heap<T> {}
unsafe impl<T: Sync> Sync for Heap<T> {}
unsafe impl<T: HeapAlloc + Send> Send for Heap<T> {}
unsafe impl<T: HeapAlloc + Sync> Sync for Heap<T> {}

/// Memory of a C++ class.
pub trait Memory {
Expand All @@ -35,4 +41,5 @@ pub trait HeapAlloc {
type Class;

fn alloc() -> *mut ();
unsafe fn dealloc(this: *mut ());
}

0 comments on commit f4a0a76

Please sign in to comment.