Skip to content

Commit

Permalink
Replace ararysize macro with fml::size function (flutter#8975)
Browse files Browse the repository at this point in the history
This is forward compatible with std::size and similar to how Chromium
removed use of the arraysize macro.
  • Loading branch information
mdempsky authored and chinmaygarde committed May 15, 2019
1 parent ce22829 commit 3cdfa80
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 51 deletions.
2 changes: 1 addition & 1 deletion ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ FILE: ../../../flutter/flow/texture.h
FILE: ../../../flutter/flow/view_holder.cc
FILE: ../../../flutter/flow/view_holder.h
FILE: ../../../flutter/flutter_kernel_transformers/lib/track_widget_constructor_locations.dart
FILE: ../../../flutter/fml/arraysize.h
FILE: ../../../flutter/fml/base32.cc
FILE: ../../../flutter/fml/base32.h
FILE: ../../../flutter/fml/base32_unittest.cc
Expand Down Expand Up @@ -215,6 +214,7 @@ FILE: ../../../flutter/fml/platform/win/message_loop_win.h
FILE: ../../../flutter/fml/platform/win/native_library_win.cc
FILE: ../../../flutter/fml/platform/win/paths_win.cc
FILE: ../../../flutter/fml/platform/win/wstring_conversion.h
FILE: ../../../flutter/fml/size.h
FILE: ../../../flutter/fml/string_view.cc
FILE: ../../../flutter/fml/string_view.h
FILE: ../../../flutter/fml/string_view_unittest.cc
Expand Down
2 changes: 1 addition & 1 deletion fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import("//build/fuchsia/sdk.gni")

source_set("fml") {
sources = [
"arraysize.h",
"base32.cc",
"base32.h",
"build_config.h",
Expand Down Expand Up @@ -47,6 +46,7 @@ source_set("fml") {
"native_library.h",
"paths.cc",
"paths.h",
"size.h",
"string_view.cc",
"string_view.h",
"synchronization/atomic_object.h",
Expand Down
16 changes: 0 additions & 16 deletions fml/arraysize.h

This file was deleted.

6 changes: 3 additions & 3 deletions fml/command_line_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include <utility>

#include "flutter/fml/arraysize.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/size.h"
#include "gtest/gtest.h"

namespace fml {
Expand Down Expand Up @@ -197,7 +197,7 @@ TEST(CommandLineTest, CommmandLineFromIterators) {
{
static const char* const argv[] = {"my_program", "--flag=value", "arg"};

auto cl = CommandLineFromIterators(argv, argv + arraysize(argv));
auto cl = CommandLineFromIterators(argv, argv + fml::size(argv));
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv[0], cl.argv0());
std::vector<CommandLine::Option> expected_options = {
Expand All @@ -211,7 +211,7 @@ TEST(CommandLineTest, CommmandLineFromIterators) {

TEST(CommandLineTest, CommandLineFromArgcArgv) {
static const char* const argv[] = {"my_program", "--flag=value", "arg"};
const int argc = static_cast<int>(arraysize(argv));
const int argc = static_cast<int>(fml::size(argv));

auto cl = CommandLineFromArgcArgv(argc, argv);
EXPECT_TRUE(cl.has_argv0());
Expand Down
19 changes: 19 additions & 0 deletions fml/size.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Flutter 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 FLUTTER_FML_SIZE_H_
#define FLUTTER_FML_SIZE_H_

#include <cstddef>

namespace fml {

template <typename T, std::size_t N>
constexpr std::size_t size(T (&array)[N]) {
return N;
}

} // namespace fml

#endif // FLUTTER_FML_SIZE_H_
6 changes: 0 additions & 6 deletions fml/synchronization/waitable_event_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
#include "flutter/fml/macros.h"
#include "gtest/gtest.h"

#ifndef arraysize
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
#endif

namespace fml {
namespace {

Expand Down
26 changes: 13 additions & 13 deletions runtime/dart_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include <vector>

#include "flutter/common/settings.h"
#include "flutter/fml/arraysize.h"
#include "flutter/fml/compiler_specific.h"
#include "flutter/fml/file.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/size.h"
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/thread_annotations.h"
#include "flutter/fml/time/time_delta.h"
Expand Down Expand Up @@ -292,11 +292,11 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
args.push_back(profiler_flag);
}

PushBackAll(&args, kDartLanguageArgs, arraysize(kDartLanguageArgs));
PushBackAll(&args, kDartLanguageArgs, fml::size(kDartLanguageArgs));

if (IsRunningPrecompiledCode()) {
PushBackAll(&args, kDartPrecompilationArgs,
arraysize(kDartPrecompilationArgs));
fml::size(kDartPrecompilationArgs));
}

// Enable Dart assertions if we are not running precompiled code. We run non-
Expand All @@ -318,42 +318,42 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
// Debug mode uses the JIT, disable code page write protection to avoid
// memory page protection changes before and after every compilation.
PushBackAll(&args, kDartWriteProtectCodeArgs,
arraysize(kDartWriteProtectCodeArgs));
fml::size(kDartWriteProtectCodeArgs));
#endif

if (enable_asserts) {
PushBackAll(&args, kDartAssertArgs, arraysize(kDartAssertArgs));
PushBackAll(&args, kDartAssertArgs, fml::size(kDartAssertArgs));
}

if (settings_.start_paused) {
PushBackAll(&args, kDartStartPausedArgs, arraysize(kDartStartPausedArgs));
PushBackAll(&args, kDartStartPausedArgs, fml::size(kDartStartPausedArgs));
}

if (settings_.disable_service_auth_codes) {
PushBackAll(&args, kDartDisableServiceAuthCodesArgs,
arraysize(kDartDisableServiceAuthCodesArgs));
fml::size(kDartDisableServiceAuthCodesArgs));
}

if (settings_.endless_trace_buffer || settings_.trace_startup) {
// If we are tracing startup, make sure the trace buffer is endless so we
// don't lose early traces.
PushBackAll(&args, kDartEndlessTraceBufferArgs,
arraysize(kDartEndlessTraceBufferArgs));
fml::size(kDartEndlessTraceBufferArgs));
}

if (settings_.trace_systrace) {
PushBackAll(&args, kDartSystraceTraceBufferArgs,
arraysize(kDartSystraceTraceBufferArgs));
PushBackAll(&args, kDartTraceStreamsArgs, arraysize(kDartTraceStreamsArgs));
fml::size(kDartSystraceTraceBufferArgs));
PushBackAll(&args, kDartTraceStreamsArgs, fml::size(kDartTraceStreamsArgs));
}

if (settings_.trace_startup) {
PushBackAll(&args, kDartTraceStartupArgs, arraysize(kDartTraceStartupArgs));
PushBackAll(&args, kDartTraceStartupArgs, fml::size(kDartTraceStartupArgs));
}

#if defined(OS_FUCHSIA)
PushBackAll(&args, kDartFuchsiaTraceArgs, arraysize(kDartFuchsiaTraceArgs));
PushBackAll(&args, kDartTraceStreamsArgs, arraysize(kDartTraceStreamsArgs));
PushBackAll(&args, kDartFuchsiaTraceArgs, fml::size(kDartFuchsiaTraceArgs));
PushBackAll(&args, kDartTraceStreamsArgs, fml::size(kDartTraceStreamsArgs));
#endif

for (size_t i = 0; i < settings_.dart_flags.size(); i++)
Expand Down
2 changes: 1 addition & 1 deletion shell/gpu/gpu_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "gpu_surface_gl.h"

#include "flutter/fml/arraysize.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/size.h"
#include "flutter/fml/trace_event.h"
#include "flutter/shell/common/persistent_cache.h"
#include "third_party/skia/include/core/SkColorFilter.h"
Expand Down
4 changes: 2 additions & 2 deletions shell/platform/android/flutter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

#include <vector>

#include "flutter/fml/arraysize.h"
#include "flutter/fml/command_line.h"
#include "flutter/fml/file.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/paths.h"
#include "flutter/fml/platform/android/jni_util.h"
#include "flutter/fml/platform/android/paths_android.h"
#include "flutter/fml/size.h"
#include "flutter/lib/ui/plugins/callback_cache.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/runtime/start_up.h"
Expand Down Expand Up @@ -145,7 +145,7 @@ bool FlutterMain::Register(JNIEnv* env) {
return false;
}

return env->RegisterNatives(clazz, methods, arraysize(methods)) == 0;
return env->RegisterNatives(clazz, methods, fml::size(methods)) == 0;
}

} // namespace flutter
6 changes: 3 additions & 3 deletions shell/platform/android/platform_view_android_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/assets/zip_asset_store.h"
#include "flutter/common/settings.h"
#include "flutter/fml/arraysize.h"
#include "flutter/fml/file.h"
#include "flutter/fml/platform/android/jni_util.h"
#include "flutter/fml/platform/android/jni_weak_ref.h"
#include "flutter/fml/platform/android/scoped_java_ref.h"
#include "flutter/fml/size.h"
#include "flutter/lib/ui/plugins/callback_cache.h"
#include "flutter/runtime/dart_service_isolate.h"
#include "flutter/shell/common/run_configuration.h"
Expand Down Expand Up @@ -650,7 +650,7 @@ bool RegisterApi(JNIEnv* env) {
};

if (env->RegisterNatives(g_flutter_jni_class->obj(), flutter_jni_methods,
arraysize(flutter_jni_methods)) != 0) {
fml::size(flutter_jni_methods)) != 0) {
FML_LOG(ERROR) << "Failed to RegisterNatives with FlutterJNI";
return false;
}
Expand Down Expand Up @@ -755,7 +755,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {

if (env->RegisterNatives(g_flutter_callback_info_class->obj(),
callback_info_methods,
arraysize(callback_info_methods)) != 0) {
fml::size(callback_info_methods)) != 0) {
FML_LOG(ERROR) << "Failed to RegisterNatives with FlutterCallbackInfo";
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions shell/platform/android/vsync_waiter_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <utility>

#include "flutter/common/task_runners.h"
#include "flutter/fml/arraysize.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/platform/android/jni_util.h"
#include "flutter/fml/platform/android/scoped_java_ref.h"
#include "flutter/fml/size.h"
#include "flutter/fml/trace_event.h"

namespace flutter {
Expand Down Expand Up @@ -102,7 +102,7 @@ bool VsyncWaiterAndroid::Register(JNIEnv* env) {

FML_CHECK(g_async_wait_for_vsync_method_ != nullptr);

return env->RegisterNatives(clazz, methods, arraysize(methods)) == 0;
return env->RegisterNatives(clazz, methods, fml::size(methods)) == 0;
}

} // namespace flutter
6 changes: 3 additions & 3 deletions shell/platform/fuchsia/dart-pkg/fuchsia/sdk_ext/fuchsia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "dart-pkg/zircon/sdk_ext/handle.h"
#include "dart-pkg/zircon/sdk_ext/natives.h"
#include "dart-pkg/zircon/sdk_ext/system.h"
#include "flutter/fml/arraysize.h"
#include "flutter/fml/size.h"
#include "third_party/dart/runtime/include/dart_api.h"
#include "third_party/tonic/dart_binding_macros.h"
#include "third_party/tonic/dart_class_library.h"
Expand Down Expand Up @@ -64,7 +64,7 @@ Dart_NativeFunction NativeLookup(Dart_Handle name,
FML_DCHECK(function_name != nullptr);
FML_DCHECK(auto_setup_scope != nullptr);
*auto_setup_scope = true;
size_t num_entries = arraysize(Entries);
size_t num_entries = fml::size(Entries);
for (size_t i = 0; i < num_entries; ++i) {
const struct NativeEntries& entry = Entries[i];
if (!strcmp(function_name, entry.name) &&
Expand All @@ -78,7 +78,7 @@ Dart_NativeFunction NativeLookup(Dart_Handle name,
}

const uint8_t* NativeSymbol(Dart_NativeFunction native_function) {
size_t num_entries = arraysize(Entries);
size_t num_entries = fml::size(Entries);
for (size_t i = 0; i < num_entries; ++i) {
const struct NativeEntries& entry = Entries[i];
if (entry.function == native_function) {
Expand Down

0 comments on commit 3cdfa80

Please sign in to comment.