Favor nullptr in JNI code.

This commit is contained in:
A.J. Beamon 2019-01-10 13:13:21 -08:00
parent bfa97d7ff2
commit 8451c0cfc1
1 changed files with 45 additions and 45 deletions

View File

@ -25,7 +25,7 @@
#include <foundationdb/fdb_c.h>
#define JNI_NULL 0
#define JNI_NULL nullptr
#if defined(__GNUG__)
#define thread_local __thread
@ -40,13 +40,13 @@
static JavaVM* g_jvm = nullptr;
static thread_local JNIEnv* g_thread_jenv = nullptr; // Defined for the network thread once it is running, and for any thread that has called registerCallback
static thread_local jmethodID g_IFutureCallback_call_methodID = 0;
static thread_local jmethodID g_IFutureCallback_call_methodID = JNI_NULL;
static thread_local bool is_external = false;
void detachIfExternalThread(void *ignore) {
if(is_external && g_thread_jenv != 0) {
if(is_external && g_thread_jenv != nullptr) {
g_thread_jenv = nullptr;
g_IFutureCallback_call_methodID = 0;
g_IFutureCallback_call_methodID = JNI_NULL;
g_jvm->DetachCurrentThread();
}
}
@ -58,7 +58,7 @@ void throwOutOfMem(JNIEnv *jenv) {
if(jenv->ExceptionOccurred())
return;
if( jenv->ThrowNew( illegalArgClass, NULL ) != 0 ) {
if( jenv->ThrowNew( illegalArgClass, nullptr ) != 0 ) {
if( !jenv->ExceptionOccurred() ) {
jenv->FatalError("Could not throw OutOfMemoryError");
} else {
@ -68,7 +68,7 @@ void throwOutOfMem(JNIEnv *jenv) {
}
}
static jthrowable getThrowable(JNIEnv *jenv, fdb_error_t e, const char* msg = NULL) {
static jthrowable getThrowable(JNIEnv *jenv, fdb_error_t e, const char* msg = nullptr) {
jclass excepClass = jenv->FindClass("com/apple/foundationdb/FDBException");
if(jenv->ExceptionOccurred())
return JNI_NULL;
@ -128,11 +128,11 @@ static bool findCallbackMethods(JNIEnv *jenv) {
}
static void callCallback( FDBFuture* f, void* data ) {
if (g_thread_jenv == 0) {
if (g_thread_jenv == nullptr) {
// We are on an external thread and must attach to the JVM.
// The shutdown hook will later detach this thread.
is_external = true;
if( g_jvm != 0 && g_jvm->AttachCurrentThreadAsDaemon((void **) &g_thread_jenv, JNI_NULL) == JNI_OK ) {
if( g_jvm != nullptr && g_jvm->AttachCurrentThreadAsDaemon((void **) &g_thread_jenv, nullptr) == JNI_OK ) {
if( !findCallbackMethods( g_thread_jenv ) ) {
g_thread_jenv->FatalError("FDB: Could not find callback method.\n");
}
@ -169,9 +169,9 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_NativeFuture_Future_1register
}
FDBFuture *f = (FDBFuture *)future;
// This is documented as not throwing, but simply returning NULL on OMM.
// This is documented as not throwing, but simply returning null on OOM.
// As belt and suspenders, we will check for pending exceptions and then,
// if there are none and the result is NULL, we'll throw our own OMM.
// if there are none and the result is null, we'll throw our own OOM.
callback = jenv->NewGlobalRef( callback );
if( !callback ) {
if( !jenv->ExceptionOccurred() )
@ -280,7 +280,7 @@ JNIEXPORT jobject JNICALL Java_com_apple_foundationdb_FutureStrings_FutureString
jclass str_clazz = jenv->FindClass("java/lang/String");
if( jenv->ExceptionOccurred() )
return JNI_NULL;
jobjectArray arr = jenv->NewObjectArray(count, str_clazz, NULL);
jobjectArray arr = jenv->NewObjectArray(count, str_clazz, JNI_NULL);
if( !arr ) {
if( !jenv->ExceptionOccurred() )
throwOutOfMem(jenv);
@ -327,7 +327,7 @@ JNIEXPORT jobject JNICALL Java_com_apple_foundationdb_FutureResults_FutureResult
return JNI_NULL;
}
jbyteArray lastKey = NULL;
jbyteArray lastKey = JNI_NULL;
if(count) {
lastKey = jenv->NewByteArray(kvs[count - 1].key_length);
if( !lastKey ) {
@ -378,7 +378,7 @@ JNIEXPORT jobject JNICALL Java_com_apple_foundationdb_FutureResults_FutureResult
throwOutOfMem(jenv);
return JNI_NULL;
}
uint8_t *keyvalues_barr = (uint8_t *)jenv->GetByteArrayElements(keyValueArray, NULL);
uint8_t *keyvalues_barr = (uint8_t *)jenv->GetByteArrayElements(keyValueArray, JNI_NULL);
if (!keyvalues_barr) {
throwRuntimeEx( jenv, "Error getting handle to native resources" );
return JNI_NULL;
@ -393,7 +393,7 @@ JNIEXPORT jobject JNICALL Java_com_apple_foundationdb_FutureResults_FutureResult
return JNI_NULL;
}
jint *length_barr = jenv->GetIntArrayElements(lengthArray, NULL);
jint *length_barr = jenv->GetIntArrayElements(lengthArray, JNI_NULL);
if( !length_barr ) {
if( !jenv->ExceptionOccurred() )
throwOutOfMem(jenv);
@ -509,11 +509,11 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBDatabase_Database_1setOpti
return;
}
FDBDatabase *c = (FDBDatabase *)dPtr;
uint8_t *barr = NULL;
uint8_t *barr = nullptr;
int size = 0;
if(value != 0) {
barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL );
if(value != JNI_NULL) {
barr = (uint8_t *)jenv->GetByteArrayElements( value, JNI_NULL );
if (!barr) {
throwRuntimeEx( jenv, "Error getting handle to native resources" );
return;
@ -521,7 +521,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBDatabase_Database_1setOpti
size = jenv->GetArrayLength( value );
}
fdb_error_t err = fdb_database_set_option( c, (FDBDatabaseOption)code, barr, size );
if(value != 0)
if(value != JNI_NULL)
jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT );
if( err ) {
safeThrow( jenv, getThrowable( jenv, err ) );
@ -534,8 +534,8 @@ JNIEXPORT jboolean JNICALL Java_com_apple_foundationdb_FDB_Error_1predicate(JNIE
JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDB_Database_1create(JNIEnv *jenv, jobject, jstring clusterFileName) {
const char* fileName = nullptr;
if(clusterFileName != 0) {
fileName = jenv->GetStringUTFChars(clusterFileName, 0);
if(clusterFileName != JNI_NULL) {
fileName = jenv->GetStringUTFChars(clusterFileName, JNI_NULL);
if(jenv->ExceptionOccurred()) {
return 0;
}
@ -544,7 +544,7 @@ JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDB_Database_1create(JNIEnv
FDBDatabase *db;
fdb_error_t err = fdb_create_database(fileName, &db);
if(clusterFileName != 0) {
if(clusterFileName != JNI_NULL) {
jenv->ReleaseStringUTFChars(clusterFileName, fileName);
}
@ -582,7 +582,7 @@ JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL );
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, JNI_NULL );
if(!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -602,7 +602,7 @@ JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL );
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, JNI_NULL );
if(!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -624,14 +624,14 @@ JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barrBegin = (uint8_t *)jenv->GetByteArrayElements( keyBeginBytes, NULL );
uint8_t *barrBegin = (uint8_t *)jenv->GetByteArrayElements( keyBeginBytes, JNI_NULL );
if (!barrBegin) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
return 0;
}
uint8_t *barrEnd = (uint8_t *)jenv->GetByteArrayElements( keyEndBytes, NULL );
uint8_t *barrEnd = (uint8_t *)jenv->GetByteArrayElements( keyEndBytes, JNI_NULL );
if (!barrEnd) {
jenv->ReleaseByteArrayElements( keyBeginBytes, (jbyte *)barrBegin, JNI_ABORT );
if( !jenv->ExceptionOccurred() )
@ -655,14 +655,14 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1s
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barrKey = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL );
uint8_t *barrKey = (uint8_t *)jenv->GetByteArrayElements( keyBytes, JNI_NULL );
if (!barrKey) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
return;
}
uint8_t *barrValue = (uint8_t *)jenv->GetByteArrayElements( valueBytes, NULL );
uint8_t *barrValue = (uint8_t *)jenv->GetByteArrayElements( valueBytes, JNI_NULL );
if (!barrValue) {
jenv->ReleaseByteArrayElements( keyBytes, (jbyte *)barrKey, JNI_ABORT );
if( !jenv->ExceptionOccurred() )
@ -684,7 +684,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1c
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL );
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, JNI_NULL );
if (!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -702,14 +702,14 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1c
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barrKeyBegin = (uint8_t *)jenv->GetByteArrayElements( keyBeginBytes, NULL );
uint8_t *barrKeyBegin = (uint8_t *)jenv->GetByteArrayElements( keyBeginBytes, JNI_NULL );
if (!barrKeyBegin) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
return;
}
uint8_t *barrKeyEnd = (uint8_t *)jenv->GetByteArrayElements( keyEndBytes, NULL );
uint8_t *barrKeyEnd = (uint8_t *)jenv->GetByteArrayElements( keyEndBytes, JNI_NULL );
if (!barrKeyEnd) {
jenv->ReleaseByteArrayElements( keyBeginBytes, (jbyte *)barrKeyBegin, JNI_ABORT );
if( !jenv->ExceptionOccurred() )
@ -732,14 +732,14 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1m
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barrKey = (uint8_t *)jenv->GetByteArrayElements( key, NULL );
uint8_t *barrKey = (uint8_t *)jenv->GetByteArrayElements( key, JNI_NULL );
if (!barrKey) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
return;
}
uint8_t *barrValue = (uint8_t *)jenv->GetByteArrayElements( value, NULL );
uint8_t *barrValue = (uint8_t *)jenv->GetByteArrayElements( value, JNI_NULL );
if (!barrValue) {
jenv->ReleaseByteArrayElements( key, (jbyte *)barrKey, JNI_ABORT );
if( !jenv->ExceptionOccurred() )
@ -772,11 +772,11 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1s
return;
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barr = NULL;
uint8_t *barr = nullptr;
int size = 0;
if(value != 0) {
barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL );
if(value != JNI_NULL) {
barr = (uint8_t *)jenv->GetByteArrayElements( value, JNI_NULL );
if (!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -785,7 +785,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1s
size = jenv->GetArrayLength( value );
}
fdb_error_t err = fdb_transaction_set_option( tr, (FDBTransactionOption)code, barr, size );
if(value != 0)
if(value != JNI_NULL)
jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT );
if( err ) {
safeThrow( jenv, getThrowable( jenv, err ) );
@ -824,7 +824,7 @@ JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( key, NULL );
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( key, JNI_NULL );
if (!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -871,7 +871,7 @@ JNIEXPORT jlong JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( key, NULL );
uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( key, JNI_NULL );
if (!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -900,7 +900,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1a
}
FDBTransaction *tr = (FDBTransaction *)tPtr;
uint8_t *begin_barr = (uint8_t *)jenv->GetByteArrayElements( keyBegin, NULL );
uint8_t *begin_barr = (uint8_t *)jenv->GetByteArrayElements( keyBegin, JNI_NULL );
if (!begin_barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -908,7 +908,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDBTransaction_Transaction_1a
}
int begin_size = jenv->GetArrayLength( keyBegin );
uint8_t *end_barr = (uint8_t *)jenv->GetByteArrayElements( keyEnd, NULL );
uint8_t *end_barr = (uint8_t *)jenv->GetByteArrayElements( keyEnd, JNI_NULL );
if (!end_barr) {
jenv->ReleaseByteArrayElements( keyBegin, (jbyte *)begin_barr, JNI_ABORT );
if( !jenv->ExceptionOccurred() )
@ -953,10 +953,10 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDB_Select_1API_1version(JNIE
}
JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDB_Network_1setOption(JNIEnv *jenv, jobject, jint code, jbyteArray value) {
uint8_t *barr = NULL;
uint8_t *barr = nullptr;
int size = 0;
if(value != 0) {
barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL );
if(value != JNI_NULL) {
barr = (uint8_t *)jenv->GetByteArrayElements( value, JNI_NULL );
if (!barr) {
if( !jenv->ExceptionOccurred() )
throwRuntimeEx( jenv, "Error getting handle to native resources" );
@ -965,7 +965,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDB_Network_1setOption(JNIEnv
size = jenv->GetArrayLength( value );
}
fdb_error_t err = fdb_network_set_option((FDBNetworkOption)code, barr, size);
if(value != 0)
if(value != JNI_NULL)
jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT );
if( err ) {
safeThrow( jenv, getThrowable( jenv, err ) );
@ -987,7 +987,7 @@ JNIEXPORT void JNICALL Java_com_apple_foundationdb_FDB_Network_1run(JNIEnv *jenv
return;
}
fdb_error_t hookErr = fdb_add_network_thread_completion_hook( &detachIfExternalThread, NULL );
fdb_error_t hookErr = fdb_add_network_thread_completion_hook( &detachIfExternalThread, nullptr );
if( hookErr ) {
safeThrow( jenv, getThrowable( jenv, hookErr ) );
}