[sanitizer] Cleanup sorting functions

llvm-svn: 331915
This commit is contained in:
Vitaly Buka 2018-05-09 20:42:11 +00:00
parent 42ba76c9f6
commit d3e55bf7fc
12 changed files with 31 additions and 41 deletions

View File

@ -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: "

View File

@ -685,7 +685,7 @@ void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) {
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;

View File

@ -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;

View File

@ -58,19 +58,6 @@ void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
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;

View File

@ -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 InternalMmapVector<char> {
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 size, Compare comp) {
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 size, Compare comp) {
// 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;
}

View File

@ -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) {

View File

@ -63,7 +63,7 @@ static void SanitizerDumpCoverage(const uptr* unsorted_pcs, uptr len) {
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;

View File

@ -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) {

View File

@ -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());

View File

@ -146,7 +146,7 @@ StackDepotReverseMap::StackDepotReverseMap() {
map_.push_back(pair);
}
}
InternalSort(&map_, map_.size(), IdDescPair::IdComparator);
Sort(map_.data(), map_.size(), &IdDescPair::IdComparator);
}
StackTrace StackDepotReverseMap::Get(u32 id) {

View File

@ -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));
}

View File

@ -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) {")