Skip to content

Commit d375d85

Browse files
committed
Added new Read method.
Added Hard Inlining attribute for all methods. Simplified code.
1 parent 395d910 commit d375d85

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

Memory/MemEx.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,79 @@
11
using System.Runtime.InteropServices;
22
using System;
3+
using System.Runtime.CompilerServices;
4+
5+
using MethImpl = System.Runtime.CompilerServices.MethodImplAttribute;
6+
using static System.Runtime.CompilerServices.MethodImplOptions;
37

48
namespace Memory;
59
public unsafe static class MemEx
610
{
11+
[MethImpl(AggressiveInlining)]
712
public static T* New<T>() where T : unmanaged => NewAlloc<T>();
13+
[MethImpl(AggressiveInlining)]
814
public static T* New<T>(T val) where T : unmanaged => NewAlloc(&val);
915

16+
[MethImpl(AggressiveInlining)]
1017
public static T* NewAlloc<T>(T* from) where T : unmanaged
1118
{
1219
T* to = (T*)Marshal.AllocCoTaskMem(sizeof(T));
1320
*to = *from;
1421
return to;
1522
}
1623

24+
[MethImpl(AggressiveInlining)]
1725
public static T* NewAlloc<T>() where T : unmanaged => (T*)Marshal.AllocCoTaskMem(sizeof(T));
1826

27+
[MethImpl(AggressiveInlining)]
1928
public static byte* Alloc(int count) => (byte*)Marshal.AllocCoTaskMem(count);
29+
[MethImpl(AggressiveInlining)]
2030
public static T* Alloc<T>(int count) where T : unmanaged => (T*)Marshal.AllocCoTaskMem(count * sizeof(T));
2131

32+
[MethImpl(AggressiveInlining)]
2233
public static T* AllocFrom<T>(T[] arr) where T : unmanaged
2334
{
2435
T* ptr = Alloc<T>(arr.Length);
2536
Copy(ptr, arr);
2637
return ptr;
2738
}
2839

40+
[MethImpl(AggressiveInlining)]
2941
public static void Free(void* ptr) => Marshal.FreeCoTaskMem((nint)ptr);
42+
[MethImpl(AggressiveInlining)]
3043
public static void Free(params void*[] ptrs)
3144
{
3245
foreach (void* ptr in ptrs)
3346
Marshal.FreeCoTaskMem((nint)ptr);
3447
}
48+
[MethImpl(AggressiveInlining)]
3549
public static void Free(nint addr) => Marshal.FreeCoTaskMem(addr);
3650

51+
[MethImpl(AggressiveInlining)]
3752
public static T* NULL<T>() where T : unmanaged => (T*)0;
3853

54+
[MethImpl(AggressiveInlining)]
3955
public static GCHandle Pin<T>(T[] arr) where T : unmanaged => GCHandle.Alloc(arr, GCHandleType.Pinned);
4056

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);
5159

60+
[MethImpl(AggressiveInlining)]
5261
public static void Copy<T>(void* to, T[] from) where T : unmanaged
5362
{
5463
int length = sizeof(T) * from.Length;
5564
fixed (void* fromPtr = from)
5665
Buffer.MemoryCopy(fromPtr, to, length, length);
5766
}
5867

68+
[MethImpl(AggressiveInlining)]
5969
public static void Copy<T, T2>(T[] to, T2[] from, int len) where T : unmanaged where T2 : unmanaged
6070
{
6171
fixed (void* fromPtr = from)
6272
fixed (void* toPtr = to)
6373
Buffer.MemoryCopy(fromPtr, toPtr, len, len);
6474
}
6575

76+
[MethImpl(AggressiveInlining)]
6677
public static void Copy<T, T2>(T[] to, T2[] from) where T : unmanaged where T2 : unmanaged
6778
{
6879
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 :
7182
Buffer.MemoryCopy(fromPtr, toPtr, length, length);
7283
}
7384

85+
[MethImpl(AggressiveInlining)]
7486
public static void Copy<T>(T[] to, void* from, int len) where T : unmanaged
7587
{
7688
fixed (void* toPtr = to)
7789
Buffer.MemoryCopy(from, toPtr, len, len);
7890
}
7991

92+
[MethImpl(AggressiveInlining)]
8093
public static void Copy<T>(T[] to, void* from) where T : unmanaged
8194
{
8295
int length = sizeof(T) * to.Length;
8396
fixed (void* toPtr = to)
8497
Buffer.MemoryCopy(from, toPtr, length, length);
8598
}
8699

100+
[MethImpl(AggressiveInlining)]
87101
public static byte[] Read(void* ptr, int len)
88102
{
89103
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);
92113
return bytes;
93114
}
94115

116+
[MethImpl(AggressiveInlining)]
95117
public static void WriteStruct<T>(T str, void* ptr) where T : unmanaged => Copy(&str, ptr, sizeof(T));
118+
[MethImpl(AggressiveInlining)]
96119
public static void WriteStruct<T>(T str, byte[] arr) where T : unmanaged
97120
{
98121
fixed (byte* ptr = arr)
99122
WriteStruct(str, ptr);
100123
}
101124

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));
103127
}

0 commit comments

Comments
 (0)