@@ -209,10 +209,9 @@ macro_rules! wrapping_arithmetic_op {
209209
210210impl < ' gc > StackValue < ' gc > {
211211 pub fn unmanaged_ptr ( ptr : * mut u8 ) -> Self {
212- if ptr. is_null ( ) {
213- Self :: NativeInt ( 0 )
214- } else {
215- Self :: UnmanagedPtr ( UnmanagedPtr ( NonNull :: new ( ptr) . unwrap ( ) ) )
212+ match NonNull :: new ( ptr) {
213+ Some ( p) => Self :: UnmanagedPtr ( UnmanagedPtr ( p) ) ,
214+ None => Self :: NativeInt ( 0 ) ,
216215 }
217216 }
218217 pub fn managed_ptr ( ptr : * mut u8 , target_type : TypeDescription , pinned : bool ) -> Self {
@@ -288,10 +287,10 @@ impl<'gc> StackValue<'gc> {
288287 match self {
289288 Self :: NativeInt ( i) => * i as * mut u8 ,
290289 Self :: UnmanagedPtr ( UnmanagedPtr ( p) ) => p. as_ptr ( ) ,
291- Self :: ManagedPtr ( m) => m
292- . pointer ( )
293- . map ( |p| p . as_ptr ( ) )
294- . unwrap_or ( std :: ptr :: null_mut ( ) ) ,
290+ Self :: ManagedPtr ( m) => match m . pointer ( ) {
291+ Some ( p ) => p . as_ptr ( ) ,
292+ None => std :: ptr :: null_mut ( ) ,
293+ } ,
295294 v => panic ! ( "expected pointer on stack, received {:?}" , v) ,
296295 }
297296 }
@@ -413,45 +412,44 @@ impl<'gc> StackValue<'gc> {
413412 /// Note: This uses `AtomicT::from_ptr` which is supported in recent Rust versions.
414413 /// Also, it does not ensure that the appropriate locks are held for the memory being accessed.
415414 pub unsafe fn load_atomic ( ptr : * const u8 , t : LoadType , ordering : AtomicOrdering ) -> Self {
416- unsafe {
417- debug_assert ! ( !ptr. is_null( ) , "Attempted to load from a null pointer" ) ;
418- let alignment = load_type_alignment ( t) ;
419- debug_assert ! (
420- ( ptr as usize ) . is_multiple_of( alignment) ,
421- "Attempted to load from an unaligned pointer {:?} for type {:?}" ,
422- ptr,
423- t
424- ) ;
425-
426- let size = match t {
427- LoadType :: Int8 | LoadType :: UInt8 => 1 ,
428- LoadType :: Int16 | LoadType :: UInt16 => 2 ,
429- LoadType :: Int32 | LoadType :: UInt32 | LoadType :: Float32 => 4 ,
430- LoadType :: Int64 | LoadType :: Float64 => 8 ,
431- LoadType :: IntPtr => size_of :: < isize > ( ) ,
432- LoadType :: Object => size_of :: < usize > ( ) ,
433- } ;
434-
435- let val = StandardAtomicAccess :: load_atomic ( ptr, size, ordering) ;
436-
437- match t {
438- LoadType :: Int8 => Self :: Int32 ( val as i8 as i32 ) ,
439- LoadType :: UInt8 => Self :: Int32 ( val as u8 as i32 ) ,
440- LoadType :: Int16 => Self :: Int32 ( val as i16 as i32 ) ,
441- LoadType :: UInt16 => Self :: Int32 ( val as u16 as i32 ) ,
442- LoadType :: Int32 | LoadType :: UInt32 => Self :: Int32 ( val as i32 ) ,
443- LoadType :: Int64 => Self :: Int64 ( val as i64 ) ,
444- LoadType :: Float32 => Self :: NativeFloat ( f32:: from_bits ( val as u32 ) as f64 ) ,
445- LoadType :: Float64 => Self :: NativeFloat ( f64:: from_bits ( val) ) ,
446- LoadType :: IntPtr => Self :: NativeInt ( val as isize ) ,
447- LoadType :: Object => {
448- let ptr = val as usize as * const ThreadSafeLock < object:: ObjectInner < ' gc > > ;
449- if ptr. is_null ( ) {
450- Self :: ObjectRef ( ObjectRef ( None ) )
451- } else {
452- Self :: ObjectRef ( ObjectRef ( Some ( Gc :: from_ptr ( ptr) ) ) )
453- }
454- }
415+ debug_assert ! ( !ptr. is_null( ) , "Attempted to load from a null pointer" ) ;
416+ let alignment = load_type_alignment ( t) ;
417+ debug_assert ! (
418+ ( ptr as usize ) . is_multiple_of( alignment) ,
419+ "Attempted to load from an unaligned pointer {:?} for type {:?}" ,
420+ ptr,
421+ t
422+ ) ;
423+
424+ let size = match t {
425+ LoadType :: Int8 | LoadType :: UInt8 => 1 ,
426+ LoadType :: Int16 | LoadType :: UInt16 => 2 ,
427+ LoadType :: Int32 | LoadType :: UInt32 | LoadType :: Float32 => 4 ,
428+ LoadType :: Int64 | LoadType :: Float64 => 8 ,
429+ LoadType :: IntPtr => size_of :: < isize > ( ) ,
430+ LoadType :: Object => size_of :: < usize > ( ) ,
431+ } ;
432+
433+ let val = unsafe { StandardAtomicAccess :: load_atomic ( ptr, size, ordering) } ;
434+
435+ match t {
436+ LoadType :: Int8 => Self :: Int32 ( val as i8 as i32 ) ,
437+ LoadType :: UInt8 => Self :: Int32 ( val as u8 as i32 ) ,
438+ LoadType :: Int16 => Self :: Int32 ( val as i16 as i32 ) ,
439+ LoadType :: UInt16 => Self :: Int32 ( val as u16 as i32 ) ,
440+ LoadType :: Int32 | LoadType :: UInt32 => Self :: Int32 ( val as i32 ) ,
441+ LoadType :: Int64 => Self :: Int64 ( val as i64 ) ,
442+ LoadType :: Float32 => Self :: NativeFloat ( f32:: from_bits ( val as u32 ) as f64 ) ,
443+ LoadType :: Float64 => Self :: NativeFloat ( f64:: from_bits ( val) ) ,
444+ LoadType :: IntPtr => Self :: NativeInt ( val as isize ) ,
445+ LoadType :: Object => {
446+ let ptr = val as usize as * const ThreadSafeLock < object:: ObjectInner < ' gc > > ;
447+ let obj = if ptr. is_null ( ) {
448+ None
449+ } else {
450+ Some ( unsafe { Gc :: from_ptr ( ptr) } )
451+ } ;
452+ Self :: ObjectRef ( ObjectRef ( obj) )
455453 }
456454 }
457455 }
@@ -470,34 +468,34 @@ impl<'gc> StackValue<'gc> {
470468 /// Note: This uses `AtomicT::from_ptr` which is supported in recent Rust versions.
471469 /// Also, it does not ensure that the appropriate locks are held for the memory being accessed.
472470 pub unsafe fn store_atomic ( self , ptr : * mut u8 , t : StoreType , ordering : AtomicOrdering ) {
473- unsafe {
474- debug_assert ! ( !ptr. is_null( ) , "Attempted to store to a null pointer" ) ;
475- let alignment = store_type_alignment ( t) ;
476- debug_assert ! (
477- ( ptr as usize ) . is_multiple_of( alignment) ,
478- "Attempted to store to an unaligned pointer {:?} for type {:?}" ,
479- ptr,
480- t
481- ) ;
482-
483- let ( val, size) = match t {
484- StoreType :: Int8 => ( self . as_i32 ( ) as u64 , 1 ) ,
485- StoreType :: Int16 => ( self . as_i32 ( ) as u64 , 2 ) ,
486- StoreType :: Int32 => ( self . as_i32 ( ) as u64 , 4 ) ,
487- StoreType :: Int64 => ( self . as_i64 ( ) as u64 , 8 ) ,
488- StoreType :: Float32 => ( ( self . as_f64 ( ) as f32 ) . to_bits ( ) as u64 , 4 ) ,
489- StoreType :: Float64 => ( self . as_f64 ( ) . to_bits ( ) , 8 ) ,
490- StoreType :: IntPtr => ( self . as_isize ( ) as u64 , size_of :: < isize > ( ) ) ,
491- StoreType :: Object => {
492- let obj = self . as_object_ref ( ) ;
493- let val = match obj. 0 {
494- Some ( h) => Gc :: as_ptr ( h) as usize ,
495- None => 0 ,
496- } ;
497- ( val as u64 , size_of :: < usize > ( ) )
498- }
499- } ;
471+ debug_assert ! ( !ptr. is_null( ) , "Attempted to store to a null pointer" ) ;
472+ let alignment = store_type_alignment ( t) ;
473+ debug_assert ! (
474+ ( ptr as usize ) . is_multiple_of( alignment) ,
475+ "Attempted to store to an unaligned pointer {:?} for type {:?}" ,
476+ ptr,
477+ t
478+ ) ;
479+
480+ let ( val, size) = match t {
481+ StoreType :: Int8 => ( self . as_i32 ( ) as u64 , 1 ) ,
482+ StoreType :: Int16 => ( self . as_i32 ( ) as u64 , 2 ) ,
483+ StoreType :: Int32 => ( self . as_i32 ( ) as u64 , 4 ) ,
484+ StoreType :: Int64 => ( self . as_i64 ( ) as u64 , 8 ) ,
485+ StoreType :: Float32 => ( ( self . as_f64 ( ) as f32 ) . to_bits ( ) as u64 , 4 ) ,
486+ StoreType :: Float64 => ( self . as_f64 ( ) . to_bits ( ) , 8 ) ,
487+ StoreType :: IntPtr => ( self . as_isize ( ) as u64 , size_of :: < isize > ( ) ) ,
488+ StoreType :: Object => {
489+ let obj = self . as_object_ref ( ) ;
490+ let val = match obj. 0 {
491+ Some ( h) => Gc :: as_ptr ( h) as usize ,
492+ None => 0 ,
493+ } ;
494+ ( val as u64 , size_of :: < usize > ( ) )
495+ }
496+ } ;
500497
498+ unsafe {
501499 StandardAtomicAccess :: store_atomic ( ptr, size, val, ordering) ;
502500 }
503501 }
0 commit comments