IO::MemorySourceIOMemoryGeneric

module Generic

Generic implementation of memory-mapped IO using temporary files. This implementation provides maximum compatibility across platforms by using Ruby's built-in Tempfile class and file descriptor mapping. The temporary files are unlinked immediately after creation to behave like anonymous memory objects.

Nested

Definitions

def self.supported?

Check if the generic temporary file implementation is supported on this system. This implementation always returns true as it uses standard Ruby temporary files which are available on all platforms. It serves as a fallback when platform-specific implementations like Linux memfd_create or POSIX shm_open are not available.

Signature

returns Boolean

always true, as temporary files are universally supported

Implementation

def self.supported?
	true
end

def new(size)

Create a new memory-mapped buffer using a temporary file. The temporary file is immediately unlinked to behave like anonymous memory, existing only in the filesystem cache.

Signature

parameter size Integer

size of the memory buffer in bytes

returns Object

a handle object that provides access to the memory buffer

Implementation

def new(size)
	Implementation.create_handle(size)
end

def with(size, &block)

Create a memory-mapped buffer and yield it to a block. The buffer is automatically cleaned up when the block exits, regardless of whether an exception is raised.

Signature

parameter size Integer

size of the memory buffer in bytes

yields {|handle| ...}
parameter handle Object

the handle to the memory buffer with access to IO and mapping operations

returns Object

the result of the block execution

Implementation

def with(size, &block)
	handle = new(size)
	begin
		yield handle
	ensure
		handle.close
	end
end