malloc 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +5 -3
- data/ext/malloc.cc +24 -5
- data/ext/malloc.hh +1 -1
- data/lib/malloc_ext.rb +14 -6
- data/test/tc_malloc.rb +21 -10
- metadata +3 -3
data/Rakefile
CHANGED
@@ -7,12 +7,12 @@ require 'rake/loaders/makefile'
|
|
7
7
|
require 'rbconfig'
|
8
8
|
|
9
9
|
PKG_NAME = 'malloc'
|
10
|
-
PKG_VERSION = '1.
|
10
|
+
PKG_VERSION = '1.2.0'
|
11
11
|
CXX = ENV[ 'CXX' ] || 'g++'
|
12
12
|
STRIP = ENV[ 'STRIP' ] || 'strip'
|
13
13
|
RB_FILES = FileList[ 'lib/**/*.rb' ]
|
14
14
|
CC_FILES = FileList[ 'ext/*.cc' ]
|
15
|
-
HH_FILES = FileList[ 'ext/*.hh' ]
|
15
|
+
HH_FILES = FileList[ 'ext/*.hh' ] + FileList[ 'ext/*.tcc' ]
|
16
16
|
TC_FILES = FileList[ 'test/tc_*.rb' ]
|
17
17
|
TS_FILES = FileList[ 'test/ts_*.rb' ]
|
18
18
|
SO_FILE = "ext/#{PKG_NAME}.so"
|
@@ -165,7 +165,9 @@ rule '.o' => '.cc' do |t|
|
|
165
165
|
end
|
166
166
|
|
167
167
|
file ".depends.mf" do |t|
|
168
|
-
sh "
|
168
|
+
sh "g++ -MM #{$CXXFLAGS} #{CC_FILES.join ' '} | " +
|
169
|
+
"sed -e :a -e N -e 's/\\n/\\$/g' -e ta | " +
|
170
|
+
"sed -e 's/ *\\\\\\$ */ /g' -e 's/\\$/\\n/g' | sed -e 's/^/ext\\//' > #{t.name}"
|
169
171
|
end
|
170
172
|
CC_FILES.each do |t|
|
171
173
|
file t.ext(".o") => t
|
data/ext/malloc.cc
CHANGED
@@ -39,7 +39,7 @@ VALUE Malloc::init( VALUE rbModule )
|
|
39
39
|
rb_define_method( cRubyClass, "orig_read",
|
40
40
|
RUBY_METHOD_FUNC( mallocRead ), 1 );
|
41
41
|
rb_define_method( cRubyClass, "orig_write",
|
42
|
-
RUBY_METHOD_FUNC( mallocWrite ), 1 );
|
42
|
+
RUBY_METHOD_FUNC( mallocWrite ), -1 );
|
43
43
|
return cRubyClass;
|
44
44
|
}
|
45
45
|
|
@@ -80,9 +80,28 @@ VALUE Malloc::mallocRead( VALUE rbSelf, VALUE rbLength )
|
|
80
80
|
return rb_str_new( self, length );
|
81
81
|
}
|
82
82
|
|
83
|
-
VALUE Malloc::mallocWrite( VALUE
|
83
|
+
VALUE Malloc::mallocWrite( int argc, VALUE *rbArgv, VALUE rbSelf )
|
84
84
|
{
|
85
|
-
|
86
|
-
|
87
|
-
|
85
|
+
VALUE retVal = Qnil;
|
86
|
+
try {
|
87
|
+
char *self; Data_Get_Struct( rbSelf, char, self );
|
88
|
+
ERRORMACRO( argc == 1 || argc == 2, Error, , "Malloc#write accepts "
|
89
|
+
"one or two arguments (not " << argc << ")" );
|
90
|
+
if ( argc == 1 ) {
|
91
|
+
VALUE rbString = rbArgv[0];
|
92
|
+
memcpy( self, StringValuePtr( rbString ), RSTRING_LEN( rbString ) );
|
93
|
+
retVal = rbString;
|
94
|
+
} else {
|
95
|
+
VALUE rbOther = rbArgv[0];
|
96
|
+
VALUE rbSize = rbArgv[1];
|
97
|
+
char *other; Data_Get_Struct( rbOther, char, other );
|
98
|
+
int size = NUM2INT( rbSize );
|
99
|
+
memcpy( self, other, size );
|
100
|
+
retVal = rbOther;
|
101
|
+
};
|
102
|
+
} catch ( std::exception &e ) {
|
103
|
+
rb_raise( rb_eRuntimeError, "%s", e.what() );
|
104
|
+
};
|
105
|
+
return retVal;
|
88
106
|
}
|
107
|
+
|
data/ext/malloc.hh
CHANGED
@@ -26,7 +26,7 @@ public:
|
|
26
26
|
static VALUE mallocNew( VALUE rbClass, VALUE rbSize );
|
27
27
|
static VALUE mallocPlus( VALUE rbSelf, VALUE rbOffset );
|
28
28
|
static VALUE mallocRead( VALUE rbSelf, VALUE rbLength );
|
29
|
-
static VALUE mallocWrite( VALUE
|
29
|
+
static VALUE mallocWrite( int argc, VALUE *rbArgv, VALUE rbSelf );
|
30
30
|
};
|
31
31
|
|
32
32
|
#endif
|
data/lib/malloc_ext.rb
CHANGED
@@ -164,16 +164,24 @@ module Hornetseye
|
|
164
164
|
# m.read 2
|
165
165
|
# # "ab"
|
166
166
|
#
|
167
|
-
# @param [String]
|
167
|
+
# @param [String,Malloc] data A string or malloc object with the data.
|
168
168
|
# @return [String] Returns the parameter +string+.
|
169
169
|
#
|
170
170
|
# @see #read
|
171
|
-
def write(
|
172
|
-
if
|
173
|
-
|
174
|
-
"
|
171
|
+
def write( data )
|
172
|
+
if data.is_a? Malloc
|
173
|
+
if data.size > @size
|
174
|
+
raise "Must not write more than #{@size} bytes of memory " +
|
175
|
+
"(illegal attempt to write #{data.size} bytes)"
|
176
|
+
end
|
177
|
+
orig_write data, data.size
|
178
|
+
else
|
179
|
+
if data.bytesize > @size
|
180
|
+
raise "Must not write more than #{@size} bytes of memory " +
|
181
|
+
"(illegal attempt to write #{data.bytesize} bytes)"
|
182
|
+
end
|
183
|
+
orig_write data
|
175
184
|
end
|
176
|
-
orig_write string
|
177
185
|
end
|
178
186
|
|
179
187
|
private :orig_write
|
data/test/tc_malloc.rb
CHANGED
@@ -19,35 +19,46 @@ Kernel::require 'malloc'
|
|
19
19
|
|
20
20
|
class TC_Malloc < Test::Unit::TestCase
|
21
21
|
|
22
|
+
Malloc = Hornetseye::Malloc
|
23
|
+
|
24
|
+
def Malloc( *args )
|
25
|
+
Hornetseye::Malloc *args
|
26
|
+
end
|
27
|
+
|
22
28
|
def test_new
|
23
|
-
assert_nothing_raised {
|
24
|
-
assert_nothing_raised {
|
25
|
-
assert_raise( ArgumentError ) {
|
29
|
+
assert_nothing_raised { Malloc.new 32 }
|
30
|
+
assert_nothing_raised { Malloc 32 }
|
31
|
+
assert_raise( ArgumentError ) { Malloc.new }
|
26
32
|
end
|
27
33
|
|
28
34
|
def test_inspect
|
29
|
-
assert_equal 'Malloc(32)',
|
35
|
+
assert_equal 'Malloc(32)', Malloc.new( 32 ).inspect
|
30
36
|
end
|
31
37
|
|
32
38
|
def test_to_s
|
33
|
-
assert_equal 'Malloc(32)',
|
39
|
+
assert_equal 'Malloc(32)', Malloc.new( 32 ).to_s
|
34
40
|
end
|
35
41
|
|
36
42
|
def test_read_write
|
37
|
-
m =
|
43
|
+
m = Malloc.new 6
|
38
44
|
assert_equal 'abcdef', m.write( 'abcdef' )
|
39
45
|
assert_equal 'abcdef', m.read( 6 )
|
40
46
|
assert_equal 'ghi', m.write( 'ghi' )
|
41
47
|
assert_equal 'ghidef', m.read( 6 )
|
48
|
+
n = Malloc.new 3
|
49
|
+
n.write 'jkl'
|
50
|
+
assert_equal n, m.write( n )
|
51
|
+
assert_equal 'jkldef', m.read( 6 )
|
42
52
|
assert_raise( RuntimeError ) { m.read 7 }
|
43
53
|
assert_raise( RuntimeError ) { m.write 'abcdefg' }
|
54
|
+
assert_raise( RuntimeError ) { m.write Malloc.new( 7 ) }
|
44
55
|
end
|
45
56
|
|
46
57
|
def test_plus
|
47
|
-
assert_raise( RuntimeError ) {
|
48
|
-
assert_nothing_raised {
|
49
|
-
assert_raise( RuntimeError ) {
|
50
|
-
m =
|
58
|
+
assert_raise( RuntimeError ) { Malloc.new( 2 ) + ( -1 ) }
|
59
|
+
assert_nothing_raised { Malloc.new( 2 ) + 2 }
|
60
|
+
assert_raise( RuntimeError ) { Malloc.new( 2 ) + 3 }
|
61
|
+
m = Malloc.new 6
|
51
62
|
m.write 'abcdef'
|
52
63
|
assert_equal 'cde', ( m + 2 ).read( 3 )
|
53
64
|
assert_raise( RuntimeError ) { ( m + 2 ).read 5 }
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
- 1
|
8
7
|
- 2
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 1.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jan Wedekind
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-18 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|