[compiler-rt] 88a1529 - [NFC][scudo] Move globals into related test

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 1 18:35:20 PDT 2021


Author: Vitaly Buka
Date: 2021-04-01T18:35:09-07:00
New Revision: 88a1529e1560f43cfbe680a703dab25a5521f7ce

URL: https://github.com/llvm/llvm-project/commit/88a1529e1560f43cfbe680a703dab25a5521f7ce
DIFF: https://github.com/llvm/llvm-project/commit/88a1529e1560f43cfbe680a703dab25a5521f7ce.diff

LOG: [NFC][scudo] Move globals into related test

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/tests/combined_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
index b93efd4453db..a90a9320ceba 100644
--- a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
@@ -18,10 +18,6 @@
 #include <thread>
 #include <vector>
 
-static std::mutex Mutex;
-static std::condition_variable Cv;
-static bool Ready;
-
 static constexpr scudo::Chunk::Origin Origin = scudo::Chunk::Origin::Malloc;
 
 template <class Config> struct UseQuarantineSetter {
@@ -349,35 +345,34 @@ TYPED_TEST(ScudoCombinedTest, BasicCombined) {
     TSD->unlock();
 }
 
-template <typename AllocatorT> static void stressAllocator(AllocatorT *A) {
-  {
-    std::unique_lock<std::mutex> Lock(Mutex);
-    while (!Ready)
-      Cv.wait(Lock);
-  }
-  std::vector<std::pair<void *, scudo::uptr>> V;
-  for (scudo::uptr I = 0; I < 256U; I++) {
-    const scudo::uptr Size = std::rand() % 4096U;
-    void *P = A->allocate(Size, Origin);
-    // A region could have ran out of memory, resulting in a null P.
-    if (P)
-      V.push_back(std::make_pair(P, Size));
-  }
-  while (!V.empty()) {
-    auto Pair = V.back();
-    A->deallocate(Pair.first, Origin, Pair.second);
-    V.pop_back();
-  }
-}
-
 TYPED_TEST(ScudoCombinedTest, ThreadedCombined) {
-  using Config = TypeParam;
-  Ready = false;
-  using AllocatorT = TestAllocator<Config>;
+  std::mutex Mutex;
+  std::condition_variable Cv;
+  bool Ready = false;
+  using AllocatorT = TestAllocator<TypeParam>;
   auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT());
   std::thread Threads[32];
   for (scudo::uptr I = 0; I < ARRAY_SIZE(Threads); I++)
-    Threads[I] = std::thread(stressAllocator<AllocatorT>, Allocator.get());
+    Threads[I] = std::thread([&]() {
+      {
+        std::unique_lock<std::mutex> Lock(Mutex);
+        while (!Ready)
+          Cv.wait(Lock);
+      }
+      std::vector<std::pair<void *, scudo::uptr>> V;
+      for (scudo::uptr I = 0; I < 256U; I++) {
+        const scudo::uptr Size = std::rand() % 4096U;
+        void *P = Allocator->allocate(Size, Origin);
+        // A region could have ran out of memory, resulting in a null P.
+        if (P)
+          V.push_back(std::make_pair(P, Size));
+      }
+      while (!V.empty()) {
+        auto Pair = V.back();
+        Allocator->deallocate(Pair.first, Origin, Pair.second);
+        V.pop_back();
+      }
+    });
   {
     std::unique_lock<std::mutex> Lock(Mutex);
     Ready = true;


        


More information about the llvm-commits mailing list