Update SharedMemory initializer to accept type and size.

This commit is contained in:
Patrick Mahoney 2011-12-17 21:50:39 -06:00
parent c320c75c8d
commit ea063678cc
1 changed files with 11 additions and 8 deletions

View File

@ -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,