Module: OklahomaMixer::Utilities::FFIDSL

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(ffi_interface) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/oklahoma_mixer/utilities.rb', line 6

def self.extended(ffi_interface)
  ffi_interface.extend(FFI::Library)
  ffi_interface.ffi_lib(
    *Array(
      ENV.fetch(
        "TOKYO_CABINET_LIB",
        Dir["/{opt,usr}/{,local/}lib{,64}/libtokyocabinet.{dylib,so*}"]
      )
    )
  )
rescue LoadError
  fail "Tokyo Cabinet could not be loaded "           +
       "(you can install it from http://1978th.net/ " +
       "and set the TOKYO_CABINET_LIB environment variable to its path)"
end

Instance Method Details

#call(details) ⇒ Object


56
57
58
59
60
61
62
# File 'lib/oklahoma_mixer/utilities.rb', line 56

def call(details)
  args =  [ ]
  args << details[:name]
  args << Array(details[:args])
  args << details.fetch(:returns, :void)
  callback(*args)
end

#const(name, values) ⇒ Object


27
28
29
30
31
# File 'lib/oklahoma_mixer/utilities.rb', line 27

def const(name, values)
  const_set( name, enum( *(0...values.size).map { |i|
    ["#{prefix.to_s[2..-1].upcase}#{values[i]}".to_sym, 1 << i]
  }.flatten ) )
end

#def_core_database_consts_and_funcsObject


71
72
73
74
75
76
77
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
108
109
110
111
112
113
114
115
116
# File 'lib/oklahoma_mixer/utilities.rb', line 71

def def_core_database_consts_and_funcs
  const :MODES, %w[OREADER OWRITER OCREAT OTRUNC ONOLCK OLCKNB OTSYNC]

  def_new_and_del_funcs
  
  func :name    => :open,
       :args    => [:pointer, :string, const_get(:MODES)],
       :returns => :bool
  func :name    => :sync,
       :args    => :pointer,
       :returns => :bool
  func :name    => :fsiz,
       :args    => :pointer,
       :returns => :uint64
  func :name    => :copy,
       :args    => [:pointer, :string],
       :returns => :bool

  func :name    => :ecode,
       :args    => :pointer,
       :returns => :int
  func :name    => :errmsg,
       :args    => :int,
       :returns => :string

  func :name    => :setmutex,
       :args    => :pointer,
       :returns => :bool

  func :name    => :vanish,
       :args    => :pointer,
       :returns => :bool
  func :name    => :rnum,
       :args    => :pointer,
       :returns => :uint64

  func :name    => :tranbegin,
       :args    => :pointer,
       :returns => :bool
  func :name    => :trancommit,
       :args    => :pointer,
       :returns => :bool
  func :name    => :tranabort,
       :args    => :pointer,
       :returns => :bool
end

#def_hash_database_consts_and_funcsObject


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/oklahoma_mixer/utilities.rb', line 118

def def_hash_database_consts_and_funcs
  const :OPTS, %w[TLARGE TDEFLATE TBZIP TTCBS]

  def_core_database_consts_and_funcs

  func :name    => :defrag,
       :args    => [:pointer, :int64],
       :returns => :bool

  func :name    => :setxmsiz,
       :args    => [:pointer, :int64],
       :returns => :bool
  func :name    => :setdfunit,
       :args    => [:pointer, :int32],
       :returns => :bool

  func :name    => :put,
       :args    => [:pointer, :pointer, :int, :pointer, :int],
       :returns => :bool
  func :name    => :putkeep,
       :args    => [:pointer, :pointer, :int, :pointer, :int],
       :returns => :bool
  func :name    => :putcat,
       :args    => [:pointer, :pointer, :int, :pointer, :int],
       :returns => :bool
  call :name    => :TCPDPROC,
       :args    => [:pointer, :int, :pointer, :pointer],
       :returns => :pointer
  func :name    => :putproc,
       :args    => [:pointer, :pointer, :int, :pointer, :int, :TCPDPROC,
                    :pointer],
       :returns => :bool
  func :name    => :addint,
       :args    => [:pointer, :pointer, :int, :int],
       :returns => :int
  func :name    => :adddouble,
       :args    => [:pointer, :pointer, :int, :double],
       :returns => :double
  func :name    => :out,
       :args    => [:pointer, :pointer, :int],
       :returns => :bool
  func :name    => :get,
       :args    => [:pointer, :pointer, :int, :pointer],
       :returns => :pointer
  func :name    => :fwmkeys,
       :args    => [:pointer, :pointer, :int, :int],
       :returns => :pointer
end

#def_new_and_del_funcsObject


64
65
66
67
68
69
# File 'lib/oklahoma_mixer/utilities.rb', line 64

def def_new_and_del_funcs
  func :name    => :new,
       :returns => :pointer
  func :name    => :del,
       :args    => :pointer
end

#func(details) ⇒ Object


33
34
35
36
37
38
39
40
# File 'lib/oklahoma_mixer/utilities.rb', line 33

def func(details)
  args =  [ ]
  args << details.fetch(:alias, details[:name])
  args << "#{prefix}#{details[:name]}".to_sym
  args << Array(details[:args])
  args << details.fetch(:returns, :void)
  attach_function(*args)
end

#prefix(new_prefix = nil) ⇒ Object


22
23
24
25
# File 'lib/oklahoma_mixer/utilities.rb', line 22

def prefix(new_prefix = nil)
  @prefix = new_prefix unless new_prefix.nil?
  defined?(@prefix) and @prefix
end

#read_from_func(func, *args) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oklahoma_mixer/utilities.rb', line 42

def read_from_func(func, *args)
  no_free = args.shift if args.first.is_a?(Symbol) and
                          args.first == :no_free
  Utilities.temp_int do |size|
    begin
      args    << size
      pointer =  send(func, *args)
      pointer.address.zero? ? nil : pointer.get_bytes(0, size.get_int(0))
    ensure
      Utilities.free(pointer) if pointer and not no_free
    end
  end
end