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 class SharedMemory < FFI::Pointer
include WithSelf include WithSelf
attr_reader :size, :fd attr_reader :size, :type, :type_size, :count, :fd
def self.open(size, &block) def self.open(size, &block)
new(size).with_self(&block) new(size).with_self(&block)
@ -21,13 +21,16 @@ module ProcessShared
end end
end end
def initialize(size) def initialize(type_or_count = 1, count = 1)
@size = case size @type, @count = case type_or_count
when Symbol when Symbol
FFI.type_size(size) [type_or_count, count]
else else
size [:uchar, type_or_count]
end end
@type_size = FFI.type_size(@type)
@size = @type_size * @count
name = "/ps-shm#{rand(10000)}" name = "/ps-shm#{rand(10000)}"
@fd = RT.shm_open(name, @fd = RT.shm_open(name,