forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.hpp
248 lines (212 loc) · 7.72 KB
/
memory.hpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#pragma once
#include <nall/algorithm.hpp>
#include <nall/stdint.hpp>
namespace nall::memory {
template<typename T = u8, u32 Align = 0> auto allocate(u32 size) -> T*;
template<typename T = u8, u32 Align = 0> auto allocate(u32 size, const T& value) -> T*;
template<typename T = u8, u32 Align = 0> auto resize(void* target, u32 size) -> T*;
template<typename T = u8, u32 Align = 0> auto free(void* target) -> void;
template<typename T = u8> auto compare(const void* target, u32 capacity, const void* source, u32 size) -> s32;
template<typename T = u8> auto compare(const void* target, const void* source, u32 size) -> s32;
template<typename T = u8> auto icompare(const void* target, u32 capacity, const void* source, u32 size) -> s32;
template<typename T = u8> auto icompare(const void* target, const void* source, u32 size) -> s32;
template<typename T = u8> auto copy(void* target, u32 capacity, const void* source, u32 size) -> T*;
template<typename T = u8> auto copy(void* target, const void* source, u32 size) -> T*;
template<typename T = u8> auto move(void* target, u32 capacity, const void* source, u32 size) -> T*;
template<typename T = u8> auto move(void* target, const void* source, u32 size) -> T*;
template<typename T = u8> auto fill(void* target, u32 capacity, const T& value = {}) -> T*;
template<typename T> auto assign(T* target) -> void {}
template<typename T, typename U, typename... P> auto assign(T* target, const U& value, P&&... p) -> void;
template<u32 size, typename T = u64> auto readl(const void* source) -> T;
template<u32 size, typename T = u64> auto readm(const void* source) -> T;
template<u32 size, typename T = u64> auto writel(void* target, T data) -> void;
template<u32 size, typename T = u64> auto writem(void* target, T data) -> void;
auto map(u32 size, bool executable) -> void*;
auto unmap(void* target, u32 size) -> void;
auto protect(void* target, u32 size, bool executable) -> void;
auto jitprotect(bool executable) -> void;
}
namespace nall::memory {
//implementation notes:
//memcmp, memcpy, memmove have terrible performance on small block sizes (FreeBSD 10.0-amd64)
//as this library is used extensively by nall/string, and most strings tend to be small,
//this library hand-codes these functions instead. surprisingly, it's a substantial speedup
template<typename T, u32 Align> auto allocate(u32 size) -> T* {
if constexpr(Align == 0) {
return (T*)malloc(size * sizeof(T));
}
#if defined(API_WINDOWS)
return (T*)_aligned_malloc(size * sizeof(T), Align);
#elif defined(API_POSIX)
T* result = nullptr;
posix_memalign((void**)&result, Align, size * sizeof(T));
return result;
#else
return (T*)malloc(size * sizeof(T));
#endif
}
template<typename T, u32 Align> auto allocate(u32 size, const T& value) -> T* {
auto result = allocate<T, Align>(size);
if(result) fill<T>(result, size, value);
return result;
}
template<typename T, u32 Align> auto resize(void* target, u32 size) -> T* {
if constexpr(Align == 0) {
return (T*)realloc(target, size * sizeof(T));
}
#if defined(API_WINDOWS)
return (T*)_aligned_realloc(target, size * sizeof(T), Align);
#elif defined(API_POSIX)
//realloc() cannot be used safely with posix_memalign(); a copy is always required
T* result = allocate<T, Align>(size);
copy<T>(result, target, size);
free(target);
return result;
#else
return (T*)realloc(target, size * sizeof(T));
#endif
}
template<typename T, u32 Align> auto free(void* target) -> void {
if constexpr(Align == 0) {
::free(target);
return;
}
#if defined(API_WINDOWS)
_aligned_free(target);
#else
::free(target);
#endif
}
template<typename T> auto compare(const void* target, u32 capacity, const void* source, u32 size) -> s32 {
auto t = (u8*)target;
auto s = (u8*)source;
auto l = min(capacity, size) * sizeof(T);
while(l--) {
auto x = *t++;
auto y = *s++;
if(x != y) return x - y;
}
if(capacity == size) return 0;
return -(capacity < size);
}
template<typename T> auto compare(const void* target, const void* source, u32 size) -> s32 {
return compare<T>(target, size, source, size);
}
template<typename T> auto icompare(const void* target, u32 capacity, const void* source, u32 size) -> s32 {
auto t = (u8*)target;
auto s = (u8*)source;
auto l = min(capacity, size) * sizeof(T);
while(l--) {
auto x = *t++;
auto y = *s++;
if(x - 'A' < 26) x += 32;
if(y - 'A' < 26) y += 32;
if(x != y) return x - y;
}
return -(capacity < size);
}
template<typename T> auto icompare(const void* target, const void* source, u32 size) -> s32 {
return icompare<T>(target, size, source, size);
}
template<typename T> auto copy(void* target, u32 capacity, const void* source, u32 size) -> T* {
auto t = (u8*)target;
auto s = (u8*)source;
auto l = min(capacity, size) * sizeof(T);
while(l--) *t++ = *s++;
return (T*)target;
}
template<typename T> auto copy(void* target, const void* source, u32 size) -> T* {
return copy<T>(target, size, source, size);
}
template<typename T> auto move(void* target, u32 capacity, const void* source, u32 size) -> T* {
auto t = (u8*)target;
auto s = (u8*)source;
auto l = min(capacity, size) * sizeof(T);
if(t < s) {
while(l--) *t++ = *s++;
} else {
t += l;
s += l;
while(l--) *--t = *--s;
}
return (T*)target;
}
template<typename T> auto move(void* target, const void* source, u32 size) -> T* {
return move<T>(target, size, source, size);
}
template<typename T> auto fill(void* target, u32 capacity, const T& value) -> T* {
auto t = (T*)target;
while(capacity--) *t++ = value;
return (T*)target;
}
template<typename T, typename U, typename... P> auto assign(T* target, const U& value, P&&... p) -> void {
*target++ = value;
assign(target, std::forward<P>(p)...);
}
template<u32 size, typename T> auto readl(const void* source) -> T {
auto p = (const u8*)source;
T data = 0;
for(u32 n = 0; n < size; n++) data |= T(*p++) << n * 8;
return data;
}
template<u32 size, typename T> auto readm(const void* source) -> T {
auto p = (const u8*)source;
T data = 0;
for(s32 n = size - 1; n >= 0; n--) data |= T(*p++) << n * 8;
return data;
}
template<u32 size, typename T> auto writel(void* target, T data) -> void {
auto p = (u8*)target;
for(u32 n = 0; n < size; n++) *p++ = data >> n * 8;
}
template<u32 size, typename T> auto writem(void* target, T data) -> void {
auto p = (u8*)target;
for(s32 n = size - 1; n >= 0; n--) *p++ = data >> n * 8;
}
inline auto map(u32 size, bool executable) -> void* {
#if defined(API_WINDOWS)
DWORD protect = executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
return VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, protect);
#elif defined(API_POSIX)
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_ANON | MAP_PRIVATE;
if(executable) {
prot |= PROT_EXEC;
#if defined(PLATFORM_MACOS)
flags |= MAP_JIT;
#endif
}
return mmap(nullptr, size, prot, flags, -1, 0);
#else
return nullptr;
#endif
}
inline auto unmap(void* target, u32 size) -> void {
#if defined(API_WINDOWS)
VirtualFree(target, 0, MEM_RELEASE);
#elif defined(API_POSIX)
munmap(target, size);
#endif
}
inline auto protect(void* target, u32 size, bool executable) -> void {
#if defined(API_WINDOWS)
DWORD protect = executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
DWORD oldProtect;
VirtualProtect(target, size, protect, &oldProtect);
#elif defined(API_POSIX)
int prot = PROT_READ | PROT_WRITE;
if(executable) {
prot |= PROT_EXEC;
}
int ret = mprotect(target, size, prot);
assert(ret == 0);
#endif
}
inline auto jitprotect(bool executable) -> void {
#if defined(PLATFORM_MACOS)
if(__builtin_available(macOS 11.0, *)) {
pthread_jit_write_protect_np(executable);
}
#endif
}
}