}
S3JniUnrefGlobal(s->jObj);
S3JniUnrefGlobal(s->jExtra);
+ }else{
+ assert( !s->jExtra );
}
*s = S3JniHook_empty;
}
/*
** Allocates one blank S3JniHook object from the recycling bin, if
-** available, else from the heap. Returns NULL or dies on OOM. Locks
-** the global mutex.
+** available, else from the heap. Returns NULL or dies on OOM,
+** depending on build options. Locks on SJG.hooks.mutex.
*/
static S3JniHook *S3JniHook__alloc(JNIEnv * const env){
S3JniHook * p = 0;
/*
** The rightful fate of all results from S3JniHook_alloc(). Locks on
-** SJG>hooks.mutex.
+** SJG.hook.mutex.
*/
static void S3JniHook__free(JNIEnv * const env, S3JniHook * const p){
if(p){
/*
** Uncache any state for the given JNIEnv, clearing all Java
** references the cache owns. Returns true if env was cached and false
-** if it was not found in the cache. Ownership of the given object is
-** passed over to this function, which makes it free for re-use.
+** if it was not found in the cache. Ownership of the S3JniEnv object
+** associated with the given argument is transferred to this function,
+** which makes it free for re-use.
**
-** Requires that the Env mutex be locked.
+** Requires that the env mutex be locked.
*/
static int S3JniEnv_uncache(JNIEnv * const env){
struct S3JniEnv * row;
}
}
if( !row ){
- return 0;
+ return 0;
}
if( pPrev) pPrev->pNext = row->pNext;
else{
** will work, despite the incorrect macro name, so long as the
** argument is a Java sqlite3 object, as this operation only has void
** pointers to work with.
+**
+** PtrCast_T(X,Y) variant expects X to be an unqualified sqlite3
+** struct type name and Y to be a native pointer to such an object in
+** the form of a jlong value. The jlong is simply cast to (X*). This
+** approach is, as of 2023-09-27, supplanting the former approach. We
+** now do the native pointer extraction in the Java side, rather than
+** the C side, because it's reportedly significantly faster.
*/
#define PtrGet_T(T,OBJ) (T*)NativePointerHolder_get(OBJ, S3JniNph(T))
#define PtrGet_sqlite3(OBJ) PtrGet_T(sqlite3, OBJ)
#define PtrGet_sqlite3_context(OBJ) PtrGet_T(sqlite3_context, OBJ)
#define PtrGet_sqlite3_stmt(OBJ) PtrGet_T(sqlite3_stmt, OBJ)
#define PtrGet_sqlite3_value(OBJ) PtrGet_T(sqlite3_value, OBJ)
+#define PtrCast_T(T,JLongPtr) (T*)(JLongPtr)
/*
** Extracts the new S3JniDb instance from the free-list, or allocates
-** one if needed, associates it with pDb, and returns. Returns NULL on
-** OOM. pDb MUST, on success of the calling operation, subsequently be
-** associated with jDb via NativePointerHolder_set().
+** one if needed, associates it with pDb, and returns. Returns NULL
+** on OOM. The returned object MUST, on success of the calling
+** operation, subsequently be associated with jDb via
+** NativePointerHolder_set() or freed using S3JniDb_set_aside().
*/
static S3JniDb * S3JniDb_alloc(JNIEnv * const env, jobject jDb){
S3JniDb * rv = 0;
}
S3JniApi(sqlite3_backup_finish(),jint,1backup_1finish)(
- JniArgsEnvClass, jobject jBack
+ JniArgsEnvClass, jlong jpBack
){
- sqlite3_backup * const pB = PtrGet_sqlite3_backup(jBack);
- NativePointerHolder_set(S3JniNph(sqlite3_backup), jBack, 0);
- return sqlite3_backup_finish(pB);
+ int rc = 0;
+ if( jpBack!=0 ){
+ rc = sqlite3_backup_finish( PtrCast_T(sqlite3_backup,jpBack) );
+ }
+ return rc;
}
S3JniApi(sqlite3_backup_init(),jobject,1backup_1init)(
- JniArgsEnvClass, jobject jDbDest, jstring jTDest,
- jobject jDbSrc, jstring jTSrc
+ JniArgsEnvClass, jlong jpDbDest, jstring jTDest,
+ jlong jpDbSrc, jstring jTSrc
){
- sqlite3 * const pDest = PtrGet_sqlite3(jDbDest);
- sqlite3 * const pSrc = PtrGet_sqlite3(jDbSrc);
+ sqlite3 * const pDest = PtrCast_T(sqlite3,jpDbDest);
+ sqlite3 * const pSrc = PtrCast_T(sqlite3,jpDbSrc);
char * const zDest = s3jni_jstring_to_utf8(jTDest, 0);
char * const zSrc = s3jni_jstring_to_utf8(jTSrc, 0);
jobject rv = 0;
}
S3JniApi(sqlite3_backup_pagecount(),jint,1backup_1pagecount)(
- JniArgsEnvClass, jobject jBack
+ JniArgsEnvClass, jlong jpBack
){
- return sqlite3_backup_pagecount(PtrGet_sqlite3_backup(jBack));
+ return sqlite3_backup_pagecount(PtrCast_T(sqlite3_backup,jpBack));
}
S3JniApi(sqlite3_backup_remaining(),jint,1backup_1remaining)(
- JniArgsEnvClass, jobject jBack
+ JniArgsEnvClass, jlong jpBack
){
- return sqlite3_backup_remaining(PtrGet_sqlite3_backup(jBack));
+ return sqlite3_backup_remaining(PtrCast_T(sqlite3_backup,jpBack));
}
S3JniApi(sqlite3_backup_step(),jint,1backup_1step)(
- JniArgsEnvClass, jobject jBack, jint nPage
+ JniArgsEnvClass, jlong jpBack, jint nPage
){
- return sqlite3_backup_step(PtrGet_sqlite3_backup(jBack), (int)nPage);
+ return sqlite3_backup_step(PtrCast_T(sqlite3_backup,jpBack), (int)nPage);
}
S3JniApi(sqlite3_bind_blob(),jint,1bind_1blob)(
S3JniApi(sqlite3_finalize(),jint,1finalize)(
JniArgsEnvClass, jlong jpStmt
){
- int rc = 0;
- if( jpStmt ){
- sqlite3_stmt * const pStmt = (void*)jpStmt;
- rc = sqlite3_finalize(pStmt);
- }
- return rc;
+ return jpStmt
+ ? sqlite3_finalize(PtrCast_T(sqlite3_stmt,jpStmt))
+ : 0;
}
S3JniApi(sqlite3_get_auxdata(),jobject,1get_1auxdata)(
goto end;
}
switch( prepVersion ){
- case 1: rc = sqlite3_prepare((sqlite3*)jpDb, (const char *)pBuf,
+ case 1: rc = sqlite3_prepare(PtrCast_T(sqlite3,jpDb), (const char *)pBuf,
(int)nMax, &pStmt, &zTail);
break;
- case 2: rc = sqlite3_prepare_v2((sqlite3*)jpDb, (const char *)pBuf,
+ case 2: rc = sqlite3_prepare_v2(PtrCast_T(sqlite3,jpDb), (const char *)pBuf,
(int)nMax, &pStmt, &zTail);
break;
- case 3: rc = sqlite3_prepare_v3((sqlite3*)jpDb, (const char *)pBuf,
+ case 3: rc = sqlite3_prepare_v3(PtrCast_T(sqlite3,jpDb), (const char *)pBuf,
(int)nMax, (unsigned int)prepFlags,
&pStmt, &zTail);
break;
default:
- assert(0 && "Invalid prepare() version");
+ assert(!"Invalid prepare() version");
}
end:
s3jni_jbyteArray_release(baSql,pBuf);
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_backup_finish
- * Signature: (Lorg/sqlite/jni/sqlite3_backup;)I
+ * Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1finish
- (JNIEnv *, jclass, jobject);
+ (JNIEnv *, jclass, jlong);
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_backup_init
- * Signature: (Lorg/sqlite/jni/sqlite3;Ljava/lang/String;Lorg/sqlite/jni/sqlite3;Ljava/lang/String;)Lorg/sqlite/jni/sqlite3_backup;
+ * Signature: (JLjava/lang/String;JLjava/lang/String;)Lorg/sqlite/jni/sqlite3_backup;
*/
JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1init
- (JNIEnv *, jclass, jobject, jstring, jobject, jstring);
+ (JNIEnv *, jclass, jlong, jstring, jlong, jstring);
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_backup_pagecount
- * Signature: (Lorg/sqlite/jni/sqlite3_backup;)I
+ * Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1pagecount
- (JNIEnv *, jclass, jobject);
+ (JNIEnv *, jclass, jlong);
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_backup_remaining
- * Signature: (Lorg/sqlite/jni/sqlite3_backup;)I
+ * Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1remaining
- (JNIEnv *, jclass, jobject);
+ (JNIEnv *, jclass, jlong);
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_backup_step
- * Signature: (Lorg/sqlite/jni/sqlite3_backup;I)I
+ * Signature: (JI)I
*/
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1step
- (JNIEnv *, jclass, jobject, jint);
+ (JNIEnv *, jclass, jlong, jint);
/*
* Class: org_sqlite_jni_SQLite3Jni
public static native int sqlite3_auto_extension(@NotNull AutoExtensionCallback callback);
@Canonical
- public static native int sqlite3_backup_finish(@NotNull sqlite3_backup b);
+ private static native int sqlite3_backup_finish(@NotNull long ptrToBackup);
@Canonical
- public static native sqlite3_backup sqlite3_backup_init(
+ public static int sqlite3_backup_finish(@NotNull sqlite3_backup b){
+ return sqlite3_backup_finish(b.clearNativePointer());
+ }
+
+ @Canonical
+ private static native sqlite3_backup sqlite3_backup_init(
+ @NotNull long ptrToDbDest, @NotNull String destTableName,
+ @NotNull long ptrToDbSrc, @NotNull String srcTableName
+ );
+
+ @Canonical
+ public static sqlite3_backup sqlite3_backup_init(
@NotNull sqlite3 dbDest, @NotNull String destTableName,
@NotNull sqlite3 dbSrc, @NotNull String srcTableName
- );
+ ){
+ return sqlite3_backup_init( dbDest.getNativePointer(), destTableName,
+ dbSrc.getNativePointer(), srcTableName );
+ }
@Canonical
- public static native int sqlite3_backup_pagecount(@NotNull sqlite3_backup b);
+ private static native int sqlite3_backup_pagecount(@NotNull long ptrToBackup);
@Canonical
- public static native int sqlite3_backup_remaining(@NotNull sqlite3_backup b);
+ public static int sqlite3_backup_pagecount(@NotNull sqlite3_backup b){
+ return sqlite3_backup_pagecount(b.getNativePointer());
+ }
@Canonical
- public static native int sqlite3_backup_step(
- @NotNull sqlite3_backup b, int nPage
- );
+ private static native int sqlite3_backup_remaining(@NotNull long ptrToBackup);
+
+ @Canonical
+ public static int sqlite3_backup_remaining(@NotNull sqlite3_backup b){
+ return sqlite3_backup_remaining(b.getNativePointer());
+ }
+
+ @Canonical
+ private static native int sqlite3_backup_step(@NotNull long ptrToBackup, int nPage);
+
+ @Canonical
+ public static int sqlite3_backup_step(@NotNull sqlite3_backup b, int nPage){
+ return sqlite3_backup_step(b.getNativePointer(), nPage);
+ }
/**
Results are undefined if data is not null and n<0 || n>=data.length.
}
private void testBackup(){
- final sqlite3 db1 = createNewDb();
- final sqlite3 db2 = createNewDb();
-
- execSql(db1, new String[]{
- "pragma page_size=512; VACUUM;",
- "create table t(a);",
- "insert into t(a) values(1),(2),(3);"
- });
- affirm( null==sqlite3_backup_init(db1,"main",db1,"main") );
- final sqlite3_backup b = sqlite3_backup_init(db2,"main",db1,"main");
- affirm( null!=b );
- affirm( b.getNativePointer()!=0 );
- int rc;
- while( SQLITE_DONE!=(rc = sqlite3_backup_step(b, 1)) ){
- affirm( 0==rc );
+ final sqlite3 dbDest = createNewDb();
+
+ try (sqlite3 dbSrc = createNewDb()) {
+ execSql(dbSrc, new String[]{
+ "pragma page_size=512; VACUUM;",
+ "create table t(a);",
+ "insert into t(a) values(1),(2),(3);"
+ });
+ affirm( null==sqlite3_backup_init(dbSrc,"main",dbSrc,"main") );
+ try (sqlite3_backup b = sqlite3_backup_init(dbDest,"main",dbSrc,"main")) {
+ affirm( null!=b );
+ affirm( b.getNativePointer()!=0 );
+ int rc;
+ while( SQLITE_DONE!=(rc = sqlite3_backup_step(b, 1)) ){
+ affirm( 0==rc );
+ }
+ affirm( sqlite3_backup_pagecount(b) > 0 );
+ rc = sqlite3_backup_finish(b);
+ affirm( 0==rc );
+ affirm( b.getNativePointer()==0 );
+ }
}
- affirm( sqlite3_backup_pagecount(b) > 0 );
- rc = sqlite3_backup_finish(b);
- affirm( 0==rc );
- affirm( b.getNativePointer()==0 );
-
- sqlite3_close_v2(db1);
-
- final sqlite3_stmt stmt = prepare(db2,"SELECT sum(a) from t");
- sqlite3_step(stmt);
- affirm( sqlite3_column_int(stmt,0) == 6 );
- sqlite3_finalize(stmt);
- sqlite3_close_v2(db2);
+ try (sqlite3_stmt stmt = prepare(dbDest,"SELECT sum(a) from t")) {
+ sqlite3_step(stmt);
+ affirm( sqlite3_column_int(stmt,0) == 6 );
+ }
+ sqlite3_close_v2(dbDest);
}
private void testRandomness(){
simply provide a type-safe way to communicate it between Java and C
via JNI.
*/
-public final class sqlite3_backup extends NativePointerHolder<sqlite3_backup> {
+public final class sqlite3_backup extends NativePointerHolder<sqlite3_backup>
+ implements AutoCloseable {
// Only invoked from JNI.
private sqlite3_backup(){}
+
+ @Override public void close(){
+ SQLite3Jni.sqlite3_backup_finish(this);
+ }
+
}
-C Start\sreworking\sJNI\smethods\ssuch\sthat\sthey\spass\svoid\spointers\sfrom\sJava\sto\sC\sinstead\sof\spassing\stheir\sstrongly-typed\swrappers,\sas\sthat\sis\sreportedly\ssignificantly\sfaster\sthan\spassing\sthe\swrapper\sobjects\sto\sC\sand\sextracting\sthe\spointers\sthere.\sThere\sare\sstill\smany,\smany\sfunctions\sleft\sto\srework\sfor\sthis.
-D 2023-09-26T21:37:52.408
+C Rework\sthe\sJNI\ssqlite3_backup\sAPIs\sto\sthe\snew\spointer-passing\sapproach.
+D 2023-09-27T09:58:36.928
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F ext/jni/GNUmakefile 42e00052401b6dd41c0cdd53b31450606ea37486283abdb038dff9be74bff71e
F ext/jni/README.md 9fceaeb17cecdc5d699dfc83c0cbc3a03fdb3b86bf676381894166c73375ee75
F ext/jni/jar-dist.make 030aaa4ae71dd86e4ec5e7c1e6cd86f9dfa47c4592c070d2e35157e42498e1fa
-F ext/jni/src/c/sqlite3-jni.c 14c1390d93ce1a0e48d402039fb37131cdc16ddd69d0762bdd0d05b229ea5570
-F ext/jni/src/c/sqlite3-jni.h 558b44b9dc5d88708ee6b43cac0cd61f836460f5de0458266ff25f7508ef92c7
+F ext/jni/src/c/sqlite3-jni.c 75006d6766e33f7a747db7339609f979732b2eb89586ca6ff6486c4cfed740fa
+F ext/jni/src/c/sqlite3-jni.h c5ae22c939f122fe8009fb69855951abe715058fe5575f78085608a587201c53
F ext/jni/src/org/sqlite/jni/AbstractCollationCallback.java 95e88ba04f4aac51ffec65693e878e234088b2f21b387f4e4285c8b72b33e436
F ext/jni/src/org/sqlite/jni/AggregateFunction.java 7312486bc65fecdb91753c0a4515799194e031f45edbe16a6373cea18f404dc4
F ext/jni/src/org/sqlite/jni/AuthorizerCallback.java e6135be32f12bf140bffa39be7fd1a45ad83b2661ed49c08dbde04c8485feb38
F ext/jni/src/org/sqlite/jni/ResultCode.java ba701f20213a5f259e94cfbfdd36eb7ac7ce7797f2c6c7fca2004ff12ce20f86
F ext/jni/src/org/sqlite/jni/RollbackHookCallback.java d12352c0e22840de484ffa9b11ed5058bb0daca2e9f218055d3c54c947a273c4
F ext/jni/src/org/sqlite/jni/SQLFunction.java 544a875d33fd160467d82e2397ac33157b29971d715a821a4fad3c899113ee8c
-F ext/jni/src/org/sqlite/jni/SQLite3Jni.java 732c9f55de35f3e3bc08a0187794473171ba07b8f886907d022ef2ffc9a813b5
+F ext/jni/src/org/sqlite/jni/SQLite3Jni.java 7d185104c1395587bf36f10492beda7d78d6686c3e43200e81f0c38b5fabdc2f
F ext/jni/src/org/sqlite/jni/ScalarFunction.java 6d387bb499fbe3bc13c53315335233dbf6a0c711e8fa7c521683219b041c614c
F ext/jni/src/org/sqlite/jni/TableColumnMetadata.java 54511b4297fa28dcb3f49b24035e34ced10e3fd44fd0e458e784f4d6b0096dab
-F ext/jni/src/org/sqlite/jni/Tester1.java 25be176398cf08cb9c0879ba33fd866cb8f579729dd4d44a4065421e1b899af4
+F ext/jni/src/org/sqlite/jni/Tester1.java 720e1efddd769d5785e95100ff48aa203f2288eea865326a1a81fd5af43ec3a5
F ext/jni/src/org/sqlite/jni/TraceV2Callback.java beb0b064c1a5f8bfe585a324ed39a4e33edbe379a3fc60f1401661620d3ca7c0
F ext/jni/src/org/sqlite/jni/UpdateHookCallback.java 8376f4a931f2d5612b295c003c9515ba933ee76d8f95610e89c339727376e36c
F ext/jni/src/org/sqlite/jni/WindowFunction.java 488980f4dbb6bdd7067d6cb9c43e4075475e51c54d9b74a5834422654b126246
F ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java ea993738b851038c16d98576abd0db3d6028a231f075a394fb8a78c7834d0f6c
F ext/jni/src/org/sqlite/jni/package-info.java a3946db2504de747a1993c4f6e8ce604bec5a8e5a134b292c3b07527bc321a99
F ext/jni/src/org/sqlite/jni/sqlite3.java 5e56a799ced58ea69e8bcad20c29a61fce32f21a7f4fb3e2702337cf8aa1a06e
-F ext/jni/src/org/sqlite/jni/sqlite3_backup.java d0bb06dd6225e76999ff6b7ab20f2643b1c4d4167431b3a93ea41943e41f094b
+F ext/jni/src/org/sqlite/jni/sqlite3_backup.java 12182124c4d4928d78db5a07ea285f1d7af04c7a148f0759a1972d5bfa87311e
F ext/jni/src/org/sqlite/jni/sqlite3_blob.java f28a30134f2e524eb7d5ab87f57f86c90140341a6e8369ee54509ac8bb96fa82
F ext/jni/src/org/sqlite/jni/sqlite3_context.java 66ca95ce904044263a4aff684abe262d56f73e6b06bca6cf650761d79d7779ad
F ext/jni/src/org/sqlite/jni/sqlite3_stmt.java 36ecee9bdde2e70c7276d1c22b48fd9c40a77412e8e0694e9c09910997d6fb4f
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 5a39a02d2dfd9ed6171cd0bd434b2bc268d0ed8ead6e1a396d1603266d9493ef
-R 47a30e26f3996fa866a87a8a39a6842f
-T *branch * jni-ptr-passing
-T *sym-jni-ptr-passing *
-T -sym-trunk * Cancelled\sby\sbranch.
+P 66c814dd473731703ee00e1ff610acfdccf09d1b87dd2355795ed697d4ed5d3e
+R f061d70c56ee00aa5b569b2ec65f25b0
U stephan
-Z 219c3daa73370ae53cc699c76c54d2c3
+Z 06b4b7e93a4be23fe9e37ef080dd1dde
# Remove this line to create a well-formed Fossil manifest.
-66c814dd473731703ee00e1ff610acfdccf09d1b87dd2355795ed697d4ed5d3e
\ No newline at end of file
+48aeb1e2cdeb4aec85c4f63a1f809215cd4b473791169e72b5ddf6d6bdc2f7b7
\ No newline at end of file