Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- impl VLACtx{
- /**
- * Create the context for the VLA.
- *
- * **Note** Unless you know what you are doing use the `vlam::ctx!` macro
- *
- * **Safety** The function is unsafe because it produces a VLACtx that is not protected by a pin
- * which means it can be moved outside the scope that makes it valid. Moving VLA will lead to UB.
- */
- #[inline(always)]
- pub unsafe fn init() -> Self{
- let mut saved_sp = null();
- // SAFETY : This only save a frame pointer
- unsafe {
- core::arch::asm!(
- "mv {saved_sp}, sp",
- saved_sp = out(reg) saved_sp,
- )
- };
- VLACtx{saved_sp, phantom_pinned: Default::default() }
- }
- /**
- * Create a buffer of `len` bytes initialized at 0
- */
- #[inline(always)]
- pub fn zeroed_buffer<'ctx>(self: &'ctx Pin<&'ctx mut Self>, len:usize) -> VLArray<'ctx, u8> {
- let mut origin: *mut u8 = null_mut();
- unsafe {
- let len = len * size_of::<u8>();
- let len = (len / MIN_ALIGN + 1) * MIN_ALIGN;
- asm!(
- "mv {orig}, sp",
- "sub sp, sp, {len}",
- orig = out(reg) origin,
- len = in(reg) len
- );
- origin = origin.sub(len);
- write_bytes(origin, 0, len);
- }
- VLArray{
- ptr:origin,
- len,
- _ctx: self,
- }
- }
- }
- impl Drop for VLACtx{
- #[inline(always)]
- fn drop(&mut self) {
- let saved_sp = self.saved_sp;
- // SAFETY: Restores the stack pointer.
- // This is only safe because the type is pinned to the stack at all time.
- unsafe {
- asm!(
- "mv sp, {saved_sp}",
- saved_sp = in(reg) saved_sp,
- )
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement