-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #533 from java-native-access/531-callback-cc-from-…
…options [531] Fix stdcall callback calling convention
- Loading branch information
Showing
3 changed files
with
70 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,9 @@ class CallbackReference extends WeakReference { | |
static final Map callbackMap = new WeakHashMap(); | ||
static final Map directCallbackMap = new WeakHashMap(); | ||
static final Map pointerCallbackMap = new WeakHashMap(); | ||
// Track memory allocations associated with this closure (usually String args) | ||
static final Map allocations = new WeakHashMap(); | ||
// Global map of allocated closures to facilitate centralized cleanup | ||
private static final Map allocatedMemory = Collections.synchronizedMap(new WeakHashMap()); | ||
|
||
private static final Method PROXY_CALLBACK_METHOD; | ||
|
@@ -146,9 +148,11 @@ private static Callback getCallback(Class type, Pointer p, boolean direct) { | |
// Keep a reference to the proxy to avoid premature GC of it | ||
CallbackProxy proxy; | ||
Method method; | ||
int callingConvention; | ||
private CallbackReference(Callback callback, int callingConvention, boolean direct) { | ||
super(callback); | ||
TypeMapper mapper = Native.getTypeMapper(callback.getClass()); | ||
this.callingConvention = callingConvention; | ||
Class[] nativeParamTypes; | ||
Class returnType; | ||
|
||
|
@@ -180,6 +184,7 @@ private CallbackReference(Callback callback, int callingConvention, boolean dire | |
} | ||
|
||
String encoding = Native.getStringEncoding(callback.getClass()); | ||
long peer = 0; | ||
if (direct) { | ||
method = getCallbackMethod(callback); | ||
nativeParamTypes = method.getParameterTypes(); | ||
|
@@ -188,12 +193,10 @@ private CallbackReference(Callback callback, int callingConvention, boolean dire | |
if (callback instanceof DLLCallback) { | ||
flags |= Native.CB_OPTION_IN_DLL; | ||
} | ||
long peer = Native.createNativeCallback(callback, method, | ||
nativeParamTypes, returnType, | ||
callingConvention, flags, | ||
encoding); | ||
cbstruct = peer != 0 ? new Pointer(peer) : null; | ||
allocatedMemory.put(this, new WeakReference(this)); | ||
peer = Native.createNativeCallback(callback, method, | ||
nativeParamTypes, returnType, | ||
callingConvention, flags, | ||
encoding); | ||
} | ||
else { | ||
if (callback instanceof CallbackProxy) { | ||
|
@@ -236,12 +239,13 @@ private CallbackReference(Callback callback, int callingConvention, boolean dire | |
} | ||
int flags = callback instanceof DLLCallback | ||
? Native.CB_OPTION_IN_DLL : 0; | ||
long peer = Native.createNativeCallback(proxy, PROXY_CALLBACK_METHOD, | ||
nativeParamTypes, returnType, | ||
callingConvention, flags, | ||
encoding); | ||
cbstruct = peer != 0 ? new Pointer(peer) : null; | ||
peer = Native.createNativeCallback(proxy, PROXY_CALLBACK_METHOD, | ||
nativeParamTypes, returnType, | ||
callingConvention, flags, | ||
encoding); | ||
} | ||
cbstruct = peer != 0 ? new Pointer(peer) : null; | ||
allocatedMemory.put(this, new WeakReference(this)); | ||
} | ||
|
||
private Class getNativeType(Class cls) { | ||
|
@@ -404,8 +408,13 @@ private static Pointer getFunctionPointer(Callback cb, boolean direct) { | |
if ((fp = getNativeFunctionPointer(cb)) != null) { | ||
return fp; | ||
} | ||
Map options = Native.getLibraryOptions(cb.getClass()); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
twall
via email
Author
Contributor
|
||
int callingConvention = cb instanceof AltCallingConvention | ||
? Function.ALT_CONVENTION : Function.C_CONVENTION; | ||
? Function.ALT_CONVENTION | ||
: (options != null && options.containsKey(Library.OPTION_CALLING_CONVENTION) | ||
? ((Integer)options.get(Library.OPTION_CALLING_CONVENTION)).intValue() | ||
: Function.C_CONVENTION); | ||
|
||
Map map = direct ? directCallbackMap : callbackMap; | ||
synchronized(callbackMap) { | ||
CallbackReference cbref = (CallbackReference)map.get(cb); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hmm, I tried this change in the meantime, but this does not work in my env.
I set the Options on a different class. In your test case this is the
OptionCallingConventionTestLibrary
The callback-class does not know anything that it is related to
OptionCallingConventionTestLibrary
. So instead of cb.getClass(), we should pass a OptionCallingConventionTestLibrary.class (which is the invoking instance of the callback.) But as far as I see, we have no reference here unless we extend the dispatcher.c to pass that reference.As workaround, I created a
and changed my initializer to
So my callbacks share the same OPTIONS instance.
Also not very nice, but better than having separate classes for 32bit/Win
(relates to: #531)