ruby-changes:13808
From: tenderlove <ko1@a...>
Date: Sun, 1 Nov 2009 10:46:55 +0900 (JST)
Subject: [ruby-changes:13808] Ruby:r25606 (trunk): * ext/dl/cptr.c (rb_dlptr_size) splitting function to reduce complexity
tenderlove 2009-11-01 10:46:44 +0900 (Sun, 01 Nov 2009) New Revision: 25606 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25606 Log: * ext/dl/cptr.c (rb_dlptr_size) splitting function to reduce complexity * ext/dl/cptr.c (rb_dlptr_null_p, rb_dlptr_aref, rb_dlptr_aset) adding documentation * ext/dl/dl.c (rb_dl_free) adding documentation * test/dl/test_cptr.rb (test_null?, test_size, test_size=, test_aref_aset) Improving test coverage * test/dl/test_dl2.rb (test_free_secure) improving test coverage Modified files: trunk/ext/dl/cptr.c trunk/ext/dl/dl.c trunk/test/dl/test_cptr.rb trunk/test/dl/test_dl2.rb Index: ext/dl/dl.c =================================================================== --- ext/dl/dl.c (revision 25605) +++ ext/dl/dl.c (revision 25606) @@ -49,6 +49,11 @@ return PTR2NUM(ptr); } +/* + * call-seq: DL.free(addr) + * + * Free the memory at address +addr+ + */ VALUE rb_dl_free(VALUE self, VALUE addr) { Index: ext/dl/cptr.c =================================================================== --- ext/dl/cptr.c (revision 25605) +++ ext/dl/cptr.c (revision 25606) @@ -223,6 +223,11 @@ return rb_dlptr_new(&(data->ptr),0,0); } +/* + * call-seq: null? + * + * Returns true if this is a null pointer. + */ VALUE rb_dlptr_null_p(VALUE self) { @@ -357,6 +362,15 @@ return rb_dlptr_new((char *)ptr - num, size + num, 0); } +/* + * call-seq: + * ptr[index] -> an_integer + * ptr[start, length] -> a_string + * + * Returns integer stored at _index_. If _start_ and _length_ are given, + * a string containing the bytes from _start_ of length _length_ will be + * returned. + */ VALUE rb_dlptr_aref(int argc, VALUE argv[], VALUE self) { @@ -380,6 +394,15 @@ return retval; } +/* + * call-seq: + * ptr[index] = int -> int + * ptr[start, length] = string or cptr or addr -> string or dl_cptr or addr + * + * Set the value at +index+ to +int+. Or, set the memory at +start+ until + * +length+ with the contents of +string+, the memory from +dl_cptr+, or the + * memory pointed at by the memory address +addr+. + */ VALUE rb_dlptr_aset(int argc, VALUE argv[], VALUE self) { @@ -415,18 +438,27 @@ return retval; } -VALUE -rb_dlptr_size(int argc, VALUE argv[], VALUE self) +/* + * call-seq: size=(size) + * + * Set the size of this pointer to +size+ + */ +static VALUE +rb_dlptr_size_set(VALUE self, VALUE size) { - VALUE size; + RPTR_DATA(self)->size = NUM2LONG(size); + return size; +} - if (rb_scan_args(argc, argv, "01", &size) == 0){ - return LONG2NUM(RPTR_DATA(self)->size); - } - else{ - RPTR_DATA(self)->size = NUM2LONG(size); - return size; - } +/* + * call-seq: size + * + * Get the size of this pointer. + */ +static VALUE +rb_dlptr_size_get(VALUE self) +{ + return LONG2NUM(RPTR_DATA(self)->size); } VALUE @@ -491,8 +523,8 @@ rb_define_method(rb_cDLCPtr, "-", rb_dlptr_minus, 1); rb_define_method(rb_cDLCPtr, "[]", rb_dlptr_aref, -1); rb_define_method(rb_cDLCPtr, "[]=", rb_dlptr_aset, -1); - rb_define_method(rb_cDLCPtr, "size", rb_dlptr_size, -1); - rb_define_method(rb_cDLCPtr, "size=", rb_dlptr_size, -1); + rb_define_method(rb_cDLCPtr, "size", rb_dlptr_size_get, 0); + rb_define_method(rb_cDLCPtr, "size=", rb_dlptr_size_set, 1); rb_define_const(rb_mDL, "NULL", rb_dlptr_new(0, 0, 0)); } Index: test/dl/test_dl2.rb =================================================================== --- test/dl/test_dl2.rb (revision 25605) +++ test/dl/test_dl2.rb (revision 25606) @@ -5,6 +5,15 @@ class TestDL < TestBase # TODO: refactor test repetition + def test_free_secure + assert_raises(SecurityError) do + Thread.new do + $SAFE = 4 + DL.free(0) + end.join + end + end + def test_realloc str = "abc" ptr_id = DL.realloc(0, 4) @@ -133,27 +142,5 @@ ary2 = dlunwrap(addr) assert_equal(ary, ary2) end - - def test_cptr() - check = Proc.new{|str,ptr| - assert_equal(str.size(), ptr.size()) - assert_equal(str, ptr.to_s()) - assert_equal(str[0,2], ptr.to_s(2)) - assert_equal(str[0,2], ptr[0,2]) - assert_equal(str[1,2], ptr[1,2]) - assert_equal(str[1,0], ptr[1,0]) - assert_equal(str[0].ord, ptr[0]) - assert_equal(str[1].ord, ptr[1]) - } - str = 'abc' - ptr = CPtr[str] - check.call(str, ptr) - str[0] = "c" - ptr[0] = "c".ord - check.call(str, ptr) - str[0,2] = "aa" - ptr[0,2] = "aa" - check.call(str, ptr) - end end end # module DL Index: test/dl/test_cptr.rb =================================================================== --- test/dl/test_cptr.rb (revision 25605) +++ test/dl/test_cptr.rb (revision 25606) @@ -29,5 +29,57 @@ assert_equal free.ptr, ptr.free.ptr end + + def test_null? + ptr = CPtr.new(0) + assert ptr.null? + end + + def test_size + ptr = CPtr.malloc(4) + assert_equal 4, ptr.size + DL.free ptr.to_i + end + + def test_size= + ptr = CPtr.malloc(4) + ptr.size = 10 + assert_equal 10, ptr.size + DL.free ptr.to_i + end + + def test_aref_aset + check = Proc.new{|str,ptr| + assert_equal(str.size(), ptr.size()) + assert_equal(str, ptr.to_s()) + assert_equal(str[0,2], ptr.to_s(2)) + assert_equal(str[0,2], ptr[0,2]) + assert_equal(str[1,2], ptr[1,2]) + assert_equal(str[1,0], ptr[1,0]) + assert_equal(str[0].ord, ptr[0]) + assert_equal(str[1].ord, ptr[1]) + } + str = 'abc' + ptr = CPtr[str] + check.call(str, ptr) + + str[0] = "c" + assert_equal 'c'.ord, ptr[0] = "c".ord + check.call(str, ptr) + + str[0,2] = "aa" + assert_equal 'aa', ptr[0,2] = "aa" + check.call(str, ptr) + + ptr2 = CPtr['cdeeee'] + str[0,2] = "cd" + assert_equal ptr2, ptr[0,2] = ptr2 + check.call(str, ptr) + + ptr3 = CPtr['vvvv'] + str[0,2] = "vv" + assert_equal ptr3.to_i, ptr[0,2] = ptr3.to_i + check.call(str, ptr) + end end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/