Merge branch 'release/v0.4.341'
This commit is contained in:
commit
cd7761b595
10 changed files with 645 additions and 130 deletions
14
CHANGELOG.md
14
CHANGELOG.md
|
|
@ -1,6 +1,18 @@
|
|||
# Change Log
|
||||
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
|
||||
|
||||
## [0.4.341] - 2022-01-23
|
||||
### Added
|
||||
- Constants for size and alignment of primitive types
|
||||
- Support for non-native byte orders of primitive types
|
||||
- Functions for reading and writing primitive types (e.g. `coffi.mem/read-float`, `coffi.mem/write-long`, etc.)
|
||||
- Layout objects may now be passed to `coffi.mem/size-of` and `coffi.mem/align-of`
|
||||
- Constants for native-order primitive layouts
|
||||
- Constants for byte orders
|
||||
|
||||
### Changed
|
||||
- The `coffi.mem/primitive?` predicate is now actually a function instead of a set
|
||||
|
||||
## [0.3.298] - 2022-01-10
|
||||
### Added
|
||||
- New `coffi.layout` namespace with support for forcing C layout rules on structs
|
||||
|
|
@ -12,7 +24,6 @@ All notable changes to this project will be documented in this file. This change
|
|||
### Fixed
|
||||
- Non-primitive arguments on upcalls would generate invalid bytecode with `nil` instructions
|
||||
|
||||
|
||||
## [0.2.259] - 2021-10-16
|
||||
### Fixed
|
||||
- Long and double arguments to upcalls failed to compile in some cases
|
||||
|
|
@ -80,6 +91,7 @@ All notable changes to this project will be documented in this file. This change
|
|||
- Support for serializing and deserializing arbitrary Clojure functions
|
||||
- Support for serializing and deserializing arbitrary Clojure data structures
|
||||
|
||||
[0.4.341]: https://github.com/IGJoshua/coffi/compare/v0.3.298...v0.4.341
|
||||
[0.3.298]: https://github.com/IGJoshua/coffi/compare/v0.2.277...v0.3.298
|
||||
[0.2.277]: https://github.com/IGJoshua/coffi/compare/v0.2.259...v0.2.277
|
||||
[0.2.259]: https://github.com/IGJoshua/coffi/compare/v0.1.251...v0.2.259
|
||||
|
|
|
|||
72
README.md
72
README.md
|
|
@ -16,8 +16,8 @@ This library is available on Clojars. Add one of the following entries to the
|
|||
`:deps` key of your `deps.edn`:
|
||||
|
||||
```clojure
|
||||
org.suskalo/coffi {:mvn/version "0.3.298"}
|
||||
io.github.IGJoshua/coffi {:git/tag "v0.3.298" :git/sha "1bbb8a7"}
|
||||
org.suskalo/coffi {:mvn/version "0.4.341"}
|
||||
io.github.IGJoshua/coffi {:git/tag "v0.4.341" :git/sha "09b8195"}
|
||||
```
|
||||
|
||||
If you use this library as a git dependency, you will need to prepare the
|
||||
|
|
@ -534,12 +534,14 @@ to the user to call `deserialize-from` on that segment with the appropriate
|
|||
type.
|
||||
|
||||
### Unwrapped Native Handles
|
||||
Sometimes the overhead brought by the automatic serialization and
|
||||
deserialization from the methods explained so far is too much. In cases like
|
||||
these, unwrapped native handles are desirable.
|
||||
Some native libraries work with handles to large amounts of data at once, making
|
||||
it undesirable to marshal data back and forth from Clojure, both because it's
|
||||
not necessary to work with the data in Clojure directly, or also because of the
|
||||
high (de)serialization costs associated with marshaling. In cases like these,
|
||||
unwrapped native handles are desirable.
|
||||
|
||||
The functions `make-downcall` and `make-varargs-factory` are provided to create
|
||||
these raw handles.
|
||||
The functions `make-downcall` and `make-varargs-factory` are also provided to
|
||||
create raw function handles.
|
||||
|
||||
```clojure
|
||||
(def raw-strlen (ffi/make-downcall "strlen" [::mem/c-string] ::mem/long))
|
||||
|
|
@ -547,7 +549,7 @@ these raw handles.
|
|||
;; => 5
|
||||
```
|
||||
|
||||
In these cases, the argument types are expected to exactly match the types
|
||||
With raw handles, the argument types are expected to exactly match the types
|
||||
expected by the native function. For primitive types, those are primitives. For
|
||||
addresses, that is `MemoryAddress`, and for composite types like structs and
|
||||
unions, that is `MemorySegment`. Both `MemoryAddress` and `MemorySegment` come
|
||||
|
|
@ -569,6 +571,58 @@ Clojure functions serialized to this type will have their arguments and return
|
|||
value exactly match the types specified and will not perform any serialization
|
||||
or deserialization at their boundaries.
|
||||
|
||||
One important caveat to consider when writing wrappers for performance-sensitive
|
||||
functions is that the convenience macro `defcfn` that coffi provides will
|
||||
already perform no serialization or deserialization on primitive arguments and
|
||||
return types, so for functions with only primitive argument and return types
|
||||
there is no performance reason to choose unwrapped native handles over the
|
||||
convenience macro.
|
||||
|
||||
### Manual (De)Serialization
|
||||
Coffi uses multimethods to dispatch to (de)serialization functions to enable
|
||||
code that's generic over the types it operates on. However, in cases where you
|
||||
know the exact types that you will be (de)serializing and the multimethod
|
||||
dispatch overhead is too high a cost, it may be appropriate to manually handle
|
||||
(de)serializing data. This will often be done paired with [Unwrapped Native
|
||||
Handles](#unwrapped-native-handles).
|
||||
|
||||
Convenience functions are provided to both read and write all primitive types
|
||||
and addresses, including byte order.
|
||||
|
||||
As an example, when wrapping a function that returns an array of big-endian
|
||||
floats, the following code might be used.
|
||||
|
||||
``` clojure
|
||||
(def ^:private returns-float-array* (ffi/make-downcall "returns_float_array" [::mem/pointer] ::mem/int))
|
||||
(def ^:private release-floats* (ffi/make-downcall "releases_float_array" [::mem/pointer] ::mem/void))
|
||||
|
||||
(defn returns-float-array
|
||||
[]
|
||||
(with-open [scope (mem/stack-scope)]
|
||||
(let [out-floats (mem/alloc mem/pointer-size scope)
|
||||
num-floats (function-handle (mem/address-of out-floats))
|
||||
floats-addr (mem/read-address out-floats)
|
||||
floats-slice (mem/slice-global floats-addr (unchecked-multiply-int mem/float-size num-floats))]
|
||||
;; Using a try/finally to perform an operation when the stack frame exits,
|
||||
;; but not to try to catch anything.
|
||||
(try
|
||||
(loop [floats (transient [])
|
||||
index 0]
|
||||
(if (>= index num-floats)
|
||||
(persistent! floats)
|
||||
(recur (conj! floats (mem/read-float floats-slice
|
||||
(unchecked-multiply-int index mem/float-size)
|
||||
mem/big-endian))
|
||||
(unchecked-inc-int index))))
|
||||
(finally
|
||||
(release-floats floats-addr))))))
|
||||
```
|
||||
|
||||
The above code manually performs all memory operations rather than relying on
|
||||
coffi's dispatch. This will be more performant, but because multimethod overhead
|
||||
is usually relatively low, it's recommended to use the multimethod variants for
|
||||
convenience in colder functions.
|
||||
|
||||
### Data Model
|
||||
In addition to the macros and functions provided to build a Clojure API for
|
||||
native libraries, facilities are provided for taking data and loading all the
|
||||
|
|
@ -1054,6 +1108,6 @@ September 2023.
|
|||
|
||||
## License
|
||||
|
||||
Copyright © 2021 Joshua Suskalo
|
||||
Copyright © 2022 Joshua Suskalo
|
||||
|
||||
Distributed under the Eclipse Public License version 1.0.
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
[clojure.tools.build.api :as b]))
|
||||
|
||||
(def lib-coord 'org.suskalo/coffi)
|
||||
(def version (format "0.3.%s" (b/git-count-revs nil)))
|
||||
(def version (format "0.4.%s" (b/git-count-revs nil)))
|
||||
|
||||
(def resource-dirs ["resources/"])
|
||||
|
||||
|
|
|
|||
2
deps.edn
2
deps.edn
|
|
@ -24,7 +24,7 @@
|
|||
:codox {:extra-deps {codox/codox {:mvn/version "0.10.7"}}
|
||||
:exec-fn codox.main/generate-docs
|
||||
:exec-args {:name "coffi"
|
||||
:version "v0.3.298"
|
||||
:version "v0.4.341"
|
||||
:description "A Foreign Function Interface in Clojure for JDK 17."
|
||||
:source-paths ["src/clj"]
|
||||
:output-path "docs"
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
<!DOCTYPE html PUBLIC ""
|
||||
"">
|
||||
<html><head><meta charset="UTF-8" /><title>coffi.ffi documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/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></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">coffi</span> <span class="project-version">v0.3.298</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">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>coffi</span></div></div></li><li class="depth-2 branch current"><a href="coffi.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-2 branch"><a href="coffi.layout.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>layout</span></div></a></li><li class="depth-2"><a href="coffi.mem.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mem</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="coffi.ffi.html#var-cfn"><div class="inner"><span>cfn</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-const"><div class="inner"><span>const</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-defcfn"><div class="inner"><span>defcfn</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-find-symbol"><div class="inner"><span>find-symbol</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-freset.21"><div class="inner"><span>freset!</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-fswap.21"><div class="inner"><span>fswap!</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-load-library"><div class="inner"><span>load-library</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-load-system-library"><div class="inner"><span>load-system-library</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-downcall"><div class="inner"><span>make-downcall</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-serde-varargs-wrapper"><div class="inner"><span>make-serde-varargs-wrapper</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-serde-wrapper"><div class="inner"><span>make-serde-wrapper</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-varargs-factory"><div class="inner"><span>make-varargs-factory</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-reify-libspec"><div class="inner"><span>reify-libspec</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-reify-symbolspec"><div class="inner"><span>reify-symbolspec</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-static-variable"><div class="inner"><span>static-variable</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-vacfn-factory"><div class="inner"><span>vacfn-factory</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">coffi.ffi</h1><div class="doc"><div class="markdown"><p>Functions for creating handles to native functions and loading native libraries.</p></div></div><div class="public anchor" id="var-cfn"><h3>cfn</h3><div class="usage"><code>(cfn symbol args ret)</code></div><div class="doc"><div class="markdown"><p>Constructs a Clojure function to call the native function referenced by <code>symbol</code>.</p>
|
||||
<html><head><meta charset="UTF-8" /><title>coffi.ffi documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/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></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">coffi</span> <span class="project-version">v0.4.341</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">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>coffi</span></div></div></li><li class="depth-2 branch current"><a href="coffi.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-2 branch"><a href="coffi.layout.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>layout</span></div></a></li><li class="depth-2"><a href="coffi.mem.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mem</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="coffi.ffi.html#var-cfn"><div class="inner"><span>cfn</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-const"><div class="inner"><span>const</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-defcfn"><div class="inner"><span>defcfn</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-find-symbol"><div class="inner"><span>find-symbol</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-freset.21"><div class="inner"><span>freset!</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-fswap.21"><div class="inner"><span>fswap!</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-load-library"><div class="inner"><span>load-library</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-load-system-library"><div class="inner"><span>load-system-library</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-downcall"><div class="inner"><span>make-downcall</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-serde-varargs-wrapper"><div class="inner"><span>make-serde-varargs-wrapper</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-serde-wrapper"><div class="inner"><span>make-serde-wrapper</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-make-varargs-factory"><div class="inner"><span>make-varargs-factory</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-reify-libspec"><div class="inner"><span>reify-libspec</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-reify-symbolspec"><div class="inner"><span>reify-symbolspec</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-static-variable"><div class="inner"><span>static-variable</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-vacfn-factory"><div class="inner"><span>vacfn-factory</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">coffi.ffi</h1><div class="doc"><div class="markdown"><p>Functions for creating handles to native functions and loading native libraries.</p></div></div><div class="public anchor" id="var-cfn"><h3>cfn</h3><div class="usage"><code>(cfn symbol args ret)</code></div><div class="doc"><div class="markdown"><p>Constructs a Clojure function to call the native function referenced by <code>symbol</code>.</p>
|
||||
<p>The function returned will serialize any passed arguments into the <code>args</code> types, and deserialize the return to the <code>ret</code> type.</p>
|
||||
<p>If your <code>args</code> and <code>ret</code> are constants, then it is more efficient to call <a href="coffi.ffi.html#var-make-downcall">make-downcall</a> followed by <a href="coffi.ffi.html#var-make-serde-wrapper">make-serde-wrapper</a> because the latter has an inline definition which will result in less overhead from serdes.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L420">view source</a></div></div><div class="public anchor" id="var-const"><h3>const</h3><div class="usage"><code>(const symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Gets the value of a constant stored in <code>symbol-or-addr</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L555">view source</a></div></div><div class="public anchor" id="var-defcfn"><h3>defcfn</h3><h4 class="type">macro</h4><div class="usage"><code>(defcfn name docstring? attr-map? symbol arg-types ret-type)</code><code>(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)</code></div><div class="doc"><div class="markdown"><p>Defines a Clojure function which maps to a native function.</p>
|
||||
<p>If your <code>args</code> and <code>ret</code> are constants, then it is more efficient to call <a href="coffi.ffi.html#var-make-downcall">make-downcall</a> followed by <a href="coffi.ffi.html#var-make-serde-wrapper">make-serde-wrapper</a> because the latter has an inline definition which will result in less overhead from serdes.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L421">view source</a></div></div><div class="public anchor" id="var-const"><h3>const</h3><div class="usage"><code>(const symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Gets the value of a constant stored in <code>symbol-or-addr</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L556">view source</a></div></div><div class="public anchor" id="var-defcfn"><h3>defcfn</h3><h4 class="type">macro</h4><div class="usage"><code>(defcfn name docstring? attr-map? symbol arg-types ret-type)</code><code>(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)</code></div><div class="doc"><div class="markdown"><p>Defines a Clojure function which maps to a native function.</p>
|
||||
<p><code>name</code> is the symbol naming the resulting var. <code>symbol</code> is a symbol or string naming the library symbol to link against. <code>arg-types</code> is a vector of qualified keywords representing the argument types. <code>ret-type</code> is a single qualified keyword representing the return type. <code>fn-tail</code> is the body of the function (potentially with multiple arities) which wraps the native one. Inside the function, <code>native-fn</code> is bound to a function that will serialize its arguments, call the native function, and deserialize its return type. If any body is present, you must call this function in order to call the native code.</p>
|
||||
<p>If no <code>fn-tail</code> is provided, then the resulting function will simply serialize the arguments according to <code>arg-types</code>, call the native function, and deserialize the return value.</p>
|
||||
<p>The number of args in the <code>fn-tail</code> need not match the number of <code>arg-types</code> for the native function. It need only call the native wrapper function with the correct arguments.</p>
|
||||
<p>See <a href="coffi.mem.html#var-serialize">serialize</a>, <a href="coffi.mem.html#var-deserialize">deserialize</a>, <a href="coffi.ffi.html#var-make-downcall">make-downcall</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L691">view source</a></div></div><div class="public anchor" id="var-find-symbol"><h3>find-symbol</h3><div class="usage"><code>(find-symbol sym)</code></div><div class="doc"><div class="markdown"><p>Gets the <a href="null">MemoryAddress</a> of a symbol from the loaded libraries.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L35">view source</a></div></div><div class="public anchor" id="var-freset.21"><h3>freset!</h3><div class="usage"><code>(freset! static-var newval)</code></div><div class="doc"><div class="markdown"><p>Sets the value of <code>static-var</code> to <code>newval</code>, running it through <a href="coffi.mem.html#var-serialize">serialize</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L580">view source</a></div></div><div class="public anchor" id="var-fswap.21"><h3>fswap!</h3><div class="usage"><code>(fswap! static-var f & args)</code></div><div class="doc"><div class="markdown"><p>Non-atomically runs the function <code>f</code> over the value stored in <code>static-var</code>.</p>
|
||||
<p>The value is deserialized before passing it to <code>f</code>, and serialized before putting the value into <code>static-var</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L589">view source</a></div></div><div class="public anchor" id="var-load-library"><h3>load-library</h3><div class="usage"><code>(load-library path)</code></div><div class="doc"><div class="markdown"><p>Loads the library at <code>path</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L30">view source</a></div></div><div class="public anchor" id="var-load-system-library"><h3>load-system-library</h3><div class="usage"><code>(load-system-library libname)</code></div><div class="doc"><div class="markdown"><p>Loads the library named <code>libname</code> from the system’s load path.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L25">view source</a></div></div><div class="public anchor" id="var-make-downcall"><h3>make-downcall</h3><div class="usage"><code>(make-downcall symbol-or-addr args ret)</code></div><div class="doc"><div class="markdown"><p>Constructs a downcall function reference to <code>symbol-or-addr</code> with the given <code>args</code> and <code>ret</code> types.</p>
|
||||
<p>See <a href="coffi.mem.html#var-serialize">serialize</a>, <a href="coffi.mem.html#var-deserialize">deserialize</a>, <a href="coffi.ffi.html#var-make-downcall">make-downcall</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L692">view source</a></div></div><div class="public anchor" id="var-find-symbol"><h3>find-symbol</h3><div class="usage"><code>(find-symbol sym)</code></div><div class="doc"><div class="markdown"><p>Gets the <a href="null">MemoryAddress</a> of a symbol from the loaded libraries.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L35">view source</a></div></div><div class="public anchor" id="var-freset.21"><h3>freset!</h3><div class="usage"><code>(freset! static-var newval)</code></div><div class="doc"><div class="markdown"><p>Sets the value of <code>static-var</code> to <code>newval</code>, running it through <a href="coffi.mem.html#var-serialize">serialize</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L581">view source</a></div></div><div class="public anchor" id="var-fswap.21"><h3>fswap!</h3><div class="usage"><code>(fswap! static-var f & args)</code></div><div class="doc"><div class="markdown"><p>Non-atomically runs the function <code>f</code> over the value stored in <code>static-var</code>.</p>
|
||||
<p>The value is deserialized before passing it to <code>f</code>, and serialized before putting the value into <code>static-var</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L590">view source</a></div></div><div class="public anchor" id="var-load-library"><h3>load-library</h3><div class="usage"><code>(load-library path)</code></div><div class="doc"><div class="markdown"><p>Loads the library at <code>path</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L30">view source</a></div></div><div class="public anchor" id="var-load-system-library"><h3>load-system-library</h3><div class="usage"><code>(load-system-library libname)</code></div><div class="doc"><div class="markdown"><p>Loads the library named <code>libname</code> from the system’s load path.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L25">view source</a></div></div><div class="public anchor" id="var-make-downcall"><h3>make-downcall</h3><div class="usage"><code>(make-downcall symbol-or-addr args ret)</code></div><div class="doc"><div class="markdown"><p>Constructs a downcall function reference to <code>symbol-or-addr</code> with the given <code>args</code> and <code>ret</code> types.</p>
|
||||
<p>The function returned takes only arguments whose types match exactly the <a href="coffi.mem.html#var-java-layout">java-layout</a> for that type, and returns an argument with exactly the <a href="coffi.mem.html#var-java-layout">java-layout</a> of the <code>ret</code> type. This function will perform no serialization or deserialization of arguments or the return type.</p>
|
||||
<p>If the <code>ret</code> type is non-primitive, then the returned function will take a first argument of a <a href="null">SegmentAllocator</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L196">view source</a></div></div><div class="public anchor" id="var-make-serde-varargs-wrapper"><h3>make-serde-varargs-wrapper</h3><div class="usage"><code>(make-serde-varargs-wrapper varargs-factory required-args ret-type)</code></div><div class="doc"><div class="markdown"><p>Constructs a wrapper function for the <code>varargs-factory</code> which produces functions that serialize the arguments and deserialize the return value.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L408">view source</a></div></div><div class="public anchor" id="var-make-serde-wrapper"><h3>make-serde-wrapper</h3><div class="usage"><code>(make-serde-wrapper downcall arg-types ret-type)</code></div><div class="doc"><div class="markdown"><p>Constructs a wrapper function for the <code>downcall</code> which serializes the arguments and deserializes the return value.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L389">view source</a></div></div><div class="public anchor" id="var-make-varargs-factory"><h3>make-varargs-factory</h3><div class="usage"><code>(make-varargs-factory symbol required-args ret)</code></div><div class="doc"><div class="markdown"><p>Returns a function for constructing downcalls with additional types for arguments.</p>
|
||||
<p>If the <code>ret</code> type is non-primitive, then the returned function will take a first argument of a <a href="null">SegmentAllocator</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L196">view source</a></div></div><div class="public anchor" id="var-make-serde-varargs-wrapper"><h3>make-serde-varargs-wrapper</h3><div class="usage"><code>(make-serde-varargs-wrapper varargs-factory required-args ret-type)</code></div><div class="doc"><div class="markdown"><p>Constructs a wrapper function for the <code>varargs-factory</code> which produces functions that serialize the arguments and deserialize the return value.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L409">view source</a></div></div><div class="public anchor" id="var-make-serde-wrapper"><h3>make-serde-wrapper</h3><div class="usage"><code>(make-serde-wrapper downcall arg-types ret-type)</code></div><div class="doc"><div class="markdown"><p>Constructs a wrapper function for the <code>downcall</code> which serializes the arguments and deserializes the return value.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L390">view source</a></div></div><div class="public anchor" id="var-make-varargs-factory"><h3>make-varargs-factory</h3><div class="usage"><code>(make-varargs-factory symbol required-args ret)</code></div><div class="doc"><div class="markdown"><p>Returns a function for constructing downcalls with additional types for arguments.</p>
|
||||
<p>The <code>required-args</code> are the types of the first arguments passed to the downcall handle, and the values passed to the returned function are only the varargs types.</p>
|
||||
<p>The returned function is memoized, so that only one downcall function will be generated per combination of argument types.</p>
|
||||
<p>See <a href="coffi.ffi.html#var-make-downcall">make-downcall</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L214">view source</a></div></div><div class="public anchor" id="var-reify-libspec"><h3>reify-libspec</h3><div class="usage"><code>(reify-libspec libspec)</code></div><div class="doc"><div class="markdown"><p>Loads all the symbols specified in the <code>libspec</code>.</p>
|
||||
<p>The value of each key of the passed map is transformed as by <a href="coffi.ffi.html#var-reify-symbolspec">reify-symbolspec</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L653">view source</a></div></div><div class="public anchor" id="var-reify-symbolspec"><h3>reify-symbolspec</h3><h4 class="type">multimethod</h4><div class="usage"></div><div class="doc"><div class="markdown"><p>Takes a spec for a symbol reference and returns a live value for that type.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L612">view source</a></div></div><div class="public anchor" id="var-static-variable"><h3>static-variable</h3><div class="usage"><code>(static-variable symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Constructs a reference to a mutable value stored in <code>symbol-or-addr</code>.</p>
|
||||
<p>See <a href="coffi.ffi.html#var-make-downcall">make-downcall</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L214">view source</a></div></div><div class="public anchor" id="var-reify-libspec"><h3>reify-libspec</h3><div class="usage"><code>(reify-libspec libspec)</code></div><div class="doc"><div class="markdown"><p>Loads all the symbols specified in the <code>libspec</code>.</p>
|
||||
<p>The value of each key of the passed map is transformed as by <a href="coffi.ffi.html#var-reify-symbolspec">reify-symbolspec</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L654">view source</a></div></div><div class="public anchor" id="var-reify-symbolspec"><h3>reify-symbolspec</h3><h4 class="type">multimethod</h4><div class="usage"></div><div class="doc"><div class="markdown"><p>Takes a spec for a symbol reference and returns a live value for that type.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L613">view source</a></div></div><div class="public anchor" id="var-static-variable"><h3>static-variable</h3><div class="usage"><code>(static-variable symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Constructs a reference to a mutable value stored in <code>symbol-or-addr</code>.</p>
|
||||
<p>The returned value can be dereferenced, and has metadata, and the address of the value can be queried with <a href="coffi.mem.html#var-address-of">address-of</a>.</p>
|
||||
<p>See <a href="coffi.ffi.html#var-freset.21">freset!</a>, <a href="coffi.ffi.html#var-fswap.21">fswap!</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L597">view source</a></div></div><div class="public anchor" id="var-vacfn-factory"><h3>vacfn-factory</h3><div class="usage"><code>(vacfn-factory symbol required-args ret)</code></div><div class="doc"><div class="markdown"><p>Constructs a varargs factory to call the native function referenced by <code>symbol</code>.</p>
|
||||
<p>The function returned takes any number of type arguments and returns a specialized Clojure function for calling the native function with those arguments.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/ffi.clj#L434">view source</a></div></div></div></body></html>
|
||||
<p>See <a href="coffi.ffi.html#var-freset.21">freset!</a>, <a href="coffi.ffi.html#var-fswap.21">fswap!</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L598">view source</a></div></div><div class="public anchor" id="var-vacfn-factory"><h3>vacfn-factory</h3><div class="usage"><code>(vacfn-factory symbol required-args ret)</code></div><div class="doc"><div class="markdown"><p>Constructs a varargs factory to call the native function referenced by <code>symbol</code>.</p>
|
||||
<p>The function returned takes any number of type arguments and returns a specialized Clojure function for calling the native function with those arguments.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/ffi.clj#L435">view source</a></div></div></div></body></html>
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<!DOCTYPE html PUBLIC ""
|
||||
"">
|
||||
<html><head><meta charset="UTF-8" /><title>coffi.layout documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/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></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">coffi</span> <span class="project-version">v0.3.298</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">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>coffi</span></div></div></li><li class="depth-2 branch"><a href="coffi.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-2 branch current"><a href="coffi.layout.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>layout</span></div></a></li><li class="depth-2"><a href="coffi.mem.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mem</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="coffi.layout.html#var-with-c-layout"><div class="inner"><span>with-c-layout</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">coffi.layout</h1><div class="doc"><div class="markdown"><p>Functions for adjusting the layout of structs.</p></div></div><div class="public anchor" id="var-with-c-layout"><h3>with-c-layout</h3><div class="usage"><code>(with-c-layout struct-spec)</code></div><div class="doc"><div class="markdown"><p>Forces a struct specification to C layout rules.</p>
|
||||
<p>This will add padding fields between fields to match C alignment requirements.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/fecee74d727b9f7e4ac111555e4d16c94aea0c3c/src/clj/coffi/layout.clj#L6">view source</a></div></div></div></body></html>
|
||||
<html><head><meta charset="UTF-8" /><title>coffi.layout documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/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></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">coffi</span> <span class="project-version">v0.4.341</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">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>coffi</span></div></div></li><li class="depth-2 branch"><a href="coffi.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-2 branch current"><a href="coffi.layout.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>layout</span></div></a></li><li class="depth-2"><a href="coffi.mem.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mem</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="coffi.layout.html#var-with-c-layout"><div class="inner"><span>with-c-layout</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">coffi.layout</h1><div class="doc"><div class="markdown"><p>Functions for adjusting the layout of structs.</p></div></div><div class="public anchor" id="var-with-c-layout"><h3>with-c-layout</h3><div class="usage"><code>(with-c-layout struct-spec)</code></div><div class="doc"><div class="markdown"><p>Forces a struct specification to C layout rules.</p>
|
||||
<p>This will add padding fields between fields to match C alignment requirements.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/4aa37e72d975ef7f39b0f3004abc9ee5805e0147/src/clj/coffi/layout.clj#L6">view source</a></div></div></div></body></html>
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -288,8 +288,8 @@
|
|||
`(mem/serialize ~sym ~type-sym ~scope)
|
||||
|
||||
(and (mem/primitive? type)
|
||||
(not (#{::mem/pointer} type)))
|
||||
(list (primitive-cast-sym type) sym)
|
||||
(not (#{::mem/pointer} (mem/primitive-type type))))
|
||||
(list (primitive-cast-sym (mem/primitive-type type)) sym)
|
||||
|
||||
(#{::mem/pointer} type)
|
||||
nil
|
||||
|
|
@ -347,8 +347,9 @@
|
|||
`(mem/deserialize-from ~expr ~ret-type-sym))
|
||||
deserialize-ret (fn [expr]
|
||||
(cond
|
||||
(or (mem/primitive? ret-type)
|
||||
(#{::mem/void} ret-type))
|
||||
(and (or (mem/primitive? ret-type)
|
||||
(#{::mem/void} ret-type))
|
||||
(not (#{::mem/pointer} (mem/primitive-type ret-type))))
|
||||
expr
|
||||
|
||||
(mem/primitive-type ret-type)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
(:require
|
||||
[clojure.spec.alpha :as s])
|
||||
(:import
|
||||
(java.nio ByteOrder)
|
||||
(jdk.incubator.foreign
|
||||
Addressable
|
||||
CLinker
|
||||
|
|
@ -30,7 +31,8 @@
|
|||
MemorySegment
|
||||
ResourceScope
|
||||
ResourceScope$Handle
|
||||
SegmentAllocator)))
|
||||
SegmentAllocator
|
||||
ValueLayout)))
|
||||
|
||||
(defn stack-scope
|
||||
"Constructs a new scope for use only in this thread.
|
||||
|
|
@ -87,14 +89,14 @@
|
|||
"Allocates `size` bytes.
|
||||
|
||||
If a `scope` is provided, the allocation will be reclaimed when it is closed."
|
||||
([size] (alloc size (connected-scope)))
|
||||
([size scope] (MemorySegment/allocateNative (long size) ^ResourceScope scope)))
|
||||
(^MemorySegment [size] (alloc size (connected-scope)))
|
||||
(^MemorySegment [size scope] (MemorySegment/allocateNative (long size) ^ResourceScope scope)))
|
||||
|
||||
(defn alloc-with
|
||||
"Allocates `size` bytes using the `allocator`."
|
||||
([allocator size]
|
||||
(^MemorySegment [allocator size]
|
||||
(.allocate ^SegmentAllocator allocator (long size)))
|
||||
([allocator size alignment]
|
||||
(^MemorySegment [allocator size alignment]
|
||||
(.allocate ^SegmentAllocator allocator (long size) (long alignment))))
|
||||
|
||||
(defmacro with-acquired
|
||||
|
|
@ -122,7 +124,7 @@
|
|||
"Gets the address of a given segment.
|
||||
|
||||
This value can be used as an argument to functions which take a pointer."
|
||||
[addressable]
|
||||
^MemoryAddress [addressable]
|
||||
(.address ^Addressable addressable))
|
||||
|
||||
(defn null?
|
||||
|
|
@ -143,27 +145,27 @@
|
|||
Because this fetches from the global segment, it has no associated scope, and
|
||||
therefore the reference created here cannot prevent the value from being
|
||||
freed. Be careful to ensure that you are not retaining an object incorrectly."
|
||||
[address size]
|
||||
^MemorySegment [address size]
|
||||
(.asSlice (MemorySegment/globalNativeSegment)
|
||||
^MemoryAddress address (long size)))
|
||||
|
||||
(defn slice
|
||||
"Get a slice over the `segment` with the given `offset`."
|
||||
([segment offset]
|
||||
(^MemorySegment [segment offset]
|
||||
(.asSlice ^MemorySegment segment (long offset)))
|
||||
([segment offset size]
|
||||
(^MemorySegment [segment offset size]
|
||||
(.asSlice ^MemorySegment segment (long offset) (long size))))
|
||||
|
||||
(defn slice-into
|
||||
"Get a slice into the `segment` starting at the `address`."
|
||||
([address segment]
|
||||
(^MemorySegment [address segment]
|
||||
(.asSlice ^MemorySegment segment ^MemoryAddress address))
|
||||
([address segment size]
|
||||
(^MemorySegment [address segment size]
|
||||
(.asSlice ^MemorySegment segment ^MemoryAddress address (long size))))
|
||||
|
||||
(defn with-offset
|
||||
"Get a new address `offset` from the old `address`."
|
||||
[address offset]
|
||||
^MemoryAddress [address offset]
|
||||
(.addOffset ^MemoryAddress address (long offset)))
|
||||
|
||||
(defn as-segment
|
||||
|
|
@ -174,49 +176,441 @@
|
|||
cleanup in a way that doesn't require modifying the code at the point of
|
||||
freeing, and allows shared or garbage collected resources to be freed
|
||||
correctly."
|
||||
([address size scope]
|
||||
(.asSegment ^MemoryAddress address size scope))
|
||||
([address size scope cleanup]
|
||||
(.asSegment ^MemoryAddress address size cleanup scope)))
|
||||
(^MemorySegment [^MemoryAddress address size scope]
|
||||
(.asSegment address (long size) scope))
|
||||
(^MemorySegment [^MemoryAddress address size ^ResourceScope scope cleanup]
|
||||
(.asSegment address (long size) cleanup scope)))
|
||||
|
||||
(defn add-close-action!
|
||||
"Adds a 0-arity function to be run when the `scope` closes."
|
||||
[scope action]
|
||||
(.addCloseAction ^ResourceScope scope action))
|
||||
[^ResourceScope scope ^Runnable action]
|
||||
(.addCloseAction scope action)
|
||||
nil)
|
||||
|
||||
(defn copy-segment
|
||||
"Copies the content to `dest` from `src`"
|
||||
[dest src]
|
||||
"Copies the content to `dest` from `src`.
|
||||
|
||||
Returns `dest`."
|
||||
^MemorySegment [^MemorySegment dest ^MemorySegment src]
|
||||
(with-acquired (map segment-scope [src dest])
|
||||
(.copyFrom ^MemorySegment dest ^MemorySegment src)))
|
||||
(.copyFrom dest src)
|
||||
dest))
|
||||
|
||||
(defn clone-segment
|
||||
"Clones the content of `segment` into a new segment of the same size."
|
||||
([segment] (clone-segment segment (connected-scope)))
|
||||
([segment scope]
|
||||
(^MemorySegment [segment] (clone-segment segment (connected-scope)))
|
||||
(^MemorySegment [^MemorySegment segment scope]
|
||||
(with-acquired [(segment-scope segment) scope]
|
||||
(doto ^MemorySegment (alloc (.byteSize ^MemorySegment segment) scope)
|
||||
(copy-segment segment)))))
|
||||
(copy-segment ^MemorySegment (alloc (.byteSize segment) scope) segment))))
|
||||
|
||||
(defn slice-segments
|
||||
"Constructs a lazy seq of `size`-length memory segments, sliced from `segment`."
|
||||
[segment size]
|
||||
(let [num-segments (quot (.byteSize ^MemorySegment segment) size)]
|
||||
[^MemorySegment segment size]
|
||||
(let [num-segments (quot (.byteSize segment) size)]
|
||||
(map #(slice segment (* % size) size)
|
||||
(range num-segments))))
|
||||
|
||||
(def ^ByteOrder big-endian
|
||||
"The big-endian [[ByteOrder]].
|
||||
|
||||
See [[little-endian]], [[native-endian]]."
|
||||
ByteOrder/BIG_ENDIAN)
|
||||
|
||||
(def ^ByteOrder little-endian
|
||||
"The little-endian [[ByteOrder]].
|
||||
|
||||
See [[big-endian]], [[native-endian]]"
|
||||
ByteOrder/LITTLE_ENDIAN)
|
||||
|
||||
(def ^ByteOrder native-endian
|
||||
"The [[ByteOrder]] for the native endianness of the current hardware.
|
||||
|
||||
See [[big-endian]], [[little-endian]]."
|
||||
(ByteOrder/nativeOrder))
|
||||
|
||||
(def ^ValueLayout byte-layout
|
||||
"The [[MemoryLayout]] for a byte in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_CHAR)
|
||||
|
||||
(def ^ValueLayout short-layout
|
||||
"The [[MemoryLayout]] for a c-sized short in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_SHORT)
|
||||
|
||||
(def ^ValueLayout int-layout
|
||||
"The [[MemoryLayout]] for a c-sized int in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_INT)
|
||||
|
||||
(def ^ValueLayout long-layout
|
||||
"The [[MemoryLayout]] for a c-sized long in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_LONG)
|
||||
|
||||
(def ^ValueLayout long-long-layout
|
||||
"The [[MemoryLayout]] for a c-sized long-long in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_LONG_LONG)
|
||||
|
||||
(def ^ValueLayout char-layout
|
||||
"The [[MemoryLayout]] for a c-sized char in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_CHAR)
|
||||
|
||||
(def ^ValueLayout float-layout
|
||||
"The [[MemoryLayout]] for a c-sized float in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_FLOAT)
|
||||
|
||||
(def ^ValueLayout double-layout
|
||||
"The [[MemoryLayout]] for a c-sized double in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_DOUBLE)
|
||||
|
||||
(def ^ValueLayout pointer-layout
|
||||
"The [[MemoryLayout]] for a native pointer in [[native-endian]] [[ByteOrder]]."
|
||||
CLinker/C_POINTER)
|
||||
|
||||
(def ^long short-size
|
||||
"The size in bytes of a c-sized short."
|
||||
(.byteSize short-layout))
|
||||
|
||||
(def ^long int-size
|
||||
"The size in bytes of a c-sized int."
|
||||
(.byteSize int-layout))
|
||||
|
||||
(def ^long long-size
|
||||
"The size in bytes of a c-sized long."
|
||||
(.byteSize long-layout))
|
||||
|
||||
(def ^long long-long-size
|
||||
"The size in bytes of a c-sized long long."
|
||||
(.byteSize long-long-layout))
|
||||
|
||||
(def ^long float-size
|
||||
"The size in bytes of a c-sized float."
|
||||
(.byteSize float-layout))
|
||||
|
||||
(def ^long double-size
|
||||
"The size in bytes of a c-sized double."
|
||||
(.byteSize double-layout))
|
||||
|
||||
(def ^long pointer-size
|
||||
"The size in bytes of a c-sized pointer."
|
||||
(.byteSize pointer-layout))
|
||||
|
||||
(def ^long short-alignment
|
||||
"The alignment in bytes of a c-sized short."
|
||||
(.byteAlignment short-layout))
|
||||
|
||||
(def ^long int-alignment
|
||||
"The alignment in bytes of a c-sized int."
|
||||
(.byteAlignment int-layout))
|
||||
|
||||
(def ^long long-alignment
|
||||
"The alignment in bytes of a c-sized long."
|
||||
(.byteAlignment long-layout))
|
||||
|
||||
(def ^long long-long-alignment
|
||||
"The alignment in bytes of a c-sized long long."
|
||||
(.byteAlignment long-long-layout))
|
||||
|
||||
(def ^long float-alignment
|
||||
"The alignment in bytes of a c-sized float."
|
||||
(.byteAlignment float-layout))
|
||||
|
||||
(def ^long double-alignment
|
||||
"The alignment in bytes of a c-sized double."
|
||||
(.byteAlignment double-layout))
|
||||
|
||||
(def ^long pointer-alignment
|
||||
"The alignment in bytes of a c-sized pointer."
|
||||
(.byteAlignment pointer-layout))
|
||||
|
||||
(defn read-byte
|
||||
"Reads a [[byte]] from the `segment`, at an optional `offset`."
|
||||
{:inline
|
||||
(fn read-byte-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getByte ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getByteAtOffset ~segment ~offset)))}
|
||||
([^MemorySegment segment]
|
||||
(MemoryAccess/getByte segment))
|
||||
([^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getByteAtOffset segment offset)))
|
||||
|
||||
(defn read-short
|
||||
"Reads a [[short]] from the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn read-short-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getShort ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getShortAtOffset ~segment ~offset))
|
||||
([segment offset byte-order]
|
||||
`(MemoryAccess/getShortAtOffset ~segment ~offset ~byte-order)))}
|
||||
([^MemorySegment segment]
|
||||
(MemoryAccess/getShort segment))
|
||||
([^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getShortAtOffset segment offset))
|
||||
([^MemorySegment segment ^long offset ^ByteOrder byte-order]
|
||||
(MemoryAccess/getShortAtOffset segment offset byte-order)))
|
||||
|
||||
(defn read-int
|
||||
"Reads a [[int]] from the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn read-int-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getInt ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getIntAtOffset ~segment ~offset))
|
||||
([segment offset byte-order]
|
||||
`(MemoryAccess/getIntAtOffset ~segment ~offset ~byte-order)))}
|
||||
([^MemorySegment segment]
|
||||
(MemoryAccess/getInt segment))
|
||||
([^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getIntAtOffset segment offset))
|
||||
([^MemorySegment segment ^long offset ^ByteOrder byte-order]
|
||||
(MemoryAccess/getIntAtOffset segment offset byte-order)))
|
||||
|
||||
(defn read-long
|
||||
"Reads a [[long]] from the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn read-long-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getLong ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getLongAtOffset ~segment ~offset))
|
||||
([segment offset byte-order]
|
||||
`(MemoryAccess/getLongAtOffset ~segment ~offset ~byte-order)))}
|
||||
(^long [^MemorySegment segment]
|
||||
(MemoryAccess/getLong segment))
|
||||
(^long [^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getLongAtOffset segment offset))
|
||||
(^long [^MemorySegment segment ^long offset ^ByteOrder byte-order]
|
||||
(MemoryAccess/getLongAtOffset segment offset byte-order)))
|
||||
|
||||
(defn read-char
|
||||
"Reads a [[char]] from the `segment`, at an optional `offset`."
|
||||
{:inline
|
||||
(fn read-char-inline
|
||||
([segment]
|
||||
`(char (Byte/toUnsignedInt (MemoryAccess/getByte ~segment))))
|
||||
([segment offset]
|
||||
`(char (Byte/toUnsignedInt (MemoryAccess/getByteAtOffset ~segment ~offset)))))}
|
||||
([^MemorySegment segment]
|
||||
(char (Byte/toUnsignedInt (MemoryAccess/getByte segment))))
|
||||
([^MemorySegment segment ^long offset]
|
||||
(char (Byte/toUnsignedInt (MemoryAccess/getByteAtOffset segment offset)))))
|
||||
|
||||
(defn read-float
|
||||
"Reads a [[float]] from the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn read-float-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getFloat ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getFloatAtOffset ~segment ~offset))
|
||||
([segment offset byte-order]
|
||||
`(MemoryAccess/getFloatAtOffset ~segment ~offset ~byte-order)))}
|
||||
([^MemorySegment segment]
|
||||
(MemoryAccess/getFloat segment))
|
||||
([^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getFloatAtOffset segment offset))
|
||||
([^MemorySegment segment ^long offset ^ByteOrder byte-order]
|
||||
(MemoryAccess/getFloatAtOffset segment offset byte-order)))
|
||||
|
||||
(defn read-double
|
||||
"Reads a [[double]] from the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn read-double-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getDouble ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getDoubleAtOffset ~segment ~offset))
|
||||
([segment offset byte-order]
|
||||
`(MemoryAccess/getDoubleAtOffset ~segment ~offset ~byte-order)))}
|
||||
(^double [^MemorySegment segment]
|
||||
(MemoryAccess/getDouble segment))
|
||||
(^double [^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getDoubleAtOffset segment offset))
|
||||
(^double [^MemorySegment segment ^long offset ^ByteOrder byte-order]
|
||||
(MemoryAccess/getDoubleAtOffset segment offset byte-order)))
|
||||
|
||||
(defn read-address
|
||||
"Reads a [[MemoryAddress]] from the `segment`, at an optional `offset`."
|
||||
{:inline
|
||||
(fn read-address-inline
|
||||
([segment]
|
||||
`(MemoryAccess/getAddress ~segment))
|
||||
([segment offset]
|
||||
`(MemoryAccess/getAddressAtOffset ~segment ~offset)))}
|
||||
(^MemoryAddress [^MemorySegment segment]
|
||||
(MemoryAccess/getAddress segment))
|
||||
(^MemoryAddress [^MemorySegment segment ^long offset]
|
||||
(MemoryAccess/getAddressAtOffset segment offset)))
|
||||
|
||||
(defn write-byte
|
||||
"Writes a [[byte]] to the `segment`, at an optional `offset`."
|
||||
{:inline
|
||||
(fn write-byte-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setByte ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setByteAtOffset ~segment ~offset ~value)))}
|
||||
([^MemorySegment segment value]
|
||||
(MemoryAccess/setByte segment ^byte value))
|
||||
([^MemorySegment segment ^long offset value]
|
||||
(MemoryAccess/setByteAtOffset segment offset ^byte value)))
|
||||
|
||||
(defn write-short
|
||||
"Writes a [[short]] to the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn write-short-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setShort ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setShortAtOffset ~segment ~offset ~value))
|
||||
([segment offset byte-order value]
|
||||
`(MemoryAccess/setShortAtOffset ~segment ~offset ~byte-order ~value)))}
|
||||
([^MemorySegment segment value]
|
||||
(MemoryAccess/setShort segment ^short value))
|
||||
([^MemorySegment segment ^long offset value]
|
||||
(MemoryAccess/setShortAtOffset segment offset ^short value))
|
||||
([^MemorySegment segment ^long offset ^ByteOrder byte-order value]
|
||||
(MemoryAccess/setShortAtOffset segment offset byte-order ^short value)))
|
||||
|
||||
(defn write-int
|
||||
"Writes a [[int]] to the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn write-int-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setInt ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setIntAtOffset ~segment ~offset ~value))
|
||||
([segment offset byte-order value]
|
||||
`(MemoryAccess/setIntAtOffset ~segment ~offset ~byte-order ~value)))}
|
||||
([^MemorySegment segment value]
|
||||
(MemoryAccess/setInt segment ^int value))
|
||||
([^MemorySegment segment ^long offset value]
|
||||
(MemoryAccess/setIntAtOffset segment offset ^int value))
|
||||
([^MemorySegment segment ^long offset ^ByteOrder byte-order value]
|
||||
(MemoryAccess/setIntAtOffset segment offset byte-order ^int value)))
|
||||
|
||||
(defn write-long
|
||||
"Writes a [[long]] to the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn write-long-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setLong ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setLongAtOffset ~segment ~offset ~value))
|
||||
([segment offset byte-order value]
|
||||
`(MemoryAccess/setLongAtOffset ~segment ~offset ~byte-order ~value)))}
|
||||
(^long [^MemorySegment segment ^long value]
|
||||
(MemoryAccess/setLong segment value))
|
||||
(^long [^MemorySegment segment ^long offset ^long value]
|
||||
(MemoryAccess/setLongAtOffset segment offset value))
|
||||
(^long [^MemorySegment segment ^long offset ^ByteOrder byte-order ^long value]
|
||||
(MemoryAccess/setLongAtOffset segment offset byte-order value)))
|
||||
|
||||
(defn write-char
|
||||
"Writes a [[char]] to the `segment`, at an optional `offset`."
|
||||
{:inline
|
||||
(fn write-char-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setByte ~segment (unchecked-byte (unchecked-int ~value))))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setByteAtOffset ~segment ~offset (unchecked-byte (unchecked-int ~value)))))}
|
||||
([^MemorySegment segment value]
|
||||
(MemoryAccess/setByte
|
||||
segment
|
||||
;; HACK(Joshua): The Clojure runtime doesn't have an unchecked-byte cast for
|
||||
;; characters, so this double cast is necessary unless I emit
|
||||
;; my own bytecode with insn.
|
||||
(unchecked-byte (unchecked-int ^char value))))
|
||||
([^MemorySegment segment ^long offset value]
|
||||
(MemoryAccess/setByteAtOffset segment offset (unchecked-byte (unchecked-int ^char value)))))
|
||||
|
||||
(defn write-float
|
||||
"Writes a [[float]] to the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn write-float-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setFloat ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setFloatAtOffset ~segment ~offset ~value))
|
||||
([segment offset byte-order value]
|
||||
`(MemoryAccess/setFloatAtOffset ~segment ~offset ~byte-order ~value)))}
|
||||
([^MemorySegment segment value]
|
||||
(MemoryAccess/setFloat segment ^float value))
|
||||
([^MemorySegment segment ^long offset value]
|
||||
(MemoryAccess/setFloatAtOffset segment offset ^float value))
|
||||
([^MemorySegment segment ^long offset ^ByteOrder byte-order value]
|
||||
(MemoryAccess/setFloatAtOffset segment offset byte-order ^float value)))
|
||||
|
||||
(defn write-double
|
||||
"Writes a [[double]] to the `segment`, at an optional `offset`.
|
||||
|
||||
If `byte-order` is not provided, it defaults to [[native-endian]]."
|
||||
{:inline
|
||||
(fn write-double-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setDouble ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setDoubleAtOffset ~segment ~offset ~value))
|
||||
([segment offset byte-order value]
|
||||
`(MemoryAccess/setDoubleAtOffset ~segment ~offset ~byte-order ~value)))}
|
||||
(^double [^MemorySegment segment ^double value]
|
||||
(MemoryAccess/setDouble segment value))
|
||||
(^double [^MemorySegment segment ^long offset ^double value]
|
||||
(MemoryAccess/setDoubleAtOffset segment offset value))
|
||||
(^double [^MemorySegment segment ^long offset ^ByteOrder byte-order ^double value]
|
||||
(MemoryAccess/setDoubleAtOffset segment offset byte-order value)))
|
||||
|
||||
(defn write-address
|
||||
"Writes a [[MemoryAddress]] to the `segment`, at an optional `offset`."
|
||||
{:inline
|
||||
(fn write-address-inline
|
||||
([segment value]
|
||||
`(MemoryAccess/setAddress ~segment ~value))
|
||||
([segment offset value]
|
||||
`(MemoryAccess/setAddressAtOffset ~segment ~offset ~value)))}
|
||||
(^MemoryAddress [^MemorySegment segment ^MemoryAddress value]
|
||||
(MemoryAccess/setAddress segment value))
|
||||
(^MemoryAddress [^MemorySegment segment ^long offset ^MemoryAddress value]
|
||||
(MemoryAccess/setAddressAtOffset segment offset value)))
|
||||
|
||||
(defn- type-dispatch
|
||||
"Gets a type dispatch value from a (potentially composite) type."
|
||||
[type]
|
||||
(cond
|
||||
(qualified-keyword? type) type
|
||||
(sequential? type) (keyword (first type))))
|
||||
(sequential? type) (keyword (first type))
|
||||
:else (throw (ex-info "Invalid type object" {:type type}))))
|
||||
|
||||
(def primitive?
|
||||
(def primitive-types
|
||||
"A set of all primitive types."
|
||||
#{::byte ::short ::int ::long ::long-long
|
||||
::char ::float ::double ::pointer})
|
||||
|
||||
(defn primitive?
|
||||
"A predicate to determine if a given type is primitive."
|
||||
[type]
|
||||
(contains? primitive-types (type-dispatch type)))
|
||||
|
||||
(defmulti primitive-type
|
||||
"Gets the primitive type that is used to pass as an argument for the `type`.
|
||||
|
||||
|
|
@ -289,39 +683,51 @@
|
|||
|
||||
(defmethod c-layout ::byte
|
||||
[_type]
|
||||
CLinker/C_CHAR)
|
||||
byte-layout)
|
||||
|
||||
(defmethod c-layout ::short
|
||||
[_type]
|
||||
CLinker/C_SHORT)
|
||||
[type]
|
||||
(if (sequential? type)
|
||||
(.withOrder short-layout (second type))
|
||||
short-layout))
|
||||
|
||||
(defmethod c-layout ::int
|
||||
[_type]
|
||||
CLinker/C_INT)
|
||||
[type]
|
||||
(if (sequential? type)
|
||||
(.withOrder int-layout (second type))
|
||||
int-layout))
|
||||
|
||||
(defmethod c-layout ::long
|
||||
[_type]
|
||||
CLinker/C_LONG)
|
||||
[type]
|
||||
(if (sequential? type)
|
||||
(.withOrder long-layout (second type))
|
||||
long-layout))
|
||||
|
||||
(defmethod c-layout ::long-long
|
||||
[_type]
|
||||
CLinker/C_LONG_LONG)
|
||||
[type]
|
||||
(if (sequential? type)
|
||||
(.withOrder long-long-layout (second type))
|
||||
long-long-layout))
|
||||
|
||||
(defmethod c-layout ::char
|
||||
[_type]
|
||||
CLinker/C_CHAR)
|
||||
char-layout)
|
||||
|
||||
(defmethod c-layout ::float
|
||||
[_type]
|
||||
CLinker/C_FLOAT)
|
||||
[type]
|
||||
(if (sequential? type)
|
||||
(.withOrder float-layout (second type))
|
||||
float-layout))
|
||||
|
||||
(defmethod c-layout ::double
|
||||
[_type]
|
||||
CLinker/C_DOUBLE)
|
||||
[type]
|
||||
(if (sequential? type)
|
||||
(.withOrder double-layout (second type))
|
||||
double-layout))
|
||||
|
||||
(defmethod c-layout ::pointer
|
||||
[_type]
|
||||
CLinker/C_POINTER)
|
||||
pointer-layout)
|
||||
|
||||
(def java-prim-layout
|
||||
"Map of primitive type names to the Java types for a method handle."
|
||||
|
|
@ -341,23 +747,27 @@
|
|||
|
||||
If a type serializes to a primitive it returns return a Java primitive type.
|
||||
Otherwise, it returns [[MemorySegment]]."
|
||||
[type]
|
||||
^Class [type]
|
||||
(java-prim-layout (or (primitive-type type) type) MemorySegment))
|
||||
|
||||
(defn size-of
|
||||
"The size in bytes of the given `type`."
|
||||
[type]
|
||||
(.byteSize ^MemoryLayout (c-layout type)))
|
||||
^long [type]
|
||||
(let [t (cond-> type
|
||||
(not (instance? MemoryLayout type)) c-layout)]
|
||||
(.byteSize ^MemoryLayout t)))
|
||||
|
||||
(defn align-of
|
||||
"The alignment in bytes of the given `type`."
|
||||
[type]
|
||||
(.byteAlignment ^MemoryLayout (c-layout type)))
|
||||
^long [type]
|
||||
(let [t (cond-> type
|
||||
(not (instance? MemoryLayout type)) c-layout)]
|
||||
(.byteAlignment ^MemoryLayout t)))
|
||||
|
||||
(defn alloc-instance
|
||||
"Allocates a memory segment for the given `type`."
|
||||
([type] (alloc-instance type (connected-scope)))
|
||||
([type scope] (MemorySegment/allocateNative (long (size-of type)) ^ResourceScope scope)))
|
||||
(^MemorySegment [type] (alloc-instance type (connected-scope)))
|
||||
(^MemorySegment [type scope] (MemorySegment/allocateNative ^long (size-of type) ^ResourceScope scope)))
|
||||
|
||||
(declare serialize serialize-into)
|
||||
|
||||
|
|
@ -456,35 +866,47 @@
|
|||
|
||||
(defmethod serialize-into ::byte
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setByte segment (byte obj)))
|
||||
(write-byte segment (byte obj)))
|
||||
|
||||
(defmethod serialize-into ::short
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setShort segment (short obj)))
|
||||
[obj type segment _scope]
|
||||
(if (sequential? type)
|
||||
(write-short segment 0 (second type) (short obj))
|
||||
(write-short segment (short obj))))
|
||||
|
||||
(defmethod serialize-into ::int
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setInt segment (int obj)))
|
||||
[obj type segment _scope]
|
||||
(if (sequential? type)
|
||||
(write-int segment 0 (second type) (int obj))
|
||||
(write-int segment (int obj))))
|
||||
|
||||
(defmethod serialize-into ::long
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setLong segment (long obj)))
|
||||
[obj type segment _scope]
|
||||
(if (sequential? type)
|
||||
(write-long segment 0 (second type) (long obj))
|
||||
(write-long segment (long obj))))
|
||||
|
||||
(defmethod serialize-into ::long-long
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setLong segment (long obj)))
|
||||
[obj type segment _scope]
|
||||
(if (sequential? type)
|
||||
(write-long segment 0 (second type) (long obj))
|
||||
(write-long segment (long obj))))
|
||||
|
||||
(defmethod serialize-into ::char
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setChar segment (char obj)))
|
||||
(write-char segment (char obj)))
|
||||
|
||||
(defmethod serialize-into ::float
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setFloat segment (float obj)))
|
||||
[obj type segment _scope]
|
||||
(if (sequential? type)
|
||||
(write-float segment 0 (second type) (float obj))
|
||||
(write-float segment (float obj))))
|
||||
|
||||
(defmethod serialize-into ::double
|
||||
[obj _type segment _scope]
|
||||
(MemoryAccess/setDouble segment (double obj)))
|
||||
[obj type segment _scope]
|
||||
(if (sequential? type)
|
||||
(write-double segment 0 (second type) (double obj))
|
||||
(write-double segment (double obj))))
|
||||
|
||||
(defmethod serialize-into ::pointer
|
||||
[obj type segment scope]
|
||||
|
|
@ -535,35 +957,47 @@
|
|||
|
||||
(defmethod deserialize-from ::byte
|
||||
[segment _type]
|
||||
(MemoryAccess/getByte segment))
|
||||
(read-byte segment))
|
||||
|
||||
(defmethod deserialize-from ::short
|
||||
[segment _type]
|
||||
(MemoryAccess/getShort segment))
|
||||
[segment type]
|
||||
(if (sequential? type)
|
||||
(read-short segment 0 (second type))
|
||||
(read-short segment)))
|
||||
|
||||
(defmethod deserialize-from ::int
|
||||
[segment _type]
|
||||
(MemoryAccess/getInt segment))
|
||||
[segment type]
|
||||
(if (sequential? type)
|
||||
(read-int segment 0 (second type))
|
||||
(read-int segment)))
|
||||
|
||||
(defmethod deserialize-from ::long
|
||||
[segment _type]
|
||||
(MemoryAccess/getLong segment))
|
||||
[segment type]
|
||||
(if (sequential? type)
|
||||
(read-long segment 0 (second type))
|
||||
(read-long segment)))
|
||||
|
||||
(defmethod deserialize-from ::long-long
|
||||
[segment _type]
|
||||
(MemoryAccess/getLong segment))
|
||||
[segment type]
|
||||
(if (sequential? type)
|
||||
(read-long segment 0 (second type))
|
||||
(read-long segment)))
|
||||
|
||||
(defmethod deserialize-from ::char
|
||||
[segment _type]
|
||||
(char (Byte/toUnsignedInt (MemoryAccess/getByte segment))))
|
||||
(read-char segment))
|
||||
|
||||
(defmethod deserialize-from ::float
|
||||
[segment _type]
|
||||
(MemoryAccess/getFloat segment))
|
||||
[segment type]
|
||||
(if (sequential? type)
|
||||
(read-float segment 0 (second type))
|
||||
(read-float segment)))
|
||||
|
||||
(defmethod deserialize-from ::double
|
||||
[segment _type]
|
||||
(MemoryAccess/getDouble segment))
|
||||
[segment type]
|
||||
(if (sequential? type)
|
||||
(read-double segment 0 (second type))
|
||||
(read-double segment)))
|
||||
|
||||
(defmethod deserialize-from ::pointer
|
||||
[segment type]
|
||||
|
|
|
|||
Loading…
Reference in a new issue