Add finalizer to SharedMemory.

This commit is contained in:
Patrick Mahoney 2011-12-17 10:31:09 -06:00
parent 7d71937bf7
commit 7bdae6482f
1 changed files with 14 additions and 2 deletions

View File

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