1- using System ;
1+ using System ;
22using System . Runtime . InteropServices ;
33
44namespace Python . Runtime
55{
66 internal class Util
77 {
8+ // On Windows, a C long is always 32 bits.
9+ static readonly bool clong32bit = Runtime . IsWindows || Runtime . Is32Bit ;
10+
811 internal static Int64 ReadCLong ( IntPtr tp , int offset )
912 {
10- // On Windows, a C long is always 32 bits.
11- if ( Runtime . IsWindows || Runtime . Is32Bit )
13+ if ( clong32bit )
1214 {
1315 return Marshal . ReadInt32 ( tp , offset ) ;
1416 }
@@ -20,7 +22,7 @@ internal static Int64 ReadCLong(IntPtr tp, int offset)
2022
2123 internal static void WriteCLong ( IntPtr type , int offset , Int64 flags )
2224 {
23- if ( Runtime . IsWindows || Runtime . Is32Bit )
25+ if ( clong32bit )
2426 {
2527 Marshal . WriteInt32 ( type , offset , ( Int32 ) ( flags & 0xffffffffL ) ) ;
2628 }
@@ -29,5 +31,34 @@ internal static void WriteCLong(IntPtr type, int offset, Int64 flags)
2931 Marshal . WriteInt64 ( type , offset , flags ) ;
3032 }
3133 }
34+
35+ internal static unsafe Int32 ReadInt32Aligned ( IntPtr ptr , int byteOffset )
36+ {
37+ byte * address = ( byte * ) ptr + byteOffset ;
38+ return * ( ( int * ) address ) ;
39+ }
40+
41+ internal static unsafe Int64 ReadInt64Aligned ( IntPtr ptr , int byteOffset )
42+ {
43+ byte * address = ( byte * ) ptr + byteOffset ;
44+ return * ( ( long * ) address ) ;
45+ }
46+
47+ internal static unsafe Int64 ReadCLongAligned ( IntPtr ptr , int byteOffset )
48+ {
49+ return clong32bit ? ReadInt32Aligned ( ptr , byteOffset ) : ReadInt64Aligned ( ptr , byteOffset ) ;
50+ }
51+
52+ internal static unsafe IntPtr ReadIntPtrAligned ( IntPtr ptr , int byteOffset )
53+ {
54+ byte * address = ( byte * ) ptr + byteOffset ;
55+ return * ( ( IntPtr * ) address ) ;
56+ }
57+
58+ internal static unsafe void WriteIntPtrAligned ( IntPtr ptr , int byteOffset , IntPtr value )
59+ {
60+ byte * address = ( byte * ) ptr + byteOffset ;
61+ * ( ( IntPtr * ) address ) = value;
62+ }
3263 }
33- }
64+ }
0 commit comments