Sha256: d42bf477e036215716265fdd90aca0c2d6bceed40aec87b79cd41382ea70f0d5
Contents?: true
Size: 902 Bytes
Versions: 2
Compression:
Stored size: 902 Bytes
Contents
# frozen_string_literal: true require 'gir_ffi/lib_c' module GirFFI # Helper module providing a safe allocation method that raises an exception # if memory cannot be allocated. module AllocationHelper # NOTE: It would be preferable to use FFI::MemoryPointer.new(size), but # there is a bug in FFI which means this gives a problem: # # let ptr be a pointer not allocated by FFI. # ptr2 = FFI::MemoryPointer.new(1) # ptr.put_pointer ptr2 # This raises an out-of-bounds error. # This occurs in method_int8_arg_and_out_callee def self.safe_malloc(size) ptr = LibC.malloc size raise NoMemoryError if ptr.null? ptr end def self.allocate(type) type_size = FFI.type_size type safe_malloc(type_size) end def self.free_after(ptr) result = yield ptr LibC.free ptr unless ptr.null? result end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
gir_ffi-0.10.2 | lib/gir_ffi/allocation_helper.rb |
gir_ffi-0.10.1 | lib/gir_ffi/allocation_helper.rb |