Author: mturk Date: Fri Apr 17 07:39:29 2009 New Revision: 765881 URL: http://svn.apache.org/viewvc?rev=765881&view=rev Log: Code update
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java commons/sandbox/runtime/trunk/src/main/native/configure commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h commons/sandbox/runtime/trunk/src/main/native/shared/error.c commons/sandbox/runtime/trunk/src/main/native/shared/pointer.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/java/org/apache/commons/runtime/Pointer.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java Fri Apr 17 07:39:29 2009 @@ -25,7 +25,7 @@ public abstract class Pointer { /* - * Pointer can be only created from native code. + * Pointer can only be created from the native code. * Suppress any instantiation except form internal classes. */ protected Pointer() @@ -36,9 +36,10 @@ private native void cleanup0() throws Throwable; - /** True if the Pointer is 64-bit. + /** + * Size in bytes of the storage needed to represent the Pointer. */ - public static final boolean IS_64_BIT = OS.getDataModel() == 64 ? true : false; + public static final int SIZEOF = OS.getDataModel() == 64 ? 8 : 4; /** Long value of the internal pointer * @return Internal pointer address casted to the {...@code long}. @@ -47,17 +48,22 @@ /** Integer value of the internal pointer * @return Internal pointer address casted to the {...@code int}. - * @throws ClassCastException if the running JVM is 64 bit. + * @throws ClassCastException if running on the 64-bit JVM. */ public abstract int intValue() throws ClassCastException; + /** Chack if the pointer is valid + * @return true if the internal pointer is not {...@code NULL}. + */ + public abstract boolean IsNull(); + /** * Called by the garbage collector when the object is destroyed. * The class will free internal resources allocated by the Operating system. * @see Object#finalize() * @throws Throwable the {...@code Exception} raised by this method. - */ + */ protected final void finalize() throws Throwable { @@ -73,11 +79,19 @@ * </p> * @see #finalize() * @throws Throwable the {...@code Exception} raised by this method. - */ - public void free() + */ + public final void free() throws Throwable { cleanup0(); } + /** + * Returns a string representation of the Pointer. + * The returned string is hexadecimal representation of the underlying + * pointer. + * @return a hexadecimal representation of the pointer. + */ + public abstract String toString(); + } Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java Fri Apr 17 07:39:29 2009 @@ -24,15 +24,23 @@ */ class Pointer32 extends Pointer { - private int POINTER; - private int CLEANUP; + private int POINTER; + private int CLEANUP; + /* + * Only created from JNI code. + */ private Pointer32(int ptr, int clr) { POINTER = ptr; CLEANUP = clr; } + public boolean IsNull() + { + return POINTER == 0; + } + public long longValue() { return POINTER; @@ -43,4 +51,11 @@ { return POINTER; } + + public String toString() + { + String h = Integer.toHexString(POINTER); + return "0x" + ("00000000" + h).substring(h.length()); + } + } Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java Fri Apr 17 07:39:29 2009 @@ -24,15 +24,23 @@ */ class Pointer64 extends Pointer { - private long POINTER; - private long CLEANUP; + private long POINTER; + private long CLEANUP; + /* + * Only created from JNI code. + */ private Pointer64(long ptr, long clr) { POINTER = ptr; CLEANUP = clr; } + public boolean IsNull() + { + return POINTER == 0L; + } + public long longValue() { return POINTER; @@ -43,4 +51,11 @@ { throw new ClassCastException(); } + + public String toString() + { + String h = Long.toHexString(POINTER); + return "0x" + ("0000000000000000" + h).substring(h.length()); + } + } Modified: commons/sandbox/runtime/trunk/src/main/native/configure URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/configure (original) +++ commons/sandbox/runtime/trunk/src/main/native/configure Fri Apr 17 07:39:29 2009 @@ -445,6 +445,7 @@ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> +#include <wctype.h> int main() {printf("%d", sizeof($1));return 0;} EOF @@ -458,6 +459,18 @@ echo $rc } +sizeof_int="`test_csizeof int`" +if [ ".$sizeof_int" = ".0" ]; then + echo "[ERROR] Cannot determine the size of int." 1>&2 + exit 1 +fi + +sizeof_wchar="`test_csizeof wchar_t`" +if [ ".$sizeof_wchar" = ".0" ]; then + echo "[ERROR] Cannot determine the size of wchar_t." 1>&2 + exit 1 +fi + # Generate configuration header file # cat > $topdir/include/ccconfig.h << EOF @@ -500,12 +513,13 @@ #define HAS_BIG_ENDIAN $bige -#define CC_SIZEOF_INT `test_csizeof int` +#define CC_SIZEOF_INT $sizeof_int #define CC_SIZEOF_LONG `test_csizeof long` #define CC_SIZEOF_LONG_LONG `test_csizeof 'long long'` #define CC_SIZEOF_VOIDP `test_csizeof 'void *'` #define CC_SIZEOF_SIZE_T `test_csizeof size_t` #define CC_SIZEOF_OFF64_T `test_csizeof off64_t` +#define CC_SIZEOF_WCHAR_T $sizeof_wchar EOF Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h Fri Apr 17 07:39:29 2009 @@ -35,6 +35,20 @@ #define THROW_FMARK __FILE__, __LINE__ #define THROW_NMARK NULL, 0 +/* Exception class enums */ +typedef enum { + ACR_EX_EGENERAL = 0, /* java/lang/Exception */ + ACR_EX_ERUNTIME, /* java/lang/RuntimeException */ + ACR_EX_ENOMEM, /* java/lang/OutOfMemoryError */ + ACR_EX_ENULL, /* java/lang/NullPointerException */ + ACR_EX_EINVAL, /* java/lang/IllegalArgumentException */ + ACR_EX_EINDEX, /* java/lang/IndexOutOfBoundsException */ + ACR_EX_ECCAST, /* java/lang/ClassCastException */ + ACR_EX_EIO, /* java/io/IOException */ + ACR_EX_ESYNC, /* java/io/SyncFailedException */ + ACR_EX_ESOCK /* java/net/SocketException */ +} acr_trowclass_e; + /* * Convenience function to help throw any class * @param env JNI Environment Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h Fri Apr 17 07:39:29 2009 @@ -76,21 +76,39 @@ #define ZSTR_TO_JSTRING(V, L) (*_E)->NewString(_E, (const jchar *)(V), (L)) #define CSTR_DECLARE(V) \ - const char *_c##V = V ? (const char *)((*_E)->GetStringUTFChars(_E, V, 0)) : NULL + const char *_c##V = V ? (const char *)((*_E)->GetStringUTFChars(_E, V, NULL)) : NULL #define CSTR_RELEASE(V) \ if (_c##V) (*_E)->ReleaseStringUTFChars(_E, V, _c##V) #define WSTR_DECLARE(V) \ jsize _wl##V = V ? (*_E)->GetStringLength(_E, V) : 0; \ - const jchar *_ws##V = V ? (const jchar *)((*_E)->GetStringChars(_E, V, 0)) : NULL; \ + const jchar *_ws##V = V ? (const jchar *)((*_E)->GetStringChars(_E, V, NULL)) : NULL; \ wchar_t *_w##V = NULL -#define WPTR_DECLARE(V) \ - _w##V = (wchar_t *)malloc((_wl##V + 1) * sizeof(wchar_t)); \ - if (_wl##V) wcsncpy(_w##V, (const wchar_t *)_ws##V, _wl##V); \ +#if CC_SIZEOF_WCHAR_T == 2 +/* wchat_t matches jchar */ +#define WPTR_DECLARE(V) \ + _w##V = (wchar_t *)malloc((_wl##V + 1) * sizeof(wchar_t)); \ + if (_wl##V) memcpy(_w##V, _ws##V, _wl##V * sizeof(wchar_t)); \ _w##V[_wl##V] = 0 +#elif CC_SIZEOF_WCHAR_T == 4 +/* Presume utf16 matches utf32. + * TODO: Figure out more optimised way of doing this. + */ +#define WPTR_DECLARE(V) \ + _w##V = (wchar_t *)malloc((_wl##V + 1) * sizeof(wchar_t)); \ + if (_wl##V) { \ + jsize _wi##V; \ + for (_wi##V = 0; _wi##V < _wl##V; _wi##V ++) \ + _w##V[_wi##V] = _ws##V[_wi##V]; \ + } _w##V[_wl##V] = 0 + +#else +#error "Unsupported wchat_t size" +#endif + #define WSTR_RELEASE(V) \ if (_ws##V) (*_E)->ReleaseStringChars(_E, V, _ws##V); \ if (_w##V) free (_w##V) 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=765881&r1=765880&r2=765881&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 Fri Apr 17 07:39:29 2009 @@ -144,11 +144,12 @@ * @param data Data * @param dlen Data length * @remark If key already exists in the table it's data will - * be overwritten. + * be overwritten and original data will be returned. + * In case the key was not already present the function returns NULL. */ -ACR_DECLARE(void) ACR_TableSet(JNIEnv *_E, const char *file, int line, - acr_table_t *t, const char *key, - const void *data, acr_size_t dlen); +ACR_DECLARE(void *) ACR_TableSet(JNIEnv *_E, const char *file, int line, + acr_table_t *t, const char *key, + const void *data, acr_size_t dlen); /** * Add the key/data pair to the table @@ -188,6 +189,43 @@ ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx, void **data, acr_size_t *dlen); +/** + * Prepares the table for traversal. + * @param t Table to use. + * @return ACR_EOF if no data is present in the table. + */ +ACR_DECLARE(int) ACR_TableFirst(acr_table_t *t); + +/** + * Get the next data from the table + * @param t Table to use. + * @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(int) ACR_TableNext(acr_table_t *t, + void **data, acr_size_t *dlen); + +/** + * Test if there is next element in the table present. + * @param t Table to use. + * @return non zero if next element exists. + */ +ACR_DECLARE(int) ACR_TableHasNext(acr_table_t *t); + +/** Callback function used in ACR_TableForEach. + * If the function returns non zero the for each loop will break. + */ +typedef int (acr_table_callback_fn_t)(char *key, void *data, acr_size_t dlen); + +/** + * Execute callback for each table entry. + * @param t Table to use. + * @param callback Callback function + */ +ACR_DECLARE(void) ACR_TableForEach(acr_table_t *t, + acr_table_callback_fn_t *callback); + #ifdef __cplusplus } #endif Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h Fri Apr 17 07:39:29 2009 @@ -95,18 +95,6 @@ /* XXX : BSD has more */ } acr_procstate_e; -typedef enum { - ACR_EX_EGENERAL = 0, /* java/lang/Exception */ - ACR_EX_ERUNTIME, /* java/lang/RuntimeException */ - ACR_EX_ENOMEM, /* java/lang/OutOfMemoryError */ - ACR_EX_ENULL, /* java/lang/NullPointerException */ - ACR_EX_EINVAL, /* java/lang/IllegalArgumentException */ - ACR_EX_EINDEX, /* java/lang/IndexOutOfBoundsException */ - ACR_EX_ECCAST, /* java/lang/ClassCastException */ - ACR_EX_EIO, /* java/io/IOException */ - ACR_EX_ESOCK /* java/net/SocketException */ -} acr_trowclass_e; - /** * @defgroup acr_ctype ctype functions * These macros allow correct support of 8-bit characters on systems which Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Fri Apr 17 07:39:29 2009 @@ -31,6 +31,7 @@ "java/lang/IndexOutOfBoundsException", "java/lang/ClassCastException", "java/io/IOException", + "java/io/SyncFailedException", "java/net/SocketException", NULL }; Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Fri Apr 17 07:39:29 2009 @@ -100,42 +100,36 @@ ACR_UnloadClass(_E, &_clazzn); } -#if CC_SIZEOF_VOIDP == 8 ACR_JNI_EXPORT_DECLARE(void, Pointer, cleanup0)(ACR_JNISTDARGS) { acr_pointer_callback_fn_t *cleanup; +#if CC_SIZEOF_VOIDP == 8 jlong h = GET_IFIELD_J(0000, _O); jlong c = GET_IFIELD_J(0001, _O); if (h) { SET_IFIELD_J(0000, _O, 0); } - cleanup = (acr_pointer_callback_fn_t *)((acr_ptr_t)c); - if (cleanup) { - (*cleanup)((void *)((acr_ptr_t)h)); - } -} - #else - -ACR_JNI_EXPORT_DECLARE(void, Pointer, cleanup0)(ACR_JNISTDARGS) -{ - acr_pointer_callback_fn_t *cleanup; - jint h = GET_IFIELD_I(0000, _O); - jint c = GET_IFIELD_I(0001, _O); + jint h = GET_IFIELD_I(0000, _O); + jint c = GET_IFIELD_I(0001, _O); if (h) { SET_IFIELD_I(0000, _O, 0); } + +#endif cleanup = (acr_pointer_callback_fn_t *)((acr_ptr_t)c); if (cleanup) { - (*cleanup)((void *)((acr_ptr_t)h)); + int rc = (*cleanup)((void *)((acr_ptr_t)h)); + if (rc) { + /* Throw RuntimeException with errno message */ + ACR_ThrowException(_E, THROW_FMARK, ACR_EX_ERUNTIME, rc); + } } } -#endif - ACR_DECLARE(jobject) ACR_CreatePointer(JNIEnv *_E, void *p, acr_pointer_callback_fn_t *cb) { 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=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/tables.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/tables.c Fri Apr 17 07:39:29 2009 @@ -33,6 +33,8 @@ }; struct acr_table_t { + /* Index for first/next operations */ + acr_size_t i; /* Pointer to the array of table entries */ acr_array_t a; /* Table of hash buckets */ @@ -153,17 +155,18 @@ return tbl; } -ACR_DECLARE(void) ACR_TableSet(JNIEnv *_E, const char *file, int line, - acr_table_t *t, const char *key, - const void *data, acr_size_t dlen) +ACR_DECLARE(void *) ACR_TableSet(JNIEnv *_E, const char *file, int line, + acr_table_t *t, const char *key, + const void *data, acr_size_t dlen) { table_entry_t *e = NULL; unsigned int hash; + void *o = NULL; if (!key || !*key) { ACR_ThrowException(_E, file, line, ACR_EX_ENULL, ACR_EISNULL); - return; /* Skip empty and null strings */ + return NULL; /* Skip empty and null strings */ } hash = times33hash(key); @@ -183,15 +186,19 @@ /* The exception has already been thrown. * Just bail out. */ - return; + return NULL; } e->key = (char *)key; /* Insert new bucket into the list */ e->next = t->hash[hash]; t->hash[hash] = e; } + else + o = e->data; e->data = (void *)data; e->dlen = dlen; + + return o; } ACR_DECLARE(void) ACR_TableAdd(JNIEnv *_E, const char *file, int line, @@ -278,3 +285,51 @@ *dlen = e[idx].dlen; return ACR_SUCCESS; } + +ACR_DECLARE(int) ACR_TableFirst(acr_table_t *t) +{ + if (!t->a.nelts) { + return ACR_EOF; + } + t->i = 0; + return ACR_SUCCESS; +} + +ACR_DECLARE(int) ACR_TableNext(acr_table_t *t, + void **data, acr_size_t *dlen) +{ + table_entry_t *e = NULL; + + if (t->i >= t->a.nelts) { + return ACR_EOF; + } + e = (table_entry_t *)t->a.elts; + if (data) + *data = e[t->i].data; + if (dlen) + *dlen = e[t->i].dlen; + t->i++; + return ACR_SUCCESS; +} + +ACR_DECLARE(int) ACR_TableHasNext(acr_table_t *t) +{ + if (t->i >= t->a.nelts) + return 0; + else + return 1; +} + +ACR_DECLARE(void) ACR_TableForEach(acr_table_t *t, + acr_table_callback_fn_t *callback) +{ + + if (t && t->a.elts) { + acr_size_t i; + table_entry_t *e = (table_entry_t *)t->a.elts; + for (i = 0; i < t->a.nelts; i++) { + if ((*callback)(e[i].key, e[i].data, e[i].dlen)) + break; + } + } +} 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=765881&r1=765880&r2=765881&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Fri Apr 17 07:39:29 2009 @@ -125,9 +125,8 @@ ACR_TableAdd(_E, THROW_FMARK, t, J2S(s), J2S(s), 2); } if (rc) { - int i; - for (i = 0;;i++) { - int r = ACR_TableIndex(t, i, &d, &l); + for (ACR_TableHasNext(t); ACR_TableHasNext(t);) { + int r = ACR_TableNext(t, &d, &l); if (r != ACR_SUCCESS) break; } @@ -211,10 +210,11 @@ return 0; } -static void callback(void *p) +static int callback(void *p) { fprintf(stderr, "[native] Pointer callback called: %p\n", p); fflush(stderr); + return 0; } ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test017)(ACR_JNISTDARGS, jint d) 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=765881&r1=765880&r2=765881&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 Fri Apr 17 07:39:29 2009 @@ -37,7 +37,8 @@ return suite; } - private char longChar[] = new char[32768]; + private static final int longStrSize = 1024 * 1024; + private char longChar[] = new char[longStrSize]; private String longString; private static native void test000(int err); private static native void test001(String msg); @@ -63,7 +64,7 @@ throws Exception { System.loadLibrary("acr"); - for (int i = 0; i < 32768; i++) { + for (int i = 0; i < longStrSize; i++) { longChar[i] = (char)('A' + (i % 20)); } longString = new String(longChar); @@ -233,7 +234,7 @@ { Pointer p = test017(0xcafebabe); assertNotNull("Pointer",p); - p.testCleanup(); + p.free(); p = null; System.gc(); // This should be enough for a gc @@ -246,6 +247,8 @@ { Pointer p = test017(0xdeadbeef); assertNotNull("Pointer", p); + String ph = Pointer.SIZEOF == 4 ? "0xdeadbeef" : "0x00000000deadbeef"; + assertEquals("Pointer string ", ph, p.toString()); p = null; System.gc(); // This should be enough for a first invocation