[compiler-rt] r331915 - [sanitizer] Cleanup sorting functions

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed May 9 13:42:11 PDT 2018


Author: vitalybuka
Date: Wed May  9 13:42:11 2018
New Revision: 331915

URL: http://llvm.org/viewvc/llvm-project?rev=331915&view=rev
Log:
[sanitizer] Cleanup sorting functions

Modified:
    compiler-rt/trunk/lib/asan/asan_memory_profile.cc
    compiler-rt/trunk/lib/lsan/lsan_common.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_secondary.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_interceptors_ioctl_netbsd.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc
    compiler-rt/trunk/utils/generate_netbsd_ioctls.awk

Modified: compiler-rt/trunk/lib/asan/asan_memory_profile.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_memory_profile.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_memory_profile.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_memory_profile.cc Wed May  9 13:42:11 2018
@@ -49,10 +49,10 @@ class HeapProfile {
   }
 
   void Print(uptr top_percent, uptr max_number_of_contexts) {
-    InternalSort(&allocations_, allocations_.size(),
-                 [](const AllocationSite &a, const AllocationSite &b) {
-                   return a.total_size > b.total_size;
-                 });
+    Sort(allocations_.data(), allocations_.size(),
+         [](const AllocationSite &a, const AllocationSite &b) {
+           return a.total_size > b.total_size;
+         });
     CHECK(total_allocated_user_size_);
     uptr total_shown = 0;
     Printf("Live Heap Allocations: %zd bytes in %zd chunks; quarantined: "

Modified: compiler-rt/trunk/lib/lsan/lsan_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common.cc Wed May  9 13:42:11 2018
@@ -685,7 +685,7 @@ void LeakReport::ReportTopLeaks(uptr num
   uptr unsuppressed_count = UnsuppressedLeakCount();
   if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count)
     Printf("The %zu top leak(s):\n", num_leaks_to_report);
-  InternalSort(&leaks_, leaks_.size(), LeakComparator);
+  Sort(leaks_.data(), leaks_.size(), &LeakComparator);
   uptr leaks_reported = 0;
   for (uptr i = 0; i < leaks_.size(); i++) {
     if (leaks_[i].is_suppressed) continue;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_secondary.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_secondary.h?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_secondary.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_secondary.h Wed May  9 13:42:11 2018
@@ -202,7 +202,7 @@ class LargeMmapAllocator {
 
   void EnsureSortedChunks() {
     if (chunks_sorted_) return;
-    SortArray(reinterpret_cast<uptr*>(chunks_), n_chunks_);
+    Sort(reinterpret_cast<uptr *>(chunks_), n_chunks_);
     for (uptr i = 0; i < n_chunks_; i++)
       chunks_[i]->chunk_idx = i;
     chunks_sorted_ = true;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Wed May  9 13:42:11 2018
@@ -58,19 +58,6 @@ void NORETURN ReportMmapFailureAndDie(up
 typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
 typedef bool U32ComparisonFunction(const u32 &a, const u32 &b);
 
-template<class T>
-static inline bool CompareLess(const T &a, const T &b) {
-  return a < b;
-}
-
-void SortArray(uptr *array, uptr size) {
-  InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
-}
-
-void SortArray(u32 *array, uptr size) {
-  InternalSort<u32*, U32ComparisonFunction>(&array, size, CompareLess);
-}
-
 const char *StripPathPrefix(const char *filepath,
                             const char *strip_path_prefix) {
   if (!filepath) return nullptr;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Wed May  9 13:42:11 2018
@@ -243,8 +243,6 @@ void SleepForMillis(int millis);
 u64 NanoTime();
 u64 MonotonicNanoTime();
 int Atexit(void (*function)(void));
-void SortArray(uptr *array, uptr size);
-void SortArray(u32 *array, uptr size);
 bool TemplateMatch(const char *templ, const char *str);
 
 // Exit
@@ -572,9 +570,14 @@ class InternalScopedString : public Inte
   uptr length_;
 };
 
+template <class T>
+struct CompareLess {
+  bool operator()(const T &a, const T &b) const { return a < b; }
+};
+
 // HeapSort for arrays and InternalMmapVector.
-template<class Container, class Compare>
-void InternalSort(Container *v, uptr size, Compare comp) {
+template <class T, class Compare = CompareLess<T>>
+void Sort(T *v, uptr size, Compare comp = {}) {
   if (size < 2)
     return;
   // Stage 1: insert elements to the heap.
@@ -582,8 +585,8 @@ void InternalSort(Container *v, uptr siz
     uptr j, p;
     for (j = i; j > 0; j = p) {
       p = (j - 1) / 2;
-      if (comp((*v)[p], (*v)[j]))
-        Swap((*v)[j], (*v)[p]);
+      if (comp(v[p], v[j]))
+        Swap(v[j], v[p]);
       else
         break;
     }
@@ -591,18 +594,18 @@ void InternalSort(Container *v, uptr siz
   // Stage 2: swap largest element with the last one,
   // and sink the new top.
   for (uptr i = size - 1; i > 0; i--) {
-    Swap((*v)[0], (*v)[i]);
+    Swap(v[0], v[i]);
     uptr j, max_ind;
     for (j = 0; j < i; j = max_ind) {
       uptr left = 2 * j + 1;
       uptr right = 2 * j + 2;
       max_ind = j;
-      if (left < i && comp((*v)[max_ind], (*v)[left]))
+      if (left < i && comp(v[max_ind], v[left]))
         max_ind = left;
-      if (right < i && comp((*v)[max_ind], (*v)[right]))
+      if (right < i && comp(v[max_ind], v[right]))
         max_ind = right;
       if (max_ind != j)
-        Swap((*v)[j], (*v)[max_ind]);
+        Swap(v[j], v[max_ind]);
       else
         break;
     }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc Wed May  9 13:42:11 2018
@@ -480,7 +480,7 @@ struct ioctl_desc_compare {
 
 static void ioctl_init() {
   ioctl_table_fill();
-  InternalSort(&ioctl_table, ioctl_table_size, ioctl_desc_compare());
+  Sort(ioctl_table, ioctl_table_size, ioctl_desc_compare());
 
   bool bad = false;
   for (unsigned i = 0; i < ioctl_table_size - 1; ++i) {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc Wed May  9 13:42:11 2018
@@ -63,7 +63,7 @@ static void SanitizerDumpCoverage(const
   uptr* pcs = static_cast<uptr*>(InternalAlloc(len * sizeof(uptr)));
 
   internal_memcpy(pcs, unsorted_pcs, len * sizeof(uptr));
-  SortArray(pcs, len);
+  Sort(pcs, len);
 
   bool module_found = false;
   uptr last_base = 0;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_interceptors_ioctl_netbsd.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_interceptors_ioctl_netbsd.inc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_interceptors_ioctl_netbsd.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_interceptors_ioctl_netbsd.inc Wed May  9 13:42:11 2018
@@ -1372,7 +1372,7 @@ struct ioctl_desc_compare {
 
 static void ioctl_init() {
   ioctl_table_fill();
-  InternalSort(&ioctl_table, ioctl_table_size, ioctl_desc_compare());
+  Sort(ioctl_table, ioctl_table_size, ioctl_desc_compare());
 
   bool bad = false;
   for (unsigned i = 0; i < ioctl_table_size - 1; ++i) {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc Wed May  9 13:42:11 2018
@@ -1035,7 +1035,7 @@ void PrintModuleMap() {
   InternalMmapVector<LoadedModule> modules;
   modules.reserve(128);
   memory_mapping.DumpListOfModules(&modules);
-  InternalSort(&modules, modules.size(), CompareBaseAddress);
+  Sort(modules.data(), modules.size(), CompareBaseAddress);
   for (uptr i = 0; i < modules.size(); ++i) {
     char uuid_str[128];
     FormatUUID(uuid_str, sizeof(uuid_str), modules[i].uuid());

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc Wed May  9 13:42:11 2018
@@ -146,7 +146,7 @@ StackDepotReverseMap::StackDepotReverseM
       map_.push_back(pair);
     }
   }
-  InternalSort(&map_, map_.size(), IdDescPair::IdComparator);
+  Sort(map_.data(), map_.size(), &IdDescPair::IdComparator);
 }
 
 StackTrace StackDepotReverseMap::Get(u32 id) {

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc Wed May  9 13:42:11 2018
@@ -39,37 +39,37 @@ TEST(SanitizerCommon, SortTest) {
   for (uptr i = 0; i < n; i++) {
     array[i] = i;
   }
-  SortArray(array, n);
+  Sort(array, n);
   EXPECT_TRUE(IsSorted(array, n));
   // Reverse order.
   for (uptr i = 0; i < n; i++) {
     array[i] = n - 1 - i;
   }
-  SortArray(array, n);
+  Sort(array, n);
   EXPECT_TRUE(IsSorted(array, n));
   // Mixed order.
   for (uptr i = 0; i < n; i++) {
     array[i] = (i % 2 == 0) ? i : n - 1 - i;
   }
-  SortArray(array, n);
+  Sort(array, n);
   EXPECT_TRUE(IsSorted(array, n));
   // All equal.
   for (uptr i = 0; i < n; i++) {
     array[i] = 42;
   }
-  SortArray(array, n);
+  Sort(array, n);
   EXPECT_TRUE(IsSorted(array, n));
   // All but one sorted.
   for (uptr i = 0; i < n - 1; i++) {
     array[i] = i;
   }
   array[n - 1] = 42;
-  SortArray(array, n);
+  Sort(array, n);
   EXPECT_TRUE(IsSorted(array, n));
   // Minimal case - sort three elements.
   array[0] = 1;
   array[1] = 0;
-  SortArray(array, 2);
+  Sort(array, 2);
   EXPECT_TRUE(IsSorted(array, 2));
 }
 

Modified: compiler-rt/trunk/utils/generate_netbsd_ioctls.awk
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/utils/generate_netbsd_ioctls.awk?rev=331915&r1=331914&r2=331915&view=diff
==============================================================================
--- compiler-rt/trunk/utils/generate_netbsd_ioctls.awk (original)
+++ compiler-rt/trunk/utils/generate_netbsd_ioctls.awk Wed May  9 13:42:11 2018
@@ -457,7 +457,7 @@ END {
   pcmd("")
   pcmd("static void ioctl_init() {")
   pcmd("  ioctl_table_fill();")
-  pcmd("  InternalSort(&ioctl_table, ioctl_table_size, ioctl_desc_compare());")
+  pcmd("  Sort(ioctl_table, ioctl_table_size, ioctl_desc_compare());")
   pcmd("")
   pcmd("  bool bad = false;")
   pcmd("  for (unsigned i = 0; i < ioctl_table_size - 1; ++i) {")




More information about the llvm-commits mailing list