add intrinsic to access vtable size and align
This commit is contained in:
parent
b5a32d01f4
commit
8affef2ccb
6 changed files with 66 additions and 2 deletions
|
|
@ -2291,6 +2291,16 @@ extern "rust-intrinsic" {
|
|||
/// [`std::hint::black_box`]: crate::hint::black_box
|
||||
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]
|
||||
pub fn black_box<T>(dummy: T) -> T;
|
||||
|
||||
/// `ptr` must point to a vtable.
|
||||
/// The intrinsic will return the size stored in that vtable.
|
||||
#[cfg(not(bootstrap))]
|
||||
pub fn vtable_size(ptr: *const ()) -> usize;
|
||||
|
||||
/// `ptr` must point to a vtable.
|
||||
/// The intrinsic will return the alignment stored in that vtable.
|
||||
#[cfg(not(bootstrap))]
|
||||
pub fn vtable_align(ptr: *const ()) -> usize;
|
||||
}
|
||||
|
||||
// Some functions are defined here because they accidentally got made
|
||||
|
|
|
|||
|
|
@ -180,10 +180,20 @@ pub struct DynMetadata<Dyn: ?Sized> {
|
|||
phantom: crate::marker::PhantomData<Dyn>,
|
||||
}
|
||||
|
||||
/// Opaque type for accessing vtables.
|
||||
///
|
||||
/// Private implementation detail of `DynMetadata::size_of` etc.
|
||||
/// Must be zero-sized since there is conceptually not actually any Abstract Machine memory behind this pointer.
|
||||
/// However, we can require pointer alignment.
|
||||
#[repr(C)]
|
||||
#[cfg(not(bootstrap))]
|
||||
struct VTable([usize; 0]);
|
||||
|
||||
/// The common prefix of all vtables. It is followed by function pointers for trait methods.
|
||||
///
|
||||
/// Private implementation detail of `DynMetadata::size_of` etc.
|
||||
#[repr(C)]
|
||||
#[cfg(bootstrap)]
|
||||
struct VTable {
|
||||
drop_in_place: fn(*mut ()),
|
||||
size_of: usize,
|
||||
|
|
@ -194,13 +204,25 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
|
|||
/// Returns the size of the type associated with this vtable.
|
||||
#[inline]
|
||||
pub fn size_of(self) -> usize {
|
||||
self.vtable_ptr.size_of
|
||||
#[cfg(bootstrap)]
|
||||
return self.vtable_ptr.size_of;
|
||||
#[cfg(not(bootstrap))]
|
||||
// SAFETY: DynMetadata always contains a valid vtable pointer
|
||||
return unsafe {
|
||||
crate::intrinsics::vtable_size(self.vtable_ptr as *const VTable as *const ())
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the alignment of the type associated with this vtable.
|
||||
#[inline]
|
||||
pub fn align_of(self) -> usize {
|
||||
self.vtable_ptr.align_of
|
||||
#[cfg(bootstrap)]
|
||||
return self.vtable_ptr.align_of;
|
||||
#[cfg(not(bootstrap))]
|
||||
// SAFETY: DynMetadata always contains a valid vtable pointer
|
||||
return unsafe {
|
||||
crate::intrinsics::vtable_align(self.vtable_ptr as *const VTable as *const ())
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the size and alignment together as a `Layout`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue