Skip to content

Commit 19eb972

Browse files
committed
Add project files.
1 parent a8cddc2 commit 19eb972

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

Memory.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33414.496
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory", "Memory\Memory.csproj", "{228B1CE4-CC4C-42B3-947A-44761202163D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{228B1CE4-CC4C-42B3-947A-44761202163D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{228B1CE4-CC4C-42B3-947A-44761202163D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{228B1CE4-CC4C-42B3-947A-44761202163D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{228B1CE4-CC4C-42B3-947A-44761202163D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {436059C8-3BAD-4878-A26A-64E5254023B2}
24+
EndGlobalSection
25+
EndGlobal

Memory/MemEx.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

Memory/Memory.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
<PackageId>Yotic.Memory</PackageId>
9+
<Title>Memory</Title>
10+
<Version>1.0.0</Version>
11+
<Authors>Yotic</Authors>
12+
<Company></Company>
13+
<Description>Memory utils</Description>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<Compile Remove="obj\**" />
18+
<EmbeddedResource Remove="obj\**" />
19+
<None Remove="obj\**" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)