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

- (Integer) size (readonly)

Number of bytes allocated

Examples:

Querying size of allocated memory

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

Returns:

  • (Integer)

    Size of allocated memory.



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.

Examples:

Allocate raw memory

m = Malloc.new 32
# Malloc(32)

Parameters:

  • size (Integer)

    Number of bytes to allocate.

Returns:

  • (Malloc)

    A new Malloc object.

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

- (Malloc) +(offset)

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"

Parameters:

  • offset (Integer)

    Non-negative offset for pointer.

Returns:

  • (Malloc)

    A new Malloc object.



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

Examples:

Duplicating a Malloc object

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

Returns:

  • (Malloc)

    A new malloc object with a copy of the data.



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

Examples:

Export to string

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


123
124
125
# File 'lib/malloc_ext.rb', line 123

def export
  read @size
end

- (String) inspect

Display information about this object

Examples:

Displaying information about a Malloc object

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

Returns:

  • (String)

    A string with 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

Examples:

Reading and writing data

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

Parameters:

  • length (Integer)

    Number of bytes to read.

Returns:

  • (String)

    A string containing the data.

See Also:



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

Examples:

Displaying information about a Malloc object

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

Returns:

  • (String)

    A string with 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

Examples:

Reading and writing data

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

Parameters:

  • data (String, Malloc)

    A string or malloc object with the data.

Returns:

  • (String)

    Returns the parameter string.

See Also:



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