You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi I'm new using JNR and I have a question regarding the "Direct" annotation on struct parameters.
When I read the comment in the code, I understood that adding this annotation would allow to store the struct in direct memory before passing it to native code, to make it persistent for later access.
public interface TestLib {
public class SimpleStruct extends Struct{
protected SimpleStruct(Runtime runtime) {
super(runtime);
}
public Double doubleMember = new Double();
}
public boolean setStructByPointer(Pointer structPointer);
public boolean setDirectStruct(@Direct SimpleStruct struct);
public boolean setStruct(SimpleStruct struct);
public boolean compareDoubleMemberAndLocalCopy();
}
And finally my test case:
public class TestCase {
public static void main(String[] args) {
TestLib testLib = LibraryLoader.create(TestLib.class).load(args[0]);
SimpleStruct simpleStruct = new SimpleStruct(Runtime.getSystemRuntime());
simpleStruct.doubleMember.set(42.0);
boolean areEqual = false;
areEqual = testLib.setStruct(simpleStruct);
System.out.println("Simple set struct : " + areEqual);
areEqual= testLib.compareDoubleMemberAndLocalCopy();
System.out.println("After simple set struct : " + areEqual);
areEqual=testLib.setDirectStruct(simpleStruct);
System.out.println("Direct set direct struct : " + areEqual);
areEqual=testLib.compareDoubleMemberAndLocalCopy();
System.out.println("After set direct struct : " + areEqual);
//here we explicitly allocate direct memory for the struct
Pointer structPointer = Memory.allocateDirect(Runtime.getRuntime(testLib), Struct.size(simpleStruct));
structPointer.putDouble(simpleStruct.doubleMember.offset(),42.0);
areEqual=testLib.setStructByPointer(structPointer);
System.out.println("Set Pointer: " + areEqual);
areEqual=testLib.compareDoubleMemberAndLocalCopy();
System.out.println("After Set Pointer : " + areEqual);
}
}
I have the following output :
Simple set struct : true
After simple set struct : false
Direct set direct struct : true
After set direct struct : false
Set Pointer: true
After Set Pointer : true
We see that simple setStruct and setDirectStruct seem to behave the same, in both case the struct content is lost in native code. It is only preserved if I explicitly allocate Direct memory for the struct (setStructByPointer).
Is it normal? If yes what is finally the effect of the "Direct" annotation?
Thanks in advance,
Best regards,
Sebastien
The text was updated successfully, but these errors were encountered:
Hi I'm new using JNR and I have a question regarding the "Direct" annotation on struct parameters.
When I read the comment in the code, I understood that adding this annotation would allow to store the struct in direct memory before passing it to native code, to make it persistent for later access.
But I have made the following experiment :
My testlib.c:
Corresponding java lib :
And finally my test case:
I have the following output :
We see that simple setStruct and setDirectStruct seem to behave the same, in both case the struct content is lost in native code. It is only preserved if I explicitly allocate Direct memory for the struct (setStructByPointer).
Is it normal? If yes what is finally the effect of the "Direct" annotation?
Thanks in advance,
Best regards,
Sebastien
The text was updated successfully, but these errors were encountered: