Class: FFI::Mmap
- Inherits:
-
Object
- Object
- FFI::Mmap
- 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
- #[](ndx) ⇒ Object
-
#initialize(filename, mode, flags) ⇒ Mmap
constructor
A new instance of Mmap.
-
#munmap(_) ⇒ Object
Called automatically on gc thanks to FFI::AutoPointer wrapper around @m.
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 |