Class: ProcessShared::Posix::SharedMemory
- Inherits:
-
FFI::Pointer
- Object
- FFI::Pointer
- ProcessShared::Posix::SharedMemory
- 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
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#fd ⇒ Object
readonly
Returns the value of attribute fd.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#type_size ⇒ Object
readonly
Returns the value of attribute type_size.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(type_or_count = 1, count = 1) ⇒ SharedMemory
constructor
A new instance of SharedMemory.
Methods included from OpenWithSelf
Methods included from ObjectBuffer
#get_object, #put_object, #read_object, #write_object
Methods included from LibC
Methods included from Errno
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
34 35 36 |
# File 'lib/process_shared/posix/shared_memory.rb', line 34 def count @count end |
#fd ⇒ Object (readonly)
Returns the value of attribute fd.
34 35 36 |
# File 'lib/process_shared/posix/shared_memory.rb', line 34 def fd @fd end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
34 35 36 |
# File 'lib/process_shared/posix/shared_memory.rb', line 34 def size @size end |
#type ⇒ Object (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_size ⇒ Object (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
Instance Method Details
#close ⇒ Object
77 78 79 80 |
# File 'lib/process_shared/posix/shared_memory.rb', line 77 def close ObjectSpace.undefine_finalizer(self) @finalize.call end |