[Ruby] splatting arguments in a C extension
Evan Phoenix
evan at fallingsnow.net
Sat Feb 2 09:04:50 PST 2008
See below.
On Feb 2, 2008, at 1:09 AM, Ryan Davis wrote:
>
> On Feb 2, 2008, at 00:46 , Ryan Davis wrote:
>
>> I'm not sure you can.
>
> I am teh awesome:
>
> #!/usr/bin/ruby -w
>
> require 'rubygems'
> require 'inline'
>
> class X
> def m a, b, c
> p [a, b, c]
> end
>
> inline do |builder|
> builder.add_type_converter("ID", '', '')
>
> builder.c_raw '
> VALUE splat_funcall(VALUE recv, ID mid, int n, VALUE args) {
> VALUE *argv = ALLOC_N(VALUE, n); // pretty sure this is GC
> friendly
No, not GC friendly. That memory will remain allocated forever. 1.8
doesn't have a simple way to a chunk of memory that will be garbage
collected. The ALLOC_N macro is just a simple wrapper around malloc()
that calls the 1.8 GC if and only if the malloc() failed, the idea
being that it failed because you were out of memory, so running the GC
should free some up.
- Evan
>
> int i;
>
> for (i = 0; i < n; i++) {
> argv[i] = rb_ary_entry(args, i);
> }
>
> return rb_funcall2(recv, mid, n, argv);
> }
> '
>
> builder.c '
> void x(VALUE ary) {
> splat_funcall(self, rb_intern("m"), 3, ary);
> }
> '
> end
> end
>
> X.new.x([1, 2, 3])
>
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
More information about the Ruby
mailing list