[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdesupport/soprano/backends/sesame2
From:       Sebastian Trueg <sebastian () trueg ! de>
Date:       2008-05-05 15:34:05
Message-ID: 1210001645.435301.20674.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 804284 by trueg:

Improved memory management. Still seems a mem leak left but the situation has been \
improved a lot.

 M  +3 -3      jniwrapper.cpp  
 M  +7 -31     jobjectref.cpp  
 M  +1 -2      jobjectref.h  
 M  +1 -1      sesame2backend.cpp  
 M  +12 -4     sesame2iterator.cpp  
 M  +1 -1      sesame2queryresultiteratorbackend.cpp  
 M  +6 -4      sesame2repository.cpp  
 M  +18 -18    sesame2repositoryconnection.cpp  
 M  +7 -27     sesame2utils.cpp  
 M  +6 -4      sesame2valuefactory.cpp  


--- trunk/kdesupport/soprano/backends/sesame2/jniwrapper.cpp #804283:804284
@@ -128,7 +128,7 @@
     static const char* constructorName = "<init>";
     static const char* defaultConstructorSig = "()V";
 
-    jclass clazz = env()->FindClass( className );
+    JClassRef clazz = env()->FindClass( className );
     if ( !clazz ) {
         debugException();
         return 0;
@@ -145,7 +145,7 @@
     va_list args;
     va_start( args, constructorSig );
 
-    JObjectRef newObject = env()->NewObjectV( clazz, constructorId, args );
+    JObjectRef newObject = env()->NewObjectV( clazz.data(), constructorId, args );
 
     va_end( args );
 
@@ -175,7 +175,7 @@
 
 Soprano::Error::Error JNIWrapper::convertAndClearException()
 {
-    jthrowable exception = env()->ExceptionOccurred();
+    JObjectRef exception = env()->ExceptionOccurred();
     if ( exception ) {
         env()->ExceptionDescribe();
         JNIObjectWrapper exWr( exception );
--- trunk/kdesupport/soprano/backends/sesame2/jobjectref.cpp #804283:804284
@@ -25,15 +25,15 @@
 
 #include <QtCore/QDebug>
 #include <QtCore/QString>
+#include <QtCore/QSharedData>
 
 
-class JObjectRef::Private
+class JObjectRef::Private : public QSharedData
 {
 public:
     Private( jobject o = 0, bool g = false )
         : object( o ),
-          global( g ),
-          m_ref( 1 ) {
+          global( g ) {
     }
 
     ~Private() {
@@ -47,19 +47,8 @@
         }
     }
 
-    int ref() {
-        return ++m_ref;
-    }
-
-    int unref() {
-        return --m_ref;
-    }
-
     jobject object;
     bool global;
-
-private:
-    int m_ref;
 };
 
 JObjectRef::JObjectRef()
@@ -77,39 +66,25 @@
 JObjectRef::JObjectRef( const JObjectRef& other )
 {
     d = other.d;
-    d->ref();
 }
 
 
 JObjectRef::~JObjectRef()
 {
-    if ( !d->unref() ) {
-        delete d;
-    }
 }
 
 
 JObjectRef& JObjectRef::operator=( const JObjectRef& other )
 {
-    if ( d != other.d ) {
-        if ( !d->unref() ) {
-            delete d;
-        }
-        d = other.d;
-        d->ref();
-    }
-
+    d = other.d;
     return *this;
 }
 
 
 JObjectRef& JObjectRef::operator=( jobject o )
 {
-    if ( !d->unref() ) {
-        delete d;
-    }
-    d = new Private( o );
-    d->ref();
+    d->object = o;
+    d->global = false;
     return *this;
 }
 
@@ -245,6 +220,7 @@
             Q_ASSERT( chars[i]>>8 == 0 );
             a[i] = ( char )chars[i];
         }
+        JNIWrapper::instance()->env()->ReleaseStringChars( JStringRef::data(), chars \
);  }
     return a;
 }
--- trunk/kdesupport/soprano/backends/sesame2/jobjectref.h #804283:804284
@@ -23,7 +23,6 @@
 #define _JOBJECT_REF_H_
 
 #include <QtCore/QSharedDataPointer>
-#include <QtCore/QSharedData>
 
 #include <jni.h>
 
@@ -53,7 +52,7 @@
 
 private:
     class Private;
-    Private* d;
+    QExplicitlySharedDataPointer<Private> d;
 };
 
 
--- trunk/kdesupport/soprano/backends/sesame2/sesame2backend.cpp #804283:804284
@@ -82,7 +82,7 @@
     QString path;
     bool memory = false;
 
-    // FIXME: support inferecen option
+    // FIXME: support inference option
 
     Q_FOREACH( BackendSetting s, settings ) {
         if ( s.option() == BackendOptionUser ) {
--- trunk/kdesupport/soprano/backends/sesame2/sesame2iterator.cpp #804283:804284
@@ -30,7 +30,8 @@
     Private( Iterator* parent )
         : m_parent( parent ),
           m_IDhasNext( 0 ),
-          m_IDnext( 0 ) {
+          m_IDnext( 0 ),
+          m_IDclose( 0 ) {
     }
 
     jmethodID IDhasNext() {
@@ -49,11 +50,20 @@
         return m_IDnext;
     }
 
+    jmethodID IDclose() {
+        if ( !m_IDclose ) {
+            m_IDclose = m_parent->getMethodID( "close", "()V" );
+            JNIWrapper::instance()->debugException();
+        }
+        return m_IDclose;
+    }
+
 private:
     Iterator* m_parent;
 
     jmethodID m_IDhasNext;
     jmethodID m_IDnext;
+    jmethodID m_IDclose;
 };
 
 
@@ -97,8 +107,6 @@
 {
     // close the result (if this is a closable it)
     if ( isInstanceOf( JNIWrapper::instance()->env()->FindClass( \
                INFO_ADUNA_ITERATION_CLOSABLEITERATION ) ) ) {
-        if ( jmethodID closeID = getMethodID( "close", "()V" ) ) {
-            callVoidMethod( closeID );
-        }
+        callVoidMethod( d->IDclose() );
     }
 }
--- trunk/kdesupport/soprano/backends/sesame2/sesame2queryresultiteratorbackend.cpp \
#804283:804284 @@ -53,7 +53,7 @@
 
         // cache the binding names, it is just simpler
         if ( isTupleResult ) {
-            jobject bindingList = result->callObjectMethod( result->getMethodID( \
"getBindingNames", "()L"JAVA_UTIL_LIST";" ) ); +            JObjectRef bindingList = \
result->callObjectMethod( result->getMethodID( "getBindingNames", \
"()L"JAVA_UTIL_LIST";" ) );  JNIObjectWrapper listWrapper( bindingList );
             Iterator it( listWrapper.callObjectMethod( listWrapper.getMethodID( \
"iterator", "()L"JAVA_UTIL_ITERATOR";" ) ) );  while ( it.hasNext() ) {
--- trunk/kdesupport/soprano/backends/sesame2/sesame2repository.cpp #804283:804284
@@ -105,7 +105,9 @@
     }
 
     // create an instance of org.openrdf.repository.sail.SailRepository
-    JObjectRef repository = JNIWrapper::instance()->constructObject( \
ORG_OPENRDF_REPOSITORY_SAIL_SAILREPOSITORY, "(L"ORG_OPENRDF_SAIL_SAIL";)V", \
store.data() ); +    JObjectRef repository = JNIWrapper::instance()->constructObject( \
ORG_OPENRDF_REPOSITORY_SAIL_SAILREPOSITORY, +                                         \
"(L"ORG_OPENRDF_SAIL_SAIL";)V", +                                                     \
store.data() );  if ( !repository ) {
         return 0;
     }
@@ -157,7 +159,7 @@
             JNIWrapper::instance()->debugException();
             return 0;
         }
-        d->valueFactory = new ValueFactory( valueFactory );
+        d->valueFactory = new ValueFactory( valueFactory.toGlobalRef() );
     }
 
     return d->valueFactory;
@@ -172,7 +174,7 @@
             JNIWrapper::instance()->debugException();
             return 0;
         }
-        d->repositoryConnection = new RepositoryConnection( repositoryConnection );
+        d->repositoryConnection = new RepositoryConnection( \
repositoryConnection.toGlobalRef() );  }
 
     return d->repositoryConnection;
@@ -189,7 +191,7 @@
             JNIWrapper::instance()->debugException();
             return 0;
         }
-        d->sopranoWrapper = new SopranoWrapper( sopranoWrapper );
+        d->sopranoWrapper = new SopranoWrapper( sopranoWrapper.toGlobalRef() );
     }
 
     return d->sopranoWrapper;
--- trunk/kdesupport/soprano/backends/sesame2/sesame2repositoryconnection.cpp \
#804283:804284 @@ -159,7 +159,7 @@
 
 JObjectRef Soprano::Sesame2::RepositoryConnection::getContextIDs()
 {
-    return callObjectMethod( d->IDgetContextIDs() );
+    return callObjectMethod( d->IDgetContextIDs() ).toGlobalRef();
 }
 
 
@@ -173,42 +173,42 @@
 
 JObjectRef Soprano::Sesame2::RepositoryConnection::getStatements( const JObjectRef& \
subject, const JObjectRef& predicate, const JObjectRef& object, const JObjectRef& \
context )  {
-    jobjectArray contexts = JNIWrapper::instance()->env()->NewObjectArray( context ? \
                1 : 0,
-                                                                           \
                d->classResource(),
-                                                                           context \
                );
-    return callObjectMethod( d->IDgetStatements(), subject.data(), predicate.data(), \
object.data(), true, contexts ); +    JObjectRef contexts = \
JNIWrapper::instance()->env()->NewObjectArray( context ? 1 : 0, +                     \
d->classResource(), +                                                                 \
context ); +    return callObjectMethod( d->IDgetStatements(), subject.data(), \
predicate.data(), object.data(), true, contexts.data() ).toGlobalRef();  }
 
 
 bool Soprano::Sesame2::RepositoryConnection::hasStatement( const JObjectRef& \
subject, const JObjectRef& predicate, const JObjectRef& object, const JObjectRef& \
context )  {
-    jobjectArray contexts = JNIWrapper::instance()->env()->NewObjectArray( context ? \
                1 : 0,
-                                                                           \
                d->classResource(),
-                                                                           context \
                );
-    return callBooleanMethod( d->IDhasStatement(), subject.data(), predicate.data(), \
object.data(), true, contexts ); +    JObjectRef contexts = \
JNIWrapper::instance()->env()->NewObjectArray( context ? 1 : 0, +                     \
d->classResource(), +                                                                 \
context ); +    return callBooleanMethod( d->IDhasStatement(), subject.data(), \
predicate.data(), object.data(), true, contexts.data() );  }
 
 
 void Soprano::Sesame2::RepositoryConnection::remove( const JObjectRef& subject, \
const JObjectRef& predicate, const JObjectRef& object, const JObjectRef& context )  {
-    jobjectArray contexts = JNIWrapper::instance()->env()->NewObjectArray( context ? \
                1 : 0,
-                                                                           \
                d->classResource(),
-                                                                           context \
                );
-    callVoidMethod( d->IDremove(), subject.data(), predicate.data(), object.data(), \
contexts ); +    JObjectRef contexts = JNIWrapper::instance()->env()->NewObjectArray( \
context ? 1 : 0, +                                                                    \
d->classResource(), +                                                                 \
context ); +    callVoidMethod( d->IDremove(), subject.data(), predicate.data(), \
object.data(), contexts.data() );  }
 
 
 void Soprano::Sesame2::RepositoryConnection::remove( const JObjectRef& statement )
 {
-    jobjectArray contexts = JNIWrapper::instance()->env()->NewObjectArray( 0, \
                d->classResource(), 0 );
-    callVoidMethod( d->IDremoveStatement(), statement.data(), contexts );
+    JObjectRef contexts = JNIWrapper::instance()->env()->NewObjectArray( 0, \
d->classResource(), 0 ); +    callVoidMethod( d->IDremoveStatement(), \
statement.data(), contexts.data() );  }
 
 
 long Soprano::Sesame2::RepositoryConnection::size()
 {
-    jobjectArray contexts = JNIWrapper::instance()->env()->NewObjectArray( 0, \
                d->classResource(), 0 );
-    return callLongMethod( d->IDsize(), contexts );
+    JObjectRef contexts = JNIWrapper::instance()->env()->NewObjectArray( 0, \
d->classResource(), 0 ); +    return callLongMethod( d->IDsize(), contexts.data() );
 }
 
 
@@ -221,7 +221,7 @@
 JObjectRef Soprano::Sesame2::RepositoryConnection::prepareQuery( const JObjectRef& \
queryLang, const JStringRef& queryString )  {
     // prepare the query
-    return callObjectMethod( d->IDprepareQuery(), queryLang.data(), \
queryString.data() ); +    return callObjectMethod( d->IDprepareQuery(), \
queryLang.data(), queryString.data() ).toGlobalRef();  }
 
 
--- trunk/kdesupport/soprano/backends/sesame2/sesame2utils.cpp #804283:804284
@@ -30,28 +30,6 @@
 #include <QtCore/QDebug>
 
 
-class ClassCache
-{
-public:
-    ClassCache() {
-        classURI = JNIWrapper::instance()->env()->FindClass( ORG_OPENRDF_MODEL_URI \
                );
-        classBNode = JNIWrapper::instance()->env()->FindClass( \
                ORG_OPENRDF_MODEL_BNODE );
-        classLiteral = JNIWrapper::instance()->env()->FindClass( \
                ORG_OPENRDF_MODEL_LITERAL );
-
-        Q_ASSERT( classURI );
-        Q_ASSERT( classBNode );
-        Q_ASSERT( classLiteral );
-    }
-
-    JClassRef classURI;
-    JClassRef classBNode;
-    JClassRef classLiteral;
-};
-
-Q_GLOBAL_STATIC( ClassCache, classCache )
-
-
-
 QUrl Soprano::Sesame2::convertURI( const JObjectRef& uri )
 {
     JNIObjectWrapper uriWrapper( uri );
@@ -64,18 +42,22 @@
 {
     JNIObjectWrapper resourceWrapper( resource );
 
+    JClassRef classURI = JNIWrapper::instance()->env()->FindClass( \
ORG_OPENRDF_MODEL_URI ); +    JClassRef classBNode = \
JNIWrapper::instance()->env()->FindClass( ORG_OPENRDF_MODEL_BNODE ); +    JClassRef \
classLiteral = JNIWrapper::instance()->env()->FindClass( ORG_OPENRDF_MODEL_LITERAL ); \
+  if ( !resource ) {
         // empty node
         return Node();
     }
-    else if ( JNIWrapper::instance()->env()->IsInstanceOf( resource, \
classCache()->classURI ) ) { +    else if ( \
JNIWrapper::instance()->env()->IsInstanceOf( resource, classURI ) ) {  return \
convertURI( resource );  }
-    else if ( JNIWrapper::instance()->env()->IsInstanceOf( resource, \
classCache()->classBNode ) ) { +    else if ( \
                JNIWrapper::instance()->env()->IsInstanceOf( resource, classBNode ) ) \
                {
         JStringRef uri = resourceWrapper.callObjectMethod( \
resourceWrapper.getMethodID( "getID", "()L"JAVA_LANG_STRING";" ) );  return Node( \
uri.toQString() );  }
-    else if ( JNIWrapper::instance()->env()->IsInstanceOf( resource, \
classCache()->classLiteral ) ) { +    else if ( \
                JNIWrapper::instance()->env()->IsInstanceOf( resource, classLiteral ) \
                ) {
         JStringRef value = resourceWrapper.callObjectMethod( \
                resourceWrapper.getMethodID( "getLabel", "()L"JAVA_LANG_STRING";" ) \
                );
         JStringRef lang = resourceWrapper.callObjectMethod( \
                resourceWrapper.getMethodID( "getLanguage", "()L"JAVA_LANG_STRING";" \
                ) );
         JObjectRef dataType = resourceWrapper.callObjectMethod( \
resourceWrapper.getMethodID( "getDatatype", "()L"ORG_OPENRDF_MODEL_URI";" ) ); @@ \
-96,8 +78,6 @@  
 Soprano::Statement Soprano::Sesame2::convertStatement( const JObjectRef& o )
 {
-    Q_ASSERT( JNIWrapper::instance()->env()->IsInstanceOf( o, \
                JNIWrapper::instance()->env()->FindClass( ORG_OPENRDF_MODEL_STATEMENT \
                ) ) );
-
     JNIObjectWrapper statementWrapper( o );
 
     JObjectRef subject = statementWrapper.callObjectMethod( \
                statementWrapper.getMethodID( "getSubject", \
                "()L"ORG_OPENRDF_MODEL_RESOURCE";" ) );
--- trunk/kdesupport/soprano/backends/sesame2/sesame2valuefactory.cpp #804283:804284
@@ -153,17 +153,19 @@
     case Node::LiteralNode:
         // FIXME: is it more performant to create the instances directly from the \
values instead of strings?  if ( node.literal().isString() && \
!node.language().isEmpty() ) { +            JStringRef val( node.toString() );
+            JStringRef lang( node.language() );
             return callObjectMethod( d->IDcreateLiteralWithLang(),
-                                     JStringRef( node.toString() ).data(),
-                                     JStringRef( node.language() ).data() );
+                                     val.data(),
+                                     lang.data() );
         }
         else{
             JStringRef ns( node.toString() );
             JStringRef dtus( node.dataType().toEncoded() );
-            jobject dataTypeUri = callObjectMethod( d->IDcreateURI(), dtus.data() );
+            JObjectRef dataTypeUri = callObjectMethod( d->IDcreateURI(), dtus.data() \
);  return callObjectMethod( d->IDcreateLiteralWithDataType(),
                                      ns.data(),
-                                     dataTypeUri );
+                                     dataTypeUri.data() );
         }
 
     default:


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic