Skip to content
Merged
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
18 changes: 18 additions & 0 deletions doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Without either setting, the JDK warns about native access.
- [Call a variadic function](#call-a-variadic-function)
- [Use native memory](#use-native-memory)
- [Memory allocated by C](#memory-allocated-by-c)
- [NULL-terminated arrays](#null-terminated-arrays)
- [Arenas](#arenas)
- [Read and write a struct](#read-and-write-a-struct)
- [Fixed arrays](#fixed-arrays)
Expand Down Expand Up @@ -548,6 +549,23 @@ deallocator of its own, bind `free`:
> **_NOTE_** After the deallocator runs, do not use the pointer. This can
> corrupt memory or stop the process.

### NULL-terminated arrays

A C function can return an array of pointers that ends with NULL, such as a
`char **` list of names. Reinterpret the pointer to one more slot at each step
and stop at NULL:

```clojure
(defn null-terminated-strings [p]
(loop [i 0 acc []]
(let [s (ffi/read (ffi/reinterpret p (* (inc i) 8)) :pointer (* i 8))]
(if (ffi/null? s)
acc
(recur (inc i) (conj acc (ffi/ptr->string s)))))))
```

> **_NOTE_** If the array does not end with NULL, the loop reads past it.

### Arenas

An arena owns its allocated memory. When the arena closes, it releases this
Expand Down
Loading