This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: SharedLibLoader patch


Ok to check this in?

2001-09-21  Per Bothner  <per@bothner.com>

    * gnu/gcj/runtime/SharedLibLoader.java:  New class.
    * gnu/gcj/runtime/natSharedLibLoader.cc:  Native methods.
    * Makefile.am:  Update accordingly.
    * configure.in:  Add AC_CHECK_LIB for dlopen.
    * include/config.h.in:  Add HAVE_DLOPEN.

Index: gnu/gcj/runtime/SharedLibLoader.java
===================================================================
RCS file: SharedLibLoader.java
diff -N SharedLibLoader.java
--- /dev/null	Tue May  5 13:32:27 1998
+++ SharedLibLoader.java	Fri Sep 21 18:18:33 2001
@@ -0,0 +1,75 @@
+/* Copyright (C) 2001  Free Software Foundation
+
+   This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
+details.  */
+
+package gnu.gcj.runtime;
+import java.util.Hashtable;
+
+/**
+ * A ClassLoader backed by a gcj-compiled shared library.
+ * @author Per Bothner <per@bothner.com>, Brainfood Inc.
+ */
+
+public class SharedLibLoader extends ClassLoader
+{
+  public native void finalize ();
+
+  /** Called during dlopen's processing of the init section. */
+  void registerClass(String name, Class cls)
+  {
+    classMap.put(name, cls);
+  }
+
+  /** Load a shared library, and associate a ClassLoader with it.
+   * @param libname named of shared library (passed to dlopen)
+   * @param parent the parent ClassLoader
+   * @parem flags passed to dlopen
+   */
+  public SharedLibLoader(String libname, ClassLoader parent, int flags)
+  {
+    super(parent);
+    init(libname, flags);
+  }
+
+
+  /** Load a shared library, and asociate a ClassLoader with it.
+   * @param libname named of shared library (passed to dlopen)
+   */
+  public SharedLibLoader(String libname)
+  {
+    super(getSystemClassLoader());
+    init(libname, 0);
+  }
+
+  void init(String libname, int flags)
+  {
+    init(libname.getBytes(), flags);
+  }
+
+  native void init(byte[] libname, int flags);
+
+  public Class loadClass(String name)
+    throws ClassNotFoundException
+  {
+    return super.loadClass(name);
+  }
+
+  public Class findClass(String name)
+    throws ClassNotFoundException
+  {
+    Object cls = classMap.get(name);
+    if (cls == null)
+      throw new ClassNotFoundException(name);
+    return (Class) cls;
+  }
+
+  /** The handle returned by dlopen. */
+  gnu.gcj.RawData handler;
+
+  /** Map classnames to Classes. */
+  Hashtable classMap = new Hashtable(20);
+}
Index: gnu/gcj/runtime/natSharedLibLoader.cc
===================================================================
RCS file: natSharedLibLoader.cc
diff -N natSharedLibLoader.cc
--- /dev/null	Tue May  5 13:32:27 1998
+++ natSharedLibLoader.cc	Fri Sep 21 18:18:33 2001
@@ -0,0 +1,75 @@
+// natSharedLibLoader.cc - Implementation of FirstThread native methods.
+
+/* Copyright (C) 2001  Free Software Foundation
+
+   This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
+details.  */
+
+#include <config.h>
+
+#include <gcj/cni.h>
+#include <gnu/gcj/runtime/SharedLibLoader.h>
+#include <dlfcn.h>
+#include <java/io/IOException.h>
+#include <java/lang/UnsupportedOperationException.h>
+
+#ifdef HAVE_DLOPEN
+/* Only used during dlopen, while having a lock on Class.class. */
+static gnu::gcj::runtime::SharedLibLoader* curLoader;
+
+typedef void (*ClassHookFunc) (jclass);
+
+static void
+::register_hook(jclass cls)
+{
+  curLoader->registerClass(cls->getName(), cls);
+}
+
+struct SharedLibDummy
+{
+  ClassHookFunc saved;
+  SharedLibDummy()
+  {
+    saved = _Jv_RegisterClassHook;
+  }
+  ~SharedLibDummy()
+  {
+    _Jv_RegisterClassHook = saved;
+    curLoader = NULL;
+  }
+};
+#endif
+
+void
+gnu::gcj::runtime::SharedLibLoader::init(jbyteArray libname, jint flags)
+{
+#ifdef HAVE_DLOPEN
+  char *lname = (char*) elements(libname);
+  if (flags==0)
+    flags = RTLD_LAZY;
+  JvSynchronize dummy1(&java::lang::Class::class$);
+  SharedLibDummy dummy2;
+  curLoader = this;
+  _Jv_RegisterClassHook = ::register_hook;
+  void *h = dlopen(lname, flags);
+  if (h == NULL)
+    {
+      const char *msg = dlerror();
+    }
+  handler = (gnu::gcj::RawData*) h;
+#else
+  const char *msg = "ShareedLibLoader is not supported on this platform";
+  throw new java::lang::UnsupportedOperationException(JvNewStringLatin1(msg));
+#endif
+}
+
+void
+gnu::gcj::runtime::SharedLibLoader::finalize()
+{
+#ifdef HAVE_DLOPEN
+  dlclose (handler);
+#endif
+}
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.169
diff -u -p -r1.169 Makefile.am
--- Makefile.am	2001/09/12 15:13:32	1.169
+++ Makefile.am	2001/09/22 01:18:34
@@ -1135,6 +1135,7 @@ gnu/gcj/protocol/jar/Connection.java \
 gnu/gcj/protocol/jar/Handler.java \
 gnu/gcj/runtime/FileDeleter.java \
 gnu/gcj/runtime/FirstThread.java \
+gnu/gcj/runtime/SharedLibLoader.java \
 gnu/gcj/runtime/VMClassLoader.java \
 gnu/java/io/ClassLoaderObjectInputStream.java \
 gnu/java/io/NullOutputStream.java \
@@ -1490,6 +1491,7 @@ gnu/gcj/io/natSimpleSHSStream.cc \
 gnu/gcj/io/shs.cc \
 gnu/gcj/protocol/core/natCoreInputStream.cc \
 gnu/gcj/runtime/natFirstThread.cc \
+gnu/gcj/runtime/natSharedLibLoader.cc \
 java/io/natFile.cc \
 java/io/natFileDescriptor.cc \
 java/io/natObjectInputStream.cc \
Index: configure.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/configure.in,v
retrieving revision 1.98
diff -u -p -r1.98 configure.in
--- configure.in	2001/09/10 18:26:48	1.98
+++ configure.in	2001/09/22 01:18:35
@@ -540,6 +540,8 @@ else
       AC_MSG_ERROR([memcpy is required])
    fi
 
+   AC_CHECK_LIB(dl, dlopen, [AC_DEFINE(HAVE_DLOPEN)])
+
    # Some library-finding code we stole from Tcl.
    #--------------------------------------------------------------------
    #	Check for the existence of the -lsocket and -lnsl libraries.
Index: include/config.h.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/config.h.in,v
retrieving revision 1.32
diff -u -p -r1.32 config.h.in
--- config.h.in	2001/08/01 17:53:00	1.32
+++ config.h.in	2001/09/22 01:18:35
@@ -148,6 +148,8 @@
 /* Define if you have dladdr() */
 #undef HAVE_DLADDR
 
+/* Define if yo have dlopen(). */
+#undef HAVE_DLOPEN
  
 /* Define if getuid() and friends are missing.  */
 #undef NO_GETUID

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]