-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds the `cxx17_backports` module to PartitionAlloc's internal mirror of `//base`. Only `base::clamp()` is retained; the unused `base::apply()` is stripped. Note: Patchset 2 provides the baseline "straight copy" for ease of reviewing and patchset 3 performs the actual mirror. Bug: 1151236 Change-Id: Idf52bda986c2fae289a3559f7399fccfc378d316 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3615589 Reviewed-by: Takashi Sakamoto <tasak@google.com> Reviewed-by: Bartek Nowierski <bartekn@chromium.org> Reviewed-by: Yuki Shiino <yukishiino@chromium.org> Commit-Queue: Kalvin Lee <kdlee@chromium.org> Cr-Commit-Position: refs/heads/main@{#1001361}
- Loading branch information
Kalvin Lee
authored and
Chromium LUCI CQ
committed
May 10, 2022
1 parent
d18a701
commit c0e0a82
Showing
6 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
base/allocator/partition_allocator/partition_alloc_base/cxx17_backports.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_CXX17_BACKPORTS_H_ | ||
#define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_CXX17_BACKPORTS_H_ | ||
|
||
#include <functional> | ||
#include <tuple> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
#include "base/allocator/partition_allocator/partition_alloc_check.h" | ||
|
||
namespace partition_alloc::internal::base { | ||
|
||
// C++14 implementation of C++17's std::clamp(): | ||
// https://en.cppreference.com/w/cpp/algorithm/clamp | ||
// Please note that the C++ spec makes it undefined behavior to call std::clamp | ||
// with a value of `lo` that compares greater than the value of `hi`. This | ||
// implementation uses a CHECK to enforce this as a hard restriction. | ||
template <typename T, typename Compare> | ||
constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp) { | ||
PA_CHECK(!comp(hi, lo)); | ||
return comp(v, lo) ? lo : comp(hi, v) ? hi : v; | ||
} | ||
|
||
template <typename T> | ||
constexpr const T& clamp(const T& v, const T& lo, const T& hi) { | ||
return base::clamp(v, lo, hi, std::less<T>{}); | ||
} | ||
|
||
} // namespace partition_alloc::internal::base | ||
|
||
#endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_CXX17_BACKPORTS_H_ |
101 changes: 101 additions & 0 deletions
101
base/allocator/partition_allocator/partition_alloc_base/cxx17_backports_pa_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/allocator/partition_allocator/partition_alloc_base/cxx17_backports.h" | ||
|
||
#include <array> | ||
#include <memory> | ||
#include <tuple> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/test/gtest_util.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace partition_alloc::internal::base { | ||
namespace { | ||
|
||
struct OneType { | ||
int some_int; | ||
}; | ||
|
||
bool operator<(const OneType& lhs, const OneType& rhs) { | ||
return lhs.some_int < rhs.some_int; | ||
} | ||
|
||
bool operator==(const OneType& lhs, const OneType& rhs) { | ||
return lhs.some_int == rhs.some_int; | ||
} | ||
|
||
struct AnotherType { | ||
int some_other_int; | ||
}; | ||
|
||
bool operator==(const AnotherType& lhs, const AnotherType& rhs) { | ||
return lhs.some_other_int == rhs.some_other_int; | ||
} | ||
|
||
TEST(Cxx17BackportTest, Clamp) { | ||
EXPECT_EQ(0, base::clamp(-5, 0, 10)); | ||
EXPECT_EQ(0, base::clamp(0, 0, 10)); | ||
EXPECT_EQ(3, base::clamp(3, 0, 10)); | ||
EXPECT_EQ(10, base::clamp(10, 0, 10)); | ||
EXPECT_EQ(10, base::clamp(15, 0, 10)); | ||
|
||
EXPECT_EQ(0.0, base::clamp(-5.0, 0.0, 10.0)); | ||
EXPECT_EQ(0.0, base::clamp(0.0, 0.0, 10.0)); | ||
EXPECT_EQ(3.0, base::clamp(3.0, 0.0, 10.0)); | ||
EXPECT_EQ(10.0, base::clamp(10.0, 0.0, 10.0)); | ||
EXPECT_EQ(10.0, base::clamp(15.0, 0.0, 10.0)); | ||
|
||
EXPECT_EQ(0, base::clamp(-5, 0, 0)); | ||
EXPECT_EQ(0, base::clamp(0, 0, 0)); | ||
EXPECT_EQ(0, base::clamp(3, 0, 0)); | ||
|
||
OneType one_type_neg5{-5}; | ||
OneType one_type_0{0}; | ||
OneType one_type_3{3}; | ||
OneType one_type_10{10}; | ||
OneType one_type_15{15}; | ||
|
||
EXPECT_EQ(one_type_0, base::clamp(one_type_neg5, one_type_0, one_type_10)); | ||
EXPECT_EQ(one_type_0, base::clamp(one_type_0, one_type_0, one_type_10)); | ||
EXPECT_EQ(one_type_3, base::clamp(one_type_3, one_type_0, one_type_10)); | ||
EXPECT_EQ(one_type_10, base::clamp(one_type_10, one_type_0, one_type_10)); | ||
EXPECT_EQ(one_type_10, base::clamp(one_type_15, one_type_0, one_type_10)); | ||
|
||
AnotherType another_type_neg5{-5}; | ||
AnotherType another_type_0{0}; | ||
AnotherType another_type_3{3}; | ||
AnotherType another_type_10{10}; | ||
AnotherType another_type_15{15}; | ||
|
||
auto compare_another_type = [](const auto& lhs, const auto& rhs) { | ||
return lhs.some_other_int < rhs.some_other_int; | ||
}; | ||
|
||
EXPECT_EQ(another_type_0, base::clamp(another_type_neg5, another_type_0, | ||
another_type_10, compare_another_type)); | ||
EXPECT_EQ(another_type_0, base::clamp(another_type_0, another_type_0, | ||
another_type_10, compare_another_type)); | ||
EXPECT_EQ(another_type_3, base::clamp(another_type_3, another_type_0, | ||
another_type_10, compare_another_type)); | ||
EXPECT_EQ(another_type_10, | ||
base::clamp(another_type_10, another_type_0, another_type_10, | ||
compare_another_type)); | ||
EXPECT_EQ(another_type_10, | ||
base::clamp(another_type_15, another_type_0, another_type_10, | ||
compare_another_type)); | ||
|
||
EXPECT_CHECK_DEATH(base::clamp(3, 10, 0)); | ||
EXPECT_CHECK_DEATH(base::clamp(3.0, 10.0, 0.0)); | ||
EXPECT_CHECK_DEATH(base::clamp(one_type_3, one_type_10, one_type_0)); | ||
EXPECT_CHECK_DEATH(base::clamp(another_type_3, another_type_10, | ||
another_type_0, compare_another_type)); | ||
} | ||
|
||
} // namespace | ||
} // namespace partition_alloc::internal::base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters