1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Use VirtualAlloc/VirtualProtect/VirtualFree for windows stack allocation.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
samuel 2018-11-22 02:17:44 +00:00
parent e7d919d265
commit 38f7bb481e

24
cont.c
View file

@ -416,10 +416,11 @@ cont_free(void *ptr)
rb_bug("Illegal root fiber parameter"); rb_bug("Illegal root fiber parameter");
} }
#ifdef _WIN32 #ifdef _WIN32
free((void*)fib->ss_sp); VirtualFree((void*)fib->ss_sp, 0, MEM_RELEASE);
#else #else
munmap((void*)fib->ss_sp, fib->ss_size); munmap((void*)fib->ss_sp, fib->ss_size);
#endif #endif
fib->ss_sp = NULL;
} }
#elif defined(_WIN32) #elif defined(_WIN32)
if (!fiber_is_root_p(fib)) { if (!fiber_is_root_p(fib)) {
@ -870,6 +871,9 @@ static char*
fiber_machine_stack_alloc(size_t size) fiber_machine_stack_alloc(size_t size)
{ {
char *ptr; char *ptr;
#ifdef _WIN32
DWORD old_protect;
#endif
if (machine_stack_cache_index > 0) { if (machine_stack_cache_index > 0) {
if (machine_stack_cache[machine_stack_cache_index - 1].size == (size / sizeof(VALUE))) { if (machine_stack_cache[machine_stack_cache_index - 1].size == (size / sizeof(VALUE))) {
@ -877,15 +881,21 @@ fiber_machine_stack_alloc(size_t size)
machine_stack_cache_index--; machine_stack_cache_index--;
machine_stack_cache[machine_stack_cache_index].ptr = NULL; machine_stack_cache[machine_stack_cache_index].ptr = NULL;
machine_stack_cache[machine_stack_cache_index].size = 0; machine_stack_cache[machine_stack_cache_index].size = 0;
} } else {
else{
/* TODO handle multiple machine stack size */ /* TODO handle multiple machine stack size */
rb_bug("machine_stack_cache size is not canonicalized"); rb_bug("machine_stack_cache size is not canonicalized");
} }
} } else {
else {
#ifdef _WIN32 #ifdef _WIN32
return malloc(size); ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
if (!ptr) {
rb_raise(rb_eFiberError, "can't allocate machine stack to fiber: %s", ERRNOMSG);
}
if (!VirtualProtect(ptr, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
}
#else #else
void *page; void *page;
STACK_GROW_DIR_DETECTION; STACK_GROW_DIR_DETECTION;
@ -1725,7 +1735,7 @@ fiber_store(rb_fiber_t *next_fib, rb_thread_t *th)
else { else {
if (terminated_machine_stack.ptr != fib->cont.machine.stack) { if (terminated_machine_stack.ptr != fib->cont.machine.stack) {
#ifdef _WIN32 #ifdef _WIN32
free((void*)terminated_machine_stack.ptr); VirtualFree(terminated_machine_stack.ptr, 0, MEM_RELEASE);
#else #else
munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE)); munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
#endif #endif