malloc 0.2.4 → 0.2.5
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 +2 -1
- data/lib/malloc_ext.rb +103 -25
- data/test/tc_malloc.rb +10 -0
- metadata +2 -2
data/Rakefile
CHANGED
|
@@ -6,7 +6,7 @@ require 'rake/packagetask'
|
|
|
6
6
|
require 'rbconfig'
|
|
7
7
|
|
|
8
8
|
PKG_NAME = 'malloc'
|
|
9
|
-
PKG_VERSION = '0.2.
|
|
9
|
+
PKG_VERSION = '0.2.5'
|
|
10
10
|
CXX = ENV[ 'CXX' ] || 'g++'
|
|
11
11
|
STRIP = ENV[ 'STRIP' ] || 'strip'
|
|
12
12
|
RB_FILES = FileList[ 'lib/**/*.rb' ]
|
|
@@ -40,6 +40,7 @@ $SITEARCHDIR = Config::CONFIG[ 'sitearchdir' ]
|
|
|
40
40
|
|
|
41
41
|
task :default => :all
|
|
42
42
|
|
|
43
|
+
desc 'Compile Ruby extension (default)'
|
|
43
44
|
task :all => [ SO_FILE ]
|
|
44
45
|
|
|
45
46
|
file SO_FILE => OBJ do |t|
|
data/lib/malloc_ext.rb
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
# String#bytesize is defined if it does not exist already
|
|
2
|
+
class String
|
|
3
|
+
|
|
4
|
+
unless method_defined? :bytesize
|
|
5
|
+
|
|
6
|
+
# String#bytesize is defined if it does not exist already
|
|
7
|
+
#
|
|
8
|
+
# Provided for compatibility with Ruby 1.8.6. Same as #size.
|
|
9
|
+
#
|
|
10
|
+
# @private
|
|
11
|
+
def bytesize
|
|
12
|
+
size
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
|
|
1
19
|
# Namespace of the Hornetseye project.
|
|
2
20
|
module Hornetseye
|
|
3
21
|
|
|
@@ -8,7 +26,15 @@ module Hornetseye
|
|
|
8
26
|
|
|
9
27
|
# Create new Malloc object
|
|
10
28
|
#
|
|
11
|
-
#
|
|
29
|
+
# Allocate the specified number of bytes of raw memory.
|
|
30
|
+
#
|
|
31
|
+
# @example Allocate raw memory
|
|
32
|
+
# require 'malloc'
|
|
33
|
+
# include Hornetseye
|
|
34
|
+
# m = Malloc.new 32
|
|
35
|
+
# # Malloc(32)
|
|
36
|
+
#
|
|
37
|
+
# @param [Integer] size Number of bytes to allocate.
|
|
12
38
|
# @return [Malloc] A new Malloc object.
|
|
13
39
|
def new( size )
|
|
14
40
|
retval = orig_new size
|
|
@@ -22,12 +48,61 @@ module Hornetseye
|
|
|
22
48
|
|
|
23
49
|
# Number of bytes allocated
|
|
24
50
|
#
|
|
51
|
+
# @example Querying size of allocated memory
|
|
52
|
+
# require 'malloc'
|
|
53
|
+
# include Hornetseye
|
|
54
|
+
# m = Malloc.new 32
|
|
55
|
+
# m.size
|
|
56
|
+
# # 32
|
|
57
|
+
# ( m + 8 ).size
|
|
58
|
+
# # 24
|
|
59
|
+
#
|
|
25
60
|
# @return [Integer] Size of allocated memory.
|
|
26
61
|
attr_reader :size
|
|
27
62
|
|
|
63
|
+
# Display information about this object
|
|
64
|
+
#
|
|
65
|
+
# @example Displaying information about a Malloc object
|
|
66
|
+
# require 'malloc'
|
|
67
|
+
# Hornetseye::Malloc.new( 8 ).inspect
|
|
68
|
+
# "Malloc(8)"
|
|
69
|
+
#
|
|
70
|
+
# @return [String] A string with information about this object.
|
|
71
|
+
def inspect
|
|
72
|
+
"Malloc(#{@size})"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Read data from memory
|
|
76
|
+
#
|
|
77
|
+
# @example Convert data of Malloc object to string
|
|
78
|
+
# require 'malloc'
|
|
79
|
+
# include Hornetseye
|
|
80
|
+
# m = Malloc.new 5
|
|
81
|
+
# m.write 'abcde'
|
|
82
|
+
# m.to_s
|
|
83
|
+
# "abcde"
|
|
84
|
+
#
|
|
85
|
+
# @return [String] A string containing the data.
|
|
86
|
+
#
|
|
87
|
+
# @see #read
|
|
88
|
+
def to_s
|
|
89
|
+
read @size
|
|
90
|
+
end
|
|
91
|
+
|
|
28
92
|
# Operator for doing pointer arithmetic
|
|
29
93
|
#
|
|
30
|
-
# @
|
|
94
|
+
# @example Pointer arithmetic
|
|
95
|
+
# require 'malloc'
|
|
96
|
+
# include Hornetseye
|
|
97
|
+
# m = Malloc.new 4
|
|
98
|
+
# # Malloc(4)
|
|
99
|
+
# m.write 'abcd'
|
|
100
|
+
# n = m + 2
|
|
101
|
+
# # Malloc(2)
|
|
102
|
+
# n.read 2
|
|
103
|
+
# # "cd"
|
|
104
|
+
#
|
|
105
|
+
# @param [Integer] offset Non-negative offset for pointer.
|
|
31
106
|
# @return [Malloc] A new Malloc object.
|
|
32
107
|
def +( offset )
|
|
33
108
|
if offset > @size
|
|
@@ -43,8 +118,20 @@ module Hornetseye
|
|
|
43
118
|
|
|
44
119
|
# Read data from memory
|
|
45
120
|
#
|
|
46
|
-
# @
|
|
121
|
+
# @example Reading and writing data
|
|
122
|
+
# require 'malloc'
|
|
123
|
+
# include Hornetseye
|
|
124
|
+
# m = Malloc.new 4
|
|
125
|
+
# # Malloc(4)
|
|
126
|
+
# m.write 'abcd'
|
|
127
|
+
# m.read 2
|
|
128
|
+
# # "ab"
|
|
129
|
+
#
|
|
130
|
+
# @param [Integer] length Number of bytes to read.
|
|
47
131
|
# @return [String] A string containing the data.
|
|
132
|
+
#
|
|
133
|
+
# @see #write
|
|
134
|
+
# @see #to_s
|
|
48
135
|
def read( length )
|
|
49
136
|
raise "Only #{@size} bytes of memory left to read " +
|
|
50
137
|
"(illegal attempt to read #{length} bytes)" if length > @size
|
|
@@ -55,8 +142,19 @@ module Hornetseye
|
|
|
55
142
|
|
|
56
143
|
# Write data to memory
|
|
57
144
|
#
|
|
58
|
-
# @
|
|
59
|
-
#
|
|
145
|
+
# @example Reading and writing data
|
|
146
|
+
# require 'malloc'
|
|
147
|
+
# include Hornetseye
|
|
148
|
+
# m = Malloc.new 4
|
|
149
|
+
# # Malloc(4)
|
|
150
|
+
# m.write 'abcd'
|
|
151
|
+
# m.read 2
|
|
152
|
+
# # "ab"
|
|
153
|
+
#
|
|
154
|
+
# @param [String] string A string with the data.
|
|
155
|
+
# @return [String] Returns the parameter +string+.
|
|
156
|
+
#
|
|
157
|
+
# @see #read
|
|
60
158
|
def write( string )
|
|
61
159
|
if string.bytesize > @size
|
|
62
160
|
raise "Must not write more than #{@size} bytes of memory " +
|
|
@@ -70,23 +168,3 @@ module Hornetseye
|
|
|
70
168
|
end
|
|
71
169
|
|
|
72
170
|
end
|
|
73
|
-
|
|
74
|
-
# The string class of the standard library
|
|
75
|
-
#
|
|
76
|
-
# @private
|
|
77
|
-
class String
|
|
78
|
-
|
|
79
|
-
unless method_defined? :bytesize
|
|
80
|
-
|
|
81
|
-
# This method won't be overriden if it is defined already
|
|
82
|
-
#
|
|
83
|
-
# Provided for compatibility with Ruby 1.8.6. Same as #size.
|
|
84
|
-
#
|
|
85
|
-
# @private
|
|
86
|
-
def bytesize
|
|
87
|
-
size
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
end
|
data/test/tc_malloc.rb
CHANGED
|
@@ -8,6 +8,16 @@ class TC_Malloc < Test::Unit::TestCase
|
|
|
8
8
|
assert_raise( ArgumentError ) { Hornetseye::Malloc.new }
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
+
def test_inspect
|
|
12
|
+
assert_equal 'Malloc(32)', Hornetseye::Malloc.new( 32 ).inspect
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_to_s
|
|
16
|
+
m = Hornetseye::Malloc.new 3
|
|
17
|
+
m.write 'abc'
|
|
18
|
+
assert_equal 'abc', m.to_s
|
|
19
|
+
end
|
|
20
|
+
|
|
11
21
|
def test_read_write
|
|
12
22
|
m = Hornetseye::Malloc.new 6
|
|
13
23
|
assert_equal 'abcdef', m.write( 'abcdef' )
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: malloc
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jan Wedekind
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date:
|
|
12
|
+
date: 2010-01-14 00:00:00 +00:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|