Class: LLDB::NativeHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/lldb/native_handle.rb

Constant Summary collapse

NULL_POINTER =
FFI::Pointer.new(0)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, release:) ⇒ NativeHandle

Returns a new instance of NativeHandle.

RBS:

  • ptr: FFI::Pointer

  • release: ^(FFI::Pointer) -> void

  • return: void



12
13
14
15
16
17
18
# File 'lib/lldb/native_handle.rb', line 12

def initialize(ptr, release:)
  # The resource is detached with Array#shift, which is a single Ruby VM
  # operation. Finalizers cannot safely acquire a Mutex on all supported
  # Rubies, so the detach itself must not depend on one.
  @state = [[ptr, release]]
  ObjectSpace.define_finalizer(self, self.class.finalizer(@state))
end

Class Method Details

.finalizer(state) ⇒ Object

RBS:

  • state: Array[Array[untyped]]

  • return: ^(Integer) -> void



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lldb/native_handle.rb', line 44

def self.finalizer(state)
  ->(_object_id) do
    resource = state.shift
    next unless resource

    pointer, release = resource
    next unless pointer && !pointer.null?

    begin
      release.call(pointer)
    rescue StandardError
      # Finalizers must never raise into an unrelated Ruby thread.
    end
  end
end

Instance Method Details

#closeObject

RBS:

  • return: bool



32
33
34
35
36
37
38
39
40
# File 'lib/lldb/native_handle.rb', line 32

def close
  resource = @state.shift
  return false unless resource

  pointer, release = resource

  release.call(pointer) unless pointer.null?
  true
end

#closed?Boolean

RBS:

  • return: bool

Returns:

  • (Boolean)


27
28
29
# File 'lib/lldb/native_handle.rb', line 27

def closed?
  @state.empty?
end

#to_ptrObject

RBS:

  • return: FFI::Pointer



21
22
23
24
# File 'lib/lldb/native_handle.rb', line 21

def to_ptr
  resource = @state.first
  resource ? resource.first : NULL_POINTER
end