ruby-changes:14663
From: yugui <ko1@a...>
Date: Sat, 30 Jan 2010 22:22:41 +0900 (JST)
Subject: [ruby-changes:14663] Ruby:r26512 (ruby_1_9_1): merges r25605 from trunk into ruby_1_9_1.
yugui 2010-01-30 21:54:41 +0900 (Sat, 30 Jan 2010) New Revision: 26512 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26512 Log: merges r25605 from trunk into ruby_1_9_1. -- * test/dl/test_dl2.rb (**) testing malloc and realloc * ext/dl/dl.c (**) adding documentation Modified files: branches/ruby_1_9_1/ext/dl/dl.c branches/ruby_1_9_1/test/dl/test_dl2.rb branches/ruby_1_9_1/version.h Index: ruby_1_9_1/ext/dl/dl.c =================================================================== --- ruby_1_9_1/ext/dl/dl.c (revision 26511) +++ ruby_1_9_1/ext/dl/dl.c (revision 26512) @@ -16,6 +16,12 @@ return rb_class_new_instance(argc, argv, rb_cDLHandle); } +/* + * call-seq: DL.malloc + * + * Allocate +size+ bytes of memory and return the integer memory address + * for the allocated memory. + */ VALUE rb_dl_malloc(VALUE self, VALUE size) { @@ -26,6 +32,13 @@ return PTR2NUM(ptr); } +/* + * call-seq: DL.realloc(addr, size) + * + * Change the size of the memory allocated at the memory location +addr+ to + * +size+ bytes. Returns the memory address of the reallocated memory, which + * may be different than the address passed in. + */ VALUE rb_dl_realloc(VALUE self, VALUE addr, VALUE size) { Index: ruby_1_9_1/version.h =================================================================== --- ruby_1_9_1/version.h (revision 26511) +++ ruby_1_9_1/version.h (revision 26512) @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.1" -#define RUBY_PATCHLEVEL 417 +#define RUBY_PATCHLEVEL 418 #define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MINOR 9 #define RUBY_VERSION_TEENY 1 Index: ruby_1_9_1/test/dl/test_dl2.rb =================================================================== --- ruby_1_9_1/test/dl/test_dl2.rb (revision 26511) +++ ruby_1_9_1/test/dl/test_dl2.rb (revision 26512) @@ -3,6 +3,53 @@ module DL class TestDL < TestBase + # TODO: refactor test repetition + + def test_realloc + str = "abc" + ptr_id = DL.realloc(0, 4) + ptr = CPtr.new(ptr_id, 4) + + assert_equal ptr_id, ptr.to_i + + cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') + x = cfunc.call([ptr_id,str].pack("l!p").unpack("l!*")) + assert_equal("abc\0", ptr[0,4]) + DL.free ptr_id + end + + def test_realloc_secure + assert_raises(SecurityError) do + Thread.new do + $SAFE = 4 + DL.realloc(0, 4) + end.join + end + end + + def test_malloc + str = "abc" + + ptr_id = DL.malloc(4) + ptr = CPtr.new(ptr_id, 4) + + assert_equal ptr_id, ptr.to_i + + cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') + x = cfunc.call([ptr_id,str].pack("l!p").unpack("l!*")) + assert_equal("abc\0", ptr[0,4]) + DL.free ptr_id + end + + def test_malloc_security + assert_raises(SecurityError) do + Thread.new do + $SAFE = 4 + DL.malloc(4) + end.join + end + end + def test_call_int() cfunc = CFunc.new(@libc['atoi'], TYPE_INT, 'atoi') x = cfunc.call(["100"].pack("p").unpack("l!*")) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/