Re: [ruby-core:99185] [Ruby master Bug#17023] How to prevent String memory to be relocated in ruby-ffi

Aaron Patterson <[email protected]> Wed, 15 Jul 2020 16:49:38 -0700
Newsgroups gmane.comp.lang.ruby.unicorn.general,gmane.comp.lang.ruby.core
Message-ID <[email protected]>

> On Jul 15, 2020, at 4:35 PM, Eric Wong <[email protected]> wrote:
>=20
> [email protected] wrote:
>> Right, that makes sense.  I really need to document this (and
>> I apologize for not doing so already), but
>> `rb_gc_register_address` will pin your objects.  When you know
>> you're done with the reference, you can release it with
>> `rb_gc_unregister_address`.  Of course if you don't call the
>> unregister function, the reference will stay alive forever.
>=20
> Btw, does rb_gc_register_mark_object pin?  A quick glance at
> gc.c tells me it doesn't, and I'll need to revert commit
> 2a6cb76d5010cb763ef5a2c305728465d15eb7c9 in unicorn:
> https://yhbt.net/unicorn-public/[email protected]/

Yes, it does pin.  I=E2=80=99m not super proud of this code, but here is =
where objects passed to rb_gc_register_mark_object get pinned:

  =
https://github.com/ruby/ruby/blob/c2a6295ec04a191c689d22254ac1ad5d665e27ad=
/vm.c#L2307-L2320

I don=E2=80=99t know why the mark object array is an array of arrays (I =
assume so as not to waste space in the array buffer?).  Maybe this could =
be a more friendly data structure.

I created a pinned list in compile.c so that objects allocated and used =
at compile time don=E2=80=99t move (they become free to move once iseq =
assembly is finished). It seems that might be a more generally useful =
thing, but so far I=E2=80=99ve only seen two places that need this =
feature.