Class: FFI::AutoPointer

Inherits:
Pointer show all
Extended by:
DataConverter
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb

Defined Under Namespace

Classes: CallableReleaser, DefaultReleaser, Releaser

Constant Summary

Constants inherited from Pointer

Pointer::SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConverter

from_native, native_type, to_native

Methods inherited from Pointer

#read, #read_array_of_type, #read_string, #read_string_length, #read_string_to_null, size, #to_ptr, #write, #write_array_of_type, #write_string, #write_string_length

Constructor Details

#initialize(pointer, method) ⇒ self #initialize(pointer, proc) ⇒ self #initialize(pointer) {|p| ... } ⇒ self #initialize(pointer) ⇒ self

Note:

The safest, and therefore preferred, calling idiom is to pass a Method as the second parameter. Example usage:

class PointerHelper
  def self.release(pointer)
    ...
  end
end

p = AutoPointer.new(other_pointer, PointerHelper.method(:release))

The above code will cause PointerHelper#release to be invoked at GC time.

Note:

The last calling idiom (only one parameter) is generally only going to be useful if you subclass FFI::AutoPointer, and override #release, which by default does nothing.

Returns a new instance of AutoPointer.

Overloads:

  • #initialize(pointer, method) ⇒ self

    The passed Method will be invoked at GC time.

    Parameters:

  • #initialize(pointer, proc) ⇒ self
    Note:

    WARNING: passing a proc may cause your pointer to never be GC’d, unless you’re careful to avoid trapping a reference to the pointer in the proc. See the test specs for examples.

    The passed Proc will be invoked at GC time (SEE WARNING BELOW!)

    Parameters:

  • #initialize(pointer) {|p| ... } ⇒ self
    Note:

    WARNING: passing a block will cause your pointer to never be GC’d. This is bad.

    The passed block will be invoked at GC time.

    Parameters:

    Yield Parameters:

    • p (Pointer)

      pointer passed to the block

  • #initialize(pointer) ⇒ self

    The pointer’s release() class method will be invoked at GC time.

    Parameters:

Raises:

  • (TypeError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb', line 78

def initialize(ptr, proc=nil, &block)
  super(ptr.type_size, ptr)
  raise TypeError, "Invalid pointer" if ptr.nil? || !ptr.kind_of?(Pointer) \
    || ptr.kind_of?(MemoryPointer) || ptr.kind_of?(AutoPointer)

  @releaser = if proc
                if not proc.respond_to?(:call)
                  raise RuntimeError.new("proc must be callable")
                end
                CallableReleaser.new(ptr, proc)

              else
                if not self.class.respond_to?(:release)
                  raise RuntimeError.new("no release method defined")
                end
                DefaultReleaser.new(ptr, self.class)
              end

  ObjectSpace.define_finalizer(self, @releaser)
  self
end

Class Method Details

.self.from_native(ptr, ctx) ⇒ AutoPointer

Create a new AutoPointer.

Override DataConverter#from_native.

Parameters:

  • ptr (Pointer)
  • ctx

    not used. Please set nil.

Returns:



198
199
200
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb', line 198

def self.from_native(val, ctx)
  self.new(val)
end

.native_typeType::POINTER

Return native type of AutoPointer.

Override DataConverter#native_type.

Returns:

  • (Type::POINTER)

Raises:

  • (RuntimeError)

    if class does not implement a #release method



184
185
186
187
188
189
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb', line 184

def self.native_type
  if not self.respond_to?(:release)
    raise RuntimeError.new("no release method defined for #{self.inspect}")
  end
  Type::POINTER
end

Instance Method Details

#autorelease=(autorelease) ⇒ Boolean

Set autorelease property. See Autorelease section at Pointer.

Parameters:

  • autorelease (Boolean)

Returns:

  • (Boolean)

    autorelease



109
110
111
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb', line 109

def autorelease=(autorelease)
  @releaser.autorelease=(autorelease)
end

#autorelease?Boolean

Get autorelease property. See Autorelease section at Pointer.

Returns:

  • (Boolean)

    autorelease



115
116
117
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb', line 115

def autorelease?
  @releaser.autorelease
end

#freenil

Free the pointer.

Returns:

  • (nil)


102
103
104
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb', line 102

def free
  @releaser.free
end