Class: ProcessShared::Posix::SharedMemory

Inherits:
FFI::Pointer
  • Object
show all
Extended by:
OpenWithSelf
Includes:
ObjectBuffer, LibC, Foreign
Defined in:
lib/process_shared/posix/shared_memory.rb

Overview

Memory block shared across processes.

Defined Under Namespace

Modules: Foreign

Constant Summary

Constants included from LibC

LibC::MAP_FAILED, LibC::MAP_PRIVATE, LibC::MAP_SHARED, LibC::O_CREAT, LibC::O_EXCL, LibC::O_RDWR, LibC::PROT_EXEC, LibC::PROT_NONE, LibC::PROT_READ, LibC::PROT_WRITE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OpenWithSelf

open

Methods included from ObjectBuffer

#get_object, #put_object, #read_object, #write_object

Methods included from LibC

type_size

Methods included from Errno

#error_check

Constructor Details

#initialize(type_or_count = 1, count = 1) ⇒ SharedMemory

Returns a new instance of SharedMemory.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/process_shared/posix/shared_memory.rb', line 44

def initialize(type_or_count = 1, count = 1)
  @type, @count = case type_or_count
                  when Symbol
                    [type_or_count, count]
                  else
                    [:uchar, type_or_count]
                  end

  @type_size = FFI.type_size(@type)
  @size = @type_size * @count

  name = "/ps-shm#{rand(10000)}"
  @fd = shm_open(name,
                 O_CREAT | O_RDWR | O_EXCL,
                 0777)
  shm_unlink(name)
  
  ftruncate(@fd, @size)
  @pointer = mmap(nil,
                  @size,
                  LibC::PROT_READ | LibC::PROT_WRITE,
                  LibC::MAP_SHARED,
                  @fd,
                  0).
    slice(0, size) # slice to get FFI::Pointer that knows its size
  # (and thus does bounds checking)

  @finalize = self.class.make_finalizer(@pointer.address, @size, @fd)
  ObjectSpace.define_finalizer(self, @finalize)

  super(@pointer)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count


34
35
36
# File 'lib/process_shared/posix/shared_memory.rb', line 34

def count
  @count
end

#fdObject (readonly)

Returns the value of attribute fd


34
35
36
# File 'lib/process_shared/posix/shared_memory.rb', line 34

def fd
  @fd
end

#sizeObject (readonly)

Returns the value of attribute size


34
35
36
# File 'lib/process_shared/posix/shared_memory.rb', line 34

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type


34
35
36
# File 'lib/process_shared/posix/shared_memory.rb', line 34

def type
  @type
end

#type_sizeObject (readonly)

Returns the value of attribute type_size


34
35
36
# File 'lib/process_shared/posix/shared_memory.rb', line 34

def type_size
  @type_size
end

Class Method Details

.make_finalizer(addr, size, fd) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/process_shared/posix/shared_memory.rb', line 36

def self.make_finalizer(addr, size, fd)
  proc do
    pointer = FFI::Pointer.new(addr)
    LibC.munmap(pointer, size)
    LibC.close(fd)
  end
end

Instance Method Details

#closeObject


77
78
79
80
# File 'lib/process_shared/posix/shared_memory.rb', line 77

def close
  ObjectSpace.undefine_finalizer(self)
  @finalize.call
end