Author: mturk Date: Mon Apr 27 06:14:03 2009 New Revision: 768872 URL: http://svn.apache.org/viewvc?rev=768872&view=rev Log: Use pointer macros
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java?rev=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java Mon Apr 27 06:14:03 2009 @@ -34,17 +34,27 @@ */ public abstract class Descriptor implements Closeable { + /* Last error operation. + * Set by the native on error condition, but never cleared. + * If the operation is recoverable the clearerr() should be + * used after the recovery. + */ private int IERRNUM; /* * Descriptor can be only created from native code. * Suppress any instantiation except form internal classes. */ - protected Descriptor() + private Descriptor() { // No Instance } + protected Descriptor(int e) + { + IERRNUM = e; + } + private native void close0() throws IOException, InstantiationException; @@ -61,6 +71,7 @@ */ public static final Descriptor NULL; static { + // Initialize NULL descriptor NULL = nulld0(); } @@ -123,6 +134,15 @@ } /** + * Set the last error of the descriptor to {...@code zero}. + * This method is public, but it is intended for the internal use only. + */ + public final void clearerr() + { + IERRNUM = 0; + } + + /** * Check if the underlying Operating system descriptor is closed. * @return {...@code true} if descriptor is closed {...@code false} otherwise. */ Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java?rev=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java Mon Apr 27 06:14:03 2009 @@ -27,17 +27,18 @@ private int IHANDLE; private int PHANDLE; - private int CLEANUP; + private int HANDLER; /* * Descriptor can be only created from native code. * Suppress any instantiation except form JNI. */ - protected Descriptor32(int i, int l, int c) + protected Descriptor32(int e, int i, int l, int h) { + super(e); IHANDLE = i; PHANDLE = l; - CLEANUP = c; + HANDLER = h; } public boolean isClosed() Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java?rev=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java Mon Apr 27 06:14:03 2009 @@ -27,17 +27,18 @@ private int IHANDLE; private long PHANDLE; - private long CLEANUP; + private long HANDLER; /* * Descriptor can be only created from native code. * Suppress any instantiation except form JNI. */ - private Descriptor64(int i, long l, long c) + private Descriptor64(int e, int i, long l, long h) { + super(e); IHANDLE = i; PHANDLE = l; - CLEANUP = c; + HANDLER = h; } public boolean isClosed() Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h?rev=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h Mon Apr 27 06:14:03 2009 @@ -36,22 +36,33 @@ ACR_DESC_SYNC } acr_descriptor_cb_type_e; +typedef struct acr_descriptor_cb_t { + int di; /* Integer descriptor */ + void *dp; /* Pointer descriptor */ + void **ctxp; /* Context data pointer */ + jobject data; /* handler provided Java object */ +} acr_descriptor_cb_t; + /** - * Descriptor callback function prototype. - * The callback function must check for data validity. + * Descriptor handler function prototype. + * The handler function must check for data validity. * Consecutive invocations will always contain NULL data. */ -typedef int (acr_descriptor_callback_fn_t)(acr_descriptor_cb_type_e, int, void *); +typedef int (acr_descriptor_handler_fn_t)(JNIEnv *, jobject, + acr_descriptor_cb_type_e, + acr_descriptor_cb_t *); /** * Create new Descriptor class instance * @param env Current JNI environment + * @param e Initial errno code for the descriptor object. * @param i Native integer descriptor to wrap into Descriptor class * @param p Native pointer descriptor to wrap into Descriptor class - * @param cb callback function to use + * @param cb handler function to use + * @param ho handler function to use */ -ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *env, int i, void *p, - acr_descriptor_callback_fn_t *cb); +ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *env, int e, int i, void *p, + acr_descriptor_handler_fn_t *cb); /** * Create new Descriptor array @@ -64,12 +75,20 @@ * Call descriptor cleanup and clear Java object. * @param env Current JNI environment * @param obj Java Descriptor object to clean. The function will - * clear the callback and data inside Java object. + * clear the handler and data inside Java object. * @return ACR_INCOMPLETE if already cleared. */ ACR_DECLARE(int) ACR_DescriptorCleanup(JNIEnv *env, jobject obj); /** + * Get the native errno number from the Descriptor object. + * @param env Current JNI environment + * @param obj Java Descriptor object use. + * @return Underlying errno. + */ +ACR_DECLARE(int) ACR_DescriptorGetErr(JNIEnv *env, jobject obj); + +/** * Get the native int descriptor from the Descriptor object. * @param env Current JNI environment * @param obj Java Descriptor object use. 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=768872&r1=768871&r2=768872&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 Mon Apr 27 06:14:03 2009 @@ -376,10 +376,12 @@ #define IFIELD_PTR "J" #define GET_IFIELD_P(I, O) \ _f##I##n.i ? (*_E)->GetLongField(_E, (O), _f##I##n.i) : 0 +#define GET_IFIELD_V(I, O, T) \ + _f##I##n.i ? (T)((acr_ssize_t)(*_E)->GetLongField(_E, (O), _f##I##n.i)) : NULL #define SET_IFIELD_P(I, O, V) \ - if (_f##I##n.i) { \ - (*_E)->SetLongField(_E, (O), _f##I##n.i, (jlong)(V)); \ + if (_f##I##n.i) { \ + (*_E)->SetLongField(_E, (O), _f##I##n.i, (jlong)((acr_ssize_t)(V))); \ } else (void)(0) #else /* CC_SIZEOF_VOIDP == 4 */ @@ -387,10 +389,12 @@ #define IFIELD_PTR "I" #define GET_IFIELD_P(I, O) \ _f##I##n.i ? (*_E)->GetIntField(_E, (O), _f##I##n.i) : 0 +#define GET_IFIELD_V(I, O, T) \ + _f##I##n.i ? (T)((acr_ssize_t)(*_E)->GetIntField(_E, (O), _f##I##n.i)) : NULL #define SET_IFIELD_P(I, O, V) \ if (_f##I##n.i) { \ - (*_E)->SetIntField(_E, (O), _f##I##n.i, (jint)(V)); \ + (*_E)->SetIntField(_E, (O), _f##I##n.i, (jint)((acr_ssize_t)(V))); \ } else (void)(0) #endif /* CC_SIZEOF_VOIDP == 8 */ Modified: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Mon Apr 27 06:14:03 2009 @@ -39,7 +39,7 @@ J_DECLARE_M_ID(0000) = { NULL, "<init>", - "(IJJ)V" + "(IIJJ)V" }; #else @@ -53,7 +53,7 @@ J_DECLARE_M_ID(0000) = { NULL, "<init>", - "(III)V" + "(IIII)V" }; #endif @@ -72,7 +72,7 @@ J_DECLARE_F_ID(0002) = { NULL, - "CLEANUP", + "HANDLER", IFIELD_PTR }; @@ -123,8 +123,7 @@ if (!_clazzn.i) { initd0(_E); } - nd = ACR_DescriptorCreate(_E, -1, NULL, NULL); - SET_IFIELD_I(0003, nd, ACR_EBADF); + nd = ACR_DescriptorCreate(_E, ACR_EBADF, -1, NULL, NULL); return nd; } @@ -132,22 +131,21 @@ ACR_JNI_EXPORT_DECLARE(void, Descriptor, close0)(ACR_JNISTDARGS) { if (_clazzn.i && J4MID(0000)) { - acr_descriptor_callback_fn_t *callback; - jint i = GET_IFIELD_I(0000, _O); - jniptr p = GET_IFIELD_P(0001, _O); - jniptr c = GET_IFIELD_P(0002, _O); + acr_descriptor_cb_t cb; + acr_descriptor_handler_fn_t *handler; + cb.di = GET_IFIELD_I(0000, _O); + cb.dp = GET_IFIELD_V(0001, _O, void *); + handler = GET_IFIELD_V(0002, _O, acr_descriptor_handler_fn_t *); - if (p) { - SET_IFIELD_P(0001, _O, 0); - } - if (i >= 0) { + if (cb.di >= 0) { SET_IFIELD_I(0000, _O, -1); } - - callback = N2P(c, acr_descriptor_callback_fn_t *); - if (callback) { - int rc = (*callback)(ACR_DESC_CLOSE, i, N2P(p, void *)); - if (rc) { + if (cb.dp) { + SET_IFIELD_P(0001, _O, NULL); + } + if (handler) { + int rc; + if ((rc = (*handler)(_E, _O, ACR_DESC_CLOSE, &cb))) { /* Throw IOException with errno message */ ACR_ThrowException(_E, THROW_FMARK, ACR_EX_EIO, rc); } @@ -160,21 +158,20 @@ ACR_JNI_EXPORT_DECLARE(void, Descriptor, close1)(ACR_JNISTDARGS) { if (_clazzn.i && J4MID(0000)) { - acr_descriptor_callback_fn_t *callback; - jint i = GET_IFIELD_I(0000, _O); - jniptr p = GET_IFIELD_P(0001, _O); - jniptr c = GET_IFIELD_P(0002, _O); + acr_descriptor_cb_t cb; + acr_descriptor_handler_fn_t *handler; + cb.di = GET_IFIELD_I(0000, _O); + cb.dp = GET_IFIELD_V(0001, _O, void *); + handler = GET_IFIELD_V(0002, _O, acr_descriptor_handler_fn_t *); - if (p) { - SET_IFIELD_P(0001, _O, 0); - } - if (i >= 0) { + if (cb.di >= 0) { SET_IFIELD_I(0000, _O, -1); } - - callback = N2P(c, acr_descriptor_callback_fn_t *); - if (callback) { - (*callback)(ACR_DESC_CLOSE, i, N2P(p, void *)); + if (cb.dp) { + SET_IFIELD_P(0001, _O, NULL); + } + if (handler) { + (*handler)(_E, _O, ACR_DESC_CLOSE, &cb); } } } @@ -182,15 +179,15 @@ ACR_JNI_EXPORT_DECLARE(void, Descriptor, sync0)(ACR_JNISTDARGS) { if (_clazzn.i && J4MID(0000)) { - acr_descriptor_callback_fn_t *callback; - jint i = GET_IFIELD_I(0000, _O); - jniptr p = GET_IFIELD_P(0001, _O); - jniptr c = GET_IFIELD_P(0002, _O); - - callback = N2P(c, acr_descriptor_callback_fn_t *); - if (callback) { - int rc = (*callback)(ACR_DESC_SYNC, i, N2P(p, void *)); - if (rc) { + acr_descriptor_cb_t cb; + acr_descriptor_handler_fn_t *handler; + cb.di = GET_IFIELD_I(0000, _O); + cb.dp = GET_IFIELD_V(0001, _O, void *); + handler = GET_IFIELD_V(0002, _O, acr_descriptor_handler_fn_t *); + + if (handler) { + int rc; + if ((rc = (*handler)(_E, _O, ACR_DESC_SYNC, &cb))) { if (rc == ACR_EINVAL) ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESYNC, 0); else @@ -203,12 +200,12 @@ } -ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *_E, int i, void *p, - acr_descriptor_callback_fn_t *cb) +ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *_E, int e, int i, void *p, + acr_descriptor_handler_fn_t *cb) { if (_clazzn.i && J4MID(0000)) return (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), - (jint)i, P2N(p), P2N(cb)); + (jint)e, (jint)i, P2N(p), P2N(cb)); else { ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); return NULL; @@ -218,32 +215,29 @@ ACR_DECLARE(int) ACR_DescriptorCleanup(ACR_JNISTDARGS) { if (_clazzn.i && J4MID(0000)) { - acr_descriptor_callback_fn_t *callback; - jint i = GET_IFIELD_I(0000, _O); - jniptr p = GET_IFIELD_P(0001, _O); - jniptr c = GET_IFIELD_P(0002, _O); + acr_descriptor_cb_t cb; + acr_descriptor_handler_fn_t *handler; if ((*_E)->MonitorEnter(_E, _O)) { /* Object locking failed */ return ACR_ENOLOCK; } - i = GET_IFIELD_I(0000, _O); - p = GET_IFIELD_P(0001, _O); - c = GET_IFIELD_P(0002, _O); - if (p) { - SET_IFIELD_P(0001, _O, 0); + cb.di = GET_IFIELD_I(0000, _O); + cb.dp = GET_IFIELD_V(0001, _O, void *); + handler = GET_IFIELD_V(0002, _O, acr_descriptor_handler_fn_t *); + if (cb.di >= 0) { + SET_IFIELD_I(0000, _O, -1); } - if (c) { - SET_IFIELD_P(0002, _O, 0); + if (cb.dp) { + SET_IFIELD_P(0001, _O, NULL); } - if (i >= 0) { - SET_IFIELD_I(0000, _O, -1); + if (handler) { + SET_IFIELD_P(0002, _O, NULL); } (*_E)->MonitorExit(_E, _O); - callback = N2P(c, acr_descriptor_callback_fn_t *); - if (callback) { - return (*callback)(ACR_DESC_CLOSE, i, N2P(p, void *)); + if (handler) { + return (*handler)(_E, _O, ACR_DESC_CLOSE, &cb); } else { /* Already cleared */ @@ -257,8 +251,7 @@ ACR_DECLARE(void *) ACR_DescriptorGetPtr(ACR_JNISTDARGS) { if (_clazzn.i && J4MID(0000)) { - jniptr p = GET_IFIELD_P(0001, _O); - return N2P(p, void *); + return GET_IFIELD_V(0001, _O, void *); } else { ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); @@ -277,6 +270,17 @@ } } +ACR_DECLARE(int) ACR_DescriptorGetErr(ACR_JNISTDARGS) +{ + if (_clazzn.i && J4MID(0000)) { + return GET_IFIELD_I(0003, _O); + } + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); + return -1; + } +} + ACR_DECLARE(int) ACR_DescriptorSetPtr(ACR_JNISTDARGS, void *p) { if (_clazzn.i && J4MID(0000)) { @@ -284,7 +288,7 @@ /* Object locking failed */ return ACR_ENOLOCK; } - SET_IFIELD_P(0001, _O, P2N(p)); + SET_IFIELD_P(0001, _O, p); #ifdef _JNI_CHECK_EXCEPTIONS if ((*_E)->ExceptionCheck(_E)) { return ACR_EGENERAL; 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=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Mon Apr 27 06:14:03 2009 @@ -135,15 +135,15 @@ { if (_clazzn.i && J4MID(0000)) { acr_pointer_cleanup_fn_t *cleanup; - jniptr h = GET_IFIELD_P(0000, _O); - jniptr c = GET_IFIELD_P(0001, _O); + void *pointer; - if (h) { - SET_IFIELD_P(0000, _O, 0); + pointer = GET_IFIELD_V(0000, _O, void *); + cleanup = GET_IFIELD_V(0001, _O, acr_pointer_cleanup_fn_t *); + if (pointer) { + SET_IFIELD_P(0000, _O, NULL); } - cleanup = (acr_pointer_cleanup_fn_t *)((acr_ptr_t)c); if (cleanup) { - int rc = (*cleanup)((void *)((acr_ptr_t)h)); + int rc = (*cleanup)(pointer); if (rc) { /* Throw RuntimeException with errno message */ ACR_ThrowException(_E, THROW_FMARK, ACR_EX_ERUNTIME, rc); @@ -159,15 +159,15 @@ { if (_clazzn.i && J4MID(0000)) { acr_pointer_cleanup_fn_t *cleanup; - jniptr h = GET_IFIELD_P(0000, _O); - jniptr c = GET_IFIELD_P(0001, _O); + void *pointer; - if (h) { - SET_IFIELD_P(0000, _O, 0); + pointer = GET_IFIELD_V(0000, _O, void *); + cleanup = GET_IFIELD_V(0001, _O, acr_pointer_cleanup_fn_t *); + if (pointer) { + SET_IFIELD_P(0000, _O, NULL); } - cleanup = N2P(c, acr_pointer_cleanup_fn_t *); if (cleanup) { - (*cleanup)(N2P(h, void *)); + (*cleanup)(pointer); } } } @@ -216,25 +216,23 @@ { if (_clazzn.i && J4MID(0000) && _O) { acr_pointer_cleanup_fn_t *cleanup; - jniptr h; - jniptr c; + void *pointer; if ((*_E)->MonitorEnter(_E, _O)) { /* Object locking failed */ return ACR_ENOLOCK; } - h = GET_IFIELD_P(0000, _O); - c = GET_IFIELD_P(0001, _O); - if (h) { - SET_IFIELD_P(0000, _O, 0); + pointer = GET_IFIELD_V(0000, _O, void *); + cleanup = GET_IFIELD_V(0001, _O, acr_pointer_cleanup_fn_t *); + if (pointer) { + SET_IFIELD_P(0000, _O, NULL); } - if (c) { - SET_IFIELD_P(0001, _O, 0); + if (cleanup) { + SET_IFIELD_P(0001, _O, NULL); } (*_E)->MonitorExit(_E, _O); - cleanup = N2P(c, acr_pointer_cleanup_fn_t *); if (cleanup) { - return (*cleanup)(N2P(h, void *)); + return (*cleanup)(pointer); } else { /* Already cleared */ @@ -248,11 +246,11 @@ ACR_DECLARE(void *) ACR_PointerGet(ACR_JNISTDARGS, size_t *len) { if (_clazzn.i && J4MID(0000) && _O) { - jniptr h = GET_IFIELD_P(0000, _O); + void *pointer = GET_IFIELD_V(0000, _O, void *); if (len) { *len = (size_t)GET_IFIELD_P(0002, _O); } - return N2P(h, void *); + return pointer; } else { ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); @@ -268,14 +266,14 @@ return ACR_ENOLOCK; } - SET_IFIELD_P(0000, _O, (jniptr)((acr_ptr_t)p)); + SET_IFIELD_P(0000, _O, p); #ifdef _JNI_CHECK_EXCEPTIONS if ((*_E)->ExceptionCheck(_E)) { return ACR_EGENERAL; } #endif if (len) { - SET_IFIELD_P(0002, _O, (jniptr)len); + SET_IFIELD_P(0002, _O, len); #ifdef _JNI_CHECK_EXCEPTIONS if ((*_E)->ExceptionCheck(_E)) { return ACR_EGENERAL; 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=768872&r1=768871&r2=768872&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Mon Apr 27 06:14:03 2009 @@ -243,9 +243,9 @@ return 0; } -static int dcallback(acr_descriptor_cb_type_e t, int i, void *p) +static int dhandler(ACR_JNISTDARGS, acr_descriptor_cb_type_e t, acr_descriptor_cb_t *cb) { - fprintf(stderr, "[native] Descriptor callback(%d) called: %d/%p\n", t, i, p); + fprintf(stderr, "[native] Descriptor handler(%d) called: %d/%p\n", t, cb->di, cb->dp); fflush(stderr); return 0; } @@ -253,7 +253,7 @@ ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test021)(ACR_JNISTDARGS, jint i, jint d) { - return ACR_DescriptorCreate(_E, i, I2P(d, void *), dcallback); + return ACR_DescriptorCreate(_E, 0, i, I2P(d, void *), dhandler); } ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test022)(ACR_JNISTDARGS, jobject d) @@ -288,7 +288,7 @@ int i; jobjectArray a = ACR_NewDescriptorArray(_E, (jsize)l); for (i = 0; i < l; i++) { - jobject d = ACR_DescriptorCreate(_E, i, NULL, dcallback); + jobject d = ACR_DescriptorCreate(_E, 0, i, NULL, dhandler); (*_E)->SetObjectArrayElement(_E, a, (jsize)i, d); } return a;