diff --git a/CHANGELOG.md b/CHANGELOG.md index db60e8b..4a46187 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,18 @@ # Change Log All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). +## [0.4.341] - 2022-01-23 +### Added +- Constants for size and alignment of primitive types +- Support for non-native byte orders of primitive types +- Functions for reading and writing primitive types (e.g. `coffi.mem/read-float`, `coffi.mem/write-long`, etc.) +- Layout objects may now be passed to `coffi.mem/size-of` and `coffi.mem/align-of` +- Constants for native-order primitive layouts +- Constants for byte orders + +### Changed +- The `coffi.mem/primitive?` predicate is now actually a function instead of a set + ## [0.3.298] - 2022-01-10 ### Added - New `coffi.layout` namespace with support for forcing C layout rules on structs @@ -12,7 +24,6 @@ All notable changes to this project will be documented in this file. This change ### Fixed - Non-primitive arguments on upcalls would generate invalid bytecode with `nil` instructions - ## [0.2.259] - 2021-10-16 ### Fixed - Long and double arguments to upcalls failed to compile in some cases @@ -80,6 +91,7 @@ All notable changes to this project will be documented in this file. This change - Support for serializing and deserializing arbitrary Clojure functions - Support for serializing and deserializing arbitrary Clojure data structures +[0.4.341]: https://github.com/IGJoshua/coffi/compare/v0.3.298...v0.4.341 [0.3.298]: https://github.com/IGJoshua/coffi/compare/v0.2.277...v0.3.298 [0.2.277]: https://github.com/IGJoshua/coffi/compare/v0.2.259...v0.2.277 [0.2.259]: https://github.com/IGJoshua/coffi/compare/v0.1.251...v0.2.259 diff --git a/README.md b/README.md index fa1a6ee..9a94b2d 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ This library is available on Clojars. Add one of the following entries to the `:deps` key of your `deps.edn`: ```clojure -org.suskalo/coffi {:mvn/version "0.3.298"} -io.github.IGJoshua/coffi {:git/tag "v0.3.298" :git/sha "1bbb8a7"} +org.suskalo/coffi {:mvn/version "0.4.341"} +io.github.IGJoshua/coffi {:git/tag "v0.4.341" :git/sha "09b8195"} ``` If you use this library as a git dependency, you will need to prepare the @@ -534,12 +534,14 @@ to the user to call `deserialize-from` on that segment with the appropriate type. ### Unwrapped Native Handles -Sometimes the overhead brought by the automatic serialization and -deserialization from the methods explained so far is too much. In cases like -these, unwrapped native handles are desirable. +Some native libraries work with handles to large amounts of data at once, making +it undesirable to marshal data back and forth from Clojure, both because it's +not necessary to work with the data in Clojure directly, or also because of the +high (de)serialization costs associated with marshaling. In cases like these, +unwrapped native handles are desirable. -The functions `make-downcall` and `make-varargs-factory` are provided to create -these raw handles. +The functions `make-downcall` and `make-varargs-factory` are also provided to +create raw function handles. ```clojure (def raw-strlen (ffi/make-downcall "strlen" [::mem/c-string] ::mem/long)) @@ -547,7 +549,7 @@ these raw handles. ;; => 5 ``` -In these cases, the argument types are expected to exactly match the types +With raw handles, the argument types are expected to exactly match the types expected by the native function. For primitive types, those are primitives. For addresses, that is `MemoryAddress`, and for composite types like structs and unions, that is `MemorySegment`. Both `MemoryAddress` and `MemorySegment` come @@ -569,6 +571,58 @@ Clojure functions serialized to this type will have their arguments and return value exactly match the types specified and will not perform any serialization or deserialization at their boundaries. +One important caveat to consider when writing wrappers for performance-sensitive +functions is that the convenience macro `defcfn` that coffi provides will +already perform no serialization or deserialization on primitive arguments and +return types, so for functions with only primitive argument and return types +there is no performance reason to choose unwrapped native handles over the +convenience macro. + +### Manual (De)Serialization +Coffi uses multimethods to dispatch to (de)serialization functions to enable +code that's generic over the types it operates on. However, in cases where you +know the exact types that you will be (de)serializing and the multimethod +dispatch overhead is too high a cost, it may be appropriate to manually handle +(de)serializing data. This will often be done paired with [Unwrapped Native +Handles](#unwrapped-native-handles). + +Convenience functions are provided to both read and write all primitive types +and addresses, including byte order. + +As an example, when wrapping a function that returns an array of big-endian +floats, the following code might be used. + +``` clojure +(def ^:private returns-float-array* (ffi/make-downcall "returns_float_array" [::mem/pointer] ::mem/int)) +(def ^:private release-floats* (ffi/make-downcall "releases_float_array" [::mem/pointer] ::mem/void)) + +(defn returns-float-array + [] + (with-open [scope (mem/stack-scope)] + (let [out-floats (mem/alloc mem/pointer-size scope) + num-floats (function-handle (mem/address-of out-floats)) + floats-addr (mem/read-address out-floats) + floats-slice (mem/slice-global floats-addr (unchecked-multiply-int mem/float-size num-floats))] + ;; Using a try/finally to perform an operation when the stack frame exits, + ;; but not to try to catch anything. + (try + (loop [floats (transient []) + index 0] + (if (>= index num-floats) + (persistent! floats) + (recur (conj! floats (mem/read-float floats-slice + (unchecked-multiply-int index mem/float-size) + mem/big-endian)) + (unchecked-inc-int index)))) + (finally + (release-floats floats-addr)))))) +``` + +The above code manually performs all memory operations rather than relying on +coffi's dispatch. This will be more performant, but because multimethod overhead +is usually relatively low, it's recommended to use the multimethod variants for +convenience in colder functions. + ### Data Model In addition to the macros and functions provided to build a Clojure API for native libraries, facilities are provided for taking data and loading all the @@ -1054,6 +1108,6 @@ September 2023. ## License -Copyright © 2021 Joshua Suskalo +Copyright © 2022 Joshua Suskalo Distributed under the Eclipse Public License version 1.0. diff --git a/build.clj b/build.clj index d4f2ea4..c0df6d7 100644 --- a/build.clj +++ b/build.clj @@ -17,7 +17,7 @@ [clojure.tools.build.api :as b])) (def lib-coord 'org.suskalo/coffi) -(def version (format "0.3.%s" (b/git-count-revs nil))) +(def version (format "0.4.%s" (b/git-count-revs nil))) (def resource-dirs ["resources/"]) diff --git a/deps.edn b/deps.edn index c3c604b..be56f5b 100644 --- a/deps.edn +++ b/deps.edn @@ -24,7 +24,7 @@ :codox {:extra-deps {codox/codox {:mvn/version "0.10.7"}} :exec-fn codox.main/generate-docs :exec-args {:name "coffi" - :version "v0.3.298" + :version "v0.4.341" :description "A Foreign Function Interface in Clojure for JDK 17." :source-paths ["src/clj"] :output-path "docs" diff --git a/docs/coffi.ffi.html b/docs/coffi.ffi.html index 4301514..2d0f7c3 100644 --- a/docs/coffi.ffi.html +++ b/docs/coffi.ffi.html @@ -1,19 +1,19 @@ -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.

+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.

+

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.

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.

find-symbol

(find-symbol sym)

Gets the MemoryAddress 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.

+

See serialize, deserialize, make-downcall.

find-symbol

(find-symbol sym)

Gets the MemoryAddress 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.

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.

+

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.

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.

-

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.

+

See make-downcall.

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.

The returned value can be dereferenced, and has metadata, and the address of the value can be queried with address-of.

-

See freset!, fswap!.

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 +

See freset!, fswap!.

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 diff --git a/docs/coffi.layout.html b/docs/coffi.layout.html index 5daf5ef..302f3c0 100644 --- a/docs/coffi.layout.html +++ b/docs/coffi.layout.html @@ -1,4 +1,4 @@ -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 +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 diff --git a/docs/coffi.mem.html b/docs/coffi.mem.html index c34767b..08465a6 100644 --- a/docs/coffi.mem.html +++ b/docs/coffi.mem.html @@ -1,37 +1,51 @@ -coffi.mem documentation

coffi.mem

Functions for managing native allocations, resource scopes, and (de)serialization.

+coffi.mem documentation

coffi.mem

Functions for managing native allocations, resource scopes, 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.

-

When writing code that manipulates a segment, it’s best practice to use with-acquired on the segment-scope in order to ensure it won’t be released during its manipulation.

add-close-action!

(add-close-action! scope action)

Adds a 0-arity function to be run when the scope closes.

address-of

(address-of addressable)

Gets the address of a given segment.

-

This value can be used as an argument to functions which take a pointer.

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 scope)

Allocates size bytes.

-

If a scope is provided, the allocation will be reclaimed when it is closed.

alloc-instance

(alloc-instance type)(alloc-instance type scope)

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.

as-segment

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

Dereferences an address into a memory segment associated with the scope.

-

If cleanup is provided, it is a 0-arity function run when the scope is closed. This can be used to register a free method for the memory, or do other cleanup in a way that doesn’t require modifying the code at the point of freeing, and allows shared or garbage collected resources to be freed correctly.

c-layout

multimethod

Gets the layout object for a given type.

+

When writing code that manipulates a segment, it’s best practice to use with-acquired on the segment-scope in order to ensure it won’t be released during its manipulation.

add-close-action!

(add-close-action! scope action)

Adds a 0-arity function to be run when the scope closes.

address-of

(address-of addressable)

Gets the address of a given segment.

+

This value can be used as an argument to functions which take a pointer.

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 scope)

Allocates size bytes.

+

If a scope is provided, the allocation will be reclaimed when it is closed.

alloc-instance

(alloc-instance type)(alloc-instance type scope)

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.

as-segment

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

Dereferences an address into a memory segment associated with the scope.

+

If cleanup is provided, it is a 0-arity function run when the scope is closed. This can be used to register a free method for the memory, or do other cleanup in a way that doesn’t require modifying the code at the point of freeing, and allows shared or garbage collected resources to be freed correctly.

big-endian

The big-endian ByteOrder.

+

See little-endian, native-endian.

byte-layout

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.

clone-segment

(clone-segment segment)(clone-segment segment scope)

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

connected-scope

(connected-scope)

Constructs a new scope to reclaim all connected resources at once.

+

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 scope)

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

connected-scope

(connected-scope)

Constructs a new scope to reclaim all connected resources at once.

The scope may be shared across threads, and all resources created with it will be cleaned up at the same time, when all references have been collected.

-

This type of scope cannot be closed, and therefore should not be created in a with-open clause.

copy-segment

(copy-segment dest src)

Copies the content to dest from src

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.

-

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.

-

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.

+

This type of scope cannot be closed, and therefore should not be created in a with-open clause.

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.

+

This creates needed serialization and deserialization implementations for the aliased 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.

+

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.

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

-

Implementations of this should be inside a with-acquired block for the the segment’s scope if they perform multiple memory operations.

global-scope

(global-scope)

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

-

This scope may be shared across threads, but is intended mainly in cases where memory is allocated with alloc but is either never freed or whose management is relinquished to a native library, such as when returned from a callback.

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.

null?

(null? addr)

Checks if a memory address is null.

primitive-type

multimethod

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

+

Implementations of this should be inside a with-acquired block for the the segment’s scope if they perform multiple memory operations.

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-scope

(global-scope)

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

+

This scope may be shared across threads, but is intended mainly in cases where memory is allocated with alloc but is either never freed or whose 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.

+

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.

+

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-long-alignment

The alignment in bytes of a c-sized long long.

long-long-layout

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

long-long-size

The size in bytes of a c-sized long long.

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?

(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?

A set of all primitive types.

scope-allocator

(scope-allocator scope)

Constructs a segment allocator from the given scope.

-

This is primarily used when working with unwrapped downcall functions. When a downcall function returns a non-primitive type, it must be provided with an allocator.

segment-scope

(segment-scope segment)

Gets the scope used to construct the segment.

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

serialize

(serialize obj type)(serialize obj type scope)

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.

+

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 a MemoryAddress from the segment, at an optional offset.

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.

+

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.

+

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.

+

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.

+

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

scope-allocator

(scope-allocator scope)

Constructs a segment allocator from the given scope.

+

This is primarily used when working with unwrapped downcall functions. When a downcall function returns a non-primitive type, it must be provided with an allocator.

segment-scope

(segment-scope segment)

Gets the scope used to construct the segment.

seq-of

(seq-of type segment)

Constructs a lazy sequence of type elements deserialized from segment.

serialize

(serialize obj type)(serialize obj type scope)

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.

Any new allocations made during the serialization should be tied to the given scope, 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.

+

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.

Any new allocations made during the serialization should be tied to the given scope, 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.

-

Implementations of this should be inside a with-acquired block for the scope if they perform multiple memory operations.

shared-scope

(shared-scope)

Constructs a new shared scope.

-

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

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-global

(slice-global address size)

Gets a slice of the global address space.

-

Because this fetches from the global segment, it has no associated scope, and therefore the reference created here cannot prevent the value from being freed. Be careful to ensure that you are not retaining an object incorrectly.

slice-into

(slice-into address segment)(slice-into address segment size)

Get a slice into the segment starting at the address.

slice-segments

(slice-segments segment size)

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

stack-scope

(stack-scope)

Constructs a new scope for use only in this thread.

-

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

with-acquired

macro

(with-acquired scopes & body)

Acquires one or more scopes until the body completes.

-

This is only necessary to do on shared scopes, however if you are operating on an arbitrary passed scope, it is best practice to wrap code that interacts with it wrapped in this.

with-offset

(with-offset address offset)

Get a new address offset from the old address.

\ No newline at end of file +

Implementations of this should be inside a with-acquired block for the scope if they perform multiple memory operations.

shared-scope

(shared-scope)

Constructs a new shared scope.

+

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

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-global

(slice-global address size)

Gets a slice of the global address space.

+

Because this fetches from the global segment, it has no associated scope, and therefore the reference created here cannot prevent the value from being freed. Be careful to ensure that you are not retaining an object incorrectly.

slice-into

(slice-into address segment)(slice-into address segment size)

Get a slice into the segment starting at the address.

slice-segments

(slice-segments segment size)

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

stack-scope

(stack-scope)

Constructs a new scope for use only in this thread.

+

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

with-acquired

macro

(with-acquired scopes & body)

Acquires one or more scopes until the body completes.

+

This is only necessary to do on shared scopes, however if you are operating on an arbitrary passed scope, it is best practice to wrap code that interacts with it wrapped in this.

with-offset

(with-offset address offset)

Get a new address offset from the old address.

write-address

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

Writes a MemoryAddress 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.

+

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.

+

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.

+

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.

+

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

\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 7381afb..dcd979b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,3 +1,3 @@ -coffi v0.3.298

coffi v0.3.298

A Foreign Function Interface in Clojure for JDK 17.

Namespaces

coffi.ffi

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

coffi.layout

Functions for adjusting the layout of structs.

Public variables and functions:

coffi.mem

Functions for managing native allocations, resource scopes, and (de)serialization.

\ No newline at end of file +coffi v0.4.341

coffi v0.4.341

A Foreign Function Interface in Clojure for JDK 17.

Namespaces

coffi.ffi

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

coffi.layout

Functions for adjusting the layout of structs.

Public variables and functions:

coffi.mem

Functions for managing native allocations, resource scopes, and (de)serialization.

\ No newline at end of file diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 3862ce4..061ff3b 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -288,8 +288,8 @@ `(mem/serialize ~sym ~type-sym ~scope) (and (mem/primitive? type) - (not (#{::mem/pointer} type))) - (list (primitive-cast-sym type) sym) + (not (#{::mem/pointer} (mem/primitive-type type)))) + (list (primitive-cast-sym (mem/primitive-type type)) sym) (#{::mem/pointer} type) nil @@ -347,8 +347,9 @@ `(mem/deserialize-from ~expr ~ret-type-sym)) deserialize-ret (fn [expr] (cond - (or (mem/primitive? ret-type) - (#{::mem/void} ret-type)) + (and (or (mem/primitive? ret-type) + (#{::mem/void} ret-type)) + (not (#{::mem/pointer} (mem/primitive-type ret-type)))) expr (mem/primitive-type ret-type) diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index e647268..492f786 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -21,6 +21,7 @@ (:require [clojure.spec.alpha :as s]) (:import + (java.nio ByteOrder) (jdk.incubator.foreign Addressable CLinker @@ -30,7 +31,8 @@ MemorySegment ResourceScope ResourceScope$Handle - SegmentAllocator))) + SegmentAllocator + ValueLayout))) (defn stack-scope "Constructs a new scope for use only in this thread. @@ -87,14 +89,14 @@ "Allocates `size` bytes. If a `scope` is provided, the allocation will be reclaimed when it is closed." - ([size] (alloc size (connected-scope))) - ([size scope] (MemorySegment/allocateNative (long size) ^ResourceScope scope))) + (^MemorySegment [size] (alloc size (connected-scope))) + (^MemorySegment [size scope] (MemorySegment/allocateNative (long size) ^ResourceScope scope))) (defn alloc-with "Allocates `size` bytes using the `allocator`." - ([allocator size] + (^MemorySegment [allocator size] (.allocate ^SegmentAllocator allocator (long size))) - ([allocator size alignment] + (^MemorySegment [allocator size alignment] (.allocate ^SegmentAllocator allocator (long size) (long alignment)))) (defmacro with-acquired @@ -122,7 +124,7 @@ "Gets the address of a given segment. This value can be used as an argument to functions which take a pointer." - [addressable] + ^MemoryAddress [addressable] (.address ^Addressable addressable)) (defn null? @@ -143,27 +145,27 @@ Because this fetches from the global segment, it has no associated scope, and therefore the reference created here cannot prevent the value from being freed. Be careful to ensure that you are not retaining an object incorrectly." - [address size] + ^MemorySegment [address size] (.asSlice (MemorySegment/globalNativeSegment) ^MemoryAddress address (long size))) (defn slice "Get a slice over the `segment` with the given `offset`." - ([segment offset] + (^MemorySegment [segment offset] (.asSlice ^MemorySegment segment (long offset))) - ([segment offset size] + (^MemorySegment [segment offset size] (.asSlice ^MemorySegment segment (long offset) (long size)))) (defn slice-into "Get a slice into the `segment` starting at the `address`." - ([address segment] + (^MemorySegment [address segment] (.asSlice ^MemorySegment segment ^MemoryAddress address)) - ([address segment size] + (^MemorySegment [address segment size] (.asSlice ^MemorySegment segment ^MemoryAddress address (long size)))) (defn with-offset "Get a new address `offset` from the old `address`." - [address offset] + ^MemoryAddress [address offset] (.addOffset ^MemoryAddress address (long offset))) (defn as-segment @@ -174,49 +176,441 @@ cleanup in a way that doesn't require modifying the code at the point of freeing, and allows shared or garbage collected resources to be freed correctly." - ([address size scope] - (.asSegment ^MemoryAddress address size scope)) - ([address size scope cleanup] - (.asSegment ^MemoryAddress address size cleanup scope))) + (^MemorySegment [^MemoryAddress address size scope] + (.asSegment address (long size) scope)) + (^MemorySegment [^MemoryAddress address size ^ResourceScope scope cleanup] + (.asSegment address (long size) cleanup scope))) (defn add-close-action! "Adds a 0-arity function to be run when the `scope` closes." - [scope action] - (.addCloseAction ^ResourceScope scope action)) + [^ResourceScope scope ^Runnable action] + (.addCloseAction scope action) + nil) (defn copy-segment - "Copies the content to `dest` from `src`" - [dest src] + "Copies the content to `dest` from `src`. + + Returns `dest`." + ^MemorySegment [^MemorySegment dest ^MemorySegment src] (with-acquired (map segment-scope [src dest]) - (.copyFrom ^MemorySegment dest ^MemorySegment src))) + (.copyFrom dest src) + dest)) (defn clone-segment "Clones the content of `segment` into a new segment of the same size." - ([segment] (clone-segment segment (connected-scope))) - ([segment scope] + (^MemorySegment [segment] (clone-segment segment (connected-scope))) + (^MemorySegment [^MemorySegment segment scope] (with-acquired [(segment-scope segment) scope] - (doto ^MemorySegment (alloc (.byteSize ^MemorySegment segment) scope) - (copy-segment segment))))) + (copy-segment ^MemorySegment (alloc (.byteSize segment) scope) segment)))) (defn slice-segments "Constructs a lazy seq of `size`-length memory segments, sliced from `segment`." - [segment size] - (let [num-segments (quot (.byteSize ^MemorySegment segment) size)] + [^MemorySegment segment size] + (let [num-segments (quot (.byteSize segment) size)] (map #(slice segment (* % size) size) (range num-segments)))) +(def ^ByteOrder big-endian + "The big-endian [[ByteOrder]]. + + See [[little-endian]], [[native-endian]]." + ByteOrder/BIG_ENDIAN) + +(def ^ByteOrder little-endian + "The little-endian [[ByteOrder]]. + + See [[big-endian]], [[native-endian]]" + ByteOrder/LITTLE_ENDIAN) + +(def ^ByteOrder native-endian + "The [[ByteOrder]] for the native endianness of the current hardware. + + See [[big-endian]], [[little-endian]]." + (ByteOrder/nativeOrder)) + +(def ^ValueLayout byte-layout + "The [[MemoryLayout]] for a byte in [[native-endian]] [[ByteOrder]]." + CLinker/C_CHAR) + +(def ^ValueLayout short-layout + "The [[MemoryLayout]] for a c-sized short in [[native-endian]] [[ByteOrder]]." + CLinker/C_SHORT) + +(def ^ValueLayout int-layout + "The [[MemoryLayout]] for a c-sized int in [[native-endian]] [[ByteOrder]]." + CLinker/C_INT) + +(def ^ValueLayout long-layout + "The [[MemoryLayout]] for a c-sized long in [[native-endian]] [[ByteOrder]]." + CLinker/C_LONG) + +(def ^ValueLayout long-long-layout + "The [[MemoryLayout]] for a c-sized long-long in [[native-endian]] [[ByteOrder]]." + CLinker/C_LONG_LONG) + +(def ^ValueLayout char-layout + "The [[MemoryLayout]] for a c-sized char in [[native-endian]] [[ByteOrder]]." + CLinker/C_CHAR) + +(def ^ValueLayout float-layout + "The [[MemoryLayout]] for a c-sized float in [[native-endian]] [[ByteOrder]]." + CLinker/C_FLOAT) + +(def ^ValueLayout double-layout + "The [[MemoryLayout]] for a c-sized double in [[native-endian]] [[ByteOrder]]." + CLinker/C_DOUBLE) + +(def ^ValueLayout pointer-layout + "The [[MemoryLayout]] for a native pointer in [[native-endian]] [[ByteOrder]]." + CLinker/C_POINTER) + +(def ^long short-size + "The size in bytes of a c-sized short." + (.byteSize short-layout)) + +(def ^long int-size + "The size in bytes of a c-sized int." + (.byteSize int-layout)) + +(def ^long long-size + "The size in bytes of a c-sized long." + (.byteSize long-layout)) + +(def ^long long-long-size + "The size in bytes of a c-sized long long." + (.byteSize long-long-layout)) + +(def ^long float-size + "The size in bytes of a c-sized float." + (.byteSize float-layout)) + +(def ^long double-size + "The size in bytes of a c-sized double." + (.byteSize double-layout)) + +(def ^long pointer-size + "The size in bytes of a c-sized pointer." + (.byteSize pointer-layout)) + +(def ^long short-alignment + "The alignment in bytes of a c-sized short." + (.byteAlignment short-layout)) + +(def ^long int-alignment + "The alignment in bytes of a c-sized int." + (.byteAlignment int-layout)) + +(def ^long long-alignment + "The alignment in bytes of a c-sized long." + (.byteAlignment long-layout)) + +(def ^long long-long-alignment + "The alignment in bytes of a c-sized long long." + (.byteAlignment long-long-layout)) + +(def ^long float-alignment + "The alignment in bytes of a c-sized float." + (.byteAlignment float-layout)) + +(def ^long double-alignment + "The alignment in bytes of a c-sized double." + (.byteAlignment double-layout)) + +(def ^long pointer-alignment + "The alignment in bytes of a c-sized pointer." + (.byteAlignment pointer-layout)) + +(defn read-byte + "Reads a [[byte]] from the `segment`, at an optional `offset`." + {:inline + (fn read-byte-inline + ([segment] + `(MemoryAccess/getByte ~segment)) + ([segment offset] + `(MemoryAccess/getByteAtOffset ~segment ~offset)))} + ([^MemorySegment segment] + (MemoryAccess/getByte segment)) + ([^MemorySegment segment ^long offset] + (MemoryAccess/getByteAtOffset segment offset))) + +(defn read-short + "Reads a [[short]] from the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn read-short-inline + ([segment] + `(MemoryAccess/getShort ~segment)) + ([segment offset] + `(MemoryAccess/getShortAtOffset ~segment ~offset)) + ([segment offset byte-order] + `(MemoryAccess/getShortAtOffset ~segment ~offset ~byte-order)))} + ([^MemorySegment segment] + (MemoryAccess/getShort segment)) + ([^MemorySegment segment ^long offset] + (MemoryAccess/getShortAtOffset segment offset)) + ([^MemorySegment segment ^long offset ^ByteOrder byte-order] + (MemoryAccess/getShortAtOffset segment offset byte-order))) + +(defn read-int + "Reads a [[int]] from the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn read-int-inline + ([segment] + `(MemoryAccess/getInt ~segment)) + ([segment offset] + `(MemoryAccess/getIntAtOffset ~segment ~offset)) + ([segment offset byte-order] + `(MemoryAccess/getIntAtOffset ~segment ~offset ~byte-order)))} + ([^MemorySegment segment] + (MemoryAccess/getInt segment)) + ([^MemorySegment segment ^long offset] + (MemoryAccess/getIntAtOffset segment offset)) + ([^MemorySegment segment ^long offset ^ByteOrder byte-order] + (MemoryAccess/getIntAtOffset segment offset byte-order))) + +(defn read-long + "Reads a [[long]] from the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn read-long-inline + ([segment] + `(MemoryAccess/getLong ~segment)) + ([segment offset] + `(MemoryAccess/getLongAtOffset ~segment ~offset)) + ([segment offset byte-order] + `(MemoryAccess/getLongAtOffset ~segment ~offset ~byte-order)))} + (^long [^MemorySegment segment] + (MemoryAccess/getLong segment)) + (^long [^MemorySegment segment ^long offset] + (MemoryAccess/getLongAtOffset segment offset)) + (^long [^MemorySegment segment ^long offset ^ByteOrder byte-order] + (MemoryAccess/getLongAtOffset segment offset byte-order))) + +(defn read-char + "Reads a [[char]] from the `segment`, at an optional `offset`." + {:inline + (fn read-char-inline + ([segment] + `(char (Byte/toUnsignedInt (MemoryAccess/getByte ~segment)))) + ([segment offset] + `(char (Byte/toUnsignedInt (MemoryAccess/getByteAtOffset ~segment ~offset)))))} + ([^MemorySegment segment] + (char (Byte/toUnsignedInt (MemoryAccess/getByte segment)))) + ([^MemorySegment segment ^long offset] + (char (Byte/toUnsignedInt (MemoryAccess/getByteAtOffset segment offset))))) + +(defn read-float + "Reads a [[float]] from the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn read-float-inline + ([segment] + `(MemoryAccess/getFloat ~segment)) + ([segment offset] + `(MemoryAccess/getFloatAtOffset ~segment ~offset)) + ([segment offset byte-order] + `(MemoryAccess/getFloatAtOffset ~segment ~offset ~byte-order)))} + ([^MemorySegment segment] + (MemoryAccess/getFloat segment)) + ([^MemorySegment segment ^long offset] + (MemoryAccess/getFloatAtOffset segment offset)) + ([^MemorySegment segment ^long offset ^ByteOrder byte-order] + (MemoryAccess/getFloatAtOffset segment offset byte-order))) + +(defn read-double + "Reads a [[double]] from the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn read-double-inline + ([segment] + `(MemoryAccess/getDouble ~segment)) + ([segment offset] + `(MemoryAccess/getDoubleAtOffset ~segment ~offset)) + ([segment offset byte-order] + `(MemoryAccess/getDoubleAtOffset ~segment ~offset ~byte-order)))} + (^double [^MemorySegment segment] + (MemoryAccess/getDouble segment)) + (^double [^MemorySegment segment ^long offset] + (MemoryAccess/getDoubleAtOffset segment offset)) + (^double [^MemorySegment segment ^long offset ^ByteOrder byte-order] + (MemoryAccess/getDoubleAtOffset segment offset byte-order))) + +(defn read-address + "Reads a [[MemoryAddress]] from the `segment`, at an optional `offset`." + {:inline + (fn read-address-inline + ([segment] + `(MemoryAccess/getAddress ~segment)) + ([segment offset] + `(MemoryAccess/getAddressAtOffset ~segment ~offset)))} + (^MemoryAddress [^MemorySegment segment] + (MemoryAccess/getAddress segment)) + (^MemoryAddress [^MemorySegment segment ^long offset] + (MemoryAccess/getAddressAtOffset segment offset))) + +(defn write-byte + "Writes a [[byte]] to the `segment`, at an optional `offset`." + {:inline + (fn write-byte-inline + ([segment value] + `(MemoryAccess/setByte ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setByteAtOffset ~segment ~offset ~value)))} + ([^MemorySegment segment value] + (MemoryAccess/setByte segment ^byte value)) + ([^MemorySegment segment ^long offset value] + (MemoryAccess/setByteAtOffset segment offset ^byte value))) + +(defn write-short + "Writes a [[short]] to the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn write-short-inline + ([segment value] + `(MemoryAccess/setShort ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setShortAtOffset ~segment ~offset ~value)) + ([segment offset byte-order value] + `(MemoryAccess/setShortAtOffset ~segment ~offset ~byte-order ~value)))} + ([^MemorySegment segment value] + (MemoryAccess/setShort segment ^short value)) + ([^MemorySegment segment ^long offset value] + (MemoryAccess/setShortAtOffset segment offset ^short value)) + ([^MemorySegment segment ^long offset ^ByteOrder byte-order value] + (MemoryAccess/setShortAtOffset segment offset byte-order ^short value))) + +(defn write-int + "Writes a [[int]] to the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn write-int-inline + ([segment value] + `(MemoryAccess/setInt ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setIntAtOffset ~segment ~offset ~value)) + ([segment offset byte-order value] + `(MemoryAccess/setIntAtOffset ~segment ~offset ~byte-order ~value)))} + ([^MemorySegment segment value] + (MemoryAccess/setInt segment ^int value)) + ([^MemorySegment segment ^long offset value] + (MemoryAccess/setIntAtOffset segment offset ^int value)) + ([^MemorySegment segment ^long offset ^ByteOrder byte-order value] + (MemoryAccess/setIntAtOffset segment offset byte-order ^int value))) + +(defn write-long + "Writes a [[long]] to the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn write-long-inline + ([segment value] + `(MemoryAccess/setLong ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setLongAtOffset ~segment ~offset ~value)) + ([segment offset byte-order value] + `(MemoryAccess/setLongAtOffset ~segment ~offset ~byte-order ~value)))} + (^long [^MemorySegment segment ^long value] + (MemoryAccess/setLong segment value)) + (^long [^MemorySegment segment ^long offset ^long value] + (MemoryAccess/setLongAtOffset segment offset value)) + (^long [^MemorySegment segment ^long offset ^ByteOrder byte-order ^long value] + (MemoryAccess/setLongAtOffset segment offset byte-order value))) + +(defn write-char + "Writes a [[char]] to the `segment`, at an optional `offset`." + {:inline + (fn write-char-inline + ([segment value] + `(MemoryAccess/setByte ~segment (unchecked-byte (unchecked-int ~value)))) + ([segment offset value] + `(MemoryAccess/setByteAtOffset ~segment ~offset (unchecked-byte (unchecked-int ~value)))))} + ([^MemorySegment segment value] + (MemoryAccess/setByte + segment + ;; HACK(Joshua): The Clojure runtime doesn't have an unchecked-byte cast for + ;; characters, so this double cast is necessary unless I emit + ;; my own bytecode with insn. + (unchecked-byte (unchecked-int ^char value)))) + ([^MemorySegment segment ^long offset value] + (MemoryAccess/setByteAtOffset segment offset (unchecked-byte (unchecked-int ^char value))))) + +(defn write-float + "Writes a [[float]] to the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn write-float-inline + ([segment value] + `(MemoryAccess/setFloat ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setFloatAtOffset ~segment ~offset ~value)) + ([segment offset byte-order value] + `(MemoryAccess/setFloatAtOffset ~segment ~offset ~byte-order ~value)))} + ([^MemorySegment segment value] + (MemoryAccess/setFloat segment ^float value)) + ([^MemorySegment segment ^long offset value] + (MemoryAccess/setFloatAtOffset segment offset ^float value)) + ([^MemorySegment segment ^long offset ^ByteOrder byte-order value] + (MemoryAccess/setFloatAtOffset segment offset byte-order ^float value))) + +(defn write-double + "Writes a [[double]] to the `segment`, at an optional `offset`. + + If `byte-order` is not provided, it defaults to [[native-endian]]." + {:inline + (fn write-double-inline + ([segment value] + `(MemoryAccess/setDouble ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setDoubleAtOffset ~segment ~offset ~value)) + ([segment offset byte-order value] + `(MemoryAccess/setDoubleAtOffset ~segment ~offset ~byte-order ~value)))} + (^double [^MemorySegment segment ^double value] + (MemoryAccess/setDouble segment value)) + (^double [^MemorySegment segment ^long offset ^double value] + (MemoryAccess/setDoubleAtOffset segment offset value)) + (^double [^MemorySegment segment ^long offset ^ByteOrder byte-order ^double value] + (MemoryAccess/setDoubleAtOffset segment offset byte-order value))) + +(defn write-address + "Writes a [[MemoryAddress]] to the `segment`, at an optional `offset`." + {:inline + (fn write-address-inline + ([segment value] + `(MemoryAccess/setAddress ~segment ~value)) + ([segment offset value] + `(MemoryAccess/setAddressAtOffset ~segment ~offset ~value)))} + (^MemoryAddress [^MemorySegment segment ^MemoryAddress value] + (MemoryAccess/setAddress segment value)) + (^MemoryAddress [^MemorySegment segment ^long offset ^MemoryAddress value] + (MemoryAccess/setAddressAtOffset segment offset value))) + (defn- type-dispatch "Gets a type dispatch value from a (potentially composite) type." [type] (cond (qualified-keyword? type) type - (sequential? type) (keyword (first type)))) + (sequential? type) (keyword (first type)) + :else (throw (ex-info "Invalid type object" {:type type})))) -(def primitive? +(def primitive-types "A set of all primitive types." #{::byte ::short ::int ::long ::long-long ::char ::float ::double ::pointer}) +(defn primitive? + "A predicate to determine if a given type is primitive." + [type] + (contains? primitive-types (type-dispatch type))) + (defmulti primitive-type "Gets the primitive type that is used to pass as an argument for the `type`. @@ -289,39 +683,51 @@ (defmethod c-layout ::byte [_type] - CLinker/C_CHAR) + byte-layout) (defmethod c-layout ::short - [_type] - CLinker/C_SHORT) + [type] + (if (sequential? type) + (.withOrder short-layout (second type)) + short-layout)) (defmethod c-layout ::int - [_type] - CLinker/C_INT) + [type] + (if (sequential? type) + (.withOrder int-layout (second type)) + int-layout)) (defmethod c-layout ::long - [_type] - CLinker/C_LONG) + [type] + (if (sequential? type) + (.withOrder long-layout (second type)) + long-layout)) (defmethod c-layout ::long-long - [_type] - CLinker/C_LONG_LONG) + [type] + (if (sequential? type) + (.withOrder long-long-layout (second type)) + long-long-layout)) (defmethod c-layout ::char [_type] - CLinker/C_CHAR) + char-layout) (defmethod c-layout ::float - [_type] - CLinker/C_FLOAT) + [type] + (if (sequential? type) + (.withOrder float-layout (second type)) + float-layout)) (defmethod c-layout ::double - [_type] - CLinker/C_DOUBLE) + [type] + (if (sequential? type) + (.withOrder double-layout (second type)) + double-layout)) (defmethod c-layout ::pointer [_type] - CLinker/C_POINTER) + pointer-layout) (def java-prim-layout "Map of primitive type names to the Java types for a method handle." @@ -341,23 +747,27 @@ If a type serializes to a primitive it returns return a Java primitive type. Otherwise, it returns [[MemorySegment]]." - [type] + ^Class [type] (java-prim-layout (or (primitive-type type) type) MemorySegment)) (defn size-of "The size in bytes of the given `type`." - [type] - (.byteSize ^MemoryLayout (c-layout type))) + ^long [type] + (let [t (cond-> type + (not (instance? MemoryLayout type)) c-layout)] + (.byteSize ^MemoryLayout t))) (defn align-of "The alignment in bytes of the given `type`." - [type] - (.byteAlignment ^MemoryLayout (c-layout type))) + ^long [type] + (let [t (cond-> type + (not (instance? MemoryLayout type)) c-layout)] + (.byteAlignment ^MemoryLayout t))) (defn alloc-instance "Allocates a memory segment for the given `type`." - ([type] (alloc-instance type (connected-scope))) - ([type scope] (MemorySegment/allocateNative (long (size-of type)) ^ResourceScope scope))) + (^MemorySegment [type] (alloc-instance type (connected-scope))) + (^MemorySegment [type scope] (MemorySegment/allocateNative ^long (size-of type) ^ResourceScope scope))) (declare serialize serialize-into) @@ -456,35 +866,47 @@ (defmethod serialize-into ::byte [obj _type segment _scope] - (MemoryAccess/setByte segment (byte obj))) + (write-byte segment (byte obj))) (defmethod serialize-into ::short - [obj _type segment _scope] - (MemoryAccess/setShort segment (short obj))) + [obj type segment _scope] + (if (sequential? type) + (write-short segment 0 (second type) (short obj)) + (write-short segment (short obj)))) (defmethod serialize-into ::int - [obj _type segment _scope] - (MemoryAccess/setInt segment (int obj))) + [obj type segment _scope] + (if (sequential? type) + (write-int segment 0 (second type) (int obj)) + (write-int segment (int obj)))) (defmethod serialize-into ::long - [obj _type segment _scope] - (MemoryAccess/setLong segment (long obj))) + [obj type segment _scope] + (if (sequential? type) + (write-long segment 0 (second type) (long obj)) + (write-long segment (long obj)))) (defmethod serialize-into ::long-long - [obj _type segment _scope] - (MemoryAccess/setLong segment (long obj))) + [obj type segment _scope] + (if (sequential? type) + (write-long segment 0 (second type) (long obj)) + (write-long segment (long obj)))) (defmethod serialize-into ::char [obj _type segment _scope] - (MemoryAccess/setChar segment (char obj))) + (write-char segment (char obj))) (defmethod serialize-into ::float - [obj _type segment _scope] - (MemoryAccess/setFloat segment (float obj))) + [obj type segment _scope] + (if (sequential? type) + (write-float segment 0 (second type) (float obj)) + (write-float segment (float obj)))) (defmethod serialize-into ::double - [obj _type segment _scope] - (MemoryAccess/setDouble segment (double obj))) + [obj type segment _scope] + (if (sequential? type) + (write-double segment 0 (second type) (double obj)) + (write-double segment (double obj)))) (defmethod serialize-into ::pointer [obj type segment scope] @@ -535,35 +957,47 @@ (defmethod deserialize-from ::byte [segment _type] - (MemoryAccess/getByte segment)) + (read-byte segment)) (defmethod deserialize-from ::short - [segment _type] - (MemoryAccess/getShort segment)) + [segment type] + (if (sequential? type) + (read-short segment 0 (second type)) + (read-short segment))) (defmethod deserialize-from ::int - [segment _type] - (MemoryAccess/getInt segment)) + [segment type] + (if (sequential? type) + (read-int segment 0 (second type)) + (read-int segment))) (defmethod deserialize-from ::long - [segment _type] - (MemoryAccess/getLong segment)) + [segment type] + (if (sequential? type) + (read-long segment 0 (second type)) + (read-long segment))) (defmethod deserialize-from ::long-long - [segment _type] - (MemoryAccess/getLong segment)) + [segment type] + (if (sequential? type) + (read-long segment 0 (second type)) + (read-long segment))) (defmethod deserialize-from ::char [segment _type] - (char (Byte/toUnsignedInt (MemoryAccess/getByte segment)))) + (read-char segment)) (defmethod deserialize-from ::float - [segment _type] - (MemoryAccess/getFloat segment)) + [segment type] + (if (sequential? type) + (read-float segment 0 (second type)) + (read-float segment))) (defmethod deserialize-from ::double - [segment _type] - (MemoryAccess/getDouble segment)) + [segment type] + (if (sequential? type) + (read-double segment 0 (second type)) + (read-double segment))) (defmethod deserialize-from ::pointer [segment type]