[NFC][sanitizer] Simplify InternalLowerBound

This commit is contained in:
Vitaly Buka 2020-12-28 17:16:49 -08:00
parent 673b12e76f
commit 4e74480e02
5 changed files with 27 additions and 32 deletions

View File

@ -551,8 +551,7 @@ static void ReportIfNotSuspended(ThreadContextBase *tctx, void *arg) {
const InternalMmapVector<tid_t> &suspended_threads =
*(const InternalMmapVector<tid_t> *)arg;
if (tctx->status == ThreadStatusRunning) {
uptr i = InternalLowerBound(suspended_threads, 0, suspended_threads.size(),
tctx->os_id, CompareLess<int>());
uptr i = InternalLowerBound(suspended_threads, tctx->os_id);
if (i >= suspended_threads.size() || suspended_threads[i] != tctx->os_id)
Report("Running thread %d was not suspended. False leaks are possible.\n",
tctx->os_id);

View File

@ -107,9 +107,7 @@ void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
auto params = static_cast<const Params *>(data);
uptr begin = reinterpret_cast<uptr>(chunk);
uptr end = begin + size;
auto i = __sanitizer::InternalLowerBound(params->allocator_caches, 0,
params->allocator_caches.size(),
begin, CompareLess<uptr>());
auto i = __sanitizer::InternalLowerBound(params->allocator_caches, begin);
if (i < params->allocator_caches.size() &&
params->allocator_caches[i] >= begin &&
end - params->allocator_caches[i] <= sizeof(AllocatorCache)) {

View File

@ -467,6 +467,7 @@ inline int ToLower(int c) {
template<typename T>
class InternalMmapVectorNoCtor {
public:
using value_type = T;
void Initialize(uptr initial_capacity) {
capacity_bytes_ = 0;
size_ = 0;
@ -651,9 +652,13 @@ void Sort(T *v, uptr size, Compare comp = {}) {
// Works like std::lower_bound: finds the first element that is not less
// than the val.
template <class Container, class Value, class Compare>
uptr InternalLowerBound(const Container &v, uptr first, uptr last,
const Value &val, Compare comp) {
template <class Container,
class Compare = CompareLess<typename Container::value_type>>
uptr InternalLowerBound(const Container &v,
const typename Container::value_type &val,
Compare comp = {}) {
uptr first = 0;
uptr last = v.size();
while (last > first) {
uptr mid = (first + last) / 2;
if (comp(v[mid], val))

View File

@ -145,8 +145,7 @@ StackTrace StackDepotReverseMap::Get(u32 id) {
if (!map_.size())
return StackTrace();
IdDescPair pair = {id, nullptr};
uptr idx =
InternalLowerBound(map_, 0, map_.size(), pair, IdDescPair::IdComparator);
uptr idx = InternalLowerBound(map_, pair, IdDescPair::IdComparator);
if (idx > map_.size() || map_[idx].id != id)
return StackTrace();
return map_[idx].desc->load();

View File

@ -226,27 +226,21 @@ bool UptrLess(uptr a, uptr b) {
}
TEST(SanitizerCommon, InternalLowerBound) {
static const uptr kSize = 5;
int arr[kSize];
arr[0] = 1;
arr[1] = 3;
arr[2] = 5;
arr[3] = 7;
arr[4] = 11;
std::vector<int> arr = {1, 3, 5, 7, 11};
EXPECT_EQ(0u, InternalLowerBound(arr, 0, kSize, 0, UptrLess));
EXPECT_EQ(0u, InternalLowerBound(arr, 0, kSize, 1, UptrLess));
EXPECT_EQ(1u, InternalLowerBound(arr, 0, kSize, 2, UptrLess));
EXPECT_EQ(1u, InternalLowerBound(arr, 0, kSize, 3, UptrLess));
EXPECT_EQ(2u, InternalLowerBound(arr, 0, kSize, 4, UptrLess));
EXPECT_EQ(2u, InternalLowerBound(arr, 0, kSize, 5, UptrLess));
EXPECT_EQ(3u, InternalLowerBound(arr, 0, kSize, 6, UptrLess));
EXPECT_EQ(3u, InternalLowerBound(arr, 0, kSize, 7, UptrLess));
EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 8, UptrLess));
EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 9, UptrLess));
EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 10, UptrLess));
EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 11, UptrLess));
EXPECT_EQ(5u, InternalLowerBound(arr, 0, kSize, 12, UptrLess));
EXPECT_EQ(0u, InternalLowerBound(arr, 0));
EXPECT_EQ(0u, InternalLowerBound(arr, 1));
EXPECT_EQ(1u, InternalLowerBound(arr, 2));
EXPECT_EQ(1u, InternalLowerBound(arr, 3));
EXPECT_EQ(2u, InternalLowerBound(arr, 4));
EXPECT_EQ(2u, InternalLowerBound(arr, 5));
EXPECT_EQ(3u, InternalLowerBound(arr, 6));
EXPECT_EQ(3u, InternalLowerBound(arr, 7));
EXPECT_EQ(4u, InternalLowerBound(arr, 8));
EXPECT_EQ(4u, InternalLowerBound(arr, 9));
EXPECT_EQ(4u, InternalLowerBound(arr, 10));
EXPECT_EQ(4u, InternalLowerBound(arr, 11));
EXPECT_EQ(5u, InternalLowerBound(arr, 12));
}
TEST(SanitizerCommon, InternalLowerBoundVsStdLowerBound) {
@ -268,8 +262,8 @@ TEST(SanitizerCommon, InternalLowerBoundVsStdLowerBound) {
for (auto to_find : {val - 1, val, val + 1}) {
uptr expected =
std::lower_bound(data.begin(), data.end(), to_find) - data.begin();
EXPECT_EQ(expected, InternalLowerBound(data.data(), 0, data.size(),
to_find, std::less<int>()));
EXPECT_EQ(expected,
InternalLowerBound(data, to_find, std::less<int>()));
}
}
}