OSDN Git Service

Move scopeguard into android::base
authorTom Cherry <tomcherry@google.com>
Wed, 5 Apr 2017 23:20:29 +0000 (16:20 -0700)
committerTom Cherry <tomcherry@google.com>
Thu, 6 Apr 2017 19:50:18 +0000 (12:50 -0700)
Test: boot bullhead, bionic unit tests
Change-Id: I223249684867655ecb53713b10da41d3014f96ae

libc/private/ScopeGuard.h [deleted file]
linker/linker.cpp
linker/linker_config.cpp
linker/tests/linker_config_test.cpp
tests/bug_26110743_test.cpp
tests/dlfcn_test.cpp
tests/math_test.cpp
tests/pthread_test.cpp
tests/unistd_test.cpp
tests/utils.h

diff --git a/libc/private/ScopeGuard.h b/libc/private/ScopeGuard.h
deleted file mode 100644 (file)
index d5a9235..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2014 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.
- */
-
-#ifndef _SCOPE_GUARD_H
-#define _SCOPE_GUARD_H
-
-#include "private/bionic_macros.h"
-
-// TODO: include explicit std::move when it becomes available
-template<typename F>
-class ScopeGuard {
- public:
-  ScopeGuard(F f) : f_(f), active_(true) {}
-
-  ScopeGuard(ScopeGuard&& that) : f_(that.f_), active_(that.active_) {
-    that.active_ = false;
-  }
-
-  ~ScopeGuard() {
-    if (active_) {
-      f_();
-    }
-  }
-
-  void disable() {
-    active_ = false;
-  }
- private:
-  F f_;
-  bool active_;
-
-  DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeGuard);
-};
-
-template<typename T>
-ScopeGuard<T> make_scope_guard(T f) {
-  return ScopeGuard<T>(f);
-}
-
-#endif  // _SCOPE_GUARD_H
index 2777d73..d31c652 100644 (file)
@@ -43,8 +43,9 @@
 #include <unordered_map>
 #include <vector>
 
+#include <android-base/scopeguard.h>
+
 // Private C library headers.
-#include "private/ScopeGuard.h"
 
 #include "linker.h"
 #include "linker_block_allocator.h"
@@ -1536,13 +1537,13 @@ bool find_libraries(android_namespace_t* ns,
   // list of libraries to link - see step 2.
   size_t soinfos_count = 0;
 
-  auto scope_guard = make_scope_guard([&]() {
+  auto scope_guard = android::base::make_scope_guard([&]() {
     for (LoadTask* t : load_tasks) {
       LoadTask::deleter(t);
     }
   });
 
-  auto failure_guard = make_scope_guard([&]() {
+  auto failure_guard = android::base::make_scope_guard([&]() {
     // Housekeeping
     soinfo_unload(soinfos, soinfos_count);
   });
@@ -1661,7 +1662,7 @@ bool find_libraries(android_namespace_t* ns,
       }
     });
 
-    failure_guard.disable();
+    failure_guard.Disable();
   }
 
   return linked;
@@ -1904,9 +1905,8 @@ void* do_dlopen(const char* name, int flags,
          ns == nullptr ? "(null)" : ns->get_name(),
          ns);
 
-  auto failure_guard = make_scope_guard([&]() {
-    LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
-  });
+  auto failure_guard = android::base::make_scope_guard(
+      [&]() { LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer()); });
 
   if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
     DL_ERR("invalid flags to dlopen: %x", flags);
@@ -1966,7 +1966,7 @@ void* do_dlopen(const char* name, int flags,
            "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
            si->get_realpath(), si->get_soname(), handle);
     si->call_constructors();
-    failure_guard.disable();
+    failure_guard.Disable();
     LD_LOG(kLogDlopen,
            "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
            si->get_realpath(), si->get_soname(), handle);
@@ -2044,9 +2044,8 @@ bool do_dlsym(void* handle,
          ns == nullptr ? "(null)" : ns->get_name(),
          ns);
 
-  auto failure_guard = make_scope_guard([&]() {
-    LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
-  });
+  auto failure_guard = android::base::make_scope_guard(
+      [&]() { LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer()); });
 
   if (sym_name == nullptr) {
     DL_ERR("dlsym failed: symbol name is null");
@@ -2077,7 +2076,7 @@ bool do_dlsym(void* handle,
 
     if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
       *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
-      failure_guard.disable();
+      failure_guard.Disable();
       LD_LOG(kLogDlsym,
              "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
              sym_name, sym_ver, found->get_soname(), *symbol);
index 33616f7..a12cfbe 100644 (file)
 #include "linker_utils.h"
 
 #include <android-base/file.h>
+#include <android-base/scopeguard.h>
 #include <android-base/strings.h>
 
-#include <private/ScopeGuard.h>
-
 #include <stdlib.h>
 
 #include <string>
@@ -379,9 +378,7 @@ bool Config::read_binary_config(const char* ld_config_file_path,
 
   Properties properties(std::move(property_map));
 
-  auto failure_guard = make_scope_guard([] {
-    g_config.clear();
-  });
+  auto failure_guard = android::base::make_scope_guard([] { g_config.clear(); });
 
   std::unordered_map<std::string, NamespaceConfig*> namespace_configs;
 
@@ -469,7 +466,7 @@ bool Config::read_binary_config(const char* ld_config_file_path,
     ns_config->set_permitted_paths(properties.get_paths(property_name_prefix + ".permitted.paths"));
   }
 
-  failure_guard.disable();
+  failure_guard.Disable();
   *config = &g_config;
   return true;
 }
index 64ab00f..418cbda 100644 (file)
 
 #include <unistd.h>
 
+#include <android-base/scopeguard.h>
 #include <android-base/stringprintf.h>
 #include <android-base/file.h>
 #include <android-base/test_utils.h>
 
-#include "private/ScopeGuard.h"
-
 
 static const char* config_str =
   "# comment \n"
@@ -116,9 +115,8 @@ static void run_linker_config_smoke_test(bool is_asan) {
   std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
   std::string version_file = std::string(tmp_dir.path) + "/.version";
 
-  auto file_guard = make_scope_guard([&version_file] {
-    unlink(version_file.c_str());
-  });
+  auto file_guard =
+      android::base::make_scope_guard([&version_file] { unlink(version_file.c_str()); });
 
   ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
 
index c49a9dc..ef474a0 100644 (file)
@@ -22,7 +22,7 @@
 #include <sys/stat.h>
 #include <sys/prctl.h>
 
-#include "private/ScopeGuard.h"
+#include <android-base/scopeguard.h>
 
 extern "C" pid_t gettid();
 
@@ -56,7 +56,7 @@ TEST(bug_26110743, ProcSelfReadlink) {
 TEST(bug_26110743, ProcSelfReadlink_NotDumpable) {
   int dumpable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
   prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
-  auto guard = make_scope_guard([&]() {
+  auto guard = android::base::make_scope_guard([&]() {
     // restore dumpable
     prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
   });
@@ -100,7 +100,7 @@ TEST(bug_26110743, ProcTaskFdReadlink) {
 TEST(bug_26110743, ProcTaskFdReadlink_NotDumpable) {
   int dumpable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
   prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
-  auto guard = make_scope_guard([&]() {
+  auto guard = android::base::make_scope_guard([&]() {
     // restore dumpable
     prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
   });
index ad8444e..4ff324e 100644 (file)
 #include <stdint.h>
 #include <string.h>
 
-#include "private/ScopeGuard.h"
-
 #include <string>
 #include <thread>
 
+#include <android-base/scopeguard.h>
+
 #include "gtest_globals.h"
 #include "dlfcn_symlink_support.h"
 #include "utils.h"
@@ -330,9 +330,7 @@ TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
   // in both dt_needed libraries, the correct relocation should
   // use the function defined in libtest_relo_check_dt_needed_order_1.so
   void* handle = nullptr;
-  auto guard = make_scope_guard([&]() {
-    dlclose(handle);
-  });
+  auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
 
   handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
@@ -986,9 +984,7 @@ TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
   dlerror(); // Clear any pending errors.
   void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
-  auto guard = make_scope_guard([&]() {
-    dlclose(handle);
-  });
+  auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
   void* sym = dlsym(handle, "getRandomNumber");
   ASSERT_TRUE(sym != nullptr) << dlerror();
   int (*fn)(void);
@@ -1009,9 +1005,7 @@ TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
 TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
   void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
-  auto guard = make_scope_guard([&]() {
-    dlclose(handle);
-  });
+  auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
   void* sym = dlsym(handle, "getRandomNumber");
   ASSERT_TRUE(sym != nullptr) << dlerror();
   int (*fn)(void);
index 6c7dffb..b960944 100644 (file)
@@ -57,7 +57,7 @@ template<typename T> inline int test_capture_isinf(const T in) {
 #include <limits.h>
 #include <stdint.h>
 
-#include <private/ScopeGuard.h>
+#include <android-base/scopeguard.h>
 
 float float_subnormal() {
   union {
@@ -775,9 +775,7 @@ TEST(math, erfcl) {
 }
 
 TEST(math, lrint) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
 
   fesetround(FE_UPWARD); // lrint/lrintf/lrintl obey the rounding mode.
   ASSERT_EQ(1235, lrint(1234.01));
@@ -799,9 +797,7 @@ TEST(math, lrint) {
 }
 
 TEST(math, rint) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
 
   fesetround(FE_UPWARD); // rint/rintf/rintl obey the rounding mode.
   feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
@@ -829,9 +825,7 @@ TEST(math, rint) {
 }
 
 TEST(math, nearbyint) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_UPWARD); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
   feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
   ASSERT_EQ(1234.0, nearbyint(1234.0));
@@ -858,9 +852,7 @@ TEST(math, nearbyint) {
 }
 
 TEST(math, lround) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_UPWARD); // lround ignores the rounding mode.
   ASSERT_EQ(1234, lround(1234.01));
   ASSERT_EQ(1234, lroundf(1234.01f));
@@ -868,9 +860,7 @@ TEST(math, lround) {
 }
 
 TEST(math, llround) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_UPWARD); // llround ignores the rounding mode.
   ASSERT_EQ(1234L, llround(1234.01));
   ASSERT_EQ(1234L, llroundf(1234.01f));
@@ -965,9 +955,7 @@ TEST(math, fdiml) {
 }
 
 TEST(math, round) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_TOWARDZERO); // round ignores the rounding mode and always rounds away from zero.
   ASSERT_DOUBLE_EQ(1.0, round(0.5));
   ASSERT_DOUBLE_EQ(-1.0, round(-0.5));
@@ -978,9 +966,7 @@ TEST(math, round) {
 }
 
 TEST(math, roundf) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_TOWARDZERO); // roundf ignores the rounding mode and always rounds away from zero.
   ASSERT_FLOAT_EQ(1.0f, roundf(0.5f));
   ASSERT_FLOAT_EQ(-1.0f, roundf(-0.5f));
@@ -991,9 +977,7 @@ TEST(math, roundf) {
 }
 
 TEST(math, roundl) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero.
   ASSERT_DOUBLE_EQ(1.0L, roundl(0.5L));
   ASSERT_DOUBLE_EQ(-1.0L, roundl(-0.5L));
@@ -1004,9 +988,7 @@ TEST(math, roundl) {
 }
 
 TEST(math, trunc) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_UPWARD); // trunc ignores the rounding mode and always rounds toward zero.
   ASSERT_DOUBLE_EQ(1.0, trunc(1.5));
   ASSERT_DOUBLE_EQ(-1.0, trunc(-1.5));
@@ -1017,9 +999,7 @@ TEST(math, trunc) {
 }
 
 TEST(math, truncf) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_UPWARD); // truncf ignores the rounding mode and always rounds toward zero.
   ASSERT_FLOAT_EQ(1.0f, truncf(1.5f));
   ASSERT_FLOAT_EQ(-1.0f, truncf(-1.5f));
@@ -1030,9 +1010,7 @@ TEST(math, truncf) {
 }
 
 TEST(math, truncl) {
-  auto guard = make_scope_guard([]() {
-    fesetenv(FE_DFL_ENV);
-  });
+  auto guard = android::base::make_scope_guard([]() { fesetenv(FE_DFL_ENV); });
   fesetround(FE_UPWARD); // truncl ignores the rounding mode and always rounds toward zero.
   ASSERT_DOUBLE_EQ(1.0L, truncl(1.5L));
   ASSERT_DOUBLE_EQ(-1.0L, truncl(-1.5L));
index 60fe294..87b4c81 100755 (executable)
 #include <atomic>
 #include <vector>
 
+#include <android-base/scopeguard.h>
+
 #include "private/bionic_constants.h"
 #include "private/bionic_macros.h"
-#include "private/ScopeGuard.h"
 #include "BionicDeathTest.h"
 #include "ScopedSignalHandler.h"
 #include "utils.h"
@@ -64,7 +65,7 @@ TEST(pthread, pthread_key_many_distinct) {
   int nkeys = PTHREAD_KEYS_MAX / 2;
   std::vector<pthread_key_t> keys;
 
-  auto scope_guard = make_scope_guard([&keys]{
+  auto scope_guard = android::base::make_scope_guard([&keys] {
     for (const auto& key : keys) {
       EXPECT_EQ(0, pthread_key_delete(key));
     }
@@ -1362,7 +1363,7 @@ TEST(pthread, pthread_attr_getstack__main_thread) {
   }
   EXPECT_EQ(rl.rlim_cur, stack_size);
 
-  auto guard = make_scope_guard([&rl, original_rlim_cur]() {
+  auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() {
     rl.rlim_cur = original_rlim_cur;
     ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
   });
index d90b01e..9eae06e 100644 (file)
@@ -996,7 +996,7 @@ TEST(UNISTD_TEST, sysconf_SC_ARG_MAX) {
   if (rl.rlim_cur == RLIM_INFINITY) {
     rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
   }
-  auto guard = make_scope_guard([&rl, original_rlim_cur]() {
+  auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() {
     rl.rlim_cur = original_rlim_cur;
     ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
   });
index fa85545..2e9b994 100644 (file)
 #include <regex>
 
 #include <android-base/file.h>
+#include <android-base/scopeguard.h>
 #include <android-base/stringprintf.h>
 
-#include "private/ScopeGuard.h"
-
 #if defined(__LP64__)
 #define PATH_TO_SYSTEM_LIB "/system/lib64/"
 #else
@@ -68,9 +67,7 @@ class Maps {
       return false;
     }
 
-    auto fp_guard = make_scope_guard([&]() {
-      fclose(fp);
-    });
+    auto fp_guard = android::base::make_scope_guard([&]() { fclose(fp); });
 
     char line[BUFSIZ];
     while (fgets(line, sizeof(line), fp) != nullptr) {