1
1
using System . Runtime . InteropServices ;
2
2
using System ;
3
+ using System . Runtime . CompilerServices ;
4
+
5
+ using MethImpl = System . Runtime . CompilerServices . MethodImplAttribute ;
6
+ using static System . Runtime . CompilerServices . MethodImplOptions ;
3
7
4
8
namespace Memory ;
5
9
public unsafe static class MemEx
6
10
{
11
+ [ MethImpl ( AggressiveInlining ) ]
7
12
public static T * New < T > ( ) where T : unmanaged => NewAlloc < T > ( ) ;
13
+ [ MethImpl ( AggressiveInlining ) ]
8
14
public static T * New < T > ( T val ) where T : unmanaged => NewAlloc ( & val ) ;
9
15
16
+ [ MethImpl ( AggressiveInlining ) ]
10
17
public static T * NewAlloc < T > ( T * from ) where T : unmanaged
11
18
{
12
19
T * to = ( T * ) Marshal . AllocCoTaskMem ( sizeof ( T ) ) ;
13
20
* to = * from ;
14
21
return to ;
15
22
}
16
23
24
+ [ MethImpl ( AggressiveInlining ) ]
17
25
public static T * NewAlloc < T > ( ) where T : unmanaged => ( T * ) Marshal . AllocCoTaskMem ( sizeof ( T ) ) ;
18
26
27
+ [ MethImpl ( AggressiveInlining ) ]
19
28
public static byte * Alloc ( int count ) => ( byte * ) Marshal . AllocCoTaskMem ( count ) ;
29
+ [ MethImpl ( AggressiveInlining ) ]
20
30
public static T * Alloc < T > ( int count ) where T : unmanaged => ( T * ) Marshal . AllocCoTaskMem ( count * sizeof ( T ) ) ;
21
31
32
+ [ MethImpl ( AggressiveInlining ) ]
22
33
public static T * AllocFrom < T > ( T [ ] arr ) where T : unmanaged
23
34
{
24
35
T * ptr = Alloc < T > ( arr . Length ) ;
25
36
Copy ( ptr , arr ) ;
26
37
return ptr ;
27
38
}
28
39
40
+ [ MethImpl ( AggressiveInlining ) ]
29
41
public static void Free ( void * ptr ) => Marshal . FreeCoTaskMem ( ( nint ) ptr ) ;
42
+ [ MethImpl ( AggressiveInlining ) ]
30
43
public static void Free ( params void * [ ] ptrs )
31
44
{
32
45
foreach ( void * ptr in ptrs )
33
46
Marshal . FreeCoTaskMem ( ( nint ) ptr ) ;
34
47
}
48
+ [ MethImpl ( AggressiveInlining ) ]
35
49
public static void Free ( nint addr ) => Marshal . FreeCoTaskMem ( addr ) ;
36
50
51
+ [ MethImpl ( AggressiveInlining ) ]
37
52
public static T * NULL < T > ( ) where T : unmanaged => ( T * ) 0 ;
38
53
54
+ [ MethImpl ( AggressiveInlining ) ]
39
55
public static GCHandle Pin < T > ( T [ ] arr ) where T : unmanaged => GCHandle . Alloc ( arr , GCHandleType . Pinned ) ;
40
56
41
- public static void Copy ( void * to , void * from , int len )
42
- {
43
- Buffer . MemoryCopy ( from , to , len , len ) ;
44
- }
45
-
46
- public static void Copy < T > ( void * to , T [ ] from , int len ) where T : unmanaged
47
- {
48
- fixed ( void * fromPtr = from )
49
- Buffer . MemoryCopy ( fromPtr , to , len , len ) ;
50
- }
57
+ [ MethImpl ( AggressiveInlining ) ]
58
+ public static void Copy ( void * to , void * from , int len ) => Buffer . MemoryCopy ( from , to , len , len ) ;
51
59
60
+ [ MethImpl ( AggressiveInlining ) ]
52
61
public static void Copy < T > ( void * to , T [ ] from ) where T : unmanaged
53
62
{
54
63
int length = sizeof ( T ) * from . Length ;
55
64
fixed ( void * fromPtr = from )
56
65
Buffer . MemoryCopy ( fromPtr , to , length , length ) ;
57
66
}
58
67
68
+ [ MethImpl ( AggressiveInlining ) ]
59
69
public static void Copy < T , T2 > ( T [ ] to , T2 [ ] from , int len ) where T : unmanaged where T2 : unmanaged
60
70
{
61
71
fixed ( void * fromPtr = from )
62
72
fixed ( void * toPtr = to )
63
73
Buffer . MemoryCopy ( fromPtr , toPtr , len , len ) ;
64
74
}
65
75
76
+ [ MethImpl ( AggressiveInlining ) ]
66
77
public static void Copy < T , T2 > ( T [ ] to , T2 [ ] from ) where T : unmanaged where T2 : unmanaged
67
78
{
68
79
int length = sizeof ( T2 ) * from . Length ;
@@ -71,33 +82,46 @@ public static void Copy<T, T2>(T[] to, T2[] from) where T : unmanaged where T2 :
71
82
Buffer . MemoryCopy ( fromPtr , toPtr , length , length ) ;
72
83
}
73
84
85
+ [ MethImpl ( AggressiveInlining ) ]
74
86
public static void Copy < T > ( T [ ] to , void * from , int len ) where T : unmanaged
75
87
{
76
88
fixed ( void * toPtr = to )
77
89
Buffer . MemoryCopy ( from , toPtr , len , len ) ;
78
90
}
79
91
92
+ [ MethImpl ( AggressiveInlining ) ]
80
93
public static void Copy < T > ( T [ ] to , void * from ) where T : unmanaged
81
94
{
82
95
int length = sizeof ( T ) * to . Length ;
83
96
fixed ( void * toPtr = to )
84
97
Buffer . MemoryCopy ( from , toPtr , length , length ) ;
85
98
}
86
99
100
+ [ MethImpl ( AggressiveInlining ) ]
87
101
public static byte [ ] Read ( void * ptr , int len )
88
102
{
89
103
byte [ ] bytes = new byte [ len ] ;
90
- fixed ( byte * bytesPtr = bytes )
91
- Copy ( bytesPtr , ptr , len ) ;
104
+ Copy ( bytes , ptr ) ;
105
+ return bytes ;
106
+ }
107
+
108
+ [ MethImpl ( AggressiveInlining ) ]
109
+ public static T [ ] Read < T > ( void * ptr , int len ) where T : unmanaged
110
+ {
111
+ T [ ] bytes = new T [ len ] ;
112
+ Copy ( bytes , ptr ) ;
92
113
return bytes ;
93
114
}
94
115
116
+ [ MethImpl ( AggressiveInlining ) ]
95
117
public static void WriteStruct < T > ( T str , void * ptr ) where T : unmanaged => Copy ( & str , ptr , sizeof ( T ) ) ;
118
+ [ MethImpl ( AggressiveInlining ) ]
96
119
public static void WriteStruct < T > ( T str , byte [ ] arr ) where T : unmanaged
97
120
{
98
121
fixed ( byte * ptr = arr )
99
122
WriteStruct ( str , ptr ) ;
100
123
}
101
124
102
- public static byte [ ] ReadStruct < T > ( T str ) where T : unmanaged => Read ( & str , sizeof ( T ) ) ;
125
+ [ MethImpl ( AggressiveInlining ) ]
126
+ public static byte [ ] ReadStruct < T > ( T str ) where T : unmanaged => Read < byte > ( & str , sizeof ( T ) ) ;
103
127
}
0 commit comments