blob: acc2276ef9d1616e777c8019f3647d2c5b67daaa [file] [log] [blame]
Jeff Browne5360fb2011-10-31 17:48:13 -07001/*
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"
Jeff Brown4b575532012-01-20 12:34:34 -080027#include "android_util_Log.h"
Jeff Browne5360fb2011-10-31 17:48:13 -070028
29namespace android {
30
Jeff Brown4b575532012-01-20 12:34:34 -080031// 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.
34static const int SOFT_HEAP_LIMIT = 8 * 1024 * 1024;
35
36
Jeff Browne5360fb2011-10-31 17:48:13 -070037// Called each time a message is logged.
38static void sqliteLogCallback(void* data, int iErrCode, const char* zMsg) {
39 bool verboseLog = !!data;
Jeff Brown08cbdad2012-03-05 10:30:06 -080040 if (iErrCode == 0 || iErrCode == SQLITE_CONSTRAINT || iErrCode == SQLITE_SCHEMA) {
Jeff Browne5360fb2011-10-31 17:48:13 -070041 if (verboseLog) {
42 ALOGV(LOG_VERBOSE, SQLITE_LOG_TAG, "(%d) %s\n", iErrCode, zMsg);
43 }
44 } else {
45 ALOG(LOG_ERROR, SQLITE_LOG_TAG, "(%d) %s\n", iErrCode, zMsg);
46 }
47}
48
49// Sets the global SQLite configuration.
Jeff Brown4b575532012-01-20 12:34:34 -080050// This must be called before any other SQLite functions are called.
51static void sqliteInitialize() {
Jeff Browne5360fb2011-10-31 17:48:13 -070052 // Enable multi-threaded mode. In this mode, SQLite is safe to use by multiple
53 // threads as long as no two threads use the same database connection at the same
54 // time (which we guarantee in the SQLite database wrappers).
55 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
56
57 // Redirect SQLite log messages to the Android log.
Jeff Brown4b575532012-01-20 12:34:34 -080058 bool verboseLog = android_util_Log_isVerboseLogEnabled(SQLITE_LOG_TAG);
Jeff Browne5360fb2011-10-31 17:48:13 -070059 sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, verboseLog ? (void*)1 : NULL);
60
61 // The soft heap limit prevents the page cache allocations from growing
62 // beyond the given limit, no matter what the max page cache sizes are
63 // set to. The limit does not, as of 3.5.0, affect any other allocations.
Jeff Brown4b575532012-01-20 12:34:34 -080064 sqlite3_soft_heap_limit(SOFT_HEAP_LIMIT);
65
66 // Initialize SQLite.
67 sqlite3_initialize();
Jeff Browne5360fb2011-10-31 17:48:13 -070068}
69
Jeff Brown4b575532012-01-20 12:34:34 -080070static jint nativeReleaseMemory(JNIEnv* env, jclass clazz) {
71 return sqlite3_release_memory(SOFT_HEAP_LIMIT);
Jeff Browne5360fb2011-10-31 17:48:13 -070072}
73
74static JNINativeMethod sMethods[] =
75{
76 /* name, signature, funcPtr */
Jeff Brown4b575532012-01-20 12:34:34 -080077 { "nativeReleaseMemory", "()I",
Jeff Browne5360fb2011-10-31 17:48:13 -070078 (void*)nativeReleaseMemory },
79};
80
81int register_android_database_SQLiteGlobal(JNIEnv *env)
82{
Jeff Brown4b575532012-01-20 12:34:34 -080083 sqliteInitialize();
84
Jeff Browne5360fb2011-10-31 17:48:13 -070085 return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteGlobal",
86 sMethods, NELEM(sMethods));
87}
88
89} // namespace android