Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/memory.cpp
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include "memory.h"
20
21
#include <cstdlib>
22
23
#if __has_include("features.h")
24
#include <features.h>
25
#endif
26
27
#if defined(__linux__) && !defined(__ANDROID__)
28
#include <sys/mman.h>
29
#endif
30
31
#if defined(__APPLE__) || defined(__ANDROID__) || defined(__OpenBSD__) \
32
|| (defined(__GLIBCXX__) && !defined(_GLIBCXX_HAVE_ALIGNED_ALLOC) && !defined(_WIN32)) \
33
|| defined(__e2k__)
34
#define POSIXALIGNEDALLOC
35
#include <stdlib.h>
36
#endif
37
38
#ifdef _WIN32
39
#if _WIN32_WINNT < 0x0601
40
#undef _WIN32_WINNT
41
#define _WIN32_WINNT 0x0601 // Force to include needed API prototypes
42
#endif
43
44
#ifndef NOMINMAX
45
#define NOMINMAX
46
#endif
47
48
#include <ios> // std::hex, std::dec
49
#include <iostream> // std::cerr
50
#include <ostream> // std::endl
51
#include <windows.h>
52
53
// The needed Windows API for processor groups could be missed from old Windows
54
// versions, so instead of calling them directly (forcing the linker to resolve
55
// the calls at compile time), try to load them at runtime. To do this we need
56
// first to define the corresponding function pointers.
57
58
extern "C" {
59
using OpenProcessToken_t = bool (*)(HANDLE, DWORD, PHANDLE);
60
using LookupPrivilegeValueA_t = bool (*)(LPCSTR, LPCSTR, PLUID);
61
using AdjustTokenPrivileges_t =
62
bool (*)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
63
}
64
#endif
65
66
67
namespace Stockfish {
68
69
// Wrappers for systems where the c++17 implementation does not guarantee the
70
// availability of aligned_alloc(). Memory allocated with std_aligned_alloc()
71
// must be freed with std_aligned_free().
72
73
void* std_aligned_alloc(size_t alignment, size_t size) {
74
#if defined(_ISOC11_SOURCE)
75
return aligned_alloc(alignment, size);
76
#elif defined(POSIXALIGNEDALLOC)
77
void* mem = nullptr;
78
posix_memalign(&mem, alignment, size);
79
return mem;
80
#elif defined(_WIN32) && !defined(_M_ARM) && !defined(_M_ARM64)
81
return _mm_malloc(size, alignment);
82
#elif defined(_WIN32)
83
return _aligned_malloc(size, alignment);
84
#else
85
return std::aligned_alloc(alignment, size);
86
#endif
87
}
88
89
void std_aligned_free(void* ptr) {
90
91
#if defined(POSIXALIGNEDALLOC)
92
free(ptr);
93
#elif defined(_WIN32) && !defined(_M_ARM) && !defined(_M_ARM64)
94
_mm_free(ptr);
95
#elif defined(_WIN32)
96
_aligned_free(ptr);
97
#else
98
free(ptr);
99
#endif
100
}
101
102
// aligned_large_pages_alloc() will return suitably aligned memory,
103
// if possible using large pages.
104
105
#if defined(_WIN32)
106
107
static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize) {
108
109
#if !defined(_WIN64)
110
return nullptr;
111
#else
112
113
HANDLE hProcessToken{};
114
LUID luid{};
115
void* mem = nullptr;
116
117
const size_t largePageSize = GetLargePageMinimum();
118
if (!largePageSize)
119
return nullptr;
120
121
// Dynamically link OpenProcessToken, LookupPrivilegeValue and AdjustTokenPrivileges
122
123
HMODULE hAdvapi32 = GetModuleHandle(TEXT("advapi32.dll"));
124
125
if (!hAdvapi32)
126
hAdvapi32 = LoadLibrary(TEXT("advapi32.dll"));
127
128
auto OpenProcessToken_f =
129
OpenProcessToken_t((void (*)()) GetProcAddress(hAdvapi32, "OpenProcessToken"));
130
if (!OpenProcessToken_f)
131
return nullptr;
132
auto LookupPrivilegeValueA_f =
133
LookupPrivilegeValueA_t((void (*)()) GetProcAddress(hAdvapi32, "LookupPrivilegeValueA"));
134
if (!LookupPrivilegeValueA_f)
135
return nullptr;
136
auto AdjustTokenPrivileges_f =
137
AdjustTokenPrivileges_t((void (*)()) GetProcAddress(hAdvapi32, "AdjustTokenPrivileges"));
138
if (!AdjustTokenPrivileges_f)
139
return nullptr;
140
141
// We need SeLockMemoryPrivilege, so try to enable it for the process
142
143
if (!OpenProcessToken_f( // OpenProcessToken()
144
GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hProcessToken))
145
return nullptr;
146
147
if (LookupPrivilegeValueA_f(nullptr, "SeLockMemoryPrivilege", &luid))
148
{
149
TOKEN_PRIVILEGES tp{};
150
TOKEN_PRIVILEGES prevTp{};
151
DWORD prevTpLen = 0;
152
153
tp.PrivilegeCount = 1;
154
tp.Privileges[0].Luid = luid;
155
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
156
157
// Try to enable SeLockMemoryPrivilege. Note that even if AdjustTokenPrivileges()
158
// succeeds, we still need to query GetLastError() to ensure that the privileges
159
// were actually obtained.
160
161
if (AdjustTokenPrivileges_f(hProcessToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &prevTp,
162
&prevTpLen)
163
&& GetLastError() == ERROR_SUCCESS)
164
{
165
// Round up size to full pages and allocate
166
allocSize = (allocSize + largePageSize - 1) & ~size_t(largePageSize - 1);
167
mem = VirtualAlloc(nullptr, allocSize, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES,
168
PAGE_READWRITE);
169
170
// Privilege no longer needed, restore previous state
171
AdjustTokenPrivileges_f(hProcessToken, FALSE, &prevTp, 0, nullptr, nullptr);
172
}
173
}
174
175
CloseHandle(hProcessToken);
176
177
return mem;
178
179
#endif
180
}
181
182
void* aligned_large_pages_alloc(size_t allocSize) {
183
184
// Try to allocate large pages
185
void* mem = aligned_large_pages_alloc_windows(allocSize);
186
187
// Fall back to regular, page-aligned, allocation if necessary
188
if (!mem)
189
mem = VirtualAlloc(nullptr, allocSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
190
191
return mem;
192
}
193
194
#else
195
196
void* aligned_large_pages_alloc(size_t allocSize) {
197
198
#if defined(__linux__)
199
constexpr size_t alignment = 2 * 1024 * 1024; // 2MB page size assumed
200
#else
201
constexpr size_t alignment = 4096; // small page size assumed
202
#endif
203
204
// Round up to multiples of alignment
205
size_t size = ((allocSize + alignment - 1) / alignment) * alignment;
206
void* mem = std_aligned_alloc(alignment, size);
207
#if defined(MADV_HUGEPAGE)
208
madvise(mem, size, MADV_HUGEPAGE);
209
#endif
210
return mem;
211
}
212
213
#endif
214
215
bool has_large_pages() {
216
217
#if defined(_WIN32)
218
219
constexpr size_t page_size = 2 * 1024 * 1024; // 2MB page size assumed
220
void* mem = aligned_large_pages_alloc_windows(page_size);
221
if (mem == nullptr)
222
{
223
return false;
224
}
225
else
226
{
227
aligned_large_pages_free(mem);
228
return true;
229
}
230
231
#elif defined(__linux__)
232
233
#if defined(MADV_HUGEPAGE)
234
return true;
235
#else
236
return false;
237
#endif
238
239
#else
240
241
return false;
242
243
#endif
244
}
245
246
247
// aligned_large_pages_free() will free the previously memory allocated
248
// by aligned_large_pages_alloc(). The effect is a nop if mem == nullptr.
249
250
#if defined(_WIN32)
251
252
void aligned_large_pages_free(void* mem) {
253
254
if (mem && !VirtualFree(mem, 0, MEM_RELEASE))
255
{
256
DWORD err = GetLastError();
257
std::cerr << "Failed to free large page memory. Error code: 0x" << std::hex << err
258
<< std::dec << std::endl;
259
exit(EXIT_FAILURE);
260
}
261
}
262
263
#else
264
265
void aligned_large_pages_free(void* mem) { std_aligned_free(mem); }
266
267
#endif
268
} // namespace Stockfish
269
270