Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on @Direct struct parameters #91

Open
SebRevol opened this issue Nov 28, 2016 · 0 comments
Open

Question on @Direct struct parameters #91

SebRevol opened this issue Nov 28, 2016 · 0 comments

Comments

@SebRevol
Copy link

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:

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

struct SimpleStruct {
	double doubleMember;
};

//local copy of doubleMember value
double  localDoubleMember;

//local pointer to the struct
struct SimpleStruct * localStruct;

bool compareDoubleMemberAndLocalCopy(){
	return localStruct->doubleMember == localDoubleMember;
}
bool setStruct(struct SimpleStruct *simpleStruct) {
	localStruct = simpleStruct;
	localDoubleMember = simpleStruct->doubleMember;
	return compareDoubleMemberAndLocalCopy();
}
bool setDirectStruct(struct SimpleStruct *simpleStruct){
	 return setStruct(simpleStruct);
}
bool setStructByPointer( struct SimpleStruct *simpleStruct){
	 return setStruct(simpleStruct);
}

Corresponding java lib :

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant