Skip to content

Commit 132a914

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
Fix code allocator for Windows
Summary: Use `VirtualAlloc` and `VirtualFree` on Windows w/ execute + read/write permissions. Reviewed By: yoney Differential Revision: D91526695 fbshipit-source-id: 454ecc3926127e600d7feb9d21fd14ca3ed81c1f
1 parent 5b5b02a commit 132a914

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

cinderx/Jit/code_allocator.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
#include "cinderx/Jit/config.h"
77
#include "cinderx/Jit/threaded_compile.h"
88

9+
#ifdef WIN32
10+
#include <Windows.h>
11+
#include <memoryapi.h>
12+
#else
913
#include <sys/mman.h>
14+
#endif
1015

1116
#include <cstring>
1217

@@ -27,6 +32,7 @@ constexpr size_t kAllocSize = 1024 * 1024 * 2;
2732

2833
// Allocate memory for JIT'd code.
2934
uint8_t* allocPages(size_t size) {
35+
#ifndef WIN32
3036
void* res = mmap(
3137
nullptr,
3238
size,
@@ -38,6 +44,12 @@ uint8_t* allocPages(size_t size) {
3844
res != MAP_FAILED,
3945
"Failed to allocate {} bytes of memory for code",
4046
size);
47+
#else
48+
void* res = VirtualAllocEx(
49+
nullptr, nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
50+
JIT_CHECK(
51+
res != nullptr, "Failed to allocate {} bytes of memory for code", size);
52+
#endif
4153
return static_cast<uint8_t*>(res);
4254
}
4355

@@ -111,9 +123,13 @@ const asmjit::Environment& CodeAllocator::asmJitEnvironment() const {
111123

112124
CodeAllocatorCinder::~CodeAllocatorCinder() {
113125
for (std::span<uint8_t> alloc : allocations_) {
126+
#ifndef WIN32
114127
JIT_CHECK(
115128
munmap(alloc.data(), alloc.size()) == 0, "Freeing code memory failed");
116129
}
130+
#else
131+
VirtualFree(alloc.data(), 0, MEM_RELEASE);
132+
#endif
117133
}
118134

119135
AllocateResult CodeAllocatorCinder::addCode(asmjit::CodeHolder* code) {
@@ -188,9 +204,13 @@ MultipleSectionCodeAllocator::~MultipleSectionCodeAllocator() {
188204
if (code_alloc_ == nullptr) {
189205
return;
190206
}
207+
#ifndef WIN32
191208
JIT_CHECK(
192209
munmap(code_alloc_, total_allocation_size_) == 0,
193210
"Freeing code sections failed");
211+
#else
212+
VirtualFree(code_alloc_, 0, MEM_RELEASE);
213+
#endif
194214
}
195215

196216
/*

0 commit comments

Comments
 (0)