Skip to content

Commit

Permalink
2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Nov 18, 2018
1 parent 560be05 commit be565f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject techascent/tech.jna "2.1-SNAPSHOT"
(defproject techascent/tech.jna "2.1"
:description "Bindings of tech.datatype system to jna."
:url "http://github.com/tech-ascent/tech.jna"
:license {:name "Eclipse Public License"
Expand Down
45 changes: 27 additions & 18 deletions src/tech/jna.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns tech.jna
(:require [tech.jna.base :as base]
[tech.datatype.jna :as dtype-jna]
[tech.datatype :as dtype])
[tech.gc-resource :as gc-resource])
(:import [com.sun.jna Native NativeLibrary Pointer Function Platform]
[com.sun.jna.ptr PointerByReference]))

Expand Down Expand Up @@ -49,6 +48,7 @@ Use with care; the default if non found is:
[libname native-library]
(base/set-loaded-library! libname native-library))


(defn load-library
^NativeLibrary [libname]
(base/load-library libname))
Expand All @@ -59,6 +59,21 @@ Use with care; the default if non found is:
(base/find-function fn-name libname))


(defn malloc-untracked
"Malloc pointer of Y bytes. Up to call to call Native/free on result at some point"
^Pointer [^long num-bytes]
(Pointer. (Native/malloc num-bytes)))


(defn malloc
"Malloc a pointer of Y bytes. Track using both resource context
and gc system."
^Pointer [^long num-bytes]
(let [retval (malloc-untracked num-bytes)]
(gc-resource/track retval #(Native/free (Pointer/nativeValue retval)))
retval))


(defn unsafe-read-byte
[^Pointer byte-ary ^long idx]
(base/unsafe-read-byte byte-ary idx))
Expand All @@ -67,7 +82,7 @@ Use with care; the default if non found is:
(defn variable-byte-ptr->string
"Convert a c-string into a string"
^String [^Pointer ptr-addr]
(base/variable-byte-ptr->string ptr-addr))
(.getString ptr-addr 0 "ASCII"))


(defn char-ptr-ptr->string-vec
Expand All @@ -78,25 +93,19 @@ Use with care; the default if non found is:

(defn string->ptr
^Pointer [^String data]
(let [str-bytes (.getBytes data "ASCII")
num-bytes (+ (alength str-bytes) 1)
typed-data (dtype-jna/make-typed-pointer :int8 num-bytes)]
(dtype/set-constant! typed-data 0 0 (dtype/ecount typed-data))
(dtype/copy! str-bytes typed-data)
(dtype-jna/->ptr-backing-store typed-data)))
(let [^Pointer retval (malloc (+ 1 (count data)))]
(.setString retval 0 data "ASCII")
retval))


(defn checknil
^Pointer [value]
(let [value (if (satisfies? dtype-jna/PToPtr value)
(dtype-jna/->ptr-backing-store value)
value)]
(if (instance? Pointer value)
(checknil (Pointer/nativeValue value))
(if (= 0 (long value))
(throw (ex-info "Pointer value is nil"
{}))
(Pointer. value)))))
(if (instance? Pointer value)
(checknil (Pointer/nativeValue value))
(if (= 0 (long value))
(throw (ex-info "Pointer value is nil"
{}))
(Pointer. value))))


(defn ensure-type
Expand Down

0 comments on commit be565f3

Please sign in to comment.