evanphx / rubinius

Rubinius, the Ruby VM

This URL has Read+Write access

rubinius / lib / memory_block.rb
100644 51 lines (40 sloc) 0.875 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'memory'
 
class MemoryBlock
  def initialize(size, clear=false)
    @size = size
    @address = Memory.allocate_memory size
    if clear
      Memory.clear_memory @address, @size
    end
  end
  
  def deallocate
    Memory.release_memory @address
  end
  
  attr_reader :address, :size
    
  def byte(index)
    Memory.fetch_byte(@address + index)
  end
  
  def long(index)
    Memory.fetch_long @address, index
  end
  
  def store_byte(index, data)
    Memory.store_byte(@address + index, data)
  end
  
  def store_long(index, data)
    Memory.store_long(@address, index, data)
  end
  
  def long_in_bounds?(index)
    long = Memory.long_size
    start = index * long
    if start > @size - long
      return false
    end
    
    return true
  end
  
  def byte_in_bounds?(index)
    if index > @size
      return false
    end
    
    return true
  end
end