From 947dae4900d97500558929e3381c3d20e99a6144 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 25 Sep 2019 23:55:39 -0400 Subject: [PATCH] Rename classes and small adjustments --- native/jni/init/getinfo.cpp | 2 +- native/jni/magiskboot/bootimg.cpp | 2 +- native/jni/magiskboot/compress.cpp | 4 +- native/jni/magiskboot/compress.h | 2 +- native/jni/magiskboot/ramdisk.cpp | 8 +-- native/jni/magiskhide/hide_policy.cpp | 2 +- native/jni/magiskhide/hide_utils.cpp | 4 +- native/jni/magiskhide/proc_monitor.cpp | 4 +- native/jni/su/su_daemon.cpp | 4 +- .../{BlockingQueue.h => blocking_queue.h} | 58 +++++++++---------- native/jni/utils/include/cpio.h | 2 +- .../utils/include/{OutStream.h => stream.h} | 4 +- native/jni/utils/misc.h | 14 ++--- 13 files changed, 53 insertions(+), 57 deletions(-) rename native/jni/utils/include/{BlockingQueue.h => blocking_queue.h} (55%) rename native/jni/utils/include/{OutStream.h => stream.h} (93%) diff --git a/native/jni/init/getinfo.cpp b/native/jni/init/getinfo.cpp index 285d0896a..904aed527 100644 --- a/native/jni/init/getinfo.cpp +++ b/native/jni/init/getinfo.cpp @@ -67,7 +67,7 @@ static bool check_key_combo() { if (events.empty()) return false; - RunFinally fin([&]() -> void { + run_finally fin([&]() -> void { for (const int &fd : events) close(fd); }); diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 117413a21..4af6c7051 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -22,7 +22,7 @@ uint32_t dyn_img_hdr::j32 = 0; uint64_t dyn_img_hdr::j64 = 0; static int64_t one_step(unique_ptr &&ptr, int fd, const void *in, size_t size) { - ptr->set_out(make_unique(fd)); + ptr->setOut(make_unique(fd)); if (!ptr->write(in, size)) return -1; return ptr->finalize(); diff --git a/native/jni/magiskboot/compress.cpp b/native/jni/magiskboot/compress.cpp index bd7e41d93..4f37ac591 100644 --- a/native/jni/magiskboot/compress.cpp +++ b/native/jni/magiskboot/compress.cpp @@ -62,7 +62,7 @@ void decompress(char *infile, const char *outfile) { out_fd = strcmp(outfile, "-") == 0 ? STDOUT_FILENO : xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); - cmp->set_out(make_unique(out_fd)); + cmp->setOut(make_unique(out_fd)); if (ext) *ext = '.'; } if (!cmp->write(buf, len)) @@ -108,7 +108,7 @@ void compress(const char *method, const char *infile, const char *outfile) { STDOUT_FILENO : xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); } - cmp->set_out(make_unique(out_fd)); + cmp->setOut(make_unique(out_fd)); read_file(in_file, [&](void *buf, size_t len) -> void { if (!cmp->write(buf, len)) diff --git a/native/jni/magiskboot/compress.h b/native/jni/magiskboot/compress.h index 2b39b315e..81b9bead8 100644 --- a/native/jni/magiskboot/compress.h +++ b/native/jni/magiskboot/compress.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include "format.h" diff --git a/native/jni/magiskboot/ramdisk.cpp b/native/jni/magiskboot/ramdisk.cpp index c42c83671..e0898118e 100644 --- a/native/jni/magiskboot/ramdisk.cpp +++ b/native/jni/magiskboot/ramdisk.cpp @@ -246,13 +246,13 @@ void magisk_cpio::compress() { fprintf(stderr, "Compressing cpio -> [%s]\n", RAMDISK_XZ); auto init = entries.extract("init"); XZEncoder encoder; - encoder.set_out(make_unique()); + encoder.setOut(make_unique()); output(encoder); encoder.finalize(); entries.clear(); entries.insert(std::move(init)); auto xz = new cpio_entry(RAMDISK_XZ, S_IFREG); - static_cast(encoder.get_out())->release(xz->data, xz->filesize); + static_cast(encoder.getOut())->release(xz->data, xz->filesize); insert(xz); } @@ -262,13 +262,13 @@ void magisk_cpio::decompress() { return; fprintf(stderr, "Decompressing cpio [%s]\n", RAMDISK_XZ); LZMADecoder decoder; - decoder.set_out(make_unique()); + decoder.setOut(make_unique()); decoder.write(it->second->data, it->second->filesize); decoder.finalize(); entries.erase(it); char *buf; size_t sz; - static_cast(decoder.get_out())->getbuf(buf, sz); + static_cast(decoder.getOut())->getbuf(buf, sz); load_cpio(buf, sz); } diff --git a/native/jni/magiskhide/hide_policy.cpp b/native/jni/magiskhide/hide_policy.cpp index 0ef62f76f..0a20cd75d 100644 --- a/native/jni/magiskhide/hide_policy.cpp +++ b/native/jni/magiskhide/hide_policy.cpp @@ -36,7 +36,7 @@ static inline void lazy_unmount(const char* mountpoint) { } void hide_daemon(int pid) { - RunFinally fin([=]() -> void { + run_finally fin([=]() -> void { // Send resume signal kill(pid, SIGCONT); _exit(0); diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 66652a764..e12b75780 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -98,7 +98,7 @@ static int add_list(const char *pkg, const char *proc = "") { // Critical region { - MutexGuard lock(monitor_lock); + mutex_guard lock(monitor_lock); hide_set.emplace(pkg, proc); } @@ -119,7 +119,7 @@ int add_list(int client) { static int rm_list(const char *pkg, const char *proc = "") { { // Critical region - MutexGuard lock(monitor_lock); + mutex_guard lock(monitor_lock); bool remove = false; auto next = hide_set.begin(); decltype(next) cur; diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index a55c29090..28da53872 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -76,7 +76,7 @@ static inline long xptrace(int request, pid_t pid, void *addr = nullptr, intptr_ } void update_uid_map() { - MutexGuard lock(monitor_lock); + mutex_guard lock(monitor_lock); uid_proc_map.clear(); string data_path(APP_DATA_DIR); data_path += "/0/"; @@ -336,7 +336,7 @@ void proc_monitor() { continue; } bool detach = false; - RunFinally detach_task([&] { + run_finally f([&] { if (detach) // Non of our business now detach_pid(pid); diff --git a/native/jni/su/su_daemon.cpp b/native/jni/su/su_daemon.cpp index 47fb48de2..1ce07393c 100644 --- a/native/jni/su/su_daemon.cpp +++ b/native/jni/su/su_daemon.cpp @@ -89,7 +89,7 @@ static shared_ptr get_su_info(unsigned uid) { shared_ptr info; { - MutexGuard lock(cache_lock); + mutex_guard lock(cache_lock); if (!cached || cached->uid != uid || !cached->is_fresh()) cached = make_shared(uid); cached->refresh(); @@ -97,7 +97,7 @@ static shared_ptr get_su_info(unsigned uid) { } info->lock(); - RunFinally unlock([&] { + run_finally unlock([&] { info->unlock(); }); diff --git a/native/jni/utils/include/BlockingQueue.h b/native/jni/utils/include/blocking_queue.h similarity index 55% rename from native/jni/utils/include/BlockingQueue.h rename to native/jni/utils/include/blocking_queue.h index b12a67073..f907da1f3 100644 --- a/native/jni/utils/include/BlockingQueue.h +++ b/native/jni/utils/include/blocking_queue.h @@ -2,41 +2,41 @@ #include #include +#include template -class BlockingQueue { +class blocking_queue { std::deque deque{}; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; bool cancelled = false; public: - ~BlockingQueue(); + ~blocking_queue(); T take(); - T &front(); - T &back(); - void put(const T&); - void put(T&&); + T &front() const; + T &back() const; + void push(const T&); + void push(T&&); template< class... Args > - void emplace_back(Args&&... args); + void emplace(Args &&... args); void clear(); void cancel(); }; #define run_and_notify(block) \ -pthread_mutex_lock(&this->lock); \ +mutex_guard g(this->lock); \ block \ -pthread_cond_signal(&this->cond); \ -pthread_mutex_unlock(&this->lock); +pthread_cond_signal(&this->cond); template -BlockingQueue::~BlockingQueue() { +blocking_queue::~blocking_queue() { pthread_mutex_destroy(&lock); pthread_cond_destroy(&cond); } template -T BlockingQueue::take() { - pthread_mutex_lock(&lock); +T blocking_queue::take() { + mutex_guard g(lock); cancelled = false; while (deque.empty() && !cancelled) pthread_cond_wait(&cond, &lock); @@ -44,55 +44,51 @@ T BlockingQueue::take() { pthread_exit(nullptr); T ret(std::move(deque.front())); deque.pop_front(); - pthread_mutex_unlock(&lock); return ret; } template -void BlockingQueue::put(const T &s) { +void blocking_queue::push(const T &s) { run_and_notify({ deque.push_back(s); }) } template -void BlockingQueue::put(T &&s) { +void blocking_queue::push(T &&s) { run_and_notify({ deque.push_back(std::move(s)); }) } template -T &BlockingQueue::front() { - pthread_mutex_lock(&lock); - auto &ret = deque.front(); - pthread_mutex_unlock(&lock); - return ret; +T &blocking_queue::front() const { + mutex_guard g(lock); + return deque.front(); } template -T &BlockingQueue::back() { - pthread_mutex_lock(&lock); - auto &ret = deque.back(); - pthread_mutex_unlock(&lock); - return ret; +T &blocking_queue::back() const { + mutex_guard g(lock); + return deque.back(); } template template -void BlockingQueue::emplace_back(Args &&... args) { +void blocking_queue::emplace(Args &&... args) { run_and_notify({ deque.emplace_back(std::forward(args)...); }) } template -void BlockingQueue::clear() { - pthread_mutex_lock(&lock); +void blocking_queue::clear() { + mutex_guard g(lock); std::deque t; deque.swap(t); - pthread_mutex_unlock(&lock); } template -void BlockingQueue::cancel() { +void blocking_queue::cancel() { run_and_notify({ cancelled = true; std::deque t; deque.swap(t); }) } + +#undef run_and_notify diff --git a/native/jni/utils/include/cpio.h b/native/jni/utils/include/cpio.h index 7ffa169d5..4f6f09917 100644 --- a/native/jni/utils/include/cpio.h +++ b/native/jni/utils/include/cpio.h @@ -6,7 +6,7 @@ #include #include -#include +#include struct cpio_newc_header; diff --git a/native/jni/utils/include/OutStream.h b/native/jni/utils/include/stream.h similarity index 93% rename from native/jni/utils/include/OutStream.h rename to native/jni/utils/include/stream.h index bcb3925b9..5216e75e2 100644 --- a/native/jni/utils/include/OutStream.h +++ b/native/jni/utils/include/stream.h @@ -19,9 +19,9 @@ public: FilterOutStream(strm_ptr &&ptr) : out(std::move(ptr)) {} - void set_out(strm_ptr &&ptr) { out = std::move(ptr); } + void setOut(strm_ptr &&ptr) { out = std::move(ptr); } - OutStream *get_out() { return out.get(); } + OutStream *getOut() { return out.get(); } bool write(const void *buf, size_t len) override { return out ? out->write(buf, len) : false; diff --git a/native/jni/utils/misc.h b/native/jni/utils/misc.h index ad299a7b0..ad6616431 100644 --- a/native/jni/utils/misc.h +++ b/native/jni/utils/misc.h @@ -27,17 +27,17 @@ void gen_rand_str(char *buf, int len, bool varlen = true); #define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos) #define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0) -class MutexGuard { +class mutex_guard { public: - explicit MutexGuard(pthread_mutex_t &m): mutex(&m) { + explicit mutex_guard(pthread_mutex_t &m): mutex(&m) { pthread_mutex_lock(mutex); } - explicit MutexGuard(pthread_mutex_t *m): mutex(m) { + explicit mutex_guard(pthread_mutex_t *m): mutex(m) { pthread_mutex_lock(mutex); } - ~MutexGuard() { + ~mutex_guard() { pthread_mutex_unlock(mutex); } @@ -45,13 +45,13 @@ private: pthread_mutex_t *mutex; }; -class RunFinally { +class run_finally { public: - explicit RunFinally(std::function &&fn): fn(std::move(fn)) {} + explicit run_finally(std::function &&fn): fn(std::move(fn)) {} void disable() { fn = nullptr; } - ~RunFinally() { if (fn) fn(); } + ~run_finally() { if (fn) fn(); } private: std::function fn;