-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
tech.v3.datatype.ffi.html
189 lines (187 loc) · 43 KB
/
tech.v3.datatype.ffi.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
117
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html PUBLIC ""
"">
<html><head><meta charset="UTF-8" /><title>tech.v3.datatype.ffi documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="highlight/solarized-light.css" /><script type="text/javascript" src="highlight/highlight.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/page_effects.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div id="header"><h2>Generated by <a href="https://github.com/weavejester/codox">Codox</a> with <a href="https://github.com/xsc/codox-theme-rdash">RDash UI</a> theme</h2><h1><a href="index.html"><span class="project-title"><span class="project-name">dtype-next</span> <span class="project-version">8.042</span></span></a></h1></div><div class="sidebar primary"><h3 class="no-link"><span class="inner">Project</span></h3><ul class="index-link"><li class="depth-1 "><a href="index.html"><div class="inner">Index</div></a></li></ul><h3 class="no-link"><span class="inner">Topics</span></h3><ul><li class="depth-1 "><a href="buffered-image.html"><div class="inner"><span>Buffered Image Support</span></div></a></li><li class="depth-1 "><a href="cheatsheet.html"><div class="inner"><span>Cheatsheet</span></div></a></li><li class="depth-1 "><a href="datatype-to-dtype-next.html"><div class="inner"><span>Why dtype-next?</span></div></a></li><li class="depth-1 "><a href="dimensions-bytecode-gen.html"><div class="inner"><span>Dimensions and Bytecode Generation</span></div></a></li><li class="depth-1 "><a href="overview.html"><div class="inner"><span>Overview</span></div></a></li></ul><h3 class="no-link"><span class="inner">Namespaces</span></h3><ul><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>tech</span></div></div></li><li class="depth-2"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>v3</span></div></div></li><li class="depth-3"><a href="tech.v3.datatype.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>datatype</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.argops.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>argops</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.bitmap.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>bitmap</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.convolve.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>convolve</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.datetime.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>datetime</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.errors.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>errors</span></div></a></li><li class="depth-4 current"><a href="tech.v3.datatype.ffi.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>ffi</span></div></a></li><li class="depth-5 branch"><a href="tech.v3.datatype.ffi.clang.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>clang</span></div></a></li><li class="depth-5"><a href="tech.v3.datatype.ffi.graalvm.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>graalvm</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.functional.html"><div class="inner"><span class="tree" style="top: -83px;"><span class="top" style="height: 92px;"></span><span class="bottom"></span></span><span>functional</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.gradient.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>gradient</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.jna.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>jna</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.jvm-map.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>jvm-map</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.list.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>list</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.locker.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>locker</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.mmap.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mmap</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.mmap-writer.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mmap-writer</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.native-buffer.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>native-buffer</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.nippy.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>nippy</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.packing.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>packing</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.reductions.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>reductions</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.rolling.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>rolling</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.sampling.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>sampling</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.datatype.struct.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>struct</span></div></a></li><li class="depth-4"><a href="tech.v3.datatype.wavelet.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>wavelet</span></div></a></li><li class="depth-3"><div class="no-link"><div class="inner"><span class="tree" style="top: -765px;"><span class="top" style="height: 774px;"></span><span class="bottom"></span></span><span>libs</span></div></div></li><li class="depth-4 branch"><a href="tech.v3.libs.buffered-image.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>buffered-image</span></div></a></li><li class="depth-4"><a href="tech.v3.libs.neanderthal.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>neanderthal</span></div></a></li><li class="depth-3"><div class="no-link"><div class="inner"><span class="tree" style="top: -83px;"><span class="top" style="height: 92px;"></span><span class="bottom"></span></span><span>parallel</span></div></div></li><li class="depth-4"><a href="tech.v3.parallel.for.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>for</span></div></a></li><li class="depth-3"><a href="tech.v3.tensor.html"><div class="inner"><span class="tree" style="top: -52px;"><span class="top" style="height: 61px;"></span><span class="bottom"></span></span><span>tensor</span></div></a></li><li class="depth-4 branch"><a href="tech.v3.tensor.color-gradients.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>color-gradients</span></div></a></li><li class="depth-4"><a href="tech.v3.tensor.dimensions.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>dimensions</span></div></a></li></ul></div><div class="sidebar secondary"><h3><a href="#top"><span class="inner">Public Vars</span></a></h3><ul><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-c-.3Estring"><div class="inner"><span>c->string</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-clear-string-.3Ec-stats.21"><div class="inner"><span>clear-string->c-stats!</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-define-foreign-interface"><div class="inner"><span>define-foreign-interface</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-define-library"><div class="inner"><span>define-library</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-define-library-functions"><div class="inner"><span>define-library-functions</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-define-library-interface"><div class="inner"><span>define-library-interface</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-enable-string-.3Ec-stats.21"><div class="inner"><span>enable-string->c-stats!</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-find-library"><div class="inner"><span>find-library</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-find-symbol"><div class="inner"><span>find-symbol</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-foreign-interface-instance-.3Ec"><div class="inner"><span>foreign-interface-instance->c</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-if-class"><div class="inner"><span>if-class</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-instantiate-class"><div class="inner"><span>instantiate-class</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-instantiate-foreign-interface"><div class="inner"><span>instantiate-foreign-interface</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-instantiate-library"><div class="inner"><span>instantiate-library</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-jdk-ffi.3F"><div class="inner"><span>jdk-ffi?</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-jdk-mmodel.3F"><div class="inner"><span>jdk-mmodel?</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-jna-ffi.3F"><div class="inner"><span>jna-ffi?</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-library-loadable.3F"><div class="inner"><span>library-loadable?</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-library-singleton"><div class="inner"><span>library-singleton</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-make-ptr"><div class="inner"><span>make-ptr</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-PLibrarySingleton"><div class="inner"><span>PLibrarySingleton</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-definition"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-definition</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-find-fn"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-find-fn</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-find-symbol"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-find-symbol</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-library"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-library</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-library-path"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-library-path</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-reset.21"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-reset!</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var-library-singleton-set.21"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-set!</span></div></a></li><li class="depth-2"><a href="tech.v3.datatype.ffi.html#var-library-singleton-set-instance.21"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>library-singleton-set-instance!</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-PToPointer"><div class="inner"><span>PToPointer</span></div></a></li><li class="depth-2 branch"><a href="tech.v3.datatype.ffi.html#var--.3Epointer"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>->pointer</span></div></a></li><li class="depth-2"><a href="tech.v3.datatype.ffi.html#var-convertible-to-pointer.3F"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>convertible-to-pointer?</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-ptr-.3Estruct"><div class="inner"><span>ptr->struct</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-set-ffi-impl.21"><div class="inner"><span>set-ffi-impl!</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-string-.3Ec"><div class="inner"><span>string->c</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-string-.3Ec-data-histogram"><div class="inner"><span>string->c-data-histogram</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-struct-member-ptr"><div class="inner"><span>struct-member-ptr</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-utf16-platform-encoding"><div class="inner"><span>utf16-platform-encoding</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-utf32-platform-encoding"><div class="inner"><span>utf32-platform-encoding</span></div></a></li><li class="depth-1"><a href="tech.v3.datatype.ffi.html#var-windows-platform.3F"><div class="inner"><span>windows-platform?</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">tech.v3.datatype.ffi</h1><div class="doc"><div class="markdown"><p>Generalized C Foreign Function Interface (FFI) that unifies JNA and JDK-16 FFI architectures.</p>
<p>Users can dynamically define and load libraries and define callbacks that C can then call.</p>
<p>This namespace is meant to work with the <code>struct</code> namespace where you can define C-structs laid out precisely in memory.</p>
<p>Available datatypes for the binding layer:</p>
<ul>
<li><code>:int8</code> <code>:int16</code> <code>:int32</code> <code>:int64</code> <code>:float32</code> <code>:float64</code> <code>:pointer</code> <code>:pointer?</code> <code>:size-t</code>.</li>
</ul>
<p><code>:pointer</code>, <code>:pointer?</code>, <code>:size-t</code> are all types that change their underlying definition depending on if the system is 32 bit or 64 bit.</p>
<p>Note that unsigned types will work but the interface will have to be defined in terms of their signed analogues.</p>
<p>Example:</p>
<pre><code class="clojure"><br />user> (require '[tech.v3.datatype :as dtype])
nil
user> (require '[tech.v3.datatype.ffi :as dtype-ffi])
nil
user> (def libmem-def (dtype-ffi/define-library
{:qsort {:rettype :void
:argtypes [['data :pointer]
['nitems :size-t]
['item-size :size-t]
['comparator :pointer]]}}))
#'user/libmem-def
user> (def libmem-inst (dtype-ffi/instantiate-library libmem-def nil))
#'user/libmem-inst
user> (def libmem-fns @libmem-inst)
#'user/libmem-fns
user> (def qsort (:qsort libmem-fns))
#'user/qsort
user> (def comp-iface-def (dtype-ffi/define-foreign-interface :int32 [:pointer :pointer]))
#'user/comp-iface-def
user> (require '[tech.v3.datatype.native-buffer :as native-buffer])
nil
user> (def comp-iface-inst (dtype-ffi/instantiate-foreign-interface
comp-iface-def
(fn [^tech.v3.datatype.ffi.Pointer lhs ^tech.v3.datatype.ffi.Pointer rhs]
(let [lhs (.getDouble (native-buffer/unsafe) (.address lhs))
rhs (.getDouble (native-buffer/unsafe) (.address rhs))]
(Double/compare lhs rhs)))))
#'user/comp-iface-inst
user> (def comp-iface-ptr (dtype-ffi/foreign-interface-instance->c
comp-iface-def
comp-iface-inst))
#'user/comp-iface-ptr
user> (def dbuf (dtype/make-container :native-heap :float64 (shuffle (range 100))))
09:00:27.900 [tech.resource.gc ref thread] INFO tech.v3.resource.gc - Reference thread starting
#'user/dbuf
user> dbuf
#native-buffer@0x00007F47084E4210<float64>[100]
[80.00, 90.00, 96.00, 29.00, 19.00, 12.00, 88.00, 94.00, 81.00, 17.00, 54.00, 52.00, 64.00, 86.00, 10.00, 76.00, 49.00, 5.000, 32.00, 69.00, ...]
user> (qsort dbuf (dtype/ecount dbuf) Double/BYTES comp-iface-ptr)
nil
user> dbuf
#native-buffer@0x00007F47084E4210<float64>[100]
[0.000, 1.000, 2.000, 3.000, 4.000, 5.000, 6.000, 7.000, 8.000, 9.000, 10.00, 11.00, 12.00, 13.00, 14.00, 15.00, 16.00, 17.00, 18.00, 19.00, ...]
</code></pre></div></div><div class="public anchor" id="var-c-.3Estring"><h3>c->string</h3><div class="usage"><code>(c->string data & [encoding])</code></div><div class="doc"><div class="markdown"><p>Convert a zero-terminated c-string to a java string. See documentation for <code>string->c</code> for encodings.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L225">view source</a></div></div><div class="public anchor" id="var-clear-string-.3Ec-stats.21"><h3>clear-string->c-stats!</h3><div class="usage"><code>(clear-string->c-stats!)</code></div><div class="doc"><div class="markdown"></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L187">view source</a></div></div><div class="public anchor" id="var-define-foreign-interface"><h3>define-foreign-interface</h3><div class="usage"><code>(define-foreign-interface rettype argtypes & [{:as options}])</code></div><div class="doc"><div class="markdown"><p>Define a strongly typed instance that can be used with foreign-interface-instance->c. This instance takes a single constructor argument which is the IFn that it is wrapping. It has a single typesafe ‘invoke’ method taking types defined by the underlying binding and transforming them into types that Clojure expects - for instance pointers are transformed to/from <code>tech.v3.datatype.ffi.Pointer</code> upon entry/exit from the wrapped IFn.</p>
<ul>
<li><code>rettype</code> - The return type of the function.</li>
<li><code>argtypes</code> - A possibly empty sequence of argument types.</li>
</ul>
<p>Options:</p>
<ul>
<li><code>:classname</code> - Similar to <code>:classname</code> in ‘define-library’. A class will be generated to <em>compile-path</em> and after this statement <code>(import classname)</code> will be a valid call.</li>
</ul>
<p>See foreign-interface-instance->c for example.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L405">view source</a></div></div><div class="public anchor" id="var-define-library"><h3>define-library</h3><div class="usage"><code>(define-library fn-defs symbols options)</code><code>(define-library fn-defs options)</code><code>(define-library fn-defs)</code></div><div class="doc"><div class="markdown"><p>Define a library returning the class. The class can be instantiated with a string naming the library path or nil for the current process. After instantiation the library instance will have strongly typed methods named the same as the keyword keys in fn-defs efficiently bound to symbols of the same exact name in library.</p>
<ul>
<li><code>fn-defs</code> - map of fn-name -> {:rettype :argtypes}.
<ul>
<li><code>argtypes</code> -</li>
<li><code>:void</code> - return type only.</li>
<li><code>:int8</code> <code>:int16</code> <code>:int32</code> <code>:int64</code></li>
<li><code>:float32</code> <code>:float64</code></li>
<li><code>:size-t</code> - int32 or int64 depending on cpu architecture.</li>
<li><code>:pointer</code> <code>:pointer?</code> - Something convertible to a Pointer type. Potentially exception when nil.</li>
<li><code>rettype</code> - any argtype plus :void</li>
</ul>
</li>
</ul>
<p>Options:</p>
<ul>
<li><code>:classname</code> - If classname (a symbol) is provided a .class file is saved to <em>compile-path</em> after which <code>(import classname)</code> will be a validcall meaning that after AOT no further reflection or class generation is required to access the class explicitly. That being said ‘import’ does not reload classes that are already on the classpath so this option is best used after library has stopped changing.</li>
</ul>
<p>Example:</p>
<pre><code class="clojure">user> (dtype-ffi/define-library {:memset {:rettype :pointer
:argtypes [:pointer :int32 :size-t]}}
{:classname 'tech.libmemset})
{:library tech.libmemset}
user> (import 'tech.libmemset)
tech.libmemset
user> (def inst (tech.libmemset. nil)) ;;nil for current process
#'user/inst
user> (def test-buf (dtype/make-container :native-heap :float32 (range 10)))
#'user/test-buf
user> test-buf
#native-buffer@0x00007F4E28CE56D0<float32>[10]
[0.000, 1.000, 2.000, 3.000, 4.000, 5.000, 6.000, 7.000, 8.000, 9.000, ]
user> (.memset ^tech.libmemset inst test-buf 0 40)
#object[tech.v3.datatype.ffi.Pointer 0x33013c57 "{:address 0x00007F4E28CE56D0 }"]
user> test-buf
#native-buffer@0x00007F4E28CE56D0<float32>[10]
[0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, ]
user>
</code></pre></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L342">view source</a></div></div><div class="public anchor" id="var-define-library-functions"><h3>define-library-functions</h3><h4 class="type">macro</h4><div class="usage"><code>(define-library-functions library-def-symbol find-fn check-error)</code></div><div class="doc"><div class="markdown"><p>Define public callable vars that will call library functions marshaling strings back and forth. These vars will call find-fn with the fn-name in a late-bound way and check-error may be provided and called on a return value assuming :check-error? is set in the library definition.</p>
<ul>
<li>library-def-symbol - The fully namespaced symbol that points to the function definitions.</li>
<li>find-fn - A function taking one argument - the fn name, and returning the function.</li>
<li>check-error - A function or macro that receives two arguments - the fn definition from above and the actual un-evaluated function call allowing you to insert pre/post checks.</li>
</ul>
<p>Example:</p>
<pre><code class="clojure">(dt-ffi/define-library-functions avclj.ffi/avcodec-fns find-avcodec-fn check-error)
</code></pre></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L602">view source</a></div></div><div class="public anchor" id="var-define-library-interface"><h3>define-library-interface</h3><h4 class="type">macro</h4><div class="usage"><code>(define-library-interface fn-defs & {:keys [classname check-error symbols libraries header-files], :as opts})</code></div><div class="doc"><div class="markdown"><p>Define public callable vars that will call library functions marshaling strings back and forth. Returns a library singleton.</p>
<ul>
<li><code>fn-defs</code> - map of fn-name -> {:rettype :argtypes}</li>
<li><code>argtypes</code> -
<ul>
<li><code>:void</code> - return type only.</li>
<li><code>:int8</code> <code>:int16</code> <code>:int32</code> <code>:int64</code></li>
<li><code>:float32</code> <code>:float64</code></li>
<li><code>:size-t</code> - int32 or int64 depending on cpu architecture.</li>
<li><code>:pointer</code> <code>:pointer?</code> - Something convertible to a Pointer type. Potentially exception when nil.</li>
</ul>
</li>
<li><code>rettype</code> - any argtype plus :void</li>
<li><code>doc</code> - docstring for the function</li>
<li><code>check-error?</code> apply pre/post checks with <code>check-error</code>. default: false</li>
</ul>
<p>Options:</p>
<ul>
<li>
<p><code>:classname</code> - If classname (a symbol) is provided a .class file is saved to <em>compile-path</em> after which <code>(import classname)</code> will be a validcall meaning that after AOT no further reflection or class generation is required to access the class explicitly. That being said ‘import’ does not reload classes that are already on the classpath so this option is best used after library has stopped changing.</p></li>
<li>
<p><code>:check-error</code> - A function or macro that receives two arguments - the fn definition from above and the actual un-evaluated function call allowing you to insert pre/post checks.</p></li>
<li>
<p><code>:symbols</code> - A sequence of symbols in the shared library that should be available for use with <code>find-symbol</code></p></li>
<li>
<p><code>:libraries</code> - (graalvm only) A sequence of dependent shared libraries that should be loaded.</p></li>
<li>
<p><code>:header-files</code> - (graalvm only) A sequence of header files.</p></li>
</ul>
<p>Example:</p>
<pre><code class="clojure"><br />user> (dt-ffi/define-library-interface
{:memset {:rettype :pointer
:argtypes [['p :pointer]
['x :int32]
['len :size-t]]}})
user> (def test-buf (dtype/make-container :native-heap :float32 (range 10)))
#'user/test-buf
user> test-buf
#native-buffer@0x00007F4E28CE56D0<float32>[10]
[0.000, 1.000, 2.000, 3.000, 4.000, 5.000, 6.000, 7.000, 8.000, 9.000, ]
user> (memset test-buf 0 40)
#object[tech.v3.datatype.ffi.Pointer 0x33013c57 "{:address 0x00007F4E28CE56D0 }"]
user> test-buf
#native-buffer@0x00007F4E28CE56D0<float32>[10]
[0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, ]
</code></pre></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L641">view source</a></div></div><div class="public anchor" id="var-enable-string-.3Ec-stats.21"><h3>enable-string->c-stats!</h3><div class="usage"><code>(enable-string->c-stats! enabled?)</code><code>(enable-string->c-stats!)</code></div><div class="doc"><div class="markdown"><p>Enable tracking how often string->c is getting called.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L179">view source</a></div></div><div class="public anchor" id="var-find-library"><h3>find-library</h3><div class="usage"><code>(find-library libname)</code></div><div class="doc"><div class="markdown"><p>This method is useful to check if loading a library will work. If successful, returns the library name that did finally succeed. This may be different from <code>libname</code> in the case where java.library.path has been used to actively find the library.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L310">view source</a></div></div><div class="public anchor" id="var-find-symbol"><h3>find-symbol</h3><div class="usage"><code>(find-symbol libname symbol-name)</code><code>(find-symbol symbol-name)</code></div><div class="doc"><div class="markdown"><p>Find a symbol in a library. A library symbol is guaranteed to have a conversion to a pointer.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L333">view source</a></div></div><div class="public anchor" id="var-foreign-interface-instance-.3Ec"><h3>foreign-interface-instance->c</h3><div class="usage"><code>(foreign-interface-instance->c ffi-def foreign-inst)</code></div><div class="doc"><div class="markdown"><p>Convert an instance of the above foreign interface definition into a Pointer suitable to be called from C. Callers must ensure that foreign-inst is visible to the gc the entire time the foreign system has a reference to the pointer.</p>
<ul>
<li><code>foreign-inst</code> - an instance of the class defined by ‘define-foreign-interface’.</li>
</ul></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L437">view source</a></div></div><div class="public anchor" id="var-if-class"><h3>if-class</h3><h4 class="type">macro</h4><div class="usage"><code>(if-class class-name then)</code><code>(if-class class-name then else?)</code></div><div class="doc"><div class="markdown"></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L626">view source</a></div></div><div class="public anchor" id="var-instantiate-class"><h3>instantiate-class</h3><div class="usage"><code>(instantiate-class cls & args)</code></div><div class="doc"><div class="markdown"><p>Utility function to instantiate a class via its first constructor in its list of declared constructors. Works with classes returned from ‘define-library’ and ‘define-foreign-interface’. Uses reflection.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L268">view source</a></div></div><div class="public anchor" id="var-instantiate-foreign-interface"><h3>instantiate-foreign-interface</h3><div class="usage"><code>(instantiate-foreign-interface ffi-def ifn)</code></div><div class="doc"><div class="markdown"><p>Instantiate a foreign interface defintion. This returns an instance object which can then be converted into a c-pointer via <code>foreign-interface-instance->c</code>.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L427">view source</a></div></div><div class="public anchor" id="var-instantiate-library"><h3>instantiate-library</h3><div class="usage"><code>(instantiate-library library-def libpath)</code></div><div class="doc"><div class="markdown"><p>Uses reflection to instantiate a library</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L399">view source</a></div></div><div class="public anchor" id="var-jdk-ffi.3F"><h3>jdk-ffi?</h3><div class="usage"><code>(jdk-ffi?)</code></div><div class="doc"><div class="markdown"><p>Is the JDK foreign function interface available?</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L97">view source</a></div></div><div class="public anchor" id="var-jdk-mmodel.3F"><h3>jdk-mmodel?</h3><div class="usage"><code>(jdk-mmodel?)</code></div><div class="doc"><div class="markdown"><p>Is the JDK native memory model available?</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L113">view source</a></div></div><div class="public anchor" id="var-jna-ffi.3F"><h3>jna-ffi?</h3><div class="usage"><code>(jna-ffi?)</code></div><div class="doc"><div class="markdown"><p>Is JNA’s ffi interface available?</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L105">view source</a></div></div><div class="public anchor" id="var-library-loadable.3F"><h3>library-loadable?</h3><div class="usage"><code>(library-loadable? libname)</code></div><div class="doc"><div class="markdown"></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L328">view source</a></div></div><div class="public anchor" id="var-library-singleton"><h3>library-singleton</h3><div class="usage"><code>(library-singleton library-def-var library-sym-var library-def-opts)</code><code>(library-singleton library-def-var)</code></div><div class="doc"><div class="markdown"><p>Create a singleton object, ideally assigned to a variable with defonce, that you can reset to auto-reload the bindings i.e. with new function definitions at the repl.</p>
<ul>
<li><code>library-def-var</code> must be something that deref’s to the latest library definition.</li>
</ul>
<pre><code class="clojure">(defonce ^:private lib (dt-ffi/library-singleton #'avcodec-fns))
;;Safe to call on uninitialized library. If the library is initialized, however,
;;a new library instance is created from the latest avcodec-fns
(dt-ffi/library-singelton-reset! lib)
(defn set-library!
[libpath]
(dt-ffi/library-singelton-set! lib libpath))
(defn- find-avcodec-fn
[fn-kwd]
(dt-ffi/library-singelton-find-fn lib fn-kwd))
</code></pre></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L539">view source</a></div></div><div class="public anchor" id="var-make-ptr"><h3>make-ptr</h3><div class="usage"><code>(make-ptr dtype prim-init-value options)</code><code>(make-ptr dtype prim-init-value)</code></div><div class="doc"><div class="markdown"><p>Make an object convertible to a pointer that points to single value of type <code>dtype</code>.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L447">view source</a></div></div><div class="public anchor" id="var-PLibrarySingleton"><h3>PLibrarySingleton</h3><h4 class="type">protocol</h4><div class="usage"></div><div class="doc"><div class="markdown"><p>Protocol to allow easy definition of a library singleton that manages re-creating the library definition when the library data changes and also recreating the library instance if necessary.</p></div></div><div class="members"><h4>members</h4><div class="inner"><div class="public anchor" id="var-library-singleton-definition"><h3>library-singleton-definition</h3><div class="usage"><code>(library-singleton-definition lib-singleton)</code></div><div class="doc"><div class="markdown"><p>Return the library definition.</p></div></div></div><div class="public anchor" id="var-library-singleton-find-fn"><h3>library-singleton-find-fn</h3><div class="usage"><code>(library-singleton-find-fn lib-singleton fn-name)</code></div><div class="doc"><div class="markdown"><p>Find a bound function in the library. Returns an implementation of clojure.lang.IFn that takes only the specific arguments.</p></div></div></div><div class="public anchor" id="var-library-singleton-find-symbol"><h3>library-singleton-find-symbol</h3><div class="usage"><code>(library-singleton-find-symbol lib-singleton sym-name)</code></div><div class="doc"><div class="markdown"><p>Find a symbol in the library. Returns a pointer.</p></div></div></div><div class="public anchor" id="var-library-singleton-library"><h3>library-singleton-library</h3><div class="usage"><code>(library-singleton-library lib-singleton)</code></div><div class="doc"><div class="markdown"><p>Return the library instance</p></div></div></div><div class="public anchor" id="var-library-singleton-library-path"><h3>library-singleton-library-path</h3><div class="usage"><code>(library-singleton-library-path lib-singleton)</code></div><div class="doc"><div class="markdown"><p>Get the bound library path</p></div></div></div><div class="public anchor" id="var-library-singleton-reset.21"><h3>library-singleton-reset!</h3><div class="usage"><code>(library-singleton-reset! lib-singleton)</code></div><div class="doc"><div class="markdown"><p>Regenerate library bindings and bind a new instance to allow repl-style iterative development.</p></div></div></div><div class="public anchor" id="var-library-singleton-set.21"><h3>library-singleton-set!</h3><div class="usage"><code>(library-singleton-set! lib-singleton libpath)</code></div><div class="doc"><div class="markdown"><p>Set the library path, generate the library and create a new instance.</p></div></div></div><div class="public anchor" id="var-library-singleton-set-instance.21"><h3>library-singleton-set-instance!</h3><div class="usage"><code>(library-singleton-set-instance! lib-singleton libinst)</code></div><div class="doc"><div class="markdown"><p>In some cases such as graal native pathways you have to hard-set the definition and instance.</p></div></div></div></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L471">view source</a></div></div><div class="public anchor" id="var-PToPointer"><h3>PToPointer</h3><h4 class="type">protocol</h4><div class="usage"></div><div class="doc"><div class="markdown"></div></div><div class="members"><h4>members</h4><div class="inner"><div class="public anchor" id="var--.3Epointer"><h3>->pointer</h3><div class="usage"><code>(->pointer item)</code></div><div class="doc"><div class="markdown"><p>Convert an item into a Pointer, throwing an exception upon failure.</p></div></div></div><div class="public anchor" id="var-convertible-to-pointer.3F"><h3>convertible-to-pointer?</h3><div class="usage"><code>(convertible-to-pointer? item)</code></div><div class="doc"><div class="markdown"><p>Query whether an item is convertible to a pointer.</p></div></div></div></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L121">view source</a></div></div><div class="public anchor" id="var-ptr-.3Estruct"><h3>ptr->struct</h3><div class="usage"><code>(ptr->struct struct-type ptr)</code></div><div class="doc"><div class="markdown"><p>Given a struct type and a pointer return a struct whose data starts at the address of the pointer.</p>
<pre><code class="clojure"> (let [codec (dt-ffi/ptr->struct (:datatype-name @av-context/codec-def*) codec-ptr)]
...)
</code></pre></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L772">view source</a></div></div><div class="public anchor" id="var-set-ffi-impl.21"><h3>set-ffi-impl!</h3><div class="usage"><code>(set-ffi-impl! ffi-kwd)</code></div><div class="doc"><div class="markdown"><p>Set the global ffi implementation. Options are * :jdk - namespace <code>'tech.v3.datatype.ffi.mmodel/ffi-fns</code> - only available with jdk’s foreign function module enabled. * :jna - namespace <code>''tech.v3.datatype.ffi.jna/ffi-fns</code> - available if JNA version 5+ is in the classpath.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L281">view source</a></div></div><div class="public anchor" id="var-string-.3Ec"><h3>string->c</h3><div class="usage"><code>(string->c str-data & [{:keys [encoding], :as options}])</code></div><div class="doc"><div class="markdown"><p>Convert a java String to a zero-padded c-string. Available encodings are</p>
<ul>
<li><code>:ascii</code>, <code>:utf-8</code> (default), <code>:utf-16</code>, <code>:utf-16LE</code>, <code>utf-16-BE</code> and <code>:utf-32</code></li>
</ul>
<p>String data will be zero padded.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L201">view source</a></div></div><div class="public anchor" id="var-string-.3Ec-data-histogram"><h3>string->c-data-histogram</h3><div class="usage"><code>(string->c-data-histogram)</code></div><div class="doc"><div class="markdown"></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L193">view source</a></div></div><div class="public anchor" id="var-struct-member-ptr"><h3>struct-member-ptr</h3><div class="usage"><code>(struct-member-ptr data-struct member)</code></div><div class="doc"><div class="markdown"><p>Get a pointer to a struct data member.</p>
<pre><code class="clojure"> (swscale/sws_scale sws-ctx
(struct-member-ptr input-frame :data)
(struct-member-ptr input-frame :linesize)
0 (:height input-frame)
(struct-member-ptr encoder-frame :data)
(struct-member-ptr encoder-frame :linesize))
</code></pre></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L789">view source</a></div></div><div class="public anchor" id="var-utf16-platform-encoding"><h3>utf16-platform-encoding</h3><div class="usage"><code>(utf16-platform-encoding)</code></div><div class="doc"><div class="markdown"><p>Get the utf-16 encoding that matches the current platform so you can encode a utf-16 string without a bom.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L128">view source</a></div></div><div class="public anchor" id="var-utf32-platform-encoding"><h3>utf32-platform-encoding</h3><div class="usage"><code>(utf32-platform-encoding)</code></div><div class="doc"><div class="markdown"><p>Get the utf-16 encoding that matches the current platform so you can encode a utf-16 string without a bom.</p></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L137">view source</a></div></div><div class="public anchor" id="var-windows-platform.3F"><h3>windows-platform?</h3><div class="usage"><code>(windows-platform?)</code></div><div class="doc"><div class="markdown"></div></div><div class="src-link"><a href="https://github.com/cnuernber/dtype-next/blob/master/src/tech/v3/datatype/ffi.clj#L146">view source</a></div></div></div></body></html>