Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 770ea55

Browse files
author
Hans-Kristian Arntzen
committed
Implement POSIX path for JIT allocator.
1 parent 9b0cf9e commit 770ea55

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

jit_allocator.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Allocator::~Allocator()
2424
for (auto &block : blocks)
2525
VirtualFree(block.code, 0, MEM_RELEASE);
2626
#else
27+
for (auto &block : blocks)
28+
munmap(block.code, block.size);
2729
#endif
2830
}
2931

@@ -37,6 +39,7 @@ static bool commit_read_write(void *ptr, size_t size)
3739
#ifdef _WIN32
3840
return VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE) == ptr;
3941
#else
42+
return mprotect(ptr, size, PROT_READ | PROT_WRITE) == 0;
4043
#endif
4144
}
4245

@@ -46,6 +49,7 @@ static bool commit_execute(void *ptr, size_t size)
4649
DWORD old_protect;
4750
return VirtualProtect(ptr, align_page(size), PAGE_EXECUTE, &old_protect) != 0;
4851
#else
52+
return mprotect(ptr, size, PROT_EXEC) == 0;
4953
#endif
5054
}
5155

@@ -76,7 +80,7 @@ void *Allocator::allocate_code(size_t size)
7680
block = &blocks.back();
7781
}
7882

79-
if (!block)
83+
if (!block || !block->code)
8084
return nullptr;
8185

8286
void *ret = block->code + block->offset;
@@ -95,6 +99,10 @@ Allocator::Block Allocator::reserve_block(size_t size)
9599
block.size = size;
96100
return block;
97101
#else
102+
block.code = static_cast<uint8_t *>(mmap(nullptr, size, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE,
103+
-1, 0));
104+
block.size = size;
105+
return block;
98106
#endif
99107
}
100108
}

0 commit comments

Comments
 (0)