Skip to content

Instantly share code, notes, and snippets.

@danielpclark
Last active June 27, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpclark/6e266f2fdf232cfe3fe265e956ee80b4 to your computer and use it in GitHub Desktop.
Save danielpclark/6e266f2fdf232cfe3fe265e956ee80b4 to your computer and use it in GitHub Desktop.

Mark

Tell Garbage Collector which objects you use. Ruby takes care of object lifecycle; C doesn't. If you still need objects, mark them in use.

void mark(void* p) {
  auto* hash = reinterpret_cast<ruby_hash_t*>(p);
  for (auto& iter: *hash) {
    rb_gc_mark(iter.first);
    rb_gc_mark(iter.second);
  }
}

Garbage Collection in C Ruby

Mark and Sweep

If you don't mark, Ruby releases the memory. Nil starts to appear in random places. Might crash in worst case.

Basic Types in C Ruby

VALUE

Alias of a long, 64 bit integer (on 64 bit Linux).

RBasic

Struct with flags and class.

Every Ruby Variable in C is a VALUE

VALUE initialize(VALUE self) {
  return self;
}

def initialize
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment