List:Commits« Previous MessageNext Message »
From:Martin Zaun Date:September 20 2009 7:39pm
Subject:bzr commit into mysql-5.1-telco-6.3 branch (martin.zaun:3056)
View as plain text  
#At file:///Users/mz/mysql/ndb-6.3-ndbjtie-work/ based on revid:martin.zaun@stripped

 3056 Martin Zaun	2009-09-20
      Extended ByteBuffer mapping to include BB.position() and check BB.remaining().

    modified:
      storage/ndb/ndbjtie/jtie/include/jtie_lib.hpp
      storage/ndb/ndbjtie/jtie/include/jtie_tconv_ptrbybb_impl.hpp
      storage/ndb/ndbjtie/jtie/test/myapi/myapi.hpp
      storage/ndb/ndbjtie/jtie/test/myjapi/Makefile
=== modified file 'storage/ndb/ndbjtie/jtie/include/jtie_lib.hpp'
--- a/storage/ndb/ndbjtie/jtie/include/jtie_lib.hpp	2009-09-18 20:40:07 +0000
+++ b/storage/ndb/ndbjtie/jtie/include/jtie_lib.hpp	2009-09-20 19:39:00 +0000
@@ -57,6 +57,24 @@ const char * const _ByteBuffer_asReadOnl
 
 // ---------------------------------------------------------------------------
 
+const char * const _ByteBuffer_remaining
+    ::class_name = "java/nio/ByteBuffer"; // includes java.nio.Buffer methods
+const char * const _ByteBuffer_remaining
+    ::member_name = "remaining";
+const char * const _ByteBuffer_remaining
+    ::member_descriptor = "()I";
+
+// ---------------------------------------------------------------------------
+
+const char * const _ByteBuffer_position
+    ::class_name = "java/nio/ByteBuffer"; // includes java.nio.Buffer methods
+const char * const _ByteBuffer_position
+    ::member_name = "position";
+const char * const _ByteBuffer_position
+    ::member_descriptor = "()I";
+
+// ---------------------------------------------------------------------------
+
 const char * const _Wrapper_cdelegate
     ::class_name = "com/mysql/jtie/Wrapper";
 const char * const _Wrapper_cdelegate

=== modified file 'storage/ndb/ndbjtie/jtie/include/jtie_tconv_ptrbybb_impl.hpp'
--- a/storage/ndb/ndbjtie/jtie/include/jtie_tconv_ptrbybb_impl.hpp	2009-09-18 20:40:07 +0000
+++ b/storage/ndb/ndbjtie/jtie/include/jtie_tconv_ptrbybb_impl.hpp	2009-09-20 19:39:00 +0000
@@ -23,6 +23,7 @@
 #define jtie_tconv_ptrbybb_impl_hpp
 
 #include <cassert>
+#include <cstring>
 #include <stdint.h>
 #include <jni.h>
 
@@ -44,11 +45,13 @@ struct _ByteBuffer_isReadOnly {
     typedef _jmethodID * memberID_t;
 };
 
-// Provides a (cached) access to method Id  of ByteBuffer.isReadOnly().
+// Provides a (cached) access to method Id of ByteBuffer.isReadOnly().
 //typedef JniMemberId< NO_CACHING, _ByteBuffer_isReadOnly >
 typedef JniMemberId< WEAK_CACHING, _ByteBuffer_isReadOnly >
     ByteBuffer_isReadOnly;
 
+// ---------------------------------------------------------------------------
+
 // Describes the method Id for ByteBuffer.asReadOnlyBuffer().
 struct _ByteBuffer_asReadOnlyBuffer {
     static const char * const class_name;
@@ -57,11 +60,43 @@ struct _ByteBuffer_asReadOnlyBuffer {
     typedef _jmethodID * memberID_t;
 };
 
-// Provides a (cached) access to method Id  of ByteBuffer.asReadOnlyBuffer().
+// Provides a (cached) access to method Id of ByteBuffer.asReadOnlyBuffer().
 //typedef JniMemberId< NO_CACHING, _ByteBuffer_asReadOnlyBuffer >
 typedef JniMemberId< WEAK_CACHING, _ByteBuffer_asReadOnlyBuffer >
     ByteBuffer_asReadOnlyBuffer;
 
+// ---------------------------------------------------------------------------
+
+// Describes the method Id for ByteBuffer.remaining().
+struct _ByteBuffer_remaining {
+    static const char * const class_name;
+    static const char * const member_name;
+    static const char * const member_descriptor;
+    typedef _jmethodID * memberID_t;
+};
+
+// Provides a (cached) access to method Id of ByteBuffer.remaining().
+//typedef JniMemberId< NO_CACHING, _ByteBuffer_remaining >
+typedef JniMemberId< WEAK_CACHING, _ByteBuffer_remaining >
+    ByteBuffer_remaining;
+
+// ---------------------------------------------------------------------------
+
+// Describes the method Id for ByteBuffer.position().
+struct _ByteBuffer_position {
+    static const char * const class_name;
+    static const char * const member_name;
+    static const char * const member_descriptor;
+    typedef _jmethodID * memberID_t;
+};
+
+// Provides a (cached) access to method Id of ByteBuffer.position().
+//typedef JniMemberId< NO_CACHING, _ByteBuffer_position >
+typedef JniMemberId< WEAK_CACHING, _ByteBuffer_position >
+    ByteBuffer_position;
+
+// ---------------------------------------------------------------------------
+
 // Returns zero if a buffer is read-only; otherwise, an exception is pending.
 inline cstatus
 ensureMutableBuffer(jtie_j_n_ByteBuffer jbb, JNIEnv * env) {
@@ -86,10 +121,9 @@ ensureMutableBuffer(jtie_j_n_ByteBuffer 
                     const char * c = "java/nio/ReadOnlyBufferException";
                     // this exception's c'tor does not take messages
                     const char * m = NULL;
-                    //const char * m = ("NDB Jtie: java.nio.ByteBuffer must"
+                    //const char * m = ("JTie: java.nio.ByteBuffer must"
                     //                  " not be read-only when mapped to a"
-                    //                  "  non-const object reference type"
-                    //                  " (file: " __FILE__ ")");
+                    //                  "  non-const object reference type");
                     registerException(env, c, m);
                 } else {
                     // ok
@@ -113,39 +147,120 @@ ensureMinBufferSize(jtie_j_n_ByteBuffer 
     // check the ByteBuffer's capacity
     jlong bc = env->GetDirectBufferCapacity(jbb);
     if (bc < N) {
+        // crashes with gcc & operator<<(ostream &, jlong/jint)
+        char m[256];
+        const long long n = N;
+        const long long BC = bc;
+        if (bc < 0) {
+            sprintf(m, "JTie: failed to retrieve java.nio.ByteBuffer's"
+                    " capacity (perhaps, a direct buffer or an unaligned"
+                    " view buffer)");
+        } else {
+            sprintf(m, "JTie: java.nio.ByteBuffer's capacity is too small"
+                    "  for the mapped parameter;"
+                    " required: %lld, found: %lld.", n, BC);
+        }
         const char * c = "java/lang/IllegalArgumentException";
-        const char * m = (bc < 0
-                          ? ("NDB Jtie: failed to retrieve the"
-                             " java.nio.ByteBuffer's capacity (might not be"
-                             " a direct buffer or might be an unaligned view"
-                             " buffer) (file: " __FILE__ ")")
-                          : ("NDB Jtie: the java.nio.ByteBuffer's capacity"
-                             "  is too small for the mapped parameter"
-                             " (file: " __FILE__ ")"));
         registerException(env, c, m);
     } else {
+#ifndef NDEBUG
+        // get a (local or global) class object reference
+        jclass cls = ByteBuffer_remaining::getClass(env);
+        if (cls == NULL) {
+            // exception pending
+        } else {
+            // get the method ID valid along with the class object reference
+            jmethodID mid = ByteBuffer_remaining::getId(env, cls);
+            if (mid == NULL) {
+                // exception pending
+            } else {
+                jint r = env->CallIntMethod(jbb, mid);
+                if (env->ExceptionCheck() != JNI_OK) {
+                    // exception pending
+                } else {
+                    if (r < N) {
+                        // crashes with gcc & operator<<(ostream &, jlong/jint)
+                        char m[256];
+                        const long long n = N;
+                        const long long R = r;
+                        sprintf(m, "JTie: too few remaining elements of"
+                                " java.nio.ByteBuffer for mapped parameter;"
+                                " required: %lld, found: %lld", n, R);
+                        const char * c = "java/lang/IllegalArgumentException";
+                        registerException(env, c, m);
+                    } else {
+                        // ok
+                        s = 0;
+                    }
+                }
+            }
+            // release reference (if needed)
+            ByteBuffer_remaining::releaseRef(env, cls);
+        }
+#else
         // ok
         s = 0;
+#endif // NDEBUG
     }    
     return s;
 }
 
-// Returns the internal buffer address of a ByteBuffer; if the address cannot
+// Returns the buffer's position; if the position cannot be accessed for any
+// reason, a negative value is returned and an exception is pending.
+inline int32_t
+getBufferPosition(jtie_j_n_ByteBuffer jbb, JNIEnv * env) {
+    // init return value to error
+    jint pos = -1;
+
+    // get a (local or global) class object reference
+    jclass cls = ByteBuffer_position::getClass(env);
+    if (cls == NULL) {
+        // exception pending
+    } else {
+        // get the method ID valid along with the class object reference
+        jmethodID mid = ByteBuffer_position::getId(env, cls);
+        if (mid == NULL) {
+            // exception pending
+        } else {
+            jint p = env->CallIntMethod(jbb, mid);
+            if (env->ExceptionCheck() != JNI_OK) {
+                // exception pending
+            } else {
+                // ok
+                pos = p;
+            }
+        }
+        // release reference (if needed)
+        ByteBuffer_position::releaseRef(env, cls);
+    }
+    return pos;
+}
+
+
+// Returns the buffer address of a direct ByteBuffer; if the address cannot
 // be accessed for any reason, NULL is returned and an exception is pending.
 inline void *
-getInternalBufferAddress(jtie_j_n_ByteBuffer jbb, JNIEnv * env) {
+getByteBufferAddress(jtie_j_n_ByteBuffer jbb, JNIEnv * env) {
     // get the internal buffer address of direct ByteBuffer
-    void * b = env->GetDirectBufferAddress(jbb);
-    if (b == NULL) {
+    char * a = static_cast<char *>(env->GetDirectBufferAddress(jbb));
+    if (a == NULL) {
         // raise exception
+        const char * m = ("JTie: cannot get the java.nio.ByteBuffer's"
+                          " internal address (perhaps, not a direct buffer"
+                          " or its memory region is undefined)");
         const char * c = "java/lang/IllegalArgumentException";
-        const char * m = ("NDB Jtie: cannot get the java.nio.ByteBuffer's"
-                          " internal address (might not be a direct buffer"
-                          " or its memory region is undefined)"
-                          " (file: " __FILE__ ")");
         registerException(env, c, m);
+    } else {
+#ifndef JTIE_BYTEBUFFER_MAPS_TO_BASE_ADDRESS
+        int32_t p = getBufferPosition(jbb, env);
+        if (p < 0) {
+            // exception pending
+        } else {
+            a += p;
+        }
+#endif // JTIE_BYTEBUFFER_MAPS_TO_BASE_ADDRESS
     }
-    return b;
+    return a;
 }
 
 // Implements the mapping of ByteBuffers to pointer parameters.
@@ -172,13 +287,13 @@ struct ByteBufferPtrParam {
                     // exception pending
                 } else {
                     assert(env->GetDirectBufferCapacity(j) >= J::capacity);
-                    void * b = getInternalBufferAddress(j, env);
-                    if (b == NULL) {
+                    void * a = getByteBufferAddress(j, env);
+                    if (a == NULL) {
                         // exception pending
                     } else {
                         // ok
                         s = 0;
-                        c = static_cast<C *>(b);
+                        c = static_cast<C *>(a);
                     }
                 }
             }
@@ -225,10 +340,10 @@ wrapByteBufferAsReadOnly(J * jbb, JNIEnv
                 // exception pending
             } else {
                 if (jo == NULL) {
+                    const char * m
+                        = ("JTie: invalid NULL return from"
+                           " java.nio.ByteBuffer.asReadOnlyBuffer()");
                     const char * c = "java/lang/AssertionError";
-                    const char * m = ("NDB JTie: invalid return NULL from"
-                                      " java.nio.ByteBuffer.asReadOnlyBuffer()"
-                                      " (file: " __FILE__ ")");
                     registerException(env, c, m);
                 } else {
                     j = static_cast< J * >(jo);

=== modified file 'storage/ndb/ndbjtie/jtie/test/myapi/myapi.hpp'
--- a/storage/ndb/ndbjtie/jtie/test/myapi/myapi.hpp	2009-09-19 08:52:42 +0000
+++ b/storage/ndb/ndbjtie/jtie/test/myapi/myapi.hpp	2009-09-20 19:39:00 +0000
@@ -983,5 +983,13 @@ inline D1 * D1::sub() { TRACE("D1 * D1::
 inline D1 * D2::sub() { TRACE("D1 * D2::sub()"); return NULL; }
 
 // ----------------------------------------------------------------------
+// pointer dereference functions
+// ----------------------------------------------------------------------
+
+// XXX!
+
+//extern int deref(int * p);
+
+// ----------------------------------------------------------------------
 
 #endif // _myapi

=== modified file 'storage/ndb/ndbjtie/jtie/test/myjapi/Makefile'
--- a/storage/ndb/ndbjtie/jtie/test/myjapi/Makefile	2009-09-19 08:52:42 +0000
+++ b/storage/ndb/ndbjtie/jtie/test/myjapi/Makefile	2009-09-20 19:39:00 +0000
@@ -17,6 +17,8 @@ include	../../../env.properties
   TEST_PKG_PATH	= test
 
   DDEFINES 	=
+# -DJTIE_BYTEBUFFER_MAPS_TO_BASE_ADDRESS
+
 
   DELIVERABLES	= libmyjapi.a libmyjapi.$(DLL_SUFFIX) \
 		$(TEST_PKG_PATH)/MyJapiTest.class \


Attachment: [text/bzr-bundle] bzr/martin.zaun@sun.com-20090920193900-qcchr4tlkmjpulx1.bundle
Thread
bzr commit into mysql-5.1-telco-6.3 branch (martin.zaun:3056)Martin Zaun20 Sep