JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

More restructuring

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨10b7b7c

⁨giterated-runtime/giterated-abi/src/heap.rs⁩ - ⁨925⁩ bytes
Raw
1 use std::{mem::MaybeUninit, ptr::drop_in_place};
2
3 use crate::{abi_backing::HeapValueBacking, FfiValue};
4
5 pub trait HeapPlacable {
6 unsafe extern "C" fn free(value: FfiValue<Self>, taken: bool);
7 }
8
9 impl<T> HeapPlacable for T {
10 unsafe extern "C" fn free(value: FfiValue<Self>, taken: bool) {
11 if !taken {
12 drop(Box::from_raw(value.inner as *mut HeapValueBacking<T>))
13 } else {
14 let allocation = Box::from_raw(value.inner as *mut T);
15
16 // Since we "took" the value, kindly inform the compiler that it can't
17 // treat the value like it exists
18 let allocation_uninit: Box<HeapValueBacking<MaybeUninit<T>>> =
19 unsafe { core::mem::transmute(allocation) };
20
21 // Since the compiler has no idea whether the value exists or not, it won't try and
22 // drop it. Success!
23 drop(allocation_uninit);
24 }
25 }
26 }
27