aboutsummaryrefslogtreecommitdiff
path: root/nativeruntime
diff options
context:
space:
mode:
Diffstat (limited to 'nativeruntime')
-rw-r--r--nativeruntime/build.gradle198
-rw-r--r--nativeruntime/cpp/CMakeLists.txt186
-rw-r--r--nativeruntime/cpp/androidfw/CMakeLists.txt11
-rw-r--r--nativeruntime/cpp/androidfw/CursorWindow.cpp431
-rw-r--r--nativeruntime/cpp/androidfw/include/androidfw/CursorWindow.h215
-rw-r--r--nativeruntime/cpp/base/include/android-base/macros.h155
-rw-r--r--nativeruntime/cpp/jni/AndroidRuntime.cpp47
-rw-r--r--nativeruntime/cpp/jni/AndroidRuntime.h41
-rw-r--r--nativeruntime/cpp/jni/JNIMain.cpp62
-rw-r--r--nativeruntime/cpp/jni/robo_android_database_CursorWindow.cpp628
-rw-r--r--nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.cpp251
-rw-r--r--nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.h54
-rw-r--r--nativeruntime/cpp/jni/robo_android_database_SQLiteConnection.cpp1068
-rw-r--r--nativeruntime/cpp/libcutils/CMakeLists.txt8
-rw-r--r--nativeruntime/cpp/libcutils/ashmem.cpp98
-rw-r--r--nativeruntime/cpp/libcutils/include/cutils/ashmem.h37
-rw-r--r--nativeruntime/cpp/liblog/CMakeLists.txt5
-rw-r--r--nativeruntime/cpp/liblog/include/log/log.h78
-rw-r--r--nativeruntime/cpp/liblog/log.c35
-rw-r--r--nativeruntime/cpp/libnativehelper/include/nativehelper/JNIHelp.h552
-rw-r--r--nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_local_ref.h85
-rw-r--r--nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_utf8_chars.h93
-rw-r--r--nativeruntime/cpp/libutils/CMakeLists.txt17
-rw-r--r--nativeruntime/cpp/libutils/SharedBuffer.cpp135
-rw-r--r--nativeruntime/cpp/libutils/SharedBuffer.h145
-rw-r--r--nativeruntime/cpp/libutils/String16.cpp450
-rw-r--r--nativeruntime/cpp/libutils/String8.cpp575
-rw-r--r--nativeruntime/cpp/libutils/Unicode.cpp561
-rw-r--r--nativeruntime/cpp/libutils/include/utils/Compat.h104
-rw-r--r--nativeruntime/cpp/libutils/include/utils/Errors.h93
-rw-r--r--nativeruntime/cpp/libutils/include/utils/String16.h331
-rw-r--r--nativeruntime/cpp/libutils/include/utils/String8.h371
-rw-r--r--nativeruntime/cpp/libutils/include/utils/TypeHelpers.h370
-rw-r--r--nativeruntime/cpp/libutils/include/utils/Unicode.h170
m---------nativeruntime/external/icu0
m---------nativeruntime/external/sqlite0
36 files changed, 40 insertions, 7620 deletions
diff --git a/nativeruntime/build.gradle b/nativeruntime/build.gradle
index 495bf65f4..1ef93175a 100644
--- a/nativeruntime/build.gradle
+++ b/nativeruntime/build.gradle
@@ -6,178 +6,58 @@ import org.robolectric.gradle.RoboJavaModulePlugin
apply plugin: RoboJavaModulePlugin
apply plugin: DeployedRoboJavaModulePlugin
-static def osName() {
- def osName = System.getProperty("os.name").toLowerCase(Locale.US);
- if (osName.contains("linux")) {
- return "linux"
- } else if (osName.contains("mac")) {
- return "mac"
- } else if (osName.contains("win")) {
- return "windows"
- }
- return "unknown"
-}
-
-static def arch() {
- def arch = System.getProperty("os.arch").toLowerCase(Locale.US);
- if (arch.equals("x86_64") || arch.equals("amd64")) {
- return "x86_64"
- }
- return arch
-}
+if (System.getenv('PUBLISH_NATIVERUNTIME_DIST_COMPAT') == "true") {
+ apply plugin: 'maven-publish'
+ apply plugin: "signing"
-static def authHeader() {
- def user = System.getenv('GITHUB_USER')
- if (!user) {
- throw new GradleException("Missing GITHUB_USER environment variable")
- }
- def token = System.getenv('GITHUB_TOKEN')
- if (!token) {
- throw new GradleException("Missing GITHUB_TOKEN environment variable")
- }
- def lp = "$user:$token"
- def encoded = Base64.getEncoder().encodeToString(lp.getBytes(StandardCharsets.UTF_8))
- return "Basic $encoded"
-}
-
-task cmakeNativeRuntime {
- doLast {
- mkdir "$buildDir/cpp"
- exec {
- workingDir "$buildDir/cpp"
- commandLine 'cmake', "-B", ".", "-S","$projectDir/cpp/", "-G", "Ninja"
- }
- }
-}
-
-task configureICU {
- onlyIf { !System.getenv('ICU_ROOT_DIR') }
- doLast {
- def os = osName()
- if (!file("$projectDir/external/icu/icu4c/source").exists()) {
- throw new GradleException("ICU submodule not detected. Please run `git submodule update --init`")
- }
- if (file("$projectDir/external/icu/icu4c/source/Makefile").exists()) {
- println("ICU Makefile detected, skipping ICU configure")
- } else {
- exec {
- workingDir "$projectDir/external/icu/icu4c/source"
- if (os.contains("linux")) {
- environment "CFLAGS", "-fPIC"
- environment "CXXFLAGS", "-fPIC"
- commandLine './runConfigureICU', 'Linux', '--enable-static', '--disable-shared'
- } else if (os.contains("mac")) {
- commandLine './runConfigureICU', 'MacOSX', '--enable-static', '--disable-shared'
- } else if (os.contains("win")) {
- commandLine 'sh', './runConfigureICU', 'MinGW', '--enable-static', '--disable-shared'
- } else {
- println("ICU configure not supported for OS '${System.getProperty("os.name")}'")
- }
- }
- }
- }
-}
+ publishing {
+ publications {
+ nativeRuntimeDist(MavenPublication) {
+ artifact System.env["NATIVERUNTIME_DIST_COMPAT_JAR"]
+ artifactId 'nativeruntime-dist-compat'
+ version System.env["NATIVERUNTIME_DIST_COMPAT_VERSION"]
-task buildICU {
- onlyIf { !System.getenv('ICU_ROOT_DIR') }
- dependsOn configureICU
- doLast {
- exec {
- def os = osName()
- if (os.contains("linux") || os.contains("mac") || os.contains("win")) {
- workingDir "$projectDir/external/icu/icu4c/source"
- commandLine 'make', '-j4'
- }
- }
- }
-}
+ pom {
+ name = "Robolectric Nativeruntime Distribution Compat"
+ description = "Robolectric Nativeruntime Distribution Compat"
+ url = "https://source.android.com/"
+ inceptionYear = "2008"
+ licenses {
+ license {
+ name = "Apache 2.0"
+ url = "http://www.apache.org/licenses/LICENSE-2.0"
+ comments = "While the EULA for the Android SDK restricts distribution of those binaries, the source code is licensed under Apache 2.0 which allows compiling binaries from source and then distributing those versions."
+ distribution = "repo"
+ }
+ }
-task makeNativeRuntime {
- dependsOn buildICU
- dependsOn cmakeNativeRuntime
- doLast {
- exec {
- workingDir "$buildDir/cpp"
- commandLine 'ninja'
- }
- }
-}
+ scm {
+ url = "https://android.googlesource.com/platform/manifest.git"
+ connection = "https://android.googlesource.com/platform/manifest.git"
+ }
-task copyNativeRuntimeToResources {
- def os = osName()
- if (System.getenv('SKIP_NATIVERUNTIME_BUILD')) {
- println("Skipping the nativeruntime build");
- } else if (!os.contains("linux") && !os.contains("mac") && !os.contains("win")) {
- println("Building the nativeruntime not supported for OS '${System.getProperty("os.name")}'")
- } else {
- dependsOn makeNativeRuntime
- outputs.dir "$buildDir/resources/main/native"
- doLast {
- copy {
- from ("$buildDir/cpp")
- include '*libnativeruntime.*'
- rename { String fileName ->
- if (os.contains("win")) {
- fileName.replace("libnativeruntime", "robolectric-nativeruntime")
- } else {
- fileName.replace("libnativeruntime", "librobolectric-nativeruntime")
+ developers {
+ developer {
+ name = "The Android Open Source Projects"
+ }
}
}
- into "$buildDir/resources/main/native/$os/${arch()}/"
}
}
- }
-}
-
-task copyNativeRuntimeFromGithubAction {
- outputs.dir "$buildDir/resources/main/native"
- doLast {
- def checkRunId = System.getenv('NATIVERUNTIME_ACTION_RUN_ID')
- def artifactsUrl = "https://api.github.com/repos/robolectric/robolectric/actions/runs/$checkRunId/artifacts"
- def downloadDir = new File("$buildDir/robolectric-nativeruntime-artifacts-$checkRunId")
- downloadDir.mkdirs()
- new JsonSlurper().parseText(new URL(artifactsUrl).text).artifacts.each { artifact ->
- def f = new File(downloadDir, "${artifact.name}.zip")
- if (!f.exists()) {
- println("Fetching ${artifact.name}.zip to $f")
- def conn = (HttpURLConnection) new URL(artifact.archive_download_url).openConnection()
- conn.instanceFollowRedirects = true
- conn.setRequestProperty("Authorization", authHeader())
+ repositories {
+ maven {
+ url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
- f.withOutputStream { out ->
- conn.inputStream.with { inp ->
- out << inp
- inp.close()
- out.close()
- }
+ credentials {
+ username = System.properties["sonatype-login"] ?: System.env['sonatypeLogin']
+ password = System.properties["sonatype-password"] ?: System.env['sonatypePassword']
}
}
- copy {
- from zipTree(f)
- include "librobolectric*"
- rename { String fileName ->
- fileName = fileName.replaceFirst("librobolectric.*dylib", "librobolectric-nativeruntime.dylib")
- return fileName.replaceFirst("librobolectric.*so", "librobolectric-nativeruntime.so")
- }
- def os = "linux"
- if (artifact.name.contains("-mac")) {
- os = "mac"
- }
- def arch = "x86_64"
- if (artifact.name.contains("-arm64")) {
- arch = "aarch64"
- }
- into "$buildDir/resources/main/native/$os/$arch/"
- }
}
}
-}
-processResources {
- if (System.getenv('NATIVERUNTIME_ACTION_RUN_ID')) {
- dependsOn copyNativeRuntimeFromGithubAction
- } else {
- dependsOn copyNativeRuntimeToResources
+ signing {
+ sign publishing.publications.nativeRuntimeDist
}
}
@@ -186,6 +66,8 @@ dependencies {
api project(":utils:reflector")
api "com.google.guava:guava:$guavaJREVersion"
+ implementation "org.robolectric:nativeruntime-dist-compat:1.0.0"
+
annotationProcessor "com.google.auto.service:auto-service:$autoServiceVersion"
compileOnly "com.google.auto.service:auto-service-annotations:$autoServiceVersion"
compileOnly AndroidSdk.MAX_SDK.coordinates
diff --git a/nativeruntime/cpp/CMakeLists.txt b/nativeruntime/cpp/CMakeLists.txt
deleted file mode 100644
index ee5d03c2e..000000000
--- a/nativeruntime/cpp/CMakeLists.txt
+++ /dev/null
@@ -1,186 +0,0 @@
-cmake_minimum_required(VERSION 3.10)
-
-# This is needed to ensure that static libraries can be linked into shared libraries.
-set(CMAKE_POSITION_INDEPENDENT_CODE ON)
-
-# Some libutils headers require C++17
-set (CMAKE_CXX_STANDARD 17)
-
-project(nativeruntime)
-
-if (WIN32)
- if(NOT DEFINED ENV{JAVA_HOME})
- message(FATAL_ERROR "JAVA_HOME is required in Windows")
- endif()
- # find_package JNI is broken on Windows, manually include header files
- set(JNI_INCLUDE_DIRS "$ENV{JAVA_HOME}/include" "$ENV{JAVA_HOME}/include/win32")
-else()
- find_package(JNI REQUIRED)
-endif()
-
-set(ANDROID_SQLITE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite")
-
-if(NOT EXISTS "${ANDROID_SQLITE_DIR}/dist/sqlite3.c")
- message(FATAL_ERROR "SQLite submodule missing. Please run `git submodule update --init`.")
-endif()
-
-if(DEFINED ENV{ICU_ROOT_DIR})
- if (WIN32)
- if(NOT EXISTS "$ENV{ICU_ROOT_DIR}/lib/libsicuin.a")
- message(FATAL_ERROR "ICU_ROOT_DIR does not contain 'lib/libsicuin.a'.")
- endif()
- else()
- if(NOT EXISTS "$ENV{ICU_ROOT_DIR}/lib/libicui18n.a")
- message(FATAL_ERROR "ICU_ROOT_DIR does not contain 'lib/libicui18n.a'.")
- endif()
- endif()
-
- message(NOTICE "Using $ENV{ICU_ROOT_DIR} as the ICU root dir")
- list(APPEND CMAKE_PREFIX_PATH "$ENV{ICU_ROOT_DIR}")
- if (WIN32)
- find_library(STATIC_ICUI18N_LIBRARY libsicuin.a)
- find_library(STATIC_ICUUC_LIBRARY libsicuuc.a)
- find_library(STATIC_ICUDATA_LIBRARY libsicudt.a)
- else()
- find_library(STATIC_ICUI18N_LIBRARY libicui18n.a)
- find_library(STATIC_ICUUC_LIBRARY libicuuc.a)
- find_library(STATIC_ICUDATA_LIBRARY libicudata.a)
- endif()
- include_directories($ENV{ICU_ROOT_DIR}/include)
-else()
- set(ICU_SUBMODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../external/icu")
-
- if(NOT EXISTS "${ICU_SUBMODULE_DIR}/icu4c/source/i18n/ucol.cpp")
- message(FATAL_ERROR "ICU submodule missing. Please run `git submodule update --init`.")
- endif()
-
- message(NOTICE "Using ${ICU_SUBMODULE_DIR} as the ICU root dir")
-
- if (WIN32)
- if(NOT EXISTS "${ICU_SUBMODULE_DIR}/icu4c/source/lib/libsicuin.a")
- message(FATAL_ERROR "ICU not built. Please run `./gradlew :nativeruntime:buildICU`.")
- endif()
- else()
- if(NOT EXISTS "${ICU_SUBMODULE_DIR}/icu4c/source/lib/libicui18n.a")
- message(FATAL_ERROR "ICU not built. Please run `./gradlew :nativeruntime:buildICU`.")
- endif()
- endif()
-
- list(APPEND CMAKE_PREFIX_PATH "${ICU_SUBMODULE_DIR}/icu4c/source/")
- if (WIN32)
- find_library(STATIC_ICUI18N_LIBRARY libsicuin.a)
- find_library(STATIC_ICUUC_LIBRARY libsicuuc.a)
- find_library(STATIC_ICUDATA_LIBRARY libsicudt.a)
- else()
- find_library(STATIC_ICUI18N_LIBRARY libicui18n.a)
- find_library(STATIC_ICUUC_LIBRARY libicuuc.a)
- find_library(STATIC_ICUDATA_LIBRARY libicudata.a)
- endif()
- include_directories(${ICU_SUBMODULE_DIR}/icu4c/source/i18n)
- include_directories(${ICU_SUBMODULE_DIR}/icu4c/source/common)
-endif()
-
-# Build flags derived from
-# https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:external/sqlite/dist/Android.bp
-
-set(SQLITE_COMPILE_OPTIONS
- -DHAVE_USLEEP=1
- -DNDEBUG=1
- -DSQLITE_DEFAULT_AUTOVACUUM=1
- -DSQLITE_DEFAULT_FILE_FORMAT=4
- -DSQLITE_DEFAULT_FILE_PERMISSIONS=0600
- -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576
- -DSQLITE_DEFAULT_LEGACY_ALTER_TABLE
- -DSQLITE_ENABLE_BATCH_ATOMIC_WRITE
- -DSQLITE_ENABLE_FTS3
- -DSQLITE_ENABLE_FTS3=1
- -DSQLITE_ENABLE_FTS3_BACKWARDS
- -DSQLITE_ENABLE_FTS4
- -DSQLITE_ENABLE_ICU=1
- -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1
- -DSQLITE_HAVE_ISNAN
- -DSQLITE_OMIT_BUILTIN_TEST
- -DSQLITE_OMIT_COMPILEOPTION_DIAGS
- -DSQLITE_OMIT_LOAD_EXTENSION
- -DSQLITE_POWERSAFE_OVERWRITE=1
- -DSQLITE_SECURE_DELETE
- -DSQLITE_TEMP_STORE=3
- -DSQLITE_THREADSAFE=2
-)
-
-include_directories(${ANDROID_SQLITE_DIR}/dist)
-include_directories(${ANDROID_SQLITE_DIR}/android)
-
-add_library(androidsqlite STATIC
- ${ANDROID_SQLITE_DIR}/android/OldPhoneNumberUtils.cpp
- ${ANDROID_SQLITE_DIR}/android/PhoneNumberUtils.cpp
- ${ANDROID_SQLITE_DIR}/android/PhoneNumberUtils.h
- ${ANDROID_SQLITE_DIR}/android/sqlite3_android.cpp
- ${ANDROID_SQLITE_DIR}/dist/sqlite3.c
- ${ANDROID_SQLITE_DIR}/dist/sqlite3ext.h
-)
-
-target_compile_options(androidsqlite PRIVATE ${SQLITE_COMPILE_OPTIONS})
-
-if (WIN32)
- target_link_libraries(androidsqlite
- --static
- ${STATIC_ICUI18N_LIBRARY}
- ${STATIC_ICUUC_LIBRARY}
- ${STATIC_ICUDATA_LIBRARY}
- gcc
- stdc++
- )
-else()
- target_link_libraries(androidsqlite
- ${STATIC_ICUI18N_LIBRARY}
- ${STATIC_ICUUC_LIBRARY}
- ${STATIC_ICUDATA_LIBRARY}
- -ldl
- -lpthread
- )
-endif()
-
-include_directories(${JNI_INCLUDE_DIRS})
-
-add_subdirectory (liblog)
-include_directories(liblog/include)
-
-include_directories(libnativehelper/include)
-
-add_subdirectory (libutils)
-include_directories(libutils/include)
-
-add_subdirectory (androidfw)
-include_directories(androidfw/include)
-
-add_subdirectory (libcutils)
-include_directories(libcutils/include)
-
-include_directories(base/include)
-
-add_library(nativeruntime SHARED
- jni/AndroidRuntime.cpp
- jni/AndroidRuntime.h
- jni/JNIMain.cpp
- jni/robo_android_database_CursorWindow.cpp
- jni/robo_android_database_SQLiteCommon.cpp
- jni/robo_android_database_SQLiteCommon.h
- jni/robo_android_database_SQLiteConnection.cpp
-)
-
-target_link_libraries(nativeruntime
- log
- utils
- androidsqlite
- cutils
- androidfw
-)
-
-if (CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
- target_link_libraries(nativeruntime
- -static-libgcc
- -static-libstdc++
- -Wl,--no-undefined # print an error if there are any undefined symbols
- )
-endif()
diff --git a/nativeruntime/cpp/androidfw/CMakeLists.txt b/nativeruntime/cpp/androidfw/CMakeLists.txt
deleted file mode 100644
index 87b89dde5..000000000
--- a/nativeruntime/cpp/androidfw/CMakeLists.txt
+++ /dev/null
@@ -1,11 +0,0 @@
-cmake_minimum_required(VERSION 3.10)
-
-project(androidfw)
-
-include_directories(include)
-
-include_directories(../libutils/include)
-include_directories(../libcutils/include)
-include_directories(../liblog/include)
-
-add_library(androidfw STATIC CursorWindow.cpp)
diff --git a/nativeruntime/cpp/androidfw/CursorWindow.cpp b/nativeruntime/cpp/androidfw/CursorWindow.cpp
deleted file mode 100644
index 59b767aa7..000000000
--- a/nativeruntime/cpp/androidfw/CursorWindow.cpp
+++ /dev/null
@@ -1,431 +0,0 @@
-/*
- * Copyright (C) 2006-2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/libs/androidfw/CursorWindow.cpp
-
-#undef LOG_TAG
-#define LOG_TAG "CursorWindow"
-
-#include <androidfw/CursorWindow.h>
-// #include <binder/Parcel.h>
-#include <assert.h>
-#include <cutils/ashmem.h>
-#include <log/log.h>
-#include <stdlib.h>
-#include <string.h>
-#if !defined(_WIN32)
-#include <sys/mman.h>
-#endif
-#include <unistd.h>
-
-namespace android {
-
-CursorWindow::CursorWindow(const String8& name, int ashmemFd, void* data,
- size_t size, bool readOnly)
- : mName(name),
- mAshmemFd(ashmemFd),
- mData(data),
- mSize(size),
- mReadOnly(readOnly) {
- #if defined(_WIN32)
- mHeader = new Header;
- #else
- mHeader = static_cast<Header*>(mData);
- #endif
-}
-
-CursorWindow::~CursorWindow() {
- #if defined(_WIN32)
- delete mHeader;
- #else
- ::munmap(mData, mSize);
- ::close(mAshmemFd);
- #endif
-}
-
-#if defined(_WIN32)
-status_t CursorWindow::create(const String8& name, size_t size,
- CursorWindow** outCursorWindow) {
- String8 ashmemName("CursorWindow: ");
- ashmemName.append(name);
-
- status_t result;
- // We don't use ashmem here, and CursorWindow constructor will use in-memory struct
- // to support Windows.
- CursorWindow* window = new CursorWindow(name, -1, nullptr, size, true /*readOnly*/);
- LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, "
- "numRows=%d, numColumns=%d, mSize=%zu, mData=%p",
- window->mHeader->freeOffset,
- window->mHeader->numRows,
- window->mHeader->numColumns,
- window->mSize, window->mData);
- if (window != nullptr) {
- *outCursorWindow = window;
- return OK;
- }
- *outCursorWindow = nullptr;
- return result;
-}
-#else
-status_t CursorWindow::create(const String8& name, size_t size,
- CursorWindow** outCursorWindow) {
- String8 ashmemName("CursorWindow: ");
- ashmemName.append(name);
-
- status_t result;
- int ashmemFd = ashmem_create_region(ashmemName.string(), size);
- if (ashmemFd < 0) {
- result = -errno;
- ALOGE("CursorWindow: ashmem_create_region() failed: errno=%d.", errno);
- } else {
- result = ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE);
- if (result < 0) {
- ALOGE("CursorWindow: ashmem_set_prot_region() failed: errno=%d", errno);
- } else {
- void* data = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,
- ashmemFd, 0);
- if (data == MAP_FAILED) {
- result = -errno;
- ALOGE("CursorWindow: mmap() failed: errno=%d.", errno);
- } else {
- result = ashmem_set_prot_region(ashmemFd, PROT_READ);
- if (result < 0) {
- ALOGE("CursorWindow: ashmem_set_prot_region() failed: errno=%d.",
- errno);
- } else {
- CursorWindow* window =
- new CursorWindow(name, ashmemFd, data, size, false /*readOnly*/);
- result = window->clear();
- if (!result) {
- LOG_WINDOW(
- "Created new CursorWindow: freeOffset=%d, "
- "numRows=%d, numColumns=%d, mSize=%zu, mData=%p",
- window->mHeader->freeOffset, window->mHeader->numRows,
- window->mHeader->numColumns, window->mSize, window->mData);
- *outCursorWindow = window;
- return OK;
- }
- delete window;
- }
- }
- ::munmap(data, size);
- }
- ::close(ashmemFd);
- }
- *outCursorWindow = nullptr;
- return result;
-}
-#endif
-//
-// status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow**
-// outCursorWindow) {
-// String8 name = parcel->readString8();
-//
-// status_t result;
-// int actualSize;
-// int ashmemFd = parcel->readFileDescriptor();
-// if (ashmemFd == int(BAD_TYPE)) {
-// result = BAD_TYPE;
-// ALOGE("CursorWindow: readFileDescriptor() failed");
-// } else {
-// ssize_t size = ashmem_get_size_region(ashmemFd);
-// if (size < 0) {
-// result = UNKNOWN_ERROR;
-// ALOGE("CursorWindow: ashmem_get_size_region() failed: errno=%d.",
-// errno);
-// } else {
-// int dupAshmemFd = ::fcntl(ashmemFd, F_DUPFD_CLOEXEC, 0);
-// if (dupAshmemFd < 0) {
-// result = -errno;
-// ALOGE("CursorWindow: fcntl() failed: errno=%d.", errno);
-// } else {
-// // the size of the ashmem descriptor can be modified between
-// ashmem_get_size_region
-// // call and mmap, so we'll check again immediately after memory is
-// mapped void* data = ::mmap(NULL, size, PROT_READ, MAP_SHARED,
-// dupAshmemFd, 0); if (data == MAP_FAILED) {
-// result = -errno;
-// ALOGE("CursorWindow: mmap() failed: errno=%d.", errno);
-// } else if ((actualSize = ashmem_get_size_region(dupAshmemFd)) != size)
-// {
-// ::munmap(data, size);
-// result = BAD_VALUE;
-// ALOGE("CursorWindow: ashmem_get_size_region() returned %d, expected
-// %d"
-// " errno=%d",
-// actualSize, (int) size, errno);
-// } else {
-// CursorWindow* window = new CursorWindow(name, dupAshmemFd,
-// data, size, true
-// /*readOnly*/);
-// LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, "
-// "numRows=%d, numColumns=%d, mSize=%zu, mData=%p",
-// window->mHeader->freeOffset,
-// window->mHeader->numRows,
-// window->mHeader->numColumns,
-// window->mSize, window->mData);
-// *outCursorWindow = window;
-// return OK;
-// }
-// ::close(dupAshmemFd);
-// }
-// }
-// }
-// *outCursorWindow = NULL;
-// return result;
-//}
-//
-// status_t CursorWindow::writeToParcel(Parcel* parcel) {
-// status_t status = parcel->writeString8(mName);
-// if (!status) {
-// status = parcel->writeDupFileDescriptor(mAshmemFd);
-// }
-// return status;
-//}
-
-status_t CursorWindow::clear() {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk);
- mHeader->firstChunkOffset = sizeof(Header);
- mHeader->numRows = 0;
- mHeader->numColumns = 0;
-
- RowSlotChunk* firstChunk =
- static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
- firstChunk->nextChunkOffset = 0;
- return OK;
-}
-
-status_t CursorWindow::setNumColumns(uint32_t numColumns) {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- uint32_t cur = mHeader->numColumns;
- if ((cur > 0 || mHeader->numRows > 0) && cur != numColumns) {
- ALOGE("Trying to go from %d columns to %d", cur, numColumns);
- return INVALID_OPERATION;
- }
- mHeader->numColumns = numColumns;
- return OK;
-}
-
-status_t CursorWindow::allocRow() {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- // Fill in the row slot
- RowSlot* rowSlot = allocRowSlot();
- if (rowSlot == nullptr) {
- return NO_MEMORY;
- }
-
- // Allocate the slots for the field directory
- size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot);
- uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/);
- if (!fieldDirOffset) {
- mHeader->numRows--;
- LOG_WINDOW(
- "The row failed, so back out the new row accounting "
- "from allocRowSlot %d",
- mHeader->numRows);
- return NO_MEMORY;
- }
- FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(fieldDirOffset));
- memset(fieldDir, 0, fieldDirSize);
-
- LOG_WINDOW(
- "Allocated row %u, rowSlot is at offset %u, fieldDir is %zu bytes at "
- "offset %u\n",
- mHeader->numRows - 1, offsetFromPtr(rowSlot), fieldDirSize,
- fieldDirOffset);
- rowSlot->offset = fieldDirOffset;
- return OK;
-}
-
-status_t CursorWindow::freeLastRow() {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- if (mHeader->numRows > 0) {
- mHeader->numRows--;
- }
- return OK;
-}
-
-uint32_t CursorWindow::alloc(size_t size, bool aligned) {
- uint32_t padding;
- if (aligned) {
- // 4 byte alignment
- padding = (~mHeader->freeOffset + 1) & 3;
- } else {
- padding = 0;
- }
-
- uint32_t offset = mHeader->freeOffset + padding;
- uint32_t nextFreeOffset = offset + size;
- if (nextFreeOffset > mSize) {
- // ALOGW("Window is full: requested allocation %zu bytes, "
- // "free space %zu bytes, window size %zu bytes",
- // size, freeSpace(), mSize);
- return 0;
- }
-
- mHeader->freeOffset = nextFreeOffset;
- return offset;
-}
-
-CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) {
- uint32_t chunkPos = row;
- RowSlotChunk* chunk =
- static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
- while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) {
- chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
- chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
- }
- return &chunk->slots[chunkPos];
-}
-
-CursorWindow::RowSlot* CursorWindow::allocRowSlot() {
- uint32_t chunkPos = mHeader->numRows;
- RowSlotChunk* chunk =
- static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
- while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) {
- chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
- chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
- }
- if (chunkPos == ROW_SLOT_CHUNK_NUM_ROWS) {
- if (!chunk->nextChunkOffset) {
- chunk->nextChunkOffset = alloc(sizeof(RowSlotChunk), true /*aligned*/);
- if (!chunk->nextChunkOffset) {
- return nullptr;
- }
- }
- chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
- chunk->nextChunkOffset = 0;
- chunkPos = 0;
- }
- mHeader->numRows += 1;
- return &chunk->slots[chunkPos];
-}
-
-CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row,
- uint32_t column) {
- if (row >= mHeader->numRows || column >= mHeader->numColumns) {
- ALOGE(
- "Failed to read row %d, column %d from a CursorWindow which "
- "has %d rows, %d columns.",
- row, column, mHeader->numRows, mHeader->numColumns);
- return nullptr;
- }
- RowSlot* rowSlot = getRowSlot(row);
- if (!rowSlot) {
- ALOGE("Failed to find rowSlot for row %d.", row);
- return nullptr;
- }
- FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(rowSlot->offset));
- return &fieldDir[column];
-}
-
-status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value,
- size_t size) {
- return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB);
-}
-
-status_t CursorWindow::putString(uint32_t row, uint32_t column,
- const char* value, size_t sizeIncludingNull) {
- return putBlobOrString(row, column, value, sizeIncludingNull,
- FIELD_TYPE_STRING);
-}
-
-status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column,
- const void* value, size_t size,
- int32_t type) {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- FieldSlot* fieldSlot = getFieldSlot(row, column);
- if (!fieldSlot) {
- return BAD_VALUE;
- }
-
- uint32_t offset = alloc(size);
- if (!offset) {
- return NO_MEMORY;
- }
-
- memcpy(offsetToPtr(offset), value, size);
-
- fieldSlot->type = type;
- fieldSlot->data.buffer.offset = offset;
- fieldSlot->data.buffer.size = size;
- return OK;
-}
-
-status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- FieldSlot* fieldSlot = getFieldSlot(row, column);
- if (!fieldSlot) {
- return BAD_VALUE;
- }
-
- fieldSlot->type = FIELD_TYPE_INTEGER;
- fieldSlot->data.l = value;
- return OK;
-}
-
-status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- FieldSlot* fieldSlot = getFieldSlot(row, column);
- if (!fieldSlot) {
- return BAD_VALUE;
- }
-
- fieldSlot->type = FIELD_TYPE_FLOAT;
- fieldSlot->data.d = value;
- return OK;
-}
-
-status_t CursorWindow::putNull(uint32_t row, uint32_t column) {
- if (mReadOnly) {
- return INVALID_OPERATION;
- }
-
- FieldSlot* fieldSlot = getFieldSlot(row, column);
- if (!fieldSlot) {
- return BAD_VALUE;
- }
-
- fieldSlot->type = FIELD_TYPE_NULL;
- fieldSlot->data.buffer.offset = 0;
- fieldSlot->data.buffer.size = 0;
- return OK;
-}
-
-}; // namespace android
diff --git a/nativeruntime/cpp/androidfw/include/androidfw/CursorWindow.h b/nativeruntime/cpp/androidfw/include/androidfw/CursorWindow.h
deleted file mode 100644
index 8975c648b..000000000
--- a/nativeruntime/cpp/androidfw/include/androidfw/CursorWindow.h
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- * Copyright (C) 2006 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/libs/androidfw/include/androidfw/CursorWindow.h
-
-#ifndef _ANDROID__DATABASE_WINDOW_H
-#define _ANDROID__DATABASE_WINDOW_H
-
-#include <inttypes.h>
-#include <stddef.h>
-#include <stdint.h>
-
-// #include <binder/Parcel.h>
-#include <log/log.h>
-#include <utils/String8.h>
-
-#if LOG_NDEBUG
-
-#define IF_LOG_WINDOW() if (false)
-#define LOG_WINDOW(...)
-
-#else
-
-#define IF_LOG_WINDOW() IF_ALOG(LOG_DEBUG, "CursorWindow")
-#define LOG_WINDOW(...) ALOG(LOG_DEBUG, "CursorWindow", __VA_ARGS__)
-
-#endif
-
-namespace android {
-
-/**
- * This class stores a set of rows from a database in a buffer. The beginning of
- * the window has first chunk of RowSlots, which are offsets to the row
- * directory, followed by an offset to the next chunk in a linked-list of
- * additional chunk of RowSlots in case the pre-allocated chunk isn't big enough
- * to refer to all rows. Each row directory has a FieldSlot per column, which
- * has the size, offset, and type of the data for that field. Note that the data
- * types come from sqlite3.h.
- *
- * Strings are stored in UTF-8.
- */
-class CursorWindow {
- CursorWindow(const String8& name, int ashmemFd, void* data, size_t size,
- bool readOnly);
-
- public:
- /* Field types. */
- enum {
- FIELD_TYPE_NULL = 0,
- FIELD_TYPE_INTEGER = 1,
- FIELD_TYPE_FLOAT = 2,
- FIELD_TYPE_STRING = 3,
- FIELD_TYPE_BLOB = 4,
- };
-
- /* Opaque type that describes a field slot. */
- struct FieldSlot {
- private:
- int32_t type;
- union {
- double d;
- int64_t l;
- struct {
- uint32_t offset;
- uint32_t size;
- } buffer;
- } data;
-
- friend class CursorWindow;
- } __attribute((packed));
-
- ~CursorWindow();
-
- static status_t create(const String8& name, size_t size,
- CursorWindow** outCursorWindow);
- // static status_t createFromParcel(Parcel* parcel, CursorWindow**
- // outCursorWindow);
- //
- // status_t writeToParcel(Parcel* parcel);
-
- inline String8 name() { return mName; }
- inline size_t size() { return mSize; }
- inline size_t freeSpace() { return mSize - mHeader->freeOffset; }
- inline uint32_t getNumRows() { return mHeader->numRows; }
- inline uint32_t getNumColumns() { return mHeader->numColumns; }
-
- status_t clear();
- status_t setNumColumns(uint32_t numColumns);
-
- /**
- * Allocate a row slot and its directory.
- * The row is initialized will null entries for each field.
- */
- status_t allocRow();
- status_t freeLastRow();
-
- status_t putBlob(uint32_t row, uint32_t column, const void* value,
- size_t size);
- status_t putString(uint32_t row, uint32_t column, const char* value,
- size_t sizeIncludingNull);
- status_t putLong(uint32_t row, uint32_t column, int64_t value);
- status_t putDouble(uint32_t row, uint32_t column, double value);
- status_t putNull(uint32_t row, uint32_t column);
-
- /**
- * Gets the field slot at the specified row and column.
- * Returns null if the requested row or column is not in the window.
- */
- FieldSlot* getFieldSlot(uint32_t row, uint32_t column);
-
- inline int32_t getFieldSlotType(FieldSlot* fieldSlot) {
- return fieldSlot->type;
- }
-
- inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) {
- return fieldSlot->data.l;
- }
-
- inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) {
- return fieldSlot->data.d;
- }
-
- inline const char* getFieldSlotValueString(FieldSlot* fieldSlot,
- size_t* outSizeIncludingNull) {
- *outSizeIncludingNull = fieldSlot->data.buffer.size;
- return static_cast<char*>(offsetToPtr(fieldSlot->data.buffer.offset,
- fieldSlot->data.buffer.size));
- }
-
- inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot,
- size_t* outSize) {
- *outSize = fieldSlot->data.buffer.size;
- return offsetToPtr(fieldSlot->data.buffer.offset,
- fieldSlot->data.buffer.size);
- }
-
- private:
- static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100;
-
- struct Header {
- // Offset of the lowest unused byte in the window.
- uint32_t freeOffset;
-
- // Offset of the first row slot chunk.
- uint32_t firstChunkOffset;
-
- uint32_t numRows;
- uint32_t numColumns;
- };
-
- struct RowSlot {
- uint32_t offset;
- };
-
- struct RowSlotChunk {
- RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS];
- uint32_t nextChunkOffset;
- };
-
- String8 mName;
- int mAshmemFd;
- void* mData;
- size_t mSize;
- bool mReadOnly;
- Header* mHeader;
-
- inline void* offsetToPtr(uint32_t offset, uint32_t bufferSize = 0) {
- if (offset >= mSize) {
- // ALOGE("Offset %" PRIu32 " out of bounds, max value %zu", offset,
- // mSize);
- return nullptr;
- }
- if (offset + bufferSize > mSize) {
- // ALOGE("End offset %" PRIu32 " out of bounds, max value %zu",
- // offset + bufferSize, mSize);
- return nullptr;
- }
- return static_cast<uint8_t*>(mData) + offset;
- }
-
- inline uint32_t offsetFromPtr(void* ptr) {
- return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData);
- }
-
- /**
- * Allocate a portion of the window. Returns the offset
- * of the allocation, or 0 if there isn't enough space.
- * If aligned is true, the allocation gets 4 byte alignment.
- */
- uint32_t alloc(size_t size, bool aligned = false);
-
- RowSlot* getRowSlot(uint32_t row);
- RowSlot* allocRowSlot();
-
- status_t putBlobOrString(uint32_t row, uint32_t column, const void* value,
- size_t size, int32_t type);
-};
-
-}; // namespace android
-
-#endif
diff --git a/nativeruntime/cpp/base/include/android-base/macros.h b/nativeruntime/cpp/base/include/android-base/macros.h
deleted file mode 100644
index 5a4a13ade..000000000
--- a/nativeruntime/cpp/base/include/android-base/macros.h
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/base/include/android-base/macros.h
-
-#ifndef UTILS_MACROS_H
-#define UTILS_MACROS_H
-
-#include <stddef.h> // for size_t
-#include <unistd.h> // for TEMP_FAILURE_RETRY
-
-#include <utility>
-
-// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
-#ifndef TEMP_FAILURE_RETRY
-#define TEMP_FAILURE_RETRY(exp) \
- ({ \
- decltype(exp) _rc; \
- do { \
- _rc = (exp); \
- } while (_rc == -1 && errno == EINTR); \
- _rc; \
- })
-#endif
-
-// A macro to disallow the copy constructor and operator= functions
-// This must be placed in the private: declarations for a class.
-//
-// For disallowing only assign or copy, delete the relevant operator or
-// constructor, for example:
-// void operator=(const TypeName&) = delete;
-// Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken
-// semantically, one should either use disallow both or neither. Try to
-// avoid these in new code.
-#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
- TypeName(const TypeName&) = delete; \
- void operator=(const TypeName&) = delete
-
-// A macro to disallow all the implicit constructors, namely the
-// default constructor, copy constructor and operator= functions.
-//
-// This should be used in the private: declarations for a class
-// that wants to prevent anyone from instantiating it. This is
-// especially useful for classes containing only static methods.
-#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
- TypeName() = delete; \
- DISALLOW_COPY_AND_ASSIGN(TypeName)
-
-// The arraysize(arr) macro returns the # of elements in an array arr.
-// The expression is a compile-time constant, and therefore can be
-// used in defining new arrays, for example. If you use arraysize on
-// a pointer by mistake, you will get a compile-time error.
-//
-// One caveat is that arraysize() doesn't accept any array of an
-// anonymous type or a type defined inside a function. In these rare
-// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
-// due to a limitation in C++'s template system. The limitation might
-// eventually be removed, but it hasn't happened yet.
-
-// This template function declaration is used in defining arraysize.
-// Note that the function doesn't need an implementation, as we only
-// use its type.
-template <typename T, size_t N>
-char (&ArraySizeHelper(T (&array)[N]))[N]; // NOLINT(readability/casting)
-
-#define arraysize(array) (sizeof(ArraySizeHelper(array)))
-
-#define SIZEOF_MEMBER(t, f) sizeof(std::declval<t>().f)
-
-// Changing this definition will cause you a lot of pain. A majority of
-// vendor code defines LIKELY and UNLIKELY this way, and includes
-// this header through an indirect path.
-#define LIKELY(exp) (__builtin_expect((exp) != 0, true))
-#define UNLIKELY(exp) (__builtin_expect((exp) != 0, false))
-
-#define WARN_UNUSED __attribute__((warn_unused_result))
-
-// A deprecated function to call to create a false use of the parameter, for
-// example:
-// int foo(int x) { UNUSED(x); return 10; }
-// to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED.
-template <typename... T>
-void UNUSED(const T&...) {}
-
-// An attribute to place on a parameter to a function, for example:
-// int foo(int x ATTRIBUTE_UNUSED) { return 10; }
-// to avoid compiler warnings.
-#define ATTRIBUTE_UNUSED __attribute__((__unused__))
-
-// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
-// between switch labels:
-// switch (x) {
-// case 40:
-// case 41:
-// if (truth_is_out_there) {
-// ++x;
-// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in
-// // comments.
-// } else {
-// return x;
-// }
-// case 42:
-// ...
-//
-// As shown in the example above, the FALLTHROUGH_INTENDED macro should be
-// followed by a semicolon. It is designed to mimic control-flow statements
-// like 'break;', so it can be placed in most places where 'break;' can, but
-// only if there are no statements on the execution path between it and the
-// next switch label.
-//
-// When compiled with clang, the FALLTHROUGH_INTENDED macro is expanded to
-// [[clang::fallthrough]] attribute, which is analysed when performing switch
-// labels fall-through diagnostic ('-Wimplicit-fallthrough'). See clang
-// documentation on language extensions for details:
-// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
-//
-// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
-// effect on diagnostics.
-//
-// In either case this macro has no effect on runtime behavior and performance
-// of code.
-#ifndef FALLTHROUGH_INTENDED
-#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
-#endif
-
-// Current ABI string
-#if defined(__arm__)
-#define ABI_STRING "arm"
-#elif defined(__aarch64__)
-#define ABI_STRING "arm64"
-#elif defined(__i386__)
-#define ABI_STRING "x86"
-#elif defined(__x86_64__)
-#define ABI_STRING "x86_64"
-#elif defined(__mips__) && !defined(__LP64__)
-#define ABI_STRING "mips"
-#elif defined(__mips__) && defined(__LP64__)
-#define ABI_STRING "mips64"
-#endif
-
-#endif // UTILS_MACROS_H
diff --git a/nativeruntime/cpp/jni/AndroidRuntime.cpp b/nativeruntime/cpp/jni/AndroidRuntime.cpp
deleted file mode 100644
index e22381c17..000000000
--- a/nativeruntime/cpp/jni/AndroidRuntime.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/core/jni/AndroidRuntime.cpp
-
-#include "AndroidRuntime.h"
-
-#include <assert.h>
-
-#include "jni.h"
-
-using namespace android;
-
-/*static*/ JavaVM* AndroidRuntime::mJavaVM = nullptr;
-
-/*static*/ JavaVM* AndroidRuntime::getJavaVM() {
- return AndroidRuntime::mJavaVM;
-}
-
-/*
- * Get the JNIEnv pointer for this thread.
- *
- * Returns NULL if the slot wasn't allocated or populated.
- */
-/*static*/ JNIEnv* AndroidRuntime::getJNIEnv() {
- JNIEnv* env;
- JavaVM* vm = AndroidRuntime::getJavaVM();
- assert(vm != nullptr);
-
- if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4) != JNI_OK)
- return nullptr;
- return env;
-}
diff --git a/nativeruntime/cpp/jni/AndroidRuntime.h b/nativeruntime/cpp/jni/AndroidRuntime.h
deleted file mode 100644
index 5e1d47ea9..000000000
--- a/nativeruntime/cpp/jni/AndroidRuntime.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/core/jni/include/android_runtime/AndroidRuntime.h
-
-#ifndef _RUNTIME_ANDROID_RUNTIME_H
-#define _RUNTIME_ANDROID_RUNTIME_H
-
-#include <jni.h>
-
-namespace android {
-
-class AndroidRuntime {
- public:
- /** return a pointer to the VM running in this process */
- static JavaVM* getJavaVM();
-
- /** return a pointer to the JNIEnv pointer for this thread */
- static JNIEnv* getJNIEnv();
-
- private:
- /* JNI JavaVM pointer */
- static JavaVM* mJavaVM;
-};
-} // namespace android
-
-#endif
diff --git a/nativeruntime/cpp/jni/JNIMain.cpp b/nativeruntime/cpp/jni/JNIMain.cpp
deleted file mode 100644
index 166e89474..000000000
--- a/nativeruntime/cpp/jni/JNIMain.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#include <jni.h>
-#include <log/log.h>
-
-#include "unicode/locid.h"
-
-namespace android {
-
-extern int register_android_database_CursorWindow(JNIEnv* env);
-extern int register_android_database_SQLiteConnection(JNIEnv* env);
-
-} // namespace android
-
-/*
- * JNI Initialization
- */
-jint JNI_OnLoad(JavaVM* jvm, void* reserved) {
- JNIEnv* env;
-
- ALOGV("loading JNI\n");
- // Check JNI version
- if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) {
- ALOGE("JNI version mismatch error");
- return JNI_ERR;
- }
-
- if (android::register_android_database_CursorWindow(env) != JNI_VERSION_1_4 ||
- android::register_android_database_SQLiteConnection(env) !=
- JNI_VERSION_1_4) {
- ALOGE("Failure during registration");
- return JNI_ERR;
- }
-
- // Configuration is stored as java System properties.
- // Get a reference to System.getProperty
- jclass systemClass = env->FindClass("java/lang/System");
- jmethodID getPropertyMethod = env->GetStaticMethodID(
- systemClass, "getProperty",
- "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
-
- // Set the default locale, which is required for e.g. SQLite's 'COLLATE
- // UNICODE'.
- auto stringLanguageTag = (jstring)env->CallStaticObjectMethod(
- systemClass, getPropertyMethod,
- env->NewStringUTF("robolectric.nativeruntime.languageTag"),
- env->NewStringUTF(""));
- const char* languageTag = env->GetStringUTFChars(stringLanguageTag, 0);
- int languageTagLength = env->GetStringLength(stringLanguageTag);
- if (languageTagLength > 0) {
- UErrorCode status = U_ZERO_ERROR;
- icu::Locale locale = icu::Locale::forLanguageTag(languageTag, status);
- if (U_SUCCESS(status)) {
- icu::Locale::setDefault(locale, status);
- }
- if (U_FAILURE(status)) {
- fprintf(stderr,
- "Failed to set the ICU default locale to '%s' (error code %d)\n",
- languageTag, status);
- }
- }
- env->ReleaseStringUTFChars(stringLanguageTag, languageTag);
- return JNI_VERSION_1_4;
-}
diff --git a/nativeruntime/cpp/jni/robo_android_database_CursorWindow.cpp b/nativeruntime/cpp/jni/robo_android_database_CursorWindow.cpp
deleted file mode 100644
index 9481b8e41..000000000
--- a/nativeruntime/cpp/jni/robo_android_database_CursorWindow.cpp
+++ /dev/null
@@ -1,628 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/core/jni/android_database_CursorWindow.cpp
-
-#undef LOG_TAG
-#define LOG_TAG "CursorWindow"
-#define LOG_NDEBUG 0
-
-#include <dirent.h>
-#include <inttypes.h>
-#include <jni.h>
-#include <log/log.h>
-#include <nativehelper/JNIHelp.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <utils/String16.h>
-#include <utils/String8.h>
-#include <utils/Unicode.h>
-
-#undef LOG_NDEBUG
-#define LOG_NDEBUG 1
-
-#include <androidfw/CursorWindow.h>
-// #include "android_os_Parcel.h"
-// #include "android_util_Binder.h"
-#include <nativehelper/scoped_local_ref.h>
-
-#include "robo_android_database_SQLiteCommon.h"
-
-namespace android {
-
-// static struct {
-// jfieldID data;
-// jfieldID sizeCopied;
-// } gCharArrayBufferClassInfo;
-
-static jfieldID getCharArrayBufferDataFieldId(JNIEnv* env, jobject obj) {
- jclass clsObj = env->GetObjectClass(obj);
- if (clsObj == nullptr) {
- printf("cls obj is null");
- }
- return env->GetFieldID(clsObj, "data", "[C");
-}
-
-static jfieldID getCharArrayBufferSizeCopiedFieldId(JNIEnv* env, jobject obj) {
- jclass clsObj = env->GetObjectClass(obj);
- if (clsObj == nullptr) {
- printf("cls obj is null");
- }
- return env->GetFieldID(clsObj, "sizeCopied", "I");
-}
-
-static jstring gEmptyString;
-
-static void throwExceptionWithRowCol(JNIEnv* env, jint row, jint column) {
- String8 msg;
- msg.appendFormat(
- "Couldn't read row %d, col %d from CursorWindow. "
- "Make sure the Cursor is initialized correctly before accessing data "
- "from it.",
- row, column);
- jniThrowException(env, "java/lang/IllegalStateException", msg.string());
-}
-
-static void throwUnknownTypeException(JNIEnv* env, jint type) {
- String8 msg;
- msg.appendFormat("UNKNOWN type %d", type);
- jniThrowException(env, "java/lang/IllegalStateException", msg.string());
-}
-
-// static int getFdCount() {
-// char fdpath[PATH_MAX];
-// int count = 0;
-// snprintf(fdpath, PATH_MAX, "/proc/%d/fd", getpid());
-// DIR* dir = opendir(fdpath);
-// if (dir != NULL) {
-// struct dirent* dirent;
-// while ((dirent = readdir(dir))) {
-// count++;
-// }
-// count -= 2; // discount "." and ".."
-// closedir(dir);
-// }
-// return count;
-// }
-
-static jlong nativeCreate(JNIEnv* env, jclass clazz, jstring nameObj,
- jint cursorWindowSize) {
- String8 name;
- const char* nameStr = env->GetStringUTFChars(nameObj, nullptr);
- name.setTo(nameStr);
- env->ReleaseStringUTFChars(nameObj, nameStr);
-
- CursorWindow* window;
- status_t status = CursorWindow::create(name, cursorWindowSize, &window);
- if (status || !window) {
- jniThrowExceptionFmt(
- env, "android/database/CursorWindowAllocationException",
- "Could not allocate CursorWindow '%s' of size %d due to error %d.",
- name.string(), cursorWindowSize, status);
- return 0;
- }
-
- LOG_WINDOW("nativeInitializeEmpty: window = %p", window);
- return reinterpret_cast<jlong>(window);
-}
-
-// static jlong nativeCreateFromParcel(JNIEnv* env, jclass clazz, jobject
-// parcelObj) {
-// Parcel* parcel = parcelForJavaObject(env, parcelObj);
-//
-// CursorWindow* window;
-// status_t status = CursorWindow::createFromParcel(parcel, &window);
-// if (status || !window) {
-// jniThrowExceptionFmt(env,
-// "android/database/CursorWindowAllocationException",
-// "Could not create CursorWindow from Parcel due to
-// error %d, process fd count=%d", status,
-// getFdCount());
-// return 0;
-// }
-//
-// LOG_WINDOW("nativeInitializeFromBinder: numRows = %d, numColumns = %d,
-// window = %p",
-// window->getNumRows(), window->getNumColumns(), window);
-// return reinterpret_cast<jlong>(window);
-// }
-
-static void nativeDispose(JNIEnv* env, jclass clazz, jlong windowPtr) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- if (window) {
- LOG_WINDOW("Closing window %p", window);
- delete window;
- }
-}
-
-static jstring nativeGetName(JNIEnv* env, jclass clazz, jlong windowPtr) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- return env->NewStringUTF(window->name().string());
-}
-//
-// static void nativeWriteToParcel(JNIEnv * env, jclass clazz, jlong windowPtr,
-// jobject parcelObj) {
-// CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
-// Parcel* parcel = parcelForJavaObject(env, parcelObj);
-//
-// status_t status = window->writeToParcel(parcel);
-// if (status) {
-// String8 msg;
-// msg.appendFormat("Could not write CursorWindow to Parcel due to error
-// %d.", status); jniThrowRuntimeException(env, msg.string());
-// }
-//}
-
-static void nativeClear(JNIEnv* env, jclass clazz, jlong windowPtr) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("Clearing window %p", window);
- status_t status = window->clear();
- if (status) {
- LOG_WINDOW("Could not clear window. error=%d", status);
- }
-}
-
-static jint nativeGetNumRows(JNIEnv* env, jclass clazz, jlong windowPtr) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- return window->getNumRows();
-}
-
-static jboolean nativeSetNumColumns(JNIEnv* env, jclass clazz, jlong windowPtr,
- jint columnNum) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- status_t status = window->setNumColumns(columnNum);
- return status == OK;
-}
-
-static jboolean nativeAllocRow(JNIEnv* env, jclass clazz, jlong windowPtr) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- status_t status = window->allocRow();
- return status == OK;
-}
-
-static void nativeFreeLastRow(JNIEnv* env, jclass clazz, jlong windowPtr) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- window->freeLastRow();
-}
-
-static jint nativeGetType(JNIEnv* env, jclass clazz, jlong windowPtr, jint row,
- jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("returning column type affinity for %d,%d from %p", row, column,
- window);
-
- CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column);
- if (!fieldSlot) {
- // FIXME: This is really broken but we have CTS tests that depend
- // on this legacy behavior.
- // throwExceptionWithRowCol(env, row, column);
- return CursorWindow::FIELD_TYPE_NULL;
- }
- return window->getFieldSlotType(fieldSlot);
-}
-
-static jbyteArray nativeGetBlob(JNIEnv* env, jclass clazz, jlong windowPtr,
- jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("Getting blob for %d,%d from %p", row, column, window);
-
- CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column);
- if (!fieldSlot) {
- throwExceptionWithRowCol(env, row, column);
- return nullptr;
- }
-
- int32_t type = window->getFieldSlotType(fieldSlot);
- if (type == CursorWindow::FIELD_TYPE_BLOB ||
- type == CursorWindow::FIELD_TYPE_STRING) {
- size_t size;
- const void* value = window->getFieldSlotValueBlob(fieldSlot, &size);
- if (!value) {
- throw_sqlite3_exception(env, "Native could not read blob slot");
- return nullptr;
- }
- jbyteArray byteArray = env->NewByteArray(size);
- if (!byteArray) {
- env->ExceptionClear();
- throw_sqlite3_exception(env, "Native could not create new byte[]");
- return nullptr;
- }
- env->SetByteArrayRegion(byteArray, 0, size,
- static_cast<const jbyte*>(value));
- return byteArray;
- } else if (type == CursorWindow::FIELD_TYPE_INTEGER) {
- throw_sqlite3_exception(env, "INTEGER data in nativeGetBlob ");
- } else if (type == CursorWindow::FIELD_TYPE_FLOAT) {
- throw_sqlite3_exception(env, "FLOAT data in nativeGetBlob ");
- } else if (type == CursorWindow::FIELD_TYPE_NULL) {
- // do nothing
- } else {
- throwUnknownTypeException(env, type);
- }
- return nullptr;
-}
-
-static jstring nativeGetString(JNIEnv* env, jclass clazz, jlong windowPtr,
- jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("Getting string for %d,%d from %p", row, column, window);
-
- CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column);
- if (!fieldSlot) {
- throwExceptionWithRowCol(env, row, column);
- return nullptr;
- }
-
- int32_t type = window->getFieldSlotType(fieldSlot);
- if (type == CursorWindow::FIELD_TYPE_STRING) {
- size_t sizeIncludingNull;
- const char* value =
- window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull);
- if (!value) {
- throw_sqlite3_exception(env, "Native could not read string slot");
- return nullptr;
- }
- if (sizeIncludingNull <= 1) {
- return gEmptyString;
- }
- // Convert to UTF-16 here instead of calling NewStringUTF. NewStringUTF
- // doesn't like UTF-8 strings with high codepoints. It actually expects
- // Modified UTF-8 with encoded surrogate pairs.
- String16 utf16(value, sizeIncludingNull - 1);
- return env->NewString(reinterpret_cast<const jchar*>(utf16.string()),
- utf16.size());
- } else if (type == CursorWindow::FIELD_TYPE_INTEGER) {
- int64_t value = window->getFieldSlotValueLong(fieldSlot);
- char buf[32];
- snprintf(buf, sizeof(buf), "%" PRId64, value);
- return env->NewStringUTF(buf);
- } else if (type == CursorWindow::FIELD_TYPE_FLOAT) {
- double value = window->getFieldSlotValueDouble(fieldSlot);
- char buf[32];
- snprintf(buf, sizeof(buf), "%g", value);
- return env->NewStringUTF(buf);
- } else if (type == CursorWindow::FIELD_TYPE_NULL) {
- return nullptr;
- } else if (type == CursorWindow::FIELD_TYPE_BLOB) {
- throw_sqlite3_exception(env, "Unable to convert BLOB to string");
- return nullptr;
- } else {
- throwUnknownTypeException(env, type);
- return nullptr;
- }
-}
-
-static jcharArray allocCharArrayBuffer(JNIEnv* env, jobject bufferObj,
- size_t size) {
- jcharArray dataObj = jcharArray(env->GetObjectField(
- bufferObj, getCharArrayBufferDataFieldId(env, bufferObj)));
- if (dataObj && size) {
- jsize capacity = env->GetArrayLength(dataObj);
- if (size_t(capacity) < size) {
- env->DeleteLocalRef(dataObj);
- dataObj = nullptr;
- }
- }
- if (!dataObj) {
- jsize capacity = size;
- if (capacity < 64) {
- capacity = 64;
- }
- dataObj = env->NewCharArray(capacity); // might throw OOM
- if (dataObj) {
- env->SetObjectField(
- bufferObj, getCharArrayBufferDataFieldId(env, bufferObj), dataObj);
- }
- }
- return dataObj;
-}
-
-static void fillCharArrayBufferUTF(JNIEnv* env, jobject bufferObj,
- const char* str, size_t len) {
- ssize_t size =
- utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str), len);
- if (size < 0) {
- size = 0; // invalid UTF8 string
- }
- jcharArray dataObj = allocCharArrayBuffer(env, bufferObj, size);
- if (dataObj) {
- if (size) {
- jchar* data =
- static_cast<jchar*>(env->GetPrimitiveArrayCritical(dataObj, nullptr));
- utf8_to_utf16_no_null_terminator(reinterpret_cast<const uint8_t*>(str),
- len, reinterpret_cast<char16_t*>(data),
- static_cast<size_t>(size));
- env->ReleasePrimitiveArrayCritical(dataObj, data, 0);
- }
- env->SetIntField(bufferObj,
- getCharArrayBufferSizeCopiedFieldId(env, bufferObj), size);
- }
-}
-
-static void clearCharArrayBuffer(JNIEnv* env, jobject bufferObj) {
- jcharArray dataObj = allocCharArrayBuffer(env, bufferObj, 0);
- if (dataObj) {
- env->SetIntField(bufferObj,
- getCharArrayBufferSizeCopiedFieldId(env, bufferObj), 0);
- }
-}
-
-static void nativeCopyStringToBuffer(JNIEnv* env, jclass clazz, jlong windowPtr,
- jint row, jint column, jobject bufferObj) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("Copying string for %d,%d from %p", row, column, window);
-
- CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column);
- if (!fieldSlot) {
- throwExceptionWithRowCol(env, row, column);
- return;
- }
-
- int32_t type = window->getFieldSlotType(fieldSlot);
- if (type == CursorWindow::FIELD_TYPE_STRING) {
- size_t sizeIncludingNull;
- const char* value =
- window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull);
- if (sizeIncludingNull > 1) {
- fillCharArrayBufferUTF(env, bufferObj, value, sizeIncludingNull - 1);
- } else {
- clearCharArrayBuffer(env, bufferObj);
- }
- } else if (type == CursorWindow::FIELD_TYPE_INTEGER) {
- int64_t value = window->getFieldSlotValueLong(fieldSlot);
- char buf[32];
- snprintf(buf, sizeof(buf), "%" PRId64, value);
- fillCharArrayBufferUTF(env, bufferObj, buf, strlen(buf));
- } else if (type == CursorWindow::FIELD_TYPE_FLOAT) {
- double value = window->getFieldSlotValueDouble(fieldSlot);
- char buf[32];
- snprintf(buf, sizeof(buf), "%g", value);
- fillCharArrayBufferUTF(env, bufferObj, buf, strlen(buf));
- } else if (type == CursorWindow::FIELD_TYPE_NULL) {
- clearCharArrayBuffer(env, bufferObj);
- } else if (type == CursorWindow::FIELD_TYPE_BLOB) {
- throw_sqlite3_exception(env, "Unable to convert BLOB to string");
- } else {
- throwUnknownTypeException(env, type);
- }
-}
-
-static jlong nativeGetLong(JNIEnv* env, jclass clazz, jlong windowPtr, jint row,
- jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("Getting long for %d,%d from %p", row, column, window);
-
- CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column);
- if (!fieldSlot) {
- throwExceptionWithRowCol(env, row, column);
- return 0;
- }
-
- int32_t type = window->getFieldSlotType(fieldSlot);
- if (type == CursorWindow::FIELD_TYPE_INTEGER) {
- return window->getFieldSlotValueLong(fieldSlot);
- } else if (type == CursorWindow::FIELD_TYPE_STRING) {
- size_t sizeIncludingNull;
- const char* value =
- window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull);
- return sizeIncludingNull > 1 ? strtoll(value, nullptr, 0) : 0L;
- } else if (type == CursorWindow::FIELD_TYPE_FLOAT) {
- return jlong(window->getFieldSlotValueDouble(fieldSlot));
- } else if (type == CursorWindow::FIELD_TYPE_NULL) {
- return 0;
- } else if (type == CursorWindow::FIELD_TYPE_BLOB) {
- throw_sqlite3_exception(env, "Unable to convert BLOB to long");
- return 0;
- } else {
- throwUnknownTypeException(env, type);
- return 0;
- }
-}
-
-static jdouble nativeGetDouble(JNIEnv* env, jclass clazz, jlong windowPtr,
- jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- LOG_WINDOW("Getting double for %d,%d from %p", row, column, window);
-
- CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column);
- if (!fieldSlot) {
- throwExceptionWithRowCol(env, row, column);
- return 0.0;
- }
-
- int32_t type = window->getFieldSlotType(fieldSlot);
- if (type == CursorWindow::FIELD_TYPE_FLOAT) {
- return window->getFieldSlotValueDouble(fieldSlot);
- } else if (type == CursorWindow::FIELD_TYPE_STRING) {
- size_t sizeIncludingNull;
- const char* value =
- window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull);
- return sizeIncludingNull > 1 ? strtod(value, nullptr) : 0.0;
- } else if (type == CursorWindow::FIELD_TYPE_INTEGER) {
- return jdouble(window->getFieldSlotValueLong(fieldSlot));
- } else if (type == CursorWindow::FIELD_TYPE_NULL) {
- return 0.0;
- } else if (type == CursorWindow::FIELD_TYPE_BLOB) {
- throw_sqlite3_exception(env, "Unable to convert BLOB to double");
- return 0.0;
- } else {
- throwUnknownTypeException(env, type);
- return 0.0;
- }
-}
-
-static jboolean nativePutBlob(JNIEnv* env, jclass clazz, jlong windowPtr,
- jbyteArray valueObj, jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- jsize len = env->GetArrayLength(valueObj);
-
- void* value = env->GetPrimitiveArrayCritical(valueObj, nullptr);
- status_t status = window->putBlob(row, column, value, len);
- env->ReleasePrimitiveArrayCritical(valueObj, value, JNI_ABORT);
-
- if (status) {
- LOG_WINDOW("Failed to put blob. error=%d", status);
- return false;
- }
-
- LOG_WINDOW("%d,%d is BLOB with %u bytes", row, column, len);
- return true;
-}
-
-static jboolean nativePutString(JNIEnv* env, jclass clazz, jlong windowPtr,
- jstring valueObj, jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
-
- size_t sizeIncludingNull = env->GetStringUTFLength(valueObj) + 1;
- const char* valueStr = env->GetStringUTFChars(valueObj, nullptr);
- if (!valueStr) {
- LOG_WINDOW("value can't be transferred to UTFChars");
- return false;
- }
- status_t status = window->putString(row, column, valueStr, sizeIncludingNull);
- env->ReleaseStringUTFChars(valueObj, valueStr);
-
- if (status) {
- LOG_WINDOW("Failed to put string. error=%d", status);
- return false;
- }
-
- LOG_WINDOW("%d,%d is TEXT with %zu bytes", row, column, sizeIncludingNull);
- return true;
-}
-
-static jboolean nativePutLong(JNIEnv* env, jclass clazz, jlong windowPtr,
- jlong value, jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- status_t status = window->putLong(row, column, value);
-
- if (status) {
- LOG_WINDOW("Failed to put long. error=%d", status);
- return false;
- }
-
- LOG_WINDOW("%d,%d is INTEGER %" PRId64, row, column, value);
- return true;
-}
-
-static jboolean nativePutDouble(JNIEnv* env, jclass clazz, jlong windowPtr,
- jdouble value, jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- status_t status = window->putDouble(row, column, value);
-
- if (status) {
- LOG_WINDOW("Failed to put double. error=%d", status);
- return false;
- }
-
- LOG_WINDOW("%d,%d is FLOAT %lf", row, column, value);
- return true;
-}
-
-static jboolean nativePutNull(JNIEnv* env, jclass clazz, jlong windowPtr,
- jint row, jint column) {
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
- status_t status = window->putNull(row, column);
-
- if (status) {
- LOG_WINDOW("Failed to put null. error=%d", status);
- return false;
- }
-
- LOG_WINDOW("%d,%d is NULL", row, column);
- return true;
-}
-
-static const JNINativeMethod sMethods[] = {
- /* name, signature, funcPtr */
- {(char*)"nativeCreate", (char*)"(Ljava/lang/String;I)J",
- reinterpret_cast<void*>(nativeCreate)},
- // { "nativeCreateFromParcel", "(Landroid/os/Parcel;)J",
- // (void*)nativeCreateFromParcel },
- {(char*)"nativeDispose", (char*)"(J)V",
- reinterpret_cast<void*>(nativeDispose)},
- // { "nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
- // (void*)nativeWriteToParcel },
-
- {const_cast<char*>("nativeGetName"),
- const_cast<char*>("(J)Ljava/lang/String;"),
- reinterpret_cast<void*>(nativeGetName)},
- {const_cast<char*>("nativeGetBlob"), const_cast<char*>("(JII)[B"),
- reinterpret_cast<void*>(nativeGetBlob)},
- {const_cast<char*>("nativeGetString"),
- const_cast<char*>("(JII)Ljava/lang/String;"),
- reinterpret_cast<void*>(nativeGetString)},
- {const_cast<char*>("nativeCopyStringToBuffer"),
- const_cast<char*>("(JIILandroid/database/CharArrayBuffer;)V"),
- reinterpret_cast<void*>(nativeCopyStringToBuffer)},
- {const_cast<char*>("nativePutBlob"), const_cast<char*>("(J[BII)Z"),
- reinterpret_cast<void*>(nativePutBlob)},
- {const_cast<char*>("nativePutString"),
- const_cast<char*>("(JLjava/lang/String;II)Z"),
- reinterpret_cast<void*>(nativePutString)},
-
- // ------- @FastNative below here ----------------------
- {const_cast<char*>("nativeClear"), const_cast<char*>("(J)V"),
- reinterpret_cast<void*>(nativeClear)},
- {const_cast<char*>("nativeGetNumRows"), const_cast<char*>("(J)I"),
- reinterpret_cast<void*>(nativeGetNumRows)},
- {const_cast<char*>("nativeSetNumColumns"), const_cast<char*>("(JI)Z"),
- reinterpret_cast<void*>(nativeSetNumColumns)},
- {const_cast<char*>("nativeAllocRow"), const_cast<char*>("(J)Z"),
- reinterpret_cast<void*>(nativeAllocRow)},
- {const_cast<char*>("nativeFreeLastRow"), const_cast<char*>("(J)V"),
- reinterpret_cast<void*>(nativeFreeLastRow)},
- {const_cast<char*>("nativeGetType"), const_cast<char*>("(JII)I"),
- reinterpret_cast<void*>(nativeGetType)},
- {const_cast<char*>("nativeGetLong"), const_cast<char*>("(JII)J"),
- reinterpret_cast<void*>(nativeGetLong)},
- {const_cast<char*>("nativeGetDouble"), const_cast<char*>("(JII)D"),
- reinterpret_cast<void*>(nativeGetDouble)},
- {const_cast<char*>("nativePutLong"), const_cast<char*>("(JJII)Z"),
- reinterpret_cast<void*>(nativePutLong)},
- {const_cast<char*>("nativePutDouble"), const_cast<char*>("(JDII)Z"),
- reinterpret_cast<void*>(nativePutDouble)},
- {const_cast<char*>("nativePutNull"), const_cast<char*>("(JII)Z"),
- reinterpret_cast<void*>(nativePutNull)},
-};
-
-int register_android_database_CursorWindow(JNIEnv* env) {
- gEmptyString = (jstring)env->NewGlobalRef(env->NewStringUTF(""));
-
- static const char* kCursorWindowClass =
- "org/robolectric/nativeruntime/CursorWindowNatives";
-
- ScopedLocalRef<jclass> cls(env, env->FindClass(kCursorWindowClass));
-
- if (cls.get() == nullptr) {
- ALOGE("jni CursorWindow registration failure, class not found '%s'",
- kCursorWindowClass);
- return JNI_ERR;
- }
-
- const jint count = sizeof(sMethods) / sizeof(sMethods[0]);
- int status = env->RegisterNatives(cls.get(), sMethods, count);
- if (status < 0) {
- ALOGE("jni CursorWindow registration failure, status: %d", status);
- return JNI_ERR;
- }
- return JNI_VERSION_1_4;
-}
-
-} // namespace android
diff --git a/nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.cpp b/nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.cpp
deleted file mode 100644
index 0da34da0b..000000000
--- a/nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.cpp
+++ /dev/null
@@ -1,251 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/core/jni/android_database_SQLiteCommon.cpp
-
-#include "robo_android_database_SQLiteCommon.h"
-
-#include <utils/String8.h>
-
-#include <map>
-#include <string>
-
-namespace android {
-
-static const std::map<int, std::string> sErrorCodesMap = {
- // Primary Result Code List
- {4, "SQLITE_ABORT"},
- {23, "SQLITE_AUTH"},
- {5, "SQLITE_BUSY"},
- {14, "SQLITE_CANTOPEN"},
- {19, "SQLITE_CONSTRAINT"},
- {11, "SQLITE_CORRUPT"},
- {101, "SQLITE_DONE"},
- {16, "SQLITE_EMPTY"},
- {1, "SQLITE_ERROR"},
- {24, "SQLITE_FORMAT"},
- {13, "SQLITE_FULL"},
- {2, "SQLITE_INTERNAL"},
- {9, "SQLITE_INTERRUPT"},
- {10, "SQLITE_IOERR"},
- {6, "SQLITE_LOCKED"},
- {20, "SQLITE_MISMATCH"},
- {21, "SQLITE_MISUSE"},
- {22, "SQLITE_NOLFS"},
- {7, "SQLITE_NOMEM"},
- {26, "SQLITE_NOTADB"},
- {12, "SQLITE_NOTFOUND"},
- {27, "SQLITE_NOTICE"},
- {0, "SQLITE_OK"},
- {3, "SQLITE_PERM"},
- {15, "SQLITE_PROTOCOL"},
- {25, "SQLITE_RANGE"},
- {8, "SQLITE_READONLY"},
- {100, "SQLITE_ROW"},
- {17, "SQLITE_SCHEMA"},
- {18, "SQLITE_TOOBIG"},
- {28, "SQLITE_WARNING"},
- // Extended Result Code List
- {516, "SQLITE_ABORT_ROLLBACK"},
- {261, "SQLITE_BUSY_RECOVERY"},
- {517, "SQLITE_BUSY_SNAPSHOT"},
- {1038, "SQLITE_CANTOPEN_CONVPATH"},
- {782, "SQLITE_CANTOPEN_FULLPATH"},
- {526, "SQLITE_CANTOPEN_ISDIR"},
- {270, "SQLITE_CANTOPEN_NOTEMPDIR"},
- {275, "SQLITE_CONSTRAINT_CHECK"},
- {531, "SQLITE_CONSTRAINT_COMMITHOOK"},
- {787, "SQLITE_CONSTRAINT_FOREIGNKEY"},
- {1043, "SQLITE_CONSTRAINT_FUNCTION"},
- {1299, "SQLITE_CONSTRAINT_NOTNULL"},
- {1555, "SQLITE_CONSTRAINT_PRIMARYKEY"},
- {2579, "SQLITE_CONSTRAINT_ROWID"},
- {1811, "SQLITE_CONSTRAINT_TRIGGER"},
- {2067, "SQLITE_CONSTRAINT_UNIQUE"},
- {2323, "SQLITE_CONSTRAINT_VTAB"},
- {267, "SQLITE_CORRUPT_VTAB"},
- {3338, "SQLITE_IOERR_ACCESS"},
- {2826, "SQLITE_IOERR_BLOCKED"},
- {3594, "SQLITE_IOERR_CHECKRESERVEDLOCK"},
- {4106, "SQLITE_IOERR_CLOSE"},
- {6666, "SQLITE_IOERR_CONVPATH"},
- {2570, "SQLITE_IOERR_DELETE"},
- {5898, "SQLITE_IOERR_DELETE_NOENT"},
- {4362, "SQLITE_IOERR_DIR_CLOSE"},
- {1290, "SQLITE_IOERR_DIR_FSYNC"},
- {1802, "SQLITE_IOERR_FSTAT"},
- {1034, "SQLITE_IOERR_FSYNC"},
- {6410, "SQLITE_IOERR_GETTEMPPATH"},
- {3850, "SQLITE_IOERR_LOCK"},
- {6154, "SQLITE_IOERR_MMAP"},
- {3082, "SQLITE_IOERR_NOMEM"},
- {2314, "SQLITE_IOERR_RDLOCK"},
- {266, "SQLITE_IOERR_READ"},
- {5642, "SQLITE_IOERR_SEEK"},
- {5130, "SQLITE_IOERR_SHMLOCK"},
- {5386, "SQLITE_IOERR_SHMMAP"},
- {4618, "SQLITE_IOERR_SHMOPEN"},
- {4874, "SQLITE_IOERR_SHMSIZE"},
- {522, "SQLITE_IOERR_SHORT_READ"},
- {1546, "SQLITE_IOERR_TRUNCATE"},
- {2058, "SQLITE_IOERR_UNLOCK"},
- {778, "SQLITE_IOERR_WRITE"},
- {262, "SQLITE_LOCKED_SHAREDCACHE"},
- {539, "SQLITE_NOTICE_RECOVER_ROLLBACK"},
- {283, "SQLITE_NOTICE_RECOVER_WAL"},
- {256, "SQLITE_OK_LOAD_PERMANENTLY"},
- {520, "SQLITE_READONLY_CANTLOCK"},
- {1032, "SQLITE_READONLY_DBMOVED"},
- {264, "SQLITE_READONLY_RECOVERY"},
- {776, "SQLITE_READONLY_ROLLBACK"},
- {284, "SQLITE_WARNING_AUTOINDEX"},
-};
-
-static std::string sqlite3_error_code_to_msg(int errcode) {
- auto it = sErrorCodesMap.find(errcode);
- if (it != sErrorCodesMap.end()) {
- return std::to_string(errcode) + " " + it->second;
- } else {
- return std::to_string(errcode);
- }
-}
-
-/* throw a SQLiteException with a message appropriate for the error in handle */
-void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle) {
- throw_sqlite3_exception(env, handle, nullptr);
-}
-
-/* throw a SQLiteException with the given message */
-void throw_sqlite3_exception(JNIEnv* env, const char* message) {
- throw_sqlite3_exception(env, nullptr, message);
-}
-
-/* throw a SQLiteException with a message appropriate for the error in handle
- concatenated with the given message
- */
-void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle,
- const char* message) {
- if (handle) {
- // get the error code and message from the SQLite connection
- // the error message may contain more information than the error code
- // because it is based on the extended error code rather than the simplified
- // error code that SQLite normally returns.
- throw_sqlite3_exception(env, sqlite3_extended_errcode(handle),
- sqlite3_errmsg(handle), message);
- } else {
- // we use SQLITE_OK so that a generic SQLiteException is thrown;
- // any code not specified in the switch statement below would do.
- throw_sqlite3_exception(env, SQLITE_OK, "unknown error", message);
- }
-}
-
-/* throw a SQLiteException for a given error code
- * should only be used when the database connection is not available because the
- * error information will not be quite as rich */
-void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode,
- const char* message) {
- throw_sqlite3_exception(env, errcode, "unknown error", message);
-}
-
-/* throw a SQLiteException for a given error code, sqlite3message, and
- user message
- */
-void throw_sqlite3_exception(JNIEnv* env, int errcode,
- const char* sqlite3Message, const char* message) {
- const char* exceptionClass;
- switch (errcode & 0xff) { /* mask off extended error code */
- case SQLITE_IOERR:
- exceptionClass = "android/database/sqlite/SQLiteDiskIOException";
- break;
- case SQLITE_CORRUPT:
- case SQLITE_NOTADB: // treat "unsupported file format" error as corruption
- // also
- exceptionClass = "android/database/sqlite/SQLiteDatabaseCorruptException";
- break;
- case SQLITE_CONSTRAINT:
- exceptionClass = "android/database/sqlite/SQLiteConstraintException";
- break;
- case SQLITE_ABORT:
- exceptionClass = "android/database/sqlite/SQLiteAbortException";
- break;
- case SQLITE_DONE:
- exceptionClass = "android/database/sqlite/SQLiteDoneException";
- sqlite3Message =
- nullptr; // SQLite error message is irrelevant in this case
- break;
- case SQLITE_FULL:
- exceptionClass = "android/database/sqlite/SQLiteFullException";
- break;
- case SQLITE_MISUSE:
- exceptionClass = "android/database/sqlite/SQLiteMisuseException";
- break;
- case SQLITE_PERM:
- exceptionClass = "android/database/sqlite/SQLiteAccessPermException";
- break;
- case SQLITE_BUSY:
- exceptionClass = "android/database/sqlite/SQLiteDatabaseLockedException";
- break;
- case SQLITE_LOCKED:
- exceptionClass = "android/database/sqlite/SQLiteTableLockedException";
- break;
- case SQLITE_READONLY:
- exceptionClass =
- "android/database/sqlite/SQLiteReadOnlyDatabaseException";
- break;
- case SQLITE_CANTOPEN:
- exceptionClass =
- "android/database/sqlite/SQLiteCantOpenDatabaseException";
- break;
- case SQLITE_TOOBIG:
- exceptionClass = "android/database/sqlite/SQLiteBlobTooBigException";
- break;
- case SQLITE_RANGE:
- exceptionClass =
- "android/database/sqlite/SQLiteBindOrColumnIndexOutOfRangeException";
- break;
- case SQLITE_NOMEM:
- exceptionClass = "android/database/sqlite/SQLiteOutOfMemoryException";
- break;
- case SQLITE_MISMATCH:
- exceptionClass =
- "android/database/sqlite/SQLiteDatatypeMismatchException";
- break;
- case SQLITE_INTERRUPT:
- exceptionClass = "android/os/OperationCanceledException";
- break;
- default:
- exceptionClass = "android/database/sqlite/SQLiteException";
- break;
- }
-
- if (sqlite3Message) {
- String8 fullMessage;
- fullMessage.append(sqlite3Message);
- std::string errcode_msg = sqlite3_error_code_to_msg(errcode);
- fullMessage.appendFormat(" (code %s)",
- errcode_msg.c_str()); // print extended error code
- if (message) {
- fullMessage.append(": ");
- fullMessage.append(message);
- }
- jniThrowException(env, exceptionClass, fullMessage.string());
- } else {
- jniThrowException(env, exceptionClass, message);
- }
-}
-
-} // namespace android
diff --git a/nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.h b/nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.h
deleted file mode 100644
index a426ce271..000000000
--- a/nativeruntime/cpp/jni/robo_android_database_SQLiteCommon.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/core/jni/android_database_SQLiteCommon.h
-
-#ifndef _ANDROID_DATABASE_SQLITE_COMMON_H
-#define _ANDROID_DATABASE_SQLITE_COMMON_H
-
-#include <jni.h>
-#include <nativehelper/JNIHelp.h>
-#include <sqlite3.h>
-
-// Special log tags defined in SQLiteDebug.java.
-#define SQLITE_LOG_TAG "SQLiteLog"
-#define SQLITE_TRACE_TAG "SQLiteStatements"
-#define SQLITE_PROFILE_TAG "SQLiteTime"
-
-namespace android {
-
-/* throw a SQLiteException with a message appropriate for the error in handle */
-void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle);
-
-/* throw a SQLiteException with the given message */
-void throw_sqlite3_exception(JNIEnv* env, const char* message);
-
-/* throw a SQLiteException with a message appropriate for the error in handle
- concatenated with the given message
- */
-void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message);
-
-/* throw a SQLiteException for a given error code */
-void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode,
- const char* message);
-
-void throw_sqlite3_exception(JNIEnv* env, int errcode,
- const char* sqlite3Message, const char* message);
-
-} // namespace android
-
-#endif // _ANDROID_DATABASE_SQLITE_COMMON_H
diff --git a/nativeruntime/cpp/jni/robo_android_database_SQLiteConnection.cpp b/nativeruntime/cpp/jni/robo_android_database_SQLiteConnection.cpp
deleted file mode 100644
index d12df8599..000000000
--- a/nativeruntime/cpp/jni/robo_android_database_SQLiteConnection.cpp
+++ /dev/null
@@ -1,1068 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:frameworks/base/core/jni/android_database_SQLiteConnection.cpp
-
-#define LOG_TAG "SQLiteConnection"
-
-#include <jni.h>
-#include <nativehelper/JNIHelp.h>
-
-#include "AndroidRuntime.h"
-// #include <android_runtime/Log.h>
-
-#include <androidfw/CursorWindow.h>
-#include <cutils/ashmem.h>
-#include <log/log.h>
-#include <nativehelper/scoped_local_ref.h>
-#include <nativehelper/scoped_utf8_chars.h>
-#include <sqlite3.h>
-#include <sqlite3_android.h>
-#include <string.h>
-#if !defined(_WIN32)
-#include <sys/mman.h>
-#endif
-#include <unistd.h>
-#include <utils/String16.h>
-#include <utils/String8.h>
-
-#include "robo_android_database_SQLiteCommon.h"
-
-// #include "core_jni_helpers.h"
-
-// Set to 1 to use UTF16 storage for localized indexes.
-#define UTF16_STORAGE 0
-
-namespace android {
-
-/* Busy timeout in milliseconds.
- * If another connection (possibly in another process) has the database locked
- * for longer than this amount of time then SQLite will generate a SQLITE_BUSY
- * error. The SQLITE_BUSY error is then raised as a
- * SQLiteDatabaseLockedException.
- *
- * In ordinary usage, busy timeouts are quite rare. Most databases only ever
- * have a single open connection at a time unless they are using WAL. When
- * using WAL, a timeout could occur if one connection is busy performing an
- * auto-checkpoint operation. The busy timeout needs to be long enough to
- * tolerate slow I/O write operations but not so long as to cause the
- * application to hang indefinitely if there is a problem acquiring a database
- * lock.
- */
-static const int BUSY_TIMEOUT_MS = 2500;
-
-static struct { jmethodID apply; } gUnaryOperator;
-
-static struct { jmethodID apply; } gBinaryOperator;
-
-struct SQLiteConnection {
- // Open flags.
- // Must be kept in sync with the constants defined in SQLiteDatabase.java.
- enum {
- OPEN_READWRITE = 0x00000000,
- OPEN_READONLY = 0x00000001,
- OPEN_READ_MASK = 0x00000001,
- NO_LOCALIZED_COLLATORS = 0x00000010,
- CREATE_IF_NECESSARY = 0x10000000,
- };
-
- sqlite3* const db;
- const int openFlags;
- const String8 path;
- const String8 label;
-
- volatile bool canceled;
-
- SQLiteConnection(sqlite3* db, int openFlags, const String8& path,
- const String8& label)
- : db(db),
- openFlags(openFlags),
- path(path),
- label(label),
- canceled(false) {}
-};
-
-// Called each time a statement begins execution, when tracing is enabled.
-static void sqliteTraceCallback(void* data, const char* sql) {
- SQLiteConnection* connection = static_cast<SQLiteConnection*>(data);
- ALOG(LOG_VERBOSE, SQLITE_TRACE_TAG, "%s: \"%s\"\n",
- connection->label.string(), sql);
-}
-
-// Called each time a statement finishes execution, when profiling is enabled.
-static void sqliteProfileCallback(void* data, const char* sql,
- sqlite3_uint64 tm) {
- SQLiteConnection* connection = static_cast<SQLiteConnection*>(data);
- ALOG(LOG_VERBOSE, SQLITE_PROFILE_TAG, "%s: \"%s\" took %0.3f ms\n",
- connection->label.string(), sql, tm * 0.000001f);
-}
-
-// Called after each SQLite VM instruction when cancelation is enabled.
-static int sqliteProgressHandlerCallback(void* data) {
- SQLiteConnection* connection = static_cast<SQLiteConnection*>(data);
- return connection->canceled;
-}
-
-static jlong nativeOpen(JNIEnv* env, jclass clazz, jstring pathStr,
- jint openFlags, jstring labelStr, jboolean enableTrace,
- jboolean enableProfile, jint lookasideSz,
- jint lookasideCnt) {
- int sqliteFlags;
- if (openFlags & SQLiteConnection::CREATE_IF_NECESSARY) {
- sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
- } else if (openFlags & SQLiteConnection::OPEN_READONLY) {
- sqliteFlags = SQLITE_OPEN_READONLY;
- } else {
- sqliteFlags = SQLITE_OPEN_READWRITE;
- }
-
- const char* pathChars = env->GetStringUTFChars(pathStr, nullptr);
- String8 path(pathChars);
- env->ReleaseStringUTFChars(pathStr, pathChars);
-
- const char* labelChars = env->GetStringUTFChars(labelStr, nullptr);
- String8 label(labelChars);
- env->ReleaseStringUTFChars(labelStr, labelChars);
-
- sqlite3* db;
- int err = sqlite3_open_v2(path.string(), &db, sqliteFlags, nullptr);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception_errcode(env, err, "Could not open database");
- return 0;
- }
-
- if (lookasideSz >= 0 && lookasideCnt >= 0) {
- int err = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, NULL,
- lookasideSz, lookasideCnt);
- if (err != SQLITE_OK) {
- ALOGE("sqlite3_db_config(..., %d, %d) failed: %d", lookasideSz,
- lookasideCnt, err);
- throw_sqlite3_exception(env, db, "Cannot set lookaside");
- sqlite3_close(db);
- return 0;
- }
- }
-
- // Check that the database is really read/write when that is what we asked
- // for.
- if ((sqliteFlags & SQLITE_OPEN_READWRITE) &&
- sqlite3_db_readonly(db, nullptr)) {
- throw_sqlite3_exception(env, db,
- "Could not open the database in read/write mode.");
- sqlite3_close(db);
- return 0;
- }
-
- // Set the default busy handler to retry automatically before returning
- // SQLITE_BUSY.
- err = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, db, "Could not set busy timeout");
- sqlite3_close(db);
- return 0;
- }
-
- // Register custom Android functions.
- err = register_android_functions(db, UTF16_STORAGE);
- if (err) {
- throw_sqlite3_exception(env, db,
- "Could not register Android SQL functions.");
- sqlite3_close(db);
- return 0;
- }
-
- // Create wrapper object.
- SQLiteConnection* connection =
- new SQLiteConnection(db, openFlags, path, label);
-
- // Enable tracing and profiling if requested.
- if (enableTrace) {
- sqlite3_trace(db, &sqliteTraceCallback, connection);
- }
- if (enableProfile) {
- sqlite3_profile(db, &sqliteProfileCallback, connection);
- }
-
- ALOGV("Opened connection %p with label '%s'", db, label.string());
- return reinterpret_cast<jlong>(connection);
-}
-
-static void nativeClose(JNIEnv* env, jclass clazz, jlong connectionPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
-
- if (connection) {
- ALOGV("Closing connection %p", connection->db);
- int err = sqlite3_close(connection->db);
- if (err != SQLITE_OK) {
- // This can happen if sub-objects aren't closed first. Make sure the
- // caller knows.
- ALOGE("sqlite3_close(%p) failed: %d", connection->db, err);
- throw_sqlite3_exception(env, connection->db, "Count not close db.");
- return;
- }
-
- delete connection;
- }
-}
-
-static void sqliteCustomScalarFunctionCallback(sqlite3_context* context,
- int argc, sqlite3_value** argv) {
- JNIEnv* env = AndroidRuntime::getJNIEnv();
- jobject functionObjGlobal =
- reinterpret_cast<jobject>(sqlite3_user_data(context));
- ScopedLocalRef<jobject> functionObj(env, env->NewLocalRef(functionObjGlobal));
- ScopedLocalRef<jstring> argString(
- env, env->NewStringUTF(
- reinterpret_cast<const char*>(sqlite3_value_text(argv[0]))));
- ScopedLocalRef<jstring> resString(
- env, (jstring)env->CallObjectMethod(
- functionObj.get(), gUnaryOperator.apply, argString.get()));
-
- if (env->ExceptionCheck()) {
- ALOGE("Exception thrown by custom scalar function");
- sqlite3_result_error(context, "Exception thrown by custom scalar function",
- -1);
- env->ExceptionDescribe();
- env->ExceptionClear();
- return;
- }
-
- if (resString.get() == nullptr) {
- sqlite3_result_null(context);
- } else {
- ScopedUtfChars res(env, resString.get());
- sqlite3_result_text(context, res.c_str(), -1, SQLITE_TRANSIENT);
- }
-}
-
-static void sqliteCustomScalarFunctionDestructor(void* data) {
- jobject functionObjGlobal = reinterpret_cast<jobject>(data);
-
- JNIEnv* env = AndroidRuntime::getJNIEnv();
- env->DeleteGlobalRef(functionObjGlobal);
-}
-
-static void nativeRegisterCustomScalarFunction(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jstring functionName,
- jobject functionObj) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
-
- jobject functionObjGlobal = env->NewGlobalRef(functionObj);
- ScopedUtfChars functionNameChars(env, functionName);
- int err = sqlite3_create_function_v2(
- connection->db, functionNameChars.c_str(), 1, SQLITE_UTF8,
- reinterpret_cast<void*>(functionObjGlobal),
- &sqliteCustomScalarFunctionCallback, nullptr, nullptr,
- &sqliteCustomScalarFunctionDestructor);
-
- if (err != SQLITE_OK) {
- ALOGE("sqlite3_create_function returned %d", err);
- env->DeleteGlobalRef(functionObjGlobal);
- throw_sqlite3_exception(env, connection->db);
- return;
- }
-}
-
-static void sqliteCustomAggregateFunctionStep(sqlite3_context* context,
- int argc, sqlite3_value** argv) {
- char** agg = reinterpret_cast<char**>(
- sqlite3_aggregate_context(context, sizeof(const char**)));
- if (agg == nullptr) {
- return;
- } else if (*agg == nullptr) {
- // During our first call the best we can do is allocate our result
- // holder and populate it with our first value; we'll reduce it
- // against any additional values in future calls
- const char* res =
- reinterpret_cast<const char*>(sqlite3_value_text(argv[0]));
- if (res == nullptr) {
- *agg = nullptr;
- } else {
- *agg = strdup(res);
- }
- return;
- }
-
- JNIEnv* env = AndroidRuntime::getJNIEnv();
- jobject functionObjGlobal =
- reinterpret_cast<jobject>(sqlite3_user_data(context));
- ScopedLocalRef<jobject> functionObj(env, env->NewLocalRef(functionObjGlobal));
- ScopedLocalRef<jstring> arg0String(
- env, env->NewStringUTF(reinterpret_cast<const char*>(*agg)));
- ScopedLocalRef<jstring> arg1String(
- env, env->NewStringUTF(
- reinterpret_cast<const char*>(sqlite3_value_text(argv[0]))));
- ScopedLocalRef<jstring> resString(
- env,
- (jstring)env->CallObjectMethod(functionObj.get(), gBinaryOperator.apply,
- arg0String.get(), arg1String.get()));
-
- if (env->ExceptionCheck()) {
- ALOGE("Exception thrown by custom aggregate function");
- sqlite3_result_error(context,
- "Exception thrown by custom aggregate function", -1);
- env->ExceptionDescribe();
- env->ExceptionClear();
- return;
- }
-
- // One way or another, we have a new value to collect, and we need to
- // free our previous value
- if (*agg != nullptr) {
- free(*agg);
- }
- if (resString.get() == nullptr) {
- *agg = nullptr;
- } else {
- ScopedUtfChars res(env, resString.get());
- *agg = strdup(res.c_str());
- }
-}
-
-static void sqliteCustomAggregateFunctionFinal(sqlite3_context* context) {
- // We pass zero size here to avoid allocating for empty sets
- char** agg = reinterpret_cast<char**>(sqlite3_aggregate_context(context, 0));
- if (agg == nullptr) {
- return;
- } else if (*agg == nullptr) {
- sqlite3_result_null(context);
- } else {
- sqlite3_result_text(context, *agg, -1, SQLITE_TRANSIENT);
- free(*agg);
- }
-}
-
-static void sqliteCustomAggregateFunctionDestructor(void* data) {
- jobject functionObjGlobal = reinterpret_cast<jobject>(data);
-
- JNIEnv* env = AndroidRuntime::getJNIEnv();
- env->DeleteGlobalRef(functionObjGlobal);
-}
-
-static void nativeRegisterCustomAggregateFunction(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jstring functionName,
- jobject functionObj) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
-
- jobject functionObjGlobal = env->NewGlobalRef(functionObj);
- ScopedUtfChars functionNameChars(env, functionName);
- int err = sqlite3_create_function_v2(
- connection->db, functionNameChars.c_str(), 1, SQLITE_UTF8,
- reinterpret_cast<void*>(functionObjGlobal), nullptr,
- &sqliteCustomAggregateFunctionStep, &sqliteCustomAggregateFunctionFinal,
- &sqliteCustomAggregateFunctionDestructor);
-
- if (err != SQLITE_OK) {
- ALOGE("sqlite3_create_function returned %d", err);
- env->DeleteGlobalRef(functionObjGlobal);
- throw_sqlite3_exception(env, connection->db);
- return;
- }
-}
-
-static void nativeRegisterLocalizedCollators(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jstring localeStr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
-
- const char* locale = env->GetStringUTFChars(localeStr, nullptr);
- int err = register_localized_collators(connection->db, locale, UTF16_STORAGE);
- env->ReleaseStringUTFChars(localeStr, locale);
-
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db);
- }
-}
-
-static jlong nativePrepareStatement(JNIEnv* env, jclass clazz,
- jlong connectionPtr, jstring sqlString) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
-
- jsize sqlLength = env->GetStringLength(sqlString);
- const jchar* sql = env->GetStringCritical(sqlString, nullptr);
- sqlite3_stmt* statement;
- int err = sqlite3_prepare16_v2(connection->db, sql, sqlLength * sizeof(jchar),
- &statement, nullptr);
- env->ReleaseStringCritical(sqlString, sql);
-
- if (err != SQLITE_OK) {
- // Error messages like 'near ")": syntax error' are not
- // always helpful enough, so construct an error string that
- // includes the query itself.
- const char* query = env->GetStringUTFChars(sqlString, nullptr);
- char* message = static_cast<char*>(malloc(strlen(query) + 50));
- if (message) {
- strcpy(message, ", while compiling: "); // less than 50 chars
- strcat(message, query);
- }
- env->ReleaseStringUTFChars(sqlString, query);
- throw_sqlite3_exception(env, connection->db, message);
- free(message);
- return 0;
- }
-
- ALOGV("Prepared statement %p on connection %p", statement, connection->db);
- return reinterpret_cast<jlong>(statement);
-}
-
-static void nativeFinalizeStatement(JNIEnv* env, jclass clazz,
- jlong connectionPtr, jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- // We ignore the result of sqlite3_finalize because it is really telling us
- // about whether any errors occurred while executing the statement. The
- // statement itself is always finalized regardless.
- ALOGV("Finalized statement %p on connection %p", statement, connection->db);
- sqlite3_finalize(statement);
-}
-
-static jint nativeGetParameterCount(JNIEnv* env, jclass clazz,
- jlong connectionPtr, jlong statementPtr) {
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- return sqlite3_bind_parameter_count(statement);
-}
-
-static jboolean nativeIsReadOnly(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr) {
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- return sqlite3_stmt_readonly(statement) != 0;
-}
-
-static jint nativeGetColumnCount(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr) {
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- return sqlite3_column_count(statement);
-}
-
-static jstring nativeGetColumnName(JNIEnv* env, jclass clazz,
- jlong connectionPtr, jlong statementPtr,
- jint index) {
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- const jchar* name =
- static_cast<const jchar*>(sqlite3_column_name16(statement, index));
- if (name) {
- size_t length = 0;
- while (name[length]) {
- length += 1;
- }
- return env->NewString(name, length);
- }
- return nullptr;
-}
-
-static void nativeBindNull(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr, jint index) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = sqlite3_bind_null(statement, index);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db, nullptr);
- }
-}
-
-static void nativeBindLong(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr, jint index, jlong value) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = sqlite3_bind_int64(statement, index, value);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db, nullptr);
- }
-}
-
-static void nativeBindDouble(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr, jint index, jdouble value) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = sqlite3_bind_double(statement, index, value);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db, nullptr);
- }
-}
-
-static void nativeBindString(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr, jint index,
- jstring valueString) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- jsize valueLength = env->GetStringLength(valueString);
- const jchar* value = env->GetStringCritical(valueString, nullptr);
- int err = sqlite3_bind_text16(statement, index, value,
- valueLength * sizeof(jchar), SQLITE_TRANSIENT);
- env->ReleaseStringCritical(valueString, value);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db, nullptr);
- }
-}
-
-static void nativeBindBlob(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr, jint index,
- jbyteArray valueArray) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- jsize valueLength = env->GetArrayLength(valueArray);
- jbyte* value =
- static_cast<jbyte*>(env->GetPrimitiveArrayCritical(valueArray, nullptr));
- int err =
- sqlite3_bind_blob(statement, index, value, valueLength, SQLITE_TRANSIENT);
- env->ReleasePrimitiveArrayCritical(valueArray, value, JNI_ABORT);
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db, nullptr);
- }
-}
-
-static void nativeResetStatementAndClearBindings(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = sqlite3_reset(statement);
- if (err == SQLITE_OK) {
- err = sqlite3_clear_bindings(statement);
- }
- if (err != SQLITE_OK) {
- throw_sqlite3_exception(env, connection->db, nullptr);
- }
-}
-
-static int executeNonQuery(JNIEnv* env, SQLiteConnection* connection,
- sqlite3_stmt* statement, bool isPragmaStmt) {
- int rc = sqlite3_step(statement);
- if (isPragmaStmt) {
- while (rc == SQLITE_ROW) {
- rc = sqlite3_step(statement);
- }
- }
- if (rc == SQLITE_ROW) {
- throw_sqlite3_exception(env,
- "Queries can be performed using SQLiteDatabase "
- "query or rawQuery methods only.");
- } else if (rc != SQLITE_DONE) {
- throw_sqlite3_exception(env, connection->db);
- }
- return rc;
-}
-
-static void nativeExecute(JNIEnv* env, jclass clazz, jlong connectionPtr,
- jlong statementPtr, jboolean isPragmaStmt) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- executeNonQuery(env, connection, statement, isPragmaStmt);
-}
-
-static jint nativeExecuteForChangedRowCount(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = executeNonQuery(env, connection, statement, false);
- return err == SQLITE_DONE ? sqlite3_changes(connection->db) : -1;
-}
-
-static jlong nativeExecuteForLastInsertedRowId(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = executeNonQuery(env, connection, statement, false);
- return err == SQLITE_DONE && sqlite3_changes(connection->db) > 0
- ? sqlite3_last_insert_rowid(connection->db)
- : -1;
-}
-
-static int executeOneRowQuery(JNIEnv* env, SQLiteConnection* connection,
- sqlite3_stmt* statement) {
- int err = sqlite3_step(statement);
- if (err != SQLITE_ROW) {
- throw_sqlite3_exception(env, connection->db);
- }
- return err;
-}
-
-static jlong nativeExecuteForLong(JNIEnv* env, jclass clazz,
- jlong connectionPtr, jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = executeOneRowQuery(env, connection, statement);
- if (err == SQLITE_ROW && sqlite3_column_count(statement) >= 1) {
- return sqlite3_column_int64(statement, 0);
- }
- return -1;
-}
-
-static jstring nativeExecuteForString(JNIEnv* env, jclass clazz,
- jlong connectionPtr, jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = executeOneRowQuery(env, connection, statement);
- if (err == SQLITE_ROW && sqlite3_column_count(statement) >= 1) {
- const jchar* text =
- static_cast<const jchar*>(sqlite3_column_text16(statement, 0));
- if (text) {
- size_t length = sqlite3_column_bytes16(statement, 0) / sizeof(jchar);
- return env->NewString(text, length);
- }
- }
- return nullptr;
-}
-
-#if defined(_WIN32)
-static int createAshmemRegionWithData(JNIEnv* env, const void* data,
- size_t length) {
- jniThrowIOException(env, -1);
- return -1;
-}
-#else
-static int createAshmemRegionWithData(JNIEnv* env, const void* data,
- size_t length) {
- int error = 0;
- int fd = ashmem_create_region(nullptr, length);
- if (fd < 0) {
- error = errno;
- ALOGE("ashmem_create_region failed: %s", strerror(error));
- } else {
- if (length > 0) {
- void* ptr =
- mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (ptr == MAP_FAILED) {
- error = errno;
- ALOGE("mmap failed: %s", strerror(error));
- } else {
- memcpy(ptr, data, length);
- munmap(ptr, length);
- }
- }
-
- if (!error) {
- if (ashmem_set_prot_region(fd, PROT_READ) < 0) {
- error = errno;
- ALOGE("ashmem_set_prot_region failed: %s", strerror(errno));
- } else {
- return fd;
- }
- }
-
- close(fd);
- }
-
- jniThrowIOException(env, error);
- return -1;
-}
-#endif
-
-static jint nativeExecuteForBlobFileDescriptor(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jlong statementPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
-
- int err = executeOneRowQuery(env, connection, statement);
- if (err == SQLITE_ROW && sqlite3_column_count(statement) >= 1) {
- const void* blob = sqlite3_column_blob(statement, 0);
- if (blob) {
- int length = sqlite3_column_bytes(statement, 0);
- if (length >= 0) {
- return createAshmemRegionWithData(env, blob, length);
- }
- }
- }
- return -1;
-}
-
-enum CopyRowResult {
- CPR_OK,
- CPR_FULL,
- CPR_ERROR,
-};
-
-static CopyRowResult copyRow(JNIEnv* env, CursorWindow* window,
- sqlite3_stmt* statement, int numColumns,
- int startPos, int addedRows) {
- // Allocate a new field directory for the row.
- status_t status = window->allocRow();
- if (status) {
- LOG_WINDOW("Failed allocating fieldDir at startPos %d row %d, error=%d",
- startPos, addedRows, status);
- return CPR_FULL;
- }
-
- // Pack the row into the window.
- CopyRowResult result = CPR_OK;
- for (int i = 0; i < numColumns; i++) {
- int type = sqlite3_column_type(statement, i);
- if (type == SQLITE_TEXT) {
- // TEXT data
- const char* text =
- reinterpret_cast<const char*>(sqlite3_column_text(statement, i));
- // SQLite does not include the NULL terminator in size, but does
- // ensure all strings are NULL terminated, so increase size by
- // one to make sure we store the terminator.
- size_t sizeIncludingNull = sqlite3_column_bytes(statement, i) + 1;
- status = window->putString(addedRows, i, text, sizeIncludingNull);
- if (status) {
- LOG_WINDOW("Failed allocating %zu bytes for text at %d,%d, error=%d",
- sizeIncludingNull, startPos + addedRows, i, status);
- result = CPR_FULL;
- break;
- }
- LOG_WINDOW("%d,%d is TEXT with %zu bytes", startPos + addedRows, i,
- sizeIncludingNull);
- } else if (type == SQLITE_INTEGER) {
- // INTEGER data
- int64_t value = sqlite3_column_int64(statement, i);
- status = window->putLong(addedRows, i, value);
- if (status) {
- LOG_WINDOW("Failed allocating space for a long in column %d, error=%d",
- i, status);
- result = CPR_FULL;
- break;
- }
- LOG_WINDOW("%d,%d is INTEGER %" PRId64, startPos + addedRows, i, value);
- } else if (type == SQLITE_FLOAT) {
- // FLOAT data
- double value = sqlite3_column_double(statement, i);
- status = window->putDouble(addedRows, i, value);
- if (status) {
- LOG_WINDOW(
- "Failed allocating space for a double in column %d, error=%d", i,
- status);
- result = CPR_FULL;
- break;
- }
- LOG_WINDOW("%d,%d is FLOAT %lf", startPos + addedRows, i, value);
- } else if (type == SQLITE_BLOB) {
- // BLOB data
- const void* blob = sqlite3_column_blob(statement, i);
- size_t size = sqlite3_column_bytes(statement, i);
- status = window->putBlob(addedRows, i, blob, size);
- if (status) {
- LOG_WINDOW("Failed allocating %zu bytes for blob at %d,%d, error=%d",
- size, startPos + addedRows, i, status);
- result = CPR_FULL;
- break;
- }
- LOG_WINDOW("%d,%d is Blob with %zu bytes", startPos + addedRows, i, size);
- } else if (type == SQLITE_NULL) {
- // NULL field
- status = window->putNull(addedRows, i);
- if (status) {
- LOG_WINDOW("Failed allocating space for a null in column %d, error=%d",
- i, status);
- result = CPR_FULL;
- break;
- }
-
- LOG_WINDOW("%d,%d is NULL", startPos + addedRows, i);
- } else {
- // Unknown data
- ALOGE("Unknown column type when filling database window");
- throw_sqlite3_exception(env, "Unknown column type when filling window");
- result = CPR_ERROR;
- break;
- }
- }
-
- // Free the last row if it was not successfully copied.
- if (result != CPR_OK) {
- window->freeLastRow();
- }
- return result;
-}
-
-static jlong nativeExecuteForCursorWindow(JNIEnv* env, jclass clazz,
- jlong connectionPtr,
- jlong statementPtr, jlong windowPtr,
- jint startPos, jint requiredPos,
- jboolean countAllRows) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
- CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr);
-
- status_t status = window->clear();
- if (status) {
- String8 msg;
- msg.appendFormat("Failed to clear the cursor window, status=%d", status);
- throw_sqlite3_exception(env, connection->db, msg.string());
- return 0;
- }
-
- int numColumns = sqlite3_column_count(statement);
- status = window->setNumColumns(numColumns);
- if (status) {
- String8 msg;
- msg.appendFormat(
- "Failed to set the cursor window column count to %d, status=%d",
- numColumns, status);
- throw_sqlite3_exception(env, connection->db, msg.string());
- return 0;
- }
-
- int retryCount = 0;
- int totalRows = 0;
- int addedRows = 0;
- bool windowFull = false;
- bool gotException = false;
- while (!gotException && (!windowFull || countAllRows)) {
- int err = sqlite3_step(statement);
- if (err == SQLITE_ROW) {
- LOG_WINDOW("Stepped statement %p to row %d", statement, totalRows);
- retryCount = 0;
- totalRows += 1;
-
- // Skip the row if the window is full or we haven't reached the start
- // position yet.
- if (startPos >= totalRows || windowFull) {
- continue;
- }
-
- CopyRowResult cpr =
- copyRow(env, window, statement, numColumns, startPos, addedRows);
- if (cpr == CPR_FULL && addedRows && startPos + addedRows <= requiredPos) {
- // We filled the window before we got to the one row that we really
- // wanted. Clear the window and start filling it again from here.
- // TODO: Would be nicer if we could progressively replace earlier rows.
- window->clear();
- window->setNumColumns(numColumns);
- startPos += addedRows;
- addedRows = 0;
- cpr = copyRow(env, window, statement, numColumns, startPos, addedRows);
- }
-
- if (cpr == CPR_OK) {
- addedRows += 1;
- } else if (cpr == CPR_FULL) {
- windowFull = true;
- } else {
- gotException = true;
- }
- } else if (err == SQLITE_DONE) {
- // All rows processed, bail
- LOG_WINDOW("Processed all rows");
- break;
- } else if (err == SQLITE_LOCKED || err == SQLITE_BUSY) {
- // The table is locked, retry
- LOG_WINDOW("Database locked, retrying");
- if (retryCount > 50) {
- ALOGE("Bailing on database busy retry");
- throw_sqlite3_exception(env, connection->db, "retrycount exceeded");
- gotException = true;
- } else {
- // Sleep to give the thread holding the lock a chance to finish
- usleep(1000);
- retryCount++;
- }
- } else {
- throw_sqlite3_exception(env, connection->db);
- gotException = true;
- }
- }
-
- LOG_WINDOW(
- "Resetting statement %p after fetching %d rows and adding %d rows "
- "to the window in %zu bytes",
- statement, totalRows, addedRows, window->size() - window->freeSpace());
- sqlite3_reset(statement);
-
- // Report the total number of rows on request.
- if (startPos > totalRows) {
- ALOGE("startPos %d > actual rows %d", startPos, totalRows);
- }
- if (totalRows > 0 && addedRows == 0) {
- String8 msg;
- msg.appendFormat(
- "Row too big to fit into CursorWindow requiredPos=%d, totalRows=%d",
- requiredPos, totalRows);
- throw_sqlite3_exception(env, SQLITE_TOOBIG, nullptr, msg.string());
- return 0;
- }
-
- jlong result = jlong(startPos) << 32 | jlong(totalRows);
- return result;
-}
-
-static jint nativeGetDbLookaside(JNIEnv* env, jobject clazz,
- jlong connectionPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
-
- int cur = -1;
- int unused;
- sqlite3_db_status(connection->db, SQLITE_DBSTATUS_LOOKASIDE_USED, &cur,
- &unused, 0);
- return cur;
-}
-
-static void nativeCancel(JNIEnv* env, jobject clazz, jlong connectionPtr) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- connection->canceled = true;
-}
-
-static void nativeResetCancel(JNIEnv* env, jobject clazz, jlong connectionPtr,
- jboolean cancelable) {
- SQLiteConnection* connection =
- reinterpret_cast<SQLiteConnection*>(connectionPtr);
- connection->canceled = false;
-
- if (cancelable) {
- sqlite3_progress_handler(connection->db, 4, sqliteProgressHandlerCallback,
- connection);
- } else {
- sqlite3_progress_handler(connection->db, 0, nullptr, nullptr);
- }
-}
-
-static const JNINativeMethod sMethods[] = {
- /* name, signature, funcPtr */
- {const_cast<char*>("nativeOpen"),
- const_cast<char*>("(Ljava/lang/String;ILjava/lang/String;ZZII)J"),
- reinterpret_cast<void*>(nativeOpen)},
- {const_cast<char*>("nativeClose"), const_cast<char*>("(J)V"),
- reinterpret_cast<void*>(nativeClose)},
- {const_cast<char*>("nativeRegisterCustomScalarFunction"),
- const_cast<char*>(
- "(JLjava/lang/String;Ljava/util/function/UnaryOperator;)V"),
- reinterpret_cast<void*>(nativeRegisterCustomScalarFunction)},
- {const_cast<char*>("nativeRegisterCustomAggregateFunction"),
- const_cast<char*>(
- "(JLjava/lang/String;Ljava/util/function/BinaryOperator;)V"),
- reinterpret_cast<void*>(nativeRegisterCustomAggregateFunction)},
- {const_cast<char*>("nativeRegisterLocalizedCollators"),
- const_cast<char*>("(JLjava/lang/String;)V"),
- reinterpret_cast<void*>(nativeRegisterLocalizedCollators)},
- {const_cast<char*>("nativePrepareStatement"),
- const_cast<char*>("(JLjava/lang/String;)J"),
- reinterpret_cast<void*>(nativePrepareStatement)},
- {const_cast<char*>("nativeFinalizeStatement"), const_cast<char*>("(JJ)V"),
- reinterpret_cast<void*>(nativeFinalizeStatement)},
- {const_cast<char*>("nativeGetParameterCount"), const_cast<char*>("(JJ)I"),
- reinterpret_cast<void*>(nativeGetParameterCount)},
- {const_cast<char*>("nativeIsReadOnly"), const_cast<char*>("(JJ)Z"),
- reinterpret_cast<void*>(nativeIsReadOnly)},
- {const_cast<char*>("nativeGetColumnCount"), const_cast<char*>("(JJ)I"),
- reinterpret_cast<void*>(nativeGetColumnCount)},
- {const_cast<char*>("nativeGetColumnName"),
- const_cast<char*>("(JJI)Ljava/lang/String;"),
- reinterpret_cast<void*>(nativeGetColumnName)},
- {const_cast<char*>("nativeBindNull"), const_cast<char*>("(JJI)V"),
- reinterpret_cast<void*>(nativeBindNull)},
- {const_cast<char*>("nativeBindLong"), const_cast<char*>("(JJIJ)V"),
- reinterpret_cast<void*>(nativeBindLong)},
- {const_cast<char*>("nativeBindDouble"), const_cast<char*>("(JJID)V"),
- reinterpret_cast<void*>(nativeBindDouble)},
- {const_cast<char*>("nativeBindString"),
- const_cast<char*>("(JJILjava/lang/String;)V"),
- reinterpret_cast<void*>(nativeBindString)},
- {const_cast<char*>("nativeBindBlob"), const_cast<char*>("(JJI[B)V"),
- reinterpret_cast<void*>(nativeBindBlob)},
- {const_cast<char*>("nativeResetStatementAndClearBindings"),
- const_cast<char*>("(JJ)V"),
- reinterpret_cast<void*>(nativeResetStatementAndClearBindings)},
- {const_cast<char*>("nativeExecute"), const_cast<char*>("(JJZ)V"),
- reinterpret_cast<void*>(nativeExecute)},
- {const_cast<char*>("nativeExecuteForLong"), const_cast<char*>("(JJ)J"),
- reinterpret_cast<void*>(nativeExecuteForLong)},
- {const_cast<char*>("nativeExecuteForString"),
- const_cast<char*>("(JJ)Ljava/lang/String;"),
- reinterpret_cast<void*>(nativeExecuteForString)},
- {const_cast<char*>("nativeExecuteForBlobFileDescriptor"),
- const_cast<char*>("(JJ)I"),
- reinterpret_cast<void*>(nativeExecuteForBlobFileDescriptor)},
- {const_cast<char*>("nativeExecuteForChangedRowCount"),
- const_cast<char*>("(JJ)I"),
- reinterpret_cast<void*>(nativeExecuteForChangedRowCount)},
- {const_cast<char*>("nativeExecuteForLastInsertedRowId"),
- const_cast<char*>("(JJ)J"),
- reinterpret_cast<void*>(nativeExecuteForLastInsertedRowId)},
- {const_cast<char*>("nativeExecuteForCursorWindow"),
- const_cast<char*>("(JJJIIZ)J"),
- reinterpret_cast<void*>(nativeExecuteForCursorWindow)},
- {const_cast<char*>("nativeGetDbLookaside"), const_cast<char*>("(J)I"),
- reinterpret_cast<void*>(nativeGetDbLookaside)},
- {const_cast<char*>("nativeCancel"), const_cast<char*>("(J)V"),
- reinterpret_cast<void*>(nativeCancel)},
- {const_cast<char*>("nativeResetCancel"), const_cast<char*>("(JZ)V"),
- reinterpret_cast<void*>(nativeResetCancel)},
-};
-
-int register_android_database_SQLiteConnection(JNIEnv* env) {
- static const char* kSQLiteClass =
- "org/robolectric/nativeruntime/SQLiteConnectionNatives";
- ScopedLocalRef<jclass> cls(env, env->FindClass(kSQLiteClass));
-
- if (cls.get() == nullptr) {
- ALOGE("jni SQLiteConnection registration failure, class not found '%s'",
- kSQLiteClass);
- return JNI_ERR;
- }
-
- jclass unaryClazz = env->FindClass("java/util/function/UnaryOperator");
- gUnaryOperator.apply = env->GetMethodID(
- unaryClazz, "apply", "(Ljava/lang/Object;)Ljava/lang/Object;");
-
- jclass binaryClazz = env->FindClass("java/util/function/BinaryOperator");
- gBinaryOperator.apply = env->GetMethodID(
- binaryClazz, "apply",
- "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
-
- const jint count = sizeof(sMethods) / sizeof(sMethods[0]);
- int status = env->RegisterNatives(cls.get(), sMethods, count);
- if (status < 0) {
- ALOGE("jni SQLite registration failure, status: %d", status);
- return JNI_ERR;
- }
-
- return JNI_VERSION_1_4;
-}
-}; // namespace android
diff --git a/nativeruntime/cpp/libcutils/CMakeLists.txt b/nativeruntime/cpp/libcutils/CMakeLists.txt
deleted file mode 100644
index 0c71a0a64..000000000
--- a/nativeruntime/cpp/libcutils/CMakeLists.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-cmake_minimum_required(VERSION 3.10)
-
-project(libcutils)
-
-include_directories(include)
-include_directories(../base/include)
-
-add_library(cutils STATIC ashmem.cpp)
diff --git a/nativeruntime/cpp/libcutils/ashmem.cpp b/nativeruntime/cpp/libcutils/ashmem.cpp
deleted file mode 100644
index 1b1fd7144..000000000
--- a/nativeruntime/cpp/libcutils/ashmem.cpp
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libcutils/ashmem-host.cpp
-
-#include "cutils/ashmem.h"
-
-/*
- * Implementation of the user-space ashmem API for the simulator, which lacks
- * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
- */
-
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <time.h>
-#include <unistd.h>
-
-// bionic and glibc both have TEMP_FAILURE_RETRY, but some (e.g. Mac OS) do not.
-#include "android-base/macros.h"
-
-static bool ashmem_validate_stat(int fd, struct stat* buf) {
- int result = fstat(fd, buf);
- if (result == -1) {
- return false;
- }
-
- /*
- * Check if this is an "ashmem" region.
- * TODO: This is very hacky, and can easily break.
- * We need some reliable indicator.
- */
- if (!(buf->st_nlink == 0 && S_ISREG(buf->st_mode))) {
- errno = ENOTTY;
- return false;
- }
- return true;
-}
-
-int ashmem_valid(int fd) {
- struct stat buf;
- return ashmem_validate_stat(fd, &buf);
-}
-
-int ashmem_create_region(const char* /*ignored*/, size_t size) {
- char pattern[PATH_MAX];
- snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX",
- getpid());
- int fd = mkstemp(pattern);
- if (fd == -1) return -1;
-
- unlink(pattern);
-
- if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
- close(fd);
- return -1;
- }
-
- return fd;
-}
-
-int ashmem_set_prot_region(int /*fd*/, int /*prot*/) { return 0; }
-
-int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
- return 0 /*ASHMEM_NOT_PURGED*/;
-}
-
-int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
- return 0 /*ASHMEM_IS_UNPINNED*/;
-}
-
-int ashmem_get_size_region(int fd) {
- struct stat buf;
- if (!ashmem_validate_stat(fd, &buf)) {
- return -1;
- }
-
- return buf.st_size;
-}
diff --git a/nativeruntime/cpp/libcutils/include/cutils/ashmem.h b/nativeruntime/cpp/libcutils/include/cutils/ashmem.h
deleted file mode 100644
index cc70b0a3d..000000000
--- a/nativeruntime/cpp/libcutils/include/cutils/ashmem.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/* cutils/ashmem.h
- **
- ** Copyright 2008 The Android Open Source Project
- **
- ** This file is dual licensed. It may be redistributed and/or modified
- ** under the terms of the Apache 2.0 License OR version 2 of the GNU
- ** General Public License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libcutils/include/cutils/ashmem.h
-
-#ifndef _CUTILS_ASHMEM_H
-#define _CUTILS_ASHMEM_H
-
-#include <stddef.h>
-
-#if defined(__BIONIC__)
-#include <linux/ashmem.h>
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int ashmem_valid(int fd);
-int ashmem_create_region(const char *name, size_t size);
-int ashmem_set_prot_region(int fd, int prot);
-int ashmem_pin_region(int fd, size_t offset, size_t len);
-int ashmem_unpin_region(int fd, size_t offset, size_t len);
-int ashmem_get_size_region(int fd);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _CUTILS_ASHMEM_H */
diff --git a/nativeruntime/cpp/liblog/CMakeLists.txt b/nativeruntime/cpp/liblog/CMakeLists.txt
deleted file mode 100644
index 520d4e216..000000000
--- a/nativeruntime/cpp/liblog/CMakeLists.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-cmake_minimum_required(VERSION 3.10)
-
-project(log)
-
-add_library(log STATIC log.c)
diff --git a/nativeruntime/cpp/liblog/include/log/log.h b/nativeruntime/cpp/liblog/include/log/log.h
deleted file mode 100644
index 8bf9a92aa..000000000
--- a/nativeruntime/cpp/liblog/include/log/log.h
+++ /dev/null
@@ -1,78 +0,0 @@
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:device/generic/goldfish-opengl/fuchsia/include/cutils/log.h
-
-#include <stdint.h>
-
-#ifndef __CUTILS_LOG_H__
-#define __CUTILS_LOG_H__
-
-#ifndef LOG_TAG
-#define LOG_TAG nullptr
-#endif
-
-enum {
- ANDROID_LOG_UNKNOWN = 0,
- ANDROID_LOG_DEFAULT,
- ANDROID_LOG_VERBOSE,
- ANDROID_LOG_DEBUG,
- ANDROID_LOG_INFO,
- ANDROID_LOG_WARN,
- ANDROID_LOG_ERROR,
- ANDROID_LOG_FATAL,
- ANDROID_LOG_SILENT,
-};
-
-#define android_printLog(prio, tag, format, ...) \
- __android_log_print(prio, tag, "[prio %d] " format, prio, ##__VA_ARGS__)
-
-#define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__)
-#define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
-
-#define __android_second(dummy, second, ...) second
-#define __android_rest(first, ...) , ##__VA_ARGS__
-
-#define android_printAssert(condition, tag, format, ...) \
- __android_log_assert(condition, tag, "assert: condition: %s " format, \
- condition, ##__VA_ARGS__)
-
-#define LOG_ALWAYS_FATAL_IF(condition, ...) \
- ((condition) \
- ? ((void)android_printAssert(#condition, LOG_TAG, ##__VA_ARGS__)) \
- : (void)0)
-
-#define LOG_ALWAYS_FATAL(...) \
- (((void)android_printAssert(NULL, LOG_TAG, ##__VA_ARGS__)))
-
-#define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
-#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
-#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
-#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
-
-#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ##__VA_ARGS__)
-
-#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
-
-#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__)
-
-#ifndef android_errorWriteLog
-#define android_errorWriteLog(tag, subTag) \
- __android_log_error_write(tag, subTag, -1, NULL, 0)
-#endif
-
-#ifndef android_errorWriteWithInfoLog
-#define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
- __android_log_error_write(tag, subTag, uid, data, dataLen)
-#endif
-
-extern "C" {
-
-int __android_log_print(int priority, const char* tag, const char* format, ...);
-
-[[noreturn]] void __android_log_assert(const char* condition, const char* tag,
- const char* format, ...);
-
-int __android_log_error_write(int tag, const char* subTag, int32_t uid,
- const char* data, uint32_t dataLen);
-}
-
-#endif
diff --git a/nativeruntime/cpp/liblog/log.c b/nativeruntime/cpp/liblog/log.c
deleted file mode 100644
index 271a2d405..000000000
--- a/nativeruntime/cpp/liblog/log.c
+++ /dev/null
@@ -1,35 +0,0 @@
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-
-int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
- ((void)prio);
- ((void)tag);
- ((void)fmt);
-
- if (prio >= 4) {
- va_list args;
- va_start(args, fmt);
- fprintf(stderr, "%s: ", tag);
- fprintf(stderr, fmt, args);
- va_end(args);
- }
- return 0;
-}
-
-int __android_log_error_write(int tag, const char* subTag, int32_t uid,
- const char* data, uint32_t dataLen) {
- ((void)tag);
- return 0;
-}
-
-void __android_log_assert(const char* condition, const char* tag,
- const char* format, ...) {
- va_list args;
- va_start(args, format);
- fprintf(stderr, "%s: ", tag);
- fprintf(stderr, format, args);
- va_end(args);
- abort();
-}
diff --git a/nativeruntime/cpp/libnativehelper/include/nativehelper/JNIHelp.h b/nativeruntime/cpp/libnativehelper/include/nativehelper/JNIHelp.h
deleted file mode 100644
index 1dd44b602..000000000
--- a/nativeruntime/cpp/libnativehelper/include/nativehelper/JNIHelp.h
+++ /dev/null
@@ -1,552 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/master:libnativehelper/include/nativehelper/JNIHelp.h
-
-/*
- * JNI helper functions.
- *
- * This file may be included by C or C++ code, which is trouble because jni.h
- * uses different typedefs for JNIEnv in each language.
- */
-#ifndef NATIVEHELPER_JNIHELP_H_
-#define NATIVEHELPER_JNIHELP_H_
-
-#include <errno.h>
-#include <jni.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/cdefs.h>
-#include <unistd.h>
-
-// #include <android/log.h>
-
-// Avoid formatting this as it must match webview's usage
-// (webview/graphics_utils.cpp).
-// clang-format off
-#ifndef NELEM
-#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
-#endif
-// clang-format on
-
-/*
- * For C++ code, we provide inlines that map to the C functions. g++ always
- * inlines these, even on non-optimized builds.
- */
-#if defined(__cplusplus)
-
-namespace android::jnihelp {
-struct [[maybe_unused]] ExpandableString {
- size_t dataSize; // The length of the C string data (not including the
- // null-terminator).
- char* data; // The C string data.
-};
-
-[[maybe_unused]] static void ExpandableStringInitialize(
- struct ExpandableString* s) {
- memset(s, 0, sizeof(*s));
-}
-
-[[maybe_unused]] static void ExpandableStringRelease(
- struct ExpandableString* s) {
- free(s->data);
- memset(s, 0, sizeof(*s));
-}
-
-[[maybe_unused]] static bool ExpandableStringAppend(struct ExpandableString* s,
- const char* text) {
- size_t textSize = strlen(text);
- size_t requiredSize = s->dataSize + textSize + 1;
- char* data = static_cast<char*>(realloc(s->data, requiredSize));
- if (data == nullptr) {
- return false;
- }
- s->data = data;
- memcpy(s->data + s->dataSize, text, textSize + 1);
- s->dataSize += textSize;
- return true;
-}
-
-[[maybe_unused]] static bool ExpandableStringAssign(struct ExpandableString* s,
- const char* text) {
- ExpandableStringRelease(s);
- return ExpandableStringAppend(s, text);
-}
-
-[[maybe_unused]] inline const char* platformStrError(int errnum, char* buf,
- size_t buflen) {
-#ifdef _WIN32
- strerror_s(buf, buflen, errnum);
- return buf;
-#elif defined(__USE_GNU)
- // char *strerror_r(int errnum, char *buf, size_t buflen); /* GNU-specific */
- return strerror_r(errnum, buf, buflen);
-#else
- // int strerror_r(int errnum, char *buf, size_t buflen); /* XSI-compliant */
- int rc = strerror_r(errnum, buf, buflen);
- if (rc != 0) {
- snprintf(buf, buflen, "errno %d", errnum);
- }
- return buf;
-#endif
-}
-
-[[maybe_unused]] static jmethodID FindMethod(JNIEnv* env, const char* className,
- const char* methodName,
- const char* descriptor) {
- // This method is only valid for classes in the core library which are
- // not unloaded during the lifetime of managed code execution.
- jclass clazz = env->FindClass(className);
- jmethodID methodId = env->GetMethodID(clazz, methodName, descriptor);
- env->DeleteLocalRef(clazz);
- return methodId;
-}
-
-[[maybe_unused]] static bool AppendJString(JNIEnv* env, jstring text,
- struct ExpandableString* dst) {
- const char* utfText = env->GetStringUTFChars(text, nullptr);
- if (utfText == nullptr) {
- return false;
- }
- bool success = ExpandableStringAppend(dst, utfText);
- env->ReleaseStringUTFChars(text, utfText);
- return success;
-}
-
-/*
- * Returns a human-readable summary of an exception object. The buffer will
- * be populated with the "binary" class name and, if present, the
- * exception message.
- */
-[[maybe_unused]] static bool GetExceptionSummary(JNIEnv* env, jthrowable thrown,
- struct ExpandableString* dst) {
- // Summary is <exception_class_name> ": " <exception_message>
- jclass exceptionClass = env->GetObjectClass(thrown); // Always succeeds
- jmethodID getName =
- FindMethod(env, "java/lang/Class", "getName", "()Ljava/lang/String;");
- jstring className = (jstring)env->CallObjectMethod(exceptionClass, getName);
- if (className == nullptr) {
- ExpandableStringAssign(dst, "<error getting class name>");
- env->ExceptionClear();
- env->DeleteLocalRef(exceptionClass);
- return false;
- }
- env->DeleteLocalRef(exceptionClass);
- exceptionClass = nullptr;
-
- if (!AppendJString(env, className, dst)) {
- ExpandableStringAssign(dst, "<error getting class name UTF-8>");
- env->ExceptionClear();
- env->DeleteLocalRef(className);
- return false;
- }
- env->DeleteLocalRef(className);
- className = nullptr;
-
- jmethodID getMessage = FindMethod(env, "java/lang/Throwable", "getMessage",
- "()Ljava/lang/String;");
- jstring message = (jstring)env->CallObjectMethod(thrown, getMessage);
- if (message == nullptr) {
- return true;
- }
-
- bool success =
- (ExpandableStringAppend(dst, ": ") && AppendJString(env, message, dst));
- if (!success) {
- // Two potential reasons for reaching here:
- //
- // 1. managed heap allocation failure (OOME).
- // 2. native heap allocation failure for the storage in |dst|.
- //
- // Attempt to append failure notification, okay to fail, |dst| contains the
- // class name of |thrown|.
- ExpandableStringAppend(dst, "<error getting message>");
- // Clear OOME if present.
- env->ExceptionClear();
- }
- env->DeleteLocalRef(message);
- message = nullptr;
- return success;
-}
-
-[[maybe_unused]] static jobject NewStringWriter(JNIEnv* env) {
- jclass clazz = env->FindClass("java/io/StringWriter");
- jmethodID init = env->GetMethodID(clazz, "<init>", "()V");
- jobject instance = env->NewObject(clazz, init);
- env->DeleteLocalRef(clazz);
- return instance;
-}
-
-[[maybe_unused]] static jstring StringWriterToString(JNIEnv* env,
- jobject stringWriter) {
- jmethodID toString = FindMethod(env, "java/io/StringWriter", "toString",
- "()Ljava/lang/String;");
- return (jstring)env->CallObjectMethod(stringWriter, toString);
-}
-
-[[maybe_unused]] static jobject NewPrintWriter(JNIEnv* env, jobject writer) {
- jclass clazz = env->FindClass("java/io/PrintWriter");
- jmethodID init = env->GetMethodID(clazz, "<init>", "(Ljava/io/Writer;)V");
- jobject instance = env->NewObject(clazz, init, writer);
- env->DeleteLocalRef(clazz);
- return instance;
-}
-
-[[maybe_unused]] static bool GetStackTrace(JNIEnv* env, jthrowable thrown,
- struct ExpandableString* dst) {
- // This function is equivalent to the following Java snippet:
- // StringWriter sw = new StringWriter();
- // PrintWriter pw = new PrintWriter(sw);
- // thrown.printStackTrace(pw);
- // String trace = sw.toString();
- // return trace;
- jobject sw = NewStringWriter(env);
- if (sw == nullptr) {
- return false;
- }
-
- jobject pw = NewPrintWriter(env, sw);
- if (pw == nullptr) {
- env->DeleteLocalRef(sw);
- return false;
- }
-
- jmethodID printStackTrace =
- FindMethod(env, "java/lang/Throwable", "printStackTrace",
- "(Ljava/io/PrintWriter;)V");
- env->CallVoidMethod(thrown, printStackTrace, pw);
-
- jstring trace = StringWriterToString(env, sw);
-
- env->DeleteLocalRef(pw);
- pw = nullptr;
- env->DeleteLocalRef(sw);
- sw = nullptr;
-
- if (trace == nullptr) {
- return false;
- }
-
- bool success = AppendJString(env, trace, dst);
- env->DeleteLocalRef(trace);
- return success;
-}
-
-[[maybe_unused]] static void GetStackTraceOrSummary(
- JNIEnv* env, jthrowable thrown, struct ExpandableString* dst) {
- // This method attempts to get a stack trace or summary info for an exception.
- // The exception may be provided in the |thrown| argument to this function.
- // If |thrown| is NULL, then any pending exception is used if it exists.
-
- // Save pending exception, callees may raise other exceptions. Any pending
- // exception is rethrown when this function exits.
- jthrowable pendingException = env->ExceptionOccurred();
- if (pendingException != nullptr) {
- env->ExceptionClear();
- }
-
- if (thrown == nullptr) {
- if (pendingException == nullptr) {
- ExpandableStringAssign(dst, "<no pending exception>");
- return;
- }
- thrown = pendingException;
- }
-
- if (!GetStackTrace(env, thrown, dst)) {
- // GetStackTrace may have raised an exception, clear it since it's not for
- // the caller.
- env->ExceptionClear();
- GetExceptionSummary(env, thrown, dst);
- }
-
- if (pendingException != nullptr) {
- // Re-throw the pending exception present when this method was called.
- env->Throw(pendingException);
- env->DeleteLocalRef(pendingException);
- }
-}
-
-[[maybe_unused]] static void DiscardPendingException(JNIEnv* env,
- const char* className) {
- jthrowable exception = env->ExceptionOccurred();
- env->ExceptionClear();
- if (exception == nullptr) {
- return;
- }
-
- struct ExpandableString summary;
- ExpandableStringInitialize(&summary);
- GetExceptionSummary(env, exception, &summary);
- // const char* details = (summary.data != NULL) ? summary.data : "Unknown";
- // __android_log_print(ANDROID_LOG_WARN, "JNIHelp",
- // "Discarding pending exception (%s) to throw %s",
- // details, className);
- ExpandableStringRelease(&summary);
- env->DeleteLocalRef(exception);
-}
-
-[[maybe_unused]] static int ThrowException(JNIEnv* env, const char* className,
- const char* ctorSig, ...) {
- int status = -1;
- jclass exceptionClass = nullptr;
-
- va_list args;
- va_start(args, ctorSig);
-
- DiscardPendingException(env, className);
-
- {
- /* We want to clean up local references before returning from this function,
- * so, regardless of return status, the end block must run. Have the work
- * done in a
- * nested block to avoid using any uninitialized variables in the end block.
- */
- exceptionClass = env->FindClass(className);
- if (exceptionClass == nullptr) {
- // __android_log_print(ANDROID_LOG_ERROR, "JNIHelp", "Unable to find
- // exception class %s",
- // className);
- /* an exception, most likely ClassNotFoundException, will now be pending
- */
- goto end;
- }
-
- jmethodID init = env->GetMethodID(exceptionClass, "<init>", ctorSig);
- if (init == nullptr) {
- // __android_log_print(ANDROID_LOG_ERROR, "JNIHelp",
- // "Failed to find constructor for '%s' '%s'",
- // className, ctorSig);
- goto end;
- }
-
- jobject instance = env->NewObjectV(exceptionClass, init, args);
- if (instance == nullptr) {
- // __android_log_print(ANDROID_LOG_ERROR, "JNIHelp", "Failed to
- // construct '%s'",
- // className);
- goto end;
- }
-
- if (env->Throw((jthrowable)instance) != JNI_OK) {
- // __android_log_print(ANDROID_LOG_ERROR, "JNIHelp", "Failed to throw
- // '%s'", className);
- /* an exception, most likely OOM, will now be pending */
- goto end;
- }
-
- /* everything worked fine, just update status to success and clean up */
- status = 0;
- }
-
-end:
- va_end(args);
- if (exceptionClass != nullptr) {
- env->DeleteLocalRef(exceptionClass);
- }
- return status;
-}
-
-[[maybe_unused]] static jstring CreateExceptionMsg(JNIEnv* env,
- const char* msg) {
- jstring detailMessage = env->NewStringUTF(msg);
- if (detailMessage == nullptr) {
- /* Not really much we can do here. We're probably dead in the water,
- but let's try to stumble on... */
- env->ExceptionClear();
- }
- return detailMessage;
-}
-} // namespace android::jnihelp
-
-/*
- * Register one or more native methods with a particular class. "className"
- * looks like "java/lang/String". Aborts on failure, returns 0 on success.
- */
-[[maybe_unused]] static int jniRegisterNativeMethods(
- JNIEnv* env, const char* className, const JNINativeMethod* methods,
- int numMethods) {
- using namespace android::jnihelp;
- jclass clazz = env->FindClass(className);
- if (clazz == nullptr) {
- // __android_log_assert("clazz == NULL", "JNIHelp",
- // "Native registration unable to find class '%s';
- // aborting...", className);
- }
- int result = env->RegisterNatives(clazz, methods, numMethods);
- env->DeleteLocalRef(clazz);
- if (result == 0) {
- return 0;
- }
-
- // Failure to register natives is fatal. Try to report the corresponding
- // exception, otherwise abort with generic failure message.
- jthrowable thrown = env->ExceptionOccurred();
- if (thrown != nullptr) {
- struct ExpandableString summary;
- ExpandableStringInitialize(&summary);
- if (GetExceptionSummary(env, thrown, &summary)) {
- // __android_log_print(ANDROID_LOG_FATAL, "JNIHelp", "%s",
- // summary.data);
- }
- ExpandableStringRelease(&summary);
- env->DeleteLocalRef(thrown);
- }
- // __android_log_print(ANDROID_LOG_FATAL, "JNIHelp",
- // "RegisterNatives failed for '%s'; aborting...",
- // className);
- return result;
-}
-
-/*
- * Throw an exception with the specified class and an optional message.
- *
- * The "className" argument will be passed directly to FindClass, which
- * takes strings with slashes (e.g. "java/lang/Object").
- *
- * If an exception is currently pending, we log a warning message and
- * clear it.
- *
- * Returns 0 on success, nonzero if something failed (e.g. the exception
- * class couldn't be found, so *an* exception will still be pending).
- *
- * Currently aborts the VM if it can't throw the exception.
- */
-[[maybe_unused]] static int jniThrowException(JNIEnv* env,
- const char* className,
- const char* msg) {
- using namespace android::jnihelp;
- jstring _detailMessage = CreateExceptionMsg(env, msg);
- int _status =
- ThrowException(env, className, "(Ljava/lang/String;)V", _detailMessage);
- if (_detailMessage != nullptr) {
- env->DeleteLocalRef(_detailMessage);
- }
- return _status;
-}
-
-/*
- * Throw an android.system.ErrnoException, with the given function name and
- * errno value.
- */
-[[maybe_unused]] static int jniThrowErrnoException(JNIEnv* env,
- const char* functionName,
- int errnum) {
- using namespace android::jnihelp;
- jstring _detailMessage = CreateExceptionMsg(env, functionName);
- int _status =
- ThrowException(env, "android/system/ErrnoException",
- "(Ljava/lang/String;I)V", _detailMessage, errnum);
- if (_detailMessage != nullptr) {
- env->DeleteLocalRef(_detailMessage);
- }
- return _status;
-}
-
-/*
- * Throw an exception with the specified class and formatted error message.
- *
- * The "className" argument will be passed directly to FindClass, which
- * takes strings with slashes (e.g. "java/lang/Object").
- *
- * If an exception is currently pending, we log a warning message and
- * clear it.
- *
- * Returns 0 on success, nonzero if something failed (e.g. the exception
- * class couldn't be found, so *an* exception will still be pending).
- *
- * Currently aborts the VM if it can't throw the exception.
- */
-[[maybe_unused]] static int jniThrowExceptionFmt(JNIEnv* env,
- const char* className,
- const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
- char msgBuf[512];
- vsnprintf(msgBuf, sizeof(msgBuf), fmt, args);
- va_end(args);
- return jniThrowException(env, className, msgBuf);
-}
-
-[[maybe_unused]] static int jniThrowNullPointerException(JNIEnv* env,
- const char* msg) {
- return jniThrowException(env, "java/lang/NullPointerException", msg);
-}
-
-[[maybe_unused]] static int jniThrowRuntimeException(JNIEnv* env,
- const char* msg) {
- return jniThrowException(env, "java/lang/RuntimeException", msg);
-}
-
-[[maybe_unused]] static int jniThrowIOException(JNIEnv* env, int errno_value) {
- using namespace android::jnihelp;
- char buffer[80];
- const char* message = platformStrError(errno_value, buffer, sizeof(buffer));
- return jniThrowException(env, "java/io/IOException", message);
-}
-
-/*
- * Returns a Java String object created from UTF-16 data either from jchar or,
- * if called from C++11, char16_t (a bitwise identical distinct type).
- */
-[[maybe_unused]] static inline jstring jniCreateString(
- JNIEnv* env, const jchar* unicodeChars, jsize len) {
- return env->NewString(unicodeChars, len);
-}
-
-[[maybe_unused]] static inline jstring jniCreateString(
- JNIEnv* env, const char16_t* unicodeChars, jsize len) {
- return jniCreateString(env, reinterpret_cast<const jchar*>(unicodeChars),
- len);
-}
-
-/*
- * Log a message and an exception.
- * If exception is NULL, logs the current exception in the JNI environment.
- */
-[[maybe_unused]] static void jniLogException(JNIEnv* env, int priority,
- const char* tag,
- jthrowable exception = nullptr) {
- using namespace android::jnihelp;
- struct ExpandableString summary;
- ExpandableStringInitialize(&summary);
- GetStackTraceOrSummary(env, exception, &summary);
- // const char* details = (summary.data != NULL) ? summary.data : "No memory
- // to report exception";
- // __android_log_write(priority, tag, details);
- ExpandableStringRelease(&summary);
-}
-
-#else // defined(__cplusplus)
-
-// ART-internal only methods (not exported), exposed for legacy C users
-
-int jniRegisterNativeMethods(JNIEnv* env, const char* className,
- const JNINativeMethod* gMethods, int numMethods);
-
-void jniLogException(JNIEnv* env, int priority, const char* tag,
- jthrowable thrown);
-
-int jniThrowException(JNIEnv* env, const char* className, const char* msg);
-
-int jniThrowNullPointerException(JNIEnv* env, const char* msg);
-
-#endif // defined(__cplusplus)
-
-#endif // NATIVEHELPER_JNIHELP_H_
diff --git a/nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_local_ref.h b/nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_local_ref.h
deleted file mode 100644
index 53e264449..000000000
--- a/nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_local_ref.h
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:libnativehelper/header_only_include/nativehelper/scoped_local_ref.h
-
-#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_REF_H_
-#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_REF_H_
-
-#include <android-base/macros.h>
-
-#include <cstddef>
-
-#include "jni.h"
-// #include "nativehelper_utils.h"
-
-// A smart pointer that deletes a JNI local reference when it goes out of scope.
-template <typename T>
-class ScopedLocalRef {
- public:
- ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) {}
-
- ScopedLocalRef(ScopedLocalRef&& s) noexcept
- : mEnv(s.mEnv), mLocalRef(s.release()) {}
-
- explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) {}
-
- ~ScopedLocalRef() { reset(); }
-
- void reset(T ptr = NULL) {
- if (ptr != mLocalRef) {
- if (mLocalRef != NULL) {
- mEnv->DeleteLocalRef(mLocalRef);
- }
- mLocalRef = ptr;
- }
- }
-
- T release() __attribute__((warn_unused_result)) {
- T localRef = mLocalRef;
- mLocalRef = NULL;
- return localRef;
- }
-
- T get() const { return mLocalRef; }
-
- // We do not expose an empty constructor as it can easily lead to errors
- // using common idioms, e.g.:
- // ScopedLocalRef<...> ref;
- // ref.reset(...);
-
- // Move assignment operator.
- ScopedLocalRef& operator=(ScopedLocalRef&& s) noexcept {
- reset(s.release());
- mEnv = s.mEnv;
- return *this;
- }
-
- // Allows "if (scoped_ref == nullptr)"
- bool operator==(std::nullptr_t) const { return mLocalRef == nullptr; }
-
- // Allows "if (scoped_ref != nullptr)"
- bool operator!=(std::nullptr_t) const { return mLocalRef != nullptr; }
-
- private:
- JNIEnv* mEnv;
- T mLocalRef;
-
- DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef);
-};
-
-#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_REF_H_
diff --git a/nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_utf8_chars.h b/nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_utf8_chars.h
deleted file mode 100644
index e2be8ef63..000000000
--- a/nativeruntime/cpp/libnativehelper/include/nativehelper/scoped_utf8_chars.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:libnativehelper/header_only_include/nativehelper/scoped_utf_chars.h
-
-#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_UTF_CHARS_H_
-#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_UTF_CHARS_H_
-
-#include <android-base/macros.h>
-#include <string.h>
-
-#include "JNIHelp.h"
-#include "jni.h"
-// #include "nativehelper_utils.h"
-
-// A smart pointer that provides read-only access to a Java string's UTF chars.
-// Unlike GetStringUTFChars, we throw NullPointerException rather than abort if
-// passed a null jstring, and c_str will return nullptr.
-// This makes the correct idiom very simple:
-//
-// ScopedUtfChars name(env, java_name);
-// if (name.c_str() == nullptr) {
-// return nullptr;
-// }
-class ScopedUtfChars {
- public:
- ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) {
- if (s == nullptr) {
- utf_chars_ = nullptr;
- jniThrowNullPointerException(env, "null");
- } else {
- utf_chars_ = env->GetStringUTFChars(s, nullptr);
- }
- }
-
- ScopedUtfChars(ScopedUtfChars&& rhs) noexcept
- : env_(rhs.env_), string_(rhs.string_), utf_chars_(rhs.utf_chars_) {
- rhs.env_ = nullptr;
- rhs.string_ = nullptr;
- rhs.utf_chars_ = nullptr;
- }
-
- ~ScopedUtfChars() {
- if (utf_chars_) {
- env_->ReleaseStringUTFChars(string_, utf_chars_);
- }
- }
-
- ScopedUtfChars& operator=(ScopedUtfChars&& rhs) noexcept {
- if (this != &rhs) {
- // Delete the currently owned UTF chars.
- this->~ScopedUtfChars();
-
- // Move the rhs ScopedUtfChars and zero it out.
- env_ = rhs.env_;
- string_ = rhs.string_;
- utf_chars_ = rhs.utf_chars_;
- rhs.env_ = nullptr;
- rhs.string_ = nullptr;
- rhs.utf_chars_ = nullptr;
- }
- return *this;
- }
-
- const char* c_str() const { return utf_chars_; }
-
- size_t size() const { return strlen(utf_chars_); }
-
- const char& operator[](size_t n) const { return utf_chars_[n]; }
-
- private:
- JNIEnv* env_;
- jstring string_;
- const char* utf_chars_;
-
- DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars);
-};
-
-#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_UTF_CHARS_H_
diff --git a/nativeruntime/cpp/libutils/CMakeLists.txt b/nativeruntime/cpp/libutils/CMakeLists.txt
deleted file mode 100644
index 46251c527..000000000
--- a/nativeruntime/cpp/libutils/CMakeLists.txt
+++ /dev/null
@@ -1,17 +0,0 @@
-cmake_minimum_required(VERSION 3.10)
-# Some libutils headers require C++17
-set (CMAKE_CXX_STANDARD 17)
-
-project(utils)
-
-include_directories(../base/include)
-include_directories(../liblog/include)
-include_directories(include)
-
-add_library(utils STATIC
- include/utils/String16.h
- SharedBuffer.cpp
- Unicode.cpp
- String8.cpp
- String16.cpp
-)
diff --git a/nativeruntime/cpp/libutils/SharedBuffer.cpp b/nativeruntime/cpp/libutils/SharedBuffer.cpp
deleted file mode 100644
index 4e35bec6d..000000000
--- a/nativeruntime/cpp/libutils/SharedBuffer.cpp
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/SharedBuffer.cpp
-
-#define LOG_TAG "sharedbuffer"
-
-#include "SharedBuffer.h"
-
-#include <log/log.h>
-#include <stdlib.h>
-#include <string.h>
-
-// ---------------------------------------------------------------------------
-
-namespace android {
-
-SharedBuffer* SharedBuffer::alloc(size_t size) {
- // Don't overflow if the combined size of the buffer / header is larger than
- // size_max.
- LOG_ALWAYS_FATAL_IF((size >= (SIZE_MAX - sizeof(SharedBuffer))),
- "Invalid buffer size %zu", size);
-
- SharedBuffer* sb =
- static_cast<SharedBuffer*>(malloc(sizeof(SharedBuffer) + size));
- if (sb) {
- // Should be std::atomic_init(&sb->mRefs, 1);
- // But that generates a warning with some compilers.
- // The following is OK on Android-supported platforms.
- sb->mRefs.store(1, std::memory_order_relaxed);
- sb->mSize = size;
- sb->mClientMetadata = 0;
- }
- return sb;
-}
-
-void SharedBuffer::dealloc(const SharedBuffer* released) {
- free(const_cast<SharedBuffer*>(released));
-}
-
-SharedBuffer* SharedBuffer::edit() const {
- if (onlyOwner()) {
- return const_cast<SharedBuffer*>(this);
- }
- SharedBuffer* sb = alloc(mSize);
- if (sb) {
- memcpy(sb->data(), data(), size());
- release();
- }
- return sb;
-}
-
-SharedBuffer* SharedBuffer::editResize(size_t newSize) const {
- if (onlyOwner()) {
- SharedBuffer* buf = const_cast<SharedBuffer*>(this);
- if (buf->mSize == newSize) return buf;
- // Don't overflow if the combined size of the new buffer / header is larger
- // than size_max.
- LOG_ALWAYS_FATAL_IF((newSize >= (SIZE_MAX - sizeof(SharedBuffer))),
- "Invalid buffer size %zu", newSize);
-
- buf = static_cast<SharedBuffer*>(
- realloc(buf, sizeof(SharedBuffer) + newSize));
- if (buf != nullptr) {
- buf->mSize = newSize;
- return buf;
- }
- }
- SharedBuffer* sb = alloc(newSize);
- if (sb) {
- const size_t mySize = mSize;
- memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize);
- release();
- }
- return sb;
-}
-
-SharedBuffer* SharedBuffer::attemptEdit() const {
- if (onlyOwner()) {
- return const_cast<SharedBuffer*>(this);
- }
- return nullptr;
-}
-
-SharedBuffer* SharedBuffer::reset(size_t new_size) const {
- // cheap-o-reset.
- SharedBuffer* sb = alloc(new_size);
- if (sb) {
- release();
- }
- return sb;
-}
-
-void SharedBuffer::acquire() const {
- mRefs.fetch_add(1, std::memory_order_relaxed);
-}
-
-int32_t SharedBuffer::release(uint32_t flags) const {
- const bool useDealloc = ((flags & eKeepStorage) == 0);
- if (onlyOwner()) {
- // Since we're the only owner, our reference count goes to zero.
- mRefs.store(0, std::memory_order_relaxed);
- if (useDealloc) {
- dealloc(this);
- }
- // As the only owner, our previous reference count was 1.
- return 1;
- }
- // There's multiple owners, we need to use an atomic decrement.
- int32_t prevRefCount = mRefs.fetch_sub(1, std::memory_order_release);
- if (prevRefCount == 1) {
- // We're the last reference, we need the acquire fence.
- std::atomic_thread_fence(std::memory_order_acquire);
- if (useDealloc) {
- dealloc(this);
- }
- }
- return prevRefCount;
-}
-
-}; // namespace android
diff --git a/nativeruntime/cpp/libutils/SharedBuffer.h b/nativeruntime/cpp/libutils/SharedBuffer.h
deleted file mode 100644
index dc5d504af..000000000
--- a/nativeruntime/cpp/libutils/SharedBuffer.h
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/SharedBuffer.h
-
-/*
- * DEPRECATED. DO NOT USE FOR NEW CODE.
- */
-
-#ifndef ANDROID_SHARED_BUFFER_H
-#define ANDROID_SHARED_BUFFER_H
-
-#include <stdint.h>
-#include <sys/types.h>
-
-#include <atomic>
-
-// ---------------------------------------------------------------------------
-
-namespace android {
-
-class SharedBuffer {
- public:
- /* flags to use with release() */
- enum { eKeepStorage = 0x00000001 };
-
- /*! allocate a buffer of size 'size' and acquire() it.
- * call release() to free it.
- */
- static SharedBuffer* alloc(size_t size);
-
- /*! free the memory associated with the SharedBuffer.
- * Fails if there are any users associated with this SharedBuffer.
- * In other words, the buffer must have been release by all its
- * users.
- */
- static void dealloc(const SharedBuffer* released);
-
- //! access the data for read
- inline const void* data() const;
-
- //! access the data for read/write
- inline void* data();
-
- //! get size of the buffer
- inline size_t size() const;
-
- //! get back a SharedBuffer object from its data
- static inline SharedBuffer* bufferFromData(void* data);
-
- //! get back a SharedBuffer object from its data
- static inline const SharedBuffer* bufferFromData(const void* data);
-
- //! get the size of a SharedBuffer object from its data
- static inline size_t sizeFromData(const void* data);
-
- //! edit the buffer (get a writtable, or non-const, version of it)
- SharedBuffer* edit() const;
-
- //! edit the buffer, resizing if needed
- SharedBuffer* editResize(size_t size) const;
-
- //! like edit() but fails if a copy is required
- SharedBuffer* attemptEdit() const;
-
- //! resize and edit the buffer, loose its content.
- SharedBuffer* reset(size_t size) const;
-
- //! acquire/release a reference on this buffer
- void acquire() const;
-
- /*! release a reference on this buffer, with the option of not
- * freeing the memory associated with it if it was the last reference
- * returns the previous reference count
- */
- int32_t release(uint32_t flags = 0) const;
-
- //! returns whether or not we're the only owner
- inline bool onlyOwner() const;
-
- private:
- inline SharedBuffer() {}
- inline ~SharedBuffer() {}
- SharedBuffer(const SharedBuffer&);
- SharedBuffer& operator=(const SharedBuffer&);
-
- // Must be sized to preserve correct alignment.
- mutable std::atomic<int32_t> mRefs;
- size_t mSize;
- uint32_t mReserved;
-
- public:
- // mClientMetadata is reserved for client use. It is initialized to 0
- // and the clients can do whatever they want with it. Note that this is
- // placed last so that it is adjcent to the buffer allocated.
- uint32_t mClientMetadata;
-};
-
-static_assert(sizeof(SharedBuffer) % 8 == 0 &&
- (sizeof(size_t) > 4 || sizeof(SharedBuffer) == 16),
- "SharedBuffer has unexpected size");
-
-// ---------------------------------------------------------------------------
-
-const void* SharedBuffer::data() const { return this + 1; }
-
-void* SharedBuffer::data() { return this + 1; }
-
-size_t SharedBuffer::size() const { return mSize; }
-
-SharedBuffer* SharedBuffer::bufferFromData(void* data) {
- return data ? static_cast<SharedBuffer*>(data) - 1 : nullptr;
-}
-
-const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
- return data ? static_cast<const SharedBuffer*>(data) - 1 : nullptr;
-}
-
-size_t SharedBuffer::sizeFromData(const void* data) {
- return data ? bufferFromData(data)->mSize : 0;
-}
-
-bool SharedBuffer::onlyOwner() const {
- return (mRefs.load(std::memory_order_acquire) == 1);
-}
-
-} // namespace android
-
-// ---------------------------------------------------------------------------
-
-#endif // ANDROID_VECTOR_H
diff --git a/nativeruntime/cpp/libutils/String16.cpp b/nativeruntime/cpp/libutils/String16.cpp
deleted file mode 100644
index 460b19c11..000000000
--- a/nativeruntime/cpp/libutils/String16.cpp
+++ /dev/null
@@ -1,450 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/String16.cpp
-
-#include <ctype.h>
-#include <log/log.h>
-#include <utils/String16.h>
-
-#include "SharedBuffer.h"
-
-namespace android {
-
-static const StaticString16 emptyString(u"");
-static inline char16_t* getEmptyString() {
- return const_cast<char16_t*>(emptyString.string());
-}
-
-// ---------------------------------------------------------------------------
-
-void* String16::alloc(size_t size) {
- SharedBuffer* buf = SharedBuffer::alloc(size);
- buf->mClientMetadata = kIsSharedBufferAllocated;
- return buf;
-}
-
-char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len) {
- if (u8len == 0) return getEmptyString();
-
- const uint8_t* u8cur = reinterpret_cast<const uint8_t*>(u8str);
-
- const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
- if (u16len < 0) {
- return getEmptyString();
- }
-
- SharedBuffer* buf =
- static_cast<SharedBuffer*>(alloc(sizeof(char16_t) * (u16len + 1)));
- if (buf) {
- u8cur = reinterpret_cast<const uint8_t*>(u8str);
- char16_t* u16str = static_cast<char16_t*>(buf->data());
-
- utf8_to_utf16(u8cur, u8len, u16str, (static_cast<size_t>(u16len)) + 1);
-
- // printf("Created UTF-16 string from UTF-8 \"%s\":", in);
- // printHexData(1, str, buf->size(), 16, 1);
- // printf("\n");
-
- return u16str;
- }
-
- return getEmptyString();
-}
-
-char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) {
- if (u16len >= SIZE_MAX / sizeof(char16_t)) {
- android_errorWriteLog(0x534e4554, "73826242");
- abort();
- }
-
- SharedBuffer* buf =
- static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t)));
- ALOG_ASSERT(buf, "Unable to allocate shared buffer");
- if (buf) {
- char16_t* str = static_cast<char16_t*>(buf->data());
- memcpy(str, u16str, u16len * sizeof(char16_t));
- str[u16len] = 0;
- return str;
- }
- return getEmptyString();
-}
-
-// ---------------------------------------------------------------------------
-
-String16::String16() : mString(getEmptyString()) {}
-
-String16::String16(StaticLinkage) : mString(nullptr) {
- // this constructor is used when we can't rely on the static-initializers
- // having run. In this case we always allocate an empty string. It's less
- // efficient than using getEmptyString(), but we assume it's uncommon.
-
- SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t)));
- char16_t* data = static_cast<char16_t*>(buf->data());
- data[0] = 0;
- mString = data;
-}
-
-String16::String16(const String16& o) : mString(o.mString) { acquire(); }
-
-String16::String16(const String16& o, size_t len, size_t begin)
- : mString(getEmptyString()) {
- setTo(o, len, begin);
-}
-
-String16::String16(const char16_t* o)
- : mString(allocFromUTF16(o, strlen16(o))) {}
-
-String16::String16(const char16_t* o, size_t len)
- : mString(allocFromUTF16(o, len)) {}
-
-String16::String16(const String8& o)
- : mString(allocFromUTF8(o.string(), o.size())) {}
-
-String16::String16(const char* o) : mString(allocFromUTF8(o, strlen(o))) {}
-
-String16::String16(const char* o, size_t len)
- : mString(allocFromUTF8(o, len)) {}
-
-String16::~String16() { release(); }
-
-size_t String16::size() const {
- if (isStaticString()) {
- return staticStringSize();
- } else {
- return SharedBuffer::sizeFromData(mString) / sizeof(char16_t) - 1;
- }
-}
-
-void String16::setTo(const String16& other) {
- release();
- mString = other.mString;
- acquire();
-}
-
-status_t String16::setTo(const String16& other, size_t len, size_t begin) {
- const size_t N = other.size();
- if (begin >= N) {
- release();
- mString = getEmptyString();
- return OK;
- }
- if ((begin + len) > N) len = N - begin;
- if (begin == 0 && len == N) {
- setTo(other);
- return OK;
- }
-
- if (&other == this) {
- LOG_ALWAYS_FATAL("Not implemented");
- }
-
- return setTo(other.string() + begin, len);
-}
-
-status_t String16::setTo(const char16_t* other) {
- return setTo(other, strlen16(other));
-}
-
-status_t String16::setTo(const char16_t* other, size_t len) {
- if (len >= SIZE_MAX / sizeof(char16_t)) {
- android_errorWriteLog(0x534e4554, "73826242");
- abort();
- }
-
- SharedBuffer* buf =
- static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
- if (buf) {
- char16_t* str = static_cast<char16_t*>(buf->data());
- memmove(str, other, len * sizeof(char16_t));
- str[len] = 0;
- mString = str;
- return OK;
- }
- return NO_MEMORY;
-}
-
-status_t String16::append(const String16& other) {
- const size_t myLen = size();
- const size_t otherLen = other.size();
- if (myLen == 0) {
- setTo(other);
- return OK;
- } else if (otherLen == 0) {
- return OK;
- }
-
- if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
- android_errorWriteLog(0x534e4554, "73826242");
- abort();
- }
-
- SharedBuffer* buf = static_cast<SharedBuffer*>(
- editResize((myLen + otherLen + 1) * sizeof(char16_t)));
- if (buf) {
- char16_t* str = static_cast<char16_t*>(buf->data());
- memcpy(str + myLen, other, (otherLen + 1) * sizeof(char16_t));
- mString = str;
- return OK;
- }
- return NO_MEMORY;
-}
-
-status_t String16::append(const char16_t* chrs, size_t otherLen) {
- const size_t myLen = size();
- if (myLen == 0) {
- setTo(chrs, otherLen);
- return OK;
- } else if (otherLen == 0) {
- return OK;
- }
-
- if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
- android_errorWriteLog(0x534e4554, "73826242");
- abort();
- }
-
- SharedBuffer* buf = static_cast<SharedBuffer*>(
- editResize((myLen + otherLen + 1) * sizeof(char16_t)));
- if (buf) {
- char16_t* str = static_cast<char16_t*>(buf->data());
- memcpy(str + myLen, chrs, otherLen * sizeof(char16_t));
- str[myLen + otherLen] = 0;
- mString = str;
- return OK;
- }
- return NO_MEMORY;
-}
-
-status_t String16::insert(size_t pos, const char16_t* chrs) {
- return insert(pos, chrs, strlen16(chrs));
-}
-
-status_t String16::insert(size_t pos, const char16_t* chrs, size_t len) {
- const size_t myLen = size();
- if (myLen == 0) {
- return setTo(chrs, len);
- return OK;
- } else if (len == 0) {
- return OK;
- }
-
- if (pos > myLen) pos = myLen;
-
-#if 0
- printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n",
- String8(*this).string(), pos,
- len, myLen, String8(chrs, len).string());
-#endif
-
- SharedBuffer* buf = static_cast<SharedBuffer*>(
- editResize((myLen + len + 1) * sizeof(char16_t)));
- if (buf) {
- char16_t* str = static_cast<char16_t*>(buf->data());
- if (pos < myLen) {
- memmove(str + pos + len, str + pos, (myLen - pos) * sizeof(char16_t));
- }
- memcpy(str + pos, chrs, len * sizeof(char16_t));
- str[myLen + len] = 0;
- mString = str;
-#if 0
- printf("Result (%d chrs): %s\n", size(), String8(*this).string());
-#endif
- return OK;
- }
- return NO_MEMORY;
-}
-
-ssize_t String16::findFirst(char16_t c) const {
- const char16_t* str = string();
- const char16_t* p = str;
- const char16_t* e = p + size();
- while (p < e) {
- if (*p == c) {
- return p - str;
- }
- p++;
- }
- return -1;
-}
-
-ssize_t String16::findLast(char16_t c) const {
- const char16_t* str = string();
- const char16_t* p = str;
- const char16_t* e = p + size();
- while (p < e) {
- e--;
- if (*e == c) {
- return e - str;
- }
- }
- return -1;
-}
-
-bool String16::startsWith(const String16& prefix) const {
- const size_t ps = prefix.size();
- if (ps > size()) return false;
- return strzcmp16(mString, ps, prefix.string(), ps) == 0;
-}
-
-bool String16::startsWith(const char16_t* prefix) const {
- const size_t ps = strlen16(prefix);
- if (ps > size()) return false;
- return strncmp16(mString, prefix, ps) == 0;
-}
-
-bool String16::contains(const char16_t* chrs) const {
- return strstr16(mString, chrs) != nullptr;
-}
-
-void* String16::edit() {
- SharedBuffer* buf;
- if (isStaticString()) {
- buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t)));
- if (buf) {
- memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t));
- }
- } else {
- buf = SharedBuffer::bufferFromData(mString)->edit();
- buf->mClientMetadata = kIsSharedBufferAllocated;
- }
- return buf;
-}
-
-void* String16::editResize(size_t newSize) {
- SharedBuffer* buf;
- if (isStaticString()) {
- size_t copySize = (size() + 1) * sizeof(char16_t);
- if (newSize < copySize) {
- copySize = newSize;
- }
- buf = static_cast<SharedBuffer*>(alloc(newSize));
- if (buf) {
- memcpy(buf->data(), mString, copySize);
- }
- } else {
- buf = SharedBuffer::bufferFromData(mString)->editResize(newSize);
- buf->mClientMetadata = kIsSharedBufferAllocated;
- }
- return buf;
-}
-
-void String16::acquire() {
- if (!isStaticString()) {
- SharedBuffer::bufferFromData(mString)->acquire();
- }
-}
-
-void String16::release() {
- if (!isStaticString()) {
- SharedBuffer::bufferFromData(mString)->release();
- }
-}
-
-bool String16::isStaticString() const {
- // See String16.h for notes on the memory layout of String16::StaticData and
- // SharedBuffer.
- static_assert(
- sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
- const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
- return (*(p - 1) & kIsSharedBufferAllocated) == 0;
-}
-
-size_t String16::staticStringSize() const {
- // See String16.h for notes on the memory layout of String16::StaticData and
- // SharedBuffer.
- static_assert(
- sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
- const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
- return static_cast<size_t>(*(p - 1));
-}
-
-status_t String16::makeLower() {
- const size_t N = size();
- const char16_t* str = string();
- char16_t* edited = nullptr;
- for (size_t i = 0; i < N; i++) {
- const char16_t v = str[i];
- if (v >= 'A' && v <= 'Z') {
- if (!edited) {
- SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
- if (!buf) {
- return NO_MEMORY;
- }
- edited = static_cast<char16_t*>(buf->data());
- mString = str = edited;
- }
- edited[i] = tolower(static_cast<char>(v));
- }
- }
- return OK;
-}
-
-status_t String16::replaceAll(char16_t replaceThis, char16_t withThis) {
- const size_t N = size();
- const char16_t* str = string();
- char16_t* edited = nullptr;
- for (size_t i = 0; i < N; i++) {
- if (str[i] == replaceThis) {
- if (!edited) {
- SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
- if (!buf) {
- return NO_MEMORY;
- }
- edited = static_cast<char16_t*>(buf->data());
- mString = str = edited;
- }
- edited[i] = withThis;
- }
- }
- return OK;
-}
-
-status_t String16::remove(size_t len, size_t begin) {
- const size_t N = size();
- if (begin >= N) {
- release();
- mString = getEmptyString();
- return OK;
- }
- if (len > N || len > N - begin) len = N - begin;
- if (begin == 0 && len == N) {
- return OK;
- }
-
- if (begin > 0) {
- SharedBuffer* buf =
- static_cast<SharedBuffer*>(editResize((N + 1) * sizeof(char16_t)));
- if (!buf) {
- return NO_MEMORY;
- }
- char16_t* str = static_cast<char16_t*>(buf->data());
- memmove(str, str + begin, (N - begin + 1) * sizeof(char16_t));
- mString = str;
- }
- SharedBuffer* buf =
- static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
- if (buf) {
- char16_t* str = static_cast<char16_t*>(buf->data());
- str[len] = 0;
- mString = str;
- return OK;
- }
- return NO_MEMORY;
-}
-
-}; // namespace android
diff --git a/nativeruntime/cpp/libutils/String8.cpp b/nativeruntime/cpp/libutils/String8.cpp
deleted file mode 100644
index 140706451..000000000
--- a/nativeruntime/cpp/libutils/String8.cpp
+++ /dev/null
@@ -1,575 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/String8.cpp
-
-#define __STDC_LIMIT_MACROS
-#include <ctype.h>
-#include <log/log.h>
-#include <stdint.h>
-#include <utils/Compat.h>
-#include <utils/String16.h>
-#include <utils/String8.h>
-
-#include "SharedBuffer.h"
-
-/*
- * Functions outside android is below the namespace android, since they use
- * functions and constants in android namespace.
- */
-
-// ---------------------------------------------------------------------------
-
-namespace android {
-
-// Separator used by resource paths. This is not platform dependent contrary
-// to OS_PATH_SEPARATOR.
-#define RES_PATH_SEPARATOR '/'
-
-static inline char* getEmptyString() {
- static SharedBuffer* gEmptyStringBuf = [] {
- SharedBuffer* buf = SharedBuffer::alloc(1);
- char* str = static_cast<char*>(buf->data());
- *str = 0;
- return buf;
- }();
-
- gEmptyStringBuf->acquire();
- return static_cast<char*>(gEmptyStringBuf->data());
-}
-
-// ---------------------------------------------------------------------------
-
-static char* allocFromUTF8(const char* in, size_t len) {
- if (len > 0) {
- if (len == SIZE_MAX) {
- return nullptr;
- }
- SharedBuffer* buf = SharedBuffer::alloc(len + 1);
- ALOG_ASSERT(buf, "Unable to allocate shared buffer");
- if (buf) {
- char* str = static_cast<char*>(buf->data());
- memcpy(str, in, len);
- str[len] = 0;
- return str;
- }
- return nullptr;
- }
-
- return getEmptyString();
-}
-
-static char* allocFromUTF16(const char16_t* in, size_t len) {
- if (len == 0) return getEmptyString();
-
- // Allow for closing '\0'
- const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
- if (resultStrLen < 1) {
- return getEmptyString();
- }
-
- SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
- ALOG_ASSERT(buf, "Unable to allocate shared buffer");
- if (!buf) {
- return getEmptyString();
- }
-
- char* resultStr = static_cast<char*>(buf->data());
- utf16_to_utf8(in, len, resultStr, resultStrLen);
- return resultStr;
-}
-
-static char* allocFromUTF32(const char32_t* in, size_t len) {
- if (len == 0) {
- return getEmptyString();
- }
-
- const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1;
- if (resultStrLen < 1) {
- return getEmptyString();
- }
-
- SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
- ALOG_ASSERT(buf, "Unable to allocate shared buffer");
- if (!buf) {
- return getEmptyString();
- }
-
- char* resultStr = static_cast<char*>(buf->data());
- utf32_to_utf8(in, len, resultStr, resultStrLen);
-
- return resultStr;
-}
-
-// ---------------------------------------------------------------------------
-
-String8::String8() : mString(getEmptyString()) {}
-
-String8::String8(StaticLinkage) : mString(nullptr) {
- // this constructor is used when we can't rely on the static-initializers
- // having run. In this case we always allocate an empty string. It's less
- // efficient than using getEmptyString(), but we assume it's uncommon.
-
- char* data = static_cast<char*>(SharedBuffer::alloc(sizeof(char))->data());
- data[0] = 0;
- mString = data;
-}
-
-String8::String8(const String8& o) : mString(o.mString) {
- SharedBuffer::bufferFromData(mString)->acquire();
-}
-
-String8::String8(const char* o) : mString(allocFromUTF8(o, strlen(o))) {
- if (mString == nullptr) {
- mString = getEmptyString();
- }
-}
-
-String8::String8(const char* o, size_t len) : mString(allocFromUTF8(o, len)) {
- if (mString == nullptr) {
- mString = getEmptyString();
- }
-}
-
-String8::String8(const String16& o)
- : mString(allocFromUTF16(o.string(), o.size())) {}
-
-String8::String8(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
-
-String8::String8(const char16_t* o, size_t len)
- : mString(allocFromUTF16(o, len)) {}
-
-String8::String8(const char32_t* o) : mString(allocFromUTF32(o, strlen32(o))) {}
-
-String8::String8(const char32_t* o, size_t len)
- : mString(allocFromUTF32(o, len)) {}
-
-String8::~String8() {
- if (mString != nullptr) {
- SharedBuffer::bufferFromData(mString)->release();
- }
-}
-
-size_t String8::length() const {
- return SharedBuffer::sizeFromData(mString) - 1;
-}
-
-String8 String8::format(const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
-
- String8 result(formatV(fmt, args));
-
- va_end(args);
- return result;
-}
-
-String8 String8::formatV(const char* fmt, va_list args) {
- String8 result;
- result.appendFormatV(fmt, args);
- return result;
-}
-
-void String8::clear() {
- SharedBuffer::bufferFromData(mString)->release();
- mString = getEmptyString();
-}
-
-void String8::setTo(const String8& other) {
- SharedBuffer::bufferFromData(other.mString)->acquire();
- SharedBuffer::bufferFromData(mString)->release();
- mString = other.mString;
-}
-
-status_t String8::setTo(const char* other) {
- const char* newString = allocFromUTF8(other, strlen(other));
- SharedBuffer::bufferFromData(mString)->release();
- mString = newString;
- if (mString) return OK;
-
- mString = getEmptyString();
- return NO_MEMORY;
-}
-
-status_t String8::setTo(const char* other, size_t len) {
- const char* newString = allocFromUTF8(other, len);
- SharedBuffer::bufferFromData(mString)->release();
- mString = newString;
- if (mString) return OK;
-
- mString = getEmptyString();
- return NO_MEMORY;
-}
-
-status_t String8::setTo(const char16_t* other, size_t len) {
- const char* newString = allocFromUTF16(other, len);
- SharedBuffer::bufferFromData(mString)->release();
- mString = newString;
- if (mString) return OK;
-
- mString = getEmptyString();
- return NO_MEMORY;
-}
-
-status_t String8::setTo(const char32_t* other, size_t len) {
- const char* newString = allocFromUTF32(other, len);
- SharedBuffer::bufferFromData(mString)->release();
- mString = newString;
- if (mString) return OK;
-
- mString = getEmptyString();
- return NO_MEMORY;
-}
-
-status_t String8::append(const String8& other) {
- const size_t otherLen = other.bytes();
- if (bytes() == 0) {
- setTo(other);
- return OK;
- } else if (otherLen == 0) {
- return OK;
- }
-
- return real_append(other.string(), otherLen);
-}
-
-status_t String8::append(const char* other) {
- return append(other, strlen(other));
-}
-
-status_t String8::append(const char* other, size_t otherLen) {
- if (bytes() == 0) {
- return setTo(other, otherLen);
- } else if (otherLen == 0) {
- return OK;
- }
-
- return real_append(other, otherLen);
-}
-
-status_t String8::appendFormat(const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
-
- status_t result = appendFormatV(fmt, args);
-
- va_end(args);
- return result;
-}
-
-status_t String8::appendFormatV(const char* fmt, va_list args) {
- int n, result = OK;
- va_list tmp_args;
-
- /* args is undefined after vsnprintf.
- * So we need a copy here to avoid the
- * second vsnprintf access undefined args.
- */
- va_copy(tmp_args, args);
- n = vsnprintf(nullptr, 0, fmt, tmp_args);
- va_end(tmp_args);
-
- if (n != 0) {
- size_t oldLength = length();
- char* buf = lockBuffer(oldLength + n);
- if (buf) {
- vsnprintf(buf + oldLength, n + 1, fmt, args);
- } else {
- result = NO_MEMORY;
- }
- }
- return result;
-}
-
-status_t String8::real_append(const char* other, size_t otherLen) {
- const size_t myLen = bytes();
-
- SharedBuffer* buf =
- SharedBuffer::bufferFromData(mString)->editResize(myLen + otherLen + 1);
- if (buf) {
- char* str = static_cast<char*>(buf->data());
- mString = str;
- str += myLen;
- memcpy(str, other, otherLen);
- str[otherLen] = '\0';
- return OK;
- }
- return NO_MEMORY;
-}
-
-char* String8::lockBuffer(size_t size) {
- SharedBuffer* buf =
- SharedBuffer::bufferFromData(mString)->editResize(size + 1);
- if (buf) {
- char* str = static_cast<char*>(buf->data());
- mString = str;
- return str;
- }
- return nullptr;
-}
-
-void String8::unlockBuffer() { unlockBuffer(strlen(mString)); }
-
-status_t String8::unlockBuffer(size_t size) {
- if (size != this->size()) {
- SharedBuffer* buf =
- SharedBuffer::bufferFromData(mString)->editResize(size + 1);
- if (!buf) {
- return NO_MEMORY;
- }
-
- char* str = static_cast<char*>(buf->data());
- str[size] = 0;
- mString = str;
- }
-
- return OK;
-}
-
-ssize_t String8::find(const char* other, size_t start) const {
- size_t len = size();
- if (start >= len) {
- return -1;
- }
- const char* s = mString + start;
- const char* p = strstr(s, other);
- return p ? p - mString : -1;
-}
-
-bool String8::removeAll(const char* other) {
- ssize_t index = find(other);
- if (index < 0) return false;
-
- char* buf = lockBuffer(size());
- if (!buf) return false; // out of memory
-
- size_t skip = strlen(other);
- size_t len = size();
- size_t tail = index;
- while (size_t(index) < len) {
- ssize_t next = find(other, index + skip);
- if (next < 0) {
- next = len;
- }
-
- memmove(buf + tail, buf + index + skip, next - index - skip);
- tail += next - index - skip;
- index = next;
- }
- unlockBuffer(tail);
- return true;
-}
-
-void String8::toLower() { toLower(0, size()); }
-
-void String8::toLower(size_t start, size_t length) {
- const size_t len = size();
- if (start >= len) {
- return;
- }
- if (start + length > len) {
- length = len - start;
- }
- char* buf = lockBuffer(len);
- buf += start;
- while (length > 0) {
- *buf = tolower(*buf);
- buf++;
- length--;
- }
- unlockBuffer(len);
-}
-
-void String8::toUpper() { toUpper(0, size()); }
-
-void String8::toUpper(size_t start, size_t length) {
- const size_t len = size();
- if (start >= len) {
- return;
- }
- if (start + length > len) {
- length = len - start;
- }
- char* buf = lockBuffer(len);
- buf += start;
- while (length > 0) {
- *buf = toupper(*buf);
- buf++;
- length--;
- }
- unlockBuffer(len);
-}
-
-// ---------------------------------------------------------------------------
-// Path functions
-
-void String8::setPathName(const char* name) { setPathName(name, strlen(name)); }
-
-void String8::setPathName(const char* name, size_t len) {
- char* buf = lockBuffer(len);
-
- memcpy(buf, name, len);
-
- // remove trailing path separator, if present
- if (len > 0 && buf[len - 1] == OS_PATH_SEPARATOR) len--;
-
- buf[len] = '\0';
-
- unlockBuffer(len);
-}
-
-String8 String8::getPathLeaf() const {
- const char* cp;
- const char* const buf = mString;
-
- cp = strrchr(buf, OS_PATH_SEPARATOR);
- if (cp == nullptr)
- return String8(*this);
- else
- return String8(cp + 1);
-}
-
-String8 String8::getPathDir() const {
- const char* cp;
- const char* const str = mString;
-
- cp = strrchr(str, OS_PATH_SEPARATOR);
- if (cp == nullptr)
- return String8("");
- else
- return String8(str, cp - str);
-}
-
-String8 String8::walkPath(String8* outRemains) const {
- const char* cp;
- const char* const str = mString;
- const char* buf = str;
-
- cp = strchr(buf, OS_PATH_SEPARATOR);
- if (cp == buf) {
- // don't include a leading '/'.
- buf = buf + 1;
- cp = strchr(buf, OS_PATH_SEPARATOR);
- }
-
- if (cp == nullptr) {
- String8 res = buf != str ? String8(buf) : *this;
- if (outRemains) *outRemains = String8("");
- return res;
- }
-
- String8 res(buf, cp - buf);
- if (outRemains) *outRemains = String8(cp + 1);
- return res;
-}
-
-/*
- * Helper function for finding the start of an extension in a pathname.
- *
- * Returns a pointer inside mString, or NULL if no extension was found.
- */
-char* String8::find_extension() const {
- const char* lastSlash;
- const char* lastDot;
- const char* const str = mString;
-
- // only look at the filename
- lastSlash = strrchr(str, OS_PATH_SEPARATOR);
- if (lastSlash == nullptr)
- lastSlash = str;
- else
- lastSlash++;
-
- // find the last dot
- lastDot = strrchr(lastSlash, '.');
- if (lastDot == nullptr) return nullptr;
-
- // looks good, ship it
- return const_cast<char*>(lastDot);
-}
-
-String8 String8::getPathExtension() const {
- char* ext;
-
- ext = find_extension();
- if (ext != nullptr)
- return String8(ext);
- else
- return String8("");
-}
-
-String8 String8::getBasePath() const {
- char* ext;
- const char* const str = mString;
-
- ext = find_extension();
- if (ext == nullptr)
- return String8(*this);
- else
- return String8(str, ext - str);
-}
-
-String8& String8::appendPath(const char* name) {
- // TODO: The test below will fail for Win32 paths. Fix later or ignore.
- if (name[0] != OS_PATH_SEPARATOR) {
- if (*name == '\0') {
- // nothing to do
- return *this;
- }
-
- size_t len = length();
- if (len == 0) {
- // no existing filename, just use the new one
- setPathName(name);
- return *this;
- }
-
- // make room for oldPath + '/' + newPath
- int newlen = strlen(name);
-
- char* buf = lockBuffer(len + 1 + newlen);
-
- // insert a '/' if needed
- if (buf[len - 1] != OS_PATH_SEPARATOR) buf[len++] = OS_PATH_SEPARATOR;
-
- memcpy(buf + len, name, newlen + 1);
- len += newlen;
-
- unlockBuffer(len);
-
- return *this;
- } else {
- setPathName(name);
- return *this;
- }
-}
-
-String8& String8::convertToResPath() {
-#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
- size_t len = length();
- if (len > 0) {
- char* buf = lockBuffer(len);
- for (char* end = buf + len; buf < end; ++buf) {
- if (*buf == OS_PATH_SEPARATOR) *buf = RES_PATH_SEPARATOR;
- }
- unlockBuffer(len);
- }
-#endif
- return *this;
-}
-
-}; // namespace android
diff --git a/nativeruntime/cpp/libutils/Unicode.cpp b/nativeruntime/cpp/libutils/Unicode.cpp
deleted file mode 100644
index 64c769702..000000000
--- a/nativeruntime/cpp/libutils/Unicode.cpp
+++ /dev/null
@@ -1,561 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/Unicode.cpp
-
-#define LOG_TAG "unicode"
-
-#include <android-base/macros.h>
-#include <limits.h>
-#include <log/log.h>
-#include <utils/Unicode.h>
-
-#if defined(_WIN32)
-#undef nhtol
-#undef htonl
-#undef nhtos
-#undef htons
-
-#define ntohl(x) \
- (((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | \
- (((x) >> 8) & 0xff00))
-#define htonl(x) ntohl(x)
-#define ntohs(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 255))
-#define htons(x) ntohs(x)
-#else
-#include <netinet/in.h>
-#endif
-
-extern "C" {
-
-static const char32_t kByteMask = 0x000000BF;
-static const char32_t kByteMark = 0x00000080;
-
-// Surrogates aren't valid for UTF-32 characters, so define some
-// constants that will let us screen them out.
-static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
-// Unused, here for completeness:
-// static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
-// static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
-static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
-static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
-static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
-static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF;
-
-// Mask used to set appropriate bits in first byte of UTF-8 sequence,
-// indexed by number of bytes in the sequence.
-// 0xxxxxxx
-// -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000
-// 110yyyyx 10xxxxxx
-// -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0
-// 1110yyyy 10yxxxxx 10xxxxxx
-// -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0
-// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
-// -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0
-static const char32_t kFirstByteMark[] = {0x00000000, 0x00000000, 0x000000C0,
- 0x000000E0, 0x000000F0};
-
-// --------------------------------------------------------------------------
-// UTF-32
-// --------------------------------------------------------------------------
-
-/**
- * Return number of UTF-8 bytes required for the character. If the character
- * is invalid, return size of 0.
- */
-static inline size_t utf32_codepoint_utf8_length(char32_t srcChar) {
- // Figure out how many bytes the result will require.
- if (srcChar < 0x00000080) {
- return 1;
- } else if (srcChar < 0x00000800) {
- return 2;
- } else if (srcChar < 0x00010000) {
- if ((srcChar < kUnicodeSurrogateStart) ||
- (srcChar > kUnicodeSurrogateEnd)) {
- return 3;
- } else {
- // Surrogates are invalid UTF-32 characters.
- return 0;
- }
- }
- // Max code point for Unicode is 0x0010FFFF.
- else if (srcChar <= kUnicodeMaxCodepoint) {
- return 4;
- } else {
- // Invalid UTF-32 character.
- return 0;
- }
-}
-
-// Write out the source character to <dstP>.
-
-static inline void utf32_codepoint_to_utf8(uint8_t *dstP, char32_t srcChar,
- size_t bytes) {
- dstP += bytes;
- switch (bytes) { /* note: everything falls through. */
- case 4:
- *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask);
- srcChar >>= 6;
- FALLTHROUGH_INTENDED;
- case 3:
- *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask);
- srcChar >>= 6;
- FALLTHROUGH_INTENDED;
- case 2:
- *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask);
- srcChar >>= 6;
- FALLTHROUGH_INTENDED;
- case 1:
- *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]);
- }
-}
-
-size_t strlen32(const char32_t *s) {
- const char32_t *ss = s;
- while (*ss) ss++;
- return ss - s;
-}
-
-size_t strnlen32(const char32_t *s, size_t maxlen) {
- const char32_t *ss = s;
- while ((maxlen > 0) && *ss) {
- ss++;
- maxlen--;
- }
- return ss - s;
-}
-
-static inline int32_t utf32_at_internal(const char *cur, size_t *num_read) {
- const char first_char = *cur;
- if ((first_char & 0x80) == 0) { // ASCII
- *num_read = 1;
- return *cur;
- }
- cur++;
- char32_t mask, to_ignore_mask;
- size_t num_to_read = 0;
- char32_t utf32 = first_char;
- for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80;
- (first_char & mask); num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
- // 0x3F == 00111111
- utf32 = (utf32 << 6) + (*cur++ & 0x3F);
- }
- to_ignore_mask |= mask;
- utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1)));
-
- *num_read = num_to_read;
- return static_cast<int32_t>(utf32);
-}
-
-int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index,
- size_t *next_index) {
- if (index >= src_len) {
- return -1;
- }
- size_t dummy_index;
- if (next_index == nullptr) {
- next_index = &dummy_index;
- }
- size_t num_read;
- int32_t ret = utf32_at_internal(src + index, &num_read);
- if (ret >= 0) {
- *next_index = index + num_read;
- }
-
- return ret;
-}
-
-ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len) {
- if (src == nullptr || src_len == 0) {
- return -1;
- }
-
- size_t ret = 0;
- const char32_t *end = src + src_len;
- while (src < end) {
- size_t char_len = utf32_codepoint_utf8_length(*src++);
- if (SSIZE_MAX - char_len < ret) {
- // If this happens, we would overflow the ssize_t type when
- // returning from this function, so we cannot express how
- // long this string is in an ssize_t.
- android_errorWriteLog(0x534e4554, "37723026");
- return -1;
- }
- ret += char_len;
- }
- return ret;
-}
-
-void utf32_to_utf8(const char32_t *src, size_t src_len, char *dst,
- size_t dst_len) {
- if (src == nullptr || src_len == 0 || dst == nullptr) {
- return;
- }
-
- const char32_t *cur_utf32 = src;
- const char32_t *end_utf32 = src + src_len;
- char *cur = dst;
- while (cur_utf32 < end_utf32) {
- size_t len = utf32_codepoint_utf8_length(*cur_utf32);
- LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
- utf32_codepoint_to_utf8((uint8_t *)cur, *cur_utf32++, len);
- cur += len;
- dst_len -= len;
- }
- LOG_ALWAYS_FATAL_IF(dst_len < 1, "dst_len < 1: %zu < 1", dst_len);
- *cur = '\0';
-}
-
-// --------------------------------------------------------------------------
-// UTF-16
-// --------------------------------------------------------------------------
-
-int strcmp16(const char16_t *s1, const char16_t *s2) {
- char16_t ch;
- int d = 0;
-
- while (true) {
- d = (int)(ch = *s1++) - (int)*s2++;
- if (d || !ch) break;
- }
-
- return d;
-}
-
-int strncmp16(const char16_t *s1, const char16_t *s2, size_t n) {
- char16_t ch;
- int d = 0;
-
- if (n == 0) {
- return 0;
- }
-
- do {
- d = (int)(ch = *s1++) - (int)*s2++;
- if (d || !ch) {
- break;
- }
- } while (--n);
-
- return d;
-}
-
-char16_t *strcpy16(char16_t *dst, const char16_t *src) {
- char16_t *q = dst;
- const char16_t *p = src;
- char16_t ch;
-
- do {
- *q++ = ch = *p++;
- } while (ch);
-
- return dst;
-}
-
-size_t strlen16(const char16_t *s) {
- const char16_t *ss = s;
- while (*ss) ss++;
- return ss - s;
-}
-
-size_t strnlen16(const char16_t *s, size_t maxlen) {
- const char16_t *ss = s;
-
- /* Important: the maxlen test must precede the reference through ss;
- since the byte beyond the maximum may segfault */
- while ((maxlen > 0) && *ss) {
- ss++;
- maxlen--;
- }
- return ss - s;
-}
-
-char16_t *strstr16(const char16_t *src, const char16_t *target) {
- const char16_t needle = *target;
- if (needle == '\0') return (char16_t *)src;
-
- const size_t target_len = strlen16(++target);
- do {
- do {
- if (*src == '\0') {
- return nullptr;
- }
- } while (*src++ != needle);
- } while (strncmp16(src, target, target_len) != 0);
- src--;
-
- return (char16_t *)src;
-}
-
-int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2) {
- const char16_t *e1 = s1 + n1;
- const char16_t *e2 = s2 + n2;
-
- while (s1 < e1 && s2 < e2) {
- const int d = (int)*s1++ - (int)*s2++;
- if (d) {
- return d;
- }
- }
-
- return n1 < n2 ? (0 - (int)*s2) : (n1 > n2 ? ((int)*s1 - 0) : 0);
-}
-
-void utf16_to_utf8(const char16_t *src, size_t src_len, char *dst,
- size_t dst_len) {
- if (src == nullptr || src_len == 0 || dst == nullptr) {
- return;
- }
-
- const char16_t *cur_utf16 = src;
- const char16_t *const end_utf16 = src + src_len;
- char *cur = dst;
- while (cur_utf16 < end_utf16) {
- char32_t utf32;
- // surrogate pairs
- if ((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16 &&
- (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
- utf32 = (*cur_utf16++ - 0xD800) << 10;
- utf32 |= *cur_utf16++ - 0xDC00;
- utf32 += 0x10000;
- } else {
- utf32 = (char32_t)*cur_utf16++;
- }
- const size_t len = utf32_codepoint_utf8_length(utf32);
- LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
- utf32_codepoint_to_utf8((uint8_t *)cur, utf32, len);
- cur += len;
- dst_len -= len;
- }
- LOG_ALWAYS_FATAL_IF(dst_len < 1, "%zu < 1", dst_len);
- *cur = '\0';
-}
-
-// --------------------------------------------------------------------------
-// UTF-8
-// --------------------------------------------------------------------------
-
-ssize_t utf8_length(const char *src) {
- const char *cur = src;
- size_t ret = 0;
- while (*cur != '\0') {
- const char first_char = *cur++;
- if ((first_char & 0x80) == 0) { // ASCII
- ret += 1;
- continue;
- }
- // (UTF-8's character must not be like 10xxxxxx,
- // but 110xxxxx, 1110xxxx, ... or 1111110x)
- if ((first_char & 0x40) == 0) {
- return -1;
- }
-
- int32_t mask, to_ignore_mask;
- size_t num_to_read = 0;
- char32_t utf32 = 0;
- for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
- num_to_read < 5 && (first_char & mask);
- num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
- if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx
- return -1;
- }
- // 0x3F == 00111111
- utf32 = (utf32 << 6) + (*cur++ & 0x3F);
- }
- // "first_char" must be (110xxxxx - 11110xxx)
- if (num_to_read == 5) {
- return -1;
- }
- to_ignore_mask |= mask;
- utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
- if (utf32 > kUnicodeMaxCodepoint) {
- return -1;
- }
-
- ret += num_to_read;
- }
- return ret;
-}
-
-ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len) {
- if (src == nullptr || src_len == 0) {
- return -1;
- }
-
- size_t ret = 0;
- const char16_t *const end = src + src_len;
- while (src < end) {
- size_t char_len;
- if ((*src & 0xFC00) == 0xD800 && (src + 1) < end &&
- (*(src + 1) & 0xFC00) == 0xDC00) {
- // surrogate pairs are always 4 bytes.
- char_len = 4;
- src += 2;
- } else {
- char_len = utf32_codepoint_utf8_length((char32_t)*src++);
- }
- if (SSIZE_MAX - char_len < ret) {
- // If this happens, we would overflow the ssize_t type when
- // returning from this function, so we cannot express how
- // long this string is in an ssize_t.
- android_errorWriteLog(0x534e4554, "37723026");
- return -1;
- }
- ret += char_len;
- }
- return ret;
-}
-
-/**
- * Returns 1-4 based on the number of leading bits.
- *
- * 1111 -> 4
- * 1110 -> 3
- * 110x -> 2
- * 10xx -> 1
- * 0xxx -> 1
- */
-static inline size_t utf8_codepoint_len(uint8_t ch) {
- return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
-}
-
-static inline void utf8_shift_and_mask(uint32_t *codePoint,
- const uint8_t byte) {
- *codePoint <<= 6;
- *codePoint |= 0x3F & byte;
-}
-
-static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src,
- size_t length) {
- uint32_t unicode;
-
- switch (length) {
- case 1:
- return src[0];
- case 2:
- unicode = src[0] & 0x1f;
- utf8_shift_and_mask(&unicode, src[1]);
- return unicode;
- case 3:
- unicode = src[0] & 0x0f;
- utf8_shift_and_mask(&unicode, src[1]);
- utf8_shift_and_mask(&unicode, src[2]);
- return unicode;
- case 4:
- unicode = src[0] & 0x07;
- utf8_shift_and_mask(&unicode, src[1]);
- utf8_shift_and_mask(&unicode, src[2]);
- utf8_shift_and_mask(&unicode, src[3]);
- return unicode;
- default:
- return 0xffff;
- }
-
- // printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result);
-}
-
-ssize_t utf8_to_utf16_length(const uint8_t *u8str, size_t u8len,
- bool overreadIsFatal) {
- const uint8_t *const u8end = u8str + u8len;
- const uint8_t *u8cur = u8str;
-
- /* Validate that the UTF-8 is the correct len */
- size_t u16measuredLen = 0;
- while (u8cur < u8end) {
- u16measuredLen++;
- int u8charLen = utf8_codepoint_len(*u8cur);
- // Malformed utf8, some characters are beyond the end.
- // Cases:
- // If u8charLen == 1, this becomes u8cur >= u8end, which cannot happen as
- // u8cur < u8end, then this condition fail and we continue, as expected. If
- // u8charLen == 2, this becomes u8cur + 1 >= u8end, which fails only if
- // u8cur == u8end - 1, that is, there was only one remaining character to
- // read but we need 2 of them. This condition holds and we return -1, as
- // expected.
- if (u8cur + u8charLen - 1 >= u8end) {
- if (overreadIsFatal) {
- LOG_ALWAYS_FATAL("Attempt to overread computing length of utf8 string");
- } else {
- return -1;
- }
- }
- uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen);
- if (codepoint > 0xFFFF)
- u16measuredLen++; // this will be a surrogate pair in utf16
- u8cur += u8charLen;
- }
-
- /**
- * Make sure that we ended where we thought we would and the output UTF-16
- * will be exactly how long we were told it would be.
- */
- if (u8cur != u8end) {
- return -1;
- }
-
- return u16measuredLen;
-}
-
-char16_t *utf8_to_utf16(const uint8_t *u8str, size_t u8len, char16_t *u16str,
- size_t u16len) {
- // A value > SSIZE_MAX is probably a negative value returned as an error and
- // casted.
- LOG_ALWAYS_FATAL_IF(u16len == 0 || u16len > SSIZE_MAX, "u16len is %zu",
- u16len);
- char16_t *end =
- utf8_to_utf16_no_null_terminator(u8str, u8len, u16str, u16len - 1);
- *end = 0;
- return end;
-}
-
-char16_t *utf8_to_utf16_no_null_terminator(const uint8_t *src, size_t srcLen,
- char16_t *dst, size_t dstLen) {
- if (dstLen == 0) {
- return dst;
- }
- // A value > SSIZE_MAX is probably a negative value returned as an error and
- // casted.
- LOG_ALWAYS_FATAL_IF(dstLen > SSIZE_MAX, "dstLen is %zu", dstLen);
- const uint8_t *const u8end = src + srcLen;
- const uint8_t *u8cur = src;
- const char16_t *const u16end = dst + dstLen;
- char16_t *u16cur = dst;
-
- while (u8cur < u8end && u16cur < u16end) {
- size_t u8len = utf8_codepoint_len(*u8cur);
- uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
-
- // Convert the UTF32 codepoint to one or more UTF16 codepoints
- if (codepoint <= 0xFFFF) {
- // Single UTF16 character
- *u16cur++ = (char16_t)codepoint;
- } else {
- // Multiple UTF16 characters with surrogates
- codepoint = codepoint - 0x10000;
- *u16cur++ = (char16_t)((codepoint >> 10) + 0xD800);
- if (u16cur >= u16end) {
- // Ooops... not enough room for this surrogate pair.
- return u16cur - 1;
- }
- *u16cur++ = (char16_t)((codepoint & 0x3FF) + 0xDC00);
- }
-
- u8cur += u8len;
- }
- return u16cur;
-}
-}
diff --git a/nativeruntime/cpp/libutils/include/utils/Compat.h b/nativeruntime/cpp/libutils/include/utils/Compat.h
deleted file mode 100644
index 2846441d3..000000000
--- a/nativeruntime/cpp/libutils/include/utils/Compat.h
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/include/utils/Compat.h
-
-#ifndef __LIB_UTILS_COMPAT_H
-#define __LIB_UTILS_COMPAT_H
-
-#include <unistd.h>
-
-#if !defined(__MINGW32__)
-#include <sys/mman.h>
-#endif
-
-#if defined(__APPLE__)
-
-/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
-static_assert(sizeof(off_t) >= 8,
- "This code requires that Mac OS have at least a 64-bit off_t.");
-typedef off_t off64_t;
-
-static inline void* mmap64(void* addr, size_t length, int prot, int flags,
- int fd, off64_t offset) {
- return mmap(addr, length, prot, flags, fd, offset);
-}
-
-static inline off64_t lseek64(int fd, off64_t offset, int whence) {
- return lseek(fd, offset, whence);
-}
-
-static inline ssize_t pread64(int fd, void* buf, size_t nbytes,
- off64_t offset) {
- return pread(fd, buf, nbytes, offset);
-}
-
-static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes,
- off64_t offset) {
- return pwrite(fd, buf, nbytes, offset);
-}
-
-static inline int ftruncate64(int fd, off64_t length) {
- return ftruncate(fd, length);
-}
-
-#endif /* __APPLE__ */
-
-#if defined(_WIN32)
-#define O_CLOEXEC O_NOINHERIT
-#define O_NOFOLLOW 0
-#define DEFFILEMODE 0666
-#endif /* _WIN32 */
-
-#define ZD "%zd"
-#define ZD_TYPE ssize_t
-
-/*
- * Needed for cases where something should be constexpr if possible, but not
- * being constexpr is fine if in pre-C++11 code (such as a const static float
- * member variable).
- */
-#if __cplusplus >= 201103L
-#define CONSTEXPR constexpr
-#else
-#define CONSTEXPR
-#endif
-
-/*
- * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
- * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
- * not already defined, then define it here.
- */
-#ifndef TEMP_FAILURE_RETRY
-/* Used to retry syscalls that can return EINTR. */
-#define TEMP_FAILURE_RETRY(exp) \
- ({ \
- typeof(exp) _rc; \
- do { \
- _rc = (exp); \
- } while (_rc == -1 && errno == EINTR); \
- _rc; \
- })
-#endif
-
-#if defined(_WIN32)
-#define OS_PATH_SEPARATOR '\\'
-#else
-#define OS_PATH_SEPARATOR '/'
-#endif
-
-#endif /* __LIB_UTILS_COMPAT_H */
diff --git a/nativeruntime/cpp/libutils/include/utils/Errors.h b/nativeruntime/cpp/libutils/include/utils/Errors.h
deleted file mode 100644
index 673f2ccbc..000000000
--- a/nativeruntime/cpp/libutils/include/utils/Errors.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/include/utils/Errors.h
-
-#ifndef ANDROID_ERRORS_H
-#define ANDROID_ERRORS_H
-
-#include <errno.h>
-#include <stdint.h>
-#include <sys/types.h>
-
-#include <string>
-
-namespace android {
-
-/**
- * The type used to return success/failure from frameworks APIs.
- * See the anonymous enum below for valid values.
- */
-typedef int32_t status_t;
-
-/*
- * Error codes.
- * All error codes are negative values.
- */
-
-// Win32 #defines NO_ERROR as well. It has the same value, so there's no
-// real conflict, though it's a bit awkward.
-#ifdef _WIN32
-#undef NO_ERROR
-#endif
-
-enum {
- OK = 0, // Preferred constant for checking success.
- NO_ERROR = OK, // Deprecated synonym for `OK`. Prefer `OK` because it doesn't
- // conflict with Windows.
-
- UNKNOWN_ERROR = (-2147483647 - 1), // INT32_MIN value
-
- NO_MEMORY = -ENOMEM,
- INVALID_OPERATION = -ENOSYS,
- BAD_VALUE = -EINVAL,
- BAD_TYPE = (UNKNOWN_ERROR + 1),
- NAME_NOT_FOUND = -ENOENT,
- PERMISSION_DENIED = -EPERM,
- NO_INIT = -ENODEV,
- ALREADY_EXISTS = -EEXIST,
- DEAD_OBJECT = -EPIPE,
- FAILED_TRANSACTION = (UNKNOWN_ERROR + 2),
-#if !defined(_WIN32)
- BAD_INDEX = -EOVERFLOW,
- NOT_ENOUGH_DATA = -ENODATA,
- WOULD_BLOCK = -EWOULDBLOCK,
- TIMED_OUT = -ETIMEDOUT,
- UNKNOWN_TRANSACTION = -EBADMSG,
-#else
- BAD_INDEX = -E2BIG,
- NOT_ENOUGH_DATA = (UNKNOWN_ERROR + 3),
- WOULD_BLOCK = (UNKNOWN_ERROR + 4),
- TIMED_OUT = (UNKNOWN_ERROR + 5),
- UNKNOWN_TRANSACTION = (UNKNOWN_ERROR + 6),
-#endif
- FDS_NOT_ALLOWED = (UNKNOWN_ERROR + 7),
- UNEXPECTED_NULL = (UNKNOWN_ERROR + 8),
-};
-
-// Human readable name of error
-std::string statusToString(status_t status);
-
-// Restore define; enumeration is in "android" namespace, so the value defined
-// there won't work for Win32 code in a different namespace.
-#ifdef _WIN32
-#define NO_ERROR 0L
-#endif
-
-} // namespace android
-
-#endif // ANDROID_ERRORS_H
diff --git a/nativeruntime/cpp/libutils/include/utils/String16.h b/nativeruntime/cpp/libutils/include/utils/String16.h
deleted file mode 100644
index 6794b3274..000000000
--- a/nativeruntime/cpp/libutils/include/utils/String16.h
+++ /dev/null
@@ -1,331 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/include/utils/String16.h
-
-#ifndef ANDROID_STRING16_H
-#define ANDROID_STRING16_H
-
-#include <utils/Errors.h>
-#include <utils/String8.h>
-#include <utils/TypeHelpers.h>
-
-#include <iostream>
-#include <string>
-
-// ---------------------------------------------------------------------------
-
-namespace android {
-
-// ---------------------------------------------------------------------------
-
-template <size_t N>
-class StaticString16;
-
-// DO NOT USE: please use std::u16string
-
-//! This is a string holding UTF-16 characters.
-class String16 {
- public:
- /*
- * Use String16(StaticLinkage) if you're statically linking against
- * libutils and declaring an empty static String16, e.g.:
- *
- * static String16 sAStaticEmptyString(String16::kEmptyString);
- * static String16 sAnotherStaticEmptyString(sAStaticEmptyString);
- */
- enum StaticLinkage { kEmptyString };
-
- String16();
- explicit String16(StaticLinkage);
- String16(const String16& o);
- String16(const String16& o, size_t len, size_t begin = 0);
- explicit String16(const char16_t* o);
- explicit String16(const char16_t* o, size_t len);
- explicit String16(const String8& o);
- explicit String16(const char* o);
- explicit String16(const char* o, size_t len);
-
- ~String16();
-
- inline const char16_t* string() const;
-
- private:
- static inline std::string std_string(const String16& str);
-
- public:
- size_t size() const;
- void setTo(const String16& other);
- status_t setTo(const char16_t* other);
- status_t setTo(const char16_t* other, size_t len);
- status_t setTo(const String16& other, size_t len, size_t begin = 0);
-
- status_t append(const String16& other);
- status_t append(const char16_t* chrs, size_t len);
-
- inline String16& operator=(const String16& other);
-
- inline String16& operator+=(const String16& other);
- inline String16 operator+(const String16& other) const;
-
- status_t insert(size_t pos, const char16_t* chrs);
- status_t insert(size_t pos, const char16_t* chrs, size_t len);
-
- ssize_t findFirst(char16_t c) const;
- ssize_t findLast(char16_t c) const;
-
- bool startsWith(const String16& prefix) const;
- bool startsWith(const char16_t* prefix) const;
-
- bool contains(const char16_t* chrs) const;
-
- status_t makeLower();
-
- status_t replaceAll(char16_t replaceThis, char16_t withThis);
-
- status_t remove(size_t len, size_t begin = 0);
-
- inline int compare(const String16& other) const;
-
- inline bool operator<(const String16& other) const;
- inline bool operator<=(const String16& other) const;
- inline bool operator==(const String16& other) const;
- inline bool operator!=(const String16& other) const;
- inline bool operator>=(const String16& other) const;
- inline bool operator>(const String16& other) const;
-
- inline bool operator<(const char16_t* other) const;
- inline bool operator<=(const char16_t* other) const;
- inline bool operator==(const char16_t* other) const;
- inline bool operator!=(const char16_t* other) const;
- inline bool operator>=(const char16_t* other) const;
- inline bool operator>(const char16_t* other) const;
-
- inline operator const char16_t*() const;
-
- // Static and non-static String16 behave the same for the users, so
- // this method isn't of much use for the users. It is public for testing.
- bool isStaticString() const;
-
- private:
- /*
- * A flag indicating the type of underlying buffer.
- */
- static constexpr uint32_t kIsSharedBufferAllocated = 0x80000000;
-
- /*
- * alloc() returns void* so that SharedBuffer class is not exposed.
- */
- static void* alloc(size_t size);
- static char16_t* allocFromUTF8(const char* u8str, size_t u8len);
- static char16_t* allocFromUTF16(const char16_t* u16str, size_t u16len);
-
- /*
- * edit() and editResize() return void* so that SharedBuffer class
- * is not exposed.
- */
- void* edit();
- void* editResize(size_t newSize);
-
- void acquire();
- void release();
-
- size_t staticStringSize() const;
-
- const char16_t* mString;
-
- protected:
- /*
- * Data structure used to allocate static storage for static String16.
- *
- * Note that this data structure and SharedBuffer are used interchangeably
- * as the underlying data structure for a String16. Therefore, the layout
- * of this data structure must match the part in SharedBuffer that is
- * visible to String16.
- */
- template <size_t N>
- struct StaticData {
- // The high bit of 'size' is used as a flag.
- static_assert(N - 1 < kIsSharedBufferAllocated, "StaticString16 too long!");
- constexpr StaticData() : size(N - 1), data{0} {}
- const uint32_t size;
- char16_t data[N];
-
- constexpr StaticData(const StaticData<N>&) = default;
- };
-
- /*
- * Helper function for constructing a StaticData object.
- */
- template <size_t N>
- static constexpr const StaticData<N> makeStaticData(const char16_t (&s)[N]) {
- StaticData<N> r;
- // The 'size' field is at the same location where mClientMetadata would
- // be for a SharedBuffer. We do NOT set kIsSharedBufferAllocated flag
- // here.
- for (size_t i = 0; i < N - 1; ++i) r.data[i] = s[i];
- return r;
- }
-
- template <size_t N>
- explicit constexpr String16(const StaticData<N>& s) : mString(s.data) {}
-
- public:
- template <size_t N>
- explicit constexpr String16(const StaticString16<N>& s)
- : mString(s.mString) {}
-};
-
-// String16 can be trivially moved using memcpy() because moving does not
-// require any change to the underlying SharedBuffer contents or reference
-// count.
-ANDROID_TRIVIAL_MOVE_TRAIT(String16)
-
-static inline std::ostream& operator<<(std::ostream& os, const String16& str) {
- os << String8(str).c_str();
- return os;
-}
-
-// ---------------------------------------------------------------------------
-
-/*
- * A StaticString16 object is a specialized String16 object. Instead of holding
- * the string data in a ref counted SharedBuffer object, it holds data in a
- * buffer within StaticString16 itself. Note that this buffer is NOT ref
- * counted and is assumed to be available for as long as there is at least a
- * String16 object using it. Therefore, one must be extra careful to NEVER
- * assign a StaticString16 to a String16 that outlives the StaticString16
- * object.
- *
- * THE SAFEST APPROACH IS TO USE StaticString16 ONLY AS GLOBAL VARIABLES.
- *
- * A StaticString16 SHOULD NEVER APPEAR IN APIs. USE String16 INSTEAD.
- */
-template <size_t N>
-class StaticString16 : public String16 {
- public:
- constexpr StaticString16(const char16_t (&s)[N])
- : String16(mData), mData(makeStaticData(s)) {}
-
- constexpr StaticString16(const StaticString16<N>& other)
- : String16(mData), mData(other.mData) {}
-
- constexpr StaticString16(const StaticString16<N>&&) = delete;
-
- // There is no reason why one would want to 'new' a StaticString16. Delete
- // it to discourage misuse.
- static void* operator new(std::size_t) = delete;
-
- private:
- const StaticData<N> mData;
-};
-
-template <typename F>
-StaticString16(const F&) -> StaticString16<sizeof(F) / sizeof(char16_t)>;
-
-// ---------------------------------------------------------------------------
-// No user servicable parts below.
-
-inline int compare_type(const String16& lhs, const String16& rhs) {
- return lhs.compare(rhs);
-}
-
-inline int strictly_order_type(const String16& lhs, const String16& rhs) {
- return compare_type(lhs, rhs) < 0;
-}
-
-inline const char16_t* String16::string() const { return mString; }
-
-inline std::string String16::std_string(const String16& str) {
- return std::string(String8(str).string());
-}
-
-inline String16& String16::operator=(const String16& other) {
- setTo(other);
- return *this;
-}
-
-inline String16& String16::operator+=(const String16& other) {
- append(other);
- return *this;
-}
-
-inline String16 String16::operator+(const String16& other) const {
- String16 tmp(*this);
- tmp += other;
- return tmp;
-}
-
-inline int String16::compare(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size());
-}
-
-inline bool String16::operator<(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size()) < 0;
-}
-
-inline bool String16::operator<=(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
-}
-
-inline bool String16::operator==(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size()) == 0;
-}
-
-inline bool String16::operator!=(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size()) != 0;
-}
-
-inline bool String16::operator>=(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
-}
-
-inline bool String16::operator>(const String16& other) const {
- return strzcmp16(mString, size(), other.mString, other.size()) > 0;
-}
-
-inline bool String16::operator<(const char16_t* other) const {
- return strcmp16(mString, other) < 0;
-}
-
-inline bool String16::operator<=(const char16_t* other) const {
- return strcmp16(mString, other) <= 0;
-}
-
-inline bool String16::operator==(const char16_t* other) const {
- return strcmp16(mString, other) == 0;
-}
-
-inline bool String16::operator!=(const char16_t* other) const {
- return strcmp16(mString, other) != 0;
-}
-
-inline bool String16::operator>=(const char16_t* other) const {
- return strcmp16(mString, other) >= 0;
-}
-
-inline bool String16::operator>(const char16_t* other) const {
- return strcmp16(mString, other) > 0;
-}
-
-inline String16::operator const char16_t*() const { return mString; }
-
-} // namespace android
-
-// ---------------------------------------------------------------------------
-
-#endif // ANDROID_STRING16_H
diff --git a/nativeruntime/cpp/libutils/include/utils/String8.h b/nativeruntime/cpp/libutils/include/utils/String8.h
deleted file mode 100644
index af8d03c33..000000000
--- a/nativeruntime/cpp/libutils/include/utils/String8.h
+++ /dev/null
@@ -1,371 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/include/utils/String8.h
-
-#ifndef ANDROID_STRING8_H
-#define ANDROID_STRING8_H
-
-#include <stdarg.h>
-#include <string.h> // for strcmp
-#include <utils/Errors.h>
-#include <utils/TypeHelpers.h>
-#include <utils/Unicode.h>
-
-#include <string> // for std::string
-
-// ---------------------------------------------------------------------------
-
-namespace android {
-
-class String16;
-
-// DO NOT USE: please use std::string
-
-//! This is a string holding UTF-8 characters. Does not allow the value more
-// than 0x10FFFF, which is not valid unicode codepoint.
-class String8 {
- public:
- /* use String8(StaticLinkage) if you're statically linking against
- * libutils and declaring an empty static String8, e.g.:
- *
- * static String8 sAStaticEmptyString(String8::kEmptyString);
- * static String8 sAnotherStaticEmptyString(sAStaticEmptyString);
- */
- enum StaticLinkage { kEmptyString };
-
- String8();
- explicit String8(StaticLinkage);
- String8(const String8& o);
- explicit String8(const char* o);
- explicit String8(const char* o, size_t len);
-
- explicit String8(const String16& o);
- explicit String8(const char16_t* o);
- explicit String8(const char16_t* o, size_t len);
- explicit String8(const char32_t* o);
- explicit String8(const char32_t* o, size_t len);
- ~String8();
-
- static inline const String8 empty();
-
- static String8 format(const char* fmt, ...)
- __attribute__((format(printf, 1, 2)));
- static String8 formatV(const char* fmt, va_list args);
-
- inline const char* c_str() const;
- inline const char* string() const;
-
- private:
- static inline std::string std_string(const String8& str);
-
- public:
- inline size_t size() const;
- inline size_t bytes() const;
- inline bool isEmpty() const;
-
- size_t length() const;
-
- void clear();
-
- void setTo(const String8& other);
- status_t setTo(const char* other);
- status_t setTo(const char* other, size_t len);
- status_t setTo(const char16_t* other, size_t len);
- status_t setTo(const char32_t* other, size_t length);
-
- status_t append(const String8& other);
- status_t append(const char* other);
- status_t append(const char* other, size_t otherLen);
-
- status_t appendFormat(const char* fmt, ...)
- __attribute__((format(printf, 2, 3)));
- status_t appendFormatV(const char* fmt, va_list args);
-
- inline String8& operator=(const String8& other);
- inline String8& operator=(const char* other);
-
- inline String8& operator+=(const String8& other);
- inline String8 operator+(const String8& other) const;
-
- inline String8& operator+=(const char* other);
- inline String8 operator+(const char* other) const;
-
- inline int compare(const String8& other) const;
-
- inline bool operator<(const String8& other) const;
- inline bool operator<=(const String8& other) const;
- inline bool operator==(const String8& other) const;
- inline bool operator!=(const String8& other) const;
- inline bool operator>=(const String8& other) const;
- inline bool operator>(const String8& other) const;
-
- inline bool operator<(const char* other) const;
- inline bool operator<=(const char* other) const;
- inline bool operator==(const char* other) const;
- inline bool operator!=(const char* other) const;
- inline bool operator>=(const char* other) const;
- inline bool operator>(const char* other) const;
-
- inline operator const char*() const;
-
- char* lockBuffer(size_t size);
- void unlockBuffer();
- status_t unlockBuffer(size_t size);
-
- // return the index of the first byte of other in this at or after
- // start, or -1 if not found
- ssize_t find(const char* other, size_t start = 0) const;
-
- // return true if this string contains the specified substring
- inline bool contains(const char* other) const;
-
- // removes all occurrence of the specified substring
- // returns true if any were found and removed
- bool removeAll(const char* other);
-
- void toLower();
- void toLower(size_t start, size_t length);
- void toUpper();
- void toUpper(size_t start, size_t length);
-
- /*
- * These methods operate on the string as if it were a path name.
- */
-
- /*
- * Set the filename field to a specific value.
- *
- * Normalizes the filename, removing a trailing '/' if present.
- */
- void setPathName(const char* name);
- void setPathName(const char* name, size_t len);
-
- /*
- * Get just the filename component.
- *
- * "/tmp/foo/bar.c" --> "bar.c"
- */
- String8 getPathLeaf() const;
-
- /*
- * Remove the last (file name) component, leaving just the directory
- * name.
- *
- * "/tmp/foo/bar.c" --> "/tmp/foo"
- * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
- * "bar.c" --> ""
- */
- String8 getPathDir() const;
-
- /*
- * Retrieve the front (root dir) component. Optionally also return the
- * remaining components.
- *
- * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
- * "/tmp" --> "tmp" (remain = "")
- * "bar.c" --> "bar.c" (remain = "")
- */
- String8 walkPath(String8* outRemains = nullptr) const;
-
- /*
- * Return the filename extension. This is the last '.' and any number
- * of characters that follow it. The '.' is included in case we
- * decide to expand our definition of what constitutes an extension.
- *
- * "/tmp/foo/bar.c" --> ".c"
- * "/tmp" --> ""
- * "/tmp/foo.bar/baz" --> ""
- * "foo.jpeg" --> ".jpeg"
- * "foo." --> ""
- */
- String8 getPathExtension() const;
-
- /*
- * Return the path without the extension. Rules for what constitutes
- * an extension are described in the comment for getPathExtension().
- *
- * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
- */
- String8 getBasePath() const;
-
- /*
- * Add a component to the pathname. We guarantee that there is
- * exactly one path separator between the old path and the new.
- * If there is no existing name, we just copy the new name in.
- *
- * If leaf is a fully qualified path (i.e. starts with '/', it
- * replaces whatever was there before.
- */
- String8& appendPath(const char* leaf);
- String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
-
- /*
- * Like appendPath(), but does not affect this string. Returns a new one
- * instead.
- */
- String8 appendPathCopy(const char* leaf) const {
- String8 p(*this);
- p.appendPath(leaf);
- return p;
- }
- String8 appendPathCopy(const String8& leaf) const {
- return appendPathCopy(leaf.string());
- }
-
- /*
- * Converts all separators in this string to /, the default path separator.
- *
- * If the default OS separator is backslash, this converts all
- * backslashes to slashes, in-place. Otherwise it does nothing.
- * Returns self.
- */
- String8& convertToResPath();
-
- private:
- status_t real_append(const char* other, size_t otherLen);
- char* find_extension() const;
-
- const char* mString;
-};
-
-// String8 can be trivially moved using memcpy() because moving does not
-// require any change to the underlying SharedBuffer contents or reference
-// count.
-ANDROID_TRIVIAL_MOVE_TRAIT(String8)
-
-// ---------------------------------------------------------------------------
-// No user servicable parts below.
-
-inline int compare_type(const String8& lhs, const String8& rhs) {
- return lhs.compare(rhs);
-}
-
-inline int strictly_order_type(const String8& lhs, const String8& rhs) {
- return compare_type(lhs, rhs) < 0;
-}
-
-inline const String8 String8::empty() { return String8(); }
-
-inline const char* String8::c_str() const { return mString; }
-inline const char* String8::string() const { return mString; }
-
-inline std::string String8::std_string(const String8& str) {
- return std::string(str.string());
-}
-
-inline size_t String8::size() const { return length(); }
-
-inline bool String8::isEmpty() const { return length() == 0; }
-
-inline size_t String8::bytes() const { return length(); }
-
-inline bool String8::contains(const char* other) const {
- return find(other) >= 0;
-}
-
-inline String8& String8::operator=(const String8& other) {
- setTo(other);
- return *this;
-}
-
-inline String8& String8::operator=(const char* other) {
- setTo(other);
- return *this;
-}
-
-inline String8& String8::operator+=(const String8& other) {
- append(other);
- return *this;
-}
-
-inline String8 String8::operator+(const String8& other) const {
- String8 tmp(*this);
- tmp += other;
- return tmp;
-}
-
-inline String8& String8::operator+=(const char* other) {
- append(other);
- return *this;
-}
-
-inline String8 String8::operator+(const char* other) const {
- String8 tmp(*this);
- tmp += other;
- return tmp;
-}
-
-inline int String8::compare(const String8& other) const {
- return strcmp(mString, other.mString);
-}
-
-inline bool String8::operator<(const String8& other) const {
- return strcmp(mString, other.mString) < 0;
-}
-
-inline bool String8::operator<=(const String8& other) const {
- return strcmp(mString, other.mString) <= 0;
-}
-
-inline bool String8::operator==(const String8& other) const {
- return strcmp(mString, other.mString) == 0;
-}
-
-inline bool String8::operator!=(const String8& other) const {
- return strcmp(mString, other.mString) != 0;
-}
-
-inline bool String8::operator>=(const String8& other) const {
- return strcmp(mString, other.mString) >= 0;
-}
-
-inline bool String8::operator>(const String8& other) const {
- return strcmp(mString, other.mString) > 0;
-}
-
-inline bool String8::operator<(const char* other) const {
- return strcmp(mString, other) < 0;
-}
-
-inline bool String8::operator<=(const char* other) const {
- return strcmp(mString, other) <= 0;
-}
-
-inline bool String8::operator==(const char* other) const {
- return strcmp(mString, other) == 0;
-}
-
-inline bool String8::operator!=(const char* other) const {
- return strcmp(mString, other) != 0;
-}
-
-inline bool String8::operator>=(const char* other) const {
- return strcmp(mString, other) >= 0;
-}
-
-inline bool String8::operator>(const char* other) const {
- return strcmp(mString, other) > 0;
-}
-
-inline String8::operator const char*() const { return mString; }
-
-} // namespace android
-
-// ---------------------------------------------------------------------------
-
-#endif // ANDROID_STRING8_H
diff --git a/nativeruntime/cpp/libutils/include/utils/TypeHelpers.h b/nativeruntime/cpp/libutils/include/utils/TypeHelpers.h
deleted file mode 100644
index 9c5b14d11..000000000
--- a/nativeruntime/cpp/libutils/include/utils/TypeHelpers.h
+++ /dev/null
@@ -1,370 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/include/utils/TypeHelpers.h
-
-#ifndef ANDROID_TYPE_HELPERS_H
-#define ANDROID_TYPE_HELPERS_H
-
-#include <stdint.h>
-#include <string.h>
-#include <sys/types.h>
-
-#include <new>
-#include <type_traits>
-
-// ---------------------------------------------------------------------------
-
-namespace android {
-
-/*
- * Types traits
- */
-
-template <typename T>
-struct trait_trivial_ctor {
- enum { value = false };
-};
-template <typename T>
-struct trait_trivial_dtor {
- enum { value = false };
-};
-template <typename T>
-struct trait_trivial_copy {
- enum { value = false };
-};
-template <typename T>
-struct trait_trivial_move {
- enum { value = false };
-};
-template <typename T>
-struct trait_pointer {
- enum { value = false };
-};
-template <typename T>
-struct trait_pointer<T*> {
- enum { value = true };
-};
-
-template <typename TYPE>
-struct traits {
- enum {
- // whether this type is a pointer
- is_pointer = trait_pointer<TYPE>::value,
- // whether this type's constructor is a no-op
- has_trivial_ctor = is_pointer || trait_trivial_ctor<TYPE>::value,
- // whether this type's destructor is a no-op
- has_trivial_dtor = is_pointer || trait_trivial_dtor<TYPE>::value,
- // whether this type type can be copy-constructed with memcpy
- has_trivial_copy = is_pointer || trait_trivial_copy<TYPE>::value,
- // whether this type can be moved with memmove
- has_trivial_move = is_pointer || trait_trivial_move<TYPE>::value
- };
-};
-
-template <typename T, typename U>
-struct aggregate_traits {
- enum {
- is_pointer = false,
- has_trivial_ctor =
- traits<T>::has_trivial_ctor && traits<U>::has_trivial_ctor,
- has_trivial_dtor =
- traits<T>::has_trivial_dtor && traits<U>::has_trivial_dtor,
- has_trivial_copy =
- traits<T>::has_trivial_copy && traits<U>::has_trivial_copy,
- has_trivial_move =
- traits<T>::has_trivial_move && traits<U>::has_trivial_move
- };
-};
-
-#define ANDROID_TRIVIAL_CTOR_TRAIT(T) \
- template <> \
- struct trait_trivial_ctor<T> { \
- enum { value = true }; \
- };
-
-#define ANDROID_TRIVIAL_DTOR_TRAIT(T) \
- template <> \
- struct trait_trivial_dtor<T> { \
- enum { value = true }; \
- };
-
-#define ANDROID_TRIVIAL_COPY_TRAIT(T) \
- template <> \
- struct trait_trivial_copy<T> { \
- enum { value = true }; \
- };
-
-#define ANDROID_TRIVIAL_MOVE_TRAIT(T) \
- template <> \
- struct trait_trivial_move<T> { \
- enum { value = true }; \
- };
-
-#define ANDROID_BASIC_TYPES_TRAITS(T) \
- ANDROID_TRIVIAL_CTOR_TRAIT(T) \
- ANDROID_TRIVIAL_DTOR_TRAIT(T) \
- ANDROID_TRIVIAL_COPY_TRAIT(T) \
- ANDROID_TRIVIAL_MOVE_TRAIT(T)
-
-// ---------------------------------------------------------------------------
-
-/*
- * basic types traits
- */
-
-ANDROID_BASIC_TYPES_TRAITS(void)
-ANDROID_BASIC_TYPES_TRAITS(bool)
-ANDROID_BASIC_TYPES_TRAITS(char)
-ANDROID_BASIC_TYPES_TRAITS(unsigned char)
-ANDROID_BASIC_TYPES_TRAITS(short)
-ANDROID_BASIC_TYPES_TRAITS(unsigned short)
-ANDROID_BASIC_TYPES_TRAITS(int)
-ANDROID_BASIC_TYPES_TRAITS(unsigned int)
-ANDROID_BASIC_TYPES_TRAITS(long)
-ANDROID_BASIC_TYPES_TRAITS(unsigned long)
-ANDROID_BASIC_TYPES_TRAITS(long long)
-ANDROID_BASIC_TYPES_TRAITS(unsigned long long)
-ANDROID_BASIC_TYPES_TRAITS(float)
-ANDROID_BASIC_TYPES_TRAITS(double)
-
-// ---------------------------------------------------------------------------
-
-/*
- * compare and order types
- */
-
-template <typename TYPE>
-inline int strictly_order_type(const TYPE& lhs, const TYPE& rhs) {
- return (lhs < rhs) ? 1 : 0;
-}
-
-template <typename TYPE>
-inline int compare_type(const TYPE& lhs, const TYPE& rhs) {
- return strictly_order_type(rhs, lhs) - strictly_order_type(lhs, rhs);
-}
-
-/*
- * create, destroy, copy and move types...
- */
-
-template <typename TYPE>
-inline void construct_type(TYPE* p, size_t n) {
- if (!traits<TYPE>::has_trivial_ctor) {
- while (n > 0) {
- n--;
- new (p++) TYPE;
- }
- }
-}
-
-template <typename TYPE>
-inline void destroy_type(TYPE* p, size_t n) {
- if (!traits<TYPE>::has_trivial_dtor) {
- while (n > 0) {
- n--;
- p->~TYPE();
- p++;
- }
- }
-}
-
-template <typename TYPE>
-typename std::enable_if<traits<TYPE>::has_trivial_copy>::type inline copy_type(
- TYPE* d, const TYPE* s, size_t n) {
- memcpy(d, s, n * sizeof(TYPE));
-}
-
-template <typename TYPE>
-typename std::enable_if<!traits<TYPE>::has_trivial_copy>::type inline copy_type(
- TYPE* d, const TYPE* s, size_t n) {
- while (n > 0) {
- n--;
- new (d) TYPE(*s);
- d++, s++;
- }
-}
-
-template <typename TYPE>
-inline void splat_type(TYPE* where, const TYPE* what, size_t n) {
- if (!traits<TYPE>::has_trivial_copy) {
- while (n > 0) {
- n--;
- new (where) TYPE(*what);
- where++;
- }
- } else {
- while (n > 0) {
- n--;
- *where++ = *what;
- }
- }
-}
-
-template <typename TYPE>
-struct use_trivial_move
- : public std::integral_constant<bool, (traits<TYPE>::has_trivial_dtor &&
- traits<TYPE>::has_trivial_copy) ||
- traits<TYPE>::has_trivial_move> {
-};
-
-template <typename TYPE>
-typename std::enable_if<use_trivial_move<TYPE>::value>::
- type inline move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
- memmove(d, s, n * sizeof(TYPE));
-}
-
-template <typename TYPE>
-typename std::enable_if<!use_trivial_move<TYPE>::value>::
- type inline move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
- d += n;
- s += n;
- while (n > 0) {
- n--;
- --d, --s;
- if (!traits<TYPE>::has_trivial_copy) {
- new (d) TYPE(*s);
- } else {
- *d = *s;
- }
- if (!traits<TYPE>::has_trivial_dtor) {
- s->~TYPE();
- }
- }
-}
-
-template <typename TYPE>
-typename std::enable_if<use_trivial_move<TYPE>::value>::
- type inline move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
- memmove(d, s, n * sizeof(TYPE));
-}
-
-template <typename TYPE>
-typename std::enable_if<!use_trivial_move<TYPE>::value>::
- type inline move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
- while (n > 0) {
- n--;
- if (!traits<TYPE>::has_trivial_copy) {
- new (d) TYPE(*s);
- } else {
- *d = *s;
- }
- if (!traits<TYPE>::has_trivial_dtor) {
- s->~TYPE();
- }
- d++, s++;
- }
-}
-
-// ---------------------------------------------------------------------------
-
-/*
- * a key/value pair
- */
-
-template <typename KEY, typename VALUE>
-struct key_value_pair_t {
- typedef KEY key_t;
- typedef VALUE value_t;
-
- KEY key;
- VALUE value;
- key_value_pair_t() {}
- key_value_pair_t(const key_value_pair_t& o) : key(o.key), value(o.value) {}
- key_value_pair_t& operator=(const key_value_pair_t& o) {
- key = o.key;
- value = o.value;
- return *this;
- }
- key_value_pair_t(const KEY& k, const VALUE& v) : key(k), value(v) {}
- explicit key_value_pair_t(const KEY& k) : key(k) {}
- inline bool operator<(const key_value_pair_t& o) const {
- return strictly_order_type(key, o.key);
- }
- inline const KEY& getKey() const { return key; }
- inline const VALUE& getValue() const { return value; }
-};
-
-template <typename K, typename V>
-struct trait_trivial_ctor<key_value_pair_t<K, V> > {
- enum { value = aggregate_traits<K, V>::has_trivial_ctor };
-};
-template <typename K, typename V>
-struct trait_trivial_dtor<key_value_pair_t<K, V> > {
- enum { value = aggregate_traits<K, V>::has_trivial_dtor };
-};
-template <typename K, typename V>
-struct trait_trivial_copy<key_value_pair_t<K, V> > {
- enum { value = aggregate_traits<K, V>::has_trivial_copy };
-};
-template <typename K, typename V>
-struct trait_trivial_move<key_value_pair_t<K, V> > {
- enum { value = aggregate_traits<K, V>::has_trivial_move };
-};
-
-// ---------------------------------------------------------------------------
-
-/*
- * Hash codes.
- */
-typedef uint32_t hash_t;
-
-template <typename TKey>
-hash_t hash_type(const TKey& key);
-
-/* Built-in hash code specializations */
-#define ANDROID_INT32_HASH(T) \
- template <> \
- inline hash_t hash_type(const T& value) { \
- return hash_t(value); \
- }
-#define ANDROID_INT64_HASH(T) \
- template <> \
- inline hash_t hash_type(const T& value) { \
- return hash_t((value >> 32) ^ value); \
- }
-#define ANDROID_REINTERPRET_HASH(T, R) \
- template <> \
- inline hash_t hash_type(const T& value) { \
- R newValue; \
- static_assert(sizeof(newValue) == sizeof(value), "size mismatch"); \
- memcpy(&newValue, &value, sizeof(newValue)); \
- return hash_type(newValue); \
- }
-
-ANDROID_INT32_HASH(bool)
-ANDROID_INT32_HASH(int8_t)
-ANDROID_INT32_HASH(uint8_t)
-ANDROID_INT32_HASH(int16_t)
-ANDROID_INT32_HASH(uint16_t)
-ANDROID_INT32_HASH(int32_t)
-ANDROID_INT32_HASH(uint32_t)
-ANDROID_INT64_HASH(int64_t)
-ANDROID_INT64_HASH(uint64_t)
-ANDROID_REINTERPRET_HASH(float, uint32_t)
-ANDROID_REINTERPRET_HASH(double, uint64_t)
-
-template <typename T>
-inline hash_t hash_type(T* const& value) {
- return hash_type(uintptr_t(value));
-}
-
-} // namespace android
-
-// ---------------------------------------------------------------------------
-
-#endif // ANDROID_TYPE_HELPERS_H
diff --git a/nativeruntime/cpp/libutils/include/utils/Unicode.h b/nativeruntime/cpp/libutils/include/utils/Unicode.h
deleted file mode 100644
index dfd73f3e0..000000000
--- a/nativeruntime/cpp/libutils/include/utils/Unicode.h
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Copyright (C) 2005 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Derived from
-// https://cs.android.com/android/platform/superproject/+/android-11.0.0_r1:system/core/libutils/include/utils/Unicode.h
-
-#ifndef ANDROID_UNICODE_H
-#define ANDROID_UNICODE_H
-
-#include <stdint.h>
-#include <sys/types.h>
-
-extern "C" {
-
-// Standard string functions on char16_t strings.
-int strcmp16(const char16_t *, const char16_t *);
-int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
-size_t strlen16(const char16_t *);
-size_t strnlen16(const char16_t *, size_t);
-char16_t *strcpy16(char16_t *, const char16_t *);
-char16_t *strstr16(const char16_t *, const char16_t *);
-
-// Version of comparison that supports embedded NULs.
-// This is different than strncmp() because we don't stop
-// at a nul character and consider the strings to be different
-// if the lengths are different (thus we need to supply the
-// lengths of both strings). This can also be used when
-// your string is not nul-terminated as it will have the
-// equivalent result as strcmp16 (unlike strncmp16).
-int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
-
-// Standard string functions on char32_t strings.
-size_t strlen32(const char32_t *);
-size_t strnlen32(const char32_t *, size_t);
-
-/**
- * Measure the length of a UTF-32 string in UTF-8. If the string is invalid
- * such as containing a surrogate character, -1 will be returned.
- */
-ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len);
-
-/**
- * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not
- * large enough to store the string, the part of the "src" string is stored
- * into "dst" as much as possible. See the examples for more detail.
- * Returns the size actually used for storing the string.
- * dst" is not nul-terminated when dst_len is fully used (like strncpy).
- *
- * \code
- * Example 1
- * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
- * "src_len" == 2
- * "dst_len" >= 7
- * ->
- * Returned value == 6
- * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0
- * (note that "dst" is nul-terminated)
- *
- * Example 2
- * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
- * "src_len" == 2
- * "dst_len" == 5
- * ->
- * Returned value == 3
- * "dst" becomes \xE3\x81\x82\0
- * (note that "dst" is nul-terminated, but \u3044 is not stored in "dst"
- * since "dst" does not have enough size to store the character)
- *
- * Example 3
- * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
- * "src_len" == 2
- * "dst_len" == 6
- * ->
- * Returned value == 6
- * "dst" becomes \xE3\x81\x82\xE3\x81\x84
- * (note that "dst" is NOT nul-terminated, like strncpy)
- * \endcode
- */
-void utf32_to_utf8(const char32_t *src, size_t src_len, char *dst,
- size_t dst_len);
-
-/**
- * Returns the unicode value at "index".
- * Returns -1 when the index is invalid (equals to or more than "src_len").
- * If returned value is positive, it is able to be converted to char32_t, which
- * is unsigned. Then, if "next_index" is not NULL, the next index to be used is
- * stored in "next_index". "next_index" can be NULL.
- */
-int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index,
- size_t *next_index);
-
-/**
- * Returns the UTF-8 length of UTF-16 string "src".
- */
-ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len);
-
-/**
- * Converts a UTF-16 string to UTF-8. The destination buffer must be large
- * enough to fit the UTF-16 as measured by utf16_to_utf8_length with an added
- * NUL terminator.
- */
-void utf16_to_utf8(const char16_t *src, size_t src_len, char *dst,
- size_t dst_len);
-
-/**
- * Returns the length of "src" when "src" is valid UTF-8 string.
- * Returns 0 if src is NULL or 0-length string. Returns -1 when the source
- * is an invalid string.
- *
- * This function should be used to determine whether "src" is valid UTF-8
- * characters with valid unicode codepoints. "src" must be nul-terminated.
- *
- * If you are going to use other utf8_to_... functions defined in this header
- * with string which may not be valid UTF-8 with valid codepoint (form 0 to
- * 0x10FFFF), you should use this function before calling others, since the
- * other functions do not check whether the string is valid UTF-8 or not.
- *
- * If you do not care whether "src" is valid UTF-8 or not, you should use
- * strlen() as usual, which should be much faster.
- */
-ssize_t utf8_length(const char *src);
-
-/**
- * Returns the UTF-16 length of UTF-8 string "src". Returns -1 in case
- * it's invalid utf8. No buffer over-read occurs because of bound checks. Using
- * overreadIsFatal you can ask to log a message and fail in case the invalid
- * utf8 could have caused an override if no bound checks were used (otherwise -1
- * is returned).
- */
-ssize_t utf8_to_utf16_length(const uint8_t *u8str, size_t u8len,
- bool overreadIsFatal = false);
-
-/**
- * Convert UTF-8 to UTF-16 including surrogate pairs.
- * Returns a pointer to the end of the string (where a NUL terminator might go
- * if you wanted to add one). At most dstLen characters are written; it won't
- * emit half a surrogate pair. If dstLen == 0 nothing is written and dst is
- * returned. If dstLen > SSIZE_MAX it aborts (this being probably a negative
- * number returned as an error and casted to unsigned).
- */
-char16_t *utf8_to_utf16_no_null_terminator(const uint8_t *src, size_t srcLen,
- char16_t *dst, size_t dstLen);
-
-/**
- * Convert UTF-8 to UTF-16 including surrogate pairs. At most dstLen - 1
- * characters are written; it won't emit half a surrogate pair; and a NUL
- * terminator is appended after. dstLen - 1 can be measured beforehand using
- * utf8_to_utf16_length. Aborts if dstLen == 0 (at least one character is needed
- * for the NUL terminator) or dstLen > SSIZE_MAX (the latter case being likely a
- * negative number returned as an error and casted to unsigned) . Returns a
- * pointer to the NUL terminator.
- */
-char16_t *utf8_to_utf16(const uint8_t *u8str, size_t u8len, char16_t *u16str,
- size_t u16len);
-}
-
-#endif
diff --git a/nativeruntime/external/icu b/nativeruntime/external/icu
deleted file mode 160000
-Subproject 0e7b4428866f3133b4abba2d932ee3faa708db1
diff --git a/nativeruntime/external/sqlite b/nativeruntime/external/sqlite
deleted file mode 160000
-Subproject 41e1a3604e32f92120a0d4ce2d62c4a5c8f8673