Add finalizer to SharedMemory.
This commit is contained in:
parent
7d71937bf7
commit
7bdae6482f
|
@ -13,6 +13,14 @@ module ProcessShared
|
||||||
new(size).with_self(&block)
|
new(size).with_self(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.make_finalizer(addr, size, fd)
|
||||||
|
proc do
|
||||||
|
pointer = FFI::Pointer.new(addr)
|
||||||
|
LibC.munmap(pointer, size)
|
||||||
|
LibC.close(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(size)
|
def initialize(size)
|
||||||
@size = case size
|
@size = case size
|
||||||
when Symbol
|
when Symbol
|
||||||
|
@ -34,12 +42,16 @@ module ProcessShared
|
||||||
LibC::MAP_SHARED,
|
LibC::MAP_SHARED,
|
||||||
@fd,
|
@fd,
|
||||||
0)
|
0)
|
||||||
|
|
||||||
|
@finalize = self.class.make_finalizer(@pointer.address, @size, @fd)
|
||||||
|
ObjectSpace.define_finalizer(self, @finalize)
|
||||||
|
|
||||||
super(@pointer)
|
super(@pointer)
|
||||||
end
|
end
|
||||||
|
|
||||||
def close
|
def close
|
||||||
LibC.munmap(@pointer, @size)
|
ObjectSpace.undefine_finalizer(self)
|
||||||
LibC.close(@fd)
|
@finalize.call
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue