GC and the stack

Hello,

As far as I understand the ruby gc will not collect objects that are
referenced from the stack, ie the “C/native” stack in a ruby extension
module.

How does the ruby gc recognize what on the stack is a reference to a ruby
object? The types on and layout of the values in a particular stack frame is
not available at runtime is it?

The initial joy of the simplicity of the ruby C API has for me turned into a
fear of not knowing what is going on concerning especially garbage
collection.

Thomas

Hi,

···

At Wed, 8 Oct 2003 20:08:13 +0900, Thomas Sondergaard wrote:

How does the ruby gc recognize what on the stack is a reference to a ruby
object? The types on and layout of the values in a particular stack frame is
not available at runtime is it?

By whether a value points an object in the slots. See
gc.c:is_pointer_to_heap().


Nobu Nakada

By whether a value points an object in the slots. See
gc.c:is_pointer_to_heap().

Okay. What if, in an extension, I have an integer on the stack that just
happens to contain a value that interpreted as a pointer points into a valid
point in the heap. Will that prevent the “pointed to” object from being
gc’ed?

Could you outline how the stack is processed? Is it just treated as a
continuous block of memory and scanned for dwords that “point” into the
heap?

Understanding this would be most helpful!

Thank you,

Thomas

Yes. This is normal for conservative garbage collection schemes. (Which
ruby uses for its stack walking) It’s very, very rarely an issue.

				Dan
···

On Wed, 8 Oct 2003, Thomas Sondergaard wrote:

By whether a value points an object in the slots. See
gc.c:is_pointer_to_heap().

Okay. What if, in an extension, I have an integer on the stack that just
happens to contain a value that interpreted as a pointer points into a valid
point in the heap. Will that prevent the “pointed to” object from being
gc’ed?

Yes. This is normal for conservative garbage collection schemes. (Which
ruby uses for its stack walking) It’s very, very rarely an issue.

I believe you. It is just important for me to understand what exactly it
takes to avoid getting reaped by the gc. My question about an integer
variable “pointing” into the ruby heap was not to point out a concern, but
to make sure that I understood it correctly.

So to sum it up: If I am sure that there is a (properly aligned) dword on
the stack that points to my sacred unreapable object it will be safe.

Thanks,

Thomas