Merge branch 'release/v1.0.450'

This commit is contained in:
Joshua Suskalo 2024-10-02 14:52:03 -04:00
commit 0847cb1008
No known key found for this signature in database
GPG key ID: 9B6BA586EFF1B9F0
20 changed files with 602 additions and 468 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

1
.gitignore vendored
View file

@ -14,3 +14,4 @@
/.socket-repl-port /.socket-repl-port
.hgignore .hgignore
.hg/ .hg/
/.direnv

View file

@ -1,6 +1,22 @@
# Change Log # 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/). All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
## [1.0.450] - 2024-10-02
### Added
- Support for JDK 22
- `reinterpret` function which changes the size associated with a segment, optionally associating it with an arena and cleanup action
### Changed
- Arglists and docstrings of functions to refer to arenas rather than the outdated terms scope or session
- Change the arguments to `as-segment` to take longs to account for the removal of an Address type
### Removed
- Deprecated functions referring to sessions and scopes
- Deprecated functions `slice-into` and `with-offset`, replaced by the function `slice`
### Fixed
- Prep step when using coffi as a dependency wouldn't re-run if it failed during execution, e.g. when using the incorrect java version
## [0.6.409] - 2023-03-31 ## [0.6.409] - 2023-03-31
### Added ### Added
- Support for JDK 19 - Support for JDK 19
@ -117,6 +133,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 functions
- Support for serializing and deserializing arbitrary Clojure data structures - Support for serializing and deserializing arbitrary Clojure data structures
[Unreleased]: https://github.com/IGJoshua/coffi/compare/v0.6.409...develop
[0.6.409]: https://github.com/IGJoshua/coffi/compare/v0.5.357...v0.6.409 [0.6.409]: https://github.com/IGJoshua/coffi/compare/v0.5.357...v0.6.409
[0.5.357]: https://github.com/IGJoshua/coffi/compare/v0.4.341...v0.5.357 [0.5.357]: https://github.com/IGJoshua/coffi/compare/v0.4.341...v0.5.357
[0.4.341]: https://github.com/IGJoshua/coffi/compare/v0.3.298...v0.4.341 [0.4.341]: https://github.com/IGJoshua/coffi/compare/v0.3.298...v0.4.341

173
README.md
View file

@ -1,23 +1,23 @@
# coffi # coffi
[![Clojars Project](https://img.shields.io/clojars/v/org.suskalo/coffi.svg)](https://clojars.org/org.suskalo/coffi) [![Clojars Project](https://img.shields.io/clojars/v/org.suskalo/coffi.svg)](https://clojars.org/org.suskalo/coffi)
Coffi is a foreign function interface library for Clojure, using the new Coffi is a foreign function interface library for Clojure, using the [Foreign
[Project Panama](https://openjdk.java.net/projects/panama/) that's a part of the Function & Memory API](https://openjdk.org/jeps/454) in JDK 22 and later. This
preview in Java 19. This allows calling native code directly from Clojure allows calling native code directly from Clojure without the need for either
without the need for either Java or native code specific to the library, as e.g. Java or native code specific to the library, as e.g. the JNI does. Coffi focuses
the JNI does. Coffi focuses on ease of use, including functions and macros for on ease of use, including functions and macros for creating wrappers to allow
creating wrappers to allow the resulting native functions to act just like the resulting native functions to act just like Clojure ones, however this
Clojure ones, however this doesn't remove the ability to write systems which doesn't remove the ability to write systems which minimize the cost of
minimize the cost of marshaling data and optimize for performance, to make use marshaling data and optimize for performance, to make use of the low-level
of the low-level access Panama gives us. access Panama gives us.
## Installation ## Installation
This library is available on Clojars. Add one of the following entries to the This library is available on Clojars. Add one of the following entries to the
`:deps` key of your `deps.edn`: `:deps` key of your `deps.edn`:
```clojure ```clojure
org.suskalo/coffi {:mvn/version "0.6.409"} org.suskalo/coffi {:mvn/version "1.0.450"}
io.github.IGJoshua/coffi {:git/tag "v0.6.409" :git/sha "f974446"} io.github.IGJoshua/coffi {:git/tag "v1.0.450" :git/sha "2676a7a"}
``` ```
If you use this library as a git dependency, you will need to prepare the If you use this library as a git dependency, you will need to prepare the
@ -27,19 +27,20 @@ library.
$ clj -X:deps prep $ clj -X:deps prep
``` ```
Coffi requires usage of the package `java.lang.foreign`, and everything in this Coffi requires usage of the package `java.lang.foreign`, and most of the
package is considered to be a preview release, which are disabled by default. In operations are considered unsafe by the JDK, and are therefore unavailable to
order to use coffi, add the following JVM arguments to your application. your code without passing some command line flags. In order to use coffi, add
the following JVM arguments to your application.
```sh ```sh
--enable-preview --enable-native-access=ALL-UNNAMED --enable-native-access=ALL-UNNAMED
``` ```
You can specify JVM arguments in a particular invocation of the Clojure CLI with You can specify JVM arguments in a particular invocation of the Clojure CLI with
the -J flag like so: the -J flag like so:
``` sh ``` sh
clj -J--enable-preview -J--enable-native-access=ALL-UNNAMED clj -J--enable-native-access=ALL-UNNAMED
``` ```
You can also specify them in an alias in your `deps.edn` file under the You can also specify them in an alias in your `deps.edn` file under the
@ -47,15 +48,20 @@ You can also specify them in an alias in your `deps.edn` file under the
using `-M`, `-A`, or `-X`. using `-M`, `-A`, or `-X`.
``` clojure ``` clojure
{:aliases {:dev {:jvm-opts ["--enable-preview" "--enable-native-access=ALL-UNNAMED"]}}} {:aliases {:dev {:jvm-opts ["--enable-native-access=ALL-UNNAMED"]}}}
``` ```
Other build tools should provide similar functionality if you check their Other build tools should provide similar functionality if you check their
documentation. documentation.
When creating an executable jar file, you can avoid the need to pass this
argument by adding the manifest attribute `Enable-Native-Access: ALL-UNNAMED` to
your jar.
Coffi also includes support for the linter clj-kondo. If you use clj-kondo and Coffi also includes support for the linter clj-kondo. If you use clj-kondo and
this library's macros are not linting correctly, you may need to install the this library's macros are not linting correctly, you may need to install the
config bundled with the library. You can do so with the following shell command: config bundled with the library. You can do so with the following shell command,
run from your project directory:
```sh ```sh
$ clj-kondo --copy-configs --dependencies --lint "$(clojure -Spath)" $ clj-kondo --copy-configs --dependencies --lint "$(clojure -Spath)"
@ -193,7 +199,7 @@ be found in `coffi.layout`.
[[:a ::mem/char] [[:a ::mem/char]
[:x ::mem/float]]])) [:x ::mem/float]]]))
(mem/size-of ::needs-padding)) (mem/size-of ::needs-padding)
;; => 8 ;; => 8
(mem/align-of ::needs-padding) (mem/align-of ::needs-padding)
@ -331,7 +337,7 @@ Clojure code to make this easier.
native-fn native-fn
[ints] [ints]
(let [arr-len (count ints) (let [arr-len (count ints)
int-array (serialize ints [::mem/array ::mem/int arr-len])] int-array (mem/serialize ints [::mem/array ::mem/int arr-len])]
(native-fn (mem/address-of int-array) arr-len))) (native-fn (mem/address-of int-array) arr-len)))
``` ```
@ -349,38 +355,34 @@ This can be used to implement out variables often seen in native code.
"out_int" [::mem/pointer] ::mem/void "out_int" [::mem/pointer] ::mem/void
native-fn native-fn
[i] [i]
(let [int-ptr (serialize i [::mem/pointer ::mem/int])] (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int])]
(native-fn int-ptr) (native-fn int-ptr)
(deserialize int-ptr [::mem/pointer ::mem/int]))) (mem/deserialize int-ptr [::mem/pointer ::mem/int])))
``` ```
### Sessions ### Arenas
**Before JDK 19 Sessions were called Scopes. Coffi retains functions that are
named for creating scopes for backwards compatibility, but they will be removed
in version 1.0.**
In order to serialize any non-primitive type (such as the previous In order to serialize any non-primitive type (such as the previous
`[::mem/pointer ::mem/int]`), off-heap memory needs to be allocated. When memory `[::mem/pointer ::mem/int]`), off-heap memory needs to be allocated. When memory
is allocated inside the JVM, the memory is associated with a session. Because is allocated inside the JVM, the memory is associated with an arena. Because
none was provided here, the session is an implicit session, and the memory will none was provided here, the arena is an implicit arena, and the memory will be
be freed when the serialized object is garbage collected. freed when the serialized object is garbage collected.
In many cases this is not desirable, because the memory is not freed in a In many cases this is not desirable, because the memory is not freed in a
deterministic manner, causing garbage collection pauses to become longer, as deterministic manner, causing garbage collection pauses to become longer, as
well as changing allocation performance. Instead of an implicit session, there well as changing allocation performance. Instead of an implicit arena, there
are other kinds of sessions as well. A `stack-session` is a thread-local are other kinds of arenas as well. A `confined-arena` is a thread-local arena.
session. Stack sessions are `Closeable`, which means they should usually be used Confined arenas are `Closeable`, which means they should usually be used in a
in a `with-open` form. When a `stack-session` is closed, it immediately frees `with-open` form. When a `confined-arena` is closed, it immediately frees all
all the memory associated with it. The previous example, `out-int`, can be the memory associated with it. The previous example, `out-int`, can be
implemented with a stack session. implemented with a confined arena.
```clojure ```clojure
(defcfn out-int (defcfn out-int
"out_int" [::mem/pointer] ::mem/void "out_int" [::mem/pointer] ::mem/void
native-fn native-fn
[i] [i]
(with-open [session (mem/stack-session)] (with-open [arena (mem/confined-arena)]
(let [int-ptr (mem/serialize i [::mem/pointer ::mem/int] session)] (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int] arena)]
(native-fn int-ptr) (native-fn int-ptr)
(mem/deserialize int-ptr [::mem/pointer ::mem/int])))) (mem/deserialize int-ptr [::mem/pointer ::mem/int]))))
``` ```
@ -388,15 +390,15 @@ implemented with a stack session.
This will free the pointer immediately upon leaving the function. This will free the pointer immediately upon leaving the function.
When memory needs to be accessible from multiple threads, there's When memory needs to be accessible from multiple threads, there's
`shared-session`. When using a `shared-session`, it should be accessed inside a `shared-arena`. When a `shared-arena` is `.close`d, it will release all its
`with-acquired` block. When a `shared-session` is `.close`d, it will release all associated memory immediately, and so this should only be done once all other
its associated memory when every `with-acquired` block associated with it is threads are done accessing memory associated with it.
exited.
In addition, two non-`Closeable` sessions are `global-session`, which never In addition, two non-`Closeable` arenas are `global-arena`, which never frees
frees the resources associated with it, and `connected-session`, which is a the resources associated with it, and `auto-arena`, which is an arena that frees
session that frees its resources on garbage collection, like an implicit its resources once all of them are unreachable during a garbage collection
session. cycle, like an implicit arena, but potentially for multiple allocations rather
than just one.
### Serialization and Deserialization ### Serialization and Deserialization
Custom serializers and deserializers may also be created. This is done using two Custom serializers and deserializers may also be created. This is done using two
@ -426,34 +428,35 @@ serialize to primitives.
```clojure ```clojure
(defmethod mem/serialize* ::vector (defmethod mem/serialize* ::vector
[obj _type session] [obj _type arena]
(mem/address-of (mem/serialize obj [::mem/array ::mem/float 3] session))) (mem/serialize obj [::mem/array ::mem/float 3] arena))
(defmethod mem/deserialize* ::vector (defmethod mem/deserialize* ::vector
[addr _type] [segment _type]
(mem/deserialize (mem/slice-global addr (mem/size-of [::mem/array ::mem/float 3])) (mem/deserialize (mem/reinterpret segment (mem/size-of [::mem/array ::mem/float 3]))
[::mem/array ::mem/float 3])) [::mem/array ::mem/float 3]))
``` ```
The `slice-global` function allows you to take an address without an associated The `reinterpret` function allows you to take a segment and decorate it with a
session and get a memory segment which can be deserialized. new size, and possibly associate it with an arena or add cleanup functions on
it.
In cases like this where we don't know the session of the pointer, we could use In cases like this where we don't know the arena of the pointer, we could use
`add-close-action!` to ensure it's freed. For example if a `free-vector!` `reinterpret` to ensure it's freed. For example if a `free-vector!` function
function that takes a pointer exists, we could use this: that takes a pointer exists, we could use this:
```clojure ```clojure
(defcfn returns-vector (defcfn returns-vector
"returns_vector" [] ::mem/pointer "returns_vector" [] ::mem/pointer
native-fn native-fn
[session] [arena]
(let [ret-ptr (native-fn)] (let [ret-ptr (native-fn)]
(add-close-action! session #(free-vector! ret-ptr)) (-> (reinterpret ret-ptr (mem/size-of ::vector) arena free-vector!)
(deserialize ret-ptr ::vector))) (deserialize ::vector))))
``` ```
This function takes a session and returns the deserialized vector, and it will This function takes an arena and returns the deserialized vector, and it will
free the pointer when the session closes. free the pointer when the arena closes.
#### Tagged Union #### Tagged Union
For the tagged union type, we will represent the value as a vector of a keyword For the tagged union type, we will represent the value as a vector of a keyword
@ -505,7 +508,7 @@ deserialize the value into and out of memory segments. This is accomplished with
(map first)))) (map first))))
(defmethod mem/serialize-into ::tagged-union (defmethod mem/serialize-into ::tagged-union
[obj [_tagged-union tags type-map] segment session] [obj [_tagged-union tags type-map] segment arena]
(mem/serialize-into (mem/serialize-into
{:tag (item-index tags (first obj)) {:tag (item-index tags (first obj))
:value (second obj)} :value (second obj)}
@ -513,7 +516,7 @@ deserialize the value into and out of memory segments. This is accomplished with
[[:tag ::mem/long] [[:tag ::mem/long]
[:value (get type-map (first obj))]]] [:value (get type-map (first obj))]]]
segment segment
session)) arena))
``` ```
This serialization method is rather simple, it just turns the vector value into This serialization method is rather simple, it just turns the vector value into
@ -570,7 +573,7 @@ it could be represented for serialization purposes like so:
This union however would not include the tag when serialized. This union however would not include the tag when serialized.
If a union is deserialized, then all that coffi does is to allocate a new If a union is deserialized, then all that coffi does is to allocate a new
segment of the appropriate size with an implicit session so that it may later be segment of the appropriate size with an implicit arena so that it may later be
garbage collected, and copies the data from the source segment into it. It's up garbage collected, and copies the data from the source segment into it. It's up
to the user to call `deserialize-from` on that segment with the appropriate to the user to call `deserialize-from` on that segment with the appropriate
type. type.
@ -593,14 +596,14 @@ create raw function handles.
With raw handles, 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 expected by the native function. For primitive types, those are primitives. For
addresses, that is `MemoryAddress`, and for composite types like structs and pointers, that is `MemorySegment`, and for composite types like structs and
unions, that is `MemorySegment`. Both `MemoryAddress` and `MemorySegment` come unions, that is also `MemorySegment`. `MemorySegment` comes from the
from the `java.lang.foreign` package. `java.lang.foreign` package.
In addition, when a raw handle returns a composite type represented with a In addition, when a raw handle returns a composite type represented with a
`MemorySegment`, it requires an additional first argument, a `SegmentAllocator`, `MemorySegment`, it requires an additional first argument, a `SegmentAllocator`,
which can be acquired with `session-allocator` to get one associated with a which can be acquired with `arena-allocator` to get one associated with a
specific session. The returned value will live until that session is released. specific arena. The returned value will live until that arena is released.
In addition, function types can be specified as being raw, in the following In addition, function types can be specified as being raw, in the following
manner: manner:
@ -635,14 +638,18 @@ As an example, when wrapping a function that returns an array of big-endian
floats, the following code might be used. floats, the following code might be used.
``` clojure ``` clojure
;; int returns_float_array(float **arr)
(def ^:private returns-float-array* (ffi/make-downcall "returns_float_array" [::mem/pointer] ::mem/int)) (def ^:private returns-float-array* (ffi/make-downcall "returns_float_array" [::mem/pointer] ::mem/int))
;; void releases_float_array(float *arr)
(def ^:private release-floats* (ffi/make-downcall "releases_float_array" [::mem/pointer] ::mem/void)) (def ^:private release-floats* (ffi/make-downcall "releases_float_array" [::mem/pointer] ::mem/void))
(defn returns-float-array (defn returns-float-array
[] []
(with-open [session (mem/stack-session)] (with-open [arena (mem/confined-arena)]
(let [out-floats (mem/alloc mem/pointer-size session) ;; float *out_floats;
num-floats (function-handle (mem/address-of out-floats)) ;; int num_floats = returns_float_array(&out_floats);
(let [out-floats (mem/alloc mem/pointer-size arena)
num-floats (returns-float-array* (mem/address-of out-floats))
floats-addr (mem/read-address out-floats) floats-addr (mem/read-address out-floats)
floats-slice (mem/slice-global floats-addr (unchecked-multiply-int mem/float-size num-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, ;; Using a try/finally to perform an operation when the stack frame exits,
@ -657,7 +664,7 @@ floats, the following code might be used.
mem/big-endian)) mem/big-endian))
(unchecked-inc-int index)))) (unchecked-inc-int index))))
(finally (finally
(release-floats floats-addr)))))) (release-floats* floats-addr))))))
``` ```
The above code manually performs all memory operations rather than relying on The above code manually performs all memory operations rather than relying on
@ -711,6 +718,8 @@ the multimethod `reify-symbolspec`, although it's recommended that for any
library authors who do so, namespaced keywords be used to name types. library authors who do so, namespaced keywords be used to name types.
## Alternatives ## Alternatives
**ALTERNATIVES INFORMATION IS OUT OF DATE. THE LINKS ARE FINE, BUT DESCRIPTIONS WILL BE UPDATED AT A LATER DATE.**
This library is not the only Clojure library providing access to native code. In This library is not the only Clojure library providing access to native code. In
addition the following libraries exist: addition the following libraries exist:
@ -740,7 +749,7 @@ appealing, as they have a smaller API surface area and it's easier to wrap
functions. functions.
### Benchmarks ### Benchmarks
**BENCHMARKS FOR COFFI AND DTYPE-NEXT ARE BASED ON AN OLD VERSION. NEW BENCHMARKS WILL BE CREATED WHEN PANAMA COMES OUT OF PREVIEW** **BENCHMARKS FOR COFFI AND DTYPE-NEXT ARE BASED ON AN OLD VERSION. NEW BENCHMARKS WILL BE CREATED SOON.**
An additional consideration when thinking about alternatives is the performance An additional consideration when thinking about alternatives is the performance
of each available option. It's an established fact that JNA (used by all three of each available option. It's an established fact that JNA (used by all three
@ -1132,24 +1141,6 @@ These features are planned for future releases.
- Mapped memory - Mapped memory
- Helper macros for custom serde implementations for composite data types - Helper macros for custom serde implementations for composite data types
### Future JDKs
The purpose of coffi is to provide a wrapper for published versions of Project
Panama, starting with JDK 17. As new JDKs are released, coffi will be ported to
the newer versions of Panama. Version `0.4.341` is the last version compatible
with JDK 17. Version `0.5.357` is the last version compatible with JDK 18.
Version `0.6.409` is the latest version compatible with JDK 19. Bugfixes, and
potential backports of newer coffi features may be found on the `jdk17-lts`
branch. Development of new features and fixes as well as support for new Panama
idioms and features will continue with focus only on the latest JDK. If a
particular feature is not specific to the newer JDK, PRs backporting it to
versions of coffi supporting Java 17 will likely be accepted.
### 1.0 Release
Because the feature that coffi wraps in the JDK is in preview as of JDK 19,
coffi itself will not be released in a 1.0.x version until the feature becomes a
core part of the JDK, likely before or during the next LTS release, Java 21, in
September 2023.
## License ## License
Copyright © 2023 Joshua Suskalo Copyright © 2023 Joshua Suskalo

View file

@ -17,7 +17,8 @@
[clojure.tools.build.api :as b])) [clojure.tools.build.api :as b]))
(def lib-coord 'org.suskalo/coffi) (def lib-coord 'org.suskalo/coffi)
(def version (format "0.6.%s" (b/git-count-revs nil))) ;;(def version (format "0.6.%s" (b/git-count-revs nil)))
(def version "1.0.450")
(def resource-dirs ["resources/"]) (def resource-dirs ["resources/"])
@ -49,11 +50,14 @@
"Compiles java classes required for interop." "Compiles java classes required for interop."
[opts] [opts]
(.mkdirs (io/file class-dir)) (.mkdirs (io/file class-dir))
(b/process {:command-args ["javac" "--enable-preview" (let [compilation-result
(b/process {:command-args ["javac"
"src/java/coffi/ffi/Loader.java" "src/java/coffi/ffi/Loader.java"
"-d" class-dir "-d" class-dir
"-target" "19" "-target" "22"
"-source" "19"]}) "-source" "22"]})]
(when-not (zero? (:exit compilation-result))
(b/delete {:path class-dir})))
opts) opts)
(defn- write-pom (defn- write-pom

View file

@ -1,6 +1,6 @@
{:paths ["src/clj" "target/classes" "resources"] {:paths ["src/clj" "target/classes" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"} :deps {org.clojure/clojure {:mvn/version "1.11.1"}
insn/insn {:mvn/version "0.2.1"}} insn/insn {:mvn/version "0.5.4"}}
:deps/prep-lib {:alias :build :deps/prep-lib {:alias :build
:fn build/compile-java :fn build/compile-java
@ -12,26 +12,25 @@
nodisassemble/nodisassemble {:mvn/version "0.1.3"}} nodisassemble/nodisassemble {:mvn/version "0.1.3"}}
;; NOTE(Joshua): If you want to use nodisassemble you should also add a ;; NOTE(Joshua): If you want to use nodisassemble you should also add a
;; -javaagent for the resolved location ;; -javaagent for the resolved location
:jvm-opts ["--enable-native-access=ALL-UNNAMED" "--enable-preview"]} :jvm-opts ["--enable-native-access=ALL-UNNAMED"]}
:test {:extra-paths ["test/clj"] :test {:extra-paths ["test/clj"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"}
io.github.cognitect-labs/test-runner io.github.cognitect-labs/test-runner
{:git/url "https://github.com/cognitect-labs/test-runner" {:git/url "https://github.com/cognitect-labs/test-runner"
:sha "62ef1de18e076903374306060ac0e8a752e57c86"}} :sha "62ef1de18e076903374306060ac0e8a752e57c86"}}
:jvm-opts ["--enable-native-access=ALL-UNNAMED" "--enable-preview"] :jvm-opts ["--enable-native-access=ALL-UNNAMED"]
:exec-fn cognitect.test-runner.api/test} :exec-fn cognitect.test-runner.api/test}
:codox {:extra-deps {codox/codox {:mvn/version "0.10.7"}} :codox {:extra-deps {codox/codox {:mvn/version "0.10.8"}}
:replace-deps {insn/insn {:mvn/version "0.2.1"}}
:exec-fn codox.main/generate-docs :exec-fn codox.main/generate-docs
:exec-args {:name "coffi" :exec-args {:name "coffi"
:version "v0.6.409" :version "v1.0.450"
:description "A Foreign Function Interface in Clojure for JDK 19." :description "A Foreign Function Interface in Clojure for JDK 22+."
:source-paths ["src/clj"] :source-paths ["src/clj"]
:output-path "docs" :output-path "docs"
:source-uri "https://github.com/IGJoshua/coffi/blob/{git-commit}/{filepath}#L{line}" :source-uri "https://github.com/IGJoshua/coffi/blob/{git-commit}/{filepath}#L{line}"
:metadata {:doc/format :markdown}} :metadata {:doc/format :markdown}}}
:jvm-opts ["--add-opens" "java.base/java.lang=ALL-UNNAMED"
"--enable-preview"]}
:build {:replace-deps {org.clojure/clojure {:mvn/version "1.10.3"} :build {:replace-deps {org.clojure/clojure {:mvn/version "1.10.3"}
io.github.clojure/tools.build {:git/tag "v0.3.0" :git/sha "e418fc9"}} io.github.clojure/tools.build {:git/tag "v0.3.0" :git/sha "e418fc9"}}

View file

@ -1,20 +1,41 @@
<!DOCTYPE html PUBLIC "" <!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.6.409</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-defconst"><div class="inner"><span>defconst</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-defvar"><div class="inner"><span>defvar</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-ensure-symbol"><div class="inner"><span>ensure-symbol</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-static-variable-segment"><div class="inner"><span>static-variable-segment</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">v1.0.450</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-defconst"><div class="inner"><span>defconst</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-defvar"><div class="inner"><span>defvar</span></div></a></li><li class="depth-1"><a href="coffi.ffi.html#var-ensure-symbol"><div class="inner"><span>ensure-symbol</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-static-variable-segment"><div class="inner"><span>static-variable-segment</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>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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L429">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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L570">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 &amp; 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/bcf2e031f995d1c7473797c70f94b9731634c97b/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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L564">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 &amp; 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><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>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>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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L745">view source</a></div></div><div class="public anchor" id="var-defconst"><h3>defconst</h3><h4 class="type">macro</h4><div class="usage"><code>(defconst symbol docstring? symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Defines a var named by <code>symbol</code> to be the value of the given <code>type</code> from <code>symbol-or-addr</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L581">view source</a></div></div><div class="public anchor" id="var-defvar"><h3>defvar</h3><h4 class="type">macro</h4><div class="usage"><code>(defvar symbol docstring? symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Defines a var named by <code>symbol</code> to be a reference to the native memory from <code>symbol-or-addr</code>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L648">view source</a></div></div><div class="public anchor" id="var-ensure-symbol"><h3>ensure-symbol</h3><div class="usage"><code>(ensure-symbol symbol-or-addr)</code></div><div class="doc"><div class="markdown"><p>Returns the argument if it is a <a href="null">MemorySegment</a>, otherwise calls <a href="coffi.ffi.html#var-find-symbol">find-symbol</a> on it.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L189">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">MemorySegment</a> of a symbol from the loaded libraries.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L39">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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L611">view source</a></div></div><div class="public anchor" id="var-fswap.21"><h3>fswap!</h3><div class="usage"><code>(fswap! static-var f &amp; 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>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>
<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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L620">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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L34">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 systems load path.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L29">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> </div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L738">view source</a></div></div><div class="public anchor" id="var-defconst"><h3>defconst</h3><h4 class="type">macro</h4><div class="usage"><code>(defconst symbol docstring? symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Defines a var named by <code>symbol</code> to be the value of the given <code>type</code> from <code>symbol-or-addr</code>.</p>
</div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L575">view source</a></div></div><div class="public anchor" id="var-defvar"><h3>defvar</h3><h4 class="type">macro</h4><div class="usage"><code>(defvar symbol docstring? symbol-or-addr type)</code></div><div class="doc"><div class="markdown"><p>Defines a var named by <code>symbol</code> to be a reference to the native memory from <code>symbol-or-addr</code>.</p>
</div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L641">view source</a></div></div><div class="public anchor" id="var-ensure-symbol"><h3>ensure-symbol</h3><div class="usage"><code>(ensure-symbol symbol-or-addr)</code></div><div class="doc"><div class="markdown"><p>Returns the argument if it is a <a href="null">MemorySegment</a>, otherwise calls <a href="coffi.ffi.html#var-find-symbol">find-symbol</a> on it.</p>
</div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L181">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">MemorySegment</a> of a symbol from the loaded libraries.</p>
</div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L38">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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L605">view source</a></div></div><div class="public anchor" id="var-fswap.21"><h3>fswap!</h3><div class="usage"><code>(fswap! static-var f &amp; 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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L614">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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L33">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 systems load path.</p>
</div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L28">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>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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L197">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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L417">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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L398">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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L189">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/bcf2e031f995d1c7473797c70f94b9731634c97b/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/bcf2e031f995d1c7473797c70f94b9731634c97b/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 <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>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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L213">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>See <a href="coffi.ffi.html#var-make-downcall">make-downcall</a>.</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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L707">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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L666">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> </div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L205">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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L700">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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L659">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.</p> <p>The returned value can be dereferenced, and has metadata.</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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L636">view source</a></div></div><div class="public anchor" id="var-static-variable-segment"><h3>static-variable-segment</h3><div class="usage"><code>(static-variable-segment static-var)</code></div><div class="doc"><div class="markdown"><p>Gets the backing <a href="null">MemorySegment</a> from <code>static-var</code>.</p> <p>See <a href="coffi.ffi.html#var-freset.21">freset!</a>, <a href="coffi.ffi.html#var-fswap.21">fswap!</a>.</p>
<p>This is primarily useful when you need to pass the static variables address to a native function which takes an <a href="null">Addressable</a>.</p></div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L628">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> </div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L630">view source</a></div></div><div class="public anchor" id="var-static-variable-segment"><h3>static-variable-segment</h3><div class="usage"><code>(static-variable-segment static-var)</code></div><div class="doc"><div class="markdown"><p>Gets the backing <a href="null">MemorySegment</a> from <code>static-var</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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/ffi.clj#L443">view source</a></div></div></div></body></html> <p>This is primarily useful when you need to pass the static variables address to a native function which takes an <a href="null">Addressable</a>.</p>
</div></div><div class="src-link"><a href="https://github.com/IGJoshua/coffi/blob/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L622">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/bcf2e031f995d1c7473797c70f94b9731634c97b/src/clj/coffi/ffi.clj#L436">view source</a></div></div></div></body></html>

View file

@ -1,4 +1,6 @@
<!DOCTYPE html PUBLIC "" <!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.6.409</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> <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">v1.0.450</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>
<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/9d6051236550967a00151244e9e0c5630e2944b1/src/clj/coffi/layout.clj#L6">view source</a></div></div></div></body></html> </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/bcf2e031f995d1c7473797c70f94b9731634c97b/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

26
flake.lock Normal file
View file

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1727634051,
"narHash": "sha256-S5kVU7U82LfpEukbn/ihcyNt2+EvG7Z5unsKW9H/yFA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "06cf0e1da4208d3766d898b7fdab6513366d45b9",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-unstable",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

33
flake.nix Normal file
View file

@ -0,0 +1,33 @@
{
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
overlays = [
(final: prev: {
clojure = prev.clojure.override { jdk = final.jdk22; };
})
];
};
in
{
devShells.${system}.default = pkgs.mkShell rec {
packages = [
];
nativeBuildInputs = with pkgs; [
clojure
];
buildInputs = with pkgs; [
];
inputsFrom = with pkgs; [
];
};
};
}

View file

@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<name>org.suskalo/coffi</name> <name>org.suskalo/coffi</name>
<description>A Foreign Function Interface in Clojure for JDK 18.</description> <description>A Foreign Function Interface in Clojure for JDK 22+.</description>
<url>https://github.com/IGJoshua/coffi</url> <url>https://github.com/IGJoshua/coffi</url>
<licenses> <licenses>
<license> <license>

View file

@ -14,10 +14,9 @@
MethodHandles MethodHandles
MethodType) MethodType)
(java.lang.foreign (java.lang.foreign
Addressable
Linker Linker
Linker$Option
FunctionDescriptor FunctionDescriptor
MemoryAddress
MemoryLayout MemoryLayout
MemorySegment MemorySegment
SegmentAllocator))) SegmentAllocator)))
@ -56,7 +55,8 @@
(defn- downcall-handle (defn- downcall-handle
"Gets the [[MethodHandle]] for the function at the `sym`." "Gets the [[MethodHandle]] for the function at the `sym`."
[sym function-descriptor] [sym function-descriptor]
(.downcallHandle (Linker/nativeLinker) sym function-descriptor)) (.downcallHandle (Linker/nativeLinker) sym function-descriptor
(make-array Linker$Option 0)))
(def ^:private load-instructions (def ^:private load-instructions
"Mapping from primitive types to the instruction used to load them onto the stack." "Mapping from primitive types to the instruction used to load them onto the stack."
@ -130,20 +130,12 @@
[:invokevirtual (prim-classes prim-type) (unbox-fn-for-type prim-type) [prim]]] [:invokevirtual (prim-classes prim-type) (unbox-fn-for-type prim-type) [prim]]]
[])))) []))))
(defn- coerce-addressable
"If the passed `type` is [[MemoryAddress]], returns [[Addressable]], otherwise returns `type`.
This is used to declare the return types of upcall stubs."
[type]
(if (= type MemoryAddress)
Addressable
type))
(defn- downcall-class (defn- downcall-class
"Class definition for an implementation of [[IFn]] which calls a closed over "Class definition for an implementation of [[IFn]] which calls a closed over
method handle without reflection, unboxing primitives when needed." method handle without reflection, unboxing primitives when needed."
[args ret] [args ret]
{:flags #{:public :final} {:flags #{:public :final}
:version 8
:super clojure.lang.AFunction :super clojure.lang.AFunction
:fields [{:name "downcall_handle" :fields [{:name "downcall_handle"
:type MethodHandle :type MethodHandle
@ -175,7 +167,7 @@
args) args)
[:invokevirtual MethodHandle "invokeExact" [:invokevirtual MethodHandle "invokeExact"
(cond->> (cond->>
(conj (mapv (comp coerce-addressable insn-layout) args) (conj (mapv insn-layout args)
(insn-layout ret)) (insn-layout ret))
(not (mem/primitive-type ret)) (cons SegmentAllocator))] (not (mem/primitive-type ret)) (cons SegmentAllocator))]
(to-object-asm ret) (to-object-asm ret)
@ -244,20 +236,20 @@
The return type and any arguments that are primitives will not The return type and any arguments that are primitives will not
be (de)serialized except to be cast. If all arguments and return are be (de)serialized except to be cast. If all arguments and return are
primitive, the `downcall` is returned directly. In cases where arguments must primitive, the `downcall` is returned directly. In cases where arguments must
be serialized, a new [[mem/stack-session]] is generated." be serialized, a new [[mem/confined-arena]] is generated."
[downcall arg-types ret-type] [downcall arg-types ret-type]
(let [;; Complexity of types (let [;; Complexity of types
const-args? (or (vector? arg-types) (nil? arg-types)) const-args? (or (vector? arg-types) (nil? arg-types))
simple-args? (when const-args? simple-args? (when const-args?
(and (every? mem/primitive? arg-types) (and (every? mem/primitive? arg-types)
;; NOTE(Joshua): Pointer types with serdes (e.g. [::mem/pointer ::mem/int]) ;; NOTE(Joshua): Pointer types with serdes (e.g. [::mem/pointer ::mem/int])
;; still require a session, making them not qualify as "simple". ;; still require an arena, making them not qualify as "simple".
(every? keyword? (filter (comp #{::mem/pointer} mem/primitive-type) arg-types)))) (every? keyword? (filter (comp #{::mem/pointer} mem/primitive-type) arg-types))))
const-ret? (s/valid? ::mem/type ret-type) const-ret? (s/valid? ::mem/type ret-type)
primitive-ret? (and const-ret? primitive-ret? (and const-ret?
(or (and (mem/primitive? ret-type) (or (and (mem/primitive? ret-type)
;; NOTE(Joshua): Pointer types with serdes require deserializing the ;; NOTE(Joshua): Pointer types with serdes require deserializing the
;; return value, but don't require passing a session to the downcall, ;; return value, but don't require passing an arena to the downcall,
;; making them cause the return to not be primitive, but it may still ;; making them cause the return to not be primitive, but it may still
;; be "simple". ;; be "simple".
(or (keyword? ret-type) (not (#{::mem/pointer} (mem/primitive-type ret-type))))) (or (keyword? ret-type) (not (#{::mem/pointer} (mem/primitive-type ret-type)))))
@ -273,7 +265,7 @@
~ret-type ~ret-type
downcall#) downcall#)
(let [;; All our symbols (let [;; All our symbols
session (gensym "session") arena (gensym "arena")
downcall-sym (gensym "downcall") downcall-sym (gensym "downcall")
args-sym (when-not const-args? args-sym (when-not const-args?
(gensym "args")) (gensym "args"))
@ -292,7 +284,7 @@
(some->> (some->>
(cond (cond
(not (s/valid? ::mem/type type)) (not (s/valid? ::mem/type type))
`(mem/serialize ~sym ~type-sym ~session) `(mem/serialize ~sym ~type-sym ~arena)
(and (mem/primitive? type) (and (mem/primitive? type)
(not (#{::mem/pointer} (mem/primitive-type type)))) (not (#{::mem/pointer} (mem/primitive-type type))))
@ -300,14 +292,14 @@
;; cast null pointers to something understood by panama ;; cast null pointers to something understood by panama
(#{::mem/pointer} type) (#{::mem/pointer} type)
`(or ~sym (MemoryAddress/NULL)) `(or ~sym (MemorySegment/NULL))
(mem/primitive-type type) (mem/primitive-type type)
`(mem/serialize* ~sym ~type-sym ~session) `(mem/serialize* ~sym ~type-sym ~arena)
:else :else
`(let [alloc# (mem/alloc-instance ~type-sym)] `(let [alloc# (mem/alloc-instance ~type-sym)]
(mem/serialize-into ~sym ~type-sym alloc# ~session) (mem/serialize-into ~sym ~type-sym alloc# ~arena)
alloc#)) alloc#))
(list sym))) (list sym)))
@ -334,7 +326,7 @@
:else :else
`(let [~args-sym (map (fn [obj# type#] `(let [~args-sym (map (fn [obj# type#]
(mem/serialize obj# type# ~session)) (mem/serialize obj# type# ~arena))
~args-sym ~args-types-sym)] ~args-sym ~args-types-sym)]
~expr))) ~expr)))
@ -343,7 +335,7 @@
;; taking restargs, and so the downcall must be applied ;; taking restargs, and so the downcall must be applied
(-> `(~@(when (symbol? args) [`apply]) (-> `(~@(when (symbol? args) [`apply])
~downcall-sym ~downcall-sym
~@(when allocator? [`(mem/session-allocator ~session)]) ~@(when allocator? [`(mem/arena-allocator ~arena)])
~@(if (symbol? args) ~@(if (symbol? args)
[args] [args]
args)) args))
@ -366,12 +358,12 @@
:else :else
(deserialize-segment expr))) (deserialize-segment expr)))
wrap-session (fn [expr] wrap-arena (fn [expr]
`(with-open [~session (mem/stack-session)] `(with-open [~arena (mem/confined-arena)]
~expr)) ~expr))
wrap-fn (fn [call needs-session?] wrap-fn (fn [call needs-arena?]
`(fn [~@(if const-args? arg-syms ['& args-sym])] `(fn [~@(if const-args? arg-syms ['& args-sym])]
~(cond-> call needs-session? wrap-session)))] ~(cond-> call needs-arena? wrap-arena)))]
`(let [;; NOTE(Joshua): To ensure all arguments are evaluated once and `(let [;; NOTE(Joshua): To ensure all arguments are evaluated once and
;; in-order, they must be bound here ;; in-order, they must be bound here
~downcall-sym ~downcall ~downcall-sym ~downcall
@ -403,15 +395,15 @@
[downcall arg-types ret-type] [downcall arg-types ret-type]
(if (mem/primitive-type ret-type) (if (mem/primitive-type ret-type)
(fn native-fn [& args] (fn native-fn [& args]
(with-open [session (mem/stack-session)] (with-open [arena (mem/confined-arena)]
(mem/deserialize* (mem/deserialize*
(apply downcall (map #(mem/serialize %1 %2 session) args arg-types)) (apply downcall (map #(mem/serialize %1 %2 arena) args arg-types))
ret-type))) ret-type)))
(fn native-fn [& args] (fn native-fn [& args]
(with-open [session (mem/stack-session)] (with-open [arena (mem/confined-arena)]
(mem/deserialize-from (mem/deserialize-from
(apply downcall (mem/session-allocator session) (apply downcall (mem/arena-allocator arena)
(map #(mem/serialize %1 %2 session) args arg-types)) (map #(mem/serialize %1 %2 arena) args arg-types))
ret-type))))) ret-type)))))
(defn make-serde-varargs-wrapper (defn make-serde-varargs-wrapper
@ -435,6 +427,7 @@
If your `args` and `ret` are constants, then it is more efficient to If your `args` and `ret` are constants, then it is more efficient to
call [[make-downcall]] followed by [[make-serde-wrapper]] because the latter call [[make-downcall]] followed by [[make-serde-wrapper]] because the latter
has an inline definition which will result in less overhead from serdes." has an inline definition which will result in less overhead from serdes."
;; TODO(Joshua): Add an inline arity for when the args and ret types are constant
[symbol args ret] [symbol args ret]
(-> symbol (-> symbol
(make-downcall args ret) (make-downcall args ret)
@ -474,6 +467,7 @@
boxes any primitives passed to it and calls a closed over [[IFn]]." boxes any primitives passed to it and calls a closed over [[IFn]]."
[arg-types ret-type] [arg-types ret-type]
{:flags #{:public :final} {:flags #{:public :final}
:version 8
:fields [{:name "upcall_ifn" :fields [{:name "upcall_ifn"
:type IFn :type IFn
:flags #{:final}}] :flags #{:final}}]
@ -489,7 +483,7 @@
{:name :upcall {:name :upcall
:flags #{:public} :flags #{:public}
:desc (conj (mapv insn-layout arg-types) :desc (conj (mapv insn-layout arg-types)
(coerce-addressable (insn-layout ret-type))) (insn-layout ret-type))
:emit [[:aload 0] :emit [[:aload 0]
[:getfield :this "upcall_ifn" IFn] [:getfield :this "upcall_ifn" IFn]
(loop [types arg-types (loop [types arg-types
@ -505,7 +499,7 @@
inc))) inc)))
acc)) acc))
[:invokeinterface IFn "invoke" (repeat (inc (count arg-types)) Object)] [:invokeinterface IFn "invoke" (repeat (inc (count arg-types)) Object)]
(to-prim-asm (coerce-addressable ret-type)) (to-prim-asm ret-type)
[(return-for-type ret-type :areturn)]]}]}) [(return-for-type ret-type :areturn)]]}]})
(defn- upcall (defn- upcall
@ -518,7 +512,7 @@
([args] (method-type args ::mem/void)) ([args] (method-type args ::mem/void))
([args ret] ([args ret]
(MethodType/methodType (MethodType/methodType
^Class (coerce-addressable (mem/java-layout ret)) ^Class (mem/java-layout ret)
^"[Ljava.lang.Class;" (into-array Class (map mem/java-layout args))))) ^"[Ljava.lang.Class;" (into-array Class (map mem/java-layout args)))))
(defn- upcall-handle (defn- upcall-handle
@ -536,30 +530,30 @@
(defn- upcall-serde-wrapper (defn- upcall-serde-wrapper
"Creates a function that wraps `f` which deserializes the arguments and "Creates a function that wraps `f` which deserializes the arguments and
serializes the return type in the [[global-session]]." serializes the return type in the [[global-arena]]."
[f arg-types ret-type] [f arg-types ret-type]
(fn [& args] (fn [& args]
(mem/serialize (mem/serialize
(apply f (map mem/deserialize args arg-types)) (apply f (map mem/deserialize args arg-types))
ret-type ret-type
(mem/global-session)))) (mem/global-arena))))
(defmethod mem/serialize* ::fn (defmethod mem/serialize* ::fn
[f [_fn arg-types ret-type & {:keys [raw-fn?]}] session] [f [_fn arg-types ret-type & {:keys [raw-fn?]}] arena]
(.upcallStub (.upcallStub
(Linker/nativeLinker) (Linker/nativeLinker)
(cond-> f ^MethodHandle (cond-> f
(not raw-fn?) (upcall-serde-wrapper arg-types ret-type) (not raw-fn?) (upcall-serde-wrapper arg-types ret-type)
:always (upcall-handle arg-types ret-type)) :always (upcall-handle arg-types ret-type))
(function-descriptor arg-types ret-type) ^FunctionDescriptor (function-descriptor arg-types ret-type)
session)) ^Arena arena
(make-array Linker$Option 0)))
(defmethod mem/deserialize* ::fn (defmethod mem/deserialize* ::fn
[addr [_fn arg-types ret-type & {:keys [raw-fn?]}]] [addr [_fn arg-types ret-type & {:keys [raw-fn?]}]]
(when-not (mem/null? addr) (when-not (mem/null? addr)
(vary-meta (vary-meta
(-> addr (-> ^MemorySegment addr
(MemorySegment/ofAddress mem/pointer-size (mem/connected-session))
(downcall-handle (function-descriptor arg-types ret-type)) (downcall-handle (function-descriptor arg-types ret-type))
(downcall-fn arg-types ret-type) (downcall-fn arg-types ret-type)
(cond-> (not raw-fn?) (make-serde-wrapper arg-types ret-type))) (cond-> (not raw-fn?) (make-serde-wrapper arg-types ret-type)))
@ -614,7 +608,7 @@
(mem/serialize-into (mem/serialize-into
newval (.-type static-var) newval (.-type static-var)
(.-seg static-var) (.-seg static-var)
(mem/global-session)) (mem/global-arena))
newval) newval)
(defn fswap! (defn fswap!
@ -640,9 +634,8 @@
See [[freset!]], [[fswap!]]." See [[freset!]], [[fswap!]]."
[symbol-or-addr type] [symbol-or-addr type]
(StaticVariable. (mem/as-segment (.address (ensure-symbol symbol-or-addr)) (StaticVariable. (.reinterpret ^MemorySegment (ensure-symbol symbol-or-addr)
(mem/size-of type) ^long (mem/size-of type))
(mem/global-session))
type (atom nil))) type (atom nil)))
(defmacro defvar (defmacro defvar

View file

@ -24,7 +24,7 @@
(pos? r) (conj [::padding [::mem/padding (- align r)]]) (pos? r) (conj [::padding [::mem/padding (- align r)]])
:always (conj field)) :always (conj field))
fields)) fields))
(let [strongest-alignment (mem/align-of struct-spec) (let [strongest-alignment (reduce max (map (comp mem/align-of second) (nth struct-spec 1)))
r (rem offset strongest-alignment)] r (rem offset strongest-alignment)]
(cond-> aligned-fields (cond-> aligned-fields
(pos? r) (conj [::padding [::mem/padding (- strongest-alignment r)]])))))] (pos? r) (conj [::padding [::mem/padding (- strongest-alignment r)]])))))]

View file

@ -1,5 +1,5 @@
(ns coffi.mem (ns coffi.mem
"Functions for managing native allocations, memory sessions, and (de)serialization. "Functions for managing native allocations, memory arenas, and (de)serialization.
For any new type to be implemented, three multimethods must be overriden, but For any new type to be implemented, three multimethods must be overriden, but
which three depends on the native representation of the type. which three depends on the native representation of the type.
@ -13,21 +13,17 @@
struct, or array, then [[c-layout]] must be overriden to return the native struct, or array, then [[c-layout]] must be overriden to return the native
layout of the type, and [[serialize-into]] and [[deserialize-from]] should be layout of the type, and [[serialize-into]] and [[deserialize-from]] should be
overriden to allow marshaling values of the type into and out of memory overriden to allow marshaling values of the type into and out of memory
segments. segments."
When writing code that manipulates a segment, it's best practice to
use [[with-acquired]] on the [[segment-session]] in order to ensure it won't be
released during its manipulation."
(:require (:require
[clojure.set :as set] [clojure.set :as set]
[clojure.spec.alpha :as s]) [clojure.spec.alpha :as s])
(:import (:import
(java.lang.foreign (java.lang.foreign
Addressable AddressLayout
MemoryAddress Arena
MemoryLayout MemoryLayout
MemorySegment MemorySegment
MemorySession MemorySegment$Scope
SegmentAllocator SegmentAllocator
ValueLayout ValueLayout
ValueLayout$OfByte ValueLayout$OfByte
@ -36,126 +32,71 @@
ValueLayout$OfLong ValueLayout$OfLong
ValueLayout$OfChar ValueLayout$OfChar
ValueLayout$OfFloat ValueLayout$OfFloat
ValueLayout$OfDouble ValueLayout$OfDouble)
ValueLayout$OfAddress)
(java.lang.ref Cleaner) (java.lang.ref Cleaner)
(java.util.function Consumer)
(java.nio ByteOrder))) (java.nio ByteOrder)))
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
(defn stack-session (defn confined-arena
"Constructs a new session for use only in this thread. "Constructs a new arena for use only in this thread.
The memory allocated within this session is cheap to allocate, like a native The memory allocated within this arena is cheap to allocate, like a native
stack." stack.
(^MemorySession []
(MemorySession/openConfined))
(^MemorySession [^Cleaner cleaner]
(MemorySession/openConfined cleaner)))
(defn ^:deprecated stack-scope The memory allocated within this arena will be cleared once it is closed, so
"Constructs a new scope for use only in this thread. it is usually a good idea to create it in a [[with-open]] clause."
(^Arena []
(Arena/ofConfined)))
The memory allocated within this scope is cheap to allocate, like a native (defn shared-arena
stack." "Constructs a new shared memory arena.
^MemorySession []
(stack-session))
(defn shared-session This arena can be shared across threads and memory allocated in it will only
"Constructs a new shared memory session. be cleaned up once any thread accessing the arena closes it."
(^Arena []
(Arena/ofShared)))
This session can be shared across threads and memory allocated in it will only (defn auto-arena
be cleaned up once every thread accessing the session closes it." "Constructs a new memory arena that is managed by the garbage collector.
(^MemorySession []
(MemorySession/openShared))
(^MemorySession [^Cleaner cleaner]
(MemorySession/openShared cleaner)))
(defn ^:deprecated shared-scope The arena may be shared across threads, and all resources created with it will
"Constructs a new shared scope.
This scope can be shared across threads and memory allocated in it will only
be cleaned up once every thread accessing the scope closes it."
^MemorySession []
(shared-session))
(defn connected-session
"Constructs a new memory session to reclaim all connected resources at once.
The session may be shared across threads, and all resources created with it
will be cleaned up at the same time, when all references have been collected.
This type of session cannot be closed, and therefore should not be created in
a [[with-open]] clause."
^MemorySession []
(MemorySession/openImplicit))
(defn ^:deprecated connected-scope
"Constructs a new scope to reclaim all connected resources at once.
The scope may be shared across threads, and all resources created with it will
be cleaned up at the same time, when all references have been collected. be cleaned up at the same time, when all references have been collected.
This type of scope cannot be closed, and therefore should not be created in This type of arena cannot be closed, and therefore should not be created in
a [[with-open]] clause." a [[with-open]] clause."
^MemorySession [] ^Arena []
(connected-session)) (Arena/ofAuto))
(defn global-session (defn global-arena
"Constructs the global session, which will never reclaim its resources. "Constructs the global arena, which will never reclaim its resources.
This session may be shared across threads, but is intended mainly in cases This arena may be shared across threads, but is intended mainly in cases where
where memory is allocated with [[alloc]] but is either never freed or whose
management is relinquished to a native library, such as when returned from a
callback."
^MemorySession []
(MemorySession/global))
(defn ^:deprecated global-scope
"Constructs the global scope, which will never reclaim its resources.
This scope may be shared across threads, but is intended mainly in cases where
memory is allocated with [[alloc]] but is either never freed or whose memory is allocated with [[alloc]] but is either never freed or whose
management is relinquished to a native library, such as when returned from a management is relinquished to a native library, such as when returned from a
callback." callback."
^MemorySession [] ^Arena []
(global-session)) (Arena/global))
(defn session-allocator (defn arena-allocator
"Constructs a segment allocator from the given `session`. "Constructs a [[SegmentAllocator]] from the given [[Arena]].
This is primarily used when working with unwrapped downcall functions. When a This is primarily used when working with unwrapped downcall functions. When a
downcall function returns a non-primitive type, it must be provided with an downcall function returns a non-primitive type, it must be provided with an
allocator." allocator."
^SegmentAllocator [^MemorySession session] ^SegmentAllocator [^Arena arena]
(SegmentAllocator/newNativeArena session)) (reify SegmentAllocator
(^MemorySegment allocate [_this ^long byte-size ^long byte-alignment]
(defn ^:deprecated scope-allocator (.allocate arena ^long byte-size ^long byte-alignment))))
"Constructs a segment allocator from the given `scope`.
This is primarily used when working with unwrapped downcall functions. When a
downcall function returns a non-primitive type, it must be provided with an
allocator."
^SegmentAllocator [^MemorySession scope]
(session-allocator scope))
(defn segment-session
"Gets the memory session used to construct the `segment`."
^MemorySession [segment]
(.session ^MemorySegment segment))
(defn ^:deprecated segment-scope
"Gets the scope used to construct the `segment`."
^MemorySession [segment]
(segment-session segment))
(defn alloc (defn alloc
"Allocates `size` bytes. "Allocates `size` bytes.
If a `session` is provided, the allocation will be reclaimed when it is closed." If an `arena` is provided, the allocation will be reclaimed when it is closed."
(^MemorySegment [size] (alloc size (connected-session))) (^MemorySegment [size] (alloc size (auto-arena)))
(^MemorySegment [size session] (MemorySegment/allocateNative (long size) ^MemorySession session)) (^MemorySegment [size arena] (.allocate ^Arena arena (long size)))
(^MemorySegment [size alignment session] (MemorySegment/allocateNative (long size) (long alignment) ^MemorySession session))) (^MemorySegment [size alignment arena] (.allocate ^Arena arena (long size) (long alignment))))
(defn alloc-with (defn alloc-with
"Allocates `size` bytes using the `allocator`." "Allocates `size` bytes using the `allocator`."
@ -164,47 +105,24 @@
(^MemorySegment [allocator size alignment] (^MemorySegment [allocator size alignment]
(.allocate ^SegmentAllocator allocator (long size) (long alignment)))) (.allocate ^SegmentAllocator allocator (long size) (long alignment))))
(defmacro with-acquired
"Acquires one or more `sessions` until the `body` completes.
This is only necessary to do on shared sessions, however if you are operating
on an arbitrary passed session, it is best practice to wrap code that
interacts with it wrapped in this."
{:style/indent 1}
[sessions & body]
(if (seq sessions)
`(let [session# ~(first sessions)
res# (volatile! ::invalid-value)]
(.whileAlive
^MemorySession session#
(^:once fn* []
(vreset! res#
(with-acquired [~@(rest sessions)]
~@body))))
@res#)
`(do ~@body)))
(s/fdef with-acquired
:args (s/cat :sessions any?
:body (s/* any?)))
(defn address-of (defn address-of
"Gets the address of a given segment. "Gets the address of a given segment.
This value can be used as an argument to functions which take a pointer." This value can be used as an argument to functions which take a pointer."
^MemoryAddress [addressable] ^long [addressable]
(.address ^Addressable addressable)) (.address ^MemorySegment addressable))
(defn null? (defn null?
"Checks if a memory address is null." "Checks if a memory address is null."
[addr] [addr]
(or (.equals (MemoryAddress/NULL) addr) (not addr))) (or (.equals (MemorySegment/NULL) addr) (not addr)))
(defn address? (defn address?
"Checks if an object is a memory address. "Checks if an object is a memory address.
`nil` is considered an address." `nil` is considered an address."
[addr] [addr]
(or (nil? addr) (instance? MemoryAddress addr))) (or (nil? addr) (instance? MemorySegment addr)))
(defn slice (defn slice
"Get a slice over the `segment` with the given `offset`." "Get a slice over the `segment` with the given `offset`."
@ -213,46 +131,47 @@
(^MemorySegment [segment offset size] (^MemorySegment [segment offset size]
(.asSlice ^MemorySegment segment (long offset) (long size)))) (.asSlice ^MemorySegment segment (long offset) (long size))))
(defn slice-into (defn reinterpret
"Get a slice into the `segment` starting at the `address`." "Reinterprets the `segment` as having the passed `size`.
(^MemorySegment [address segment]
(.asSlice ^MemorySegment segment ^MemoryAddress address))
(^MemorySegment [address segment size]
(.asSlice ^MemorySegment segment ^MemoryAddress address (long size))))
(defn with-offset If `arena` is passed, the scope of the `segment` is associated with the arena,
"Get a new address `offset` from the old `address`." as well as its access constraints. If `cleanup` is passed, it will be a
^MemoryAddress [address offset] 1-argument function of a fresh memory segment backed by the same memory as the
(.addOffset ^MemoryAddress address (long offset))) returned segment which should perform any required cleanup operations. It will
be called when the `arena` is closed."
(defn add-close-action! (^MemorySegment [^MemorySegment segment size]
"Adds a 0-arity function to be run when the `session` closes." (.reinterpret segment (long size) (auto-arena) nil))
[^MemorySession session ^Runnable action] (^MemorySegment [^MemorySegment segment size ^Arena arena]
(.addCloseAction session action) (.reinterpret segment (long size) arena nil))
nil) (^MemorySegment [^MemorySegment segment size ^Arena arena cleanup]
(.reinterpret segment (long size) arena
(reify Consumer
(accept [_this segment]
(cleanup segment))))))
(defn as-segment (defn as-segment
"Dereferences an `address` into a memory segment associated with the `session`." "Dereferences an `address` into a memory segment associated with the `arena` (default global)."
(^MemorySegment [^MemoryAddress address size] (^MemorySegment [^long address]
(MemorySegment/ofAddress address (long size) (connected-session))) (MemorySegment/ofAddress address))
(^MemorySegment [^MemoryAddress address size session] (^MemorySegment [^long address size]
(MemorySegment/ofAddress address (long size) session))) (reinterpret (MemorySegment/ofAddress address) size))
(^MemorySegment [^long address size ^Arena arena]
(reinterpret (MemorySegment/ofAddress address) (long size) arena nil))
(^MemorySegment [^long address size ^Arena arena cleanup]
(reinterpret (MemorySegment/ofAddress address) (long size) arena cleanup)))
(defn copy-segment (defn copy-segment
"Copies the content to `dest` from `src`. "Copies the content to `dest` from `src`.
Returns `dest`." Returns `dest`."
^MemorySegment [^MemorySegment dest ^MemorySegment src] ^MemorySegment [^MemorySegment dest ^MemorySegment src]
(with-acquired [(segment-session src) (segment-session dest)] (.copyFrom dest src))
(.copyFrom dest src)
dest))
(defn clone-segment (defn clone-segment
"Clones the content of `segment` into a new segment of the same size." "Clones the content of `segment` into a new segment of the same size."
(^MemorySegment [segment] (clone-segment segment (connected-session))) (^MemorySegment [segment] (clone-segment segment (auto-arena)))
(^MemorySegment [^MemorySegment segment session] (^MemorySegment [^MemorySegment segment ^Arena arena]
(with-acquired [(segment-session segment) session] (copy-segment ^MemorySegment (alloc (.byteSize segment) arena) segment)))
(copy-segment ^MemorySegment (alloc (.byteSize segment) session) segment))))
(defn slice-segments (defn slice-segments
"Constructs a lazy seq of `size`-length memory segments, sliced from `segment`." "Constructs a lazy seq of `size`-length memory segments, sliced from `segment`."
@ -307,7 +226,7 @@
"The [[MemoryLayout]] for a c-sized double in [[native-endian]] [[ByteOrder]]." "The [[MemoryLayout]] for a c-sized double in [[native-endian]] [[ByteOrder]]."
ValueLayout/JAVA_DOUBLE) ValueLayout/JAVA_DOUBLE)
(def ^ValueLayout$OfAddress pointer-layout (def ^AddressLayout pointer-layout
"The [[MemoryLayout]] for a native pointer in [[native-endian]] [[ByteOrder]]." "The [[MemoryLayout]] for a native pointer in [[native-endian]] [[ByteOrder]]."
ValueLayout/ADDRESS) ValueLayout/ADDRESS)
@ -517,20 +436,20 @@
(.get segment (.withOrder ^ValueLayout$OfDouble double-layout byte-order) offset))) (.get segment (.withOrder ^ValueLayout$OfDouble double-layout byte-order) offset)))
(defn read-address (defn read-address
"Reads a [[MemoryAddress]] from the `segment`, at an optional `offset`." "Reads an address from the `segment`, at an optional `offset`, wrapped in a [[MemorySegment]]."
{:inline {:inline
(fn read-address-inline (fn read-address-inline
([segment] ([segment]
`(let [segment# ~segment] `(let [segment# ~segment]
(.get ^MemorySegment segment# ^ValueLayout$OfAddress pointer-layout 0))) (.get ^MemorySegment segment# ^AddressLayout pointer-layout 0)))
([segment offset] ([segment offset]
`(let [segment# ~segment `(let [segment# ~segment
offset# ~offset] offset# ~offset]
(.get ^MemorySegment segment# ^ValueLayout$OfAddress pointer-layout offset#))))} (.get ^MemorySegment segment# ^AddressLayout pointer-layout offset#))))}
(^MemoryAddress [^MemorySegment segment] (^MemorySegment [^MemorySegment segment]
(.get segment ^ValueLayout$OfAddress pointer-layout 0)) (.get segment ^AddressLayout pointer-layout 0))
(^MemoryAddress [^MemorySegment segment ^long offset] (^MemorySegment [^MemorySegment segment ^long offset]
(.get segment ^ValueLayout$OfAddress pointer-layout offset))) (.get segment ^AddressLayout pointer-layout offset)))
(defn write-byte (defn write-byte
"Writes a [[byte]] to the `segment`, at an optional `offset`." "Writes a [[byte]] to the `segment`, at an optional `offset`."
@ -715,22 +634,22 @@
(.set segment (.withOrder ^ValueLayout$OfDouble double-layout byte-order) offset value))) (.set segment (.withOrder ^ValueLayout$OfDouble double-layout byte-order) offset value)))
(defn write-address (defn write-address
"Writes a [[MemoryAddress]] to the `segment`, at an optional `offset`." "Writes the address of the [[MemorySegment]] `value` to the `segment`, at an optional `offset`."
{:inline {:inline
(fn write-address-inline (fn write-address-inline
([segment value] ([segment value]
`(let [segment# ~segment `(let [segment# ~segment
value# ~value] value# ~value]
(.set ^MemorySegment segment# ^ValueLayout$OfAddress pointer-layout 0 ^Addressable value#))) (.set ^MemorySegment segment# ^AddressLayout pointer-layout 0 ^MemorySegment value#)))
([segment offset value] ([segment offset value]
`(let [segment# ~segment `(let [segment# ~segment
offset# ~offset offset# ~offset
value# ~value] value# ~value]
(.set ^MemorySegment segment# ^ValueLayout$OfAddress pointer-layout offset# ^Addressable value#))))} (.set ^MemorySegment segment# ^AddressLayout pointer-layout offset# ^MemorySegment value#))))}
(^MemoryAddress [^MemorySegment segment ^MemoryAddress value] ([^MemorySegment segment ^MemorySegment value]
(.set segment ^ValueLayout$OfAddress pointer-layout 0 value)) (.set segment ^AddressLayout pointer-layout 0 value))
(^MemoryAddress [^MemorySegment segment ^long offset ^MemoryAddress value] ([^MemorySegment segment ^long offset ^MemorySegment value]
(.set segment ^ValueLayout$OfAddress pointer-layout offset value))) (.set segment ^AddressLayout pointer-layout offset value)))
(defn- type-dispatch (defn- type-dispatch
"Gets a type dispatch value from a (potentially composite) type." "Gets a type dispatch value from a (potentially composite) type."
@ -867,7 +786,7 @@
::char Byte/TYPE ::char Byte/TYPE
::float Float/TYPE ::float Float/TYPE
::double Double/TYPE ::double Double/TYPE
::pointer MemoryAddress ::pointer MemorySegment
::void Void/TYPE}) ::void Void/TYPE})
(defn java-layout (defn java-layout
@ -894,8 +813,8 @@
(defn alloc-instance (defn alloc-instance
"Allocates a memory segment for the given `type`." "Allocates a memory segment for the given `type`."
(^MemorySegment [type] (alloc-instance type (connected-session))) (^MemorySegment [type] (alloc-instance type (auto-arena)))
(^MemorySegment [type session] (MemorySegment/allocateNative ^long (size-of type) ^MemorySession session))) (^MemorySegment [type arena] (.allocate ^Arena arena ^long (size-of type) ^long (align-of type))))
(declare serialize serialize-into) (declare serialize serialize-into)
@ -903,136 +822,130 @@
"Constructs a serialized version of the `obj` and returns it. "Constructs a serialized version of the `obj` and returns it.
Any new allocations made during the serialization should be tied to the given Any new allocations made during the serialization should be tied to the given
`session`, except in extenuating circumstances. `arena`, except in extenuating circumstances.
This method should only be implemented for types that serialize to primitives." This method should only be implemented for types that serialize to primitives."
(fn (fn
#_{:clj-kondo/ignore [:unused-binding]} #_{:clj-kondo/ignore [:unused-binding]}
[obj type session] [obj type arena]
(type-dispatch type))) (type-dispatch type)))
(defmethod serialize* :default (defmethod serialize* :default
[obj type _session] [obj type _arena]
(throw (ex-info "Attempted to serialize a non-primitive type with primitive methods" (throw (ex-info "Attempted to serialize a non-primitive type with primitive methods"
{:type type {:type type
:object obj}))) :object obj})))
(defmethod serialize* ::byte (defmethod serialize* ::byte
[obj _type _session] [obj _type _arena]
(byte obj)) (byte obj))
(defmethod serialize* ::short (defmethod serialize* ::short
[obj _type _session] [obj _type _arena]
(short obj)) (short obj))
(defmethod serialize* ::int (defmethod serialize* ::int
[obj _type _session] [obj _type _arena]
(int obj)) (int obj))
(defmethod serialize* ::long (defmethod serialize* ::long
[obj _type _session] [obj _type _arena]
(long obj)) (long obj))
(defmethod serialize* ::char (defmethod serialize* ::char
[obj _type _session] [obj _type _arena]
(char obj)) (char obj))
(defmethod serialize* ::float (defmethod serialize* ::float
[obj _type _session] [obj _type _arena]
(float obj)) (float obj))
(defmethod serialize* ::double (defmethod serialize* ::double
[obj _type _session] [obj _type _arena]
(double obj)) (double obj))
(defmethod serialize* ::pointer (defmethod serialize* ::pointer
[obj type session] [obj type arena]
(if-not (null? obj) (if-not (null? obj)
(if (sequential? type) (if (sequential? type)
(with-acquired [session] (let [segment (alloc-instance (second type) arena)]
(let [segment (alloc-instance (second type) session)] (serialize-into obj (second type) segment arena)
(serialize-into obj (second type) segment session) (address-of segment))
(address-of segment)))
obj) obj)
(MemoryAddress/NULL))) (MemorySegment/NULL)))
(defmethod serialize* ::void (defmethod serialize* ::void
[_obj _type _session] [_obj _type _arena]
nil) nil)
(defmulti serialize-into (defmulti serialize-into
"Writes a serialized version of the `obj` to the given `segment`. "Writes a serialized version of the `obj` to the given `segment`.
Any new allocations made during the serialization should be tied to the given Any new allocations made during the serialization should be tied to the given
`session`, except in extenuating circumstances. `arena`, except in extenuating circumstances.
This method should be implemented for any type which does not This method should be implemented for any type which does not
override [[c-layout]]. override [[c-layout]].
For any other type, this will serialize it as [[serialize*]] before writing For any other type, this will serialize it as [[serialize*]] before writing
the result value into the `segment`. the result value into the `segment`."
Implementations of this should be inside a [[with-acquired]] block for the
`session` if they perform multiple memory operations."
(fn (fn
#_{:clj-kondo/ignore [:unused-binding]} #_{:clj-kondo/ignore [:unused-binding]}
[obj type segment session] [obj type segment arena]
(type-dispatch type))) (type-dispatch type)))
(defmethod serialize-into :default (defmethod serialize-into :default
[obj type segment session] [obj type segment arena]
(if-some [prim-layout (primitive-type type)] (if-some [prim-layout (primitive-type type)]
(with-acquired [(segment-session segment) session] (serialize-into (serialize* obj type arena) prim-layout segment arena)
(serialize-into (serialize* obj type session) prim-layout segment session))
(throw (ex-info "Attempted to serialize an object to a type that has not been overridden" (throw (ex-info "Attempted to serialize an object to a type that has not been overridden"
{:type type {:type type
:object obj})))) :object obj}))))
(defmethod serialize-into ::byte (defmethod serialize-into ::byte
[obj _type segment _session] [obj _type segment _arena]
(write-byte segment (byte obj))) (write-byte segment (byte obj)))
(defmethod serialize-into ::short (defmethod serialize-into ::short
[obj type segment _session] [obj type segment _arena]
(if (sequential? type) (if (sequential? type)
(write-short segment 0 (second type) (short obj)) (write-short segment 0 (second type) (short obj))
(write-short segment (short obj)))) (write-short segment (short obj))))
(defmethod serialize-into ::int (defmethod serialize-into ::int
[obj type segment _session] [obj type segment _arena]
(if (sequential? type) (if (sequential? type)
(write-int segment 0 (second type) (int obj)) (write-int segment 0 (second type) (int obj))
(write-int segment (int obj)))) (write-int segment (int obj))))
(defmethod serialize-into ::long (defmethod serialize-into ::long
[obj type segment _session] [obj type segment _arena]
(if (sequential? type) (if (sequential? type)
(write-long segment 0 (second type) (long obj)) (write-long segment 0 (second type) (long obj))
(write-long segment (long obj)))) (write-long segment (long obj))))
(defmethod serialize-into ::char (defmethod serialize-into ::char
[obj _type segment _session] [obj _type segment _arena]
(write-char segment (char obj))) (write-char segment (char obj)))
(defmethod serialize-into ::float (defmethod serialize-into ::float
[obj type segment _session] [obj type segment _arena]
(if (sequential? type) (if (sequential? type)
(write-float segment 0 (second type) (float obj)) (write-float segment 0 (second type) (float obj))
(write-float segment (float obj)))) (write-float segment (float obj))))
(defmethod serialize-into ::double (defmethod serialize-into ::double
[obj type segment _session] [obj type segment _arena]
(if (sequential? type) (if (sequential? type)
(write-double segment 0 (second type) (double obj)) (write-double segment 0 (second type) (double obj))
(write-double segment (double obj)))) (write-double segment (double obj))))
(defmethod serialize-into ::pointer (defmethod serialize-into ::pointer
[obj type segment session] [obj type segment arena]
(with-acquired [(segment-session segment) session]
(write-address (write-address
segment segment
(cond-> obj (cond-> obj
(sequential? type) (serialize* type session))))) (sequential? type) (serialize* type arena))))
(defn serialize (defn serialize
"Serializes an arbitrary type. "Serializes an arbitrary type.
@ -1040,12 +953,12 @@
For types which have a primitive representation, this serializes into that For types which have a primitive representation, this serializes into that
representation. For types which do not, it allocates a new segment and representation. For types which do not, it allocates a new segment and
serializes into that." serializes into that."
([obj type] (serialize obj type (connected-session))) ([obj type] (serialize obj type (auto-arena)))
([obj type session] ([obj type arena]
(if (primitive-type type) (if (primitive-type type)
(serialize* obj type session) (serialize* obj type arena)
(let [segment (alloc-instance type session)] (let [segment (alloc-instance type arena)]
(serialize-into obj type segment session) (serialize-into obj type segment arena)
segment)))) segment))))
(declare deserialize deserialize*) (declare deserialize deserialize*)
@ -1054,10 +967,7 @@
"Deserializes the given segment into a Clojure data structure. "Deserializes the given segment into a Clojure data structure.
For types that serialize to primitives, a default implementation will For types that serialize to primitives, a default implementation will
deserialize the primitive before calling [[deserialize*]]. deserialize the primitive before calling [[deserialize*]]."
Implementations of this should be inside a [[with-acquired]] block for the the
`segment`'s session if they perform multiple memory operations."
(fn (fn
#_{:clj-kondo/ignore [:unused-binding]} #_{:clj-kondo/ignore [:unused-binding]}
[segment type] [segment type]
@ -1113,9 +1023,8 @@
(defmethod deserialize-from ::pointer (defmethod deserialize-from ::pointer
[segment type] [segment type]
(with-acquired [(segment-session segment)]
(cond-> (read-address segment) (cond-> (read-address segment)
(sequential? type) (deserialize* type)))) (sequential? type) (deserialize* type)))
(defmulti deserialize* (defmulti deserialize*
"Deserializes a primitive object into a Clojure data structure. "Deserializes a primitive object into a Clojure data structure.
@ -1165,8 +1074,11 @@
[addr type] [addr type]
(when-not (null? addr) (when-not (null? addr)
(if (sequential? type) (if (sequential? type)
(deserialize-from (as-segment addr (size-of (second type))) (let [target-type (second type)]
(second type)) (deserialize-from
(.reinterpret ^MemorySegment (read-address addr)
^long (size-of target-type))
target-type))
addr))) addr)))
(defmethod deserialize* ::void (defmethod deserialize* ::void
@ -1188,8 +1100,7 @@
(defn seq-of (defn seq-of
"Constructs a lazy sequence of `type` elements deserialized from `segment`." "Constructs a lazy sequence of `type` elements deserialized from `segment`."
[type segment] [type segment]
(with-acquired [(segment-session segment)] (map #(deserialize % type) (slice-segments segment (size-of type))))
(map #(deserialize % type) (slice-segments segment (size-of type)))))
;;; Raw composite types ;;; Raw composite types
;; TODO(Joshua): Ensure that all the raw values don't have anything happen on ;; TODO(Joshua): Ensure that all the raw values don't have anything happen on
@ -1200,7 +1111,7 @@
(c-layout type)) (c-layout type))
(defmethod serialize-into ::raw (defmethod serialize-into ::raw
[obj _type segment _session] [obj _type segment _arena]
(if (instance? MemorySegment obj) (if (instance? MemorySegment obj)
(copy-segment segment obj) (copy-segment segment obj)
obj)) obj))
@ -1218,15 +1129,15 @@
::pointer) ::pointer)
(defmethod serialize* ::c-string (defmethod serialize* ::c-string
[obj _type session] [obj _type ^Arena arena]
(if obj (if obj
(address-of (.allocateUtf8String (session-allocator session) ^String obj)) (.allocateFrom arena ^String obj)
(MemoryAddress/NULL))) (MemorySegment/NULL)))
(defmethod deserialize* ::c-string (defmethod deserialize* ::c-string
[addr _type] [addr _type]
(when-not (null? addr) (when-not (null? addr)
(.getUtf8String ^MemoryAddress addr 0))) (.getString (.reinterpret ^MemorySegment addr Integer/MAX_VALUE) 0)))
;;; Union types ;;; Union types
@ -1237,7 +1148,7 @@
(into-array MemoryLayout items)))) (into-array MemoryLayout items))))
(defmethod serialize-into ::union (defmethod serialize-into ::union
[obj [_union _types & {:keys [dispatch extract]} :as type] segment session] [obj [_union _types & {:keys [dispatch extract]} :as type] segment arena]
(when-not dispatch (when-not dispatch
(throw (ex-info "Attempted to serialize a union with no dispatch function" (throw (ex-info "Attempted to serialize a union with no dispatch function"
{:type type {:type type
@ -1249,7 +1160,7 @@
obj) obj)
type type
segment segment
session))) arena)))
(defmethod deserialize-from ::union (defmethod deserialize-from ::union
[segment type] [segment type]
@ -1266,7 +1177,7 @@
(into-array MemoryLayout fields)))) (into-array MemoryLayout fields))))
(defmethod serialize-into ::struct (defmethod serialize-into ::struct
[obj [_struct fields] segment session] [obj [_struct fields] segment arena]
(loop [offset 0 (loop [offset 0
fields fields] fields fields]
(when (seq fields) (when (seq fields)
@ -1274,7 +1185,7 @@
size (size-of type)] size (size-of type)]
(serialize-into (serialize-into
(get obj field) type (get obj field) type
(slice segment offset size) session) (slice segment offset size) arena)
(recur (long (+ offset size)) (rest fields)))))) (recur (long (+ offset size)) (rest fields))))))
(defmethod deserialize-from ::struct (defmethod deserialize-from ::struct
@ -1297,10 +1208,10 @@
(defmethod c-layout ::padding (defmethod c-layout ::padding
[[_padding size]] [[_padding size]]
(MemoryLayout/paddingLayout (* 8 size))) (MemoryLayout/paddingLayout size))
(defmethod serialize-into ::padding (defmethod serialize-into ::padding
[_obj [_padding _size] _segment _session] [_obj [_padding _size] _segment _arena]
nil) nil)
(defmethod deserialize-from ::padding (defmethod deserialize-from ::padding
@ -1316,9 +1227,9 @@
(c-layout type))) (c-layout type)))
(defmethod serialize-into ::array (defmethod serialize-into ::array
[obj [_array type count] segment session] [obj [_array type count] segment arena]
(dorun (dorun
(map #(serialize-into %1 type %2 session) (map #(serialize-into %1 type %2 arena)
obj obj
(slice-segments (slice segment 0 (* count (size-of type))) (slice-segments (slice segment 0 (* count (size-of type)))
(size-of type))))) (size-of type)))))
@ -1363,10 +1274,10 @@
variants)))) variants))))
(defmethod serialize* ::enum (defmethod serialize* ::enum
[obj [_enum variants & {:keys [repr]}] session] [obj [_enum variants & {:keys [repr]}] arena]
(serialize* ((enum-variants-map variants) obj) (serialize* ((enum-variants-map variants) obj)
(or repr ::int) (or repr ::int)
session)) arena))
(defmethod deserialize* ::enum (defmethod deserialize* ::enum
[obj [_enum variants & {:keys [_repr]}]] [obj [_enum variants & {:keys [_repr]}]]
@ -1381,9 +1292,9 @@
::int)) ::int))
(defmethod serialize* ::flagset (defmethod serialize* ::flagset
[obj [_flagset bits & {:keys [repr]}] session] [obj [_flagset bits & {:keys [repr]}] arena]
(let [bits-map (enum-variants-map bits)] (let [bits-map (enum-variants-map bits)]
(reduce #(bit-set %1 (get bits-map %2)) (serialize* 0 (or repr ::int) session) obj))) (reduce #(bit-set %1 (get bits-map %2)) (serialize* 0 (or repr ::int) arena) obj)))
(defmethod deserialize* ::flagset (defmethod deserialize* ::flagset
[obj [_flagset bits & {:keys [repr]}]] [obj [_flagset bits & {:keys [repr]}]]
@ -1415,8 +1326,8 @@
[_type#] [_type#]
(primitive-type aliased#)) (primitive-type aliased#))
(defmethod serialize* ~new-type (defmethod serialize* ~new-type
[obj# _type# session#] [obj# _type# arena#]
(serialize* obj# aliased# session#)) (serialize* obj# aliased# arena#))
(defmethod deserialize* ~new-type (defmethod deserialize* ~new-type
[obj# _type#] [obj# _type#]
(deserialize* obj# aliased#))) (deserialize* obj# aliased#)))
@ -1425,8 +1336,8 @@
[_type#] [_type#]
(c-layout aliased#)) (c-layout aliased#))
(defmethod serialize-into ~new-type (defmethod serialize-into ~new-type
[obj# _type# segment# session#] [obj# _type# segment# arena#]
(serialize-into obj# aliased# segment# session#)) (serialize-into obj# aliased# segment# arena#))
(defmethod deserialize-from ~new-type (defmethod deserialize-from ~new-type
[segment# _type#] [segment# _type#]
(deserialize-from segment# aliased#))))) (deserialize-from segment# aliased#)))))

View file

@ -10,6 +10,8 @@ import java.lang.foreign.*;
*/ */
public class Loader { public class Loader {
static SymbolLookup lookup = Linker.nativeLinker().defaultLookup().or(SymbolLookup.loaderLookup());
/** /**
* Loads a library from a given absolute file path. * Loads a library from a given absolute file path.
* *
@ -37,7 +39,6 @@ public class Loader {
* @param symbol The name of the symbol to load from a library. * @param symbol The name of the symbol to load from a library.
*/ */
public static MemorySegment findSymbol(String symbol) { public static MemorySegment findSymbol(String symbol) {
return Linker.nativeLinker().defaultLookup().lookup(symbol) return lookup.find(symbol).orElse(null);
.orElseGet(() -> SymbolLookup.loaderLookup().lookup(symbol).orElse(null));
} }
} }

View file

@ -26,10 +26,18 @@ CString upcall_test(StringFactory fun) {
return fun(); return fun();
} }
int upcall_test2(int (*f)(void)) {
return f();
}
int counter = 0; int counter = 0;
static char* responses[] = { "Hello, world!", "Goodbye friend.", "co'oi prenu" }; static char* responses[] = { "Hello, world!", "Goodbye friend.", "co'oi prenu" };
char* upcall_test_int_fn_string_ret(int (*f)(void)) {
return responses[f()];
}
CString get_string1(void) { CString get_string1(void) {
return responses[counter++ % 3]; return responses[counter++ % 3];
} }
@ -63,3 +71,10 @@ AlignmentTest get_struct() {
return ret; return ret;
} }
void test_call_with_trailing_string_arg(int a, int b, char* text) {
printf("call of `test_call_with_trailing_string_arg` with a=%i b=%i text='%s'",1,2,text);
printf("\r ");
return;
}

View file

@ -29,8 +29,18 @@
(t/deftest can-make-upcall (t/deftest can-make-upcall
(t/is (= ((ffi/cfn "upcall_test" [[::ffi/fn [] ::mem/c-string]] ::mem/c-string) (t/is (= ((ffi/cfn "upcall_test" [[::ffi/fn [] ::mem/c-string]] ::mem/c-string)
(fn [] "hello")) (fn [] "hello from clojure from c from clojure"))
"hello"))) "hello from clojure from c from clojure")))
(t/deftest can-make-upcall2
(t/is (= ((ffi/cfn "upcall_test2" [[::ffi/fn [] ::mem/int]] ::mem/int)
(fn [] 5))
5)))
(t/deftest can-make-upcall-int-fn-string-ret
(t/is (= ((ffi/cfn "upcall_test_int_fn_string_ret" [[::ffi/fn [] ::mem/int]] ::mem/c-string)
(fn [] 2))
"co'oi prenu")))
(mem/defalias ::alignment-test (mem/defalias ::alignment-test
(layout/with-c-layout (layout/with-c-layout
@ -49,3 +59,12 @@
(ffi/freset! (ffi/static-variable "counter" ::mem/int) 1) (ffi/freset! (ffi/static-variable "counter" ::mem/int) 1)
(t/is (= ((ffi/cfn "get_string1" [] ::mem/c-string)) (t/is (= ((ffi/cfn "get_string1" [] ::mem/c-string))
"Goodbye friend."))) "Goodbye friend.")))
(t/deftest can-call-with-trailing-string-arg
(t/is
(=
((ffi/cfn "test_call_with_trailing_string_arg"
[::mem/int ::mem/int ::mem/c-string]
::mem/void)
1 2 "third arg"))))

View file

@ -0,0 +1,32 @@
(ns coffi.mem-test
(:require
[clojure.test :as t]
[coffi.ffi :as ffi]
[coffi.layout :as layout]
[coffi.mem :as mem])
(:import
(java.lang.foreign
AddressLayout
Arena
MemoryLayout
MemorySegment
MemorySegment$Scope
SegmentAllocator
ValueLayout
ValueLayout$OfByte
ValueLayout$OfShort
ValueLayout$OfInt
ValueLayout$OfLong
ValueLayout$OfChar
ValueLayout$OfFloat
ValueLayout$OfDouble)
(java.lang.ref Cleaner)
(java.nio ByteOrder)))
(ffi/load-library "target/ffi_test.so")
(t/deftest can-serialize-string
(t/is
(instance? MemorySegment (mem/serialize "this is a string" ::mem/c-string))))