Copying Data from Java to C++ Objects Array Using JNI

Guy Yafe :

I have Java code that requires heavy calculations which I would like to forward into C++ using JNI.
My main concern is having all the data serialized in memory, and next to forward the computation to GPU.
Since the data is received in Java, but the main calculations are done using C++, I thought of arranging all the data continuously in a raw array (ByteBuffer or raw bytes from Unsafe), in the same structure as the C++ object.
For example, suppose I have a point with x and y. In C++ the object has a size of 24 bytes. 8 bytes for (I guess) VTable, 8 bytes for x and 8 bytes for y.
So in Java, I would arrange all the data in the same structure and pass the buffer to C++ using JNI, and in C++ cast it to an array of points.

This worked fine, and I am allowing myself to assume that I will always use the same C++ compiler, same JDK, same OS and same HW (at least for testing the feasibility of the solution).

My question is if these assumptions are correct, or there is a better way to pass serialized data between Java and C++ (I must use JNI and not some kind of IPC)?

Some Name :

if I can rely on the C++ structure (offset, alignment of the fields, etc.)

No, unless you know what your compiler on your specific platform will do in that case. It yields undefined behavior.

Aliasing the content of ByteBuffer (aka char *) with Point * to later access its members is not possible in idiomatic C. Take a look at the C Standard N1570 6.5 (p7):

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:88)

— a type compatible with the effective type of the object,

Assuming you know that void * returned by GetDirectBufferAddress is itself returned by a call to malloc(size) or friends (it actually uses malloc) where size_t size = sizeof(struct Point) than you can cast it to Point * initialize its member from native code and later use it. That would be a conforming way (one of).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

passing string array from java to C with JNI

From Java

JNI: passing integer array from Java to C

From Java

Can I reference C++ objects in Java Code using JNI?

From Dev

Efficiently passing GPB serialized data from Java to C++ using JNI

From Java

Getting text data from C++ using JNI through std::ostream into Java

From Dev

Copying data from a raw image using c++ and objective c

From Java

Pass an array back from a JNI function to without copying it

From Dev

Copying objects from one json array to another

From Dev

How to pass a Java Integer array into C using JNI?

From Java

JNI: From C code to Java and JNI

From Dev

Get an Array of Strings from Java to C++ JNI

From Dev

rendering data from an array of objects using an onclick

From Java

JNI Stream binary data from C++ to Java

From Dev

How to call a function with arguments in C++ from JAVA using JNI?

From Java

Passing strings from java to c++ using JNI

From Java

Using a java socket from JNI / C++ code

From Dev

Passing float[] from C++ to Java using JNI

From Dev

Return list<unsigned char*> from C++ to Java using JNI

From Dev

Unable to make JNI call from c++ to java in android lollipop using jni

From Dev

Function to make an array of strings using data from an array of objects?

From Dev

std::transform in copying data from json array to a float array C++

From Dev

copying objects with data fields

From Dev

Check if jobject is an array using Java JNI

From Dev

How to get array of data members from array of objects in c++?

From Java

Getting a null byte array in java from JNI

From Java

How to return an array from JNI to Java?

From Java

How to return int array from Java to JNI

From Java

Copying string from argv to char array in C

From Dev

Copying objects from a pointer

Related Related

  1. 1

    passing string array from java to C with JNI

  2. 2

    JNI: passing integer array from Java to C

  3. 3

    Can I reference C++ objects in Java Code using JNI?

  4. 4

    Efficiently passing GPB serialized data from Java to C++ using JNI

  5. 5

    Getting text data from C++ using JNI through std::ostream into Java

  6. 6

    Copying data from a raw image using c++ and objective c

  7. 7

    Pass an array back from a JNI function to without copying it

  8. 8

    Copying objects from one json array to another

  9. 9

    How to pass a Java Integer array into C using JNI?

  10. 10

    JNI: From C code to Java and JNI

  11. 11

    Get an Array of Strings from Java to C++ JNI

  12. 12

    rendering data from an array of objects using an onclick

  13. 13

    JNI Stream binary data from C++ to Java

  14. 14

    How to call a function with arguments in C++ from JAVA using JNI?

  15. 15

    Passing strings from java to c++ using JNI

  16. 16

    Using a java socket from JNI / C++ code

  17. 17

    Passing float[] from C++ to Java using JNI

  18. 18

    Return list<unsigned char*> from C++ to Java using JNI

  19. 19

    Unable to make JNI call from c++ to java in android lollipop using jni

  20. 20

    Function to make an array of strings using data from an array of objects?

  21. 21

    std::transform in copying data from json array to a float array C++

  22. 22

    copying objects with data fields

  23. 23

    Check if jobject is an array using Java JNI

  24. 24

    How to get array of data members from array of objects in c++?

  25. 25

    Getting a null byte array in java from JNI

  26. 26

    How to return an array from JNI to Java?

  27. 27

    How to return int array from Java to JNI

  28. 28

    Copying string from argv to char array in C

  29. 29

    Copying objects from a pointer

HotTag

Archive