Module: Aws::Crt::Native

Extended by:
FFI::Library
Defined in:
lib/aws-crt/native.rb

Overview

FFI Bindings to native CRT functions

Defined Under Namespace

Classes: CrtBuf, PropertyList

Class Method Summary collapse

Class Method Details

.array_to_native(array) ⇒ Object

Given a ruby array of strings, return a native array: char** and the FFI::MemoryPointers (these need to be pined for the length the native memory will be used to avoid GC)



62
63
64
65
66
67
68
69
# File 'lib/aws-crt/native.rb', line 62

def self.array_to_native(array)
  native = FFI::MemoryPointer.new(:pointer, array.size)
  pointers = array.map do |s|
    FFI::MemoryPointer.from_string(s.to_s)
  end
  native.write_array_of_pointer(pointers)
  [native, pointers]
end

.attach_function(c_name, params, returns, options = {}) ⇒ Object

Extends FFI::attach_function

  1. Allows us to only supply the aws_crt C name and removes

    the aws_crt.
    
  2. Wraps the call in an error-raise checker (unless options

= false)
  1. Creates a bang method that does not do automatic error checking.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aws-crt/native.rb', line 78

def self.attach_function(c_name, params, returns, options = {})
  ruby_name = c_name.to_s.sub(/aws_crt_/, '').to_sym
  raise_errors = options.fetch(:raise, true)
  options.delete(:raise)
  unless raise_errors
    return super(ruby_name, c_name, params, returns, options)
  end

  bang_name = "#{ruby_name}!"

  super(ruby_name, c_name, params, returns, options)
  alias_method(bang_name, ruby_name)

  define_method(ruby_name) do |*args, &block|
    res = public_send(bang_name, *args, &block)
    # functions that return void cannot fail
    return unless res

    # for functions that return int, non-zero indicates failure
    Errors.raise_last_error if res.is_a?(Integer) && res != 0

    # for functions that return pointer, NULL indicates failure
    Errors.raise_last_error if res.is_a?(FFI::Pointer) && res.null?

    res
  end

  module_function ruby_name
  module_function bang_name
end

.hash_to_native_arrays(hash) ⇒ Object

Given a ruby hash (string -> string), return two native arrays: char** (:pointer) AND a list of all of the FFI::MemoryPointers that must be kept around to avoid GC



53
54
55
56
57
# File 'lib/aws-crt/native.rb', line 53

def self.hash_to_native_arrays(hash)
  key_array, keys_p = array_to_native(hash.keys)
  value_array, values_p = array_to_native(hash.values)
  [key_array, value_array, keys_p + values_p]
end