Use `absl::flat_hash_map` instead (#2001)

This commit is contained in:
LoveSy 2022-06-29 02:21:39 +08:00 committed by GitHub
parent b92bf13ef5
commit 307b88f1d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 13 deletions

View File

@ -62,6 +62,7 @@ jobs:
ccache -o max_size=1G ccache -o max_size=1G
ccache -o hash_dir=false ccache -o hash_dir=false
ccache -o compiler_check='%compiler% -dumpmachine; %compiler% -dumpversion' ccache -o compiler_check='%compiler% -dumpmachine; %compiler% -dumpversion'
ccache -o hard_link=true
ccache -zp ccache -zp
- name: Build with Gradle - name: Build with Gradle
run: | run: |

View File

@ -20,7 +20,8 @@
#include "hook_bridge.h" #include "hook_bridge.h"
#include "native_util.h" #include "native_util.h"
#include "lsplant.hpp" #include "lsplant.hpp"
#include "unordered_map" #include <absl/container/flat_hash_map.h>
#include <memory>
#include <shared_mutex> #include <shared_mutex>
#include <set> #include <set>
@ -34,8 +35,7 @@ struct HookItem {
}; };
std::shared_mutex hooked_lock; std::shared_mutex hooked_lock;
// Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. absl::flat_hash_map<jmethodID, std::unique_ptr<HookItem>> hooked_methods;
std::unordered_map<jmethodID, HookItem> hooked_methods;
jmethodID invoke = nullptr; jmethodID invoke = nullptr;
} }
@ -64,14 +64,17 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, hookMethod, jobject hookMethod,
{ {
std::shared_lock lk(hooked_lock); std::shared_lock lk(hooked_lock);
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
hook_item = &found->second; hook_item = found->second.get();
} }
} }
if (!hook_item) { if (!hook_item) {
std::unique_lock lk(hooked_lock); std::unique_lock lk(hooked_lock);
hook_item = &hooked_methods[target]; if (auto &ptr = hooked_methods[target]; !ptr) {
ptr = std::make_unique<HookItem>();
hook_item = ptr.get();
newHook = true; newHook = true;
} }
}
if (newHook) { if (newHook) {
auto init = env->GetMethodID(hooker, "<init>", "(Ljava/lang/reflect/Executable;)V"); auto init = env->GetMethodID(hooker, "<init>", "(Ljava/lang/reflect/Executable;)V");
auto callback_method = env->ToReflectedMethod(hooker, env->GetMethodID(hooker, "callback", auto callback_method = env->ToReflectedMethod(hooker, env->GetMethodID(hooker, "callback",
@ -92,7 +95,7 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, unhookMethod, jobject hookMethod, jo
{ {
std::shared_lock lk(hooked_lock); std::shared_lock lk(hooked_lock);
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
hook_item = &found->second; hook_item = found->second.get();
} }
} }
if (!hook_item) return JNI_FALSE; if (!hook_item) return JNI_FALSE;
@ -118,7 +121,7 @@ LSP_DEF_NATIVE_METHOD(jobject, HookBridge, invokeOriginalMethod, jobject hookMet
{ {
std::shared_lock lk(hooked_lock); std::shared_lock lk(hooked_lock);
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
hook_item = &found->second; hook_item = found->second.get();
} }
} }
jobject to_call = hookMethod; jobject to_call = hookMethod;
@ -142,7 +145,7 @@ LSP_DEF_NATIVE_METHOD(jobjectArray, HookBridge, callbackSnapshot, jobject method
{ {
std::shared_lock lk(hooked_lock); std::shared_lock lk(hooked_lock);
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
hook_item = &found->second; hook_item = found->second.get();
} }
} }
if (!hook_item) return nullptr; if (!hook_item) return nullptr;

View File

@ -19,11 +19,12 @@
#pragma once #pragma once
#include <string> #include <string>
#include <absl/container/flat_hash_map.h>
#include "utils/jni_helper.hpp" #include "utils/jni_helper.hpp"
class WA: public dex::Writer::Allocator { class WA: public dex::Writer::Allocator {
// addr: {size, fd} // addr: {size, fd}
std::unordered_map<void*, std::pair<size_t, int>> allocated_; absl::flat_hash_map<void*, std::pair<size_t, int>> allocated_;
public: public:
inline void* Allocate(size_t size) override { inline void* Allocate(size_t size) override {
auto fd = ASharedMemory_create("", size); auto fd = ASharedMemory_create("", size);

View File

@ -27,7 +27,8 @@ set(LIBCXX_SOURCES
filesystem/operations.cpp filesystem/operations.cpp
functional.cpp functional.cpp
future.cpp future.cpp
hash.cpp # use absl instead
# hash.cpp
ios.cpp ios.cpp
iostream.cpp iostream.cpp
locale.cpp locale.cpp
@ -44,7 +45,7 @@ set(LIBCXX_SOURCES
strstream.cpp strstream.cpp
system_error.cpp system_error.cpp
thread.cpp thread.cpp
typeinfo.cpp # typeinfo.cpp
utility.cpp utility.cpp
valarray.cpp valarray.cpp
variant.cpp variant.cpp
@ -101,6 +102,7 @@ target_include_directories(cxx PRIVATE ${LIBCXX_INCLUDES} ${LIBCXXABI_INCLUDES})
link_libraries(cxx) link_libraries(cxx)
OPTION(LSPLANT_BUILD_SHARED OFF)
add_subdirectory(lsplant/lsplant/src/main/jni) add_subdirectory(lsplant/lsplant/src/main/jni)
add_subdirectory(dobby) add_subdirectory(dobby)
add_subdirectory(fmt) add_subdirectory(fmt)

2
external/lsplant vendored

@ -1 +1 @@
Subproject commit 5867b673803e255c4f0c75b9055fe7da3209e2ae Subproject commit ca19d61942994a72b8bf875ea7ed97c0e0513b74