Class: Hornetseye::Malloc
- Defined in:
- docs/malloc/lib/malloc_ext.rb,
docs/multiarray/lib/multiarray/malloc.rb
Overview
Malloc is extended with a few methods
Instance Attribute Summary collapse
-
#size ⇒ Integer
readonly
Number of bytes allocated.
Class Method Summary collapse
-
.align(size, alignment) ⇒ Malloc
Create new Malloc object with aligned memory.
-
.new(size) ⇒ Malloc
Create new Malloc object.
Instance Method Summary collapse
-
#+(offset) ⇒ Malloc
Operator for doing pointer arithmetic.
-
#dup ⇒ Malloc
Duplicate object.
-
#export ⇒ Object
Read complete data.
-
#inspect ⇒ String
Display information about this object.
-
#load(typecode) ⇒ Node
Read typed value.
-
#read(length) ⇒ String
Read data from memory.
-
#save(value) ⇒ Node
Write typed value to memory.
-
#to_s ⇒ String
Display information about this object.
-
#write(data) ⇒ String
Write data to memory.
Instance Attribute Details
#size ⇒ Integer (readonly)
Number of bytes allocated
95 96 97 |
# File 'docs/malloc/lib/malloc_ext.rb', line 95 def size @size end |
Class Method Details
.align(size, alignment) ⇒ Malloc
Create new Malloc object with aligned memory
Allocate memory with specified alignment
76 77 78 79 80 81 |
# File 'docs/malloc/lib/malloc_ext.rb', line 76 def align size, alignment offset = alignment - 1 retval = Malloc.new size + offset incr = ((retval.to_i + offset) & ~offset) - retval.to_i retval + incr end |
.new(size) ⇒ Malloc
Create new Malloc object
Allocate the specified number of bytes of raw memory.
55 56 57 58 59 |
# File 'docs/malloc/lib/malloc_ext.rb', line 55 def new( size ) retval = orig_new size retval.instance_eval { @size = size } retval end |
Instance Method Details
#+(offset) ⇒ Malloc
Operator for doing pointer arithmetic
160 161 162 163 164 165 166 167 168 |
# File 'docs/malloc/lib/malloc_ext.rb', line 160 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 |
#dup ⇒ Malloc
Duplicate object
119 120 121 122 123 |
# File 'docs/malloc/lib/malloc_ext.rb', line 119 def dup retval = Malloc.new @size retval.write self retval end |
#export ⇒ Object
Read complete data
143 144 145 |
# File 'docs/malloc/lib/malloc_ext.rb', line 143 def export read @size end |
#inspect ⇒ String
Display information about this object
104 105 106 |
# File 'docs/malloc/lib/malloc_ext.rb', line 104 def inspect "Malloc(#{@size})" end |
#load(typecode) ⇒ Node
Read typed value
30 31 32 |
# File 'docs/multiarray/lib/multiarray/malloc.rb', line 30 def load( typecode ) read( typecode.storage_size ).unpack( typecode.directive ) end |
#read(length) ⇒ String
Read data from memory
186 187 188 189 190 |
# File 'docs/malloc/lib/malloc_ext.rb', line 186 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 |
#save(value) ⇒ Node
Write typed value to memory
39 40 41 42 |
# File 'docs/multiarray/lib/multiarray/malloc.rb', line 39 def save( value ) write value.values.pack( value.typecode.directive ) value end |
#to_s ⇒ String
Display information about this object
132 133 134 |
# File 'docs/malloc/lib/malloc_ext.rb', line 132 def to_s inspect end |
#write(data) ⇒ String
Write data to memory
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'docs/malloc/lib/malloc_ext.rb', line 207 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 |