Update SharedMemory initializer to accept type and size.
This commit is contained in:
parent
c320c75c8d
commit
ea063678cc
|
@ -7,7 +7,7 @@ module ProcessShared
|
|||
class SharedMemory < FFI::Pointer
|
||||
include WithSelf
|
||||
|
||||
attr_reader :size, :fd
|
||||
attr_reader :size, :type, :type_size, :count, :fd
|
||||
|
||||
def self.open(size, &block)
|
||||
new(size).with_self(&block)
|
||||
|
@ -21,14 +21,17 @@ module ProcessShared
|
|||
end
|
||||
end
|
||||
|
||||
def initialize(size)
|
||||
@size = case size
|
||||
def initialize(type_or_count = 1, count = 1)
|
||||
@type, @count = case type_or_count
|
||||
when Symbol
|
||||
FFI.type_size(size)
|
||||
[type_or_count, count]
|
||||
else
|
||||
size
|
||||
[:uchar, type_or_count]
|
||||
end
|
||||
|
||||
@type_size = FFI.type_size(@type)
|
||||
@size = @type_size * @count
|
||||
|
||||
name = "/ps-shm#{rand(10000)}"
|
||||
@fd = RT.shm_open(name,
|
||||
LibC::O_CREAT | LibC::O_RDWR | LibC::O_EXCL,
|
||||
|
|
Loading…
Reference in New Issue