Feature #2126: ruby_init_stack() - add ability to specify or query max_stack_size
http://redmine.ruby-lang.org/issues/show/2126

Author: Suraj Kurapati
Status: Open, Priority: Normal

Hello,

== Background ==

I am embedding Ruby 1.9 inside a callback function of a C program (a Verilog
simulator, to be precise):

1. The first time when the callback function is called, I will initialize Ruby
   and create a Fiber (to run some Ruby code).

2. 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().

3. 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:


1. 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);


2. 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);


3. 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.


----------------------------------------
http://redmine.ruby-lang.org