[前][次][番号順一覧][スレッド一覧]

ruby-changes:13878

From: tenderlove <ko1@a...>
Date: Sat, 7 Nov 2009 07:50:20 +0900 (JST)
Subject: [ruby-changes:13878] Ruby:r25678 (trunk): * ext/dl/cptr.c (rb_dlptr_s_malloc, rb_dlptr_initialize): adding

tenderlove	2009-11-07 07:50:05 +0900 (Sat, 07 Nov 2009)

  New Revision: 25678

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25678

  Log:
    * ext/dl/cptr.c (rb_dlptr_s_malloc, rb_dlptr_initialize): adding
      documentation
    * test/dl/test_cptr.rb (**): testing that malloc works when passed free
      functions

  Modified files:
    trunk/ext/dl/cptr.c
    trunk/test/dl/test_cptr.rb

Index: ext/dl/cptr.c
===================================================================
--- ext/dl/cptr.c	(revision 25677)
+++ ext/dl/cptr.c	(revision 25678)
@@ -123,6 +123,15 @@
     return obj;
 }
 
+/*
+ * call-seq:
+ *    DL::CPtr.new(address)                   => dl_cptr
+ *    DL::CPtr.new(address, size)             => dl_cptr
+ *    DL::CPtr.new(address, size, freefunc)   => dl_cptr
+ *
+ * Create a new pointer to +address+ with an optional +size+ and +freefunc+.
+ * +freefunc+ will be called when the instance is garbage collected.
+ */
 static VALUE
 rb_dlptr_initialize(int argc, VALUE argv[], VALUE self)
 {
@@ -163,6 +172,16 @@
     return Qnil;
 }
 
+/*
+ * call-seq:
+ *
+ *    DL::CPtr.malloc(size, freefunc = nil)  => dl cptr instance
+ *
+ * Allocate +size+ bytes of memory and associate it with an optional
+ * +freefunc+ that will be called when the pointer is garbage collected.
+ * +freefunc+ must be an address pointing to a function or an instance of
+ * DL::CFunc
+ */
 static VALUE
 rb_dlptr_s_malloc(int argc, VALUE argv[], VALUE klass)
 {
Index: test/dl/test_cptr.rb
===================================================================
--- test/dl/test_cptr.rb	(revision 25677)
+++ test/dl/test_cptr.rb	(revision 25678)
@@ -3,6 +3,22 @@
 
 module DL
   class TestCPtr < TestBase
+    def test_malloc_free_func_int
+      free = CFunc.new(@libc['free'], TYPE_VOID, 'free')
+
+      ptr  = CPtr.malloc(10, free.to_i)
+      assert_equal 10, ptr.size
+      assert_equal free.to_i, ptr.free.to_i
+    end
+
+    def test_malloc_free_func
+      free = CFunc.new(@libc['free'], TYPE_VOID, 'free')
+
+      ptr  = CPtr.malloc(10, free)
+      assert_equal 10, ptr.size
+      assert_equal free.to_i, ptr.free.to_i
+    end
+
     def test_to_str
       str = "hello world"
       ptr = CPtr[str]

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]