jna/jna-3.0.9-processopen.patch

99 lines
3.6 KiB
Diff
Raw Normal View History

diff -ur jna-3.0.9/native/dispatch.c jna-3.0.9.process/native/dispatch.c
--- jna-3.0.9/native/dispatch.c 2008-09-22 11:55:59.000000000 -0400
+++ jna-3.0.9.process/native/dispatch.c 2008-12-30 17:03:22.000000000 -0500
@@ -641,14 +641,20 @@
void *handle = NULL;
LIBNAMETYPE libname = NULL;
- if ((libname = LIBNAME2CSTR(env, lib)) != NULL) {
- handle = (void *)LOAD_LIBRARY(libname);
- if (!handle) {
- char buf[1024];
- throwByName(env, EUnsatisfiedLink, LOAD_ERROR(buf, sizeof(buf)));
+ /* dlopen on Unix allows NULL to mean "current process" */
+ if (lib != NULL) {
+ if ((libname = LIBNAME2CSTR(env, lib)) == NULL) {
+ return (jlong)A2L(NULL);
}
- free(libname);
}
+
+ handle = (void *)LOAD_LIBRARY(libname);
+ if (!handle) {
+ char buf[1024];
+ throwByName(env, EUnsatisfiedLink, LOAD_ERROR(buf, sizeof(buf)));
+ }
+ if (libname != NULL)
+ free(libname);
return (jlong)A2L(handle);
}
diff -ur jna-3.0.9/src/com/sun/jna/NativeLibrary.java jna-3.0.9.process/src/com/sun/jna/NativeLibrary.java
--- jna-3.0.9/src/com/sun/jna/NativeLibrary.java 2008-12-30 17:19:15.000000000 -0500
+++ jna-3.0.9.process/src/com/sun/jna/NativeLibrary.java 2008-12-30 17:17:45.000000000 -0500
@@ -45,6 +45,7 @@
private final String libraryPath;
private final Map functions = new HashMap();
+ private static WeakReference currentProcess;
private static final Map libraries = new HashMap();
private static final Map searchPaths = Collections.synchronizedMap(new HashMap());
private static final List librarySearchPath = new LinkedList();
@@ -196,6 +197,27 @@
}
/**
+ * Returns an instance of NativeLibrary which refers to the current process.
+ * This is useful for accessing functions which were already mapped by some
+ * other mechanism, without having to reference or even know the exact
+ * name of the native library.
+ */
+ public static synchronized final NativeLibrary getProcess() {
+ NativeLibrary library = null;
+ if (currentProcess != null) {
+ library = (NativeLibrary) currentProcess.get();
+ }
+
+ if (library == null) {
+ long handle = open(null);
+ library = new NativeLibrary("<process>", null, handle);
+ currentProcess = new WeakReference(library);
+ }
+
+ return library;
+ }
+
+ /**
* Add a path to search for the specified library, ahead of any system paths
*
* @param libraryName The name of the library to use the path for
@@ -287,9 +309,13 @@
public String getName() {
return libraryName;
}
- /** Returns the file on disk corresponding to this NativeLibrary instacne.
+ /**
+ * Returns the file on disk corresponding to this NativeLibrary instance.
+ * If this NativeLibrary represents the current process, this function will return null.
*/
public File getFile() {
+ if (libraryPath == null)
+ return null;
return new File(libraryPath);
}
/** Close the library when it is no longer referenced. */
@@ -300,8 +326,11 @@
public void dispose() {
synchronized(libraries) {
libraries.remove(getName());
- libraries.remove(getFile().getAbsolutePath());
- libraries.remove(getFile().getName());
+ File path = getFile();
+ if (path != null) {
+ libraries.remove(path.getAbsolutePath());
+ libraries.remove(path.getName());
+ }
}
synchronized(this) {
if (handle != 0) {