Feature #2126
closedruby_init_stack() - add ability to specify or query max_stack_size
Description
=begin
Hello,
== Background ==
I am embedding Ruby 1.9 inside a callback function of a C program (a Verilog
simulator, to be precise):
-
The first time when the callback function is called, I will initialize Ruby
and create a Fiber (to run some Ruby code). -
For all subsequent times when the callback function is called, I will resume
the Fiber. The Fiber will then run some Ruby code and return control back to
the callback function, using Fiber.yield(). -
This process repeats until the callback function is no longer called by the C
program.
== Problem ==
Each time when the callback function is called, the C program's current stack
pointer is different: sometimes it descends below Ruby's stack start
addresss!
For this reason, it is not safe to use the default RUBY_INIT_STACK macro (which
assumes that the C program's current stack pointer never descends below Ruby's
stack start addresss).
== Request for Solution ==
To solve the problem, I need one of the following features:
-
Add a max_stack_size parameter to ruby_init_stack():
void ruby_init_stack(VALUE* stack_start, size_t max_stack_size);
And also add a function to return Ruby's preferred stack size:
size_t ruby_preferred_stack_size();
This way, I can provide a custom stack to Ruby:
size_t stack_size = ruby_preferred_stack_size();
VALUE* stack_end = malloc(sizeof(VALUE) * stack_size);
VALUE* stack_start = stack_end + stack_size;ruby_init_stack(stack_start, stack_size);
-
Make ruby_init_stack() return the maximum stack size it has chosen:
size_t ruby_init_stack(VALUE* stack_start);
This way, I can grow my custom stack to fit Ruby's needs:
VALUE* stack_end = malloc(sizeof(VALUE));
size_t stack_size = ruby_init_stack(stack_end);
VALUE* new_stack_end = realloc(stack_end, stack_size); -
Add a RUBY_AUTO_STACK macro which makes Ruby allocate its own stack on the
heap using malloc(). I would call this macro instead of RUBY_INIT_STACK.
Thanks for your consideration.
=end