Class: JITBuffer

Inherits:
Object
  • Object
show all
Defined in:
ext/jit_buffer/jit_buffer.c,
lib/jit_buffer.rb

Defined Under Namespace

Modules: MMAP Classes: Exception, OutOfBoundsException, ReadOnlyException

Constant Summary collapse

VERSION =
'1.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory, size) ⇒ JITBuffer

Returns a new instance of JITBuffer.



60
61
62
63
64
65
# File 'lib/jit_buffer.rb', line 60

def initialize memory, size
  @writeable = false
  @memory = memory
  @size   = size
  @pos    = 0
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



58
59
60
# File 'lib/jit_buffer.rb', line 58

def pos
  @pos
end

Class Method Details

.new(size) ⇒ Object



52
53
54
55
56
# File 'lib/jit_buffer.rb', line 52

def self.new size
  x = super(MMAP.mmap_buffer(size), size)
  MMAP.pthread_jit_write_protect_np(true)
  x
end

Instance Method Details

#executable!Object



103
104
105
106
107
# File 'lib/jit_buffer.rb', line 103

def executable!
  MMAP.pthread_jit_write_protect_np true
  MMAP.sys_icache_invalidate @memory.to_i, @size
  @writeable = false
end

#getcObject



81
82
83
84
85
86
# File 'lib/jit_buffer.rb', line 81

def getc
  raise(OutOfBoundsException, "You've gone too far!") if pos >= @size
  x = @memory[pos]
  @pos += 1
  x
end

#putc(byte) ⇒ Object

Raises:



67
68
69
70
71
72
# File 'lib/jit_buffer.rb', line 67

def putc byte
  raise(ReadOnlyException, "Buffer is read only!") unless @writeable
  raise(OutOfBoundsException, "Buffer full! #{pos} - #{@size}") if pos >= @size
  @memory[pos] = byte
  @pos += 1
end

#read(len) ⇒ Object



88
89
90
91
92
93
# File 'lib/jit_buffer.rb', line 88

def read len
  raise(OutOfBoundsException, "You've gone too far!") if pos + len >= @size
  x = @memory[pos, pos + len]
  @pos += len
  x
end

#seek(pos, whence = IO::SEEK_SET) ⇒ Object

Raises:

  • (NotImplementedError)


95
96
97
98
99
100
101
# File 'lib/jit_buffer.rb', line 95

def seek pos, whence = IO::SEEK_SET
  raise NotImplementedError if whence != IO::SEEK_SET
  raise OutOfBoundsException if pos >= @size

  @pos = pos
  self
end

#to_function(params, ret) ⇒ Object



114
115
116
# File 'lib/jit_buffer.rb', line 114

def to_function params, ret
  Fiddle::Function.new @memory.to_i, params, ret
end

#to_iObject

Get the address of the executable memory



119
120
121
# File 'lib/jit_buffer.rb', line 119

def to_i
  @memory.to_i
end

#write(bytes) ⇒ Object

Raises:



74
75
76
77
78
79
# File 'lib/jit_buffer.rb', line 74

def write bytes
  raise(ReadOnlyException, "Buffer is read only!") unless @writeable
  raise OutOfBoundsException if pos + bytes.bytesize >= @size
  @memory[pos, bytes.length] = bytes
  @pos += bytes.bytesize
end

#writeable!Object



109
110
111
112
# File 'lib/jit_buffer.rb', line 109

def writeable!
  MMAP.pthread_jit_write_protect_np false
  @writeable = true
end