Open Manishearth opened 3 years ago
The first way is supporting &mut MaybeUninit<T>
as parameters.
E.g.
#[diplomat::bridge]
mod ffi {
impl FixedDecimal {
fn new_in_place(&mut MaybeUninit<Self>) {..}
}
}
For this code, the following stuff happens:
FixedDecimal_new_in_place
generated with a FixedDecimal*
parameterFixedDecimal_get_layout()
generated, returns a size and alignmentFixedDecimal_drop_in_place()
generatedFixedDecimal::new_in_place()
generated with no parametersALLOCATE_FIXED_DECIMAL()
macro generated to alloca()
fixeddecimal#[diplomat::bridge] mod ffi {
impl PluralRules {
fn new_alloc(Locale, alloc: CustomAllocFn) -> CustomAllocBox<PluralRules> {
let pr = PluralRules::new(Locale);
CustomAllocBox::new(pr, alloc);
}
}
}
CustomAllocBox
is a type with an aborting destructor, that is intended to be called with a new(alloc_fn)
and paired with box.free(free_fn)
. This generates:
FixedDecimal_drop_in_place()
CustomAllocBox
is internally a *mut T
Currently opaque structs need to be allocated with the system allocator. It would be nice if C++ (at least) callers could handle their own memory management, perhaps using bump allocators for short-lived objects.
There are two ways I can see us doing this, which I'll write out in comments