Skip to content

Commit

Permalink
Migrate base::{size,empty,data} to STL equivalents in //base.
Browse files Browse the repository at this point in the history
This is not a complete migration; this only migrates fully-qualified
references to these functions, with the minimal set of build fixes
needed to make things compile.

Bug: 1299695
Change-Id: Ib7359b018dae054adcc87e435aee0f186c74fd50
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3492822
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Owners-Override: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#975977}
  • Loading branch information
zetafunction authored and Chromium LUCI CQ committed Feb 28, 2022
1 parent 3cedd0e commit f45f476
Show file tree
Hide file tree
Showing 104 changed files with 426 additions and 494 deletions.
5 changes: 2 additions & 3 deletions base/allocator/partition_allocator/address_pool_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "base/allocator/partition_allocator/partition_alloc_constants.h"
#include "base/allocator/partition_allocator/partition_alloc_notreached.h"
#include "base/allocator/partition_allocator/reservation_offset_table.h"
#include "base/cxx17_backports.h"
#include "base/lazy_instance.h"
#include "build/build_config.h"

Expand Down Expand Up @@ -57,7 +56,7 @@ pool_handle AddressPoolManager::Add(uintptr_t ptr, size_t length) {
PA_DCHECK(!(ptr & kSuperPageOffsetMask));
PA_DCHECK(!((ptr + length) & kSuperPageOffsetMask));

for (pool_handle i = 0; i < base::size(pools_); ++i) {
for (pool_handle i = 0; i < std::size(pools_); ++i) {
if (!pools_[i].IsInitialized()) {
pools_[i].Initialize(ptr, length);
return i + 1;
Expand Down Expand Up @@ -86,7 +85,7 @@ uintptr_t AddressPoolManager::GetPoolBaseAddress(pool_handle handle) {
}

void AddressPoolManager::ResetForTesting() {
for (pool_handle i = 0; i < base::size(pools_); ++i)
for (pool_handle i = 0; i < std::size(pools_); ++i)
pools_[i].Reset();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "base/allocator/partition_allocator/tagging.h"
#include "base/bits.h"
#include "base/cpu.h"
#include "base/cxx17_backports.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/system/sys_info.h"
Expand Down Expand Up @@ -147,7 +146,7 @@ const size_t kTestSizes[] = {
1 << 20,
1 << 21,
};
constexpr size_t kTestSizesCount = base::size(kTestSizes);
constexpr size_t kTestSizesCount = std::size(kTestSizes);

void AllocateRandomly(base::PartitionRoot<base::internal::ThreadSafe>* root,
size_t count,
Expand Down Expand Up @@ -4382,10 +4381,10 @@ class ScopedOpenCLNoOpKernel {
const size_t source_lengths[] = {
strlen(sources[0]),
};
static_assert(base::size(sources) == base::size(source_lengths),
static_assert(std::size(sources) == std::size(source_lengths),
"arrays must be parallel");

program_ = clCreateProgramWithSource(context_, base::size(sources), sources,
program_ = clCreateProgramWithSource(context_, std::size(sources), sources,
source_lengths, &rv);
ASSERT_EQ(rv, CL_SUCCESS) << "clCreateProgramWithSource";

Expand Down
6 changes: 3 additions & 3 deletions base/android/jank_metric_uma_recorder_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ const int64_t kDurations[] = {
1'000'000, // 1ms
20'000'000, // 20ms
};
const size_t kDurationsLen = base::size(kDurations);
const size_t kDurationsLen = std::size(kDurations);

static_assert(base::size(kDurations) == base::size(kTimestampsNs),
static_assert(std::size(kDurations) == std::size(kTimestampsNs),
"Length of timestamps and durations should be equal.");

// Jank bursts are calculated based on durations.
const int64_t kJankBursts[] = {
100'000'000, // 100ms
20'000'000, // 20ms
};
const size_t kJankBurstsLen = base::size(kJankBursts);
const size_t kJankBurstsLen = std::size(kJankBursts);

} // namespace

Expand Down
29 changes: 14 additions & 15 deletions base/android/jni_array_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

#include <stddef.h>
#include <stdint.h>
#include <algorithm>

#include <algorithm>
#include <limits>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/cxx17_backports.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand All @@ -23,7 +22,7 @@ namespace android {

TEST(JniArray, BasicConversions) {
const uint8_t kBytes[] = {0, 1, 2, 3};
const size_t kLen = base::size(kBytes);
const size_t kLen = std::size(kBytes);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> bytes = ToJavaByteArray(env, kBytes, kLen);
ASSERT_TRUE(bytes);
Expand Down Expand Up @@ -79,7 +78,7 @@ void CheckBoolConversion(JNIEnv* env,

TEST(JniArray, BoolConversions) {
const bool kBools[] = {false, true, false};
const size_t kLen = base::size(kBools);
const size_t kLen = std::size(kBools);

JNIEnv* env = AttachCurrentThread();
CheckBoolConversion(env, kBools, kLen, ToJavaBooleanArray(env, kBools, kLen));
Expand All @@ -105,7 +104,7 @@ void CheckIntConversion(
TEST(JniArray, IntConversions) {
const int kInts[] = {0, 1, -1, std::numeric_limits<int32_t>::min(),
std::numeric_limits<int32_t>::max()};
const size_t kLen = base::size(kInts);
const size_t kLen = std::size(kInts);

JNIEnv* env = AttachCurrentThread();
CheckIntConversion(env, kInts, kLen, ToJavaIntArray(env, kInts, kLen));
Expand Down Expand Up @@ -133,7 +132,7 @@ void CheckLongConversion(JNIEnv* env,
TEST(JniArray, LongConversions) {
const int64_t kLongs[] = {0, 1, -1, std::numeric_limits<int64_t>::min(),
std::numeric_limits<int64_t>::max()};
const size_t kLen = base::size(kLongs);
const size_t kLen = std::size(kLongs);

JNIEnv* env = AttachCurrentThread();
CheckLongConversion(env, kLongs, kLen, ToJavaLongArray(env, kLongs, kLen));
Expand Down Expand Up @@ -207,7 +206,7 @@ TEST(JniArray, ArrayOfStringArrayConversionUTF16) {

TEST(JniArray, FloatConversions) {
const float kFloats[] = { 0.0f, 1.0f, -10.0f};
const size_t kLen = base::size(kFloats);
const size_t kLen = std::size(kFloats);

JNIEnv* env = AttachCurrentThread();
CheckFloatConversion(env, kFloats, kLen,
Expand All @@ -219,7 +218,7 @@ TEST(JniArray, FloatConversions) {

TEST(JniArray, JavaBooleanArrayToBoolVector) {
const bool kBools[] = {false, true, false};
const size_t kLen = base::size(kBools);
const size_t kLen = std::size(kBools);

JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbooleanArray> jbooleans(env, env->NewBooleanArray(kLen));
Expand All @@ -242,7 +241,7 @@ TEST(JniArray, JavaBooleanArrayToBoolVector) {

TEST(JniArray, JavaIntArrayToIntVector) {
const int kInts[] = {0, 1, -1};
const size_t kLen = base::size(kInts);
const size_t kLen = std::size(kInts);

JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen));
Expand All @@ -264,7 +263,7 @@ TEST(JniArray, JavaIntArrayToIntVector) {

TEST(JniArray, JavaLongArrayToInt64Vector) {
const int64_t kInt64s[] = {0LL, 1LL, -1LL};
const size_t kLen = base::size(kInt64s);
const size_t kLen = std::size(kInt64s);

JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jlongArray> jlongs(env, env->NewLongArray(kLen));
Expand Down Expand Up @@ -292,7 +291,7 @@ TEST(JniArray, JavaLongArrayToInt64Vector) {

TEST(JniArray, JavaLongArrayToLongVector) {
const int64_t kInt64s[] = {0LL, 1LL, -1LL};
const size_t kLen = base::size(kInt64s);
const size_t kLen = std::size(kInt64s);

JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jlongArray> jlongs(env, env->NewLongArray(kLen));
Expand All @@ -319,7 +318,7 @@ TEST(JniArray, JavaLongArrayToLongVector) {

TEST(JniArray, JavaFloatArrayToFloatVector) {
const float kFloats[] = {0.0, 0.5, -0.5};
const size_t kLen = base::size(kFloats);
const size_t kLen = std::size(kFloats);

JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jfloatArray> jfloats(env, env->NewFloatArray(kLen));
Expand Down Expand Up @@ -489,12 +488,12 @@ TEST(JniArray, JavaArrayOfIntArrayToIntVector) {
// Populate int[][] object.
const int kInts0[] = {0, 1, -1, std::numeric_limits<int32_t>::min(),
std::numeric_limits<int32_t>::max()};
const size_t kLen0 = base::size(kInts0);
const size_t kLen0 = std::size(kInts0);
ScopedJavaLocalRef<jintArray> int_array0 = ToJavaIntArray(env, kInts0, kLen0);
env->SetObjectArrayElement(array.obj(), 0, int_array0.obj());

const int kInts1[] = {3, 4, 5};
const size_t kLen1 = base::size(kInts1);
const size_t kLen1 = std::size(kInts1);
ScopedJavaLocalRef<jintArray> int_array1 = ToJavaIntArray(env, kInts1, kLen1);
env->SetObjectArrayElement(array.obj(), 1, int_array1.obj());

Expand All @@ -504,7 +503,7 @@ TEST(JniArray, JavaArrayOfIntArrayToIntVector) {
env->SetObjectArrayElement(array.obj(), 2, int_array2.obj());

const int kInts3[] = {16};
const size_t kLen3 = base::size(kInts3);
const size_t kLen3 = std::size(kInts3);
ScopedJavaLocalRef<jintArray> int_array3 = ToJavaIntArray(env, kInts3, kLen3);
env->SetObjectArrayElement(array.obj(), 3, int_array3.obj());

Expand Down
4 changes: 2 additions & 2 deletions base/command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ constexpr CommandLine::StringPieceType kSwitchPrefixes[] = {L"--", L"-", L"/"};
// Unixes don't use slash as a switch.
constexpr CommandLine::StringPieceType kSwitchPrefixes[] = {"--", "-"};
#endif
size_t switch_prefix_count = base::size(kSwitchPrefixes);
size_t switch_prefix_count = std::size(kSwitchPrefixes);

#if BUILDFLAG(IS_WIN)
// Switch string that specifies the single argument to the command line.
Expand Down Expand Up @@ -196,7 +196,7 @@ void CommandLine::set_slash_is_not_a_switch() {
// The last switch prefix should be slash, so adjust the size to skip it.
static_assert(base::make_span(kSwitchPrefixes).back() == L"/",
"Error: Last switch prefix is not a slash.");
switch_prefix_count = base::size(kSwitchPrefixes) - 1;
switch_prefix_count = std::size(kSwitchPrefixes) - 1;
}

// static
Expand Down
8 changes: 4 additions & 4 deletions base/command_line_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST(CommandLineTest, CommandLineConstructor) {
FILE_PATH_LITERAL("--not-a-switch"),
FILE_PATH_LITERAL("\"in the time of submarines...\""),
FILE_PATH_LITERAL("unquoted arg-with-space")};
CommandLine cl(size(argv), argv);
CommandLine cl(std::size(argv), argv);

EXPECT_FALSE(cl.GetCommandLineString().empty());
EXPECT_FALSE(cl.HasSwitch("cruller"));
Expand Down Expand Up @@ -293,7 +293,7 @@ TEST(CommandLineTest, AppendSwitchesDashDash) {
const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
FILE_PATH_LITERAL("--"),
FILE_PATH_LITERAL("--arg1") };
CommandLine cl(size(raw_argv), raw_argv);
CommandLine cl(std::size(raw_argv), raw_argv);

cl.AppendSwitch("switch1");
cl.AppendSwitchASCII("switch2", "foo");
Expand Down Expand Up @@ -564,7 +564,7 @@ TEST(CommandLineTest, MultipleSameSwitch) {
FILE_PATH_LITERAL("-baz"),
FILE_PATH_LITERAL("--foo=two") // --foo second time
};
CommandLine cl(size(argv), argv);
CommandLine cl(std::size(argv), argv);

EXPECT_TRUE(cl.HasSwitch("foo"));
EXPECT_TRUE(cl.HasSwitch("baz"));
Expand Down Expand Up @@ -614,7 +614,7 @@ TEST(CommandLineTest, MultipleFilterFileSwitch) {
CommandLine::SetDuplicateSwitchHandler(
std::make_unique<MergeDuplicateFoosSemicolon>());

CommandLine cl(size(argv), argv);
CommandLine cl(std::size(argv), argv);

EXPECT_TRUE(cl.HasSwitch("mergeable-foo"));
EXPECT_TRUE(cl.HasSwitch("baz"));
Expand Down
11 changes: 5 additions & 6 deletions base/containers/checked_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
#include <utility>

#include "base/containers/checked_iterators.h"
#include "base/cxx17_backports.h"
#include "base/template_util.h"

namespace base {

// CheckedContiguousRange is a light-weight wrapper around a container modeling
// the ContiguousContainer requirement [1, 2]. Effectively this means that the
// container stores its elements contiguous in memory. Furthermore, it is
// expected that base::data(container) and base::size(container) are valid
// expected that std::data(container) and std::size(container) are valid
// expressions, and that data() + idx is dereferenceable for all idx in the
// range [0, size()). In the standard library this includes the containers
// std::string, std::vector and std::array, but other containers like
Expand Down Expand Up @@ -51,7 +50,7 @@ template <typename ContiguousContainer>
class CheckedContiguousRange {
public:
using element_type = std::remove_pointer_t<decltype(
base::data(std::declval<ContiguousContainer&>()))>;
std::data(std::declval<ContiguousContainer&>()))>;
using value_type = std::remove_cv_t<element_type>;
using reference = element_type&;
using const_reference = const element_type&;
Expand Down Expand Up @@ -132,17 +131,17 @@ class CheckedContiguousRange {
}

constexpr pointer data() const noexcept {
return container_ ? base::data(*container_) : nullptr;
return container_ ? std::data(*container_) : nullptr;
}

constexpr const_pointer cdata() const noexcept { return data(); }

constexpr size_type size() const noexcept {
return container_ ? base::size(*container_) : 0;
return container_ ? std::size(*container_) : 0;
}

constexpr bool empty() const noexcept {
return container_ ? base::empty(*container_) : true;
return container_ ? std::empty(*container_) : true;
}

private:
Expand Down
12 changes: 6 additions & 6 deletions base/containers/checked_range_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ TEST(CheckedContiguousRange, Constructor_String) {
TEST(CheckedContiguousRange, Constructor_Array) {
static constexpr int array[] = {1, 2, 3, 4, 5};
constexpr CheckedContiguousRange<const int[5]> range(array);
static_assert(data(array) == range.data(), "");
static_assert(size(array) == range.size(), "");
static_assert(std::data(array) == range.data(), "");
static_assert(std::size(array) == range.size(), "");
}

TEST(CheckedContiguousRange, Constructor_StdArray) {
static constexpr std::array<int, 5> array = {1, 2, 3, 4, 5};
constexpr CheckedContiguousRange<const std::array<int, 5>> range(array);
static_assert(data(array) == range.data(), "");
static_assert(base::size(array) == range.size(), "");
static_assert(std::size(array) == range.size(), "");
}

TEST(CheckedContiguousRange, Constructor_StringPiece) {
Expand All @@ -60,8 +60,8 @@ TEST(CheckedContiguousRange, Constructor_StringPiece) {
TEST(CheckedContiguousRange, Constructor_InitializerList) {
static constexpr std::initializer_list<int> il = {1, 2, 3, 4, 5};
constexpr CheckedContiguousRange<const std::initializer_list<int>> range(il);
static_assert(base::data(il) == range.data(), "");
static_assert(base::size(il) == range.size(), "");
static_assert(std::data(il) == range.data(), "");
static_assert(std::size(il) == range.size(), "");
}

TEST(CheckedContiguousRange, Constructor_Copy) {
Expand Down Expand Up @@ -162,7 +162,7 @@ TEST(CheckedContiguousRange, DataSizeEmpty_Constexpr) {
constexpr CheckedContiguousRange<const std::array<int, 0>> range(array);
static_assert(data(array) == range.data(), "");
static_assert(data(array) == range.cdata(), "");
static_assert(base::size(array) == range.size(), "");
static_assert(std::size(array) == range.size(), "");
static_assert(range.empty(), "");
}

Expand Down
7 changes: 3 additions & 4 deletions base/containers/flat_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "base/as_const.h"
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/cxx17_backports.h"
#include "base/functional/not_fn.h"
#include "base/ranges/algorithm.h"
#include "base/template_util.h"
Expand Down Expand Up @@ -75,14 +74,14 @@ constexpr std::array<U, N> ToArray(const T (&data)[N]) {
return ToArrayImpl<U>(data, std::make_index_sequence<N>());
}

// Helper that calls `container.reserve(base::size(source))`.
// Helper that calls `container.reserve(std::size(source))`.
template <typename T, typename U>
constexpr void ReserveIfSupported(const T&, const U&) {}

template <typename T, typename U>
auto ReserveIfSupported(T& container, const U& source)
-> decltype(container.reserve(base::size(source)), void()) {
container.reserve(base::size(source));
-> decltype(container.reserve(std::size(source)), void()) {
container.reserve(std::size(source));
}

// std::pair's operator= is not constexpr prior to C++20. Thus we need this
Expand Down
3 changes: 1 addition & 2 deletions base/containers/intrusive_heap_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "base/callback_helpers.h"
#include "base/check_op.h"
#include "base/cxx17_backports.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/rand_util.h"
Expand Down Expand Up @@ -69,7 +68,7 @@ void ExpectCanonical(const IntrusiveHeapInt& heap) {
void MakeCanonical(IntrusiveHeapInt* heap) {
static constexpr int kInts[] = {CANONICAL_ELEMENTS};
heap->clear();
heap->insert(kInts, kInts + base::size(kInts));
heap->insert(kInts, kInts + std::size(kInts));
ExpectCanonical(*heap);
}

Expand Down
Loading

0 comments on commit f45f476

Please sign in to comment.