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> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 21 | #include "core_jni_helpers.h" |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 22 | |
| 23 | #include <sqlite3.h> |
| 24 | #include <sqlite3_android.h> |
| 25 | |
| 26 | #include "android_database_SQLiteCommon.h" |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 27 | #include "android_util_Log.h" |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 31 | // Limit heap to 8MB for now. This is 4 times the maximum cursor window |
| 32 | // size, as has been used by the original code in SQLiteDatabase for |
| 33 | // a long time. |
| 34 | static const int SOFT_HEAP_LIMIT = 8 * 1024 * 1024; |
| 35 | |
| 36 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 37 | // Called each time a message is logged. |
Jeff Brown | 5dd67d6 | 2015-03-16 15:21:45 -0700 | [diff] [blame] | 38 | static void sqliteLogCallback(void* data, int err, const char* msg) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 39 | bool verboseLog = !!data; |
Jeff Brown | 5dd67d6 | 2015-03-16 15:21:45 -0700 | [diff] [blame] | 40 | int errType = err & 255; |
| 41 | if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA |
| 42 | || errType == SQLITE_NOTICE || err == SQLITE_WARNING_AUTOINDEX) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 43 | if (verboseLog) { |
Jeff Brown | 5dd67d6 | 2015-03-16 15:21:45 -0700 | [diff] [blame] | 44 | ALOG(LOG_VERBOSE, SQLITE_LOG_TAG, "(%d) %s\n", err, msg); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 45 | } |
Jeff Brown | 5dd67d6 | 2015-03-16 15:21:45 -0700 | [diff] [blame] | 46 | } else if (errType == SQLITE_WARNING) { |
| 47 | ALOG(LOG_WARN, SQLITE_LOG_TAG, "(%d) %s\n", err, msg); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 48 | } else { |
Jeff Brown | 5dd67d6 | 2015-03-16 15:21:45 -0700 | [diff] [blame] | 49 | ALOG(LOG_ERROR, SQLITE_LOG_TAG, "(%d) %s\n", err, msg); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | // Sets the global SQLite configuration. |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 54 | // This must be called before any other SQLite functions are called. |
| 55 | static void sqliteInitialize() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 56 | // Enable multi-threaded mode. In this mode, SQLite is safe to use by multiple |
| 57 | // threads as long as no two threads use the same database connection at the same |
| 58 | // time (which we guarantee in the SQLite database wrappers). |
| 59 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 60 | |
| 61 | // Redirect SQLite log messages to the Android log. |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 62 | bool verboseLog = android_util_Log_isVerboseLogEnabled(SQLITE_LOG_TAG); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 63 | sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, verboseLog ? (void*)1 : NULL); |
| 64 | |
| 65 | // The soft heap limit prevents the page cache allocations from growing |
| 66 | // beyond the given limit, no matter what the max page cache sizes are |
| 67 | // set to. The limit does not, as of 3.5.0, affect any other allocations. |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 68 | sqlite3_soft_heap_limit(SOFT_HEAP_LIMIT); |
| 69 | |
| 70 | // Initialize SQLite. |
| 71 | sqlite3_initialize(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 74 | static jint nativeReleaseMemory(JNIEnv* env, jclass clazz) { |
| 75 | return sqlite3_release_memory(SOFT_HEAP_LIMIT); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 78 | static const JNINativeMethod sMethods[] = |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 79 | { |
| 80 | /* name, signature, funcPtr */ |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 81 | { "nativeReleaseMemory", "()I", (void*)nativeReleaseMemory }, |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | int register_android_database_SQLiteGlobal(JNIEnv *env) |
| 85 | { |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 86 | sqliteInitialize(); |
| 87 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 88 | return RegisterMethodsOrDie(env, "android/database/sqlite/SQLiteGlobal", |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 89 | sMethods, NELEM(sMethods)); |
| 90 | } |
| 91 | |
| 92 | } // namespace android |