Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "SQLiteGlobal" |
| 18 | |
| 19 | #include <jni.h> |
| 20 | #include <JNIHelp.h> |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
| 23 | #include <sqlite3.h> |
| 24 | #include <sqlite3_android.h> |
| 25 | |
| 26 | #include "android_database_SQLiteCommon.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // Called each time a message is logged. |
| 31 | static void sqliteLogCallback(void* data, int iErrCode, const char* zMsg) { |
| 32 | bool verboseLog = !!data; |
| 33 | if (iErrCode == 0 || iErrCode == SQLITE_CONSTRAINT) { |
| 34 | if (verboseLog) { |
| 35 | ALOGV(LOG_VERBOSE, SQLITE_LOG_TAG, "(%d) %s\n", iErrCode, zMsg); |
| 36 | } |
| 37 | } else { |
| 38 | ALOG(LOG_ERROR, SQLITE_LOG_TAG, "(%d) %s\n", iErrCode, zMsg); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Sets the global SQLite configuration. |
| 43 | // This must be called before any other SQLite functions are called. */ |
| 44 | static void nativeConfig(JNIEnv* env, jclass clazz, jboolean verboseLog, jint softHeapLimit) { |
| 45 | // Enable multi-threaded mode. In this mode, SQLite is safe to use by multiple |
| 46 | // threads as long as no two threads use the same database connection at the same |
| 47 | // time (which we guarantee in the SQLite database wrappers). |
| 48 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 49 | |
| 50 | // Redirect SQLite log messages to the Android log. |
| 51 | sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, verboseLog ? (void*)1 : NULL); |
| 52 | |
| 53 | // The soft heap limit prevents the page cache allocations from growing |
| 54 | // beyond the given limit, no matter what the max page cache sizes are |
| 55 | // set to. The limit does not, as of 3.5.0, affect any other allocations. |
| 56 | sqlite3_soft_heap_limit(softHeapLimit); |
| 57 | } |
| 58 | |
| 59 | static jint nativeReleaseMemory(JNIEnv* env, jclass clazz, jint bytesToFree) { |
| 60 | return sqlite3_release_memory(bytesToFree); |
| 61 | } |
| 62 | |
| 63 | static JNINativeMethod sMethods[] = |
| 64 | { |
| 65 | /* name, signature, funcPtr */ |
| 66 | { "nativeConfig", "(ZI)V", |
| 67 | (void*)nativeConfig }, |
| 68 | { "nativeReleaseMemory", "(I)I", |
| 69 | (void*)nativeReleaseMemory }, |
| 70 | }; |
| 71 | |
| 72 | int register_android_database_SQLiteGlobal(JNIEnv *env) |
| 73 | { |
| 74 | return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteGlobal", |
| 75 | sMethods, NELEM(sMethods)); |
| 76 | } |
| 77 | |
| 78 | } // namespace android |