Discussion:
[Gc] Creating stack-like memory containing GC roots
Christian Schafmeister
2015-09-23 19:52:11 UTC
Permalink
I’ve written a Common Lisp compiler that uses LLVM as the backend and that interoperates with C++.

I’m using the Boehm garbage collector and I need to create a stack alongside the regular C++ stack that holds GC roots.

Is there any way using the Boehm garbage collector to identify a region of memory that will contain this stack and continuously update the top of the stack so that Boehm only examines the region that can contain pointers?

I need to push a frame containing GC roots onto the stack and then later pop the frame. Ideally I would just need to provide Boehm a pointer that points to the end of the current frame and everything from that address up to and including the first frame would be scanned for roots.

I have seen the GC_add_roots function but that lets me define a region once. I need some way to continuously update the end of the stack.
Currently I’m using GC_malloc and GC_free to allocate and free frames - but I feel that for something like a stack this has more overhead than I need.
I’ve tried doing this all on the main C++ stack - but I have to use non-standard features like __builtin_alloca or variable length arrays and they have caused me a lot of grief.

Best,

Christian Schafmeister
Professor,
Chemistry Department
Temple University
Christian Schafmeister
2015-09-23 21:20:13 UTC
Permalink
Shiro - thank you very much. I’ll check that out.
I once used bdwgc's "kind" mechanism for the similar purpose. The "kind" is
conceptually a custom allocator with your own mark procedure. So, you allocate
your stack by the new kind so that GC knows about it, but you make the attached
marker scan only the active region of your stack.
I suppose the kind mechanism is more for generic purpose (e.g. "atomic" or "stubborn"
allocators are realized by kinds), so using it for just custom stack region may be
an overkill. The source suggests that the kind isn't for casual use. I'd like bgwgc
experts here to suggest better advices.
If you're interested, check Gauche's vm.c.
Look at the code inside #ifdef USE_CUSTOM_STACK_MARKER.
https://github.com/shirok/Gauche/blob/master/src/vm.c <https://github.com/shirok/Gauche/blob/master/src/vm.c>
I’ve written a Common Lisp compiler that uses LLVM as the backend and that interoperates with C++.
I’m using the Boehm garbage collector and I need to create a stack alongside the regular C++ stack that holds GC roots.
Is there any way using the Boehm garbage collector to identify a region of memory that will contain this stack and continuously update the top of the stack so that Boehm only examines the region that can contain pointers?
I need to push a frame containing GC roots onto the stack and then later pop the frame. Ideally I would just need to provide Boehm a pointer that points to the end of the current frame and everything from that address up to and including the first frame would be scanned for roots.
I have seen the GC_add_roots function but that lets me define a region once. I need some way to continuously update the end of the stack.
Currently I’m using GC_malloc and GC_free to allocate and free frames - but I feel that for something like a stack this has more overhead than I need.
I’ve tried doing this all on the main C++ stack - but I have to use non-standard features like __builtin_alloca or variable length arrays and they have caused me a lot of grief.
Best,
Christian Schafmeister
Professor,
Chemistry Department
Temple University
_______________________________________________
bdwgc mailing list
https://lists.opendylan.org/mailman/listinfo/bdwgc <https://lists.opendylan.org/mailman/listinfo/bdwgc>
Basile Starynkevitch
2015-09-23 21:08:59 UTC
Permalink
Post by Christian Schafmeister
I’ve written a Common Lisp compiler that uses LLVM as the backend and that interoperates with C++.
I’m using the Boehm garbage collector and I need to create a stack alongside the regular C++ stack that holds GC roots.
Just use a regular GC-ed zone, but don't forget to grow it (by copying
it to a greater zone when needed) and to clear the popped values. Use
offsets, not pointers, into that zone.

Regards.
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***
Christian Schafmeister
2015-09-23 21:18:49 UTC
Permalink
Thank you for responding!

So popping a frame off the stack would involve memset’ing the memory of the frame to zeros? And I would allocate the memory using GC_malloc_uncollectable. I wouldn’t provide Boehm with any pointer to the top (moving end) of the stack.
That would mean that the unused memory of the stack would be full of zeros but it would still be scanned by the GC.
Will that not add significant overhead?

I see how to make that work, I’m just concerned about the overhead.

I could occasionally dynamically resize this stack if the amount of unused space becomes obscene...


Best,

.Chris.
Post by Christian Schafmeister
I’ve written a Common Lisp compiler that uses LLVM as the backend and that interoperates with C++.
I’m using the Boehm garbage collector and I need to create a stack alongside the regular C++ stack that holds GC roots.
Just use a regular GC-ed zone, but don't forget to grow it (by copying it to a greater zone when needed) and to clear the popped values. Use offsets, not pointers, into that zone.
Regards.
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***
Loading...