Author: mturk
Date: Wed Apr 15 18:36:01 2009
New Revision: 765296

URL: http://svn.apache.org/viewvc?rev=765296&view=rev
Log:
Add Java Class cache

Added:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h   (with 
props)
    commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
    commons/sandbox/runtime/trunk/src/main/native/shared/tables.c
    commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
    
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Wed Apr 15 
18:36:01 2009
@@ -65,6 +65,7 @@
 
 COMMON_OBJS=\
        $(SRCDIR)/shared/buildmark.$(OBJ) \
+       $(SRCDIR)/shared/clazz.$(OBJ) \
        $(SRCDIR)/shared/dbb.$(OBJ) \
        $(SRCDIR)/shared/error.$(OBJ) \
        $(SRCDIR)/shared/memory.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Wed Apr 15 
18:36:01 2009
@@ -59,6 +59,7 @@
 
 COMMON_OBJS=\
        $(SRCDIR)/shared/buildmark.$(OBJ) \
+       $(SRCDIR)/shared/clazz.$(OBJ) \
        $(SRCDIR)/shared/dbb.$(OBJ) \
        $(SRCDIR)/shared/error.$(OBJ) \
        $(SRCDIR)/shared/memory.$(OBJ) \

Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h?rev=765296&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h Wed Apr 
15 18:36:01 2009
@@ -0,0 +1,52 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACR_CLAZZ_H
+#define _ACR_CLAZZ_H
+
+#include "acr.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file acr_clazz.h
+ * @brief
+ *
+ * ACR Java Class functions
+ *
+ */
+
+/**
+ * Add class to the local cache and create global
+ * rference to it.
+ * @param env Current JNI environment.
+ * @param lock Java Object use for synchronisation.
+ * @param name JNI class name.
+ * @remark Class name must follow the JNI convention of class naming
+ * prefixing the class with 'L' and terminating it with ';'
+ * eg. "Ljava.lang.String;"
+ */
+ACR_DECLARE(jclass) ACR_AddClassToGlobalCache(JNIEnv *env,
+                                              jobject lock, const char *name);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_CLAZZ_H */

Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h Wed Apr 
15 18:36:01 2009
@@ -178,11 +178,15 @@
                               void **data, acr_size_t *dlen);
 
 /**
- * Get the internal table array
- * @param tbl Table from which to get the array.
- * @remark Returned array should be used only for traversing the data.
+ * Get the n'th data from the table
+ * @param t Table to use.
+ * @param idx Index to use (0 ... table size)
+ * @param data Pointer where to store the data. Can be NULL
+ * @param dlen Pointer where to store the data length. Can be NULL
+ * @return ACR_EOF on end.
  */
-ACR_DECLARE(const acr_array_t *) ACR_TableArray(acr_table_t *tbl);
+ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx,
+                                void **data, acr_size_t *dlen);
 
 #ifdef __cplusplus
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c Wed Apr 15 
18:36:01 2009
@@ -16,6 +16,7 @@
 
 #include "acr.h"
 #include "acr_private.h"
+#include "acr_error.h"
 #include "acr_vm.h"
 
 #include <pthread.h>
@@ -28,6 +29,10 @@
 static JavaVM       *acr_pvm = NULL;
 static pthread_key_t acr_thread_key;
 
+extern int ACR_InitClazzCache(JNIEnv *);
+extern int ACR_DestroyClazzCache(JNIEnv *);
+
+
 typedef struct acr_thread_local_t {
     JNIEnv  *env;
     int     attached;
@@ -63,8 +68,10 @@
     }
     if ((env = ACR_GetJNIEnv()) == NULL)
         return JNI_ERR;
-    else
-        return JNI_VERSION_1_4;
+    if (ACR_InitClazzCache(env) != ACR_SUCCESS)
+        return JNI_ERR;
+
+    return JNI_VERSION_1_4;
 }
 
 ACR_DECLARE(JNIEnv *)ACR_GetJNIEnv()

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c Wed Apr 15 
18:36:01 2009
@@ -16,6 +16,8 @@
 
 #include "acr.h"
 #include "acr_private.h"
+#include "acr_error.h"
+#include "acr_vm.h"
 
 #define ACR_WANT_LATE_DLL
 #include "acr_arch.h"
@@ -36,6 +38,9 @@
 LPOSVERSIONINFOEXA      acr_osver = &osver;
 static JavaVM          *acr_pvm   = NULL;
 
+extern int ACR_InitClazzCache(JNIEnv *);
+extern int ACR_DestroyClazzCache(JNIEnv *);
+
 typedef struct acr_thread_local_t {
     JNIEnv  *env;
     int     attached;
@@ -308,7 +313,10 @@
     }
 
     env = epp;
-    return  JNI_VERSION_1_4;
+    if (ACR_InitClazzCache(env) != ACR_SUCCESS)
+        return JNI_ERR;
+
+    return JNI_VERSION_1_4;
 }
 
 ACR_DECLARE(JNIEnv *)ACR_GetJNIEnv()

Added: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=765296&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Wed Apr 15 
18:36:01 2009
@@ -0,0 +1,92 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_arch.h"
+#include "acr_clazz.h"
+#include "acr_private.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_vm.h"
+#include "acr_tables.h"
+#include "acr_types.h"
+
+#define CLAZZ_INI_SIZE  128
+static acr_table_t *clazz_cache;
+
+int ACR_InitClazzCache(JNIEnv *_E)
+{
+
+    if (clazz_cache)
+        return ACR_SUCCESS;
+    clazz_cache = ACR_TableMake(_E, THROW_FMARK, CLAZZ_INI_SIZE);
+    if (!clazz_cache)
+        return ACR_EISNULL;
+
+    return ACR_SUCCESS;    
+}
+
+int ACR_DestroyClazzCache(JNIEnv *_E)
+{
+    int i;
+    if (!clazz_cache)
+        return ACR_SUCCESS;
+    for (i = 0; ; i++) {
+        void *c;
+        if (ACR_TableIndex(clazz_cache, i, &c, NULL) == ACR_SUCCESS) {
+            (*_E)->DeleteGlobalRef(_E, (jobject)c);
+        }
+        else
+            break;
+    }
+    ACR_TableFree(_E, THROW_FMARK, clazz_cache);
+    clazz_cache = NULL;
+
+    return ACR_SUCCESS;    
+}
+
+ACR_DECLARE(jclass) ACR_AddClassToGlobalCache(JNIEnv *_E, 
+                                              jobject m, const char *name)
+{
+    jobject o;
+    jclass  c = NULL;
+
+    o = (jobject)(*_E)->FindClass(_E, name);
+    if (o != NULL) {
+        c = (jclass)(*_E)->NewGlobalRef(_E, o);
+        (*_E)->DeleteLocalRef(_E, o);
+        if (m)
+            (*_E)->MonitorEnter(_E, m);    
+        ACR_TableAdd(_E, THROW_FMARK, clazz_cache, name, c, 0);
+        if (m)
+            (*_E)->MonitorExit(_E, m);    
+    }
+
+    return c;
+}
+
+int acr_clazz_cache_size()
+{
+    int i;
+    if (!clazz_cache)
+        return 0;
+    for (i = 0; ; i++) {
+        if (ACR_TableIndex(clazz_cache, i, NULL, NULL) != ACR_SUCCESS)
+            break;
+    }
+    return i;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/tables.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/tables.c?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/tables.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/tables.c Wed Apr 15 
18:36:01 2009
@@ -263,10 +263,18 @@
     }
 }
 
-ACR_DECLARE(const acr_array_t *) ACR_TableArray(acr_table_t *tbl)
+ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx,
+                                void **data, acr_size_t *dlen)
 {
-    if (tbl)
-        return &(tbl->a);
-    else
-        return NULL;
+    table_entry_t *e = NULL;
+
+    if (idx >= t->a.nelts) {
+        return ACR_EOF;
+    }
+    e = (table_entry_t *)t->a.elts;
+    if (data)
+        *data = e[idx].data;
+    if (dlen)
+        *dlen = e[idx].dlen;
+    return ACR_SUCCESS;
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Wed Apr 15 
18:36:01 2009
@@ -22,6 +22,7 @@
 #include "acr_memory.h"
 #include "acr_tables.h"
 #include "acr_vm.h"
+#include "acr_clazz.h"
 
 
 /**
@@ -120,10 +121,16 @@
             rc = 1;
     }
     if (rc) {
-        const acr_array_t *a;
         ACR_TableAdd(_E, THROW_FMARK, t, J2S(s), J2S(s), 2);
-        a = ACR_TableArray(t);
-        if (a->nelts != 2)
+    }
+    if (rc) {
+        int i;
+        for (i = 0;;i++) {
+            int r = ACR_TableIndex(t, i, &d, &l);
+            if (r != ACR_SUCCESS)
+                break;
+        }
+        if (l != 2)
             rc = 0;
     }
     ACR_TableFree(_E, THROW_FMARK, t);
@@ -150,3 +157,36 @@
     ACR_Free(_E, THROW_FMARK, str);
     return l;
 }
+
+ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test011)(ACR_JNISTDARGS, jstring s)
+{
+    jclass c;
+    CSTR_DECLARE(s);
+
+    c = ACR_AddClassToGlobalCache(_E, _O, J2S(s));
+
+    CSTR_RELEASE(s);
+    if (c != NULL)
+        return 1;
+    else
+        return 0;
+}
+
+extern int acr_clazz_cache_size(void);
+ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test012)(ACR_JNISTDARGS, jint d)
+{
+    return  acr_clazz_cache_size();
+}
+
+extern int ACR_DestroyClazzCache(JNIEnv *);
+
+ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test013)(ACR_JNISTDARGS, jint d)
+{
+    jint rc = 0;
+    
+    if (d) {
+        rc = ACR_DestroyClazzCache(_E);
+    }
+    return rc;
+}
+

Modified: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=765296&r1=765295&r2=765296&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
 Wed Apr 15 18:36:01 2009
@@ -50,6 +50,9 @@
     private static native int  test008(String msg);
     private static native int  test009(String msg);
     private static native int  test010(String msg);
+    private static native int  test011(String msg);
+    private static native int  test012(int d);
+    private static native int  test013(int d);
 
 
     protected void setUp()
@@ -171,6 +174,32 @@
         assertEquals("Length", 6, i);
     }
 
+    public void testCCache()
+        throws Exception
+    {
+        int i = test011("Ljava/lang/String;");
+        assertEquals("Value", 1, i);
+        int j = test011("[Ljava/lang/String;");
+        assertEquals("Value", 1, j);
+    }
+
+    public void testCCacheSize()
+        throws Exception
+    {
+        int i = test012(0);
+        int j = test011("[Ljava/lang/String;");
+        assertEquals("Value", 1, j);
+        int n = test012(0);
+        assertEquals("Size",  i + 1, n);
+    }
+
+    public void testCCacheDestroy()
+        throws Exception
+    {
+        // Shuold not be called as last
+        int i = test013(1);
+        assertEquals("Value", 0, i);
+    }
 
 
 }


Reply via email to