forked from trungnt2910/MemoryModule.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStyleMemory.cs
More file actions
119 lines (111 loc) · 3.43 KB
/
Copy pathCStyleMemory.cs
File metadata and controls
119 lines (111 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace MemoryModule
{
internal static unsafe class CStyleMemory
{
private const int magic = 0x414d_534c;
public static unsafe void* calloc(uint nitems, uint count)
{
uint size = nitems * count;
void* voidPtr = malloc(size);
if (voidPtr != null)
{
byte* numPtr = (byte*)voidPtr;
int index = 0;
while (true)
{
if (index >= size)
{
break;
}
numPtr[index] = 0;
index++;
}
}
return voidPtr;
}
public static unsafe void free(void* item)
{
if ((item != null) && mem_is_valid(item))
{
item = (void *)IntPtr.Subtract((IntPtr)item, 8);
Marshal.FreeHGlobal(new IntPtr(item));
}
}
public static unsafe void* malloc(uint size)
{
void* voidPtr = Marshal.AllocHGlobal((int)(((int)size) + 8)).ToPointer();
if (voidPtr != null)
{
*((int*)voidPtr) = 0x414d_534c;
*((uint*)(IntPtr.Add((IntPtr)voidPtr, 4))) = size;
voidPtr = (void *)IntPtr.Add((IntPtr)voidPtr, 8);
}
return voidPtr;
}
private static unsafe bool mem_is_valid(void* item)
{
item = (void*)IntPtr.Subtract((IntPtr)item, 8);
return (*(((int*)item)) == 0x414d_534c);
}
public static unsafe void* realloc(void* item, uint size)
{
void* voidPtr;
if (item == null)
{
voidPtr = malloc(size);
}
else if (size == 0)
{
free(item);
voidPtr = null;
}
else if (!mem_is_valid(item))
{
voidPtr = null;
}
else
{
uint num = *((uint*)(IntPtr.Subtract((IntPtr)item, 4)));
if (size < num)
{
num = size;
}
void* voidPtr2 = malloc(size);
if (voidPtr2 != null)
{
int num2 = 0;
while (true)
{
if (num2 >= num)
{
break;
}
*((byte*)(IntPtr.Add((IntPtr)voidPtr2, num2))) = *((byte*)(IntPtr.Add((IntPtr)item, num2)));
num2++;
}
}
free(item);
voidPtr = voidPtr2;
}
return voidPtr;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void* memcpy(void* __dest, void* __src, uint __n)
{
Unsafe.CopyBlockUnaligned(__dest, __src, __n);
return __dest;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void* memset(void* __s, int __c, uint __n)
{
Unsafe.InitBlockUnaligned(__s, (byte)__c, __n);
return __s;
}
}
}