mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-29 08:01:23 +03:00
Start reworking JNI methods such that they pass void pointers from Java to C instead of passing their strongly-typed wrappers, as that is reportedly significantly faster than passing the wrapper objects to C and extracting the pointers there. There are still many, many functions left to rework for this.
FossilOrigin-Name: 66c814dd473731703ee00e1ff610acfdccf09d1b87dd2355795ed697d4ed5d3e
This commit is contained in:
@ -1441,26 +1441,9 @@ static void * NativePointerHolder__get(JNIEnv * env, jobject jNph,
|
||||
#define PtrGet_sqlite3_stmt(OBJ) PtrGet_T(sqlite3_stmt, OBJ)
|
||||
#define PtrGet_sqlite3_value(OBJ) PtrGet_T(sqlite3_value, OBJ)
|
||||
|
||||
#if 0
|
||||
/*
|
||||
** Enters the S3JniDb mutex and PtrGet_sqlite3()'s jObj. If that's
|
||||
** NULL then it leaves the mutex, else the mutex is still entered
|
||||
** when this returns and the caller is obligated to leave it.
|
||||
*/
|
||||
static sqlite3* PtrGet__sqlite3_lock(JNIEnv * const env, jobject jObj){
|
||||
sqlite3 *rv;
|
||||
S3JniDb_mutex_enter;
|
||||
rv = PtrGet_sqlite3(jObj);
|
||||
if( !rv ){ S3JniDb_mutex_leave; }
|
||||
return rv;
|
||||
}
|
||||
#undef PtrGet_sqlite3
|
||||
#define PtrGet_sqlite3(JOBJ) PtrGet__sqlite3_lock(env, (JOBJ))
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Extracts the new S3JniDb instance from the free-list, or allocates
|
||||
** one if needed, associats it with pDb, and returns. Returns NULL on
|
||||
** 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().
|
||||
*/
|
||||
@ -1475,7 +1458,7 @@ static S3JniDb * S3JniDb_alloc(JNIEnv * const env, jobject jDb){
|
||||
}
|
||||
S3JniDb_mutex_leave;
|
||||
if( 0==rv ){
|
||||
rv = s3jni_malloc( sizeof(S3JniDb));
|
||||
rv = s3jni_malloc(sizeof(S3JniDb));
|
||||
if( rv ){
|
||||
s3jni_incr( &SJG.metrics.nPdbAlloc );
|
||||
}
|
||||
@ -3408,13 +3391,12 @@ S3JniApi(sqlite3_extended_result_codes(),jboolean,1extended_1result_1codes)(
|
||||
}
|
||||
|
||||
S3JniApi(sqlite3_finalize(),jint,1finalize)(
|
||||
JniArgsEnvClass, jobject jpStmt
|
||||
JniArgsEnvClass, jlong jpStmt
|
||||
){
|
||||
int rc = 0;
|
||||
sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt);
|
||||
if( pStmt ){
|
||||
if( jpStmt ){
|
||||
sqlite3_stmt * const pStmt = (void*)jpStmt;
|
||||
rc = sqlite3_finalize(pStmt);
|
||||
NativePointerHolder_set(S3JniNph(sqlite3_stmt), jpStmt, 0);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
@ -3637,7 +3619,7 @@ S3JniApi(sqlite3_open_v2(),jint,1open_1v2)(
|
||||
|
||||
/* Proxy for the sqlite3_prepare[_v2/3]() family. */
|
||||
jint sqlite3_jni_prepare_v123( int prepVersion, JNIEnv * const env, jclass self,
|
||||
jobject jDb, jbyteArray baSql,
|
||||
jlong jpDb, jbyteArray baSql,
|
||||
jint nMax, jint prepFlags,
|
||||
jobject jOutStmt, jobject outTail){
|
||||
sqlite3_stmt * pStmt = 0;
|
||||
@ -3656,13 +3638,13 @@ jint sqlite3_jni_prepare_v123( int prepVersion, JNIEnv * const env, jclass self,
|
||||
goto end;
|
||||
}
|
||||
switch( prepVersion ){
|
||||
case 1: rc = sqlite3_prepare(PtrGet_sqlite3(jDb), (const char *)pBuf,
|
||||
case 1: rc = sqlite3_prepare((sqlite3*)jpDb, (const char *)pBuf,
|
||||
(int)nMax, &pStmt, &zTail);
|
||||
break;
|
||||
case 2: rc = sqlite3_prepare_v2(PtrGet_sqlite3(jDb), (const char *)pBuf,
|
||||
case 2: rc = sqlite3_prepare_v2((sqlite3*)jpDb, (const char *)pBuf,
|
||||
(int)nMax, &pStmt, &zTail);
|
||||
break;
|
||||
case 3: rc = sqlite3_prepare_v3(PtrGet_sqlite3(jDb), (const char *)pBuf,
|
||||
case 3: rc = sqlite3_prepare_v3((sqlite3*)jpDb, (const char *)pBuf,
|
||||
(int)nMax, (unsigned int)prepFlags,
|
||||
&pStmt, &zTail);
|
||||
break;
|
||||
@ -3697,24 +3679,24 @@ end:
|
||||
return (jint)rc;
|
||||
}
|
||||
S3JniApi(sqlite3_prepare(),jint,1prepare)(
|
||||
JNIEnv * const env, jclass self, jobject jDb, jbyteArray baSql,
|
||||
JNIEnv * const env, jclass self, jlong jpDb, jbyteArray baSql,
|
||||
jint nMax, jobject jOutStmt, jobject outTail
|
||||
){
|
||||
return sqlite3_jni_prepare_v123(1, env, self, jDb, baSql, nMax, 0,
|
||||
return sqlite3_jni_prepare_v123(1, env, self, jpDb, baSql, nMax, 0,
|
||||
jOutStmt, outTail);
|
||||
}
|
||||
S3JniApi(sqlite3_prepare_v2(),jint,1prepare_1v2)(
|
||||
JNIEnv * const env, jclass self, jobject jDb, jbyteArray baSql,
|
||||
JNIEnv * const env, jclass self, jlong jpDb, jbyteArray baSql,
|
||||
jint nMax, jobject jOutStmt, jobject outTail
|
||||
){
|
||||
return sqlite3_jni_prepare_v123(2, env, self, jDb, baSql, nMax, 0,
|
||||
return sqlite3_jni_prepare_v123(2, env, self, jpDb, baSql, nMax, 0,
|
||||
jOutStmt, outTail);
|
||||
}
|
||||
S3JniApi(sqlite3_prepare_v3(),jint,1prepare_1v3)(
|
||||
JNIEnv * const env, jclass self, jobject jDb, jbyteArray baSql,
|
||||
JNIEnv * const env, jclass self, jlong jpDb, jbyteArray baSql,
|
||||
jint nMax, jint prepFlags, jobject jOutStmt, jobject outTail
|
||||
){
|
||||
return sqlite3_jni_prepare_v123(3, env, self, jDb, baSql, nMax,
|
||||
return sqlite3_jni_prepare_v123(3, env, self, jpDb, baSql, nMax,
|
||||
prepFlags, jOutStmt, outTail);
|
||||
}
|
||||
|
||||
|
@ -1406,10 +1406,10 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1error_1offset
|
||||
/*
|
||||
* Class: org_sqlite_jni_SQLite3Jni
|
||||
* Method: sqlite3_finalize
|
||||
* Signature: (Lorg/sqlite/jni/sqlite3_stmt;)I
|
||||
* Signature: (J)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1finalize
|
||||
(JNIEnv *, jclass, jobject);
|
||||
(JNIEnv *, jclass, jlong);
|
||||
|
||||
/*
|
||||
* Class: org_sqlite_jni_SQLite3Jni
|
||||
@ -1510,26 +1510,26 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1open_1v2
|
||||
/*
|
||||
* Class: org_sqlite_jni_SQLite3Jni
|
||||
* Method: sqlite3_prepare
|
||||
* Signature: (Lorg/sqlite/jni/sqlite3;[BILorg/sqlite/jni/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/OutputPointer/Int32;)I
|
||||
* Signature: (J[BILorg/sqlite/jni/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/OutputPointer/Int32;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1prepare
|
||||
(JNIEnv *, jclass, jobject, jbyteArray, jint, jobject, jobject);
|
||||
(JNIEnv *, jclass, jlong, jbyteArray, jint, jobject, jobject);
|
||||
|
||||
/*
|
||||
* Class: org_sqlite_jni_SQLite3Jni
|
||||
* Method: sqlite3_prepare_v2
|
||||
* Signature: (Lorg/sqlite/jni/sqlite3;[BILorg/sqlite/jni/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/OutputPointer/Int32;)I
|
||||
* Signature: (J[BILorg/sqlite/jni/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/OutputPointer/Int32;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1prepare_1v2
|
||||
(JNIEnv *, jclass, jobject, jbyteArray, jint, jobject, jobject);
|
||||
(JNIEnv *, jclass, jlong, jbyteArray, jint, jobject, jobject);
|
||||
|
||||
/*
|
||||
* Class: org_sqlite_jni_SQLite3Jni
|
||||
* Method: sqlite3_prepare_v3
|
||||
* Signature: (Lorg/sqlite/jni/sqlite3;[BIILorg/sqlite/jni/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/OutputPointer/Int32;)I
|
||||
* Signature: (J[BIILorg/sqlite/jni/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/OutputPointer/Int32;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1prepare_1v3
|
||||
(JNIEnv *, jclass, jobject, jbyteArray, jint, jint, jobject, jobject);
|
||||
(JNIEnv *, jclass, jlong, jbyteArray, jint, jint, jobject, jobject);
|
||||
|
||||
/*
|
||||
* Class: org_sqlite_jni_SQLite3Jni
|
||||
|
@ -29,5 +29,18 @@ package org.sqlite.jni;
|
||||
public class NativePointerHolder<ContextType> {
|
||||
//! Only set from JNI, where access permissions don't matter.
|
||||
private volatile long nativePointer = 0;
|
||||
/**
|
||||
For use ONLY by package-level APIs which act as proxies for
|
||||
close/finalize operations. Such ops must call this to zero out
|
||||
the pointer so that this object is not carrying a stale
|
||||
pointer. This function returns the prior value of the pointer and
|
||||
sets it to 0.
|
||||
*/
|
||||
final long clearNativePointer() {
|
||||
final long rv = nativePointer;
|
||||
nativePointer= 0;
|
||||
return rv;
|
||||
}
|
||||
|
||||
public final long getNativePointer(){ return nativePointer; }
|
||||
}
|
||||
|
@ -738,7 +738,12 @@ public final class SQLite3Jni {
|
||||
public static native int sqlite3_error_offset(@NotNull sqlite3 db);
|
||||
|
||||
@Canonical
|
||||
public static native int sqlite3_finalize(@NotNull sqlite3_stmt stmt);
|
||||
private static native int sqlite3_finalize(long ptrToStmt);
|
||||
|
||||
@Canonical
|
||||
public static int sqlite3_finalize(@NotNull sqlite3_stmt stmt){
|
||||
return null==stmt ? 0 : sqlite3_finalize(stmt.clearNativePointer());
|
||||
}
|
||||
|
||||
@Canonical
|
||||
public static native int sqlite3_initialize();
|
||||
@ -850,7 +855,7 @@ public final class SQLite3Jni {
|
||||
*/
|
||||
@Canonical
|
||||
private static native int sqlite3_prepare(
|
||||
@NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int maxBytes,
|
||||
@NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes,
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt,
|
||||
@Nullable OutputPointer.Int32 pTailOffset
|
||||
);
|
||||
@ -869,14 +874,16 @@ public final class SQLite3Jni {
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt,
|
||||
@Nullable OutputPointer.Int32 pTailOffset
|
||||
){
|
||||
return sqlite3_prepare(db, sqlUtf8, sqlUtf8.length, outStmt, pTailOffset);
|
||||
return sqlite3_prepare(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
|
||||
outStmt, pTailOffset);
|
||||
}
|
||||
|
||||
public static int sqlite3_prepare(
|
||||
@NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt
|
||||
){
|
||||
return sqlite3_prepare(db, sqlUtf8, sqlUtf8.length, outStmt, null);
|
||||
return sqlite3_prepare(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
|
||||
outStmt, null);
|
||||
}
|
||||
|
||||
public static int sqlite3_prepare(
|
||||
@ -884,7 +891,8 @@ public final class SQLite3Jni {
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt
|
||||
){
|
||||
final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8);
|
||||
return sqlite3_prepare(db, utf8, utf8.length, outStmt, null);
|
||||
return sqlite3_prepare(db.getNativePointer(), utf8, utf8.length,
|
||||
outStmt, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -907,7 +915,7 @@ public final class SQLite3Jni {
|
||||
*/
|
||||
@Canonical
|
||||
private static native int sqlite3_prepare_v2(
|
||||
@NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int maxBytes,
|
||||
@NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes,
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt,
|
||||
@Nullable OutputPointer.Int32 pTailOffset
|
||||
);
|
||||
@ -923,14 +931,16 @@ public final class SQLite3Jni {
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt,
|
||||
@Nullable OutputPointer.Int32 pTailOffset
|
||||
){
|
||||
return sqlite3_prepare_v2(db, sqlUtf8, sqlUtf8.length, outStmt, pTailOffset);
|
||||
return sqlite3_prepare_v2(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
|
||||
outStmt, pTailOffset);
|
||||
}
|
||||
|
||||
public static int sqlite3_prepare_v2(
|
||||
@NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt
|
||||
){
|
||||
return sqlite3_prepare_v2(db, sqlUtf8, sqlUtf8.length, outStmt, null);
|
||||
return sqlite3_prepare_v2(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
|
||||
outStmt, null);
|
||||
}
|
||||
|
||||
public static int sqlite3_prepare_v2(
|
||||
@ -938,7 +948,8 @@ public final class SQLite3Jni {
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt
|
||||
){
|
||||
final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8);
|
||||
return sqlite3_prepare_v2(db, utf8, utf8.length, outStmt, null);
|
||||
return sqlite3_prepare_v2(db.getNativePointer(), utf8, utf8.length,
|
||||
outStmt, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -958,7 +969,7 @@ public final class SQLite3Jni {
|
||||
*/
|
||||
@Canonical
|
||||
private static native int sqlite3_prepare_v3(
|
||||
@NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int maxBytes,
|
||||
@NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes,
|
||||
int prepFlags, @NotNull OutputPointer.sqlite3_stmt outStmt,
|
||||
@Nullable OutputPointer.Int32 pTailOffset
|
||||
);
|
||||
@ -974,7 +985,8 @@ public final class SQLite3Jni {
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt,
|
||||
@Nullable OutputPointer.Int32 pTailOffset
|
||||
){
|
||||
return sqlite3_prepare_v3(db, sqlUtf8, sqlUtf8.length, prepFlags, outStmt, pTailOffset);
|
||||
return sqlite3_prepare_v3(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
|
||||
prepFlags, outStmt, pTailOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -985,7 +997,8 @@ public final class SQLite3Jni {
|
||||
@NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int prepFlags,
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt
|
||||
){
|
||||
return sqlite3_prepare_v3(db, sqlUtf8, sqlUtf8.length, prepFlags, outStmt, null);
|
||||
return sqlite3_prepare_v3(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
|
||||
prepFlags, outStmt, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -998,7 +1011,8 @@ public final class SQLite3Jni {
|
||||
@NotNull OutputPointer.sqlite3_stmt outStmt
|
||||
){
|
||||
final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8);
|
||||
return sqlite3_prepare_v3(db, utf8, utf8.length, prepFlags, outStmt, null);
|
||||
return sqlite3_prepare_v3(db.getNativePointer(), utf8, utf8.length,
|
||||
prepFlags, outStmt, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user