For various reasons, I need to be able to attached some piece of data to
a method defined in C.  The data should:
  1. follow the method around if it is aliased
  2. be eligible for garbage collection if the method is undefined
  3. not interfere with the method in any way

I used to use define_method with a proc, but this prevents my method
from taking a block parameter.

I currently store the data in the unused member of NODE_CFUNC, but this
causes a memory leak (I have no way of knowing when the method becomes
unused).

This morning, I came up with this idea:

  static void define_method_with_data(
      VALUE klass,
      ID id,
      VALUE (*cfunc)(ANYARGS),
      int arity,
      VALUE data)
  {
  #if RUBY_VERSION_CODE >= 180
    VALUE origin = rb_class_boot(klass);
  #else
    VALUE origin = rb_class_new(klass);
  #endif
    FL_SET(origin, FL_SINGLETON);
    RBASIC(origin)->klass = data;
    NODE * node = NEW_FBODY(NEW_CFUNC(cfunc, arity), id, origin);
    rb_add_method(klass, id, node, NOEX_PUBLIC);
  }

Ruby treats the new method as an alias for another method.  I can
retrieve the attached data like this:

  static VALUE get_method_data()
  {
    return RBASIC(ruby_frame->last_class)->klass;
  }

Though I feel there is some problem with doing this, I haven't been able
to find one.

Any thoughts on this?  Is there another way to do it?

Paul