Skip to content

Commit

Permalink
jna pathway passes test
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Sep 23, 2024
1 parent 9a47a50 commit 8c02a5e
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/tech/v3/datatype.clj
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ user> (dtype/make-reader :float32 5 (* idx 2))
Options:
* `:resource-type` - defaults to `:gc` - maps to `:track-type` in `tech.v3.resource`
* `:resource-type` - defaults to `:gc` - maps to `:track-type` in `tech.v3.resource/track`
but can also be set to nil in which case the data is not tracked this library will
not clean it up.
* `:uninitialized?` - do not initialize to zero. Use for perf in very very rare cases.
Expand Down
242 changes: 137 additions & 105 deletions src/tech/v3/datatype/ffi/jna.clj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
byvalue
libffi_test*
4 changes: 4 additions & 0 deletions test/cpp/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

gcc -shared -fPIC -rdynamic -o libffi_test.so ffi_test_lib.c
g++ test_byvalue.cpp -L./ -lffi_test -o byvalue
13 changes: 13 additions & 0 deletions test/cpp/byvalue_nested.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
typedef struct {
int abcd;
struct {
int a;
double b;
} first_struct;
struct {
double c;
int d;
} second_struct;
} ByValue;

extern const char* byvalue_nested(ByValue bv);
13 changes: 13 additions & 0 deletions test/cpp/ffi_test_lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include "byvalue_nested.h"


char json_buffer[1024] = { 0 };


const char* byvalue_nested(ByValue bv) {
snprintf(json_buffer,1024,"{\"abcd\":%d \"a\":%d \"b\":%lf \"c\":%lf \"d\":%d}", bv.abcd,
bv.first_struct.a, bv.first_struct.b,
bv.second_struct.c, bv.second_struct.d);
return json_buffer;
}
13 changes: 13 additions & 0 deletions test/cpp/test_byvalue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <cstdio>
extern "C" {
#include "byvalue_nested.h"
}

using namespace std;


int main (int c, char** v) {
ByValue bc = { 10, { 5, 4.0 }, {3.0, 9}};
printf("%s\n", byvalue_nested(bc));
return 0;
}
44 changes: 42 additions & 2 deletions test/tech/v3/datatype/ffi_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[tech.v3.datatype.native-buffer :as native-buffer]
[tech.v3.datatype.nio-buffer]
[clojure.test :refer [deftest is]]
[clojure.tools.logging :as log])
[clojure.tools.logging :as log]
[clojure.data.json :as json])
(:import [tech.v3.datatype.ffi Pointer]))


Expand Down Expand Up @@ -68,12 +69,51 @@
(dt-ffi/set-ffi-impl! :jna)
(generic-define-library))

(defn nested-byvalue
[]
(let [anon1 (dt-struct/define-datatype! :anon1 [{:name :a :datatype :int32}
{:name :b :datatype :float64}])
anon2 (dt-struct/define-datatype! :anon2 [{:name :c :datatype :float64}
{:name :d :datatype :int32}])
bv-type (dt-struct/define-datatype! :by-value [{:name :abcd :datatype :int32}
{:name :first-struct :datatype :anon1}
{:name :second-struct :datatype :anon2}])
bv (dt-struct/new-struct :by-value {:container-type :native-heap})
^java.util.Map a1 (get bv :first-struct)
^java.util.Map a2 (get bv :second-struct)
_ (do
(.put bv :abcd 10)
(.put a1 :a 5)
(.put a1 :b 4.0)
(.put a2 :c 3.0)
(.put a2 :d 9))
bv-lib-def (dt-ffi/define-library
;;function definitions
{:byvalue_nested {:rettype :pointer
:argtypes [[bv '(by-value :by-value)]]}}
nil nil)
lib (dt-ffi/instantiate-library bv-lib-def (str (System/getProperty "user.dir")
"/test/cpp/libffi_test.so"))
lib-fns @lib
test-fn (get lib-fns :byvalue_nested)
answer (test-fn bv)]
(is (= {"abcd" 10, "a" 5, "b" 4.0, "c" 3.0, "d" 9}
(json/read-str answer)))))

(comment
(nested-byvalue)
)

(deftest jna-byvalue-test
(dt-ffi/set-ffi-impl! :jna)
(nested-byvalue))


(if (dt-ffi/jdk-ffi?)
(deftest mmodel-ffi-test
(dt-ffi/set-ffi-impl! :jdk-21)
(generic-define-library))
(log/warn "JDK-16 FFI pathway not tested."))
(log/warn "JDK-21 FFI pathway not tested."))


(deftest library-instance-test
Expand Down

0 comments on commit 8c02a5e

Please sign in to comment.