Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Returns the native address of pointer p as a Clojure long.
Function.

Returns the alignment, in bytes, of type keyword t or of a struct layout.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1385-L1388">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1431-L1434">Source</a></sub></p>

## <a name="babashka.ffi/alloc">`alloc`</a>
``` clojure
Expand All @@ -160,7 +160,7 @@ result with the matching C deallocator.

CAUTION: Do not close the arena while C uses its memory.
C can access released memory.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1441-L1464">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1487-L1510">Source</a></sub></p>

## <a name="babashka.ffi/auto-arena">`auto-arena`</a>
``` clojure
Expand All @@ -170,7 +170,7 @@ Function.

Returns an arena that the garbage collector manages.
Keep the arena reachable while C uses its pointers. You cannot close it.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1402-L1406">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1448-L1452">Source</a></sub></p>

## <a name="babashka.ffi/byte-buffer">`byte-buffer`</a>
``` clojure
Expand All @@ -186,7 +186,7 @@ invalid memory access can stop the process.

The byte order is big-endian, as it is for each new ByteBuffer. If you need a
different byte order, set it with .order.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1760-L1771">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1806-L1817">Source</a></sub></p>

## <a name="babashka.ffi/callback">`callback`</a>
``` clojure
Expand Down Expand Up @@ -216,9 +216,13 @@ An automatic arena releases the pointer once the pointer itself becomes
unreachable. The garbage collector cannot see the copy that C holds. Use an
automatic arena only when your reference outlives every call that C can make.

Creating a callback is not free in terms of performance, it takes about 8 us
on the JVM and 2 to 4 us in babashka. In performance sensitive code, when
possible, re-use a callback rather than creating it for every invocation.

CAUTION: Unregister the callback before its arena releases the pointer.
Catch exceptions inside f. An uncaught exception can stop the process.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L2713-L2755">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L2759-L2805">Source</a></sub></p>

## <a name="babashka.ffi/cfn">`cfn`</a>
``` clojure
Expand All @@ -244,7 +248,7 @@ create the binding before you load its library.
A :& in argtypes declares a variadic C function. The types before :& are
the fixed parameters. Types after :& declare the variadic argument types.
With no types after :&, each call infers them from its values.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1054-L1111">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1100-L1157">Source</a></sub></p>

## <a name="babashka.ffi/clone">`clone`</a>
``` clojure
Expand All @@ -254,7 +258,7 @@ Function.

Allocates a copy of pointer src in arena with the same size and returns
the new pointer. Use reinterpret to specify a size for pointers from C.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1674-L1681">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1720-L1727">Source</a></sub></p>

## <a name="babashka.ffi/confined-arena">`confined-arena`</a>
``` clojure
Expand All @@ -264,7 +268,7 @@ Function.

Returns an arena for one thread.
Create this arena in with-open to release its memory.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1390-L1394">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1436-L1440">Source</a></sub></p>

## <a name="babashka.ffi/copy">`copy`</a>
``` clojure
Expand All @@ -283,7 +287,7 @@ middle of dst, slice it first:
(ffi/copy src (ffi/slice dst 16) n)

Supports overlapping regions, as with memmove.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1653-L1672">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1699-L1718">Source</a></sub></p>

## <a name="babashka.ffi/defcfn">`defcfn`</a>
``` clojure
Expand Down Expand Up @@ -336,7 +340,7 @@ The wrapper can have multiple arities. Its argument lists can differ from
the C function. The raw name does not enter the namespace. The wrapper
form needs a literal argtypes vector. Only the plain form accepts an
argtypes expression.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1243-L1351">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1289-L1397">Source</a></sub></p>

## <a name="babashka.ffi/find-symbol">`find-symbol`</a>
``` clojure
Expand All @@ -350,7 +354,7 @@ Finds sym and returns a pointer to it. Returns nil for an unknown symbol.
A library value limits the search to one library and its dependencies.
Without a library value, find-symbol searches all loaded libraries. Then it
searches the default system lookup.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L748-L756">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L794-L802">Source</a></sub></p>

## <a name="babashka.ffi/global-arena">`global-arena`</a>
``` clojure
Expand All @@ -360,7 +364,7 @@ Function.

Returns the global arena. Its memory exists until the process stops.
You cannot close this arena.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1408-L1412">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1454-L1458">Source</a></sub></p>

## <a name="babashka.ffi/load-library">`load-library`</a>
``` clojure
Expand All @@ -385,7 +389,7 @@ map uses the keys :mac, :linux, and :windows:
common installation directories. Returns a library map whose :path value
identifies the loaded candidate. The map can be the first argument to cfn.
In that form, cfn searches this library and its dependencies.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L636-L672">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L668-L706">Source</a></sub></p>

## <a name="babashka.ffi/load-system-library">`load-system-library`</a>
``` clojure
Expand All @@ -396,15 +400,15 @@ Function.
Loads a shared library by its short name. For example, "z" selects
libz.dylib, libz.so, or z.dll. On Linux, the search also includes versioned
names such as libz.so.1. Returns the same library map as load-library.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L674-L713">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L708-L749">Source</a></sub></p>

## <a name="babashka.ffi/null">`null`</a>




The NULL pointer.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1796-L1798">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1842-L1844">Source</a></sub></p>

## <a name="babashka.ffi/null?">`null?`</a>
``` clojure
Expand All @@ -413,7 +417,7 @@ The NULL pointer.
Function.

Returns true for a NULL pointer. Returns false for all other pointers.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1800-L1803">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1846-L1849">Source</a></sub></p>

## <a name="babashka.ffi/place">`place`</a>
``` clojure
Expand All @@ -437,7 +441,7 @@ as a vector, a union as a pointer on read and a pair on write. A path to
a union member accepts the member's value directly on write.

Throws for an invalid path. Create a place once and reuse it.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1738-L1758">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1784-L1804">Source</a></sub></p>

## <a name="babashka.ffi/pointer?">`pointer?`</a>
``` clojure
Expand Down Expand Up @@ -482,7 +486,7 @@ specifies the layout member's type and offset.

Checks the access against the size of p. Rejects a zero-size pointer.
reinterpret specifies a valid size.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1512-L1546">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1558-L1592">Source</a></sub></p>

## <a name="babashka.ffi/read-array">`read-array`</a>
``` clojure
Expand All @@ -503,7 +507,7 @@ read with [:array :pointer n].

For an array of structs, or for elements decoded the way read decodes
them, use read with an [:array t n] layout.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1612-L1632">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1658-L1678">Source</a></sub></p>

## <a name="babashka.ffi/reinterpret">`reinterpret`</a>
``` clojure
Expand Down Expand Up @@ -550,7 +554,7 @@ Function.

Returns an arena for multiple threads.
Create this arena in with-open to release its memory.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1396-L1400">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1442-L1446">Source</a></sub></p>

## <a name="babashka.ffi/size">`size`</a>
``` clojure
Expand All @@ -570,7 +574,7 @@ Function.

Returns the size of a type keyword or struct layout, in bytes. The size
of a struct includes padding.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1379-L1383">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1425-L1429">Source</a></sub></p>

## <a name="babashka.ffi/slice">`slice`</a>
``` clojure
Expand All @@ -597,7 +601,7 @@ Function.

Copies s into arena as a NUL-terminated UTF-8 string and returns its
pointer. The arena controls the lifetime of the string.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1773-L1777">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1819-L1823">Source</a></sub></p>

## <a name="babashka.ffi/with-open">`with-open`</a>
``` clojure
Expand All @@ -610,7 +614,7 @@ value in reverse order when body returns or throws.

CAUTION: On Node.js the arena closes when body returns. Do not return a
promise that still uses it.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1414-L1428">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1460-L1474">Source</a></sub></p>

## <a name="babashka.ffi/write">`write`</a>
``` clojure
Expand All @@ -626,7 +630,7 @@ selects a union member, pass the member's value directly.

Checks the access against the size of p. Rejects a zero-size pointer.
reinterpret specifies a valid size.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1548-L1577">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1594-L1623">Source</a></sub></p>

## <a name="babashka.ffi/write-array">`write-array`</a>
``` clojure
Expand All @@ -641,7 +645,7 @@ Copies Java array arr into memory at pointer p, at byte offset (default
Copies raw bytes without converting elements. arr must be a Java array
of the matching type: an int[] for :int, a long[] for :long or :pointer, a
byte[] for :char.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1634-L1651">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1680-L1697">Source</a></sub></p>

## <a name="babashka.ffi/write-string">`write-string`</a>
``` clojure
Expand All @@ -657,4 +661,4 @@ The write takes the UTF-8 byte count of s plus one byte.
(alength (.getBytes s "UTF-8")) is that byte count without the NUL.
Checks the write against the size of p. Rejects a zero-size pointer.
reinterpret specifies a valid size.
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1779-L1794">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/ffi/blob/main/src/babashka/ffi.clj#L1825-L1840">Source</a></sub></p>
18 changes: 17 additions & 1 deletion doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,22 @@ per iteration.

### Callbacks

Creating a callback is not free in terms of performance, it takes about 8 us
on the JVM, 2 to 4 us in babashka and 2 us on Node.js. In performance
sensitive code, when possible, re-use a callback rather than creating it for
every invocation:

```clojure
(def int-comparator
(ffi/callback (ffi/global-arena)
(fn [l r]
(compare (ffi/read (ffi/reinterpret l 4) :int)
(ffi/read (ffi/reinterpret r 4) :int)))
[:pointer :pointer] :int))

(qsort values 5 4 int-comparator)
```

In a build with libffi, every callback goes through libffi, and an empty
callback costs about 30 ns. In a build without libffi, a native image
registers these callback shapes when it is built, and a callback costs about
Expand Down Expand Up @@ -1189,7 +1205,7 @@ The library includes call trampolines and reachability metadata.
For a git dependency, use the sources in the library checkout under `~/.gitlibs`.
For a Maven dependency, first extract the sources from the jar:

```sh
```sh
unzip -o "$(clojure -Spath -A:native | tr ':' '\n' | grep 'ffi-.*\.jar')" \
'babashka/ffi/impl/*.java' -d src-java
```
Expand Down
4 changes: 4 additions & 0 deletions src/babashka/ffi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,10 @@
unreachable. The garbage collector cannot see the copy that C holds. Use an
automatic arena only when your reference outlives every call that C can make.

Creating a callback is not free in terms of performance, it takes about 8 us
on the JVM and 2 to 4 us in babashka. In performance sensitive code, when
possible, re-use a callback rather than creating it for every invocation.

CAUTION: Unregister the callback before its arena releases the pointer.
Catch exceptions inside f. An uncaught exception can stop the process."
[arena f argtypes rettype]
Expand Down
4 changes: 4 additions & 0 deletions src/babashka/ffi.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,10 @@
it once the pointer itself becomes unreachable. The garbage collector
cannot see the copy that C holds.

Creating a callback is not free in terms of performance, it takes about 2 us
on Node.js. In performance sensitive code, when possible, re-use a callback
rather than creating it for every invocation.

CAUTION: Unregister the callback before its arena releases the pointer."
[^Arena arena f argtypes rettype]
(when (or (vector? rettype) (some vector? argtypes))
Expand Down
Loading