From 96dfff04a0bfd88c17be9b56a0be7b6476e79a3d Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 4 Oct 2024 11:59:42 -0400 Subject: [PATCH 1/6] Remove reference to fixed "known issue" from readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a0bbbd2..c779202 100644 --- a/README.md +++ b/README.md @@ -1130,7 +1130,6 @@ release: ``` Unable to find static field: ACC_OPEN in interface org.objectweb.asm.Opcodes ``` -- Pointer wrapper types like `[::mem/pointer ::mem/int]` currently use one too many layers of indirection. This is fixed on develop. ## Future Plans These features are planned for future releases. From d0bfd20117d7f5a3c9d49cabd64f1bf95258474b Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 4 Oct 2024 16:00:57 -0400 Subject: [PATCH 2/6] Pare down the readme to an acceptable size --- README.md | 1054 ++--------------------------------------------------- 1 file changed, 29 insertions(+), 1025 deletions(-) diff --git a/README.md b/README.md index c779202..23831ac 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ on ease of use, including functions and macros for creating wrappers to allow the resulting native functions to act just like Clojure ones, however this doesn't remove the ability to write systems which minimize the cost of marshaling data and optimize for performance, to make use of the low-level -access Panama gives us. +access the FF&M API gives us. ## Installation -This library is available on Clojars. Add one of the following entries to the -`:deps` key of your `deps.edn`: +This library is available on Clojars, or as a git dependency. Add one of the +following entries to the `:deps` key of your `deps.edn`: ```clojure org.suskalo/coffi {:mvn/version "1.0.486"} @@ -56,7 +56,7 @@ 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. +your jar. See your build tool's documentation for how to add this. 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 @@ -68,14 +68,13 @@ $ clj-kondo --copy-configs --dependencies --lint "$(clojure -Spath)" ``` ## Usage -There are two major components to coffi and interacting with native code: -manipulating off-heap memory, and loading native code for use with Clojure. - -In the simplest cases, the native functions you call will work exclusively with -built-in types, for example the function `strlen` from libc. +The two main namespaces are `coffi.mem` which provides functions for allocating +and manipulating off-heap memory and (de)serializing values, and `coffi.ffi` +which can load native libraries, declare native function wrappers, and +(de)serialize functions as callbacks. ```clojure -(require '[coffi.mem :as mem :refer [defalias]]) +(require '[coffi.mem :as mem]) (require '[coffi.ffi :as ffi :refer [defcfn]]) (defcfn strlen @@ -84,657 +83,34 @@ built-in types, for example the function `strlen` from libc. (strlen "hello") ;; => 5 -``` -The first argument to `defcfn` is the name of the Clojure var that will hold the -native function reference, followed by an optional docstring and attribute map, -then the C function identifier, including the name of the native symbol, a -vector of argument types, and the return type. - -If you wish to use a native function as an anonymous function, it can be done -with the `cfn` function. - -```clojure -((ffi/cfn "strlen" [::mem/c-string] ::mem/long) "hello") -;; => 5 -``` - -If you want to use functions from libraries other than libc, then you'll need to -load them. Two functions are provided for this, `load-system-library`, and -`load-library`. `load-system-library` takes a string which represents the name -of a library that should be loaded via system lookup. - -```clojure (ffi/load-system-library "z") ``` -This will load libz from the appropriate place on the user's load path. - -Alternatively, `load-library` takes a file path to a dynamically loaded library. - -```clojure -(ffi/load-library "lib/libz.so") -``` - -This will load libz from the lib subdirectory of the current working directory. -As you can see this requires the entire filename, including platform-specific -file extensions. - -If a library is attempted to be loaded but doesn't exist or otherwise can't be -loaded, an exception is thrown. This can be convenient as any namespace with a -`load-library` call at the top level cannot be required without the library -being able to be loaded. - -### Primitive Types -Coffi defines a basic set of primitive types: -- byte -- short -- int -- long -- char -- float -- double -- pointer - -Each of these types maps to their C counterpart. Values of any of these -primitive types except for `pointer` will be cast with their corresponding -Clojure function when they are passed as arguments to native functions. -Additionally, the `c-string` type is defined, although it is not primitive. - -### Composite Types -In addition, some composite types are also defined in coffi, including struct -and union types (unions will be discussed with serialization and -deserialization). For an example C struct and function: - -```c -typedef struct point { - float x; - float y; -} Point; - -Point zero(void) { - Point res = {}; - - res.x = 0.0; - res.y = 0.0; - - return res; -} -``` - -The corresponding coffi definition is like so: - -```clojure -(defcfn zero-point - "zero" [] [::mem/struct [[:x ::mem/float] [:y ::mem/float]]]) - -(zero-point) -;; => {:x 0.0, -;; :y 0.0} -``` - -Writing out struct definitions like this every time would get tedious, so the -macro `defalias` is used to define a struct alias. - -```clojure -(defalias ::point - [::mem/struct - [[:x ::mem/float] - [:y ::mem/float]]]) - -(defcfn zero-point - "zero" [] ::point) -``` - -Struct definitions do not include any padding by default. Functions for -transforming struct types to include padding conforming to various standards can -be found in `coffi.layout`. - -``` clojure -(require '[coffi.layout :as layout]) - -(defalias ::needs-padding - (layout/with-c-layout - [::mem/struct - [[:a ::mem/char] - [:x ::mem/float]]])) - -(mem/size-of ::needs-padding) -;; => 8 - -(mem/align-of ::needs-padding) -;; => 4 -``` - -Values deserialized with types produced from layout functions may include an -extra `:coffi.layout/padding` key with a nil value. - -A limitation of the `defcfn` macro in its current form is that types provided to -it must be provided in a literal form, not as an expression that evaluates to a -type. This means that if you wish to use a layout function on a struct you must -define an alias for it before the type can be used as a type in `defcfn`. - -In cases where a pointer to some data is required to pass as an argument to a -native function, but doesn't need to be read back in, the `pointer` primitive -type can take a type argument. - -```clojure -[::mem/pointer ::mem/int] -``` - -Arrays are also supported via a type argument. Keep in mind that they are the -array itself, and not a pointer to the array like you might see in certain cases -in C. - -```clojure -[::mem/array ::mem/int 3] -``` - -### Callbacks -In addition to these composite types, there is also support for Clojure -functions. - -```clojure -[::ffi/fn [::mem/c-string] ::mem/int] -``` - -Be aware though that if an exception is thrown out of a callback that is called -from C, the JVM will crash. The resulting crash log should include the exception -type and message in the registers section, but it's important to be aware of all -the same. Ideally you should test your callbacks before actually passing them to -native code. - -When writing a wrapper library for a C library, it may be a good choice to wrap -all passed Clojure functions in an additional function which catches all -throwables, potentially notifies the user in some manner (e.g. logging), and -returns a default value. This is on the wrapper library's developer to decide -when and where this is appropriate, as in some cases no reasonable default -return value can be determined and it is most sensible to simply crash the JVM. -This is the reason that coffi defaults to this behavior, as in the author's -opinion it is better to fail hard and fast rather than to attempt to produce a -default and cause unexpected behavior later. - -Another important thing to keep in mind is the expected lifetime of the function -that you pass to native code. For example it is perfectly fine to pass an -anonymous function to a native function if the callback will never be called -again once the native function returns. If however it saves the callback for -later use the JVM may collect it prematurely, causing a crash when the callback -is later called by native code. - -### Variadic Functions -Some native functions can take any number of arguments, and in these cases coffi -provides `vacfn-factory` (for "varargs C function factory"). - -```clojure -(def printf-factory (ffi/vacfn-factory "printf" [::mem/c-string] ::mem/int)) -``` - -This returns a function of the types of the rest of the arguments which itself -returns a native function wrapper. - -```clojure -(def print-int (printf-factory ::mem/int)) - -(print-int "Some integer: %d\n" 5) -;; Some integer: 5 -``` - -At the moment there is no equivalent to `defcfn` for varargs functions. - -Some native functions that are variadic use the type `va_list` to make it easier -for other languages to call them in their FFI. At the time of writing, coffi -does not support va-list, however it is a planned feature. - -### Global Variables -Some libraries include global variables or constants accessible through symbols. -To start with, constant values stored in symbols can be fetched with `const`, or -the parallel macro `defconst` - -```clojure -(def some-const (ffi/const "some_const" ::mem/int)) -(ffi/defconst some-const "some_const" ::mem/int) -``` - -This value is fetched once when you call `const` and is turned into a Clojure -value. If you need to refer to a global variable, then `static-variable` (or -parallel `defvar`) can be used to create a reference to the native value. - -```clojure -(def some-var (ffi/static-variable "some_var" ::mem/int)) -(ffi/defvar some-var "some_var" ::mem/int) -``` - -This variable is an `IDeref`. Each time you dereference it, the value will be -deserialized from the native memory and returned. Additional functions are -provided for mutating the variable. - -```clojure -(ffi/freset! some-var 5) -;; => 5 -@some-var -;; => 5 -``` - -Be aware however that there is no synchronization on these types. The value -being read is not read atomically, so you may see an inconsistent state if the -value is being mutated on another thread. - -A parallel function `fswap!` is also provided, but it does not provide any -atomic semantics either. - -The memory that backs the static variable can be fetched with the function -`static-variable-segment`, which can be used to pass a pointer to the static -variable to native functions that require it. - -### Complex Wrappers -Some functions require more complex code to map nicely to a Clojure function. -The `defcfn` macro provides facilities to wrap the native function with some -Clojure code to make this easier. - -```clojure -(defcfn takes-array - "takes_array_with_count" [::mem/pointer ::mem/long] ::mem/void - native-fn - [ints] - (let [arr-len (count ints) - int-array (mem/serialize ints [::mem/array ::mem/int arr-len])] - (native-fn int-array arr-len))) -``` - -The symbol `native-fn` can be any unqualified symbol, and names the native -function being wrapped. It must be called in the function body below if you want -to call the native code. - -This `serialize` function has a paired `deserialize`, and allows marshaling -Clojure data back and forth to native data structures. - -This can be used to implement out variables often seen in native code. - -```clojure -(defcfn out-int - "out_int" [::mem/pointer] ::mem/void - native-fn - [i] - (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int])] - (native-fn int-ptr) - (mem/deserialize int-ptr [::mem/pointer ::mem/int]))) -``` - -### Arenas -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 -is allocated inside the JVM, the memory is associated with an arena. Because -none was provided here, the arena is an implicit arena, and the memory will be -freed when the serialized object is garbage collected. - -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 -well as changing allocation performance. Instead of an implicit arena, there -are other kinds of arenas as well. A `confined-arena` is a thread-local arena. -Confined arenas are `Closeable`, which means they should usually be used in a -`with-open` form. When a `confined-arena` is closed, it immediately frees all -the memory associated with it. The previous example, `out-int`, can be -implemented with a confined arena. - -```clojure -(defcfn out-int - "out_int" [::mem/pointer] ::mem/void - native-fn - [i] - (with-open [arena (mem/confined-arena)] - (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int] arena)] - (native-fn int-ptr) - (mem/deserialize int-ptr [::mem/pointer ::mem/int])))) -``` - -This will free the pointer immediately upon leaving the function. - -When memory needs to be accessible from multiple threads, there's -`shared-arena`. When a `shared-arena` is `.close`d, it will release all its -associated memory immediately, and so this should only be done once all other -threads are done accessing memory associated with it. - -In addition, two non-`Closeable` arenas are `global-arena`, which never frees -the resources associated with it, and `auto-arena`, which is an arena that frees -its resources once all of them are unreachable during a garbage collection -cycle, like an implicit arena, but potentially for multiple allocations rather -than just one. - -### Serialization and Deserialization -Custom serializers and deserializers may also be created. This is done using two -sets of three multimethods which can be extended by the user. For any given -type, only one set need be implemented. - -Two examples of custom types are given here, one is a 3d vector, and the other -an example of a tagged union. - -#### Vector3 -For the vector type, it will serialize to a pointer to an array of three floats. - -The multimethod `primitive-type` returns the primitive type that a given type -serializes to. For this example, it should be a pointer. - -```clojure -(defmethod mem/primitive-type ::vector - [_type] - ::mem/pointer) -``` - -For any type which doesn't serialize to a primitive, it returns nil, and -therefore need not be overriden. - -Next is `serialize*` and `deserialize*`, multimethods that work with types that -serialize to primitives. - -```clojure -(defmethod mem/serialize* ::vector - [obj _type arena] - (mem/serialize obj [::mem/array ::mem/float 3] arena)) - -(defmethod mem/deserialize* ::vector - [segment _type] - (mem/deserialize (mem/reinterpret segment (mem/size-of [::mem/array ::mem/float 3])) - [::mem/array ::mem/float 3])) -``` - -The `reinterpret` function allows you to take a segment and decorate it with a -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 arena of the pointer, we could use -`reinterpret` to ensure it's freed. For example if a `free-vector!` function -that takes a pointer exists, we could use this: - -```clojure -(defcfn returns-vector - "returns_vector" [] ::mem/pointer - native-fn - [arena] - (let [ret-ptr (native-fn)] - (-> (reinterpret ret-ptr (mem/size-of ::vector) arena free-vector!) - (deserialize ::vector)))) -``` - -This function takes an arena and returns the deserialized vector, and it will -free the pointer when the arena closes. - -#### Tagged Union -For the tagged union type, we will represent the value as a vector of a keyword -naming the tag and the value. The type itself will need to take arguments, -similar to `struct`. For example, if we were to represent a result type like in -Rust, we might have the following values: - -```clojure -[:ok 5] -[:err "Invalid number format"] -``` - -To represent this, we can have a `tagged-union` type. For this instance of the -result type, it may look like this: - -```clojure -[::tagged-union [:ok :err] {:ok ::mem/int :err ::mem/c-string}] -``` - -The native representation of these objects is a struct of the tag and a union of -the value. In order to correctly serialize the data and pass it to native code, -we need a representation of the native layout of the data. The `c-layout` -multimethod provides that. - -```clojure -(defmethod mem/c-layout ::tagged-union - [[_tagged-union tags type-map]] - (mem/c-layout [::mem/struct - [[:tag ::mem/long] - [:value [::mem/union (vals type-map)]]]])) -``` - -Types with type arguments are represented as vectors of the type name and any -additional arguments. The type name is what is dispatched on for the -multimethods. - -Now that we have a native layout, we need to be able to serialize and -deserialize the value into and out of memory segments. This is accomplished with -`serialize-into` and `deserialize-from`. - -```clojure -(defn item-index - "Gets the index of the first occurance of `item` in `coll`." - [coll item] - (first - (->> coll - (map-indexed vector) - (filter (comp #{item} second)) - (map first)))) - -(defmethod mem/serialize-into ::tagged-union - [obj [_tagged-union tags type-map] segment arena] - (mem/serialize-into - {:tag (item-index tags (first obj)) - :value (second obj)} - [::mem/struct - [[:tag ::mem/long] - [:value (get type-map (first obj))]]] - segment - arena)) -``` - -This serialization method is rather simple, it just turns the vector value into -a map, and serializes it as a struct, choosing the type of the value based on -the tag. - -```clojure -(defmethod mem/deserialize-from ::tagged-union - [segment [_tagged-union tags type-map]] - (let [tag (mem/deserialize-from segment ::mem/long)] - [(nth tags tag) - (mem/deserialize-from - (mem/slice segment (mem/size-of ::mem/long)) - (get type-map tag))])) -``` - -Deserialization is a little more complex. First the tag is retrieved from the -beginning of the segment, and then the type of the value is decided based on -that before it is deserialized. - -### Unions -In the last section the custom serialization and deserialization of a tagged -union used a union from coffi in order to define the native layout, but not for -actual serialization or deserialization. This is intentional. A union in coffi -is rather limited. It can be serialized, but not deserialized without external -information. - -```clojure -[::mem/union - #{::mem/float ::mem/double} - :dispatch #(cond - (float? %) ::mem/float - (double? %) ::mem/double)] -``` - -This is a minimal union in coffi. If the `:dispatch` keyword argument is not -passed, then the union cannot be serialized, as coffi would not know which type -to serialize the values as. In the example with a tagged union, a dispatch -function was not provided because the type was only used for the native layout. - -In addition to a dispatch function, when serializing a union an extract function -may also be provided. In the case of the value in the tagged union from before, -it could be represented for serialization purposes like so: - -```clojure -[::mem/union - #{::mem/int ::mem/c-string} - :dispatch #(case (first %) - :ok ::mem/int - :err ::mem/c-string) - :extract second] -``` - -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 -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 -to the user to call `deserialize-from` on that segment with the appropriate -type. - -### Unwrapped Native Handles -Some native libraries work with handles to large amounts of data at once, making -it undesirable to marshal data back and forth from Clojure, both because it's -not necessary to work with the data in Clojure directly, or also because of the -high (de)serialization costs associated with marshaling. In cases like these, -unwrapped native handles are desirable. - -The functions `make-downcall` and `make-varargs-factory` are also provided to -create raw function handles. - -```clojure -(def raw-strlen (ffi/make-downcall "strlen" [::mem/c-string] ::mem/long)) -(raw-strlen (mem/serialize "hello" ::mem/c-string)) -;; => 5 -``` - -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 -pointers, that is `MemorySegment`, and for composite types like structs and -unions, that is also `MemorySegment`. `MemorySegment` comes from the -`java.lang.foreign` package. - -In addition, when a raw handle returns a composite type represented with a -`MemorySegment`, it requires an additional first argument, a `SegmentAllocator`, -which can be acquired with `arena-allocator` to get one associated with a -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 -manner: - -```clojure -[::ffi/fn [::mem/int] ::mem/int :raw-fn? true] -``` - -Clojure functions serialized to this type will have their arguments and return -value exactly match the types specified and will not perform any serialization -or deserialization at their boundaries. - -One important caveat to consider when writing wrappers for performance-sensitive -functions is that the convenience macro `defcfn` that coffi provides will -already perform no serialization or deserialization on primitive arguments and -return types, so for functions with only primitive argument and return types -there is no performance reason to choose unwrapped native handles over the -convenience macro. - -### Manual (De)Serialization -Coffi uses multimethods to dispatch to (de)serialization functions to enable -code that's generic over the types it operates on. However, in cases where you -know the exact types that you will be (de)serializing and the multimethod -dispatch overhead is too high a cost, it may be appropriate to manually handle -(de)serializing data. This will often be done paired with [Unwrapped Native -Handles](#unwrapped-native-handles). - -Convenience functions are provided to both read and write all primitive types -and addresses, including byte order. - -As an example, when wrapping a function that returns an array of big-endian -floats, the following code might be used. - -``` clojure -;; int returns_float_array(float **arr) -(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)) - -(defn returns-float-array - [] - (with-open [arena (mem/confined-arena)] - ;; float *out_floats; - ;; int num_floats = returns_float_array(&out_floats); - (let [out-floats (mem/alloc mem/pointer-size arena) - num-floats (returns-float-array* out-floats) - floats-addr (mem/read-address out-floats) - floats-slice (mem/reinterpret floats-addr (unchecked-multiply-int mem/float-size num-floats))] - ;; Using a try/finally to perform an operation when the stack frame exits, - ;; but not to try to catch anything. - (try - (loop [floats (transient []) - index 0] - (if (>= index num-floats) - (persistent! floats) - (recur (conj! floats (mem/read-float floats-slice - (unchecked-multiply-int index mem/float-size) - mem/big-endian)) - (unchecked-inc-int index)))) - (finally - (release-floats* floats-addr)))))) -``` - -The above code manually performs all memory operations rather than relying on -coffi's dispatch. This will be more performant, but because multimethod overhead -is usually relatively low, it's recommended to use the multimethod variants for -convenience in colder functions. - -### Data Model -In addition to the macros and functions provided to build a Clojure API for -native libraries, facilities are provided for taking data and loading all the -symbols specified by it. This can be useful if a library provides (or an -external provider maintains) a data representation of their API, as Clojure data -to represent it may be programmatically generated from these sources. - -The data to represent an API is a map with the following form: - -```clojure -(def strlen-libspec - {:strlen {:type :function - :symbol "strlen" - :function/args [::mem/c-string] - :function/ret ::mem/long}}) -``` - -Each key in this map represents a single symbol to be loaded. The value is a map -with at least the keys `:type` and `:symbol`. These are the currently recognized -types: - -- function -- varargs-factory -- const -- static-var - -Each one has its own set of additional keys which can be added to the map. Both -`function` and `varargs-factory` have the three keys `:function/args`, -`:function/ret`, and `:function/raw-fn?`. The `const` type has `:const/type` and -`static-var` has `:static-var/type`. - -This data can be passed to the function `reify-libspec`, which will take the -data and return a map from the same keys as the input map to whatever value is -appropriate for a given symbol type (e.g. a Clojure function for `function`, a -value for `const`, etc.). - -```clojure -(ffi/reify-libspec strlen-libspec) -;; => {:strlen #function[...]} -``` - -This functionality can be extended by specifying new types as implementations of -the multimethod `reify-symbolspec`, although it's recommended that for any -library authors who do so, namespaced keywords be used to name types. +In the `coffi.mem` namespace there are types for all the signed primitive +numeric types in C, plus `::mem/pointer` and `::mem/c-string`, and ways to use +malli-like type declarations to define structs, unions, arrays, enums, and +flagsets. ## 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 -addition the following libraries exist: +addition the following libraries (among others) exist: - [dtype-next](https://github.com/cnuernber/dtype-next) - [tech.jna](https://github.com/techascent/tech.jna) - [clojure-jna](https://github.com/Chouser/clojure-jna) -Dtype-next has support for Java versions 8-16 and GraalVM, but is focused +Dtype-next has support for Java versions 8-15, 17+, and GraalVM, but is focused strongly on array-based programming, as well as being focused on keeping memory in the native side rather than marshaling data to and from Clojure-native -structures. In Java 16, this uses the first iteration of Panama, while in other -Java versions it uses JNA. +structures. In Java 17+, this uses the Foreign Function & Memory API (a part of +Project Panama until stabilization in JDK 22), while in other Java versions it +uses JNA. Tech.jna and clojure-jna both use the JNA library in all cases, and neither -provide support for dealing with struct types or callbacks. +provide explicit support for callbacks. JNA allows the use of +`java.nio.ByteBuffer`s to pass structs by value, and both libraries provide ways +to use this by-value construction to call by-reference apis. An additional alternative to coffi is to directly use the JNI, which is the longest-standing method of wrapping native code in the JVM, but comes with the @@ -742,385 +118,13 @@ downside that it requires you to write both native and Java code to use, even if you only intend to use it from Clojure. If your application needs to be able to run in earlier versions of the JVM than -17, or you don't want to use incubator functionality, you should consider these -other options. Dtype-next provides the most robust support for native code, but -if you are wrapping a simple library then the other libraries may be more -appealing, as they have a smaller API surface area and it's easier to wrap -functions. +22, you should consider these other options. Dtype-next provides the most robust +support for native code, but if you are wrapping a simple library then the other +libraries may be more appealing, as they have a smaller API surface area and +it's easier to wrap functions. -### Benchmarks -**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 -of each available option. It's an established fact that JNA (used by all three -alternative libraries on JDK <16) introduces more overhead when calling native -code than JNI does. - -In order to provide a benchmark to see how much of a difference the different -native interfaces make, we can use -[criterium](https://github.com/hugoduncan/criterium) to benchmark each. -[GLFW](https://www.glfw.org)'s -[`glfwGetTime`](https://www.glfw.org/docs/latest/group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a) -function will be used for the test as it performs a simple operation, and is -conveniently already wrapped in JNI by the excellent -[LWJGL](https://www.lwjgl.org/) library. - -The following benchmarks were run on a Lenovo Thinkpad with an Intel i7-10610U -running Manjaro Linux, using Clojure 1.10.3 on Java 17. - -#### JNI -The baseline for performance is the JNI. Using LWJGL it's relatively simple to -benchmark. The following Clojure CLI command will start a repl with LWJGL and -criterium loaded. - -```sh -$ clj -Sdeps '{:deps {org.lwjgl/lwjgl {:mvn/version "3.2.3"} - org.lwjgl/lwjgl-glfw {:mvn/version "3.2.3"} - org.lwjgl/lwjgl$natives-linux {:mvn/version "3.2.3"} - org.lwjgl/lwjgl-glfw$natives-linux {:mvn/version "3.2.3"} - criterium/criterium {:mvn/version "0.4.6"}}}' -``` - -Then from the repl - -```clojure -user=> (import 'org.lwjgl.glfw.GLFW) -org.lwjgl.glfw.GLFW -user=> (require '[criterium.core :as bench]) -nil -user=> (GLFW/glfwInit) -true -user=> (bench/bench (GLFW/glfwGetTime) :verbose) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 17+35-2724 -Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2667074721.basis -Evaluation count : 1613349900 in 60 samples of 26889165 calls. - Execution time sample mean : 32.698446 ns - Execution time mean : 32.697811 ns -Execution time sample std-deviation : 1.274600 ns - Execution time std-deviation : 1.276437 ns - Execution time lower quantile : 30.750813 ns ( 2.5%) - Execution time upper quantile : 33.757662 ns (97.5%) - Overhead used : 6.400704 ns -nil -``` - -GLFW requires that we initialize it before calling the `glfwGetTime` function. -Besides that this is a simple interop call which directly maps to the native -function. - -This gives us a basis of 32.7 ns +/-1.3 ns. All other libraries will be -evaluated relative to this result. - -To ensure fairness, we'll also get that overhead value to be used in further -tests. - -```clojure -user=> bench/estimated-overhead-cache -6.400703613065185E-9 -``` - -#### Coffi -The dependencies when using coffi are simpler, but it also requires some JVM -options to support the foreign access api. - -```sh -$ clj -Sdeps '{:deps {org.suskalo/coffi {:mvn/version "0.1.205"} - criterium/criterium {:mvn/version "0.4.6"}}}' \ - -J--add-modules=jdk.incubator.foreign \ - -J--enable-native-access=ALL-UNNAMED -``` - -In order to ensure fair comparisons, we're going to use the same overhead value -on each run, so before we do the benchmark we'll set it to the observed value -from last time. - -```clojure -user=> (require '[criterium.core :as bench]) -nil -user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) -6.400703613065185E-9 -user=> (require '[coffi.ffi :as ffi]) -nil -user=> (require '[coffi.mem :as mem]) -nil -user=> (ffi/load-system-library "glfw") -nil -user=> ((ffi/cfn "glfwInit" [] ::mem/int)) -1 -user=> (let [f (ffi/cfn "glfwGetTime" [] ::mem/double)] - (bench/bench (f) :verbose)) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 17+35-2724 -Runtime arguments: --add-modules=jdk.incubator.foreign --enable-native-access=ALL-UNNAMED -Dclojure.basis=/home/jsusk/.clojure/.cpcache/72793624.basis -Evaluation count : 1657995600 in 60 samples of 27633260 calls. - Execution time sample mean : 31.382665 ns - Execution time mean : 31.386493 ns -Execution time sample std-deviation : 1.598571 ns - Execution time std-deviation : 1.608818 ns - Execution time lower quantile : 29.761194 ns ( 2.5%) - Execution time upper quantile : 33.228276 ns (97.5%) - Overhead used : 6.400704 ns -nil -``` - -This result is about 1.3 ns faster, and while that is less than the standard -deviation of 1.6, it's quite close to it. - -#### Clojure-JNA -Clojure-JNA uses the JNA library, which was designed to provide Java with an -easy way to access native libraries, but which is known for not having the -greatest performance. Since this is an older project, I'm also including the -clojure dependency to ensure the correct version is used. - -```sh -$ clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.10.3"} - net.n01se/clojure-jna {:mvn/version "1.0.0"} - criterium/criterium {:mvn/version "0.4.6"}}}' -``` - -The naive way to call the function using Clojure-JNA is to use `jna/invoke`. - -```clojure -user=> (require '[criterium.core :as bench]) -nil -user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) -6.400703613065185E-9 -user=> (require '[net.n01se.clojure-jna :as jna]) -nil -user=> (jna/invoke Integer glfw/glfwInit) -1 -user=> (bench/bench (jna/invoke Double glfw/glfwGetTime) :verbose) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 17+35-2724 -Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/3229486237.basis -Evaluation count : 195948720 in 60 samples of 3265812 calls. - Execution time sample mean : 350.335614 ns - Execution time mean : 350.373520 ns -Execution time sample std-deviation : 24.833070 ns - Execution time std-deviation : 24.755929 ns - Execution time lower quantile : 300.000019 ns ( 2.5%) - Execution time upper quantile : 365.759273 ns (97.5%) - Overhead used : 6.400704 ns - -Found 13 outliers in 60 samples (21.6667 %) - low-severe 12 (20.0000 %) - low-mild 1 (1.6667 %) - Variance from outliers : 53.4220 % Variance is severely inflated by outliers -nil -``` - -As you can see, this method of calling functions is very bad for performance, -with call overhead dominating function runtime by an order of magnitude. That -said, this isn't a completely fair comparison, nor the most realistic, because -this way of calling functions looks the function up on each invocation. - -To adjust for this, we'll use the `jna/to-fn` function to give a persistent -handle to the function that we can call. - -```clojure -user=> (let [f (jna/to-fn Double glfw/glfwGetTime)] - (bench/bench (f) :verbose)) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 17+35-2724 -Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/3229486237.basis -Evaluation count : 611095020 in 60 samples of 10184917 calls. - Execution time sample mean : 104.623634 ns - Execution time mean : 104.638406 ns -Execution time sample std-deviation : 7.649296 ns - Execution time std-deviation : 7.638963 ns - Execution time lower quantile : 92.446016 ns ( 2.5%) - Execution time upper quantile : 110.258832 ns (97.5%) - Overhead used : 6.400704 ns -nil -``` - -This is much better, but is still about 3x slower than JNI, meaning the overhead -from using JNA is still bigger than the function runtime. - -This performance penalty is still small in the scope of longer-running -functions, and so may not be a concern for your application, but it is something -to be aware of. - -#### tech.jna -The tech.jna library is similar in scope to Clojure-JNA, however was written to -fit into an ecosystem of libraries meant for array-based programming for machine -learning and data science. - -```sh -$ clj -Sdeps '{:deps {techascent/tech.jna {:mvn/version "4.05"} - criterium/criterium {:mvn/version "0.4.6"}}}' -``` - -This library is also quite simple to use, the only slightly odd thing I'm doing -here is to dereference the var outside the benchmark in order to ensure it's an -apples-to-apples comparison. We don't want var dereference time mucking up our -benchmark. - -```clojure -user=> (require '[criterium.core :as bench]) -nil -user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) -6.400703613065185E-9 -user=> (require '[tech.v3.jna :as jna]) -nil -user=> (jna/def-jna-fn "glfw" glfwInit "initialize glfw" Integer) -#'user/glfwInit -user=> (glfwInit) -Oct 09, 2021 10:30:50 AM clojure.tools.logging$eval1122$fn__1125 invoke -INFO: Library glfw found at [:system "glfw"] -1 -user=> (jna/def-jna-fn "glfw" glfwGetTime "gets the time as a double since init" Double) -#'user/glfwGetTime -user=> (let [f @#'glfwGetTime] - (bench/bench (f) :verbose)) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 17+35-2724 -Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2910209237.basis -Evaluation count : 323281680 in 60 samples of 5388028 calls. - Execution time sample mean : 203.976803 ns - Execution time mean : 203.818712 ns -Execution time sample std-deviation : 14.557312 ns - Execution time std-deviation : 14.614080 ns - Execution time lower quantile : 179.732593 ns ( 2.5%) - Execution time upper quantile : 213.929374 ns (97.5%) - Overhead used : 6.400704 ns -nil -``` - -This version is even slower than Clojure-JNA. I'm unsure where this overhead is -coming from, but I'll admit that I haven't looked at their implementations very -closely. - -#### dtype-next -The library dtype-next replaced tech.jna in the toolkit of the group working on -machine learning and array-based programming, and it includes support for -composite data types including structs, as well as primitive functions and -callbacks. - -In addition, dtype-next has two different ffi backends. First is JNA, which is -usable on any JDK version, and is what we'll use for the first benchmark. Second -is the Java 16 version of Project Panama, which will be shown next. - -In order to use the dtype-next ffi with the JNA backend, the JNA library has to -be included in the dependencies. - -```sh -$ clj -Sdeps '{:deps {cnuernber/dtype-next {:mvn/version "8.032"} - net.java.dev.jna/jna {:mvn/version "5.8.0"} - criterium/criterium {:mvn/version "0.4.6"}}}' -``` - -The dtype-next library also requires some more ceremony around declaring native -functions. One advantage this has is that multiple symbols with the same name -can be loaded from different shared libraries, but it also does increase -friction when defining native wrappers. - -Some easier ways to define native wrappers are provided than what is seen here, -but they share some disadvantages in documentation over the core methods -provided in coffi, although they are comparable to the data model provided in -coffi. - -```clojure -user=> (require '[criterium.core :as bench]) -nil -user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) -6.400703613065185E-9 -user=> (require '[tech.v3.datatype.ffi :as dt-ffi]) -nil -user=> (def fn-defs {:glfwInit {:rettype :int32} :glfwGetTime {:rettype :float64}}) -#'user/fn-defs -user=> (def library-def (dt-ffi/define-library fn-defs)) -#'user/library-def -user=> (def library-instance (dt-ffi/instantiate-library library-def "/usr/lib/libglfw.so")) -#'user/library-instance -user=> (def init (:glfwInit @library-instance)) -#'user/init -user=> (init) -1 -user=> (let [f (:glfwGetTime @library-instance)] - (bench/bench (f) :verbose)) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 17+35-2724 -Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/643862289.basis -Evaluation count : 710822100 in 60 samples of 11847035 calls. - Execution time sample mean : 90.900112 ns - Execution time mean : 90.919917 ns -Execution time sample std-deviation : 6.463312 ns - Execution time std-deviation : 6.470108 ns - Execution time lower quantile : 79.817126 ns ( 2.5%) - Execution time upper quantile : 95.454652 ns (97.5%) - Overhead used : 6.400704 ns -nil -``` - -This version of JNA usage is significantly faster than either of the other JNA -libraries, but is still substantially slower than using JNI or coffi. - -In addition to the JNA backend, dtype-next has a Java 16-specific backend that -uses an older version of Panama. This version requires similar setup to coffi in -order to run. - -```sh -$ clj -Sdeps '{:deps {cnuernber/dtype-next {:mvn/version "8.032"} - criterium/criterium {:mvn/version "0.4.6"}}}' \ - -J--add-modules=jdk.incubator.foreign \ - -J-Dforeign.restricted=permit \ - -J--add-opens=java.base/java.lang=ALL-UNNAMED \ - -J-Djava.library.path=/usr/lib/x86_64-linux-gnu -``` - -The actual code to run the benchmark is identical to the last example, but is -reproduced here for completeness. - -```clojure -user=> (require '[criterium.core :as bench]) -nil -user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) -6.400703613065185E-9 -user=> (require '[tech.v3.datatype.ffi :as dt-ffi]) -nil -user=> (def fn-defs {:glfwInit {:rettype :int32} :glfwGetTime {:rettype :float64}}) -#'user/fn-defs -user=> (def library-def (dt-ffi/define-library fn-defs)) -#'user/library-def -user=> (def library-instance (dt-ffi/instantiate-library library-def "/usr/lib/libglfw.so")) -#'user/library-instance -user=> (def init (:glfwInit @library-instance)) -#'user/init -user=> (init) -1 -user=> (let [f (:glfwGetTime @library-instance)] - (bench/bench (f) :verbose)) -amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) -OpenJDK 64-Bit Server VM 16.0.2+7 -Runtime arguments: --add-modules=jdk.incubator.foreign -Dforeign.restricted=permit --add-opens=java.base/java.lang=ALL-UNNAMED -Djava.library.path=/usr/lib/x86_64-linux-gnu -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2337051659.basis -Evaluation count : 1588513080 in 60 samples of 26475218 calls. - Execution time sample mean : 58.732468 ns - Execution time mean : 58.647361 ns -Execution time sample std-deviation : 9.732389 ns - Execution time std-deviation : 9.791738 ns - Execution time lower quantile : 31.318115 ns ( 2.5%) - Execution time upper quantile : 65.449222 ns (97.5%) - Overhead used : 6.400704 ns - -Found 14 outliers in 60 samples (23.3333 %) - low-severe 8 (13.3333 %) - low-mild 4 (6.6667 %) - high-mild 2 (3.3333 %) - Variance from outliers : 87.6044 % Variance is severely inflated by outliers -nil -``` - -Not reproduced here, but notable for comparison, in my testing Java 16's version -of the JNI version performed about the same. - -This is significantly faster than the JNA version of dtype-next, but it is still -slower than modern Panama. This is likely to simply be a result of optimizations -and changes to the Panama API, and when dtype-next is updated to use the Java 17 -version of Panama I expect it will perform in line with coffi, but this -benchmark will be reproduced when this happens. Still, this shows that as it -stands, coffi is the fastest FFI available to Clojure developers. +There is also a [third party round up](https://docs.google.com/spreadsheets/d/1ViLHNUgrO2osh2AH0h7MaCaXz8g0UpLbyWojY5f10kk/edit?gid=332155605#gid=332155605) +of FFI options for Clojure. ## Known Issues The project author is aware of these issues and plans to fix them in a future @@ -1135,14 +139,14 @@ release: These features are planned for future releases. - Support for va_args type -- Header parsing tool for generating a data model? +- Header parsing tool for generating a data model? (maybe just work with [clong](https://github.com/phronmophobic/clong)?) - Generic type aliases - Unsigned integer types - Record-based struct types - Helper macro for out arguments - Improve error messages from defcfn macro - Mapped memory -- Helper macros for custom serde implementations for composite data types +- Helper macros for custom serde implementations for composite data types (this is in progress [for structs](https://github.com/IGJoshua/coffi/issues/12)!) ## License From 4b490bf24f190f7282fa07e8d839898c42242328 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 4 Oct 2024 16:11:21 -0400 Subject: [PATCH 3/6] Add Getting Started article --- README.md | 6 +- deps.edn | 1 + docs/01-Getting-Started.html | 147 +++++++++++++ docs/articles/01-Getting-Started.md | 309 ++++++++++++++++++++++++++++ docs/coffi.ffi.html | 42 ++-- docs/coffi.layout.html | 4 +- docs/coffi.mem.html | 148 ++++++------- docs/index.html | 2 +- 8 files changed, 560 insertions(+), 99 deletions(-) create mode 100644 docs/01-Getting-Started.html create mode 100644 docs/articles/01-Getting-Started.md diff --git a/README.md b/README.md index 23831ac..c50f506 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ doesn't remove the ability to write systems which minimize the cost of marshaling data and optimize for performance, to make use of the low-level access the FF&M API gives us. +- [Getting Started](igjoshua.github.io/coffi/01-Getting-Started.html) +- [API Documentation](igjoshua.github.io/coffi/) +- [Recent Changes](CHANGELOG.md) + ## Installation This library is available on Clojars, or as a git dependency. Add one of the following entries to the `:deps` key of your `deps.edn`: @@ -37,7 +41,7 @@ the following JVM arguments to your application. ``` 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 clj -J--enable-native-access=ALL-UNNAMED diff --git a/deps.edn b/deps.edn index 843775d..1aed6e4 100644 --- a/deps.edn +++ b/deps.edn @@ -29,6 +29,7 @@ :version "v1.0.486" :description "A Foreign Function Interface in Clojure for JDK 22+." :source-paths ["src/clj"] + :doc-paths ["docs/articles"] :output-path "docs" :source-uri "https://github.com/IGJoshua/coffi/blob/{git-commit}/{filepath}#L{line}" :metadata {:doc/format :markdown}}} diff --git a/docs/01-Getting-Started.html b/docs/01-Getting-Started.html new file mode 100644 index 0000000..fa78e5d --- /dev/null +++ b/docs/01-Getting-Started.html @@ -0,0 +1,147 @@ + +Getting Started

Getting Started

+

Installation

+

This library is available on Clojars. Add one of the following entries to the :deps key of your deps.edn:

+
org.suskalo/coffi {:mvn/version "x.y.z"}
+io.github.IGJoshua/coffi {:git/tag "x.y.z" :git/sha "abcdef0"}
+
+

See GitHub for the latest releases.

+

If you use this library as a git dependency, you will need to prepare the library.

+
$ clj -X:deps prep
+
+

Coffi requires usage of the package java.lang.foreign, and most of the operations are considered unsafe by the JDK, and are therefore unavailable to your code without passing some command line flags. In order to use coffi, add the following JVM arguments to your application.

+
--enable-native-access=ALL-UNNAMED
+
+

You can specify JVM arguments in a particular invocation of the Clojure CLI with the -J flag like so:

+
clj -J--enable-native-access=ALL-UNNAMED
+
+

You can also specify them in an alias in your deps.edn file under the :jvm-opts key (see the next example) and then invoking the CLI with that alias using -M, -A, or -X.

+
{:aliases {:dev {:jvm-opts ["--enable-native-access=ALL-UNNAMED"]}}}
+
+

Other build tools should provide similar functionality if you check their 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.

+

Basic Usage

+

There are two major components to coffi and interacting with native code: manipulating off-heap memory, and loading native code for use with Clojure.

+

In the simplest cases, the native functions you call will work exclusively with built-in types, for example the function strlen from libc.

+
(require '[coffi.mem :as mem :refer [defalias]])
+(require '[coffi.ffi :as ffi :refer [defcfn]])
+
+(defcfn strlen
+  "Given a string, measures its length in bytes."
+  strlen [::mem/c-string] ::mem/long)
+
+(strlen "hello")
+;; => 5
+
+

The first argument to defcfn is the name of the Clojure var that will hold the native function reference, followed by an optional docstring and attribute map, then the C function identifier, including the name of the native symbol, a vector of argument types, and the return type.

+

If you wish to use a native function as an anonymous function, it can be done with the cfn function.

+
((ffi/cfn "strlen" [::mem/c-string] ::mem/long) "hello")
+;; => 5
+
+

If you want to use functions from libraries other than libc, then you’ll need to load them. Two functions are provided for this, load-system-library, and load-library. load-system-library takes a string which represents the name of a library that should be loaded via system lookup.

+
(ffi/load-system-library "z")
+
+

This will load libz from the appropriate place on the user’s load path.

+

Alternatively, load-library takes a file path to a dynamically loaded library.

+
(ffi/load-library "lib/libz.so")
+
+

This will load libz from the lib subdirectory of the current working directory. As you can see this requires the entire filename, including platform-specific file extensions.

+

If a library is attempted to be loaded but doesn’t exist or otherwise can’t be loaded, an exception is thrown. This can be convenient as any namespace with a load-library call at the top level cannot be required without the library being able to be loaded.

+

Primitive Types

+

Coffi defines a basic set of primitive types: - byte - short - int - long - char - float - double - pointer

+

Each of these types maps to their C counterpart. Values of any of these primitive types except for pointer will be cast with their corresponding Clojure function when they are passed as arguments to native functions. Additionally, the c-string type is defined, although it is not primitive.

+

Composite Types

+

In addition, some composite types are also defined in coffi, including struct and union types (unions will be discussed with serialization and deserialization). For an example C struct and function:

+
typedef struct point {
+    float x;
+    float y;
+} Point;
+
+Point zero(void) {
+    Point res = {};
+
+    res.x = 0.0;
+    res.y = 0.0;
+
+    return res;
+}
+
+

The corresponding coffi definition is like so:

+
(defcfn zero-point
+  "zero" [] [::mem/struct [[:x ::mem/float] [:y ::mem/float]]])
+
+(zero-point)
+;; => {:x 0.0,
+;;     :y 0.0}
+
+

Writing out struct definitions like this every time would get tedious, so the macro defalias is used to define a struct alias.

+
(defalias ::point
+  [::mem/struct
+   [[:x ::mem/float]
+    [:y ::mem/float]]])
+
+(defcfn zero-point
+  "zero" [] ::point)
+
+

Struct definitions do not include any padding by default. Functions for transforming struct types to include padding conforming to various standards can be found in coffi.layout.

+
(require '[coffi.layout :as layout])
+
+(defalias ::needs-padding
+  (layout/with-c-layout
+   [::mem/struct
+    [[:a ::mem/char]
+     [:x ::mem/float]]]))
+
+(mem/size-of ::needs-padding)
+;; => 8
+
+(mem/align-of ::needs-padding)
+;; => 4
+
+

Values deserialized with types produced from layout functions may include an extra :coffi.layout/padding key with a nil value.

+

A limitation of the defcfn macro in its current form is that types provided to it must be provided in a literal form, not as an expression that evaluates to a type. This means that if you wish to use a layout function on a struct you must define an alias for it before the type can be used as a type in defcfn.

+

In cases where a pointer to some data is required to pass as an argument to a native function, but doesn’t need to be read back in, the pointer primitive type can take a type argument.

+
[::mem/pointer ::mem/int]
+
+

Arrays are also supported via a type argument. Keep in mind that they are the array itself, and not a pointer to the array like you might see in certain cases in C.

+
[::mem/array ::mem/int 3]
+
+

Callbacks

+

In addition to these composite types, there is also support for Clojure functions.

+
[::ffi/fn [::mem/c-string] ::mem/int]
+
+

Be aware though that if an exception is thrown out of a callback that is called from C, the JVM will crash. The resulting crash log should include the exception type and message in the registers section, but it’s important to be aware of all the same. Ideally you should test your callbacks before actually passing them to native code.

+

When writing a wrapper library for a C library, it may be a good choice to wrap all passed Clojure functions in an additional function which catches all throwables, potentially notifies the user in some manner (e.g. logging), and returns a default value. This is on the wrapper library’s developer to decide when and where this is appropriate, as in some cases no reasonable default return value can be determined and it is most sensible to simply crash the JVM. This is the reason that coffi defaults to this behavior, as in the author’s opinion it is better to fail hard and fast rather than to attempt to produce a default and cause unexpected behavior later.

+

Another important thing to keep in mind is the expected lifetime of the function that you pass to native code. For example it is perfectly fine to pass an anonymous function to a native function if the callback will never be called again once the native function returns. If however it saves the callback for later use the JVM may collect it prematurely, causing a crash when the callback is later called by native code.

+

Variadic Functions

+

Some native functions can take any number of arguments, and in these cases coffi provides vacfn-factory (for “varargs C function factory”).

+
(def printf-factory (ffi/vacfn-factory "printf" [::mem/c-string] ::mem/int))
+
+

This returns a function of the types of the rest of the arguments which itself returns a native function wrapper.

+
(def print-int (printf-factory ::mem/int))
+
+(print-int "Some integer: %d\n" 5)
+;; Some integer: 5
+
+

At the moment there is no equivalent to defcfn for varargs functions.

+

Some native functions that are variadic use the type va_list to make it easier for other languages to call them in their FFI. At the time of writing, coffi does not support va-list, however it is a planned feature.

+

Global Variables

+

Some libraries include global variables or constants accessible through symbols. To start with, constant values stored in symbols can be fetched with const, or the parallel macro defconst

+
(def some-const (ffi/const "some_const" ::mem/int))
+(ffi/defconst some-const "some_const" ::mem/int)
+
+

This value is fetched once when you call const and is turned into a Clojure value. If you need to refer to a global variable, then static-variable (or parallel defvar) can be used to create a reference to the native value.

+
(def some-var (ffi/static-variable "some_var" ::mem/int))
+(ffi/defvar some-var "some_var" ::mem/int)
+
+

This variable is an IDeref. Each time you dereference it, the value will be deserialized from the native memory and returned. Additional functions are provided for mutating the variable.

+
(ffi/freset! some-var 5)
+;; => 5
+@some-var
+;; => 5
+
+

Be aware however that there is no synchronization on these types. The value being read is not read atomically, so you may see an inconsistent state if the value is being mutated on another thread.

+

A parallel function fswap! is also provided, but it does not provide any atomic semantics either.

+

The memory that backs the static variable can be fetched with the function static-variable-segment, which can be used to pass a pointer to the static variable to native functions that require it.

+
\ No newline at end of file diff --git a/docs/articles/01-Getting-Started.md b/docs/articles/01-Getting-Started.md new file mode 100644 index 0000000..f190cc2 --- /dev/null +++ b/docs/articles/01-Getting-Started.md @@ -0,0 +1,309 @@ +# Getting Started + +## Installation +This library is available on Clojars. Add one of the following entries to the +`:deps` key of your `deps.edn`: + +```clojure +org.suskalo/coffi {:mvn/version "x.y.z"} +io.github.IGJoshua/coffi {:git/tag "x.y.z" :git/sha "abcdef0"} +``` + +See GitHub for the [latest releases](https://github.com/IGJoshua/coffi/releases). + +If you use this library as a git dependency, you will need to prepare the +library. + +```sh +$ clj -X:deps prep +``` + +Coffi requires usage of the package `java.lang.foreign`, and most of the +operations are considered unsafe by the JDK, and are therefore unavailable to +your code without passing some command line flags. In order to use coffi, add +the following JVM arguments to your application. + +```sh +--enable-native-access=ALL-UNNAMED +``` + +You can specify JVM arguments in a particular invocation of the Clojure CLI with +the -J flag like so: + +``` sh +clj -J--enable-native-access=ALL-UNNAMED +``` + +You can also specify them in an alias in your `deps.edn` file under the +`:jvm-opts` key (see the next example) and then invoking the CLI with that alias +using `-M`, `-A`, or `-X`. + +``` clojure +{:aliases {:dev {:jvm-opts ["--enable-native-access=ALL-UNNAMED"]}}} +``` + +Other build tools should provide similar functionality if you check their +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. + +## Basic Usage +There are two major components to coffi and interacting with native code: +manipulating off-heap memory, and loading native code for use with Clojure. + +In the simplest cases, the native functions you call will work exclusively with +built-in types, for example the function `strlen` from libc. + +```clojure +(require '[coffi.mem :as mem :refer [defalias]]) +(require '[coffi.ffi :as ffi :refer [defcfn]]) + +(defcfn strlen + "Given a string, measures its length in bytes." + strlen [::mem/c-string] ::mem/long) + +(strlen "hello") +;; => 5 +``` + +The first argument to `defcfn` is the name of the Clojure var that will hold the +native function reference, followed by an optional docstring and attribute map, +then the C function identifier, including the name of the native symbol, a +vector of argument types, and the return type. + +If you wish to use a native function as an anonymous function, it can be done +with the `cfn` function. + +```clojure +((ffi/cfn "strlen" [::mem/c-string] ::mem/long) "hello") +;; => 5 +``` + +If you want to use functions from libraries other than libc, then you'll need to +load them. Two functions are provided for this, `load-system-library`, and +`load-library`. `load-system-library` takes a string which represents the name +of a library that should be loaded via system lookup. + +```clojure +(ffi/load-system-library "z") +``` + +This will load libz from the appropriate place on the user's load path. + +Alternatively, `load-library` takes a file path to a dynamically loaded library. + +```clojure +(ffi/load-library "lib/libz.so") +``` + +This will load libz from the lib subdirectory of the current working directory. +As you can see this requires the entire filename, including platform-specific +file extensions. + +If a library is attempted to be loaded but doesn't exist or otherwise can't be +loaded, an exception is thrown. This can be convenient as any namespace with a +`load-library` call at the top level cannot be required without the library +being able to be loaded. + +### Primitive Types +Coffi defines a basic set of primitive types: +- byte +- short +- int +- long +- char +- float +- double +- pointer + +Each of these types maps to their C counterpart. Values of any of these +primitive types except for `pointer` will be cast with their corresponding +Clojure function when they are passed as arguments to native functions. +Additionally, the `c-string` type is defined, although it is not primitive. + +### Composite Types +In addition, some composite types are also defined in coffi, including struct +and union types (unions will be discussed with serialization and +deserialization). For an example C struct and function: + +```c +typedef struct point { + float x; + float y; +} Point; + +Point zero(void) { + Point res = {}; + + res.x = 0.0; + res.y = 0.0; + + return res; +} +``` + +The corresponding coffi definition is like so: + +```clojure +(defcfn zero-point + "zero" [] [::mem/struct [[:x ::mem/float] [:y ::mem/float]]]) + +(zero-point) +;; => {:x 0.0, +;; :y 0.0} +``` + +Writing out struct definitions like this every time would get tedious, so the +macro `defalias` is used to define a struct alias. + +```clojure +(defalias ::point + [::mem/struct + [[:x ::mem/float] + [:y ::mem/float]]]) + +(defcfn zero-point + "zero" [] ::point) +``` + +Struct definitions do not include any padding by default. Functions for +transforming struct types to include padding conforming to various standards can +be found in `coffi.layout`. + +``` clojure +(require '[coffi.layout :as layout]) + +(defalias ::needs-padding + (layout/with-c-layout + [::mem/struct + [[:a ::mem/char] + [:x ::mem/float]]])) + +(mem/size-of ::needs-padding) +;; => 8 + +(mem/align-of ::needs-padding) +;; => 4 +``` + +Values deserialized with types produced from layout functions may include an +extra `:coffi.layout/padding` key with a nil value. + +A limitation of the `defcfn` macro in its current form is that types provided to +it must be provided in a literal form, not as an expression that evaluates to a +type. This means that if you wish to use a layout function on a struct you must +define an alias for it before the type can be used as a type in `defcfn`. + +In cases where a pointer to some data is required to pass as an argument to a +native function, but doesn't need to be read back in, the `pointer` primitive +type can take a type argument. + +```clojure +[::mem/pointer ::mem/int] +``` + +Arrays are also supported via a type argument. Keep in mind that they are the +array itself, and not a pointer to the array like you might see in certain cases +in C. + +```clojure +[::mem/array ::mem/int 3] +``` + +### Callbacks +In addition to these composite types, there is also support for Clojure +functions. + +```clojure +[::ffi/fn [::mem/c-string] ::mem/int] +``` + +Be aware though that if an exception is thrown out of a callback that is called +from C, the JVM will crash. The resulting crash log should include the exception +type and message in the registers section, but it's important to be aware of all +the same. Ideally you should test your callbacks before actually passing them to +native code. + +When writing a wrapper library for a C library, it may be a good choice to wrap +all passed Clojure functions in an additional function which catches all +throwables, potentially notifies the user in some manner (e.g. logging), and +returns a default value. This is on the wrapper library's developer to decide +when and where this is appropriate, as in some cases no reasonable default +return value can be determined and it is most sensible to simply crash the JVM. +This is the reason that coffi defaults to this behavior, as in the author's +opinion it is better to fail hard and fast rather than to attempt to produce a +default and cause unexpected behavior later. + +Another important thing to keep in mind is the expected lifetime of the function +that you pass to native code. For example it is perfectly fine to pass an +anonymous function to a native function if the callback will never be called +again once the native function returns. If however it saves the callback for +later use the JVM may collect it prematurely, causing a crash when the callback +is later called by native code. + +### Variadic Functions +Some native functions can take any number of arguments, and in these cases coffi +provides `vacfn-factory` (for "varargs C function factory"). + +```clojure +(def printf-factory (ffi/vacfn-factory "printf" [::mem/c-string] ::mem/int)) +``` + +This returns a function of the types of the rest of the arguments which itself +returns a native function wrapper. + +```clojure +(def print-int (printf-factory ::mem/int)) + +(print-int "Some integer: %d\n" 5) +;; Some integer: 5 +``` + +At the moment there is no equivalent to `defcfn` for varargs functions. + +Some native functions that are variadic use the type `va_list` to make it easier +for other languages to call them in their FFI. At the time of writing, coffi +does not support va-list, however it is a planned feature. + +### Global Variables +Some libraries include global variables or constants accessible through symbols. +To start with, constant values stored in symbols can be fetched with `const`, or +the parallel macro `defconst` + +```clojure +(def some-const (ffi/const "some_const" ::mem/int)) +(ffi/defconst some-const "some_const" ::mem/int) +``` + +This value is fetched once when you call `const` and is turned into a Clojure +value. If you need to refer to a global variable, then `static-variable` (or +parallel `defvar`) can be used to create a reference to the native value. + +```clojure +(def some-var (ffi/static-variable "some_var" ::mem/int)) +(ffi/defvar some-var "some_var" ::mem/int) +``` + +This variable is an `IDeref`. Each time you dereference it, the value will be +deserialized from the native memory and returned. Additional functions are +provided for mutating the variable. + +```clojure +(ffi/freset! some-var 5) +;; => 5 +@some-var +;; => 5 +``` + +Be aware however that there is no synchronization on these types. The value +being read is not read atomically, so you may see an inconsistent state if the +value is being mutated on another thread. + +A parallel function `fswap!` is also provided, but it does not provide any +atomic semantics either. + +The memory that backs the static variable can be fetched with the function +`static-variable-segment`, which can be used to pass a pointer to the static +variable to native functions that require it. diff --git a/docs/coffi.ffi.html b/docs/coffi.ffi.html index c843e2a..7cc8c2c 100644 --- a/docs/coffi.ffi.html +++ b/docs/coffi.ffi.html @@ -1,41 +1,41 @@ -coffi.ffi documentation

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

+coffi.ffi documentation

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

cfn

(cfn symbol args ret)

Constructs a Clojure function to call the native function referenced by symbol.

The function returned will serialize any passed arguments into the args types, and deserialize the return to the ret type.

If your args and ret are constants, then it is more efficient to call make-downcall followed by make-serde-wrapper because the latter has an inline definition which will result in less overhead from serdes.

-

const

(const symbol-or-addr type)

Gets the value of a constant stored in symbol-or-addr.

-

defcfn

macro

(defcfn name docstring? attr-map? symbol arg-types ret-type)(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)

Defines a Clojure function which maps to a native function.

+

const

(const symbol-or-addr type)

Gets the value of a constant stored in symbol-or-addr.

+

defcfn

macro

(defcfn name docstring? attr-map? symbol arg-types ret-type)(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)

Defines a Clojure function which maps to a native function.

name is the symbol naming the resulting var. symbol is a symbol or string naming the library symbol to link against. arg-types is a vector of qualified keywords representing the argument types. ret-type is a single qualified keyword representing the return type. fn-tail is the body of the function (potentially with multiple arities) which wraps the native one. Inside the function, native-fn 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.

If no fn-tail is provided, then the resulting function will simply serialize the arguments according to arg-types, call the native function, and deserialize the return value.

The number of args in the fn-tail need not match the number of arg-types for the native function. It need only call the native wrapper function with the correct arguments.

See serialize, deserialize, make-downcall.

-

defconst

macro

(defconst symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be the value of the given type from symbol-or-addr.

-

defvar

macro

(defvar symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be a reference to the native memory from symbol-or-addr.

-

ensure-symbol

(ensure-symbol symbol-or-addr)

Returns the argument if it is a MemorySegment, otherwise calls find-symbol on it.

-

find-symbol

(find-symbol sym)

Gets the MemorySegment of a symbol from the loaded libraries.

-

freset!

(freset! static-var newval)

Sets the value of static-var to newval, running it through serialize.

-

fswap!

(fswap! static-var f & args)

Non-atomically runs the function f over the value stored in static-var.

+

defconst

macro

(defconst symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be the value of the given type from symbol-or-addr.

+

defvar

macro

(defvar symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be a reference to the native memory from symbol-or-addr.

+

ensure-symbol

(ensure-symbol symbol-or-addr)

Returns the argument if it is a MemorySegment, otherwise calls find-symbol on it.

+

find-symbol

(find-symbol sym)

Gets the MemorySegment of a symbol from the loaded libraries.

+

freset!

(freset! static-var newval)

Sets the value of static-var to newval, running it through serialize.

+

fswap!

(fswap! static-var f & args)

Non-atomically runs the function f over the value stored in static-var.

The value is deserialized before passing it to f, and serialized before putting the value into static-var.

-

load-library

(load-library path)

Loads the library at path.

-

load-system-library

(load-system-library libname)

Loads the library named libname from the system’s load path.

-

make-downcall

(make-downcall symbol-or-addr args ret)

Constructs a downcall function reference to symbol-or-addr with the given args and ret types.

+

load-library

(load-library path)

Loads the library at path.

+

load-system-library

(load-system-library libname)

Loads the library named libname from the system’s load path.

+

make-downcall

(make-downcall symbol-or-addr args ret)

Constructs a downcall function reference to symbol-or-addr with the given args and ret types.

The function returned takes only arguments whose types match exactly the java-layout for that type, and returns an argument with exactly the java-layout of the ret type. This function will perform no serialization or deserialization of arguments or the return type.

If the ret type is non-primitive, then the returned function will take a first argument of a SegmentAllocator.

-

make-serde-varargs-wrapper

(make-serde-varargs-wrapper varargs-factory required-args ret-type)

Constructs a wrapper function for the varargs-factory which produces functions that serialize the arguments and deserialize the return value.

-

make-serde-wrapper

(make-serde-wrapper downcall arg-types ret-type)

Constructs a wrapper function for the downcall which serializes the arguments and deserializes the return value.

-

make-varargs-factory

(make-varargs-factory symbol required-args ret)

Returns a function for constructing downcalls with additional types for arguments.

+

make-serde-varargs-wrapper

(make-serde-varargs-wrapper varargs-factory required-args ret-type)

Constructs a wrapper function for the varargs-factory which produces functions that serialize the arguments and deserialize the return value.

+

make-serde-wrapper

(make-serde-wrapper downcall arg-types ret-type)

Constructs a wrapper function for the downcall which serializes the arguments and deserializes the return value.

+

make-varargs-factory

(make-varargs-factory symbol required-args ret)

Returns a function for constructing downcalls with additional types for arguments.

The required-args 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.

The returned function is memoized, so that only one downcall function will be generated per combination of argument types.

See make-downcall.

-

reify-libspec

(reify-libspec libspec)

Loads all the symbols specified in the libspec.

+

reify-libspec

(reify-libspec libspec)

Loads all the symbols specified in the libspec.

The value of each key of the passed map is transformed as by reify-symbolspec.

-

reify-symbolspec

multimethod

Takes a spec for a symbol reference and returns a live value for that type.

-

static-variable

(static-variable symbol-or-addr type)

Constructs a reference to a mutable value stored in symbol-or-addr.

+

reify-symbolspec

multimethod

Takes a spec for a symbol reference and returns a live value for that type.

+

static-variable

(static-variable symbol-or-addr type)

Constructs a reference to a mutable value stored in symbol-or-addr.

The returned value can be dereferenced, and has metadata.

See freset!, fswap!.

-

static-variable-segment

(static-variable-segment static-var)

Gets the backing MemorySegment from static-var.

+

static-variable-segment

(static-variable-segment static-var)

Gets the backing MemorySegment from static-var.

This is primarily useful when you need to pass the static variable’s address to a native function which takes an Addressable.

-

vacfn-factory

(vacfn-factory symbol required-args ret)

Constructs a varargs factory to call the native function referenced by symbol.

+

vacfn-factory

(vacfn-factory symbol required-args ret)

Constructs a varargs factory to call the native function referenced by symbol.

The function returned takes any number of type arguments and returns a specialized Clojure function for calling the native function with those arguments.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/coffi.layout.html b/docs/coffi.layout.html index 11c4c01..bff4af1 100644 --- a/docs/coffi.layout.html +++ b/docs/coffi.layout.html @@ -1,6 +1,6 @@ -coffi.layout documentation

coffi.layout

Functions for adjusting the layout of structs.

+coffi.layout documentation

coffi.layout

Functions for adjusting the layout of structs.

with-c-layout

(with-c-layout struct-spec)

Forces a struct specification to C layout rules.

This will add padding fields between fields to match C alignment requirements.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/coffi.mem.html b/docs/coffi.mem.html index 7cb91c2..cc459be 100644 --- a/docs/coffi.mem.html +++ b/docs/coffi.mem.html @@ -1,121 +1,121 @@ -coffi.mem documentation

coffi.mem

Functions for managing native allocations, memory arenas, and (de)serialization.

+coffi.mem documentation

coffi.mem

Functions for managing native allocations, memory arenas, and (de)serialization.

For any new type to be implemented, three multimethods must be overriden, but which three depends on the native representation of the type.

If the native representation of the type is a primitive (whether or not other data beyond the primitive is associated with it, as e.g. a pointer), then primitive-type must be overriden to return which primitive type it is serialized as, then serialize* and deserialize* should be overriden.

If the native representation of the type is a composite type, like a union, 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 overriden to allow marshaling values of the type into and out of memory segments.

address-of

(address-of addressable)

Gets the address of a given segment as a number.

-

address?

(address? addr)

Checks if an object is a memory address.

+

address?

(address? addr)

Checks if an object is a memory address.

nil is considered an address.

-

align-of

(align-of type)

The alignment in bytes of the given type.

-

alloc

(alloc size)(alloc size arena)(alloc size alignment arena)

Allocates size bytes.

+

align-of

(align-of type)

The alignment in bytes of the given type.

+

alloc

(alloc size)(alloc size arena)(alloc size alignment arena)

Allocates size bytes.

If an arena is provided, the allocation will be reclaimed when it is closed.

-

alloc-instance

(alloc-instance type)(alloc-instance type arena)

Allocates a memory segment for the given type.

-

alloc-with

(alloc-with allocator size)(alloc-with allocator size alignment)

Allocates size bytes using the allocator.

-

arena-allocator

(arena-allocator arena)

Constructs a SegmentAllocator from the given Arena.

+

alloc-instance

(alloc-instance type)(alloc-instance type arena)

Allocates a memory segment for the given type.

+

alloc-with

(alloc-with allocator size)(alloc-with allocator size alignment)

Allocates size bytes using the allocator.

+

arena-allocator

(arena-allocator arena)

Constructs a SegmentAllocator from the given Arena.

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.

-

as-segment

(as-segment address)(as-segment address size)(as-segment address size arena)(as-segment address size arena cleanup)

Dereferences an address into a memory segment associated with the arena (default global).

-

auto-arena

(auto-arena)

Constructs a new memory arena that is managed by the garbage collector.

+

as-segment

(as-segment address)(as-segment address size)(as-segment address size arena)(as-segment address size arena cleanup)

Dereferences an address into a memory segment associated with the arena (default global).

+

auto-arena

(auto-arena)

Constructs a new memory arena that is managed by the garbage collector.

The arena 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 arena cannot be closed, and therefore should not be created in a with-open clause.

-

big-endian

The big-endian ByteOrder.

+

big-endian

The big-endian ByteOrder.

See little-endian, native-endian.

-

byte-layout

The MemoryLayout for a byte in native-endian ByteOrder.

-

c-layout

multimethod

Gets the layout object for a given type.

+

byte-layout

The MemoryLayout for a byte in native-endian ByteOrder.

+

c-layout

multimethod

Gets the layout object for a given type.

If a type is primitive it will return the appropriate primitive layout (see c-prim-layout).

Otherwise, it should return a GroupLayout for the given type.

-

char-layout

The MemoryLayout for a c-sized char in native-endian ByteOrder.

-

clone-segment

(clone-segment segment)(clone-segment segment arena)

Clones the content of segment into a new segment of the same size.

-

confined-arena

(confined-arena)

Constructs a new arena for use only in this thread.

+

char-layout

The MemoryLayout for a c-sized char in native-endian ByteOrder.

+

clone-segment

(clone-segment segment)(clone-segment segment arena)

Clones the content of segment into a new segment of the same size.

+

confined-arena

(confined-arena)

Constructs a new arena for use only in this thread.

The memory allocated within this arena is cheap to allocate, like a native stack.

The memory allocated within this arena will be cleared once it is closed, so it is usually a good idea to create it in a with-open clause.

-

copy-segment

(copy-segment dest src)

Copies the content to dest from src.

+

copy-segment

(copy-segment dest src)

Copies the content to dest from src.

Returns dest.

-

defalias

macro

(defalias new-type aliased-type)

Defines a type alias from new-type to aliased-type.

+

defalias

macro

(defalias new-type aliased-type)

Defines a type alias from new-type to aliased-type.

This creates needed serialization and deserialization implementations for the aliased type.

-

deserialize

(deserialize obj type)

Deserializes an arbitrary type.

+

deserialize

(deserialize obj type)

Deserializes an arbitrary type.

For types which have a primitive representation, this deserializes the primitive representation. For types which do not, this deserializes out of a segment.

-

deserialize*

multimethod

Deserializes a primitive object into a Clojure data structure.

+

deserialize*

multimethod

Deserializes a primitive object into a Clojure data structure.

This is intended for use with types that are returned as a primitive but which need additional processing before they can be returned.

-

deserialize-from

multimethod

Deserializes the given segment into a Clojure data structure.

+

deserialize-from

multimethod

Deserializes the given segment into a Clojure data structure.

For types that serialize to primitives, a default implementation will deserialize the primitive before calling deserialize*.

-

double-alignment

The alignment in bytes of a c-sized double.

-

double-layout

The MemoryLayout for a c-sized double in native-endian ByteOrder.

-

double-size

The size in bytes of a c-sized double.

-

float-alignment

The alignment in bytes of a c-sized float.

-

float-layout

The MemoryLayout for a c-sized float in native-endian ByteOrder.

-

float-size

The size in bytes of a c-sized float.

-

global-arena

(global-arena)

Constructs the global arena, which will never reclaim its resources.

+

double-alignment

The alignment in bytes of a c-sized double.

+

double-layout

The MemoryLayout for a c-sized double in native-endian ByteOrder.

+

double-size

The size in bytes of a c-sized double.

+

float-alignment

The alignment in bytes of a c-sized float.

+

float-layout

The MemoryLayout for a c-sized float in native-endian ByteOrder.

+

float-size

The size in bytes of a c-sized float.

+

global-arena

(global-arena)

Constructs the global arena, which will never reclaim its resources.

This arena may be shared across threads, but is intended mainly in cases 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.

-

int-alignment

The alignment in bytes of a c-sized int.

-

int-layout

The MemoryLayout for a c-sized int in native-endian ByteOrder.

-

int-size

The size in bytes of a c-sized int.

-

java-layout

(java-layout type)

Gets the Java class to an argument of this type for a method handle.

+

int-alignment

The alignment in bytes of a c-sized int.

+

int-layout

The MemoryLayout for a c-sized int in native-endian ByteOrder.

+

int-size

The size in bytes of a c-sized int.

+

java-layout

(java-layout type)

Gets the Java class to an argument of this type for a method handle.

If a type serializes to a primitive it returns return a Java primitive type. Otherwise, it returns MemorySegment.

-

java-prim-layout

Map of primitive type names to the Java types for a method handle.

-

little-endian

The little-endian ByteOrder.

+

java-prim-layout

Map of primitive type names to the Java types for a method handle.

+

little-endian

The little-endian ByteOrder.

See big-endian, native-endian

-

long-alignment

The alignment in bytes of a c-sized long.

-

long-layout

The MemoryLayout for a c-sized long in native-endian ByteOrder.

-

long-size

The size in bytes of a c-sized long.

-

native-endian

The ByteOrder for the native endianness of the current hardware.

+

long-alignment

The alignment in bytes of a c-sized long.

+

long-layout

The MemoryLayout for a c-sized long in native-endian ByteOrder.

+

long-size

The size in bytes of a c-sized long.

+

native-endian

The ByteOrder for the native endianness of the current hardware.

See big-endian, little-endian.

-

null

The NULL pointer object.

+

null

The NULL pointer object.

While this object is safe to pass to functions which serialize to a pointer, it’s generally encouraged to simply pass nil. This value primarily exists to make it easier to write custom types with a primitive pointer representation.

-

null?

(null? addr)

Checks if a memory address is null.

-

pointer-alignment

The alignment in bytes of a c-sized pointer.

-

pointer-layout

The MemoryLayout for a native pointer in native-endian ByteOrder.

-

pointer-size

The size in bytes of a c-sized pointer.

-

primitive-type

multimethod

Gets the primitive type that is used to pass as an argument for the type.

+

null?

(null? addr)

Checks if a memory address is null.

+

pointer-alignment

The alignment in bytes of a c-sized pointer.

+

pointer-layout

The MemoryLayout for a native pointer in native-endian ByteOrder.

+

pointer-size

The size in bytes of a c-sized pointer.

+

primitive-type

multimethod

Gets the primitive type that is used to pass as an argument for the type.

This is for objects which are passed to native functions as primitive types, but which need additional logic to be performed during serialization and deserialization.

Implementations of this method should take into account that type arguments may not always be evaluated before passing to this function.

Returns nil for any type which does not have a primitive representation.

-

primitive-types

A set of all primitive types.

-

primitive?

(primitive? type)

A predicate to determine if a given type is primitive.

-

read-address

(read-address segment)(read-address segment offset)

Reads an address from the segment, at an optional offset, wrapped in a MemorySegment.

-

read-byte

(read-byte segment)(read-byte segment offset)

Reads a byte from the segment, at an optional offset.

-

read-char

(read-char segment)(read-char segment offset)

Reads a char from the segment, at an optional offset.

-

read-double

(read-double segment)(read-double segment offset)(read-double segment offset byte-order)

Reads a double from the segment, at an optional offset.

+

primitive-types

A set of all primitive types.

+

primitive?

(primitive? type)

A predicate to determine if a given type is primitive.

+

read-address

(read-address segment)(read-address segment offset)

Reads an address from the segment, at an optional offset, wrapped in a MemorySegment.

+

read-byte

(read-byte segment)(read-byte segment offset)

Reads a byte from the segment, at an optional offset.

+

read-char

(read-char segment)(read-char segment offset)

Reads a char from the segment, at an optional offset.

+

read-double

(read-double segment)(read-double segment offset)(read-double segment offset byte-order)

Reads a double from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-float

(read-float segment)(read-float segment offset)(read-float segment offset byte-order)

Reads a float from the segment, at an optional offset.

+

read-float

(read-float segment)(read-float segment offset)(read-float segment offset byte-order)

Reads a float from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-int

(read-int segment)(read-int segment offset)(read-int segment offset byte-order)

Reads a int from the segment, at an optional offset.

+

read-int

(read-int segment)(read-int segment offset)(read-int segment offset byte-order)

Reads a int from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-long

(read-long segment)(read-long segment offset)(read-long segment offset byte-order)

Reads a long from the segment, at an optional offset.

+

read-long

(read-long segment)(read-long segment offset)(read-long segment offset byte-order)

Reads a long from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-short

(read-short segment)(read-short segment offset)(read-short segment offset byte-order)

Reads a short from the segment, at an optional offset.

+

read-short

(read-short segment)(read-short segment offset)(read-short segment offset byte-order)

Reads a short from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

reinterpret

(reinterpret segment size)(reinterpret segment size arena)(reinterpret segment size arena cleanup)

Reinterprets the segment as having the passed size.

+

reinterpret

(reinterpret segment size)(reinterpret segment size arena)(reinterpret segment size arena cleanup)

Reinterprets the segment as having the passed size.

If arena is passed, the scope of the segment is associated with the arena, as well as its access constraints. If cleanup is passed, it will be a 1-argument function of a fresh memory segment backed by the same memory as the returned segment which should perform any required cleanup operations. It will be called when the arena is closed.

-

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

-

serialize

(serialize obj type)(serialize obj type arena)

Serializes an arbitrary type.

+

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

+

serialize

(serialize obj type)(serialize obj type arena)

Serializes an arbitrary type.

For types which have a primitive representation, this serializes into that representation. For types which do not, it allocates a new segment and serializes into that.

-

serialize*

multimethod

Constructs a serialized version of the obj and returns it.

+

serialize*

multimethod

Constructs a serialized version of the obj and returns it.

Any new allocations made during the serialization should be tied to the given arena, except in extenuating circumstances.

This method should only be implemented for types that serialize to primitives.

-

serialize-into

multimethod

Writes a serialized version of the obj to the given segment.

+

serialize-into

multimethod

Writes a serialized version of the obj to the given segment.

Any new allocations made during the serialization should be tied to the given arena, except in extenuating circumstances.

This method should be implemented for any type which does not override c-layout.

For any other type, this will serialize it as serialize* before writing the result value into the segment.

-

shared-arena

(shared-arena)

Constructs a new shared memory arena.

+

shared-arena

(shared-arena)

Constructs a new shared memory arena.

This arena can be shared across threads and memory allocated in it will only be cleaned up once any thread accessing the arena closes it.

-

short-alignment

The alignment in bytes of a c-sized short.

-

short-layout

The MemoryLayout for a c-sized short in native-endian ByteOrder.

-

short-size

The size in bytes of a c-sized short.

-

size-of

(size-of type)

The size in bytes of the given type.

-

slice

(slice segment offset)(slice segment offset size)

Get a slice over the segment with the given offset.

-

slice-segments

(slice-segments segment size)

Constructs a lazy seq of size-length memory segments, sliced from segment.

-

write-address

(write-address segment value)(write-address segment offset value)

Writes the address of the MemorySegment value to the segment, at an optional offset.

-

write-byte

(write-byte segment value)(write-byte segment offset value)

Writes a byte to the segment, at an optional offset.

-

write-char

(write-char segment value)(write-char segment offset value)

Writes a char to the segment, at an optional offset.

-

write-double

(write-double segment value)(write-double segment offset value)(write-double segment offset byte-order value)

Writes a double to the segment, at an optional offset.

+

short-alignment

The alignment in bytes of a c-sized short.

+

short-layout

The MemoryLayout for a c-sized short in native-endian ByteOrder.

+

short-size

The size in bytes of a c-sized short.

+

size-of

(size-of type)

The size in bytes of the given type.

+

slice

(slice segment offset)(slice segment offset size)

Get a slice over the segment with the given offset.

+

slice-segments

(slice-segments segment size)

Constructs a lazy seq of size-length memory segments, sliced from segment.

+

write-address

(write-address segment value)(write-address segment offset value)

Writes the address of the MemorySegment value to the segment, at an optional offset.

+

write-byte

(write-byte segment value)(write-byte segment offset value)

Writes a byte to the segment, at an optional offset.

+

write-char

(write-char segment value)(write-char segment offset value)

Writes a char to the segment, at an optional offset.

+

write-double

(write-double segment value)(write-double segment offset value)(write-double segment offset byte-order value)

Writes a double to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-float

(write-float segment value)(write-float segment offset value)(write-float segment offset byte-order value)

Writes a float to the segment, at an optional offset.

+

write-float

(write-float segment value)(write-float segment offset value)(write-float segment offset byte-order value)

Writes a float to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-int

(write-int segment value)(write-int segment offset value)(write-int segment offset byte-order value)

Writes a int to the segment, at an optional offset.

+

write-int

(write-int segment value)(write-int segment offset value)(write-int segment offset byte-order value)

Writes a int to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-long

(write-long segment value)(write-long segment offset value)(write-long segment offset byte-order value)

Writes a long to the segment, at an optional offset.

+

write-long

(write-long segment value)(write-long segment offset value)(write-long segment offset byte-order value)

Writes a long to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-short

(write-short segment value)(write-short segment offset value)(write-short segment offset byte-order value)

Writes a short to the segment, at an optional offset.

+

write-short

(write-short segment value)(write-short segment offset value)(write-short segment offset byte-order value)

Writes a short to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 36ad180..0e9517c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,6 +1,6 @@ -coffi v1.0.486

coffi v1.0.486

A Foreign Function Interface in Clojure for JDK 22+.

Namespaces

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

+coffi v1.0.486

coffi v1.0.486

A Foreign Function Interface in Clojure for JDK 22+.

Topics

Namespaces

coffi.layout

Functions for adjusting the layout of structs.

Public variables and functions:

\ No newline at end of file From 3c16e00fc1b7ca4d34d90c00003b7ea3785d9099 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 4 Oct 2024 16:15:36 -0400 Subject: [PATCH 4/6] Actually use url schemes for website links in the readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c50f506..ec8fce4 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ doesn't remove the ability to write systems which minimize the cost of marshaling data and optimize for performance, to make use of the low-level access the FF&M API gives us. -- [Getting Started](igjoshua.github.io/coffi/01-Getting-Started.html) -- [API Documentation](igjoshua.github.io/coffi/) +- [Getting Started](https://igjoshua.github.io/coffi/01-Getting-Started.html) +- [API Documentation](https://igjoshua.github.io/coffi/) - [Recent Changes](CHANGELOG.md) ## Installation From 58eaffdf543a3aa00436dafef2c65b247ad3f916 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 4 Oct 2024 16:27:06 -0400 Subject: [PATCH 5/6] Add more articles from readme sections --- docs/01-Getting-Started.html | 23 +- docs/02-Memory-Management.html | 18 ++ docs/03-Builtin-Types.html | 30 ++ docs/04-Custom-Types.html | 82 ++++++ docs/05-Low-Level-Wrappers.html | 51 ++++ docs/50-Data-Model.html | 25 ++ docs/99-Benchmarks.html | 258 +++++++++++++++++ docs/articles/01-Getting-Started.md | 34 +++ docs/articles/02-Memory-Management.md | 38 +++ docs/articles/03-Builtin-Types.md | 53 ++++ docs/articles/04-Custom-Types.md | 136 +++++++++ docs/articles/05-Low-Level-Wrappers.md | 95 +++++++ docs/articles/50-Data-Model.md | 44 +++ docs/articles/99-Benchmarks.md | 373 +++++++++++++++++++++++++ docs/coffi.ffi.html | 42 +-- docs/coffi.layout.html | 4 +- docs/coffi.mem.html | 148 +++++----- docs/index.html | 2 +- 18 files changed, 1357 insertions(+), 99 deletions(-) create mode 100644 docs/02-Memory-Management.html create mode 100644 docs/03-Builtin-Types.html create mode 100644 docs/04-Custom-Types.html create mode 100644 docs/05-Low-Level-Wrappers.html create mode 100644 docs/50-Data-Model.html create mode 100644 docs/99-Benchmarks.html create mode 100644 docs/articles/02-Memory-Management.md create mode 100644 docs/articles/03-Builtin-Types.md create mode 100644 docs/articles/04-Custom-Types.md create mode 100644 docs/articles/05-Low-Level-Wrappers.md create mode 100644 docs/articles/50-Data-Model.md create mode 100644 docs/articles/99-Benchmarks.md diff --git a/docs/01-Getting-Started.html b/docs/01-Getting-Started.html index fa78e5d..3d4bb2b 100644 --- a/docs/01-Getting-Started.html +++ b/docs/01-Getting-Started.html @@ -1,6 +1,6 @@ -Getting Started

Getting Started

+Getting Started

Getting Started

Installation

This library is available on Clojars. Add one of the following entries to the :deps key of your deps.edn:

org.suskalo/coffi {:mvn/version "x.y.z"}
@@ -144,4 +144,25 @@ Point zero(void) {
 

Be aware however that there is no synchronization on these types. The value being read is not read atomically, so you may see an inconsistent state if the value is being mutated on another thread.

A parallel function fswap! is also provided, but it does not provide any atomic semantics either.

The memory that backs the static variable can be fetched with the function static-variable-segment, which can be used to pass a pointer to the static variable to native functions that require it.

+

Complex Wrappers

+

Some functions require more complex code to map nicely to a Clojure function. The defcfn macro provides facilities to wrap the native function with some Clojure code to make this easier.

+
(defcfn takes-array
+  "takes_array_with_count" [::mem/pointer ::mem/long] ::mem/void
+  native-fn
+  [ints]
+  (let [arr-len (count ints)
+        int-array (mem/serialize ints [::mem/array ::mem/int arr-len])]
+    (native-fn int-array arr-len)))
+
+

The symbol native-fn can be any unqualified symbol, and names the native function being wrapped. It must be called in the function body below if you want to call the native code.

+

This serialize function has a paired deserialize, and allows marshaling Clojure data back and forth to native data structures.

+

This can be used to implement out variables often seen in native code.

+
(defcfn out-int
+  "out_int" [::mem/pointer] ::mem/void
+  native-fn
+  [i]
+  (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int])]
+    (native-fn int-ptr)
+    (mem/deserialize int-ptr [::mem/pointer ::mem/int])))
+
\ No newline at end of file diff --git a/docs/02-Memory-Management.html b/docs/02-Memory-Management.html new file mode 100644 index 0000000..a0489fa --- /dev/null +++ b/docs/02-Memory-Management.html @@ -0,0 +1,18 @@ + +Memory Management

Memory Management

+

In order to serialize any non-primitive type, off-heap memory needs to be allocated. When memory is allocated inside the JVM, the memory is associated with an arena. If none is provided, the arena is an implicit arena, and the memory will be freed when the serialized object is garbage collected.

+

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 well as changing allocation performance. Instead of an implicit arena, there are other kinds of arenas as well. A confined-arena is a thread-local arena. Confined arenas are Closeable, which means they should usually be used in a with-open form. When a confined-arena is closed, it immediately frees all the memory associated with it. The previous example, out-int, can be implemented with a confined arena.

+
(defcfn out-int
+  "out_int" [::mem/pointer] ::mem/void
+  native-fn
+  [i]
+  (with-open [arena (mem/confined-arena)]
+    (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int] arena)]
+      (native-fn int-ptr)
+      (mem/deserialize int-ptr [::mem/pointer ::mem/int]))))
+
+

This will free the pointer immediately upon leaving the function.

+

When memory needs to be accessible from multiple threads, there’s shared-arena. When a shared-arena is .closed, it will release all its associated memory immediately, and so this should only be done once all other threads are done accessing memory associated with it.

+

In addition, two non-Closeable arenas are global-arena, which never frees the resources associated with it, and auto-arena, which is an arena that frees its resources once all of them are unreachable during a garbage collection cycle, like an implicit arena, but potentially for multiple allocations rather than just one.

+
\ No newline at end of file diff --git a/docs/03-Builtin-Types.html b/docs/03-Builtin-Types.html new file mode 100644 index 0000000..ca6ec7d --- /dev/null +++ b/docs/03-Builtin-Types.html @@ -0,0 +1,30 @@ + +Built-in Types

Built-in Types

+

Primitives

+

Arrays

+

Pointers

+

Structs

+

Enums

+

Flagsets

+

Unions

+

Unions in coffi are rather limited. They can be serialized, but not deserialized without external information.

+
[::mem/union
+ #{::mem/float ::mem/double}
+ :dispatch #(cond
+             (float? %) ::mem/float
+             (double? %) ::mem/double)]
+
+

This is a minimal union in coffi. If the :dispatch keyword argument is not passed, then the union cannot be serialized, as coffi would not know which type to serialize the values as. In the example with a tagged union, a dispatch function was not provided because the type was only used for the native layout.

+

In addition to a dispatch function, when serializing a union an extract function may also be provided. In the case of the value in the tagged union from before, it could be represented for serialization purposes like so:

+
[::mem/union
+ #{::mem/int ::mem/c-string}
+ :dispatch #(case (first %)
+              :ok ::mem/int
+              :err ::mem/c-string)
+ :extract second]
+
+

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 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 to the user to call deserialize-from on that segment with the appropriate type.

+

Raw Types

+
\ No newline at end of file diff --git a/docs/04-Custom-Types.html b/docs/04-Custom-Types.html new file mode 100644 index 0000000..50e6805 --- /dev/null +++ b/docs/04-Custom-Types.html @@ -0,0 +1,82 @@ + +Custom Types

Custom Types

+

Custom types with serializers and deserializers may be created. This is done using two sets of three multimethods which can be extended by the user. For any given type, only one set need be implemented.

+

Two examples of custom types are given here, one is a 3d vector, and the other an example of a tagged union.

+

Vector3

+

For the vector type, it will serialize to a pointer to an array of three floats.

+

The multimethod primitive-type returns the primitive type that a given type serializes to. For this example, it should be a pointer.

+
(defmethod mem/primitive-type ::vector
+  [_type]
+  ::mem/pointer)
+
+

For any type which doesn’t serialize to a primitive, it returns nil, and therefore need not be overriden.

+

Next is serialize* and deserialize*, multimethods that work with types that serialize to primitives.

+
(defmethod mem/serialize* ::vector
+  [obj _type arena]
+  (mem/serialize obj [::mem/array ::mem/float 3] arena))
+
+(defmethod mem/deserialize* ::vector
+  [segment _type]
+  (mem/deserialize (mem/reinterpret segment (mem/size-of [::mem/array ::mem/float 3]))
+                   [::mem/array ::mem/float 3]))
+
+

The reinterpret function allows you to take a segment and decorate it with a 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 arena of the pointer, we could use reinterpret to ensure it’s freed. For example if a free-vector! function that takes a pointer exists, we could use this:

+
(defcfn returns-vector
+  "returns_vector" [] ::mem/pointer
+  native-fn
+  [arena]
+  (let [ret-ptr (native-fn)]
+    (-> (reinterpret ret-ptr (mem/size-of ::vector) arena free-vector!)
+        (deserialize ::vector))))
+
+

This function takes an arena and returns the deserialized vector, and it will free the pointer when the arena closes.

+

Tagged Union

+

For the tagged union type, we will represent the value as a vector of a keyword naming the tag and the value. The type itself will need to take arguments, similar to struct. For example, if we were to represent a result type like in Rust, we might have the following values:

+
[:ok 5]
+[:err "Invalid number format"]
+
+

To represent this, we can have a tagged-union type. For this instance of the result type, it may look like this:

+
[::tagged-union [:ok :err] {:ok ::mem/int :err ::mem/c-string}]
+
+

The native representation of these objects is a struct of the tag and a union of the value. In order to correctly serialize the data and pass it to native code, we need a representation of the native layout of the data. The c-layout multimethod provides that.

+
(defmethod mem/c-layout ::tagged-union
+  [[_tagged-union tags type-map]]
+  (mem/c-layout [::mem/struct
+                 [[:tag ::mem/long]
+                  [:value [::mem/union (vals type-map)]]]]))
+
+

Types with type arguments are represented as vectors of the type name and any additional arguments. The type name is what is dispatched on for the multimethods.

+

Now that we have a native layout, we need to be able to serialize and deserialize the value into and out of memory segments. This is accomplished with serialize-into and deserialize-from.

+
(defn item-index
+  "Gets the index of the first occurance of `item` in `coll`."
+  [coll item]
+  (first
+   (->> coll
+        (map-indexed vector)
+        (filter (comp #{item} second))
+        (map first))))
+
+(defmethod mem/serialize-into ::tagged-union
+  [obj [_tagged-union tags type-map] segment arena]
+  (mem/serialize-into
+   {:tag (item-index tags (first obj))
+    :value (second obj)}
+   [::mem/struct
+    [[:tag ::mem/long]
+     [:value (get type-map (first obj))]]]
+   segment
+   arena))
+
+

This serialization method is rather simple, it just turns the vector value into a map, and serializes it as a struct, choosing the type of the value based on the tag.

+
(defmethod mem/deserialize-from ::tagged-union
+  [segment [_tagged-union tags type-map]]
+  (let [tag (mem/deserialize-from segment ::mem/long)]
+    [(nth tags tag)
+     (mem/deserialize-from
+      (mem/slice segment (mem/size-of ::mem/long))
+      (get type-map tag))]))
+
+

Deserialization is a little more complex. First the tag is retrieved from the beginning of the segment, and then the type of the value is decided based on that before it is deserialized.

+
\ No newline at end of file diff --git a/docs/05-Low-Level-Wrappers.html b/docs/05-Low-Level-Wrappers.html new file mode 100644 index 0000000..0b9ab2c --- /dev/null +++ b/docs/05-Low-Level-Wrappers.html @@ -0,0 +1,51 @@ + +Low-Level Wrappers

Low-Level Wrappers

+

Unwrapped Native Handles

+

Some native libraries work with handles to large amounts of data at once, making it undesirable to marshal data back and forth from Clojure, both because it’s not necessary to work with the data in Clojure directly, or also because of the high (de)serialization costs associated with marshaling. In cases like these, unwrapped native handles are desirable.

+

The functions make-downcall and make-varargs-factory are also provided to create raw function handles.

+
(def raw-strlen (ffi/make-downcall "strlen" [::mem/c-string] ::mem/long))
+(raw-strlen (mem/serialize "hello" ::mem/c-string))
+;; => 5
+
+

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 pointers, that is MemorySegment, and for composite types like structs and unions, that is also MemorySegment. MemorySegment comes from the java.lang.foreign package.

+

In addition, when a raw handle returns a composite type represented with a MemorySegment, it requires an additional first argument, a SegmentAllocator, which can be acquired with arena-allocator to get one associated with a 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 manner:

+
[::ffi/fn [::mem/int] ::mem/int :raw-fn? true]
+
+

Clojure functions serialized to this type will have their arguments and return value exactly match the types specified and will not perform any serialization or deserialization at their boundaries.

+

One important caveat to consider when writing wrappers for performance-sensitive functions is that the convenience macro defcfn that coffi provides will already perform no serialization or deserialization on primitive arguments and return types, so for functions with only primitive argument and return types there is no performance reason to choose unwrapped native handles over the convenience macro.

+

Manual (De)Serialization

+

Coffi uses multimethods to dispatch to (de)serialization functions to enable code that’s generic over the types it operates on. However, in cases where you know the exact types that you will be (de)serializing and the multimethod dispatch overhead is too high a cost, it may be appropriate to manually handle (de)serializing data. This will often be done paired with Unwrapped Native Handles.

+

Convenience functions are provided to both read and write all primitive types and addresses, including byte order.

+

As an example, when wrapping a function that returns an array of big-endian floats, the following code might be used.

+
;; int returns_float_array(float **arr)
+(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))
+
+(defn returns-float-array
+  []
+  (with-open [arena (mem/confined-arena)]
+    ;; float *out_floats;
+    ;; int num_floats = returns_float_array(&out_floats);
+    (let [out-floats (mem/alloc mem/pointer-size arena)
+          num-floats (returns-float-array* out-floats)
+          floats-addr (mem/read-address out-floats)
+          floats-slice (mem/reinterpret floats-addr (unchecked-multiply-int mem/float-size num-floats))]
+      ;; Using a try/finally to perform an operation when the stack frame exits,
+      ;; but not to try to catch anything.
+      (try
+        (loop [floats (transient [])
+               index 0]
+          (if (>= index num-floats)
+            (persistent! floats)
+            (recur (conj! floats (mem/read-float floats-slice
+                                                 (unchecked-multiply-int index mem/float-size)
+                                                 mem/big-endian))
+                   (unchecked-inc-int index))))
+        (finally
+          (release-floats* floats-addr))))))
+
+

The above code manually performs all memory operations rather than relying on coffi’s dispatch. This will be more performant, but because multimethod overhead is usually relatively low, it’s recommended to use the multimethod variants for convenience in colder functions.

+
\ No newline at end of file diff --git a/docs/50-Data-Model.html b/docs/50-Data-Model.html new file mode 100644 index 0000000..aba841f --- /dev/null +++ b/docs/50-Data-Model.html @@ -0,0 +1,25 @@ + +Data Model

Data Model

+

In addition to the macros and functions provided to build a Clojure API for native libraries, facilities are provided for taking data and loading all the symbols specified by it. This can be useful if a library provides (or an external provider maintains) a data representation of their API, as Clojure data to represent it may be programmatically generated from these sources.

+

The data to represent an API is a map with the following form:

+
(def strlen-libspec
+  {:strlen {:type :function
+            :symbol "strlen"
+            :function/args [::mem/c-string]
+            :function/ret ::mem/long}})
+
+

Each key in this map represents a single symbol to be loaded. The value is a map with at least the keys :type and :symbol. These are the currently recognized types:

+
    +
  • function
  • +
  • varargs-factory
  • +
  • const
  • +
  • static-var
  • +
+

Each one has its own set of additional keys which can be added to the map. Both function and varargs-factory have the three keys :function/args, :function/ret, and :function/raw-fn?. The const type has :const/type and static-var has :static-var/type.

+

This data can be passed to the function reify-libspec, which will take the data and return a map from the same keys as the input map to whatever value is appropriate for a given symbol type (e.g. a Clojure function for function, a value for const, etc.).

+
(ffi/reify-libspec strlen-libspec)
+;; => {:strlen #function[...]}
+
+

This functionality can be extended by specifying new types as implementations of the multimethod reify-symbolspec, although it’s recommended that for any library authors who do so, namespaced keywords be used to name types.

+
\ No newline at end of file diff --git a/docs/99-Benchmarks.html b/docs/99-Benchmarks.html new file mode 100644 index 0000000..0009067 --- /dev/null +++ b/docs/99-Benchmarks.html @@ -0,0 +1,258 @@ + +Benchmarks

Benchmarks

+

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 of each available option. It’s an established fact that JNA (used by all three alternative libraries on JDK <16) introduces more overhead when calling native code than JNI does.

+

In order to provide a benchmark to see how much of a difference the different native interfaces make, we can use criterium to benchmark each. GLFW’s glfwGetTime function will be used for the test as it performs a simple operation, and is conveniently already wrapped in JNI by the excellent LWJGL library.

+

The following benchmarks were run on a Lenovo Thinkpad with an Intel i7-10610U running Manjaro Linux, using Clojure 1.10.3 on Java 17.

+

JNI

+

The baseline for performance is the JNI. Using LWJGL it’s relatively simple to benchmark. The following Clojure CLI command will start a repl with LWJGL and criterium loaded.

+
$ clj -Sdeps '{:deps {org.lwjgl/lwjgl {:mvn/version "3.2.3"}
+                      org.lwjgl/lwjgl-glfw {:mvn/version "3.2.3"}
+                      org.lwjgl/lwjgl$natives-linux {:mvn/version "3.2.3"}
+                      org.lwjgl/lwjgl-glfw$natives-linux {:mvn/version "3.2.3"}
+                      criterium/criterium {:mvn/version "0.4.6"}}}'
+
+

Then from the repl

+
user=> (import 'org.lwjgl.glfw.GLFW)
+org.lwjgl.glfw.GLFW
+user=> (require '[criterium.core :as bench])
+nil
+user=> (GLFW/glfwInit)
+true
+user=> (bench/bench (GLFW/glfwGetTime) :verbose)
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 17+35-2724
+Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2667074721.basis
+Evaluation count : 1613349900 in 60 samples of 26889165 calls.
+      Execution time sample mean : 32.698446 ns
+             Execution time mean : 32.697811 ns
+Execution time sample std-deviation : 1.274600 ns
+    Execution time std-deviation : 1.276437 ns
+   Execution time lower quantile : 30.750813 ns ( 2.5%)
+   Execution time upper quantile : 33.757662 ns (97.5%)
+                   Overhead used : 6.400704 ns
+nil
+
+

GLFW requires that we initialize it before calling the glfwGetTime function. Besides that this is a simple interop call which directly maps to the native function.

+

This gives us a basis of 32.7 ns +/-1.3 ns. All other libraries will be evaluated relative to this result.

+

To ensure fairness, we’ll also get that overhead value to be used in further tests.

+
user=> bench/estimated-overhead-cache
+6.400703613065185E-9
+
+

Coffi

+

The dependencies when using coffi are simpler, but it also requires some JVM options to support the foreign access api.

+
$ clj -Sdeps '{:deps {org.suskalo/coffi {:mvn/version "0.1.205"}
+                      criterium/criterium {:mvn/version "0.4.6"}}}' \
+      -J--add-modules=jdk.incubator.foreign \
+      -J--enable-native-access=ALL-UNNAMED
+
+

In order to ensure fair comparisons, we’re going to use the same overhead value on each run, so before we do the benchmark we’ll set it to the observed value from last time.

+
user=> (require '[criterium.core :as bench])
+nil
+user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9))
+6.400703613065185E-9
+user=> (require '[coffi.ffi :as ffi])
+nil
+user=> (require '[coffi.mem :as mem])
+nil
+user=> (ffi/load-system-library "glfw")
+nil
+user=> ((ffi/cfn "glfwInit" [] ::mem/int))
+1
+user=> (let [f (ffi/cfn "glfwGetTime" [] ::mem/double)]
+         (bench/bench (f) :verbose))
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 17+35-2724
+Runtime arguments: --add-modules=jdk.incubator.foreign --enable-native-access=ALL-UNNAMED -Dclojure.basis=/home/jsusk/.clojure/.cpcache/72793624.basis
+Evaluation count : 1657995600 in 60 samples of 27633260 calls.
+      Execution time sample mean : 31.382665 ns
+             Execution time mean : 31.386493 ns
+Execution time sample std-deviation : 1.598571 ns
+    Execution time std-deviation : 1.608818 ns
+   Execution time lower quantile : 29.761194 ns ( 2.5%)
+   Execution time upper quantile : 33.228276 ns (97.5%)
+                   Overhead used : 6.400704 ns
+nil
+
+

This result is about 1.3 ns faster, and while that is less than the standard deviation of 1.6, it’s quite close to it.

+

Clojure-JNA

+

Clojure-JNA uses the JNA library, which was designed to provide Java with an easy way to access native libraries, but which is known for not having the greatest performance. Since this is an older project, I’m also including the clojure dependency to ensure the correct version is used.

+
$ clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
+                      net.n01se/clojure-jna {:mvn/version "1.0.0"}
+                      criterium/criterium {:mvn/version "0.4.6"}}}'
+
+

The naive way to call the function using Clojure-JNA is to use jna/invoke.

+
user=> (require '[criterium.core :as bench])
+nil
+user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9))
+6.400703613065185E-9
+user=> (require '[net.n01se.clojure-jna :as jna])
+nil
+user=> (jna/invoke Integer glfw/glfwInit)
+1
+user=> (bench/bench (jna/invoke Double glfw/glfwGetTime) :verbose)
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 17+35-2724
+Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/3229486237.basis
+Evaluation count : 195948720 in 60 samples of 3265812 calls.
+      Execution time sample mean : 350.335614 ns
+             Execution time mean : 350.373520 ns
+Execution time sample std-deviation : 24.833070 ns
+    Execution time std-deviation : 24.755929 ns
+   Execution time lower quantile : 300.000019 ns ( 2.5%)
+   Execution time upper quantile : 365.759273 ns (97.5%)
+                   Overhead used : 6.400704 ns
+
+Found 13 outliers in 60 samples (21.6667 %)
+	low-severe	 12 (20.0000 %)
+	low-mild	 1 (1.6667 %)
+ Variance from outliers : 53.4220 % Variance is severely inflated by outliers
+nil
+
+

As you can see, this method of calling functions is very bad for performance, with call overhead dominating function runtime by an order of magnitude. That said, this isn’t a completely fair comparison, nor the most realistic, because this way of calling functions looks the function up on each invocation.

+

To adjust for this, we’ll use the jna/to-fn function to give a persistent handle to the function that we can call.

+
user=> (let [f (jna/to-fn Double glfw/glfwGetTime)]
+         (bench/bench (f) :verbose))
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 17+35-2724
+Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/3229486237.basis
+Evaluation count : 611095020 in 60 samples of 10184917 calls.
+      Execution time sample mean : 104.623634 ns
+             Execution time mean : 104.638406 ns
+Execution time sample std-deviation : 7.649296 ns
+    Execution time std-deviation : 7.638963 ns
+   Execution time lower quantile : 92.446016 ns ( 2.5%)
+   Execution time upper quantile : 110.258832 ns (97.5%)
+                   Overhead used : 6.400704 ns
+nil
+
+

This is much better, but is still about 3x slower than JNI, meaning the overhead from using JNA is still bigger than the function runtime.

+

This performance penalty is still small in the scope of longer-running functions, and so may not be a concern for your application, but it is something to be aware of.

+

tech.jna

+

The tech.jna library is similar in scope to Clojure-JNA, however was written to fit into an ecosystem of libraries meant for array-based programming for machine learning and data science.

+
$ clj -Sdeps '{:deps {techascent/tech.jna {:mvn/version "4.05"}
+                      criterium/criterium {:mvn/version "0.4.6"}}}'
+
+

This library is also quite simple to use, the only slightly odd thing I’m doing here is to dereference the var outside the benchmark in order to ensure it’s an apples-to-apples comparison. We don’t want var dereference time mucking up our benchmark.

+
user=> (require '[criterium.core :as bench])
+nil
+user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9))
+6.400703613065185E-9
+user=> (require '[tech.v3.jna :as jna])
+nil
+user=> (jna/def-jna-fn "glfw" glfwInit "initialize glfw" Integer)
+#'user/glfwInit
+user=> (glfwInit)
+Oct 09, 2021 10:30:50 AM clojure.tools.logging$eval1122$fn__1125 invoke
+INFO: Library glfw found at [:system "glfw"]
+1
+user=> (jna/def-jna-fn "glfw" glfwGetTime "gets the time as a double since init" Double)
+#'user/glfwGetTime
+user=> (let [f @#'glfwGetTime]
+         (bench/bench (f) :verbose))
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 17+35-2724
+Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2910209237.basis
+Evaluation count : 323281680 in 60 samples of 5388028 calls.
+      Execution time sample mean : 203.976803 ns
+             Execution time mean : 203.818712 ns
+Execution time sample std-deviation : 14.557312 ns
+    Execution time std-deviation : 14.614080 ns
+   Execution time lower quantile : 179.732593 ns ( 2.5%)
+   Execution time upper quantile : 213.929374 ns (97.5%)
+                   Overhead used : 6.400704 ns
+nil
+
+

This version is even slower than Clojure-JNA. I’m unsure where this overhead is coming from, but I’ll admit that I haven’t looked at their implementations very closely.

+

dtype-next

+

The library dtype-next replaced tech.jna in the toolkit of the group working on machine learning and array-based programming, and it includes support for composite data types including structs, as well as primitive functions and callbacks.

+

In addition, dtype-next has two different ffi backends. First is JNA, which is usable on any JDK version, and is what we’ll use for the first benchmark. Second is the Java 16 version of Project Panama, which will be shown next.

+

In order to use the dtype-next ffi with the JNA backend, the JNA library has to be included in the dependencies.

+
$ clj -Sdeps '{:deps {cnuernber/dtype-next {:mvn/version "8.032"}
+                      net.java.dev.jna/jna {:mvn/version "5.8.0"}
+                      criterium/criterium {:mvn/version "0.4.6"}}}'
+
+

The dtype-next library also requires some more ceremony around declaring native functions. One advantage this has is that multiple symbols with the same name can be loaded from different shared libraries, but it also does increase friction when defining native wrappers.

+

Some easier ways to define native wrappers are provided than what is seen here, but they share some disadvantages in documentation over the core methods provided in coffi, although they are comparable to the data model provided in coffi.

+
user=> (require '[criterium.core :as bench])
+nil
+user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9))
+6.400703613065185E-9
+user=> (require '[tech.v3.datatype.ffi :as dt-ffi])
+nil
+user=> (def fn-defs {:glfwInit {:rettype :int32} :glfwGetTime {:rettype :float64}})
+#'user/fn-defs
+user=> (def library-def (dt-ffi/define-library fn-defs))
+#'user/library-def
+user=> (def library-instance (dt-ffi/instantiate-library library-def "/usr/lib/libglfw.so"))
+#'user/library-instance
+user=> (def init (:glfwInit @library-instance))
+#'user/init
+user=> (init)
+1
+user=> (let [f (:glfwGetTime @library-instance)]
+         (bench/bench (f) :verbose))
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 17+35-2724
+Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/643862289.basis
+Evaluation count : 710822100 in 60 samples of 11847035 calls.
+      Execution time sample mean : 90.900112 ns
+             Execution time mean : 90.919917 ns
+Execution time sample std-deviation : 6.463312 ns
+    Execution time std-deviation : 6.470108 ns
+   Execution time lower quantile : 79.817126 ns ( 2.5%)
+   Execution time upper quantile : 95.454652 ns (97.5%)
+                   Overhead used : 6.400704 ns
+nil
+
+

This version of JNA usage is significantly faster than either of the other JNA libraries, but is still substantially slower than using JNI or coffi.

+

In addition to the JNA backend, dtype-next has a Java 16-specific backend that uses an older version of Panama. This version requires similar setup to coffi in order to run.

+
$ clj -Sdeps '{:deps {cnuernber/dtype-next {:mvn/version "8.032"}
+                      criterium/criterium {:mvn/version "0.4.6"}}}' \
+      -J--add-modules=jdk.incubator.foreign \
+      -J-Dforeign.restricted=permit \
+      -J--add-opens=java.base/java.lang=ALL-UNNAMED \
+      -J-Djava.library.path=/usr/lib/x86_64-linux-gnu
+
+

The actual code to run the benchmark is identical to the last example, but is reproduced here for completeness.

+
user=> (require '[criterium.core :as bench])
+nil
+user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9))
+6.400703613065185E-9
+user=> (require '[tech.v3.datatype.ffi :as dt-ffi])
+nil
+user=> (def fn-defs {:glfwInit {:rettype :int32} :glfwGetTime {:rettype :float64}})
+#'user/fn-defs
+user=> (def library-def (dt-ffi/define-library fn-defs))
+#'user/library-def
+user=> (def library-instance (dt-ffi/instantiate-library library-def "/usr/lib/libglfw.so"))
+#'user/library-instance
+user=> (def init (:glfwInit @library-instance))
+#'user/init
+user=> (init)
+1
+user=> (let [f (:glfwGetTime @library-instance)]
+         (bench/bench (f) :verbose))
+amd64 Linux 5.10.68-1-MANJARO 8 cpu(s)
+OpenJDK 64-Bit Server VM 16.0.2+7
+Runtime arguments: --add-modules=jdk.incubator.foreign -Dforeign.restricted=permit --add-opens=java.base/java.lang=ALL-UNNAMED -Djava.library.path=/usr/lib/x86_64-linux-gnu -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2337051659.basis
+Evaluation count : 1588513080 in 60 samples of 26475218 calls.
+      Execution time sample mean : 58.732468 ns
+             Execution time mean : 58.647361 ns
+Execution time sample std-deviation : 9.732389 ns
+    Execution time std-deviation : 9.791738 ns
+   Execution time lower quantile : 31.318115 ns ( 2.5%)
+   Execution time upper quantile : 65.449222 ns (97.5%)
+                   Overhead used : 6.400704 ns
+
+Found 14 outliers in 60 samples (23.3333 %)
+	low-severe	 8 (13.3333 %)
+	low-mild	 4 (6.6667 %)
+	high-mild	 2 (3.3333 %)
+ Variance from outliers : 87.6044 % Variance is severely inflated by outliers
+nil
+
+

Not reproduced here, but notable for comparison, in my testing Java 16’s version of the JNI version performed about the same.

+

This is significantly faster than the JNA version of dtype-next, but it is still slower than modern Panama. This is likely to simply be a result of optimizations and changes to the Panama API, and when dtype-next is updated to use the Java 17 version of Panama I expect it will perform in line with coffi, but this benchmark will be reproduced when this happens. Still, this shows that as it stands, coffi is the fastest FFI available to Clojure developers.

+
\ No newline at end of file diff --git a/docs/articles/01-Getting-Started.md b/docs/articles/01-Getting-Started.md index f190cc2..6fb3cc6 100644 --- a/docs/articles/01-Getting-Started.md +++ b/docs/articles/01-Getting-Started.md @@ -307,3 +307,37 @@ atomic semantics either. The memory that backs the static variable can be fetched with the function `static-variable-segment`, which can be used to pass a pointer to the static variable to native functions that require it. + +### Complex Wrappers +Some functions require more complex code to map nicely to a Clojure function. +The `defcfn` macro provides facilities to wrap the native function with some +Clojure code to make this easier. + +```clojure +(defcfn takes-array + "takes_array_with_count" [::mem/pointer ::mem/long] ::mem/void + native-fn + [ints] + (let [arr-len (count ints) + int-array (mem/serialize ints [::mem/array ::mem/int arr-len])] + (native-fn int-array arr-len))) +``` + +The symbol `native-fn` can be any unqualified symbol, and names the native +function being wrapped. It must be called in the function body below if you want +to call the native code. + +This `serialize` function has a paired `deserialize`, and allows marshaling +Clojure data back and forth to native data structures. + +This can be used to implement out variables often seen in native code. + +```clojure +(defcfn out-int + "out_int" [::mem/pointer] ::mem/void + native-fn + [i] + (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int])] + (native-fn int-ptr) + (mem/deserialize int-ptr [::mem/pointer ::mem/int]))) +``` diff --git a/docs/articles/02-Memory-Management.md b/docs/articles/02-Memory-Management.md new file mode 100644 index 0000000..2826d31 --- /dev/null +++ b/docs/articles/02-Memory-Management.md @@ -0,0 +1,38 @@ +# Memory Management +In order to serialize any non-primitive type, off-heap memory needs to be +allocated. When memory is allocated inside the JVM, the memory is associated +with an arena. If none is provided, the arena is an implicit arena, and the +memory will be freed when the serialized object is garbage collected. + +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 +well as changing allocation performance. Instead of an implicit arena, there +are other kinds of arenas as well. A `confined-arena` is a thread-local arena. +Confined arenas are `Closeable`, which means they should usually be used in a +`with-open` form. When a `confined-arena` is closed, it immediately frees all +the memory associated with it. The previous example, `out-int`, can be +implemented with a confined arena. + +```clojure +(defcfn out-int + "out_int" [::mem/pointer] ::mem/void + native-fn + [i] + (with-open [arena (mem/confined-arena)] + (let [int-ptr (mem/serialize i [::mem/pointer ::mem/int] arena)] + (native-fn int-ptr) + (mem/deserialize int-ptr [::mem/pointer ::mem/int])))) +``` + +This will free the pointer immediately upon leaving the function. + +When memory needs to be accessible from multiple threads, there's +`shared-arena`. When a `shared-arena` is `.close`d, it will release all its +associated memory immediately, and so this should only be done once all other +threads are done accessing memory associated with it. + +In addition, two non-`Closeable` arenas are `global-arena`, which never frees +the resources associated with it, and `auto-arena`, which is an arena that frees +its resources once all of them are unreachable during a garbage collection +cycle, like an implicit arena, but potentially for multiple allocations rather +than just one. diff --git a/docs/articles/03-Builtin-Types.md b/docs/articles/03-Builtin-Types.md new file mode 100644 index 0000000..81d8cfb --- /dev/null +++ b/docs/articles/03-Builtin-Types.md @@ -0,0 +1,53 @@ +# Built-in Types + +### Primitives + +### Arrays + +### Pointers + +### Structs + +### Enums + +### Flagsets + +### Unions +Unions in coffi are rather limited. They can be serialized, but not deserialized +without external information. + +```clojure +[::mem/union + #{::mem/float ::mem/double} + :dispatch #(cond + (float? %) ::mem/float + (double? %) ::mem/double)] +``` + +This is a minimal union in coffi. If the `:dispatch` keyword argument is not +passed, then the union cannot be serialized, as coffi would not know which type +to serialize the values as. In the example with a tagged union, a dispatch +function was not provided because the type was only used for the native layout. + +In addition to a dispatch function, when serializing a union an extract function +may also be provided. In the case of the value in the tagged union from before, +it could be represented for serialization purposes like so: + +```clojure +[::mem/union + #{::mem/int ::mem/c-string} + :dispatch #(case (first %) + :ok ::mem/int + :err ::mem/c-string) + :extract second] +``` + +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 +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 +to the user to call `deserialize-from` on that segment with the appropriate +type. + +### Raw Types diff --git a/docs/articles/04-Custom-Types.md b/docs/articles/04-Custom-Types.md new file mode 100644 index 0000000..e785d9d --- /dev/null +++ b/docs/articles/04-Custom-Types.md @@ -0,0 +1,136 @@ +# Custom Types +Custom types with serializers and deserializers may be created. This is done +using two sets of three multimethods which can be extended by the user. For any +given type, only one set need be implemented. + +Two examples of custom types are given here, one is a 3d vector, and the other +an example of a tagged union. + +### Vector3 +For the vector type, it will serialize to a pointer to an array of three floats. + +The multimethod `primitive-type` returns the primitive type that a given type +serializes to. For this example, it should be a pointer. + +```clojure +(defmethod mem/primitive-type ::vector + [_type] + ::mem/pointer) +``` + +For any type which doesn't serialize to a primitive, it returns nil, and +therefore need not be overriden. + +Next is `serialize*` and `deserialize*`, multimethods that work with types that +serialize to primitives. + +```clojure +(defmethod mem/serialize* ::vector + [obj _type arena] + (mem/serialize obj [::mem/array ::mem/float 3] arena)) + +(defmethod mem/deserialize* ::vector + [segment _type] + (mem/deserialize (mem/reinterpret segment (mem/size-of [::mem/array ::mem/float 3])) + [::mem/array ::mem/float 3])) +``` + +The `reinterpret` function allows you to take a segment and decorate it with a +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 arena of the pointer, we could use +`reinterpret` to ensure it's freed. For example if a `free-vector!` function +that takes a pointer exists, we could use this: + +```clojure +(defcfn returns-vector + "returns_vector" [] ::mem/pointer + native-fn + [arena] + (let [ret-ptr (native-fn)] + (-> (reinterpret ret-ptr (mem/size-of ::vector) arena free-vector!) + (deserialize ::vector)))) +``` + +This function takes an arena and returns the deserialized vector, and it will +free the pointer when the arena closes. + +### Tagged Union +For the tagged union type, we will represent the value as a vector of a keyword +naming the tag and the value. The type itself will need to take arguments, +similar to `struct`. For example, if we were to represent a result type like in +Rust, we might have the following values: + +```clojure +[:ok 5] +[:err "Invalid number format"] +``` + +To represent this, we can have a `tagged-union` type. For this instance of the +result type, it may look like this: + +```clojure +[::tagged-union [:ok :err] {:ok ::mem/int :err ::mem/c-string}] +``` + +The native representation of these objects is a struct of the tag and a union of +the value. In order to correctly serialize the data and pass it to native code, +we need a representation of the native layout of the data. The `c-layout` +multimethod provides that. + +```clojure +(defmethod mem/c-layout ::tagged-union + [[_tagged-union tags type-map]] + (mem/c-layout [::mem/struct + [[:tag ::mem/long] + [:value [::mem/union (vals type-map)]]]])) +``` + +Types with type arguments are represented as vectors of the type name and any +additional arguments. The type name is what is dispatched on for the +multimethods. + +Now that we have a native layout, we need to be able to serialize and +deserialize the value into and out of memory segments. This is accomplished with +`serialize-into` and `deserialize-from`. + +```clojure +(defn item-index + "Gets the index of the first occurance of `item` in `coll`." + [coll item] + (first + (->> coll + (map-indexed vector) + (filter (comp #{item} second)) + (map first)))) + +(defmethod mem/serialize-into ::tagged-union + [obj [_tagged-union tags type-map] segment arena] + (mem/serialize-into + {:tag (item-index tags (first obj)) + :value (second obj)} + [::mem/struct + [[:tag ::mem/long] + [:value (get type-map (first obj))]]] + segment + arena)) +``` + +This serialization method is rather simple, it just turns the vector value into +a map, and serializes it as a struct, choosing the type of the value based on +the tag. + +```clojure +(defmethod mem/deserialize-from ::tagged-union + [segment [_tagged-union tags type-map]] + (let [tag (mem/deserialize-from segment ::mem/long)] + [(nth tags tag) + (mem/deserialize-from + (mem/slice segment (mem/size-of ::mem/long)) + (get type-map tag))])) +``` + +Deserialization is a little more complex. First the tag is retrieved from the +beginning of the segment, and then the type of the value is decided based on +that before it is deserialized. diff --git a/docs/articles/05-Low-Level-Wrappers.md b/docs/articles/05-Low-Level-Wrappers.md new file mode 100644 index 0000000..e0f131f --- /dev/null +++ b/docs/articles/05-Low-Level-Wrappers.md @@ -0,0 +1,95 @@ +# Low-Level Wrappers + +### Unwrapped Native Handles +Some native libraries work with handles to large amounts of data at once, making +it undesirable to marshal data back and forth from Clojure, both because it's +not necessary to work with the data in Clojure directly, or also because of the +high (de)serialization costs associated with marshaling. In cases like these, +unwrapped native handles are desirable. + +The functions `make-downcall` and `make-varargs-factory` are also provided to +create raw function handles. + +```clojure +(def raw-strlen (ffi/make-downcall "strlen" [::mem/c-string] ::mem/long)) +(raw-strlen (mem/serialize "hello" ::mem/c-string)) +;; => 5 +``` + +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 +pointers, that is `MemorySegment`, and for composite types like structs and +unions, that is also `MemorySegment`. `MemorySegment` comes from the +`java.lang.foreign` package. + +In addition, when a raw handle returns a composite type represented with a +`MemorySegment`, it requires an additional first argument, a `SegmentAllocator`, +which can be acquired with `arena-allocator` to get one associated with a +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 +manner: + +```clojure +[::ffi/fn [::mem/int] ::mem/int :raw-fn? true] +``` + +Clojure functions serialized to this type will have their arguments and return +value exactly match the types specified and will not perform any serialization +or deserialization at their boundaries. + +One important caveat to consider when writing wrappers for performance-sensitive +functions is that the convenience macro `defcfn` that coffi provides will +already perform no serialization or deserialization on primitive arguments and +return types, so for functions with only primitive argument and return types +there is no performance reason to choose unwrapped native handles over the +convenience macro. + +### Manual (De)Serialization +Coffi uses multimethods to dispatch to (de)serialization functions to enable +code that's generic over the types it operates on. However, in cases where you +know the exact types that you will be (de)serializing and the multimethod +dispatch overhead is too high a cost, it may be appropriate to manually handle +(de)serializing data. This will often be done paired with [Unwrapped Native +Handles](#unwrapped-native-handles). + +Convenience functions are provided to both read and write all primitive types +and addresses, including byte order. + +As an example, when wrapping a function that returns an array of big-endian +floats, the following code might be used. + +``` clojure +;; int returns_float_array(float **arr) +(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)) + +(defn returns-float-array + [] + (with-open [arena (mem/confined-arena)] + ;; float *out_floats; + ;; int num_floats = returns_float_array(&out_floats); + (let [out-floats (mem/alloc mem/pointer-size arena) + num-floats (returns-float-array* out-floats) + floats-addr (mem/read-address out-floats) + floats-slice (mem/reinterpret floats-addr (unchecked-multiply-int mem/float-size num-floats))] + ;; Using a try/finally to perform an operation when the stack frame exits, + ;; but not to try to catch anything. + (try + (loop [floats (transient []) + index 0] + (if (>= index num-floats) + (persistent! floats) + (recur (conj! floats (mem/read-float floats-slice + (unchecked-multiply-int index mem/float-size) + mem/big-endian)) + (unchecked-inc-int index)))) + (finally + (release-floats* floats-addr)))))) +``` + +The above code manually performs all memory operations rather than relying on +coffi's dispatch. This will be more performant, but because multimethod overhead +is usually relatively low, it's recommended to use the multimethod variants for +convenience in colder functions. diff --git a/docs/articles/50-Data-Model.md b/docs/articles/50-Data-Model.md new file mode 100644 index 0000000..429255a --- /dev/null +++ b/docs/articles/50-Data-Model.md @@ -0,0 +1,44 @@ +# Data Model +In addition to the macros and functions provided to build a Clojure API for +native libraries, facilities are provided for taking data and loading all the +symbols specified by it. This can be useful if a library provides (or an +external provider maintains) a data representation of their API, as Clojure data +to represent it may be programmatically generated from these sources. + +The data to represent an API is a map with the following form: + +```clojure +(def strlen-libspec + {:strlen {:type :function + :symbol "strlen" + :function/args [::mem/c-string] + :function/ret ::mem/long}}) +``` + +Each key in this map represents a single symbol to be loaded. The value is a map +with at least the keys `:type` and `:symbol`. These are the currently recognized +types: + +- function +- varargs-factory +- const +- static-var + +Each one has its own set of additional keys which can be added to the map. Both +`function` and `varargs-factory` have the three keys `:function/args`, +`:function/ret`, and `:function/raw-fn?`. The `const` type has `:const/type` and +`static-var` has `:static-var/type`. + +This data can be passed to the function `reify-libspec`, which will take the +data and return a map from the same keys as the input map to whatever value is +appropriate for a given symbol type (e.g. a Clojure function for `function`, a +value for `const`, etc.). + +```clojure +(ffi/reify-libspec strlen-libspec) +;; => {:strlen #function[...]} +``` + +This functionality can be extended by specifying new types as implementations of +the multimethod `reify-symbolspec`, although it's recommended that for any +library authors who do so, namespaced keywords be used to name types. diff --git a/docs/articles/99-Benchmarks.md b/docs/articles/99-Benchmarks.md new file mode 100644 index 0000000..69ef11c --- /dev/null +++ b/docs/articles/99-Benchmarks.md @@ -0,0 +1,373 @@ +# Benchmarks +**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 +of each available option. It's an established fact that JNA (used by all three +alternative libraries on JDK <16) introduces more overhead when calling native +code than JNI does. + +In order to provide a benchmark to see how much of a difference the different +native interfaces make, we can use +[criterium](https://github.com/hugoduncan/criterium) to benchmark each. +[GLFW](https://www.glfw.org)'s +[`glfwGetTime`](https://www.glfw.org/docs/latest/group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a) +function will be used for the test as it performs a simple operation, and is +conveniently already wrapped in JNI by the excellent +[LWJGL](https://www.lwjgl.org/) library. + +The following benchmarks were run on a Lenovo Thinkpad with an Intel i7-10610U +running Manjaro Linux, using Clojure 1.10.3 on Java 17. + +### JNI +The baseline for performance is the JNI. Using LWJGL it's relatively simple to +benchmark. The following Clojure CLI command will start a repl with LWJGL and +criterium loaded. + +```sh +$ clj -Sdeps '{:deps {org.lwjgl/lwjgl {:mvn/version "3.2.3"} + org.lwjgl/lwjgl-glfw {:mvn/version "3.2.3"} + org.lwjgl/lwjgl$natives-linux {:mvn/version "3.2.3"} + org.lwjgl/lwjgl-glfw$natives-linux {:mvn/version "3.2.3"} + criterium/criterium {:mvn/version "0.4.6"}}}' +``` + +Then from the repl + +```clojure +user=> (import 'org.lwjgl.glfw.GLFW) +org.lwjgl.glfw.GLFW +user=> (require '[criterium.core :as bench]) +nil +user=> (GLFW/glfwInit) +true +user=> (bench/bench (GLFW/glfwGetTime) :verbose) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 17+35-2724 +Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2667074721.basis +Evaluation count : 1613349900 in 60 samples of 26889165 calls. + Execution time sample mean : 32.698446 ns + Execution time mean : 32.697811 ns +Execution time sample std-deviation : 1.274600 ns + Execution time std-deviation : 1.276437 ns + Execution time lower quantile : 30.750813 ns ( 2.5%) + Execution time upper quantile : 33.757662 ns (97.5%) + Overhead used : 6.400704 ns +nil +``` + +GLFW requires that we initialize it before calling the `glfwGetTime` function. +Besides that this is a simple interop call which directly maps to the native +function. + +This gives us a basis of 32.7 ns +/-1.3 ns. All other libraries will be +evaluated relative to this result. + +To ensure fairness, we'll also get that overhead value to be used in further +tests. + +```clojure +user=> bench/estimated-overhead-cache +6.400703613065185E-9 +``` + +### Coffi +The dependencies when using coffi are simpler, but it also requires some JVM +options to support the foreign access api. + +```sh +$ clj -Sdeps '{:deps {org.suskalo/coffi {:mvn/version "0.1.205"} + criterium/criterium {:mvn/version "0.4.6"}}}' \ + -J--add-modules=jdk.incubator.foreign \ + -J--enable-native-access=ALL-UNNAMED +``` + +In order to ensure fair comparisons, we're going to use the same overhead value +on each run, so before we do the benchmark we'll set it to the observed value +from last time. + +```clojure +user=> (require '[criterium.core :as bench]) +nil +user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) +6.400703613065185E-9 +user=> (require '[coffi.ffi :as ffi]) +nil +user=> (require '[coffi.mem :as mem]) +nil +user=> (ffi/load-system-library "glfw") +nil +user=> ((ffi/cfn "glfwInit" [] ::mem/int)) +1 +user=> (let [f (ffi/cfn "glfwGetTime" [] ::mem/double)] + (bench/bench (f) :verbose)) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 17+35-2724 +Runtime arguments: --add-modules=jdk.incubator.foreign --enable-native-access=ALL-UNNAMED -Dclojure.basis=/home/jsusk/.clojure/.cpcache/72793624.basis +Evaluation count : 1657995600 in 60 samples of 27633260 calls. + Execution time sample mean : 31.382665 ns + Execution time mean : 31.386493 ns +Execution time sample std-deviation : 1.598571 ns + Execution time std-deviation : 1.608818 ns + Execution time lower quantile : 29.761194 ns ( 2.5%) + Execution time upper quantile : 33.228276 ns (97.5%) + Overhead used : 6.400704 ns +nil +``` + +This result is about 1.3 ns faster, and while that is less than the standard +deviation of 1.6, it's quite close to it. + +### Clojure-JNA +Clojure-JNA uses the JNA library, which was designed to provide Java with an +easy way to access native libraries, but which is known for not having the +greatest performance. Since this is an older project, I'm also including the +clojure dependency to ensure the correct version is used. + +```sh +$ clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.10.3"} + net.n01se/clojure-jna {:mvn/version "1.0.0"} + criterium/criterium {:mvn/version "0.4.6"}}}' +``` + +The naive way to call the function using Clojure-JNA is to use `jna/invoke`. + +```clojure +user=> (require '[criterium.core :as bench]) +nil +user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) +6.400703613065185E-9 +user=> (require '[net.n01se.clojure-jna :as jna]) +nil +user=> (jna/invoke Integer glfw/glfwInit) +1 +user=> (bench/bench (jna/invoke Double glfw/glfwGetTime) :verbose) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 17+35-2724 +Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/3229486237.basis +Evaluation count : 195948720 in 60 samples of 3265812 calls. + Execution time sample mean : 350.335614 ns + Execution time mean : 350.373520 ns +Execution time sample std-deviation : 24.833070 ns + Execution time std-deviation : 24.755929 ns + Execution time lower quantile : 300.000019 ns ( 2.5%) + Execution time upper quantile : 365.759273 ns (97.5%) + Overhead used : 6.400704 ns + +Found 13 outliers in 60 samples (21.6667 %) + low-severe 12 (20.0000 %) + low-mild 1 (1.6667 %) + Variance from outliers : 53.4220 % Variance is severely inflated by outliers +nil +``` + +As you can see, this method of calling functions is very bad for performance, +with call overhead dominating function runtime by an order of magnitude. That +said, this isn't a completely fair comparison, nor the most realistic, because +this way of calling functions looks the function up on each invocation. + +To adjust for this, we'll use the `jna/to-fn` function to give a persistent +handle to the function that we can call. + +```clojure +user=> (let [f (jna/to-fn Double glfw/glfwGetTime)] + (bench/bench (f) :verbose)) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 17+35-2724 +Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/3229486237.basis +Evaluation count : 611095020 in 60 samples of 10184917 calls. + Execution time sample mean : 104.623634 ns + Execution time mean : 104.638406 ns +Execution time sample std-deviation : 7.649296 ns + Execution time std-deviation : 7.638963 ns + Execution time lower quantile : 92.446016 ns ( 2.5%) + Execution time upper quantile : 110.258832 ns (97.5%) + Overhead used : 6.400704 ns +nil +``` + +This is much better, but is still about 3x slower than JNI, meaning the overhead +from using JNA is still bigger than the function runtime. + +This performance penalty is still small in the scope of longer-running +functions, and so may not be a concern for your application, but it is something +to be aware of. + +### tech.jna +The tech.jna library is similar in scope to Clojure-JNA, however was written to +fit into an ecosystem of libraries meant for array-based programming for machine +learning and data science. + +```sh +$ clj -Sdeps '{:deps {techascent/tech.jna {:mvn/version "4.05"} + criterium/criterium {:mvn/version "0.4.6"}}}' +``` + +This library is also quite simple to use, the only slightly odd thing I'm doing +here is to dereference the var outside the benchmark in order to ensure it's an +apples-to-apples comparison. We don't want var dereference time mucking up our +benchmark. + +```clojure +user=> (require '[criterium.core :as bench]) +nil +user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) +6.400703613065185E-9 +user=> (require '[tech.v3.jna :as jna]) +nil +user=> (jna/def-jna-fn "glfw" glfwInit "initialize glfw" Integer) +#'user/glfwInit +user=> (glfwInit) +Oct 09, 2021 10:30:50 AM clojure.tools.logging$eval1122$fn__1125 invoke +INFO: Library glfw found at [:system "glfw"] +1 +user=> (jna/def-jna-fn "glfw" glfwGetTime "gets the time as a double since init" Double) +#'user/glfwGetTime +user=> (let [f @#'glfwGetTime] + (bench/bench (f) :verbose)) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 17+35-2724 +Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2910209237.basis +Evaluation count : 323281680 in 60 samples of 5388028 calls. + Execution time sample mean : 203.976803 ns + Execution time mean : 203.818712 ns +Execution time sample std-deviation : 14.557312 ns + Execution time std-deviation : 14.614080 ns + Execution time lower quantile : 179.732593 ns ( 2.5%) + Execution time upper quantile : 213.929374 ns (97.5%) + Overhead used : 6.400704 ns +nil +``` + +This version is even slower than Clojure-JNA. I'm unsure where this overhead is +coming from, but I'll admit that I haven't looked at their implementations very +closely. + +### dtype-next +The library dtype-next replaced tech.jna in the toolkit of the group working on +machine learning and array-based programming, and it includes support for +composite data types including structs, as well as primitive functions and +callbacks. + +In addition, dtype-next has two different ffi backends. First is JNA, which is +usable on any JDK version, and is what we'll use for the first benchmark. Second +is the Java 16 version of Project Panama, which will be shown next. + +In order to use the dtype-next ffi with the JNA backend, the JNA library has to +be included in the dependencies. + +```sh +$ clj -Sdeps '{:deps {cnuernber/dtype-next {:mvn/version "8.032"} + net.java.dev.jna/jna {:mvn/version "5.8.0"} + criterium/criterium {:mvn/version "0.4.6"}}}' +``` + +The dtype-next library also requires some more ceremony around declaring native +functions. One advantage this has is that multiple symbols with the same name +can be loaded from different shared libraries, but it also does increase +friction when defining native wrappers. + +Some easier ways to define native wrappers are provided than what is seen here, +but they share some disadvantages in documentation over the core methods +provided in coffi, although they are comparable to the data model provided in +coffi. + +```clojure +user=> (require '[criterium.core :as bench]) +nil +user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) +6.400703613065185E-9 +user=> (require '[tech.v3.datatype.ffi :as dt-ffi]) +nil +user=> (def fn-defs {:glfwInit {:rettype :int32} :glfwGetTime {:rettype :float64}}) +#'user/fn-defs +user=> (def library-def (dt-ffi/define-library fn-defs)) +#'user/library-def +user=> (def library-instance (dt-ffi/instantiate-library library-def "/usr/lib/libglfw.so")) +#'user/library-instance +user=> (def init (:glfwInit @library-instance)) +#'user/init +user=> (init) +1 +user=> (let [f (:glfwGetTime @library-instance)] + (bench/bench (f) :verbose)) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 17+35-2724 +Runtime arguments: -Dclojure.basis=/home/jsusk/.clojure/.cpcache/643862289.basis +Evaluation count : 710822100 in 60 samples of 11847035 calls. + Execution time sample mean : 90.900112 ns + Execution time mean : 90.919917 ns +Execution time sample std-deviation : 6.463312 ns + Execution time std-deviation : 6.470108 ns + Execution time lower quantile : 79.817126 ns ( 2.5%) + Execution time upper quantile : 95.454652 ns (97.5%) + Overhead used : 6.400704 ns +nil +``` + +This version of JNA usage is significantly faster than either of the other JNA +libraries, but is still substantially slower than using JNI or coffi. + +In addition to the JNA backend, dtype-next has a Java 16-specific backend that +uses an older version of Panama. This version requires similar setup to coffi in +order to run. + +```sh +$ clj -Sdeps '{:deps {cnuernber/dtype-next {:mvn/version "8.032"} + criterium/criterium {:mvn/version "0.4.6"}}}' \ + -J--add-modules=jdk.incubator.foreign \ + -J-Dforeign.restricted=permit \ + -J--add-opens=java.base/java.lang=ALL-UNNAMED \ + -J-Djava.library.path=/usr/lib/x86_64-linux-gnu +``` + +The actual code to run the benchmark is identical to the last example, but is +reproduced here for completeness. + +```clojure +user=> (require '[criterium.core :as bench]) +nil +user=> (alter-var-root #'bench/estimated-overhead-cache (constantly 6.400703613065185E-9)) +6.400703613065185E-9 +user=> (require '[tech.v3.datatype.ffi :as dt-ffi]) +nil +user=> (def fn-defs {:glfwInit {:rettype :int32} :glfwGetTime {:rettype :float64}}) +#'user/fn-defs +user=> (def library-def (dt-ffi/define-library fn-defs)) +#'user/library-def +user=> (def library-instance (dt-ffi/instantiate-library library-def "/usr/lib/libglfw.so")) +#'user/library-instance +user=> (def init (:glfwInit @library-instance)) +#'user/init +user=> (init) +1 +user=> (let [f (:glfwGetTime @library-instance)] + (bench/bench (f) :verbose)) +amd64 Linux 5.10.68-1-MANJARO 8 cpu(s) +OpenJDK 64-Bit Server VM 16.0.2+7 +Runtime arguments: --add-modules=jdk.incubator.foreign -Dforeign.restricted=permit --add-opens=java.base/java.lang=ALL-UNNAMED -Djava.library.path=/usr/lib/x86_64-linux-gnu -Dclojure.basis=/home/jsusk/.clojure/.cpcache/2337051659.basis +Evaluation count : 1588513080 in 60 samples of 26475218 calls. + Execution time sample mean : 58.732468 ns + Execution time mean : 58.647361 ns +Execution time sample std-deviation : 9.732389 ns + Execution time std-deviation : 9.791738 ns + Execution time lower quantile : 31.318115 ns ( 2.5%) + Execution time upper quantile : 65.449222 ns (97.5%) + Overhead used : 6.400704 ns + +Found 14 outliers in 60 samples (23.3333 %) + low-severe 8 (13.3333 %) + low-mild 4 (6.6667 %) + high-mild 2 (3.3333 %) + Variance from outliers : 87.6044 % Variance is severely inflated by outliers +nil +``` + +Not reproduced here, but notable for comparison, in my testing Java 16's version +of the JNI version performed about the same. + +This is significantly faster than the JNA version of dtype-next, but it is still +slower than modern Panama. This is likely to simply be a result of optimizations +and changes to the Panama API, and when dtype-next is updated to use the Java 17 +version of Panama I expect it will perform in line with coffi, but this +benchmark will be reproduced when this happens. Still, this shows that as it +stands, coffi is the fastest FFI available to Clojure developers. diff --git a/docs/coffi.ffi.html b/docs/coffi.ffi.html index 7cc8c2c..c911409 100644 --- a/docs/coffi.ffi.html +++ b/docs/coffi.ffi.html @@ -1,41 +1,41 @@ -coffi.ffi documentation

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

+coffi.ffi documentation

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

cfn

(cfn symbol args ret)

Constructs a Clojure function to call the native function referenced by symbol.

The function returned will serialize any passed arguments into the args types, and deserialize the return to the ret type.

If your args and ret are constants, then it is more efficient to call make-downcall followed by make-serde-wrapper because the latter has an inline definition which will result in less overhead from serdes.

-

const

(const symbol-or-addr type)

Gets the value of a constant stored in symbol-or-addr.

-

defcfn

macro

(defcfn name docstring? attr-map? symbol arg-types ret-type)(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)

Defines a Clojure function which maps to a native function.

+

const

(const symbol-or-addr type)

Gets the value of a constant stored in symbol-or-addr.

+

defcfn

macro

(defcfn name docstring? attr-map? symbol arg-types ret-type)(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)

Defines a Clojure function which maps to a native function.

name is the symbol naming the resulting var. symbol is a symbol or string naming the library symbol to link against. arg-types is a vector of qualified keywords representing the argument types. ret-type is a single qualified keyword representing the return type. fn-tail is the body of the function (potentially with multiple arities) which wraps the native one. Inside the function, native-fn 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.

If no fn-tail is provided, then the resulting function will simply serialize the arguments according to arg-types, call the native function, and deserialize the return value.

The number of args in the fn-tail need not match the number of arg-types for the native function. It need only call the native wrapper function with the correct arguments.

See serialize, deserialize, make-downcall.

-

defconst

macro

(defconst symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be the value of the given type from symbol-or-addr.

-

defvar

macro

(defvar symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be a reference to the native memory from symbol-or-addr.

-

ensure-symbol

(ensure-symbol symbol-or-addr)

Returns the argument if it is a MemorySegment, otherwise calls find-symbol on it.

-

find-symbol

(find-symbol sym)

Gets the MemorySegment of a symbol from the loaded libraries.

-

freset!

(freset! static-var newval)

Sets the value of static-var to newval, running it through serialize.

-

fswap!

(fswap! static-var f & args)

Non-atomically runs the function f over the value stored in static-var.

+

defconst

macro

(defconst symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be the value of the given type from symbol-or-addr.

+

defvar

macro

(defvar symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be a reference to the native memory from symbol-or-addr.

+

ensure-symbol

(ensure-symbol symbol-or-addr)

Returns the argument if it is a MemorySegment, otherwise calls find-symbol on it.

+

find-symbol

(find-symbol sym)

Gets the MemorySegment of a symbol from the loaded libraries.

+

freset!

(freset! static-var newval)

Sets the value of static-var to newval, running it through serialize.

+

fswap!

(fswap! static-var f & args)

Non-atomically runs the function f over the value stored in static-var.

The value is deserialized before passing it to f, and serialized before putting the value into static-var.

-

load-library

(load-library path)

Loads the library at path.

-

load-system-library

(load-system-library libname)

Loads the library named libname from the system’s load path.

-

make-downcall

(make-downcall symbol-or-addr args ret)

Constructs a downcall function reference to symbol-or-addr with the given args and ret types.

+

load-library

(load-library path)

Loads the library at path.

+

load-system-library

(load-system-library libname)

Loads the library named libname from the system’s load path.

+

make-downcall

(make-downcall symbol-or-addr args ret)

Constructs a downcall function reference to symbol-or-addr with the given args and ret types.

The function returned takes only arguments whose types match exactly the java-layout for that type, and returns an argument with exactly the java-layout of the ret type. This function will perform no serialization or deserialization of arguments or the return type.

If the ret type is non-primitive, then the returned function will take a first argument of a SegmentAllocator.

-

make-serde-varargs-wrapper

(make-serde-varargs-wrapper varargs-factory required-args ret-type)

Constructs a wrapper function for the varargs-factory which produces functions that serialize the arguments and deserialize the return value.

-

make-serde-wrapper

(make-serde-wrapper downcall arg-types ret-type)

Constructs a wrapper function for the downcall which serializes the arguments and deserializes the return value.

-

make-varargs-factory

(make-varargs-factory symbol required-args ret)

Returns a function for constructing downcalls with additional types for arguments.

+

make-serde-varargs-wrapper

(make-serde-varargs-wrapper varargs-factory required-args ret-type)

Constructs a wrapper function for the varargs-factory which produces functions that serialize the arguments and deserialize the return value.

+

make-serde-wrapper

(make-serde-wrapper downcall arg-types ret-type)

Constructs a wrapper function for the downcall which serializes the arguments and deserializes the return value.

+

make-varargs-factory

(make-varargs-factory symbol required-args ret)

Returns a function for constructing downcalls with additional types for arguments.

The required-args 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.

The returned function is memoized, so that only one downcall function will be generated per combination of argument types.

See make-downcall.

-

reify-libspec

(reify-libspec libspec)

Loads all the symbols specified in the libspec.

+

reify-libspec

(reify-libspec libspec)

Loads all the symbols specified in the libspec.

The value of each key of the passed map is transformed as by reify-symbolspec.

-

reify-symbolspec

multimethod

Takes a spec for a symbol reference and returns a live value for that type.

-

static-variable

(static-variable symbol-or-addr type)

Constructs a reference to a mutable value stored in symbol-or-addr.

+

reify-symbolspec

multimethod

Takes a spec for a symbol reference and returns a live value for that type.

+

static-variable

(static-variable symbol-or-addr type)

Constructs a reference to a mutable value stored in symbol-or-addr.

The returned value can be dereferenced, and has metadata.

See freset!, fswap!.

-

static-variable-segment

(static-variable-segment static-var)

Gets the backing MemorySegment from static-var.

+

static-variable-segment

(static-variable-segment static-var)

Gets the backing MemorySegment from static-var.

This is primarily useful when you need to pass the static variable’s address to a native function which takes an Addressable.

-

vacfn-factory

(vacfn-factory symbol required-args ret)

Constructs a varargs factory to call the native function referenced by symbol.

+

vacfn-factory

(vacfn-factory symbol required-args ret)

Constructs a varargs factory to call the native function referenced by symbol.

The function returned takes any number of type arguments and returns a specialized Clojure function for calling the native function with those arguments.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/coffi.layout.html b/docs/coffi.layout.html index bff4af1..0783ab7 100644 --- a/docs/coffi.layout.html +++ b/docs/coffi.layout.html @@ -1,6 +1,6 @@ -coffi.layout documentation

coffi.layout

Functions for adjusting the layout of structs.

+coffi.layout documentation

coffi.layout

Functions for adjusting the layout of structs.

with-c-layout

(with-c-layout struct-spec)

Forces a struct specification to C layout rules.

This will add padding fields between fields to match C alignment requirements.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/coffi.mem.html b/docs/coffi.mem.html index cc459be..0afc416 100644 --- a/docs/coffi.mem.html +++ b/docs/coffi.mem.html @@ -1,121 +1,121 @@ -coffi.mem documentation

coffi.mem

Functions for managing native allocations, memory arenas, and (de)serialization.

+coffi.mem documentation

coffi.mem

Functions for managing native allocations, memory arenas, and (de)serialization.

For any new type to be implemented, three multimethods must be overriden, but which three depends on the native representation of the type.

If the native representation of the type is a primitive (whether or not other data beyond the primitive is associated with it, as e.g. a pointer), then primitive-type must be overriden to return which primitive type it is serialized as, then serialize* and deserialize* should be overriden.

If the native representation of the type is a composite type, like a union, 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 overriden to allow marshaling values of the type into and out of memory segments.

address-of

(address-of addressable)

Gets the address of a given segment as a number.

-

address?

(address? addr)

Checks if an object is a memory address.

+

address?

(address? addr)

Checks if an object is a memory address.

nil is considered an address.

-

align-of

(align-of type)

The alignment in bytes of the given type.

-

alloc

(alloc size)(alloc size arena)(alloc size alignment arena)

Allocates size bytes.

+

align-of

(align-of type)

The alignment in bytes of the given type.

+

alloc

(alloc size)(alloc size arena)(alloc size alignment arena)

Allocates size bytes.

If an arena is provided, the allocation will be reclaimed when it is closed.

-

alloc-instance

(alloc-instance type)(alloc-instance type arena)

Allocates a memory segment for the given type.

-

alloc-with

(alloc-with allocator size)(alloc-with allocator size alignment)

Allocates size bytes using the allocator.

-

arena-allocator

(arena-allocator arena)

Constructs a SegmentAllocator from the given Arena.

+

alloc-instance

(alloc-instance type)(alloc-instance type arena)

Allocates a memory segment for the given type.

+

alloc-with

(alloc-with allocator size)(alloc-with allocator size alignment)

Allocates size bytes using the allocator.

+

arena-allocator

(arena-allocator arena)

Constructs a SegmentAllocator from the given Arena.

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.

-

as-segment

(as-segment address)(as-segment address size)(as-segment address size arena)(as-segment address size arena cleanup)

Dereferences an address into a memory segment associated with the arena (default global).

-

auto-arena

(auto-arena)

Constructs a new memory arena that is managed by the garbage collector.

+

as-segment

(as-segment address)(as-segment address size)(as-segment address size arena)(as-segment address size arena cleanup)

Dereferences an address into a memory segment associated with the arena (default global).

+

auto-arena

(auto-arena)

Constructs a new memory arena that is managed by the garbage collector.

The arena 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 arena cannot be closed, and therefore should not be created in a with-open clause.

-

big-endian

The big-endian ByteOrder.

+

big-endian

The big-endian ByteOrder.

See little-endian, native-endian.

-

byte-layout

The MemoryLayout for a byte in native-endian ByteOrder.

-

c-layout

multimethod

Gets the layout object for a given type.

+

byte-layout

The MemoryLayout for a byte in native-endian ByteOrder.

+

c-layout

multimethod

Gets the layout object for a given type.

If a type is primitive it will return the appropriate primitive layout (see c-prim-layout).

Otherwise, it should return a GroupLayout for the given type.

-

char-layout

The MemoryLayout for a c-sized char in native-endian ByteOrder.

-

clone-segment

(clone-segment segment)(clone-segment segment arena)

Clones the content of segment into a new segment of the same size.

-

confined-arena

(confined-arena)

Constructs a new arena for use only in this thread.

+

char-layout

The MemoryLayout for a c-sized char in native-endian ByteOrder.

+

clone-segment

(clone-segment segment)(clone-segment segment arena)

Clones the content of segment into a new segment of the same size.

+

confined-arena

(confined-arena)

Constructs a new arena for use only in this thread.

The memory allocated within this arena is cheap to allocate, like a native stack.

The memory allocated within this arena will be cleared once it is closed, so it is usually a good idea to create it in a with-open clause.

-

copy-segment

(copy-segment dest src)

Copies the content to dest from src.

+

copy-segment

(copy-segment dest src)

Copies the content to dest from src.

Returns dest.

-

defalias

macro

(defalias new-type aliased-type)

Defines a type alias from new-type to aliased-type.

+

defalias

macro

(defalias new-type aliased-type)

Defines a type alias from new-type to aliased-type.

This creates needed serialization and deserialization implementations for the aliased type.

-

deserialize

(deserialize obj type)

Deserializes an arbitrary type.

+

deserialize

(deserialize obj type)

Deserializes an arbitrary type.

For types which have a primitive representation, this deserializes the primitive representation. For types which do not, this deserializes out of a segment.

-

deserialize*

multimethod

Deserializes a primitive object into a Clojure data structure.

+

deserialize*

multimethod

Deserializes a primitive object into a Clojure data structure.

This is intended for use with types that are returned as a primitive but which need additional processing before they can be returned.

-

deserialize-from

multimethod

Deserializes the given segment into a Clojure data structure.

+

deserialize-from

multimethod

Deserializes the given segment into a Clojure data structure.

For types that serialize to primitives, a default implementation will deserialize the primitive before calling deserialize*.

-

double-alignment

The alignment in bytes of a c-sized double.

-

double-layout

The MemoryLayout for a c-sized double in native-endian ByteOrder.

-

double-size

The size in bytes of a c-sized double.

-

float-alignment

The alignment in bytes of a c-sized float.

-

float-layout

The MemoryLayout for a c-sized float in native-endian ByteOrder.

-

float-size

The size in bytes of a c-sized float.

-

global-arena

(global-arena)

Constructs the global arena, which will never reclaim its resources.

+

double-alignment

The alignment in bytes of a c-sized double.

+

double-layout

The MemoryLayout for a c-sized double in native-endian ByteOrder.

+

double-size

The size in bytes of a c-sized double.

+

float-alignment

The alignment in bytes of a c-sized float.

+

float-layout

The MemoryLayout for a c-sized float in native-endian ByteOrder.

+

float-size

The size in bytes of a c-sized float.

+

global-arena

(global-arena)

Constructs the global arena, which will never reclaim its resources.

This arena may be shared across threads, but is intended mainly in cases 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.

-

int-alignment

The alignment in bytes of a c-sized int.

-

int-layout

The MemoryLayout for a c-sized int in native-endian ByteOrder.

-

int-size

The size in bytes of a c-sized int.

-

java-layout

(java-layout type)

Gets the Java class to an argument of this type for a method handle.

+

int-alignment

The alignment in bytes of a c-sized int.

+

int-layout

The MemoryLayout for a c-sized int in native-endian ByteOrder.

+

int-size

The size in bytes of a c-sized int.

+

java-layout

(java-layout type)

Gets the Java class to an argument of this type for a method handle.

If a type serializes to a primitive it returns return a Java primitive type. Otherwise, it returns MemorySegment.

-

java-prim-layout

Map of primitive type names to the Java types for a method handle.

-

little-endian

The little-endian ByteOrder.

+

java-prim-layout

Map of primitive type names to the Java types for a method handle.

+

little-endian

The little-endian ByteOrder.

See big-endian, native-endian

-

long-alignment

The alignment in bytes of a c-sized long.

-

long-layout

The MemoryLayout for a c-sized long in native-endian ByteOrder.

-

long-size

The size in bytes of a c-sized long.

-

native-endian

The ByteOrder for the native endianness of the current hardware.

+

long-alignment

The alignment in bytes of a c-sized long.

+

long-layout

The MemoryLayout for a c-sized long in native-endian ByteOrder.

+

long-size

The size in bytes of a c-sized long.

+

native-endian

The ByteOrder for the native endianness of the current hardware.

See big-endian, little-endian.

-

null

The NULL pointer object.

+

null

The NULL pointer object.

While this object is safe to pass to functions which serialize to a pointer, it’s generally encouraged to simply pass nil. This value primarily exists to make it easier to write custom types with a primitive pointer representation.

-

null?

(null? addr)

Checks if a memory address is null.

-

pointer-alignment

The alignment in bytes of a c-sized pointer.

-

pointer-layout

The MemoryLayout for a native pointer in native-endian ByteOrder.

-

pointer-size

The size in bytes of a c-sized pointer.

-

primitive-type

multimethod

Gets the primitive type that is used to pass as an argument for the type.

+

null?

(null? addr)

Checks if a memory address is null.

+

pointer-alignment

The alignment in bytes of a c-sized pointer.

+

pointer-layout

The MemoryLayout for a native pointer in native-endian ByteOrder.

+

pointer-size

The size in bytes of a c-sized pointer.

+

primitive-type

multimethod

Gets the primitive type that is used to pass as an argument for the type.

This is for objects which are passed to native functions as primitive types, but which need additional logic to be performed during serialization and deserialization.

Implementations of this method should take into account that type arguments may not always be evaluated before passing to this function.

Returns nil for any type which does not have a primitive representation.

-

primitive-types

A set of all primitive types.

-

primitive?

(primitive? type)

A predicate to determine if a given type is primitive.

-

read-address

(read-address segment)(read-address segment offset)

Reads an address from the segment, at an optional offset, wrapped in a MemorySegment.

-

read-byte

(read-byte segment)(read-byte segment offset)

Reads a byte from the segment, at an optional offset.

-

read-char

(read-char segment)(read-char segment offset)

Reads a char from the segment, at an optional offset.

-

read-double

(read-double segment)(read-double segment offset)(read-double segment offset byte-order)

Reads a double from the segment, at an optional offset.

+

primitive-types

A set of all primitive types.

+

primitive?

(primitive? type)

A predicate to determine if a given type is primitive.

+

read-address

(read-address segment)(read-address segment offset)

Reads an address from the segment, at an optional offset, wrapped in a MemorySegment.

+

read-byte

(read-byte segment)(read-byte segment offset)

Reads a byte from the segment, at an optional offset.

+

read-char

(read-char segment)(read-char segment offset)

Reads a char from the segment, at an optional offset.

+

read-double

(read-double segment)(read-double segment offset)(read-double segment offset byte-order)

Reads a double from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-float

(read-float segment)(read-float segment offset)(read-float segment offset byte-order)

Reads a float from the segment, at an optional offset.

+

read-float

(read-float segment)(read-float segment offset)(read-float segment offset byte-order)

Reads a float from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-int

(read-int segment)(read-int segment offset)(read-int segment offset byte-order)

Reads a int from the segment, at an optional offset.

+

read-int

(read-int segment)(read-int segment offset)(read-int segment offset byte-order)

Reads a int from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-long

(read-long segment)(read-long segment offset)(read-long segment offset byte-order)

Reads a long from the segment, at an optional offset.

+

read-long

(read-long segment)(read-long segment offset)(read-long segment offset byte-order)

Reads a long from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-short

(read-short segment)(read-short segment offset)(read-short segment offset byte-order)

Reads a short from the segment, at an optional offset.

+

read-short

(read-short segment)(read-short segment offset)(read-short segment offset byte-order)

Reads a short from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

reinterpret

(reinterpret segment size)(reinterpret segment size arena)(reinterpret segment size arena cleanup)

Reinterprets the segment as having the passed size.

+

reinterpret

(reinterpret segment size)(reinterpret segment size arena)(reinterpret segment size arena cleanup)

Reinterprets the segment as having the passed size.

If arena is passed, the scope of the segment is associated with the arena, as well as its access constraints. If cleanup is passed, it will be a 1-argument function of a fresh memory segment backed by the same memory as the returned segment which should perform any required cleanup operations. It will be called when the arena is closed.

-

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

-

serialize

(serialize obj type)(serialize obj type arena)

Serializes an arbitrary type.

+

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

+

serialize

(serialize obj type)(serialize obj type arena)

Serializes an arbitrary type.

For types which have a primitive representation, this serializes into that representation. For types which do not, it allocates a new segment and serializes into that.

-

serialize*

multimethod

Constructs a serialized version of the obj and returns it.

+

serialize*

multimethod

Constructs a serialized version of the obj and returns it.

Any new allocations made during the serialization should be tied to the given arena, except in extenuating circumstances.

This method should only be implemented for types that serialize to primitives.

-

serialize-into

multimethod

Writes a serialized version of the obj to the given segment.

+

serialize-into

multimethod

Writes a serialized version of the obj to the given segment.

Any new allocations made during the serialization should be tied to the given arena, except in extenuating circumstances.

This method should be implemented for any type which does not override c-layout.

For any other type, this will serialize it as serialize* before writing the result value into the segment.

-

shared-arena

(shared-arena)

Constructs a new shared memory arena.

+

shared-arena

(shared-arena)

Constructs a new shared memory arena.

This arena can be shared across threads and memory allocated in it will only be cleaned up once any thread accessing the arena closes it.

-

short-alignment

The alignment in bytes of a c-sized short.

-

short-layout

The MemoryLayout for a c-sized short in native-endian ByteOrder.

-

short-size

The size in bytes of a c-sized short.

-

size-of

(size-of type)

The size in bytes of the given type.

-

slice

(slice segment offset)(slice segment offset size)

Get a slice over the segment with the given offset.

-

slice-segments

(slice-segments segment size)

Constructs a lazy seq of size-length memory segments, sliced from segment.

-

write-address

(write-address segment value)(write-address segment offset value)

Writes the address of the MemorySegment value to the segment, at an optional offset.

-

write-byte

(write-byte segment value)(write-byte segment offset value)

Writes a byte to the segment, at an optional offset.

-

write-char

(write-char segment value)(write-char segment offset value)

Writes a char to the segment, at an optional offset.

-

write-double

(write-double segment value)(write-double segment offset value)(write-double segment offset byte-order value)

Writes a double to the segment, at an optional offset.

+

short-alignment

The alignment in bytes of a c-sized short.

+

short-layout

The MemoryLayout for a c-sized short in native-endian ByteOrder.

+

short-size

The size in bytes of a c-sized short.

+

size-of

(size-of type)

The size in bytes of the given type.

+

slice

(slice segment offset)(slice segment offset size)

Get a slice over the segment with the given offset.

+

slice-segments

(slice-segments segment size)

Constructs a lazy seq of size-length memory segments, sliced from segment.

+

write-address

(write-address segment value)(write-address segment offset value)

Writes the address of the MemorySegment value to the segment, at an optional offset.

+

write-byte

(write-byte segment value)(write-byte segment offset value)

Writes a byte to the segment, at an optional offset.

+

write-char

(write-char segment value)(write-char segment offset value)

Writes a char to the segment, at an optional offset.

+

write-double

(write-double segment value)(write-double segment offset value)(write-double segment offset byte-order value)

Writes a double to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-float

(write-float segment value)(write-float segment offset value)(write-float segment offset byte-order value)

Writes a float to the segment, at an optional offset.

+

write-float

(write-float segment value)(write-float segment offset value)(write-float segment offset byte-order value)

Writes a float to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-int

(write-int segment value)(write-int segment offset value)(write-int segment offset byte-order value)

Writes a int to the segment, at an optional offset.

+

write-int

(write-int segment value)(write-int segment offset value)(write-int segment offset byte-order value)

Writes a int to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-long

(write-long segment value)(write-long segment offset value)(write-long segment offset byte-order value)

Writes a long to the segment, at an optional offset.

+

write-long

(write-long segment value)(write-long segment offset value)(write-long segment offset byte-order value)

Writes a long to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-short

(write-short segment value)(write-short segment offset value)(write-short segment offset byte-order value)

Writes a short to the segment, at an optional offset.

+

write-short

(write-short segment value)(write-short segment offset value)(write-short segment offset byte-order value)

Writes a short to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 0e9517c..0009880 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,6 +1,6 @@ -coffi v1.0.486

coffi v1.0.486

A Foreign Function Interface in Clojure for JDK 22+.

Topics

Namespaces

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

+coffi v1.0.486

coffi v1.0.486

A Foreign Function Interface in Clojure for JDK 22+.

Topics

Namespaces

coffi.layout

Functions for adjusting the layout of structs.

Public variables and functions:

\ No newline at end of file From ef9b205750600b471f2467dd72519b962329932a Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 4 Oct 2024 16:35:05 -0400 Subject: [PATCH 6/6] Fix a broken list and make wip stuff explicit --- docs/01-Getting-Started.html | 14 ++- docs/02-Memory-Management.html | 2 +- docs/03-Builtin-Types.html | 2 +- docs/04-Custom-Types.html | 2 +- docs/05-Low-Level-Wrappers.html | 2 +- docs/50-Data-Model.html | 2 +- docs/99-Benchmarks.html | 2 +- docs/articles/01-Getting-Started.md | 1 + docs/articles/03-Builtin-Types.md | 2 +- docs/articles/99-Benchmarks.md | 2 +- docs/coffi.ffi.html | 42 ++++---- docs/coffi.layout.html | 4 +- docs/coffi.mem.html | 148 ++++++++++++++-------------- docs/index.html | 2 +- 14 files changed, 119 insertions(+), 108 deletions(-) diff --git a/docs/01-Getting-Started.html b/docs/01-Getting-Started.html index 3d4bb2b..b3ecc4a 100644 --- a/docs/01-Getting-Started.html +++ b/docs/01-Getting-Started.html @@ -1,6 +1,6 @@ -Getting Started

Getting Started

+Getting Started

Getting Started

Installation

This library is available on Clojars. Add one of the following entries to the :deps key of your deps.edn:

org.suskalo/coffi {:mvn/version "x.y.z"}
@@ -49,7 +49,17 @@ io.github.IGJoshua/coffi {:git/tag "x.y.z" :git/sha "abcdef0"}
 

This will load libz from the lib subdirectory of the current working directory. As you can see this requires the entire filename, including platform-specific file extensions.

If a library is attempted to be loaded but doesn’t exist or otherwise can’t be loaded, an exception is thrown. This can be convenient as any namespace with a load-library call at the top level cannot be required without the library being able to be loaded.

Primitive Types

-

Coffi defines a basic set of primitive types: - byte - short - int - long - char - float - double - pointer

+

Coffi defines a basic set of primitive types:

+
    +
  • byte
  • +
  • short
  • +
  • int
  • +
  • long
  • +
  • char
  • +
  • float
  • +
  • double
  • +
  • pointer
  • +

Each of these types maps to their C counterpart. Values of any of these primitive types except for pointer will be cast with their corresponding Clojure function when they are passed as arguments to native functions. Additionally, the c-string type is defined, although it is not primitive.

Composite Types

In addition, some composite types are also defined in coffi, including struct and union types (unions will be discussed with serialization and deserialization). For an example C struct and function:

diff --git a/docs/02-Memory-Management.html b/docs/02-Memory-Management.html index a0489fa..1ed7324 100644 --- a/docs/02-Memory-Management.html +++ b/docs/02-Memory-Management.html @@ -1,6 +1,6 @@ -Memory Management

Memory Management

+Memory Management

Memory Management

In order to serialize any non-primitive type, off-heap memory needs to be allocated. When memory is allocated inside the JVM, the memory is associated with an arena. If none is provided, the arena is an implicit arena, and the memory will be freed when the serialized object is garbage collected.

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 well as changing allocation performance. Instead of an implicit arena, there are other kinds of arenas as well. A confined-arena is a thread-local arena. Confined arenas are Closeable, which means they should usually be used in a with-open form. When a confined-arena is closed, it immediately frees all the memory associated with it. The previous example, out-int, can be implemented with a confined arena.

(defcfn out-int
diff --git a/docs/03-Builtin-Types.html b/docs/03-Builtin-Types.html
index ca6ec7d..0222e5a 100644
--- a/docs/03-Builtin-Types.html
+++ b/docs/03-Builtin-Types.html
@@ -1,6 +1,6 @@
 
-Built-in Types

Built-in Types

+Built-in Types **WIP**

Built-in Types WIP

Primitives

Arrays

Pointers

diff --git a/docs/04-Custom-Types.html b/docs/04-Custom-Types.html index 50e6805..3194169 100644 --- a/docs/04-Custom-Types.html +++ b/docs/04-Custom-Types.html @@ -1,6 +1,6 @@ -Custom Types

Custom Types

+Custom Types

Custom Types

Custom types with serializers and deserializers may be created. This is done using two sets of three multimethods which can be extended by the user. For any given type, only one set need be implemented.

Two examples of custom types are given here, one is a 3d vector, and the other an example of a tagged union.

Vector3

diff --git a/docs/05-Low-Level-Wrappers.html b/docs/05-Low-Level-Wrappers.html index 0b9ab2c..6539b51 100644 --- a/docs/05-Low-Level-Wrappers.html +++ b/docs/05-Low-Level-Wrappers.html @@ -1,6 +1,6 @@ -Low-Level Wrappers

Low-Level Wrappers

+Low-Level Wrappers

Low-Level Wrappers

Unwrapped Native Handles

Some native libraries work with handles to large amounts of data at once, making it undesirable to marshal data back and forth from Clojure, both because it’s not necessary to work with the data in Clojure directly, or also because of the high (de)serialization costs associated with marshaling. In cases like these, unwrapped native handles are desirable.

The functions make-downcall and make-varargs-factory are also provided to create raw function handles.

diff --git a/docs/50-Data-Model.html b/docs/50-Data-Model.html index aba841f..79468a1 100644 --- a/docs/50-Data-Model.html +++ b/docs/50-Data-Model.html @@ -1,6 +1,6 @@ -Data Model

Data Model

+Data Model

Data Model

In addition to the macros and functions provided to build a Clojure API for native libraries, facilities are provided for taking data and loading all the symbols specified by it. This can be useful if a library provides (or an external provider maintains) a data representation of their API, as Clojure data to represent it may be programmatically generated from these sources.

The data to represent an API is a map with the following form:

(def strlen-libspec
diff --git a/docs/99-Benchmarks.html b/docs/99-Benchmarks.html
index 0009067..94fd620 100644
--- a/docs/99-Benchmarks.html
+++ b/docs/99-Benchmarks.html
@@ -1,6 +1,6 @@
 
-Benchmarks

Benchmarks

+Benchmarks **OUTDATED**

Benchmarks OUTDATED

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 of each available option. It’s an established fact that JNA (used by all three alternative libraries on JDK <16) introduces more overhead when calling native code than JNI does.

In order to provide a benchmark to see how much of a difference the different native interfaces make, we can use criterium to benchmark each. GLFW’s glfwGetTime function will be used for the test as it performs a simple operation, and is conveniently already wrapped in JNI by the excellent LWJGL library.

diff --git a/docs/articles/01-Getting-Started.md b/docs/articles/01-Getting-Started.md index 6fb3cc6..1374b52 100644 --- a/docs/articles/01-Getting-Started.md +++ b/docs/articles/01-Getting-Started.md @@ -109,6 +109,7 @@ being able to be loaded. ### Primitive Types Coffi defines a basic set of primitive types: + - byte - short - int diff --git a/docs/articles/03-Builtin-Types.md b/docs/articles/03-Builtin-Types.md index 81d8cfb..03bb8c8 100644 --- a/docs/articles/03-Builtin-Types.md +++ b/docs/articles/03-Builtin-Types.md @@ -1,4 +1,4 @@ -# Built-in Types +# Built-in Types **WIP** ### Primitives diff --git a/docs/articles/99-Benchmarks.md b/docs/articles/99-Benchmarks.md index 69ef11c..6e30eff 100644 --- a/docs/articles/99-Benchmarks.md +++ b/docs/articles/99-Benchmarks.md @@ -1,4 +1,4 @@ -# Benchmarks +# Benchmarks **OUTDATED** **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 diff --git a/docs/coffi.ffi.html b/docs/coffi.ffi.html index c911409..975a489 100644 --- a/docs/coffi.ffi.html +++ b/docs/coffi.ffi.html @@ -1,41 +1,41 @@ -coffi.ffi documentation

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

+coffi.ffi documentation

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

cfn

(cfn symbol args ret)

Constructs a Clojure function to call the native function referenced by symbol.

The function returned will serialize any passed arguments into the args types, and deserialize the return to the ret type.

If your args and ret are constants, then it is more efficient to call make-downcall followed by make-serde-wrapper because the latter has an inline definition which will result in less overhead from serdes.

-

const

(const symbol-or-addr type)

Gets the value of a constant stored in symbol-or-addr.

-

defcfn

macro

(defcfn name docstring? attr-map? symbol arg-types ret-type)(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)

Defines a Clojure function which maps to a native function.

+

const

(const symbol-or-addr type)

Gets the value of a constant stored in symbol-or-addr.

+

defcfn

macro

(defcfn name docstring? attr-map? symbol arg-types ret-type)(defcfn name docstring? attr-map? symbol arg-types ret-type native-fn & fn-tail)

Defines a Clojure function which maps to a native function.

name is the symbol naming the resulting var. symbol is a symbol or string naming the library symbol to link against. arg-types is a vector of qualified keywords representing the argument types. ret-type is a single qualified keyword representing the return type. fn-tail is the body of the function (potentially with multiple arities) which wraps the native one. Inside the function, native-fn 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.

If no fn-tail is provided, then the resulting function will simply serialize the arguments according to arg-types, call the native function, and deserialize the return value.

The number of args in the fn-tail need not match the number of arg-types for the native function. It need only call the native wrapper function with the correct arguments.

See serialize, deserialize, make-downcall.

-

defconst

macro

(defconst symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be the value of the given type from symbol-or-addr.

-

defvar

macro

(defvar symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be a reference to the native memory from symbol-or-addr.

-

ensure-symbol

(ensure-symbol symbol-or-addr)

Returns the argument if it is a MemorySegment, otherwise calls find-symbol on it.

-

find-symbol

(find-symbol sym)

Gets the MemorySegment of a symbol from the loaded libraries.

-

freset!

(freset! static-var newval)

Sets the value of static-var to newval, running it through serialize.

-

fswap!

(fswap! static-var f & args)

Non-atomically runs the function f over the value stored in static-var.

+

defconst

macro

(defconst symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be the value of the given type from symbol-or-addr.

+

defvar

macro

(defvar symbol docstring? symbol-or-addr type)

Defines a var named by symbol to be a reference to the native memory from symbol-or-addr.

+

ensure-symbol

(ensure-symbol symbol-or-addr)

Returns the argument if it is a MemorySegment, otherwise calls find-symbol on it.

+

find-symbol

(find-symbol sym)

Gets the MemorySegment of a symbol from the loaded libraries.

+

freset!

(freset! static-var newval)

Sets the value of static-var to newval, running it through serialize.

+

fswap!

(fswap! static-var f & args)

Non-atomically runs the function f over the value stored in static-var.

The value is deserialized before passing it to f, and serialized before putting the value into static-var.

-

load-library

(load-library path)

Loads the library at path.

-

load-system-library

(load-system-library libname)

Loads the library named libname from the system’s load path.

-

make-downcall

(make-downcall symbol-or-addr args ret)

Constructs a downcall function reference to symbol-or-addr with the given args and ret types.

+

load-library

(load-library path)

Loads the library at path.

+

load-system-library

(load-system-library libname)

Loads the library named libname from the system’s load path.

+

make-downcall

(make-downcall symbol-or-addr args ret)

Constructs a downcall function reference to symbol-or-addr with the given args and ret types.

The function returned takes only arguments whose types match exactly the java-layout for that type, and returns an argument with exactly the java-layout of the ret type. This function will perform no serialization or deserialization of arguments or the return type.

If the ret type is non-primitive, then the returned function will take a first argument of a SegmentAllocator.

-

make-serde-varargs-wrapper

(make-serde-varargs-wrapper varargs-factory required-args ret-type)

Constructs a wrapper function for the varargs-factory which produces functions that serialize the arguments and deserialize the return value.

-

make-serde-wrapper

(make-serde-wrapper downcall arg-types ret-type)

Constructs a wrapper function for the downcall which serializes the arguments and deserializes the return value.

-

make-varargs-factory

(make-varargs-factory symbol required-args ret)

Returns a function for constructing downcalls with additional types for arguments.

+

make-serde-varargs-wrapper

(make-serde-varargs-wrapper varargs-factory required-args ret-type)

Constructs a wrapper function for the varargs-factory which produces functions that serialize the arguments and deserialize the return value.

+

make-serde-wrapper

(make-serde-wrapper downcall arg-types ret-type)

Constructs a wrapper function for the downcall which serializes the arguments and deserializes the return value.

+

make-varargs-factory

(make-varargs-factory symbol required-args ret)

Returns a function for constructing downcalls with additional types for arguments.

The required-args 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.

The returned function is memoized, so that only one downcall function will be generated per combination of argument types.

See make-downcall.

-

reify-libspec

(reify-libspec libspec)

Loads all the symbols specified in the libspec.

+

reify-libspec

(reify-libspec libspec)

Loads all the symbols specified in the libspec.

The value of each key of the passed map is transformed as by reify-symbolspec.

-

reify-symbolspec

multimethod

Takes a spec for a symbol reference and returns a live value for that type.

-

static-variable

(static-variable symbol-or-addr type)

Constructs a reference to a mutable value stored in symbol-or-addr.

+

reify-symbolspec

multimethod

Takes a spec for a symbol reference and returns a live value for that type.

+

static-variable

(static-variable symbol-or-addr type)

Constructs a reference to a mutable value stored in symbol-or-addr.

The returned value can be dereferenced, and has metadata.

See freset!, fswap!.

-

static-variable-segment

(static-variable-segment static-var)

Gets the backing MemorySegment from static-var.

+

static-variable-segment

(static-variable-segment static-var)

Gets the backing MemorySegment from static-var.

This is primarily useful when you need to pass the static variable’s address to a native function which takes an Addressable.

-

vacfn-factory

(vacfn-factory symbol required-args ret)

Constructs a varargs factory to call the native function referenced by symbol.

+

vacfn-factory

(vacfn-factory symbol required-args ret)

Constructs a varargs factory to call the native function referenced by symbol.

The function returned takes any number of type arguments and returns a specialized Clojure function for calling the native function with those arguments.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/coffi.layout.html b/docs/coffi.layout.html index 0783ab7..aaca13e 100644 --- a/docs/coffi.layout.html +++ b/docs/coffi.layout.html @@ -1,6 +1,6 @@ -coffi.layout documentation

coffi.layout

Functions for adjusting the layout of structs.

+coffi.layout documentation

coffi.layout

Functions for adjusting the layout of structs.

with-c-layout

(with-c-layout struct-spec)

Forces a struct specification to C layout rules.

This will add padding fields between fields to match C alignment requirements.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/coffi.mem.html b/docs/coffi.mem.html index 0afc416..b8f16b4 100644 --- a/docs/coffi.mem.html +++ b/docs/coffi.mem.html @@ -1,121 +1,121 @@ -coffi.mem documentation

coffi.mem

Functions for managing native allocations, memory arenas, and (de)serialization.

+coffi.mem documentation

coffi.mem

Functions for managing native allocations, memory arenas, and (de)serialization.

For any new type to be implemented, three multimethods must be overriden, but which three depends on the native representation of the type.

If the native representation of the type is a primitive (whether or not other data beyond the primitive is associated with it, as e.g. a pointer), then primitive-type must be overriden to return which primitive type it is serialized as, then serialize* and deserialize* should be overriden.

If the native representation of the type is a composite type, like a union, 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 overriden to allow marshaling values of the type into and out of memory segments.

address-of

(address-of addressable)

Gets the address of a given segment as a number.

-

address?

(address? addr)

Checks if an object is a memory address.

+

address?

(address? addr)

Checks if an object is a memory address.

nil is considered an address.

-

align-of

(align-of type)

The alignment in bytes of the given type.

-

alloc

(alloc size)(alloc size arena)(alloc size alignment arena)

Allocates size bytes.

+

align-of

(align-of type)

The alignment in bytes of the given type.

+

alloc

(alloc size)(alloc size arena)(alloc size alignment arena)

Allocates size bytes.

If an arena is provided, the allocation will be reclaimed when it is closed.

-

alloc-instance

(alloc-instance type)(alloc-instance type arena)

Allocates a memory segment for the given type.

-

alloc-with

(alloc-with allocator size)(alloc-with allocator size alignment)

Allocates size bytes using the allocator.

-

arena-allocator

(arena-allocator arena)

Constructs a SegmentAllocator from the given Arena.

+

alloc-instance

(alloc-instance type)(alloc-instance type arena)

Allocates a memory segment for the given type.

+

alloc-with

(alloc-with allocator size)(alloc-with allocator size alignment)

Allocates size bytes using the allocator.

+

arena-allocator

(arena-allocator arena)

Constructs a SegmentAllocator from the given Arena.

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.

-

as-segment

(as-segment address)(as-segment address size)(as-segment address size arena)(as-segment address size arena cleanup)

Dereferences an address into a memory segment associated with the arena (default global).

-

auto-arena

(auto-arena)

Constructs a new memory arena that is managed by the garbage collector.

+

as-segment

(as-segment address)(as-segment address size)(as-segment address size arena)(as-segment address size arena cleanup)

Dereferences an address into a memory segment associated with the arena (default global).

+

auto-arena

(auto-arena)

Constructs a new memory arena that is managed by the garbage collector.

The arena 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 arena cannot be closed, and therefore should not be created in a with-open clause.

-

big-endian

The big-endian ByteOrder.

+

big-endian

The big-endian ByteOrder.

See little-endian, native-endian.

-

byte-layout

The MemoryLayout for a byte in native-endian ByteOrder.

-

c-layout

multimethod

Gets the layout object for a given type.

+

byte-layout

The MemoryLayout for a byte in native-endian ByteOrder.

+

c-layout

multimethod

Gets the layout object for a given type.

If a type is primitive it will return the appropriate primitive layout (see c-prim-layout).

Otherwise, it should return a GroupLayout for the given type.

-

char-layout

The MemoryLayout for a c-sized char in native-endian ByteOrder.

-

clone-segment

(clone-segment segment)(clone-segment segment arena)

Clones the content of segment into a new segment of the same size.

-

confined-arena

(confined-arena)

Constructs a new arena for use only in this thread.

+

char-layout

The MemoryLayout for a c-sized char in native-endian ByteOrder.

+

clone-segment

(clone-segment segment)(clone-segment segment arena)

Clones the content of segment into a new segment of the same size.

+

confined-arena

(confined-arena)

Constructs a new arena for use only in this thread.

The memory allocated within this arena is cheap to allocate, like a native stack.

The memory allocated within this arena will be cleared once it is closed, so it is usually a good idea to create it in a with-open clause.

-

copy-segment

(copy-segment dest src)

Copies the content to dest from src.

+

copy-segment

(copy-segment dest src)

Copies the content to dest from src.

Returns dest.

-

defalias

macro

(defalias new-type aliased-type)

Defines a type alias from new-type to aliased-type.

+

defalias

macro

(defalias new-type aliased-type)

Defines a type alias from new-type to aliased-type.

This creates needed serialization and deserialization implementations for the aliased type.

-

deserialize

(deserialize obj type)

Deserializes an arbitrary type.

+

deserialize

(deserialize obj type)

Deserializes an arbitrary type.

For types which have a primitive representation, this deserializes the primitive representation. For types which do not, this deserializes out of a segment.

-

deserialize*

multimethod

Deserializes a primitive object into a Clojure data structure.

+

deserialize*

multimethod

Deserializes a primitive object into a Clojure data structure.

This is intended for use with types that are returned as a primitive but which need additional processing before they can be returned.

-

deserialize-from

multimethod

Deserializes the given segment into a Clojure data structure.

+

deserialize-from

multimethod

Deserializes the given segment into a Clojure data structure.

For types that serialize to primitives, a default implementation will deserialize the primitive before calling deserialize*.

-

double-alignment

The alignment in bytes of a c-sized double.

-

double-layout

The MemoryLayout for a c-sized double in native-endian ByteOrder.

-

double-size

The size in bytes of a c-sized double.

-

float-alignment

The alignment in bytes of a c-sized float.

-

float-layout

The MemoryLayout for a c-sized float in native-endian ByteOrder.

-

float-size

The size in bytes of a c-sized float.

-

global-arena

(global-arena)

Constructs the global arena, which will never reclaim its resources.

+

double-alignment

The alignment in bytes of a c-sized double.

+

double-layout

The MemoryLayout for a c-sized double in native-endian ByteOrder.

+

double-size

The size in bytes of a c-sized double.

+

float-alignment

The alignment in bytes of a c-sized float.

+

float-layout

The MemoryLayout for a c-sized float in native-endian ByteOrder.

+

float-size

The size in bytes of a c-sized float.

+

global-arena

(global-arena)

Constructs the global arena, which will never reclaim its resources.

This arena may be shared across threads, but is intended mainly in cases 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.

-

int-alignment

The alignment in bytes of a c-sized int.

-

int-layout

The MemoryLayout for a c-sized int in native-endian ByteOrder.

-

int-size

The size in bytes of a c-sized int.

-

java-layout

(java-layout type)

Gets the Java class to an argument of this type for a method handle.

+

int-alignment

The alignment in bytes of a c-sized int.

+

int-layout

The MemoryLayout for a c-sized int in native-endian ByteOrder.

+

int-size

The size in bytes of a c-sized int.

+

java-layout

(java-layout type)

Gets the Java class to an argument of this type for a method handle.

If a type serializes to a primitive it returns return a Java primitive type. Otherwise, it returns MemorySegment.

-

java-prim-layout

Map of primitive type names to the Java types for a method handle.

-

little-endian

The little-endian ByteOrder.

+

java-prim-layout

Map of primitive type names to the Java types for a method handle.

+

little-endian

The little-endian ByteOrder.

See big-endian, native-endian

-

long-alignment

The alignment in bytes of a c-sized long.

-

long-layout

The MemoryLayout for a c-sized long in native-endian ByteOrder.

-

long-size

The size in bytes of a c-sized long.

-

native-endian

The ByteOrder for the native endianness of the current hardware.

+

long-alignment

The alignment in bytes of a c-sized long.

+

long-layout

The MemoryLayout for a c-sized long in native-endian ByteOrder.

+

long-size

The size in bytes of a c-sized long.

+

native-endian

The ByteOrder for the native endianness of the current hardware.

See big-endian, little-endian.

-

null

The NULL pointer object.

+

null

The NULL pointer object.

While this object is safe to pass to functions which serialize to a pointer, it’s generally encouraged to simply pass nil. This value primarily exists to make it easier to write custom types with a primitive pointer representation.

-

null?

(null? addr)

Checks if a memory address is null.

-

pointer-alignment

The alignment in bytes of a c-sized pointer.

-

pointer-layout

The MemoryLayout for a native pointer in native-endian ByteOrder.

-

pointer-size

The size in bytes of a c-sized pointer.

-

primitive-type

multimethod

Gets the primitive type that is used to pass as an argument for the type.

+

null?

(null? addr)

Checks if a memory address is null.

+

pointer-alignment

The alignment in bytes of a c-sized pointer.

+

pointer-layout

The MemoryLayout for a native pointer in native-endian ByteOrder.

+

pointer-size

The size in bytes of a c-sized pointer.

+

primitive-type

multimethod

Gets the primitive type that is used to pass as an argument for the type.

This is for objects which are passed to native functions as primitive types, but which need additional logic to be performed during serialization and deserialization.

Implementations of this method should take into account that type arguments may not always be evaluated before passing to this function.

Returns nil for any type which does not have a primitive representation.

-

primitive-types

A set of all primitive types.

-

primitive?

(primitive? type)

A predicate to determine if a given type is primitive.

-

read-address

(read-address segment)(read-address segment offset)

Reads an address from the segment, at an optional offset, wrapped in a MemorySegment.

-

read-byte

(read-byte segment)(read-byte segment offset)

Reads a byte from the segment, at an optional offset.

-

read-char

(read-char segment)(read-char segment offset)

Reads a char from the segment, at an optional offset.

-

read-double

(read-double segment)(read-double segment offset)(read-double segment offset byte-order)

Reads a double from the segment, at an optional offset.

+

primitive-types

A set of all primitive types.

+

primitive?

(primitive? type)

A predicate to determine if a given type is primitive.

+

read-address

(read-address segment)(read-address segment offset)

Reads an address from the segment, at an optional offset, wrapped in a MemorySegment.

+

read-byte

(read-byte segment)(read-byte segment offset)

Reads a byte from the segment, at an optional offset.

+

read-char

(read-char segment)(read-char segment offset)

Reads a char from the segment, at an optional offset.

+

read-double

(read-double segment)(read-double segment offset)(read-double segment offset byte-order)

Reads a double from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-float

(read-float segment)(read-float segment offset)(read-float segment offset byte-order)

Reads a float from the segment, at an optional offset.

+

read-float

(read-float segment)(read-float segment offset)(read-float segment offset byte-order)

Reads a float from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-int

(read-int segment)(read-int segment offset)(read-int segment offset byte-order)

Reads a int from the segment, at an optional offset.

+

read-int

(read-int segment)(read-int segment offset)(read-int segment offset byte-order)

Reads a int from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-long

(read-long segment)(read-long segment offset)(read-long segment offset byte-order)

Reads a long from the segment, at an optional offset.

+

read-long

(read-long segment)(read-long segment offset)(read-long segment offset byte-order)

Reads a long from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

read-short

(read-short segment)(read-short segment offset)(read-short segment offset byte-order)

Reads a short from the segment, at an optional offset.

+

read-short

(read-short segment)(read-short segment offset)(read-short segment offset byte-order)

Reads a short from the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

reinterpret

(reinterpret segment size)(reinterpret segment size arena)(reinterpret segment size arena cleanup)

Reinterprets the segment as having the passed size.

+

reinterpret

(reinterpret segment size)(reinterpret segment size arena)(reinterpret segment size arena cleanup)

Reinterprets the segment as having the passed size.

If arena is passed, the scope of the segment is associated with the arena, as well as its access constraints. If cleanup is passed, it will be a 1-argument function of a fresh memory segment backed by the same memory as the returned segment which should perform any required cleanup operations. It will be called when the arena is closed.

-

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

-

serialize

(serialize obj type)(serialize obj type arena)

Serializes an arbitrary type.

+

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

+

serialize

(serialize obj type)(serialize obj type arena)

Serializes an arbitrary type.

For types which have a primitive representation, this serializes into that representation. For types which do not, it allocates a new segment and serializes into that.

-

serialize*

multimethod

Constructs a serialized version of the obj and returns it.

+

serialize*

multimethod

Constructs a serialized version of the obj and returns it.

Any new allocations made during the serialization should be tied to the given arena, except in extenuating circumstances.

This method should only be implemented for types that serialize to primitives.

-

serialize-into

multimethod

Writes a serialized version of the obj to the given segment.

+

serialize-into

multimethod

Writes a serialized version of the obj to the given segment.

Any new allocations made during the serialization should be tied to the given arena, except in extenuating circumstances.

This method should be implemented for any type which does not override c-layout.

For any other type, this will serialize it as serialize* before writing the result value into the segment.

-

shared-arena

(shared-arena)

Constructs a new shared memory arena.

+

shared-arena

(shared-arena)

Constructs a new shared memory arena.

This arena can be shared across threads and memory allocated in it will only be cleaned up once any thread accessing the arena closes it.

-

short-alignment

The alignment in bytes of a c-sized short.

-

short-layout

The MemoryLayout for a c-sized short in native-endian ByteOrder.

-

short-size

The size in bytes of a c-sized short.

-

size-of

(size-of type)

The size in bytes of the given type.

-

slice

(slice segment offset)(slice segment offset size)

Get a slice over the segment with the given offset.

-

slice-segments

(slice-segments segment size)

Constructs a lazy seq of size-length memory segments, sliced from segment.

-

write-address

(write-address segment value)(write-address segment offset value)

Writes the address of the MemorySegment value to the segment, at an optional offset.

-

write-byte

(write-byte segment value)(write-byte segment offset value)

Writes a byte to the segment, at an optional offset.

-

write-char

(write-char segment value)(write-char segment offset value)

Writes a char to the segment, at an optional offset.

-

write-double

(write-double segment value)(write-double segment offset value)(write-double segment offset byte-order value)

Writes a double to the segment, at an optional offset.

+

short-alignment

The alignment in bytes of a c-sized short.

+

short-layout

The MemoryLayout for a c-sized short in native-endian ByteOrder.

+

short-size

The size in bytes of a c-sized short.

+

size-of

(size-of type)

The size in bytes of the given type.

+

slice

(slice segment offset)(slice segment offset size)

Get a slice over the segment with the given offset.

+

slice-segments

(slice-segments segment size)

Constructs a lazy seq of size-length memory segments, sliced from segment.

+

write-address

(write-address segment value)(write-address segment offset value)

Writes the address of the MemorySegment value to the segment, at an optional offset.

+

write-byte

(write-byte segment value)(write-byte segment offset value)

Writes a byte to the segment, at an optional offset.

+

write-char

(write-char segment value)(write-char segment offset value)

Writes a char to the segment, at an optional offset.

+

write-double

(write-double segment value)(write-double segment offset value)(write-double segment offset byte-order value)

Writes a double to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-float

(write-float segment value)(write-float segment offset value)(write-float segment offset byte-order value)

Writes a float to the segment, at an optional offset.

+

write-float

(write-float segment value)(write-float segment offset value)(write-float segment offset byte-order value)

Writes a float to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-int

(write-int segment value)(write-int segment offset value)(write-int segment offset byte-order value)

Writes a int to the segment, at an optional offset.

+

write-int

(write-int segment value)(write-int segment offset value)(write-int segment offset byte-order value)

Writes a int to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-long

(write-long segment value)(write-long segment offset value)(write-long segment offset byte-order value)

Writes a long to the segment, at an optional offset.

+

write-long

(write-long segment value)(write-long segment offset value)(write-long segment offset byte-order value)

Writes a long to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-

write-short

(write-short segment value)(write-short segment offset value)(write-short segment offset byte-order value)

Writes a short to the segment, at an optional offset.

+

write-short

(write-short segment value)(write-short segment offset value)(write-short segment offset byte-order value)

Writes a short to the segment, at an optional offset.

If byte-order is not provided, it defaults to native-endian.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 0009880..a753207 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,6 +1,6 @@ -coffi v1.0.486

coffi v1.0.486

A Foreign Function Interface in Clojure for JDK 22+.

Topics

Namespaces

coffi.ffi

Functions for creating handles to native functions and loading native libraries.

+coffi v1.0.486

coffi v1.0.486

A Foreign Function Interface in Clojure for JDK 22+.

Topics

Namespaces

coffi.layout

Functions for adjusting the layout of structs.

Public variables and functions:

\ No newline at end of file