Module: ProcessShared::ObjectBuffer
- Included in:
- Posix::SharedMemory
- Defined in:
- lib/process_shared/object_buffer.rb
Overview
Provides reading and writing of serialized objects from a memory buffer.
Instance Method Summary collapse
-
#get_object(offset) ⇒ Object
Read the serialized object at
offset
(in bytes) using Marshal.load. -
#put_object(offset, obj) ⇒ Object
Write the serialization of
obj
(using Marshal.dump) to this shared memory object atoffset
(in bytes). -
#read_object ⇒ Object
Equivalent to obj).
-
#write_object(obj) ⇒ Object
Equivalent to obj).
Instance Method Details
#get_object(offset) ⇒ Object
Read the serialized object at offset
(in bytes) using Marshal.load.
33 34 35 36 37 |
# File 'lib/process_shared/object_buffer.rb', line 33 def get_object(offset) io = to_shm_io io.seek(offset) Marshal.load(io) end |
#put_object(offset, obj) ⇒ Object
Write the serialization of obj
(using Marshal.dump) to this shared memory object at offset
(in bytes).
Raises IndexError if there is insufficient space.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/process_shared/object_buffer.rb', line 11 def put_object(offset, obj) # FIXME: This is a workaround to an issue I'm seeing in # 1.8.7-p352 (not tested in other 1.8's). If I used the code # below that works in 1.9, then inside SharedMemoryIO#write, the # passed string object is 'terminated' (garbage collected?) and # won't respond to any methods... This way is less efficient # since it involves the creation of an intermediate string, but # it works in 1.8.7-p352. if RUBY_VERSION =~ /^1.8/ str = Marshal.dump(obj) return put_bytes(offset, str, 0, str.size) end io = SharedMemoryIO.new(self) io.seek(offset) Marshal.dump(obj, io) end |
#read_object ⇒ Object
Equivalent to obj)
47 48 49 |
# File 'lib/process_shared/object_buffer.rb', line 47 def read_object Marshal.load(to_shm_io) end |
#write_object(obj) ⇒ Object
Equivalent to obj)
40 41 42 |
# File 'lib/process_shared/object_buffer.rb', line 40 def write_object(obj) put_object(0, obj) end |