From cb5da39f20a17bd73e791dca1e7e1832f0adbc53 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 19 Jun 2019 18:47:15 +1200 Subject: [PATCH] Use shared implementation of `rb_ec_initialize_vm_stack`. --- cont.c | 14 +------------- thread.c | 11 +---------- vm.c | 28 +++++++++++++++++----------- vm_core.h | 7 +++++++ 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/cont.c b/cont.c index e3db1b32d2..accf93aba0 100644 --- a/cont.c +++ b/cont.c @@ -1530,19 +1530,7 @@ fiber_init(VALUE fibval, VALUE proc) vm_stack = ruby_xmalloc(fib_stack_bytes); } cont->free_vm_stack = 1; - rb_ec_set_vm_stack(sec, vm_stack, fib_stack_bytes / sizeof(VALUE)); - sec->cfp = (void *)(sec->vm_stack + sec->vm_stack_size); - - rb_vm_push_frame(sec, - NULL, - VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME, - Qnil, /* self */ - VM_BLOCK_HANDLER_NONE, - 0, /* specval */ - NULL, /* pc */ - sec->vm_stack, /* sp */ - 0, /* local_size */ - 0); + rb_ec_initialize_vm_stack(sec, vm_stack, fib_stack_bytes / sizeof(VALUE)); sec->tag = NULL; sec->local_storage = NULL; diff --git a/thread.c b/thread.c index cf418da0e4..df6ca01c9d 100644 --- a/thread.c +++ b/thread.c @@ -728,16 +728,7 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_s } vm_stack = alloca(size * sizeof(VALUE)); - rb_ec_set_vm_stack(th->ec, vm_stack, size); - th->ec->cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size); - - rb_vm_push_frame(th->ec, - 0 /* dummy iseq */, - VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */, - Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */, - 0 /* dummy cref/me */, - 0 /* dummy pc */, th->ec->vm_stack, 0, 0 - ); + rb_ec_initialize_vm_stack(th->ec, vm_stack, size); ruby_thread_set_native(th); diff --git a/vm.c b/vm.c index 4b4b037b4f..439464a42f 100644 --- a/vm.c +++ b/vm.c @@ -2685,6 +2685,22 @@ thread_alloc(VALUE klass) return obj; } +void +rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size) +{ + rb_ec_set_vm_stack(ec, stack, size); + + ec->cfp = (void *)(ec->vm_stack + ec->vm_stack_size); + + rb_vm_push_frame(ec, + NULL /* dummy iseq */, + VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */, + Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */, + 0 /* dummy cref/me */, + 0 /* dummy pc */, ec->vm_stack, 0, 0 + ); +} + static void th_init(rb_thread_t *th, VALUE self) { @@ -2693,17 +2709,7 @@ th_init(rb_thread_t *th, VALUE self) if (self == 0) { size_t size = th->vm->default_params.thread_vm_stack_size / sizeof(VALUE); - rb_ec_set_vm_stack(th->ec, ALLOC_N(VALUE, size), size); - - th->ec->cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size); - - rb_vm_push_frame(th->ec, - 0 /* dummy iseq */, - VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */, - Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */, - 0 /* dummy cref/me */, - 0 /* dummy pc */, th->ec->vm_stack, 0, 0 - ); + rb_ec_initialize_vm_stack(th->ec, ALLOC_N(VALUE, size), size); } else { VM_ASSERT(th->ec->cfp == NULL); VM_ASSERT(th->ec->vm_stack == NULL); diff --git a/vm_core.h b/vm_core.h index a0f9388df7..7860c2d505 100644 --- a/vm_core.h +++ b/vm_core.h @@ -903,8 +903,15 @@ typedef struct rb_execution_context_struct { } machine; } rb_execution_context_t; +// Set the vm_stack pointer in the execution context. void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size); +// Initialize the vm_stack pointer in the execution context and push the initial stack frame. +// @param ec the execution context to update. +// @param stack a pointer to the stack to use. +// @param size the size of the stack, as in `VALUE stack[size]`. +void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size); + typedef struct rb_thread_struct { struct list_node vmlt_node; VALUE self;