1
+ using System . Runtime . InteropServices ;
2
+ using System ;
3
+
4
+ namespace Memory ;
5
+ public unsafe static class MemEx
6
+ {
7
+ public static T * New < T > ( ) where T : unmanaged => NewAlloc < T > ( ) ;
8
+ public static T * New < T > ( T val ) where T : unmanaged => NewAlloc ( & val ) ;
9
+
10
+ public static T * NewAlloc < T > ( T * from ) where T : unmanaged
11
+ {
12
+ T * to = ( T * ) Marshal . AllocCoTaskMem ( sizeof ( T ) ) ;
13
+ * to = * from ;
14
+ return to ;
15
+ }
16
+
17
+ public static T * NewAlloc < T > ( ) where T : unmanaged => ( T * ) Marshal . AllocCoTaskMem ( sizeof ( T ) ) ;
18
+
19
+ public static byte * Alloc ( int count ) => ( byte * ) Marshal . AllocCoTaskMem ( count ) ;
20
+ public static T * Alloc < T > ( int count ) where T : unmanaged => ( T * ) Marshal . AllocCoTaskMem ( count * sizeof ( T ) ) ;
21
+
22
+ public static T * AllocFrom < T > ( T [ ] arr ) where T : unmanaged
23
+ {
24
+ T * ptr = Alloc < T > ( arr . Length ) ;
25
+ Copy ( ptr , arr ) ;
26
+ return ptr ;
27
+ }
28
+
29
+ public static void Free ( void * ptr ) => Marshal . FreeCoTaskMem ( ( nint ) ptr ) ;
30
+ public static void Free ( params void * [ ] ptrs )
31
+ {
32
+ foreach ( void * ptr in ptrs )
33
+ Marshal . FreeCoTaskMem ( ( nint ) ptr ) ;
34
+ }
35
+ public static void Free ( nint addr ) => Marshal . FreeCoTaskMem ( addr ) ;
36
+
37
+ public static T * NULL < T > ( ) where T : unmanaged => ( T * ) 0 ;
38
+
39
+ public static GCHandle Pin < T > ( T [ ] arr ) where T : unmanaged => GCHandle . Alloc ( arr , GCHandleType . Pinned ) ;
40
+
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
+ }
51
+
52
+ public static void Copy < T > ( void * to , T [ ] from ) where T : unmanaged
53
+ {
54
+ int length = sizeof ( T ) * from . Length ;
55
+ fixed ( void * fromPtr = from )
56
+ Buffer . MemoryCopy ( fromPtr , to , length , length ) ;
57
+ }
58
+
59
+ public static void Copy < T , T2 > ( T [ ] to , T2 [ ] from , int len ) where T : unmanaged where T2 : unmanaged
60
+ {
61
+ fixed ( void * fromPtr = from )
62
+ fixed ( void * toPtr = to )
63
+ Buffer . MemoryCopy ( fromPtr , toPtr , len , len ) ;
64
+ }
65
+
66
+ public static void Copy < T , T2 > ( T [ ] to , T2 [ ] from ) where T : unmanaged where T2 : unmanaged
67
+ {
68
+ int length = sizeof ( T2 ) * from . Length ;
69
+ fixed ( void * fromPtr = from )
70
+ fixed ( void * toPtr = to )
71
+ Buffer . MemoryCopy ( fromPtr , toPtr , length , length ) ;
72
+ }
73
+
74
+ public static void Copy < T > ( T [ ] to , void * from , int len ) where T : unmanaged
75
+ {
76
+ fixed ( void * toPtr = to )
77
+ Buffer . MemoryCopy ( from , toPtr , len , len ) ;
78
+ }
79
+
80
+ public static void Copy < T > ( T [ ] to , void * from ) where T : unmanaged
81
+ {
82
+ int length = sizeof ( T ) * to . Length ;
83
+ fixed ( void * toPtr = to )
84
+ Buffer . MemoryCopy ( from , toPtr , length , length ) ;
85
+ }
86
+
87
+ public static byte [ ] Read ( void * ptr , int len )
88
+ {
89
+ byte [ ] bytes = new byte [ len ] ;
90
+ fixed ( byte * bytesPtr = bytes )
91
+ Copy ( bytesPtr , ptr , len ) ;
92
+ return bytes ;
93
+ }
94
+
95
+ public static void WriteStruct < T > ( T str , void * ptr ) where T : unmanaged => Copy ( & str , ptr , sizeof ( T ) ) ;
96
+ public static void WriteStruct < T > ( T str , byte [ ] arr ) where T : unmanaged
97
+ {
98
+ fixed ( byte * ptr = arr )
99
+ WriteStruct ( str , ptr ) ;
100
+ }
101
+
102
+ public static byte [ ] ReadStruct < T > ( T str ) where T : unmanaged => Read ( & str , sizeof ( T ) ) ;
103
+ }
0 commit comments