class Libvirt::HostCallbackStorage

Attributes

name[R]

Public Class Methods

new(name) click to toggle source
# File lib/libvirt/host_callback_storage.rb, line 12
def initialize(name)
  @name = name
  @inner_storage = Hash.new { |h, key| h[key] = {} }
end

Public Instance Methods

allocate_struct() click to toggle source

@return [Array<2>]

cb_data [Libvirt::HostCallbackStorage::CallbackDataStruct],
cb_data_free_func [FFI::Function]
# File lib/libvirt/host_callback_storage.rb, line 20
def allocate_struct
  dbg { '#allocate_struct' }

  cb_data_ptr = ::FFI::MemoryPointer.new(:char, CallbackDataStruct.size, false)
  cb_data = CallbackDataStruct.new(cb_data_ptr)
  cb_data_free_func = FFI::Common.free_function do |pointer|
    dbg { "cb_data_free_func triggered pointer=#{pointer}" }
    remove_struct(pointer)
  end
  [cb_data, cb_data_free_func]
end
remove_struct(pointer) click to toggle source
# File lib/libvirt/host_callback_storage.rb, line 48
def remove_struct(pointer)
  dbg { "#remove_struct pointer=#{pointer}," }

  cb_data_struct = CallbackDataStruct.new(pointer)
  connection_pointer = cb_data_struct[:connection_pointer]
  callback_id = cb_data_struct[:callback_id]
  dbg { "#remove_struct pointer=#{pointer}, connection_pointer=#{connection_pointer}, callback_id=#{callback_id}," }

  cb_data = @inner_storage[connection_pointer.address].delete(callback_id)
  @inner_storage.delete(connection_pointer.address) if @inner_storage[connection_pointer.address].empty?

  cb_data[:opaque]
end
retrieve_from_pointer(pointer) click to toggle source

@param [::FFI::Pointer] @return [Array<2>] cb [Proc], opaque [Object]

# File lib/libvirt/host_callback_storage.rb, line 64
def retrieve_from_pointer(pointer)
  dbg { "#retrieve_from_pointer pointer=#{pointer}," }

  cb_data_struct = CallbackDataStruct.new(pointer)
  connection_pointer = cb_data_struct[:connection_pointer]
  callback_id = cb_data_struct[:callback_id]
  cb_data = @inner_storage[connection_pointer.address][callback_id]
  [cb_data[:cb], cb_data[:opaque]]
end
store_struct(cb_data, options) click to toggle source
# File lib/libvirt/host_callback_storage.rb, line 32
def store_struct(cb_data, options)
  dbg { '#store_struct' }

  options.assert_valid_keys(:connection_pointer, :callback_id, :cb, :opaque, :free_func)
  connection_pointer = options.fetch(:connection_pointer)
  callback_id = options.fetch(:callback_id)
  cb = options.fetch(:cb)
  opaque = options.fetch(:opaque)
  free_func = options.fetch(:free_func)
  cb_data[:connection_pointer] = connection_pointer
  cb_data[:callback_id] = callback_id
  @inner_storage[connection_pointer.address][callback_id] = {
      cb: cb, opaque: opaque, pointer: cb_data.pointer, free_func: free_func
  }
end

Private Instance Methods

dbg(&block) click to toggle source
# File lib/libvirt/host_callback_storage.rb, line 76
def dbg(&block)
  Util.log(:debug, "Libvirt::HostCallbackStorage(#{name})", &block)
end