harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcfi...@apache.org
Subject svn commit: r645545 - in /harmony/enhanced/drlvm/trunk/vm: em/src/ gc_gen/src/common/ include/ include/open/ port/include/ vmcore/include/ vmcore/src/class_support/ vmcore/src/init/ vmcore/src/jni/ vmcore/src/kernel_classes/native/ vmstart/src/compmgr/
Date Mon, 07 Apr 2008 14:48:13 GMT
Author: mcfirst
Date: Mon Apr  7 07:47:59 2008
New Revision: 645545

URL: http://svn.apache.org/viewvc?rev=645545&view=rev
Log:
Applying the patch from HARMONY-5644 [drlvm][jni] using a component manger for VM startup

Modified:
    harmony/enhanced/drlvm/trunk/vm/em/src/DrlEMImpl.cpp
    harmony/enhanced/drlvm/trunk/vm/em/src/em_intf.cpp
    harmony/enhanced/drlvm/trunk/vm/gc_gen/src/common/gc_for_vm.cpp
    harmony/enhanced/drlvm/trunk/vm/include/component_manager.h
    harmony/enhanced/drlvm/trunk/vm/include/open/compmgr.h
    harmony/enhanced/drlvm/trunk/vm/include/open/vm.h
    harmony/enhanced/drlvm/trunk/vm/port/include/clog.h
    harmony/enhanced/drlvm/trunk/vm/port/include/cxxlog.h
    harmony/enhanced/drlvm/trunk/vm/port/include/port_malloc.h
    harmony/enhanced/drlvm/trunk/vm/vmcore/include/environment.h
    harmony/enhanced/drlvm/trunk/vm/vmcore/include/init.h
    harmony/enhanced/drlvm/trunk/vm/vmcore/include/jni_direct.h
    harmony/enhanced/drlvm/trunk/vm/vmcore/src/class_support/Environment.cpp
    harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/parse_arguments.cpp
    harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/vm_init.cpp
    harmony/enhanced/drlvm/trunk/vm/vmcore/src/jni/jni.cpp
    harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/native/java_lang_EMThreadSupport.cpp
    harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.cpp
    harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.h

Modified: harmony/enhanced/drlvm/trunk/vm/em/src/DrlEMImpl.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/em/src/DrlEMImpl.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/em/src/DrlEMImpl.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/em/src/DrlEMImpl.cpp Mon Apr  7 07:47:59 2008
@@ -51,14 +51,16 @@
 
 DrlEMImpl* DrlEMFactory::emInstance = NULL;
 
+// TODO you may get a question why we get the same execution manager
+// for multiple VM instances the proper way is to get rid of static emInstance
 DrlEMImpl* DrlEMFactory::createAndInitEMInstance() {
-    assert(emInstance == NULL);
-    emInstance = new DrlEMImpl();
-    if (!emInstance->init()) {
-        delete emInstance;
-        emInstance = NULL;
+    if (emInstance == NULL) {
+        emInstance = new DrlEMImpl();
+        if (!emInstance->init()) {
+            delete emInstance;
+            emInstance = NULL;
+        }
     }
-        
     return emInstance;
 }
 

Modified: harmony/enhanced/drlvm/trunk/vm/em/src/em_intf.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/em/src/em_intf.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/em/src/em_intf.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/em/src/em_intf.cpp Mon Apr  7 07:47:59 2008
@@ -29,6 +29,7 @@
 #include <apr_strings.h>
 
 #include "DrlEMImpl.h"
+#include "port_malloc.h"
 
 #include <assert.h>
 
@@ -96,7 +97,7 @@
 
 static const char*
 GetVendor() {
-    return "Intel";
+    return "Apache Software Foundation";
 }
 
 char* tbs_timeout = NULL;
@@ -126,8 +127,6 @@
     return interface_names;
 }
 
-static OpenComponentHandle component_interface = NULL;
-static OpenInstanceAllocatorHandle allocator_interface = NULL;
 static OpenEmVmHandle em_vm_interface = NULL;
 
 static int
@@ -149,15 +148,7 @@
 static int
 CreateInstance(OpenInstanceHandle* p_instance,
                apr_pool_t* pool) {
-    struct _OpenInstance* instance = (struct _OpenInstance*) apr_palloc(pool, sizeof(_OpenComponent));
-
-    instance->intf = component_interface;
-    *p_instance = instance;
-    if (DrlEMFactory::createAndInitEMInstance()!=NULL) {
-        return JNI_OK;
-    } else {
-        return JNI_ERR;
-    }
+    return (DrlEMFactory::createAndInitEMInstance() != NULL) ? JNI_OK : JNI_ERR;
 }
 
 static int
@@ -172,14 +163,9 @@
                  OpenInstanceAllocatorHandle* p_allocator,
                  apr_pool_t* pool)
 {
-    if (component_interface || allocator_interface || em_vm_interface) {
-        /* Virtual table already initialized */
-        return JNI_ERR;
-    }
     em_pool = pool;
 
-    struct _OpenComponent* c_intf = (struct _OpenComponent*) apr_palloc(pool, sizeof(_OpenComponent));
-
+    STD_PCALLOC_STRUCT(pool, _OpenComponent, c_intf);
     c_intf->GetName = GetName;
     c_intf->GetVersion = GetEmVersion;
     c_intf->GetDescription = GetDescription;
@@ -189,12 +175,11 @@
     c_intf->GetInterface = GetInterface;
     c_intf->Free = Free;
 
-    _OpenInstanceAllocator* a_intf = (_OpenInstanceAllocator*)
-        apr_palloc(pool, sizeof(_OpenInstanceAllocator));
+    STD_PCALLOC_STRUCT(pool, _OpenInstanceAllocator, a_intf);
     a_intf->CreateInstance = CreateInstance;
     a_intf->FreeInstance = FreeInstance;
 
-    _OpenEmVm* vm_intf = (_OpenEmVm*) apr_palloc(pool, sizeof(_OpenEmVm));
+    STD_PCALLOC_STRUCT(pool, _OpenEmVm, vm_intf);
     vm_intf->ExecuteMethod = ExecuteMethod;
     vm_intf->CompileMethod = CompileMethod;
     vm_intf->RegisterCodeChunk = RegisterCodeChunk;
@@ -203,15 +188,12 @@
     vm_intf->ProfilerThreadTimeout = ProfilerThreadTimeout;
     vm_intf->ClassloaderUnloadingCallback = ClassloaderUnloadingCallback;
 
-    component_interface = (OpenComponentHandle) c_intf;
-    allocator_interface = (OpenInstanceAllocatorHandle) a_intf;
+    *p_component = (OpenComponentHandle) c_intf;
+    *p_allocator = (OpenInstanceAllocatorHandle) a_intf;
     em_vm_interface = (OpenEmVmHandle) vm_intf;
 
-    *p_component = component_interface;
-    *p_allocator = allocator_interface;
     return JNI_OK;
 }
-
 
 
 #ifdef __cplusplus

Modified: harmony/enhanced/drlvm/trunk/vm/gc_gen/src/common/gc_for_vm.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/gc_gen/src/common/gc_for_vm.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/gc_gen/src/common/gc_for_vm.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/gc_gen/src/common/gc_for_vm.cpp Mon Apr  7 07:47:59 2008
@@ -67,7 +67,9 @@
 int gc_init() 
 {
   INFO2("gc.process", "GC: call GC init...\n");
-  assert(p_global_gc == NULL);
+  if (p_global_gc != NULL) {
+      return JNI_ERR;
+  }
 
   vm_gc_lock_init();
 

Modified: harmony/enhanced/drlvm/trunk/vm/include/component_manager.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/include/component_manager.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/include/component_manager.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/include/component_manager.h Mon Apr  7 07:47:59 2008
@@ -76,17 +76,18 @@
                     const char* initializer_function_name);
 
 /**
- * Deallocate all instances of a given component, unregister a component in a 
- * component manager, and free all component resources using
+ * Does nothing if there was more successful attempts to add the component than
+ * component releases, otherwise deallocates all instances of a given component, unregisters a
+ * component in the component manager, and frees all component resources using
  * <code>Free</code> function. If the component is loaded from a dynamic 
- * library and no components are using the library, then unload
+ * library and no other components are using the library, then unloads
  * the dynamic library.
  *
  * This function is safe to call from multiple threads.
  *
  * @return <code>APR_SUCCESS</code> if successful, or a non-zero error code.
  */
-int CmFreeComponent(const char* component_name);
+int CmReleaseComponent(const char* component_name);
 
 
 #ifdef __cplusplus

Modified: harmony/enhanced/drlvm/trunk/vm/include/open/compmgr.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/include/open/compmgr.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/include/open/compmgr.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/include/open/compmgr.h Mon Apr  7 07:47:59 2008
@@ -38,19 +38,15 @@
 
 /**
  * The <code>_OpenInterface</code> structure represents
- * a virtual table of interface functions.
+ * a virtual table of interface functions. An interface 
+ * contains function pointers:
+ * <pre>
+ *    void* func1;
+ *    void* func2;
+ *    void* func3;
+ * </pre>
  */
-struct _OpenInterface
-{
-    void* func1;
-    /*
-     * A real interface can contain more
-     * function pointers.
-     *
-     * void* func2;
-     * void* func3;
-     */
-};
+union _OpenInterface;
 
 /**
  * @name Handles
@@ -62,7 +58,7 @@
  * @ingroup Handles
  * The handle of the abstract component interface.
  */
-typedef const struct _OpenInterface* OpenInterfaceHandle;
+typedef const union _OpenInterface* OpenInterfaceHandle;
 
 /**
  * Default component interface. Each component must
@@ -120,7 +116,6 @@
      * @return APR_SUCCESS if successful, otherwise a non-zero error code
      */
     int (*Free) ();
-
 };
 /**
  * @ingroup Handles
@@ -129,17 +124,22 @@
 typedef const struct _OpenComponent* OpenComponentHandle;
 
 /**
- * The generic component instance.
+ * The generic component instance. It contains several interfacec tables and
+ * other data.
+ * <pre>
+ *      OpenInterfaceHandle intf1;
+ *      OpenInterfaceHandle intf2;
+ *      void* data1;
+ *      void* data2;
+ * </pre>
  */
-struct _OpenInstance
-{
-    OpenComponentHandle intf;
-};
+union _OpenInstance;
+
 /**
  * @ingroup Handles
  * The handle of the open instance.
  */
-typedef const struct _OpenInstance* OpenInstanceHandle;
+typedef const union _OpenInstance* OpenInstanceHandle;
 
 /**
  * The private component interface used by a component manager to allocate
@@ -183,34 +183,58 @@
 {
 
     /**
-     * Get a default interface of a registered component by name.
+     * Gets a default interface of a registered component by name.
      * @param[out] p_component on return, points to
-     * a handle of default component interface
-     * @param name a component name
-     * @return APR_SUCCESS if successful, otherwise a non-zero error code
+     * a handle of the default component interface
+     * @param name the component name
+     * @return APR_SUCCESS if successful, APR_NOTFOUND if a component with
+     * the given name cannot be found, or another non-zero error code
      */
     int (*GetComponent) (OpenComponentHandle* p_component,
             const char* name);
 
     /**
-     * Create a new component instance and register it in
+     * Gets a default interface of a component instance.
+     * @param[out] p_component on return, points to
+     * a handle of the default component interface
+     * @param instance the instance handle
+     * @return APR_SUCCESS if successful, otherwise a non-zero error code
+     */
+    int (*GetComponentByInstance) (OpenComponentHandle* p_component,
+            OpenInstanceHandle instance);
+
+
+    /**
+     * Creates a new component instance and register it in
      * a component manager.
      * @param[out] p_instance on return, points to a handle of a newly
      * created instance
-     * @param name a name of interface
+     * @param name the component name
      * @return APR_SUCCESS if successful, otherwise a non-zero error code
      */
     int (*CreateInstance) (OpenInstanceHandle* p_instance,
             const char* name);
 
     /**
-     * Unregister the instance in a component manager,
-     * free memory and other resources held by the instance.
+     * Unregisters the instance in a component manager,
+     * frees memory and other resources held by the instance.
      * @param instance a handle to the instance to Free
      * @return APR_SUCCESS if successful, otherwise a non-zero error code
      */
     int (*FreeInstance) (OpenInstanceHandle instance);
 
+    /**
+     * Fills a pre-allocated buffer with instances of the component.
+     * @param[in,out] a pointer to the given buffer which is preallocated by
+     * a caller and filled with component instances on return
+     * @param buf_len the length of the buffer
+     * @param[out] len an actual number of instances
+     * @param name a component name
+     * @return APR_SUCCESS if successful, APR_NOTFOUND if a component with
+     * the given name cannot be found, or another non-zero error code
+     */
+    int (*GetInstances) (OpenInstanceHandle* p_instance, int buf_len, int* len,
+            const char* name);
 };
 /**
  * @ingroup Handles

Modified: harmony/enhanced/drlvm/trunk/vm/include/open/vm.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/include/open/vm.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/include/open/vm.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/include/open/vm.h Mon Apr  7 07:47:59 2008
@@ -23,17 +23,19 @@
 #ifndef _VM_EXPORT_H
 #define _VM_EXPORT_H
 
+#define OPEN_VM "vm"
+#define OPEN_VM_VERSION "1.0"
+#define OPEN_INTF_VM "open.interface.vm." OPEN_VM_VERSION
+
+#define O_A_H_VM_VMDIR  "org.apache.harmony.vm.vmdir"
+
 #ifdef WIN32
 #include <stddef.h>
 #else
 #include <unistd.h>
 #endif
-
-
-
 #include "open/types.h"
 
-#define O_A_H_VM_VMDIR         "org.apache.harmony.vm.vmdir"
 
 #ifdef __cplusplus
 extern "C" {

Modified: harmony/enhanced/drlvm/trunk/vm/port/include/clog.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/port/include/clog.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/port/include/clog.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/port/include/clog.h Mon Apr  7 07:47:59 2008
@@ -107,7 +107,7 @@
 #define VERIFY(expr, message) {\
         if(!(expr)) { \
         const char* formatted_msg = log_printf message; \
-        const char* complete_msg = log_printf("Assertion failed: %s\n %s", #expr, formatted_msg); \
+        const char* complete_msg = log_printf("Internal error: %s failed\n%s", #expr, formatted_msg); \
         log4cxx_from_c(LOG_DOMAIN, DIE, complete_msg, __FILE__, __LOG4CXX_FUNC__, __LINE__); \
         STD_FREE((void*)formatted_msg); \
         STD_FREE((void*)complete_msg); \
@@ -119,6 +119,12 @@
 #define ASSERT(expr, message)  VERIFY(expr, message)
 
 #endif //NDEBUG 
+
+#define VERIFY_SUCCESS(func) { \
+    int ret = func; \
+    VERIFY(0 == ret, \
+        ("a call to " #func " returned a non-zero error code %d", ret)); \
+}
 
 #endif // LOG_DOMAIN
 

Modified: harmony/enhanced/drlvm/trunk/vm/port/include/cxxlog.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/port/include/cxxlog.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/port/include/cxxlog.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/port/include/cxxlog.h Mon Apr  7 07:47:59 2008
@@ -157,7 +157,7 @@
 #define VERIFY(expr, message) { \
     if (!(expr)) { \
     LoggerString logger_string; \
-    logger_string << "Assertion failed: " << #expr << "\n" << message; \
+    logger_string << "Internal error: " << #expr << " failed\n" << message; \
     log4cxx_from_c(LOG_DOMAIN, DIE, logger_string.release(), __FILE__, __LOG4CXX_FUNC__, __LINE__); \
     shutdown_log_system(); \
     assert(expr); \
@@ -166,12 +166,6 @@
 #define ASSERT(expr, message)  VERIFY(expr, message)
 
 #endif // NDEBUG 
-
-#define PORT_VERIFY_SUCCESS(func) { \
-    int ret = func; \
-    VERIFY(APR_SUCCESS == ret, \
-        "Internal error: a call returned non-zero error code" #func); \
-}
 
 #endif // LOG_DOMAIN
 

Modified: harmony/enhanced/drlvm/trunk/vm/port/include/port_malloc.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/port/include/port_malloc.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/port/include/port_malloc.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/port/include/port_malloc.h Mon Apr  7 07:47:59 2008
@@ -158,4 +158,10 @@
     #define STD_ALLOCA(s) alloca(s)
 #endif // _MEMMGR
 
+#define STD_PCALLOC_STRUCT(pool, type, name) \
+    type* name = (type*) apr_pcalloc(pool, sizeof(type)); \
+    if (NULL == name) { \
+        return ENOMEM; \
+    }
+
 #endif // _PORT_MALLOC_H_

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/include/environment.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/include/environment.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/include/environment.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/include/environment.h Mon Apr  7 07:47:59 2008
@@ -337,6 +337,7 @@
      */
 
     OpenComponentManagerHandle cm;
+    OpenComponentHandle em_component;
     OpenInstanceHandle em_instance;
     OpenEmVmHandle em_interface;
 

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/include/init.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/include/init.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/include/init.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/include/init.h Mon Apr  7 07:47:59 2008
@@ -40,7 +40,8 @@
 /**
  * Parses string pool size required for environment initialization.
  */
-void parse_vm_arguments1(JavaVMInitArgs *vm_args, size_t *string_pool_size);
+void parse_vm_arguments1(JavaVMInitArgs *vm_args, size_t *p_string_pool_size,
+                         jboolean *p_is_class_data_shared, void **portlib);
 /**
  * Collects all arguments in VM properties.
  */

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/include/jni_direct.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/include/jni_direct.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/include/jni_direct.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/include/jni_direct.h Mon Apr  7 07:47:59 2008
@@ -36,7 +36,7 @@
 typedef struct JNIEnv_Internal JNIEnv_Internal;
 
 struct JavaVM_Internal : public JavaVM_External {
-    apr_pool_t * pool;
+    apr_pool_t * pool1;
     Global_Env * vm_env;   
     APR_RING_ENTRY(JavaVM_Internal) link;
     void* reserved;

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/src/class_support/Environment.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/src/class_support/Environment.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/src/class_support/Environment.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/src/class_support/Environment.cpp Mon Apr  7 07:47:59 2008
@@ -262,9 +262,6 @@
     // Unload jit instances.
     vm_delete_all_jits();
 
-    // Unload component manager and all registered components.
-    CmRelease();
-
     // Unload all system native libraries.
     natives_cleanup();
 

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/parse_arguments.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/parse_arguments.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/parse_arguments.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/parse_arguments.cpp Mon Apr  7 07:47:59 2008
@@ -48,29 +48,15 @@
 #define LOG_DOMAIN "vm.core"
 #include "cxxlog.h"
 
-//------ Begin DYNOPT support --------------------------------------
-//
-#ifndef PLATFORM_POSIX       // for DYNOPT in Win64
-
-extern JIT      *dynopt_jitc;
-extern bool     dynamic_optimization;
-extern bool     software_profile;
-extern bool        data_ear;
-extern unsigned long    data_ear_interval;
-extern int              data_ear_filter;
-extern bool        branch_trace;
-extern unsigned long    btb_interval;
-
-#endif // !PLATFORM_POSIX
-//
-//------ End DYNOPT support ----------------------------------------
-
 #define EXECUTABLE_NAME "java"
 #define USE_JAVA_HELP LECHO(29, "Use {0} -help to get help on command line options" << EXECUTABLE_NAME)
 #define LECHO_VERSION LECHO(32, VERSION << VERSION_SVN_TAG << __DATE__ << VERSION_OS \
         << VERSION_ARCH << VERSION_COMPILER << VERSION_DEBUG_STRING)
 #define LECHO_VM_VERSION LECHO(33, VM_VERSION)
-#define STRING_POOL_SIZE_OPT "-XX:vm.stringPoolSize="
+#define STRING_POOL_SIZE_OPTION "-XX:vm.stringPoolSize="
+#define CLASS_DATA_SHARING_OFF_OPTION "-Xshare:off"
+#define CLASS_DATA_SHARING_ON_OPTION "-Xshare:on"
+#define PORTLIB_OPTION "_org.apache.harmony.vmi.portlib"
 
 extern bool dump_stubs;
 extern bool parallel_jit;
@@ -140,29 +126,25 @@
     }
 }
 
-void* get_portlib_for_logger(Global_Env *p_env) {
-    for (int i = 0; i < p_env->vm_arguments.nOptions; i++) {
-        const char* option = p_env->vm_arguments.options[i].optionString;
-        if (strcmp(option, "_org.apache.harmony.vmi.portlib") == 0) {
-            return p_env->vm_arguments.options[i].extraInfo;
-        }
-    }
-    return NULL;
-}
-
-void parse_vm_arguments1(JavaVMInitArgs *vm_args, size_t *p_string_pool_size)
+void parse_vm_arguments1(JavaVMInitArgs *vm_args, size_t *p_string_pool_size, jboolean *p_is_class_data_shared, void **portlib)
 {
     *p_string_pool_size = DEFAULT_STRING_TABLE_SIZE;
     for (int i = 0; i < vm_args->nOptions; i++) {
         const char* option = vm_args->options[i].optionString;
-        if (begins_with(option, STRING_POOL_SIZE_OPT)) {
-            const char* arg = option + strlen(STRING_POOL_SIZE_OPT);
+        if (begins_with(option, STRING_POOL_SIZE_OPTION)) {
+            const char* arg = option + strlen(STRING_POOL_SIZE_OPTION);
             *p_string_pool_size = parse_size(arg);
             if (0 == *p_string_pool_size) {
                 LECHO(34, "Negative or invalid string pool size. A default value is used, " << DEFAULT_STRING_TABLE_SIZE << " bytes.");
                 *p_string_pool_size = DEFAULT_STRING_TABLE_SIZE;
             }
             TRACE2("init", "string_pool_size = " << *p_string_pool_size);
+        } else if (!strcmp(option, CLASS_DATA_SHARING_OFF_OPTION)) {
+            *p_is_class_data_shared = JNI_FALSE;
+        } else if (!strcmp(option, CLASS_DATA_SHARING_ON_OPTION)) {
+            *p_is_class_data_shared = JNI_TRUE;
+        } else if (!strcmp(option, PORTLIB_OPTION)) {
+            *portlib = vm_args->options[i].extraInfo;
         }
     }
 } // parse_vm_arguments1
@@ -308,7 +290,7 @@
             }
             p_env->VmProperties()->set("thread.stacksize", arg);
     	}	  
-        else if (begins_with(option, STRING_POOL_SIZE_OPT)) {
+        else if (begins_with(option, STRING_POOL_SIZE_OPTION)) {
             // the pool is already created
         }
         else if (begins_with(option, "-agentlib:")) {

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/vm_init.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/vm_init.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/vm_init.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/src/init/vm_init.cpp Mon Apr  7 07:47:59 2008
@@ -231,15 +231,19 @@
         return status;
     }
 
-    status = vm_env->cm->CreateInstance(&(vm_env->em_instance), "em");
-    if (status != JNI_OK) {
+    status = vm_env->cm->GetComponent(&(vm_env->em_component), OPEN_EM);
+    if (JNI_OK != status) {
+        return status;
+    }
+
+    status = vm_env->cm->CreateInstance(&(vm_env->em_instance), OPEN_EM);
+    if (JNI_OK != status) {
         LWARN(14, "Cannot instantiate EM");
         return status;
     }
 
-    status = vm_env->em_instance->intf->GetInterface(
-        (OpenInterfaceHandle*) &(vm_env->em_interface), OPEN_INTF_EM_VM);
-    if (status != JNI_OK) {
+    status = vm_env->em_component->GetInterface((OpenInterfaceHandle*) &(vm_env->em_interface), OPEN_INTF_EM_VM);
+    if (JNI_OK != status) {
         LWARN(15, "Cannot get EM_VM interface");
         return status;
     }
@@ -801,18 +805,8 @@
     // Initialize arguments
     initialize_vm_cmd_state(vm_env, vm_arguments);
 
-    // Initialize logging system as soon as possible.
-    init_log_system(get_portlib_for_logger(vm_env));
-    set_log_levels_from_cmd(&vm_env->vm_arguments);
-
     vm_monitor_init();
 
-    status = CmAcquire(&vm_env->cm);
-    if (status != JNI_OK) {
-        LWARN(23, "Failed to initialize a \"Component Manager\".");
-        return status; 
-    }
-
     /*    BEGIN: Property processing.    */
 
     // 20030407 Note: property initialization must follow initialization of the default JITs to allow 
@@ -822,7 +816,6 @@
     if (status != JNI_OK) return status;
 
     tm_properties = (struct tm_props*) STD_MALLOC(sizeof(struct tm_props));
-
     if (!tm_properties) {
         LWARN(30, "failed to allocate mem for tp properties");
         return JNI_ERR;

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/src/jni/jni.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/src/jni/jni.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/src/jni/jni.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/src/jni/jni.cpp Mon Apr  7 07:47:59 2008
@@ -57,6 +57,7 @@
 #include "finalizer_thread.h"
 #include "ref_enqueue_thread.h"
 #include "port_malloc.h"
+#include "component_manager.h"
 
 #ifdef _IPF_
 #include "stub_code_utils.h"
@@ -355,51 +356,102 @@
 };
 
 /**
- * List of all running in the current process.
+ * VM instances running in the current process.
  */ 
-APR_RING_HEAD(JavaVM_Internal_T, JavaVM_Internal) GLOBAL_VMS;
+union _OpenInstance {
+    JavaVM_Internal jvm;
+};
 
-/**
- * Memory pool to keep global data.
- */
-apr_pool_t * GLOBAL_POOL = NULL;
+static const char*
+GetName() {
+    return OPEN_VM;
+}
 
-/**
- * Used to synchronize VM creation and destruction.
- */
-apr_thread_mutex_t * GLOBAL_LOCK = NULL;
-
-static jboolean & get_init_status() {
-    static jboolean init_status = JNI_FALSE;
-    return init_status;
+static const char*
+GetVmVersion() {
+    return OPEN_VM_VERSION;
 }
-/**
- * Initializes JNI module.
- * Should  be called before creating first VM.
- */
-static jint jni_init()
-{
-    jint status;
 
-    if (get_init_status() == JNI_FALSE) {
-         status = apr_initialize();
-        if (status != APR_SUCCESS) return JNI_ERR;
-
-        if (port_atomic_cas8((volatile uint8 *)&get_init_status(), JNI_TRUE, JNI_FALSE) == JNI_FALSE) {
-            APR_RING_INIT(&GLOBAL_VMS, JavaVM_Internal, link);
-            status = apr_pool_create(&GLOBAL_POOL, 0);
-            if (status != APR_SUCCESS) return JNI_ERR;
-
-            status = apr_thread_mutex_create(&GLOBAL_LOCK, APR_THREAD_MUTEX_DEFAULT, GLOBAL_POOL);
-            if (status != APR_SUCCESS) {
-                apr_pool_destroy(GLOBAL_POOL);
-                return JNI_ERR;
-            }
-        }
+static const char*
+GetDescription() {
+    return "Java virtual machine core";
+}
+
+static const char*
+GetVendor() {
+    return "Apache Software Foundation";
+}
+
+static const char*
+GetProperty(const char* key) {
+    return NULL;
+}
+
+static const char* interface_names[] = {
+    OPEN_INTF_VM,
+    NULL
+};
+
+static const char**
+ListInterfaceNames() {
+    return interface_names;
+}
+
+static int
+GetInterface(OpenInterfaceHandle* p_intf,
+             const char* intf_name) {
+    if (!strcmp(intf_name, OPEN_INTF_VM)) {
+        *p_intf = NULL; // no interface for now
+        return JNI_OK;
+    } else {
+        return JNI_ERR;
     }
+}
+
+static int
+CreateInstance(OpenInstanceHandle* p_instance,
+               apr_pool_t* pool) {
+    STD_PCALLOC_STRUCT(pool, JavaVM_Internal, instance);
+
+    instance->pool1 = pool; // TODO rename pool1 bak to pool
+    *p_instance = (OpenInstanceHandle) instance;
+    return JNI_OK;
+}
+
+static int
+FreeInstance(OpenInstanceHandle instance) {
+    return JNI_OK;
+}
+
+static int
+Free() {
+    return JNI_OK;
+}
+
+int VmInitialize(OpenComponentHandle* p_component,
+                 OpenInstanceAllocatorHandle* p_allocator,
+                 apr_pool_t* pool)
+{
+    STD_PCALLOC_STRUCT(pool, _OpenComponent, c_intf);
+    c_intf->GetName = GetName;
+    c_intf->GetVersion = GetVmVersion;
+    c_intf->GetDescription = GetDescription;
+    c_intf->GetVendor = GetVendor;
+    c_intf->GetProperty = GetProperty;
+    c_intf->ListInterfaceNames = ListInterfaceNames;
+    c_intf->GetInterface = GetInterface;
+    c_intf->Free = Free;
+
+    STD_PCALLOC_STRUCT(pool, _OpenInstanceAllocator, a_intf);
+    a_intf->CreateInstance = CreateInstance;
+    a_intf->FreeInstance = FreeInstance;
+
+    *p_component = (OpenComponentHandle) c_intf;
+    *p_allocator = (OpenInstanceAllocatorHandle) a_intf;
     return JNI_OK;
 }
 
+
 /*    BEGIN: List of directly exported functions.    */
 
 jint JNICALL JNI_GetDefaultJavaVMInitArgs(void * args)
@@ -413,27 +465,19 @@
 }
 
 jint JNICALL JNI_GetCreatedJavaVMs(JavaVM ** vmBuf,
-                                               jsize bufLen,
-                                               jsize * nVMs)
+                                   jsize bufLen,
+                                   jsize *nVMs)
 {
-    jint status = jni_init();
-    if (status != JNI_OK) {
+    OpenComponentManagerHandle cm;
+
+    int status  = CmAcquire(&cm);
+    if (APR_SUCCESS != status) {
         return status;
     }
 
-    apr_thread_mutex_lock(GLOBAL_LOCK);
-
-    *nVMs = 0;
-    JavaVM_Internal * current_vm = APR_RING_FIRST(&GLOBAL_VMS);
-    while (current_vm != APR_RING_SENTINEL(&GLOBAL_VMS, JavaVM_Internal, link)) {
-        if (*nVMs < bufLen) {
-            vmBuf[*nVMs] = (JavaVM *)current_vm;
-        }
-        ++(*nVMs);
-        current_vm = APR_RING_NEXT(current_vm, link);
-    }
-    apr_thread_mutex_unlock(GLOBAL_LOCK);
-    return JNI_OK;
+    status = cm->GetInstances((OpenInstanceHandle*) vmBuf, bufLen, nVMs, OPEN_VM);
+    int release_status = CmRelease();
+    return (status == JNI_OK) ? release_status : status;
 }
 
 jint JNICALL JNI_CreateJavaVM(JavaVM ** p_vm, JNIEnv ** p_jni_env,
@@ -441,85 +485,89 @@
     jboolean daemon = JNI_FALSE;
     char * name = "main";
     JNIEnv * jni_env;
-    JavaVMInitArgs * vm_args;
-    JavaVM_Internal * java_vm;
     Global_Env * vm_env;
-    apr_pool_t * vm_global_pool = NULL;
     jthread java_thread;
-    jint status;
-
-    status = jni_init();        
-    
-    if (status != JNI_OK) return status;
+    int n_vm = 0;
 
 #ifdef _MEMMGR
     start_monitor_malloc();
 #endif
 
-    apr_thread_mutex_lock(GLOBAL_LOCK);
-
-    // TODO: only one VM instance can be created in the process address space.
-    if (!APR_RING_EMPTY(&GLOBAL_VMS, JavaVM_Internal, link)) {
-        status = JNI_ERR;
-        goto done;
+    // TODO implement support for JDK1_1InitArgs
+    if (((JavaVMInitArgs *)args)->version == JNI_VERSION_1_1) {
+        return JNI_EVERSION;
     }
-
-    // Create global memory pool.
-    status = apr_pool_create(&vm_global_pool, NULL);
-    if (status != APR_SUCCESS) {
-        TRACE2("jni", "Unable to create memory pool for VM");
-        status = JNI_ENOMEM;
-        goto done;
+    JavaVMInitArgs *vm_args = (JavaVMInitArgs *)args;
+ 
+    // get a string pool size and if the shared data pool should be read from cache
+    size_t string_pool_size;
+    jboolean is_class_data_shared;
+    void *portlib = NULL;
+    parse_vm_arguments1(vm_args, &string_pool_size, &is_class_data_shared, &portlib);
+
+    // initialize logging system as soon as possible
+    // log system supports the only instance
+    // TODO make the logging initialization thread and multi-instance safe
+    init_log_system(portlib);
+    set_log_levels_from_cmd(vm_args);
+
+    OpenComponentManagerHandle cm;
+    int status  = CmAcquire(&cm);
+    if (APR_SUCCESS != status) {
+        return status;
     }
 
-    // TODO: current implementation doesn't support JDK1_1InitArgs.
-    if (((JavaVMInitArgs *)args)->version == JNI_VERSION_1_1) {
-        status = JNI_EVERSION;
-        goto done;
+    // Register VM component.
+    status = CmAddComponent(VmInitialize);
+    if (JNI_OK != status) {
+        goto component_allocation_error;
     }
 
-    vm_args = (JavaVMInitArgs *)args;
-    // Create JavaVM_Internal.
-    java_vm = (JavaVM_Internal *) apr_palloc(vm_global_pool, sizeof(JavaVM_Internal));
-    if (java_vm == NULL) {
-        status = JNI_ENOMEM;
-        goto done;
+    JavaVM_Internal* java_vm;
+    status = cm->CreateInstance((OpenInstanceHandle*) &java_vm, OPEN_VM);
+    if (JNI_OK != status) {
+        goto instance_allocation_error;
     }
 
-    // Get a string pool size.
-    size_t string_pool_size;
-    parse_vm_arguments1(vm_args, &string_pool_size);
+    status = cm->GetInstances(NULL, 0, &n_vm, OPEN_VM);
+    // TODO implement support more than one VM instance in the process address space
+    if (1 != n_vm) {
+        status = JNI_ERR;
+        goto instance_number_error;
+    }
 
     // Create Global_Env.
-    vm_env = new(vm_global_pool) Global_Env(vm_global_pool, string_pool_size);
+    vm_env = new(java_vm->pool1) Global_Env(java_vm->pool1, string_pool_size);
     if (vm_env == NULL) {
         status = JNI_ENOMEM;
-        goto done;
+        goto error;
     }
 
+    vm_env->cm = cm;
     vm_env->start_time = apr_time_now()/1000;
 
     java_vm->functions = &java_vm_vtable;
-    java_vm->pool = vm_global_pool;
     java_vm->vm_env = vm_env;
     java_vm->reserved = (void *)0x1234abcd;
     *p_vm = java_vm;
         
     status = vm_init1(java_vm, vm_args);
     if (status != JNI_OK) {
-        goto done;
+        goto error;
     }
 
     // Attaches main thread to VM.
     status = vm_attach_internal(&jni_env, &java_thread, java_vm, NULL, name, daemon);
-    if (status != JNI_OK) goto done;
+    if (status != JNI_OK) {
+        goto error;
+    }
 
-    // Attaches main thread to TM.
+    // attaches a main thread to TM
     {
         IDATA jtstatus = jthread_attach(jni_env, java_thread, daemon);
         if (jtstatus != TM_ERROR_NONE) {
             status = JNI_ERR;
-            goto done;
+            goto error;
         }
     }
     assert(jthread_self() != NULL);
@@ -530,7 +578,7 @@
 
     status = vm_init2(jni_env);
     if (status != JNI_OK) {
-        goto done;
+        goto error;
     }
 
     // Send VM start event. JNI services are available now.
@@ -540,34 +588,35 @@
     finalizer_threads_init(java_vm, jni_env);   /* added for NATIVE FINALIZER THREAD */
     ref_enqueue_thread_init(java_vm, jni_env);  /* added for NATIVE REFERENCE ENQUEUE THREAD */
 
-    // The VM is fully initialized now.
+    // the VM is fully initialized now
     vm_env->vm_state = Global_Env::VM_RUNNING;
 
-    // Send VM init event.
+    // send VM init event
     jvmti_send_vm_init_event(vm_env);
 
-    // Thread start event for the main thread should be sent after VMInit callback has finished.
+    // thread start event for the main thread should be sent after VMInit callback has finished
     if (jvmti_should_report_event(JVMTI_EVENT_THREAD_START)) {
         jvmti_send_thread_start_end_event(p_TLS_vmthread, 1);
     }
 
-    // Register created VM.
-    APR_RING_INSERT_TAIL(&GLOBAL_VMS, java_vm, JavaVM_Internal, link);
-
-    // Store Java heap memory size after initialization
-    vm_env->init_gc_used_memory = (size_t)gc_total_memory();
+    // store Java heap memory size after initialization
+    vm_env->init_gc_used_memory = (size_t) gc_total_memory();
 
-    // Store native memory size after initialization
+    // store native memory size after initialization
     vm_env->init_used_memory = port_vmem_used_size();
 
-    status  = JNI_OK;
-done:
-    apr_thread_mutex_unlock(GLOBAL_LOCK);
-
-    if (status != JNI_OK && NULL != vm_global_pool) {
-        apr_pool_destroy(vm_global_pool);
-    }
+    return JNI_OK;
 
+    /* in case of error, frees resources in reverse order, ignores other errors */
+error:
+instance_number_error:
+    cm->FreeInstance((OpenInstanceHandle) java_vm);
+instance_allocation_error:
+    CmReleaseComponent(OPEN_VM);
+component_allocation_error:
+    CmRelease();
+    // TODO make the logging initialization thread and multi-instance safe
+    shutdown_log_system();
     return status;
 }
 
@@ -1505,21 +1554,14 @@
     }    
     assert(java_thread != NULL);
 
-    apr_thread_mutex_lock(GLOBAL_LOCK);
-
-    // Remove current VM from the list of all VMs running in the current adress space.
-    APR_RING_REMOVE(java_vm, link);
-    
-    apr_thread_mutex_unlock(GLOBAL_LOCK);
-
     status = vm_destroy(java_vm, java_thread);
 
     // Destroy VM environment.
     java_vm->vm_env->~Global_Env();
     java_vm->vm_env = NULL;
     
-    // Destroy VM pool.
-    apr_pool_destroy(java_vm->pool);
+    CmReleaseComponent(OPEN_VM);
+    CmRelease();
 
 #ifdef _MEMMGR_REPORT
     report_leaked_malloc();

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/native/java_lang_EMThreadSupport.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/native/java_lang_EMThreadSupport.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/native/java_lang_EMThreadSupport.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/native/java_lang_EMThreadSupport.cpp Mon Apr  7 07:47:59 2008
@@ -38,7 +38,7 @@
     (JNIEnv *jenv, jclass cls) 
 {
     return (jboolean) strcmp("false", 
-        VM_Global_State::loader_env->em_instance->intf->
+        VM_Global_State::loader_env->em_component->
         GetProperty(OPEN_EM_VM_PROFILER_NEEDS_THREAD_SUPPORT));
 }
 
@@ -53,7 +53,7 @@
 JNIEXPORT jint JNICALL 
 Java_java_lang_EMThreadSupport_getTimeout(JNIEnv *jenv, jclass cls) 
 {
-    const char* timeout_string = VM_Global_State::loader_env->em_instance->intf->
+    const char* timeout_string = VM_Global_State::loader_env->em_component->
         GetProperty(OPEN_EM_VM_PROFILER_THREAD_TIMEOUT);
     return atoi(timeout_string);
 }

Modified: harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.cpp
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.cpp?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.cpp (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.cpp Mon Apr  7 07:47:59 2008
@@ -27,27 +27,13 @@
 #include "component_manager_impl.h"
 
 #define LOG_DOMAIN "compmgr"
-#include "cxxlog.h"
+#include "clog.h"
 
 /*
  * Private variables and functions. See public fuctions
  * at the end of file.
  */
 
-#ifndef NDEBUG
-/**
- * Instance sanity check.
- */
-static int
-is_instance_valid(OpenInstanceHandle instance) {
-    return apr_isdigit(*(instance->intf->GetVersion()));
-}
-#endif
-
-#define ASSERT_IS_INSTANCE_VALID(instance) ASSERT(is_instance_valid(instance), \
-    "Instance is not properly initialized, " \
-    "the first strucutre element should point to a default interface table");
-
 /**
  * Lock which synchronizes all component manager operations.
  *
@@ -76,11 +62,47 @@
 #define MAX_ERROR_BUFFER_SIZE 1024
 
 /**
+ * Get an instance information structure for a given instance pointer.
+ * @param[out] pp_instance_info_next on return, points to a <code>next</code>
+ * slot which points to the corresponding instance info. The slot is used
+ * when the instance is to be removed from the list.
+ * @param[out] p_component_info on return, points to a handle of
+ * a component information structure
+ * @param instance the instance handle
+ * @return APR_SUCCESS if successful, APR_NOTFOUND if the instance was not
+ * found, or another non-zero error code
+ */
+static int
+GetInstanceInfoSlot(_InstanceInfo*** pp_instance_info,
+                ComponentInfoHandle* p_component_info,
+                OpenInstanceHandle instance) {
+    _ComponentInfo* component_info = component_manager_impl->components;
+    while (component_info) {
+        _InstanceInfo** p_instance_info = &component_info->instances;
+        _InstanceInfo* instance_info = component_info->instances;
+        while (instance_info) {
+            if (instance == instance_info->instance) {
+                /* found corresponding information structures */
+                *p_component_info = component_info;
+                *pp_instance_info = p_instance_info;
+                return APR_SUCCESS;
+            }
+            p_instance_info = &instance_info->next;
+            instance_info = instance_info->next;
+        }
+        component_info = component_info->next;
+    }
+    return APR_NOTFOUND;
+}
+
+/**
  * Get a component information structure for a component
  * with a given name.
  * @param[out] p_component_info on return, points to a handle of
  * a component information structure
  * @param name component name
+ * @return APR_SUCCESS if successful, APR_NOTFOUND if a component with
+ * the given name cannot be found, or another non-zero error code
  */
 static int
 GetComponentInfo(ComponentInfoHandle* p_component_info,
@@ -88,7 +110,7 @@
     ComponentInfoHandle component_info = component_manager_impl->components;
     while (component_info) {
         if (!strcmp(name, component_info->component->GetName())) {
-            /* Found a component information structure */
+            /* found a component information structure */
             *p_component_info = component_info;
             return APR_SUCCESS;
         }
@@ -97,6 +119,24 @@
     return APR_NOTFOUND;
 }
 
+static int
+GetComponentInfoSlot(_ComponentInfo*** pp_component_info,
+                     const char* name) {
+    _ComponentInfo** p_component_info =
+        &component_manager_impl->components;
+    _ComponentInfo* component_info = component_manager_impl->components;
+    while (component_info) {
+        if (!strcmp(component_info->component->GetName(), name)) {
+            *pp_component_info = p_component_info;
+            return APR_SUCCESS;
+        }
+        p_component_info = &(component_info->next);
+        component_info = component_info->next;
+    }
+    return APR_NOTFOUND;
+}
+
+
 /**
  * Checks if DLL is still used by registered components.
  * @param lib DLL handle
@@ -117,6 +157,24 @@
     return APR_NOTFOUND;
 }
 
+#ifndef NDEBUG
+/**
+ * Instance sanity check.
+ */
+static int
+is_instance_valid(OpenInstanceHandle instance) {
+    ComponentInfoHandle component_info;
+    _InstanceInfo** p_instance_info;
+    VERIFY_SUCCESS(GetInstanceInfoSlot(&p_instance_info, &component_info, instance));
+    return apr_isdigit(*(component_info->component->GetVersion()));
+}
+#endif
+
+#define ASSERT_IS_INSTANCE_VALID(instance) ASSERT(is_instance_valid(instance), \
+    ("Instance is not properly initialized, " \
+    "the first strucutre element should point to a default interface table"));
+
+
 /**
  * Allocate an instance info structure and add it
  * to a component manager list.
@@ -125,8 +183,7 @@
 AddInstance(OpenInstanceHandle instance,
             ComponentInfoHandle component_info,
             apr_pool_t* pool) {
-    TRACE("Cm.AddInstance()"); 
-    ASSERT_IS_INSTANCE_VALID(instance);
+    TRACE(("Cm.AddInstance()")); 
 
     _InstanceInfo* instance_info =
         (_InstanceInfo*) apr_palloc(pool, sizeof(_InstanceInfo));
@@ -141,6 +198,8 @@
 
     instance_info->next = component_info->instances;
     ((struct _ComponentInfo*) component_info)->instances = instance_info;
+
+    ASSERT_IS_INSTANCE_VALID(instance);
     return APR_SUCCESS;
 }
 
@@ -156,52 +215,17 @@
 static int
 RemoveInstanceInfo(InstanceInfoHandle* p_instance_info,
                    OpenInstanceHandle instance) {
-
+    _InstanceInfo** p_instance_info_next;
     ComponentInfoHandle component_info;
-    GetComponentInfo(&component_info, instance->intf->GetName());
 
-    _InstanceInfo** p_instance_info_next =
-        &(((struct _ComponentInfo*) component_info)->instances);
-    _InstanceInfo* instance_info = component_info->instances;
-    while (instance_info) {
-        if (instance_info->instance == instance) {
-            *p_instance_info_next = instance_info->next;
-            *p_instance_info = instance_info;
-            return APR_SUCCESS;
-        }
-        p_instance_info_next = &(instance_info->next);
-        instance_info = instance_info->next;
+    int ret = GetInstanceInfoSlot(&p_instance_info_next, &component_info, instance);
+    if (APR_SUCCESS != ret) {
+        return ret;
     }
-    return APR_NOTFOUND;
-}
 
-/**
- * Unregister a component and return a pointer to a correspondent
- * component information structure.
- * @param[out] p_component_info on return, points to
- * <code>ComponentInfoHandle</code> handle which correspond to the
- * unregisterd component
- * @param name a component name to be unregistered
- * @return APR_SUCCESS if successful, or APR_NOTFOUND if the component
- * cannot be found
- */
-static int
-RemoveComponentInfo(ComponentInfoHandle* p_component_info,
-                     const char* name) {
-    _ComponentInfo** p_component_info_next =
-        &component_manager_impl->components;
-    _ComponentInfo* component_info = component_manager_impl->components;
-    while (component_info) {
-        if (!strcmp(component_info->component->GetName(),
-            name)) {
-            *p_component_info_next = component_info->next;
-            *p_component_info = component_info;
-            return APR_SUCCESS;
-        }
-        p_component_info_next = &(component_info->next);
-        component_info = component_info->next;
-    }
-    return APR_NOTFOUND;
+    *p_instance_info = *p_instance_info_next;
+    *p_instance_info_next = (*p_instance_info)->next;
+    return APR_SUCCESS;
 }
 
 /**
@@ -320,19 +344,58 @@
 }
 
 static int
+DumpComponent(OpenComponentHandle component) {
+    TRACE(("%s-%s (Vendor: %s)\nDescription:\t\nInterfaces:\t",
+        component->GetName(), component->GetVersion(),
+        component->GetVendor(), component->GetDescription()));
+
+    const char** p_name = component->ListInterfaceNames();
+    /*
+     * String array is NULL terminated. We increase a string pointer until
+     * it becomes NULL.
+     */
+    for(; *p_name; p_name++) {
+        TRACE((" %s", *p_name));
+    }
+    return APR_SUCCESS;
+}
+
+/**
+ * Decreases a component reference count. If the count becomes zero,
+ * unregisters a component and frees all associated instances.
+ * @param name a component name to be unregistered
+ * @return APR_SUCCESS if successful, or APR_NOTFOUND if the component
+ * cannot be found
+ */
+static int
 RemoveAndFreeComponentInfo(const char* component_name) {
-    ComponentInfoHandle component_info;
-    int ret = RemoveComponentInfo(&component_info, component_name);
+    TRACE(("static RemoveAndFreeComponentInfo()"));
+
+    _ComponentInfo** p_component_info;
+    int ret = GetComponentInfoSlot(&p_component_info, component_name);
     if (APR_SUCCESS != ret) {
         return ret;
     }
 
+    _ComponentInfo* component_info = *p_component_info;
+    DumpComponent(component_info->component);
+
+    assert(component_info->num_clients > 0);
+    TRACE(("Removing one of %d component subscribers", component_info->num_clients));
+
+    if (--component_info->num_clients > 0) {
+        /* there are still clients for this component, stop here */
+        return APR_SUCCESS;
+    }
+
+    /* since there are no more cllients, remove the component */
+    *p_component_info = component_info->next;
     return FreeComponentInfo(component_info);
 }
 
 static int
 InitializeGlobalLock() {
-   TRACE("Cm.InitializeGlobalLock()");
+   TRACE(("Cm.InitializeGlobalLock()"));
     apr_pool_t* pool;
     int ret = apr_pool_create(&pool, NULL);
     if (APR_SUCCESS != ret) {
@@ -377,7 +440,7 @@
  */
 static int
 Destroy() {
-    TRACE("Cm.Destroy()");
+    TRACE(("Cm.Destroy()"));
     int ret = APR_SUCCESS, ret_new;
 
     /* Deallocate all components */
@@ -405,7 +468,7 @@
 AddComponent(OpenComponentInitializer init_func,
              DllHandle lib,
              apr_pool_t* parent_pool) {
-    TRACE("Cm.AddComponent()");
+    TRACE(("static AddComponent()"));
 
     apr_pool_t* pool;
     int ret = apr_pool_create(&pool, parent_pool);
@@ -413,49 +476,47 @@
         return ret;
     }
 
-    _ComponentInfo* component_info = (_ComponentInfo*)
+    _ComponentInfo* new_component_info = (_ComponentInfo*)
         apr_pcalloc(pool, sizeof(_ComponentInfo));
-
-    if (NULL == component_info) {
-        /* Out of memory */
-        apr_pool_destroy(pool);
-        return APR_ENOMEM;
+    if (NULL == new_component_info) {
+        /* out of memory */
+        ret = APR_ENOMEM;
+        goto error;
     }
 
     OpenComponentHandle component;
-    ret = init_func(&component,
-        &(component_info->instance_allocator),
+    ret = init_func(&component, &(new_component_info->instance_allocator),
         pool);
     if (APR_SUCCESS != ret) {
-        apr_pool_destroy(pool);
-        return ret;
+        goto error;
     }
-    component_info->component = component;
-    component_info->pool = pool;
-    component_info->declaring_library = lib;
-
-    TRACE("Cm.AddComponent():\t" << component->GetName()
-        << "-" << component->GetVersion()
-        << " (Vendor: "
-        << component->GetVendor()
-        << ")\n"
-        << "Description:\t"
-        << component->GetDescription() << "\n"
-        << "Interfaces:\t");
+    new_component_info->component = component;
+    new_component_info->pool = pool;
+    new_component_info->declaring_library = lib;
 
-    const char** p_name = component->ListInterfaceNames();
-    /*
-     * String array is NULL terminated. We increase a string pointer until
-     * it becomes NULL.
-     */
-    for(; *p_name; p_name++) {
-        TRACE(" " << *p_name);
-    }
+    DumpComponent(component);
+
+    /* check that the component does not exist */
+    ComponentInfoHandle component_info;
+    ret = GetComponentInfo(&component_info, component->GetName());
 
-    /* Add to the global component manager list */
-    component_info->next = component_manager_impl->components;
-    component_manager_impl->components = component_info;
+    if (APR_SUCCESS == ret) {
+        /* the component with the same name is already registered */
+        apr_pool_destroy(pool);
+        ((struct _ComponentInfo*) component_info)->num_clients++;
+    } else if (APR_NOTFOUND == ret) {
+        /* add a new component to the global component manager list */
+        new_component_info->next = component_manager_impl->components;
+        component_manager_impl->components = new_component_info;
+        new_component_info->num_clients = 1;
+    } else {
+        goto error;
+    }
     return APR_SUCCESS;
+
+error:
+    apr_pool_destroy(pool);
+    return ret;
 }
 
 static int
@@ -489,14 +550,14 @@
  */
 static int
 LoadLib(DllHandle* p_lib, const char* path) {
-    TRACE("Cm.LoadLibrary(\"" << path << "\")");
+    TRACE(("Cm.LoadLibrary(\"%s\")", path));
     int ret = FindLibrary(p_lib, path);
     if (APR_SUCCESS == ret) {
         return APR_SUCCESS;
     }
 
     ASSERT(APR_NOTFOUND == ret, \
-        "Unexpected return code from FindLibrary()");
+        ("Unexpected return code from FindLibrary()"));
 
     apr_pool_t* pool;
     ret = apr_pool_create(&pool, component_manager_impl->pool);
@@ -525,8 +586,7 @@
         char buffer[MAX_ERROR_BUFFER_SIZE];
         apr_dso_error(lib->descriptor,
                 buffer, MAX_ERROR_BUFFER_SIZE);
-        TRACE("Error loading " << path << ": " \
-            << buffer);
+        TRACE(("Error loading %s: %s", path, buffer));
         apr_pool_destroy(pool);
         return ret;
     }
@@ -542,74 +602,111 @@
  * by means of a component manager virtual table.
  */
 static int
-CmGetComponent(OpenComponentHandle* p_component,
-               const char* name) {
-    int ret = apr_thread_rwlock_rdlock(global_lock);
-    if (APR_SUCCESS != ret) {
-        return ret;
-    }
+GetComponent(OpenComponentHandle* p_component,
+             const char* name) {
+    VERIFY_SUCCESS(apr_thread_rwlock_rdlock(global_lock));
 
     ComponentInfoHandle component_info;
-    ret = GetComponentInfo(&component_info, name);
-    if (APR_SUCCESS != ret) {
-        apr_thread_rwlock_unlock(global_lock);
-        return ret;
+    int ret = GetComponentInfo(&component_info, name);
+    if (APR_SUCCESS == ret) {
+        *p_component = component_info->component;
     }
-    *p_component = component_info->component;
-    return apr_thread_rwlock_unlock(global_lock);
+
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    return ret;
 }
 
+static int
+GetComponentByInstance(OpenComponentHandle* p_component,
+                       OpenInstanceHandle instance) {
+    VERIFY_SUCCESS(apr_thread_rwlock_rdlock(global_lock));
+
+    ComponentInfoHandle component_info;
+    _InstanceInfo** p_instance_info;
+
+    int ret = GetInstanceInfoSlot(&p_instance_info, &component_info, instance);
+    if (APR_SUCCESS == ret) {
+        *p_component = component_info->component;
+    }
+
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    return ret;
+}
 
 static int
-CmCreateInstance(OpenInstanceHandle* p_instance,
+CreateInstance(OpenInstanceHandle* p_instance,
                     const char* name) {
-    int ret = apr_thread_rwlock_wrlock(global_lock);
-    if (APR_SUCCESS != ret) {
-        return ret;
-    }
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
 
     ComponentInfoHandle component_info;
-    ret = GetComponentInfo(&component_info, name);
+    int ret = GetComponentInfo(&component_info, name);
     if (APR_SUCCESS != ret) {
-        apr_thread_rwlock_unlock(global_lock);
-        return ret;
+        goto unlock;
     }
 
     apr_pool_t* pool;
     ret = apr_pool_create(&pool, component_info->pool);
-    if (APR_SUCCESS != ret) {
-        apr_thread_rwlock_unlock(global_lock);
-        return ret;
+    if (APR_SUCCESS != ret)  {
+        goto unlock;
     }
+
     ret = component_info->instance_allocator->CreateInstance(p_instance, pool);
-    if (APR_SUCCESS != ret) {
+    if (APR_SUCCESS != ret)  {
         apr_pool_destroy(pool);
-        apr_thread_rwlock_unlock(global_lock);
-        return ret;
+        goto unlock;
     }
 
     ret = AddInstance(*p_instance, component_info, pool);
-    if (APR_SUCCESS != ret) {
-        component_info->instance_allocator->FreeInstance(*p_instance); /* Ignore errors */
+    if (APR_SUCCESS != ret)  {
+         /* ignore errors */
+        component_info->instance_allocator->FreeInstance(*p_instance);
         apr_pool_destroy(pool);
-        apr_thread_rwlock_unlock(global_lock);
-        return ret;
     }
-    return apr_thread_rwlock_unlock(global_lock);
+
+unlock:
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    return ret;
 }
 
 
 static int
-CmFreeInstance(OpenInstanceHandle instance) {
-    int ret = apr_thread_rwlock_wrlock(global_lock);
-    if (APR_SUCCESS != ret) {
-        return ret;
-    }
+FreeInstance(OpenInstanceHandle instance) {
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
+
+    int ret = RemoveAndFreeInstanceInfo(instance);
+
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    return ret;
+}
+
+static int
+GetInstances(OpenInstanceHandle* p_instance, int buf_len, int* len,
+             const char* name) {
+    VERIFY_SUCCESS(apr_thread_rwlock_rdlock(global_lock));
+
+    ComponentInfoHandle component_info;
+    int ret = GetComponentInfo(&component_info, name);
+
+    if (APR_SUCCESS == ret) {
+        *len = 0;
+        const OpenInstanceHandle* p_buf_end = p_instance + buf_len;
+        _InstanceInfo* instance_info = component_info->instances;
+
+        while (instance_info && p_instance < p_buf_end) {
+            *p_instance = instance_info->instance;
+            p_instance++;
+            (*len)++;
+            instance_info = instance_info->next;
+        }
 
-    ret = RemoveAndFreeInstanceInfo(instance);
+        while (instance_info) {
+            (*len)++;
+            instance_info = instance_info->next;
+        }
+    }
 
-    int unlock_ret = apr_thread_rwlock_unlock(global_lock);
-    return (APR_SUCCESS == ret) ? unlock_ret : ret;
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    return ret;
 }
 
 /**
@@ -617,7 +714,7 @@
  */
 static int
 Create() {
-    TRACE("Cm.Create()");
+    TRACE(("Cm.Create()"));
 
     apr_pool_t* pool;
     int ret = apr_pool_create(&pool, global_pool);
@@ -634,9 +731,11 @@
     }
 
     component_manager_impl->num_clients = 1;
-    component_manager_impl->cm.GetComponent = CmGetComponent;
-    component_manager_impl->cm.CreateInstance = CmCreateInstance;
-    component_manager_impl->cm.FreeInstance = CmFreeInstance;
+    component_manager_impl->cm.GetComponent = GetComponent;
+    component_manager_impl->cm.GetComponentByInstance = GetComponentByInstance;
+    component_manager_impl->cm.CreateInstance = CreateInstance;
+    component_manager_impl->cm.FreeInstance = FreeInstance;
+    component_manager_impl->cm.GetInstances = GetInstances;
     component_manager_impl->pool = pool;
 
     return APR_SUCCESS;
@@ -648,27 +747,28 @@
  */
 int
 CmAcquire(OpenComponentManagerHandle* p_cm) {
-    TRACE("Cm.Acquire()");
+    TRACE(("Cm.Acquire()"));
     InitializeGlobalLock();
 
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
     if (NULL == component_manager_impl) {
         int ret = Create();
         if (APR_SUCCESS != ret) {
-            PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+            VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
             return ret;
         }
     } else {
         component_manager_impl->num_clients++;
     }
+    TRACE(("Cm.Acquire(): component_manager_impl->num_clients = %d", component_manager_impl->num_clients));
     *p_cm = (OpenComponentManagerHandle) component_manager_impl;
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
     return APR_SUCCESS;
 }
 
 int
 CmRelease() {
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
     component_manager_impl->num_clients--;
 
     int ret = APR_SUCCESS;
@@ -676,63 +776,61 @@
         ret = Destroy();
     }
     
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
     return ret;
 }
 
 int
 CmAddComponent(OpenComponentInitializer init_func) {
-    TRACE("Cm.AddComponent()");
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
+    TRACE(("Cm.AddComponent()"));
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
 
     int ret = AddComponent(init_func, NULL, component_manager_impl->pool);
 
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
     return ret;
 }
 
 int
 CmLoadComponent(const char* path,
                 const char* init_func_name) {
-    TRACE("Cm.LoadComponent(\"" << path
-        << "\", " << init_func_name << "())");
+    TRACE(("Cm.LoadComponent(\"%s\", %s())", path, init_func_name));
 
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
 
     DllHandle lib;
     int ret = LoadLib(&lib, path);
     if (APR_SUCCESS != ret) {
-        PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
-        printf("failed to load: %d %s\n", ret, path);
-        return ret;
+        TRACE(("failed to load: %d %s\n", ret, path));
+        goto load_error;
     }
     
     OpenComponentInitializer init_func;
     ret = GetOpenComponentInitializer(&init_func, lib, init_func_name);
     if (APR_SUCCESS != ret) {
-        /* Ignore error */
-        ReleaseLib(lib);
-        PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
-        printf("failed to init: %d\n", ret);
-        return ret;
+        TRACE(("failed to init: %d\n", ret));
+        goto init_error;
     }
 
     ret = AddComponent(init_func, lib, lib->pool);
-    if (APR_SUCCESS != ret) {
-        /* Ignore error */
-        ReleaseLib(lib);
-        PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    if (APR_SUCCESS == ret) {
+        VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+        return APR_SUCCESS;
     }
 
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
-    return APR_SUCCESS;
+    /* ignore errors */
+init_error:
+    ReleaseLib(lib);
+load_error:
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    return ret;
 }
 
 int
-CmFreeComponent(const char* component_name) {
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
+CmReleaseComponent(const char* component_name) {
+    VERIFY_SUCCESS(apr_thread_rwlock_wrlock(global_lock));
     int ret = RemoveAndFreeComponentInfo(component_name);
-    PORT_VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
+    VERIFY_SUCCESS(apr_thread_rwlock_unlock(global_lock));
     return ret;
 }
 

Modified: harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.h?rev=645545&r1=645544&r2=645545&view=diff
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.h (original)
+++ harmony/enhanced/drlvm/trunk/vm/vmstart/src/compmgr/component_manager_impl.h Mon Apr  7 07:47:59 2008
@@ -51,6 +51,10 @@
      * The list of instances of the component.
      */
     _InstanceInfo* instances;
+    /**
+     * Reference counter.
+     */
+    int num_clients;
     apr_pool_t* pool;
 };
 typedef const struct _ComponentInfo* ComponentInfoHandle;



Mime
View raw message