Skip to content

Commit

Permalink
[PA] Mirror cxx17_backports
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 5 deletions.
1 change: 1 addition & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3639,6 +3639,7 @@ test("base_unittests") {
"allocator/partition_allocator/page_allocator_unittest.cc",
"allocator/partition_allocator/partition_alloc_base/bits_pa_unittest.cc",
"allocator/partition_allocator/partition_alloc_base/cpu_pa_unittest.cc",
"allocator/partition_allocator/partition_alloc_base/cxx17_backports_pa_unittest.cc",
"allocator/partition_allocator/partition_alloc_base/logging_pa_unittest.cc",
"allocator/partition_allocator/partition_alloc_base/rand_util_pa_unittest.cc",
"allocator/partition_allocator/partition_alloc_base/scoped_clear_last_error_pa_unittest.cc",
Expand Down
1 change: 1 addition & 0 deletions base/allocator/partition_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ target(partition_alloc_target_type, "partition_alloc") {
"partition_alloc_base/bits.h",
"partition_alloc_base/cpu.cc",
"partition_alloc_base/cpu.h",
"partition_alloc_base/cxx17_backports.h",
"partition_alloc_base/debug/alias.cc",
"partition_alloc_base/debug/alias.h",
"partition_alloc_base/logging.cc",
Expand Down
1 change: 0 additions & 1 deletion base/allocator/partition_allocator/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ include_rules = [
"+base/check.h",
"+base/check_op.h",
"+base/compiler_specific.h",
"+base/cxx17_backports.h",
"+base/dcheck_is_on.h",
"+base/debug/proc_maps_linux.h",
"+base/fuchsia/fuchsia_logging.h",
Expand Down
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_
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
8 changes: 4 additions & 4 deletions base/allocator/partition_allocator/thread_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include <atomic>
#include <cstdint>

#include "base/allocator/partition_allocator/partition_alloc_base/cxx17_backports.h"
#include "base/allocator/partition_allocator/partition_alloc_check.h"
#include "base/allocator/partition_allocator/partition_alloc_config.h"
#include "base/allocator/partition_allocator/partition_alloc_constants.h"
#include "base/allocator/partition_allocator/partition_root.h"
#include "base/base_export.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/cxx17_backports.h"
#include "base/dcheck_is_on.h"
#include "build/build_config.h"

Expand Down Expand Up @@ -294,7 +294,7 @@ void ThreadCacheRegistry::RunPeriodicPurge() {
// of cached memory cannot change between calls (since we do not purge
// background threads, but only ask them to purge their own cache at the next
// allocation).
periodic_purge_next_interval_ = base::clamp(
periodic_purge_next_interval_ = internal::base::clamp(
periodic_purge_next_interval_, kMinPurgeInterval, kMaxPurgeInterval);

PurgeAll();
Expand Down Expand Up @@ -411,8 +411,8 @@ void ThreadCache::SetGlobalLimits(PartitionRoot<>* root, float multiplier) {
constexpr size_t kMinLimit = 1;
// |PutInBucket()| is called on a full bucket, which should not overflow.
constexpr size_t kMaxLimit = std::numeric_limits<uint8_t>::max() - 1;
global_limits_[index] =
static_cast<uint8_t>(base::clamp(value, kMinLimit, kMaxLimit));
global_limits_[index] = static_cast<uint8_t>(
internal::base::clamp(value, kMinLimit, kMaxLimit));
PA_DCHECK(global_limits_[index] >= kMinLimit);
PA_DCHECK(global_limits_[index] <= kMaxLimit);
}
Expand Down

0 comments on commit c0e0a82

Please sign in to comment.