Class: Hornetseye::Malloc
- Inherits:
-
Object
- Object
- Hornetseye::Malloc
- Defined in:
- lib/malloc_ext.rb
Overview
Class for allocating raw memory and for doing pointer operations
Instance Attribute Summary (collapse)
-
- (Integer) size
readonly
Number of bytes allocated.
Class Method Summary (collapse)
-
+ (Malloc) new(size)
Create new Malloc object.
Instance Method Summary (collapse)
-
- (Malloc) +(offset)
Operator for doing pointer arithmetic.
-
- (Malloc) dup
Duplicate object.
-
- (Object) export
Read complete data.
-
- (String) inspect
Display information about this object.
-
- (String) read(length)
Read data from memory.
-
- (String) to_s
Display information about this object.
-
- (String) write(data)
Write data to memory.
Instance Attribute Details
- (Integer) size (readonly)
Number of bytes allocated
75 76 77 |
# File 'lib/malloc_ext.rb', line 75 def size @size end |
Class Method Details
+ (Malloc) new(size)
Create new Malloc object
Allocate the specified number of bytes of raw memory.
55 56 57 58 59 |
# File 'lib/malloc_ext.rb', line 55 def new( size ) retval = orig_new size retval.instance_eval { @size = size } retval end |
Instance Method Details
- (Malloc) +(offset)
Operator for doing pointer arithmetic
140 141 142 143 144 145 146 147 148 |
# File 'lib/malloc_ext.rb', line 140 def +( offset ) if offset > @size raise "Offset must not be more than #{@size} (but was #{offset})" end mark, size = self, @size - offset retval = orig_plus offset retval.instance_eval { @mark, @size = mark, size } retval end |
- (Malloc) dup
Duplicate object
99 100 101 102 103 |
# File 'lib/malloc_ext.rb', line 99 def dup retval = Malloc.new @size retval.write self retval end |
- (Object) export
Read complete data
123 124 125 |
# File 'lib/malloc_ext.rb', line 123 def export read @size end |
- (String) inspect
Display information about this object
84 85 86 |
# File 'lib/malloc_ext.rb', line 84 def inspect "Malloc(#{@size})" end |
- (String) read(length)
Read data from memory
166 167 168 169 170 |
# File 'lib/malloc_ext.rb', line 166 def read( length ) raise "Only #{@size} bytes of memory left to read " + "(illegal attempt to read #{length} bytes)" if length > @size orig_read length end |
- (String) to_s
Display information about this object
112 113 114 |
# File 'lib/malloc_ext.rb', line 112 def to_s inspect end |
- (String) write(data)
Write data to memory
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/malloc_ext.rb', line 187 def write( data ) if data.is_a? Malloc if data.size > @size raise "Must not write more than #{@size} bytes of memory " + "(illegal attempt to write #{data.size} bytes)" end orig_write data, data.size else if data.bytesize > @size raise "Must not write more than #{@size} bytes of memory " + "(illegal attempt to write #{data.bytesize} bytes)" end orig_write data end end |