Class: Hornetseye::Malloc

Inherits:
Object
  • Object
show all
Defined in:
lib/malloc_ext.rb

Overview

Class for allocating raw memory and for doing pointer operations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sizeInteger (readonly)

Number of bytes allocated

Examples:

Querying size of allocated memory

m = Malloc.new 32
m.size
# 32
( m + 8 ).size
# 24


95
96
97
# File '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

Examples:

Allocate raw memory

m = Malloc.align 256, 16
# Malloc(256)

See Also:



76
77
78
79
80
81
# File '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.

Examples:

Allocate raw memory

m = Malloc.new 32
# Malloc(32)

See Also:



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

#+(offset) ⇒ Malloc

Operator for doing pointer arithmetic

Examples:

Pointer arithmetic

m = Malloc.new 4
# Malloc(4)
m.write 'abcd'
n = m + 2
# Malloc(2)
n.read 2
# "cd"


160
161
162
163
164
165
166
167
168
# File '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

#dupMalloc

Duplicate object

Examples:

Duplicating a Malloc object

m = Malloc.new 3
m.write 'aaa'
n = m.dup
n.write 'bbb'
m.read 3
"aaa"


119
120
121
122
123
# File 'lib/malloc_ext.rb', line 119

def dup
  retval = Malloc.new @size
  retval.write self
  retval
end

#exportObject

Read complete data

Examples:

Export to string

m = Malloc.new 3
m.write 'abc'
m.export
"abc"


143
144
145
# File 'lib/malloc_ext.rb', line 143

def export
  read @size
end

#inspectString

Display information about this object

Examples:

Displaying information about a Malloc object

Malloc.new( 8 ).inspect
"Malloc(8)"


104
105
106
# File 'lib/malloc_ext.rb', line 104

def inspect
  "Malloc(#{@size})"
end

#read(length) ⇒ String

Read data from memory

Examples:

Reading and writing data

m = Malloc.new 4
# Malloc(4)
m.write 'abcd'
m.read 2
# "ab"

See Also:



186
187
188
189
190
# File '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

#to_sString

Display information about this object

Examples:

Displaying information about a Malloc object

Malloc.new( 8 ).to_s
"Malloc(8)"


132
133
134
# File 'lib/malloc_ext.rb', line 132

def to_s
  inspect
end

#write(data) ⇒ String

Write data to memory

Examples:

Reading and writing data

m = Malloc.new 4
# Malloc(4)
m.write 'abcd'
m.read 2
# "ab"

See Also:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File '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