Class: FFI::Mmap

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/mmap/mmap.rb,
lib/ffi/mmap/version.rb

Defined Under Namespace

Modules: Internal

Constant Summary collapse

PROT_READ =
1
MAP_SHARED =
1
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode, flags) ⇒ Mmap

Returns a new instance of Mmap.



16
17
18
19
20
21
22
# File 'lib/ffi/mmap/mmap.rb', line 16

def initialize(filename, mode, flags)
    @f    = File.open(filename, mode)
    @size = @f.size
    @m    = FFI::AutoPointer.new(Internal.mmap(nil,@size,PROT_READ, flags,@f.fileno,0),
        self.method(:munmap))
    raise "Mmap failed" if @m.address == 0xffffffffffffffff
end

Instance Method Details

#[](ndx) ⇒ Object



30
31
32
33
34
35
# File 'lib/ffi/mmap/mmap.rb', line 30

def[] ndx
    first = ndx.first
    last  = ndx.last
    last = @size-1 if last >= 0 && last >= @size
    @m.get_bytes(first, last - first+1)
end

#munmap(_) ⇒ Object

Called automatically on gc thanks to FFI::AutoPointer wrapper around @m



25
26
27
28
# File 'lib/ffi/mmap/mmap.rb', line 25

def munmap(_)
    Internal.munmap(@m,@len)
    @m = nil
end