From d31e721e6ca0818b6f0913500ec68b7ea1143430 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Sat, 21 Apr 2018 20:50:03 -0700 Subject: [PATCH] Import intrusively ref counted shared pointers into FML. (flutter/engine#5062) --- engine/src/flutter/fml/BUILD.gn | 8 + engine/src/flutter/fml/build_config.h | 110 ++++ engine/src/flutter/fml/compiler_specific.h | 79 +++ engine/src/flutter/fml/eintr_wrapper.h | 60 ++ engine/src/flutter/fml/file.h | 2 +- engine/src/flutter/fml/icu_util.cc | 4 +- engine/src/flutter/fml/icu_util.h | 2 +- engine/src/flutter/fml/macros.h | 25 +- engine/src/flutter/fml/mapping.h | 10 +- engine/src/flutter/fml/memory/ref_counted.h | 136 ++++ .../flutter/fml/memory/ref_counted_internal.h | 107 +++ .../fml/memory/ref_counted_unittest.cc | 607 ++++++++++++++++++ engine/src/flutter/fml/memory/ref_ptr.h | 250 ++++++++ .../src/flutter/fml/memory/ref_ptr_internal.h | 38 ++ .../src/flutter/fml/memory/thread_checker.h | 4 +- engine/src/flutter/fml/memory/weak_ptr.h | 12 +- .../flutter/fml/memory/weak_ptr_internal.h | 8 +- engine/src/flutter/fml/message_loop.h | 3 +- engine/src/flutter/fml/message_loop_impl.cc | 2 +- engine/src/flutter/fml/message_loop_impl.h | 4 +- engine/src/flutter/fml/native_library.h | 4 +- engine/src/flutter/fml/paths.cc | 2 +- .../flutter/fml/platform/android/jni_util.h | 2 +- .../platform/android/message_loop_android.cc | 1 - .../platform/android/message_loop_android.h | 4 +- .../fml/platform/android/scoped_java_ref.h | 8 +- .../flutter/fml/platform/darwin/cf_utils.h | 4 +- .../fml/platform/darwin/message_loop_darwin.h | 4 +- .../fml/platform/darwin/platform_version.h | 2 +- .../platform/darwin/resource_mapping_darwin.h | 4 +- .../fml/platform/darwin/scoped_block.h | 4 +- .../fml/platform/darwin/scoped_nsobject.h | 8 +- .../fml/platform/linux/message_loop_linux.cc | 6 +- .../fml/platform/linux/message_loop_linux.h | 4 +- .../src/flutter/fml/platform/linux/timerfd.cc | 4 +- .../flutter/fml/platform/posix/file_posix.cc | 6 +- .../fml/platform/posix/mapping_posix.cc | 9 +- .../fml/platform/win/message_loop_win.h | 4 +- engine/src/flutter/fml/task_runner.h | 4 +- engine/src/flutter/fml/thread.cc | 2 +- engine/src/flutter/fml/thread.h | 4 +- engine/src/flutter/fml/thread_local.h | 10 +- engine/src/flutter/fml/trace_event.h | 4 +- engine/src/flutter/fml/unique_fd.cc | 4 +- engine/src/flutter/fml/unique_fd.h | 2 +- engine/src/flutter/fml/unique_object.h | 8 +- 46 files changed, 1503 insertions(+), 86 deletions(-) create mode 100644 engine/src/flutter/fml/build_config.h create mode 100644 engine/src/flutter/fml/compiler_specific.h create mode 100644 engine/src/flutter/fml/eintr_wrapper.h create mode 100644 engine/src/flutter/fml/memory/ref_counted.h create mode 100644 engine/src/flutter/fml/memory/ref_counted_internal.h create mode 100644 engine/src/flutter/fml/memory/ref_counted_unittest.cc create mode 100644 engine/src/flutter/fml/memory/ref_ptr.h create mode 100644 engine/src/flutter/fml/memory/ref_ptr_internal.h diff --git a/engine/src/flutter/fml/BUILD.gn b/engine/src/flutter/fml/BUILD.gn index ffbf949cc0..5295af35ae 100644 --- a/engine/src/flutter/fml/BUILD.gn +++ b/engine/src/flutter/fml/BUILD.gn @@ -4,6 +4,9 @@ source_set("fml") { sources = [ + "build_config.h", + "compiler_specific.h", + "eintr_wrapper.h", "file.h", "icu_util.cc", "icu_util.h", @@ -13,6 +16,10 @@ source_set("fml") { "memory/weak_ptr.h", "memory/weak_ptr_internal.cc", "memory/weak_ptr_internal.h", + "memory/ref_counted.h", + "memory/ref_counted_internal.h", + "memory/ref_ptr.h", + "memory/ref_ptr_internal.h", "message_loop.cc", "message_loop.h", "message_loop_impl.cc", @@ -130,6 +137,7 @@ executable("fml_unittests") { sources = [ "memory/weak_ptr_unittest.cc", + "memory/ref_counted_unittest.cc", "message_loop_unittests.cc", "thread_local_unittests.cc", "thread_unittests.cc", diff --git a/engine/src/flutter/fml/build_config.h b/engine/src/flutter/fml/build_config.h new file mode 100644 index 0000000000..ca9a91a382 --- /dev/null +++ b/engine/src/flutter/fml/build_config.h @@ -0,0 +1,110 @@ +// Copyright 2016 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file adds defines about the platform we're currently building on. +// Operating System: +// OS_WIN / OS_MACOSX / OS_LINUX / OS_POSIX (MACOSX or LINUX) / +// OS_NACL (NACL_SFI or NACL_NONSFI) / OS_NACL_SFI / OS_NACL_NONSFI +// Compiler: +// COMPILER_MSVC / COMPILER_GCC +// Processor: +// ARCH_CPU_X86 / ARCH_CPU_X86_64 / ARCH_CPU_X86_FAMILY (X86 or X86_64) +// ARCH_CPU_32_BITS / ARCH_CPU_64_BITS + +#ifndef FLUTTER_FML_BUILD_CONFIG_H_ +#define FLUTTER_FML_BUILD_CONFIG_H_ + +#if defined(__Fuchsia__) +#define OS_FUCHSIA 1 +#elif defined(ANDROID) +#define OS_ANDROID 1 +#elif defined(__APPLE__) +// only include TargetConditions after testing ANDROID as some android builds +// on mac don't have this header available and it's not needed unless the target +// is really mac/ios. +#include +#define OS_MACOSX 1 +#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE +#define OS_IOS 1 +#endif // defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE +#elif defined(__linux__) +#define OS_LINUX 1 +// include a system header to pull in features.h for glibc/uclibc macros. +#include +#if defined(__GLIBC__) && !defined(__UCLIBC__) +// we really are using glibc, not uClibc pretending to be glibc +#define LIBC_GLIBC 1 +#endif +#elif defined(_WIN32) +#define OS_WIN 1 +#elif defined(__FreeBSD__) +#define OS_FREEBSD 1 +#elif defined(__OpenBSD__) +#define OS_OPENBSD 1 +#elif defined(__sun) +#define OS_SOLARIS 1 +#elif defined(__QNXNTO__) +#define OS_QNX 1 +#else +#error Please add support for your platform in flutter/fml/build_config.h +#endif + +// For access to standard BSD features, use OS_BSD instead of a +// more specific macro. +#if defined(OS_FREEBSD) || defined(OS_OPENBSD) +#define OS_BSD 1 +#endif + +// For access to standard POSIXish features, use OS_POSIX instead of a +// more specific macro. +#if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_FREEBSD) || \ + defined(OS_OPENBSD) || defined(OS_SOLARIS) || defined(OS_ANDROID) || \ + defined(OS_NACL) || defined(OS_QNX) +#define OS_POSIX 1 +#endif + +// Processor architecture detection. For more info on what's defined, see: +// http://msdn.microsoft.com/en-us/library/b0084kay.aspx +// http://www.agner.org/optimize/calling_conventions.pdf +// or with gcc, run: "echo | gcc -E -dM -" +#if defined(_M_X64) || defined(__x86_64__) +#define ARCH_CPU_X86_FAMILY 1 +#define ARCH_CPU_X86_64 1 +#define ARCH_CPU_64_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#elif defined(_M_IX86) || defined(__i386__) +#define ARCH_CPU_X86_FAMILY 1 +#define ARCH_CPU_X86 1 +#define ARCH_CPU_32_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#elif defined(__ARMEL__) +#define ARCH_CPU_ARM_FAMILY 1 +#define ARCH_CPU_ARMEL 1 +#define ARCH_CPU_32_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#elif defined(__aarch64__) +#define ARCH_CPU_ARM_FAMILY 1 +#define ARCH_CPU_ARM64 1 +#define ARCH_CPU_64_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#elif defined(__pnacl__) +#define ARCH_CPU_32_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#elif defined(__MIPSEL__) +#if defined(__LP64__) +#define ARCH_CPU_MIPS64_FAMILY 1 +#define ARCH_CPU_MIPS64EL 1 +#define ARCH_CPU_64_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#else +#define ARCH_CPU_MIPS_FAMILY 1 +#define ARCH_CPU_MIPSEL 1 +#define ARCH_CPU_32_BITS 1 +#define ARCH_CPU_LITTLE_ENDIAN 1 +#endif +#else +#error Please add support for your architecture in flutter/fml/build_config.h +#endif + +#endif // FLUTTER_FML_BUILD_CONFIG_H_ diff --git a/engine/src/flutter/fml/compiler_specific.h b/engine/src/flutter/fml/compiler_specific.h new file mode 100644 index 0000000000..68fbefe1b9 --- /dev/null +++ b/engine/src/flutter/fml/compiler_specific.h @@ -0,0 +1,79 @@ +// Copyright 2016 The Fuchsia 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_COMPILER_SPECIFIC_H_ +#define FLUTTER_FML_COMPILER_SPECIFIC_H_ + +#if !defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER) +#error Unsupported compiler. +#endif + +// Annotate a variable indicating it's ok if the variable is not used. +// (Typically used to silence a compiler warning when the assignment +// is important for some other reason.) +// Use like: +// int x = ...; +// FML_ALLOW_UNUSED_LOCAL(x); +#define FML_ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0 + +// Annotate a typedef or function indicating it's ok if it's not used. +// Use like: +// typedef Foo Bar ALLOW_UNUSED_TYPE; +#if defined(__GNUC__) || defined(__clang__) +#define FML_ALLOW_UNUSED_TYPE __attribute__((unused)) +#else +#define FML_ALLOW_UNUSED_TYPE +#endif + +// Annotate a function indicating it should not be inlined. +// Use like: +// NOINLINE void DoStuff() { ... } +#if defined(__GNUC__) || defined(__clang__) +#define FML_NOINLINE __attribute__((noinline)) +#elif defined(_MSC_VER) +#define FML_NOINLINE __declspec(noinline) +#endif + +// Specify memory alignment for structs, classes, etc. +// Use like: +// class FML_ALIGNAS(16) MyClass { ... } +// FML_ALIGNAS(16) int array[4]; +#if defined(__GNUC__) || defined(__clang__) +#define FML_ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) +#elif defined(_MSC_VER) +#define FML_ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) +#endif + +// Return the byte alignment of the given type (available at compile time). +// Use like: +// FML_ALIGNOF(int32) // this would be 4 +#if defined(__GNUC__) || defined(__clang__) +#define FML_ALIGNOF(type) __alignof__(type) +#elif defined(_MSC_VER) +#define FML_ALIGNOF(type) __alignof(type) +#endif + +// Annotate a function indicating the caller must examine the return value. +// Use like: +// int foo() FML_WARN_UNUSED_RESULT; +// To explicitly ignore a result, see |ignore_result()| in base/macros.h. +#if defined(__GNUC__) || defined(__clang__) +#define FML_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#else +#define FML_WARN_UNUSED_RESULT +#endif + +// Tell the compiler a function is using a printf-style format string. +// |format_param| is the one-based index of the format string parameter; +// |dots_param| is the one-based index of the "..." parameter. +// For v*printf functions (which take a va_list), pass 0 for dots_param. +// (This is undocumented but matches what the system C headers do.) +#if defined(__GNUC__) || defined(__clang__) +#define FML_PRINTF_FORMAT(format_param, dots_param) \ + __attribute__((format(printf, format_param, dots_param))) +#else +#define FML_PRINTF_FORMAT(format_param, dots_param) +#endif + +#endif // FLUTTER_FML_COMPILER_SPECIFIC_H_ diff --git a/engine/src/flutter/fml/eintr_wrapper.h b/engine/src/flutter/fml/eintr_wrapper.h new file mode 100644 index 0000000000..66c8ae0cd0 --- /dev/null +++ b/engine/src/flutter/fml/eintr_wrapper.h @@ -0,0 +1,60 @@ +// Copyright 2016 The Fuchsia 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_EINTR_WRAPPER_H_ +#define FLUTTER_FML_EINTR_WRAPPER_H_ + +#include "flutter/fml/build_config.h" + +#include + +#if defined(OS_WIN) + +// Windows has no concept of EINTR. +#define FML_HANDLE_EINTR(x) (x) +#define FML_IGNORE_EINTR(x) (x) + +#else + +#if defined(NDEBUG) + +#define FML_HANDLE_EINTR(x) \ + ({ \ + decltype(x) eintr_wrapper_result; \ + do { \ + eintr_wrapper_result = (x); \ + } while (eintr_wrapper_result == -1 && errno == EINTR); \ + eintr_wrapper_result; \ + }) + +#else + +#define FML_HANDLE_EINTR(x) \ + ({ \ + int eintr_wrapper_counter = 0; \ + decltype(x) eintr_wrapper_result; \ + do { \ + eintr_wrapper_result = (x); \ + } while (eintr_wrapper_result == -1 && errno == EINTR && \ + eintr_wrapper_counter++ < 100); \ + eintr_wrapper_result; \ + }) + +#endif // NDEBUG + +#define FML_IGNORE_EINTR(x) \ + ({ \ + decltype(x) eintr_wrapper_result; \ + do { \ + eintr_wrapper_result = (x); \ + if (eintr_wrapper_result == -1 && errno == EINTR) { \ + eintr_wrapper_result = 0; \ + } \ + } while (0); \ + eintr_wrapper_result; \ + }) + +#endif // defined(OS_WIN) + +#endif // FLUTTER_FML_EINTR_WRAPPER_H_ diff --git a/engine/src/flutter/fml/file.h b/engine/src/flutter/fml/file.h index 2327b2eef8..53da3b8ca7 100644 --- a/engine/src/flutter/fml/file.h +++ b/engine/src/flutter/fml/file.h @@ -5,8 +5,8 @@ #ifndef FLUTTER_FML_FILE_H_ #define FLUTTER_FML_FILE_H_ +#include "flutter/fml/macros.h" #include "flutter/fml/unique_fd.h" -#include "lib/fxl/macros.h" namespace fml { diff --git a/engine/src/flutter/fml/icu_util.cc b/engine/src/flutter/fml/icu_util.cc index 0c0acd06db..88ae64034a 100644 --- a/engine/src/flutter/fml/icu_util.cc +++ b/engine/src/flutter/fml/icu_util.cc @@ -7,9 +7,9 @@ #include #include +#include "flutter/fml/build_config.h" #include "flutter/fml/mapping.h" #include "flutter/fml/paths.h" -#include "lib/fxl/build_config.h" #include "lib/fxl/logging.h" #include "third_party/icu/source/common/unicode/udata.h" @@ -87,7 +87,7 @@ class ICUContext { bool valid_; std::unique_ptr mapping_; - FXL_DISALLOW_COPY_AND_ASSIGN(ICUContext); + FML_DISALLOW_COPY_AND_ASSIGN(ICUContext); }; void InitializeICUOnce(const std::string& icu_data_path) { diff --git a/engine/src/flutter/fml/icu_util.h b/engine/src/flutter/fml/icu_util.h index 2d31e15113..1d20945ee0 100644 --- a/engine/src/flutter/fml/icu_util.h +++ b/engine/src/flutter/fml/icu_util.h @@ -7,7 +7,7 @@ #include -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" namespace fml { namespace icu { diff --git a/engine/src/flutter/fml/macros.h b/engine/src/flutter/fml/macros.h index ba46b9dda4..0fb8079ef7 100644 --- a/engine/src/flutter/fml/macros.h +++ b/engine/src/flutter/fml/macros.h @@ -5,7 +5,7 @@ #ifndef FLUTTER_FML_MACROS_H_ #define FLUTTER_FML_MACROS_H_ -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" #ifndef FML_USED_ON_EMBEDDER @@ -17,4 +17,27 @@ #endif // FML_USED_ON_EMBEDDER +#define FML_DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete + +#define FML_DISALLOW_ASSIGN(TypeName) \ + TypeName& operator=(const TypeName&) = delete + +#define FML_DISALLOW_MOVE(TypeName) \ + TypeName(TypeName&&) = delete; \ + TypeName& operator=(TypeName&&) = delete + +#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&) = delete; \ + TypeName& operator=(const TypeName&) = delete + +#define FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName) \ + TypeName(const TypeName&) = delete; \ + TypeName(TypeName&&) = delete; \ + TypeName& operator=(const TypeName&) = delete; \ + TypeName& operator=(TypeName&&) = delete + +#define FML_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ + TypeName() = delete; \ + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName) + #endif // FLUTTER_FML_MACROS_H_ diff --git a/engine/src/flutter/fml/mapping.h b/engine/src/flutter/fml/mapping.h index 00e9685dd6..1f10975906 100644 --- a/engine/src/flutter/fml/mapping.h +++ b/engine/src/flutter/fml/mapping.h @@ -10,8 +10,8 @@ #include #include "flutter/fml//unique_fd.h" -#include "lib/fxl/build_config.h" -#include "lib/fxl/macros.h" +#include "flutter/fml/build_config.h" +#include "flutter/fml/macros.h" namespace fml { @@ -26,7 +26,7 @@ class Mapping { virtual const uint8_t* GetMapping() const = 0; private: - FXL_DISALLOW_COPY_AND_ASSIGN(Mapping); + FML_DISALLOW_COPY_AND_ASSIGN(Mapping); }; bool PlatformHasResourcesBundle(); @@ -53,7 +53,7 @@ class FileMapping : public Mapping { fml::UniqueFD mapping_handle_; #endif - FXL_DISALLOW_COPY_AND_ASSIGN(FileMapping); + FML_DISALLOW_COPY_AND_ASSIGN(FileMapping); }; class DataMapping : public Mapping { @@ -69,7 +69,7 @@ class DataMapping : public Mapping { private: std::vector data_; - FXL_DISALLOW_COPY_AND_ASSIGN(DataMapping); + FML_DISALLOW_COPY_AND_ASSIGN(DataMapping); }; } // namespace fml diff --git a/engine/src/flutter/fml/memory/ref_counted.h b/engine/src/flutter/fml/memory/ref_counted.h new file mode 100644 index 0000000000..4dc6fc5ee1 --- /dev/null +++ b/engine/src/flutter/fml/memory/ref_counted.h @@ -0,0 +1,136 @@ +// Copyright 2016 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Provides a base class for reference-counted classes. + +#ifndef FLUTTER_FML_MEMORY_REF_COUNTED_H_ +#define FLUTTER_FML_MEMORY_REF_COUNTED_H_ + +#include "flutter/fml/macros.h" +#include "flutter/fml/memory/ref_counted_internal.h" +#include "flutter/fml/memory/ref_ptr.h" + +namespace fml { + +// A base class for (thread-safe) reference-counted classes. Use like: +// +// class Foo : public RefCountedThreadSafe { +// ... +// }; +// +// |~Foo()| *may* be made private (e.g., to avoid accidental deletion of objects +// while there are still references to them), |Foo| should friend +// |RefCountedThreadSafe|; use |FML_FRIEND_REF_COUNTED_THREAD_SAFE()| for +// this: +// +// class Foo : public RefCountedThreadSafe { +// ... +// private: +// FML_FRIEND_REF_COUNTED_THREAD_SAFE(Foo); +// ~Foo(); +// ... +// }; +// +// Similarly, |Foo(...)| may be made private. In this case, there should either +// be a static factory method performing the requisite adoption: +// +// class Foo : public RefCountedThreadSafe { +// ... +// public: +// inline static RefPtr Create() { return AdoptRef(new Foo()); } +// ... +// private: +// Foo(); +// ... +// }; +// +// Or, to allow |MakeRefCounted()| to be used, use +// |FML_FRIEND_MAKE_REF_COUNTED()|: +// +// class Foo : public RefCountedThreadSafe { +// ... +// private: +// FML_FRIEND_MAKE_REF_COUNTED(Foo); +// Foo(); +// Foo(const Bar& bar, bool maybe); +// ... +// }; +// +// For now, we only have thread-safe reference counting, since that's all we +// need. It's easy enough to add thread-unsafe versions if necessary. +template +class RefCountedThreadSafe : public internal::RefCountedThreadSafeBase { + public: + // Adds a reference to this object. + // Inherited from the internal superclass: + // void AddRef() const; + + // Releases a reference to this object. This will destroy this object once the + // last reference is released. + void Release() const { + if (internal::RefCountedThreadSafeBase::Release()) + delete static_cast(this); + } + + // Returns true if there is exactly one reference to this object. Use of this + // is subtle, and should usually be avoided. To assert that there is only one + // reference (typically held by the calling thread, possibly in a local + // variable), use |AssertHasOneRef()| instead. However, a use is: + // + // if (foo->HasOneRef()) { + // // Do something "fast", since |foo| is the only reference and no other + // // thread can get another reference. + // ... + // } else { + // // Do something slower, but still valid even if there were only one + // // reference. + // ... + // } + // + // Inherited from the internal superclass: + // bool HasOneRef(); + + // Asserts that there is exactly one reference to this object; does nothing in + // Release builds (when |NDEBUG| is defined). + // Inherited from the internal superclass: + // void AssertHasOneRef(); + + protected: + // Constructor. Note that the object is constructed with a reference count of + // 1, and then must be adopted (see |AdoptRef()| in ref_ptr.h). + RefCountedThreadSafe() {} + + // Destructor. Note that this object should only be destroyed via |Release()| + // (see above), or something that calls |Release()| (see, e.g., |RefPtr<>| in + // ref_ptr.h). + ~RefCountedThreadSafe() {} + + private: +#ifndef NDEBUG + template + friend RefPtr AdoptRef(U*); + // Marks the initial reference (assumed on construction) as adopted. This is + // only required for Debug builds (when |NDEBUG| is not defined). + // TODO(vtl): Should this really be private? This makes manual ref-counting + // and also writing one's own ref pointer class impossible. + void Adopt() { internal::RefCountedThreadSafeBase::Adopt(); } +#endif + + FML_DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); +}; + +// If you subclass |RefCountedThreadSafe| and want to keep your destructor +// private, use this. (See the example above |RefCountedThreadSafe|.) +#define FML_FRIEND_REF_COUNTED_THREAD_SAFE(T) \ + friend class ::fml::RefCountedThreadSafe + +// If you want to keep your constructor(s) private and still want to use +// |MakeRefCounted()|, use this. (See the example above +// |RefCountedThreadSafe|.) +#define FML_FRIEND_MAKE_REF_COUNTED(T) \ + friend class ::fml::internal::MakeRefCountedHelper + +} // namespace fml + +#endif // FLUTTER_FML_MEMORY_REF_COUNTED_H_ diff --git a/engine/src/flutter/fml/memory/ref_counted_internal.h b/engine/src/flutter/fml/memory/ref_counted_internal.h new file mode 100644 index 0000000000..bbe037d47c --- /dev/null +++ b/engine/src/flutter/fml/memory/ref_counted_internal.h @@ -0,0 +1,107 @@ +// Copyright 2016 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Internal implementation details for ref_counted.h. + +#ifndef FLUTTER_FML_MEMORY_REF_COUNTED_INTERNAL_H_ +#define FLUTTER_FML_MEMORY_REF_COUNTED_INTERNAL_H_ + +#include + +#include "flutter/fml/macros.h" +#include "lib/fxl/logging.h" + +namespace fml { +namespace internal { + +// See ref_counted.h for comments on the public methods. +class RefCountedThreadSafeBase { + public: + void AddRef() const { +#ifndef NDEBUG + FXL_DCHECK(!adoption_required_); + FXL_DCHECK(!destruction_started_); +#endif + ref_count_.fetch_add(1u, std::memory_order_relaxed); + } + + bool HasOneRef() const { + return ref_count_.load(std::memory_order_acquire) == 1u; + } + + void AssertHasOneRef() const { FXL_DCHECK(HasOneRef()); } + + protected: + RefCountedThreadSafeBase(); + ~RefCountedThreadSafeBase(); + + // Returns true if the object should self-delete. + bool Release() const { +#ifndef NDEBUG + FXL_DCHECK(!adoption_required_); + FXL_DCHECK(!destruction_started_); +#endif + FXL_DCHECK(ref_count_.load(std::memory_order_acquire) != 0u); + // TODO(vtl): We could add the following: + // if (ref_count_.load(std::memory_order_relaxed) == 1u) { + // #ifndef NDEBUG + // destruction_started_= true; + // #endif + // return true; + // } + // This would be correct. On ARM (an Nexus 4), in *single-threaded* tests, + // this seems to make the destruction case marginally faster (barely + // measurable), and while the non-destruction case remains about the same + // (possibly marginally slower, but my measurements aren't good enough to + // have any confidence in that). I should try multithreaded/multicore tests. + if (ref_count_.fetch_sub(1u, std::memory_order_release) == 1u) { + std::atomic_thread_fence(std::memory_order_acquire); +#ifndef NDEBUG + destruction_started_ = true; +#endif + return true; + } + return false; + } + +#ifndef NDEBUG + void Adopt() { + FXL_DCHECK(adoption_required_); + adoption_required_ = false; + } +#endif + + private: + mutable std::atomic_uint_fast32_t ref_count_; + +#ifndef NDEBUG + mutable bool adoption_required_; + mutable bool destruction_started_; +#endif + + FML_DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); +}; + +inline RefCountedThreadSafeBase::RefCountedThreadSafeBase() + : ref_count_(1u) +#ifndef NDEBUG + , + adoption_required_(true), + destruction_started_(false) +#endif +{ +} + +inline RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { +#ifndef NDEBUG + FXL_DCHECK(!adoption_required_); + // Should only be destroyed as a result of |Release()|. + FXL_DCHECK(destruction_started_); +#endif +} + +} // namespace internal +} // namespace fml + +#endif // FLUTTER_FML_MEMORY_REF_COUNTED_INTERNAL_H_ diff --git a/engine/src/flutter/fml/memory/ref_counted_unittest.cc b/engine/src/flutter/fml/memory/ref_counted_unittest.cc new file mode 100644 index 0000000000..40b7bd095d --- /dev/null +++ b/engine/src/flutter/fml/memory/ref_counted_unittest.cc @@ -0,0 +1,607 @@ +// Copyright 2016 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file tests both ref_counted.h and ref_ptr.h (which the former includes). +// TODO(vtl): Possibly we could separate these tests out better, since a lot of +// it is actually testing |RefPtr|. + +#include "flutter/fml/memory/ref_counted.h" + +#include "flutter/fml/macros.h" +#include "gtest/gtest.h" + +#if defined(__clang__) +#define ALLOW_PESSIMIZING_MOVE(code_line) \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wpessimizing-move\"") code_line; \ + _Pragma("clang diagnostic pop") +#else +#define ALLOW_PESSIMIZING_MOVE(code_line) code_line; +#endif + +#if defined(__clang__) +#define ALLOW_SELF_MOVE(code_line) \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wself-move\"") code_line; \ + _Pragma("clang diagnostic pop") +#else +#define ALLOW_SELF_MOVE(code_line) code_line; +#endif + +namespace fml { +namespace { + +class MyClass : public RefCountedThreadSafe { + protected: + MyClass(MyClass** created, bool* was_destroyed) + : was_destroyed_(was_destroyed) { + if (created) + *created = this; + } + virtual ~MyClass() { + if (was_destroyed_) + *was_destroyed_ = true; + } + + private: + FML_FRIEND_REF_COUNTED_THREAD_SAFE(MyClass); + FML_FRIEND_MAKE_REF_COUNTED(MyClass); + + bool* was_destroyed_; + + FML_DISALLOW_COPY_AND_ASSIGN(MyClass); +}; + +class MySubclass final : public MyClass { + private: + FML_FRIEND_REF_COUNTED_THREAD_SAFE(MySubclass); + FML_FRIEND_MAKE_REF_COUNTED(MySubclass); + + MySubclass(MySubclass** created, bool* was_destroyed) + : MyClass(nullptr, was_destroyed) { + if (created) + *created = this; + } + ~MySubclass() override {} + + FML_DISALLOW_COPY_AND_ASSIGN(MySubclass); +}; + +TEST(RefCountedTest, Constructors) { + bool was_destroyed; + + { + // Default. + RefPtr r; + EXPECT_TRUE(r.get() == nullptr); + EXPECT_FALSE(r); + } + + { + // Nullptr. + RefPtr r(nullptr); + EXPECT_TRUE(r.get() == nullptr); + EXPECT_FALSE(r); + } + + { + MyClass* created = nullptr; + was_destroyed = false; + // Adopt, then RVO. + RefPtr r(MakeRefCounted(&created, &was_destroyed)); + EXPECT_TRUE(created); + EXPECT_EQ(created, r.get()); + EXPECT_TRUE(r); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MyClass* created = nullptr; + was_destroyed = false; + // Adopt, then move. + ALLOW_PESSIMIZING_MOVE(RefPtr r( + std::move(MakeRefCounted(&created, &was_destroyed)))) + EXPECT_TRUE(created); + EXPECT_EQ(created, r.get()); + EXPECT_TRUE(r); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MyClass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + // Copy. + RefPtr r2(r1); + EXPECT_TRUE(created); + EXPECT_EQ(created, r1.get()); + EXPECT_EQ(created, r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MyClass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + // From raw pointer. + RefPtr r2(created); + EXPECT_TRUE(created); + EXPECT_EQ(created, r1.get()); + EXPECT_EQ(created, r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MySubclass* created = nullptr; + was_destroyed = false; + // Adopt, then "move". + RefPtr r(MakeRefCounted(&created, &was_destroyed)); + EXPECT_TRUE(created); + EXPECT_EQ(static_cast(created), r.get()); + EXPECT_TRUE(r); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MySubclass* created = nullptr; + was_destroyed = false; + // Adopt, then "move". + ALLOW_PESSIMIZING_MOVE(RefPtr r( + std::move(MakeRefCounted(&created, &was_destroyed)))) + EXPECT_TRUE(created); + EXPECT_EQ(static_cast(created), r.get()); + EXPECT_TRUE(r); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MySubclass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + // "Copy". + RefPtr r2(r1); + EXPECT_TRUE(created); + EXPECT_EQ(static_cast(created), r1.get()); + EXPECT_EQ(static_cast(created), r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MySubclass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + // From raw pointer. + RefPtr r2(created); + EXPECT_TRUE(created); + EXPECT_EQ(static_cast(created), r1.get()); + EXPECT_EQ(static_cast(created), r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); +} + +TEST(RefCountedTest, NullAssignmentToNull) { + RefPtr r1; + // No-op null assignment using |nullptr|. + r1 = nullptr; + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_FALSE(r1); + + RefPtr r2; + // No-op null assignment using copy constructor. + r1 = r2; + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r2.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r2); + + // No-op null assignment using move constructor. + r1 = std::move(r2); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r2.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r2); + + RefPtr r3; + // No-op null assignment using "copy" constructor. + r1 = r3; + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r3.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r3); + + // No-op null assignment using "move" constructor. + r1 = std::move(r3); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r3.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r3); +} + +TEST(RefCountedTest, NonNullAssignmentToNull) { + bool was_destroyed; + + { + MyClass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + RefPtr r2; + // Copy assignment (to null ref pointer). + r2 = r1; + EXPECT_EQ(created, r1.get()); + EXPECT_EQ(created, r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MyClass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + RefPtr r2; + // Move assignment (to null ref pointer). + r2 = std::move(r1); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_EQ(created, r2.get()); + EXPECT_FALSE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MySubclass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + RefPtr r2; + // "Copy" assignment (to null ref pointer). + r2 = r1; + EXPECT_EQ(created, r1.get()); + EXPECT_EQ(static_cast(created), r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MySubclass* created = nullptr; + was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + RefPtr r2; + // "Move" assignment (to null ref pointer). + r2 = std::move(r1); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_EQ(static_cast(created), r2.get()); + EXPECT_FALSE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); +} + +TEST(RefCountedTest, NullAssignmentToNonNull) { + bool was_destroyed = false; + RefPtr r1(MakeRefCounted(nullptr, &was_destroyed)); + // Null assignment (to non-null ref pointer) using |nullptr|. + r1 = nullptr; + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_TRUE(was_destroyed); + + was_destroyed = false; + r1 = MakeRefCounted(nullptr, &was_destroyed); + RefPtr r2; + // Null assignment (to non-null ref pointer) using copy constructor. + r1 = r2; + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r2.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r2); + EXPECT_TRUE(was_destroyed); + + was_destroyed = false; + r1 = MakeRefCounted(nullptr, &was_destroyed); + // Null assignment using move constructor. + r1 = std::move(r2); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r2.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r2); + EXPECT_TRUE(was_destroyed); + + was_destroyed = false; + r1 = MakeRefCounted(nullptr, &was_destroyed); + RefPtr r3; + // Null assignment (to non-null ref pointer) using "copy" constructor. + r1 = r3; + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r3.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r3); + EXPECT_TRUE(was_destroyed); + + was_destroyed = false; + r1 = MakeRefCounted(nullptr, &was_destroyed); + // Null assignment (to non-null ref pointer) using "move" constructor. + r1 = std::move(r3); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_TRUE(r3.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_FALSE(r3); + EXPECT_TRUE(was_destroyed); +} + +TEST(RefCountedTest, NonNullAssignmentToNonNull) { + bool was_destroyed1; + bool was_destroyed2; + + { + was_destroyed1 = false; + was_destroyed2 = false; + RefPtr r1(MakeRefCounted(nullptr, &was_destroyed1)); + RefPtr r2(MakeRefCounted(nullptr, &was_destroyed2)); + // Copy assignment (to non-null ref pointer). + r2 = r1; + EXPECT_EQ(r1.get(), r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed1); + EXPECT_TRUE(was_destroyed2); + } + EXPECT_TRUE(was_destroyed1); + + { + was_destroyed1 = false; + was_destroyed2 = false; + RefPtr r1(MakeRefCounted(nullptr, &was_destroyed1)); + RefPtr r2(MakeRefCounted(nullptr, &was_destroyed2)); + // Move assignment (to non-null ref pointer). + r2 = std::move(r1); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_FALSE(r2.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed1); + EXPECT_TRUE(was_destroyed2); + } + EXPECT_TRUE(was_destroyed1); + + { + was_destroyed1 = false; + was_destroyed2 = false; + RefPtr r1(MakeRefCounted(nullptr, &was_destroyed1)); + RefPtr r2(MakeRefCounted(nullptr, &was_destroyed2)); + // "Copy" assignment (to non-null ref pointer). + r2 = r1; + EXPECT_EQ(r1.get(), r2.get()); + EXPECT_TRUE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed1); + EXPECT_TRUE(was_destroyed2); + } + EXPECT_TRUE(was_destroyed1); + + { + was_destroyed1 = false; + was_destroyed2 = false; + RefPtr r1(MakeRefCounted(nullptr, &was_destroyed1)); + RefPtr r2(MakeRefCounted(nullptr, &was_destroyed2)); + // Move assignment (to non-null ref pointer). + r2 = std::move(r1); + EXPECT_TRUE(r1.get() == nullptr); + EXPECT_FALSE(r2.get() == nullptr); + EXPECT_FALSE(r1); + EXPECT_TRUE(r2); + EXPECT_FALSE(was_destroyed1); + EXPECT_TRUE(was_destroyed2); + } + EXPECT_TRUE(was_destroyed1); +} + +TEST(RefCountedTest, SelfAssignment) { + bool was_destroyed; + + { + MyClass* created = nullptr; + was_destroyed = false; + RefPtr r(MakeRefCounted(&created, &was_destroyed)); + // Copy. + r = r; + EXPECT_EQ(created, r.get()); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); + + { + MyClass* created = nullptr; + was_destroyed = false; + RefPtr r(MakeRefCounted(&created, &was_destroyed)); + // Move. + ALLOW_SELF_MOVE(r = std::move(r)) + EXPECT_EQ(created, r.get()); + EXPECT_FALSE(was_destroyed); + } + EXPECT_TRUE(was_destroyed); +} + +TEST(RefCountedTest, Swap) { + MyClass* created1 = nullptr; + bool was_destroyed1 = false; + RefPtr r1(MakeRefCounted(&created1, &was_destroyed1)); + EXPECT_TRUE(created1); + EXPECT_EQ(created1, r1.get()); + + MyClass* created2 = nullptr; + bool was_destroyed2 = false; + RefPtr r2(MakeRefCounted(&created2, &was_destroyed2)); + EXPECT_TRUE(created2); + EXPECT_EQ(created2, r2.get()); + EXPECT_NE(created1, created2); + + r1.swap(r2); + EXPECT_EQ(created2, r1.get()); + EXPECT_EQ(created1, r2.get()); +} + +TEST(RefCountedTest, GetAndDereferenceOperators) { + // Note: We check here that .get(), operator*, and operator-> are const, but + // return non-const pointers/refs. + + MyClass* created = nullptr; + const RefPtr r(MakeRefCounted(&created, nullptr)); + MyClass* ptr = r.get(); // Assign to non-const pointer. + EXPECT_EQ(created, ptr); + ptr = r.operator->(); // Assign to non-const pointer. + EXPECT_EQ(created, ptr); + MyClass& ref = *r; // "Assign" to non-const reference. + EXPECT_EQ(created, &ref); +} + +// You can manually call |AddRef()| and |Release()| if you want. +TEST(RefCountedTest, AddRefRelease) { + MyClass* created = nullptr; + bool was_destroyed = false; + { + RefPtr r(MakeRefCounted(&created, &was_destroyed)); + EXPECT_EQ(created, r.get()); + created->AddRef(); + } + EXPECT_FALSE(was_destroyed); + created->Release(); + EXPECT_TRUE(was_destroyed); +} + +TEST(RefCountedTest, Mix) { + MySubclass* created = nullptr; + bool was_destroyed = false; + RefPtr r1(MakeRefCounted(&created, &was_destroyed)); + ASSERT_FALSE(was_destroyed); + EXPECT_TRUE(created->HasOneRef()); + created->AssertHasOneRef(); + + RefPtr r2 = r1; + ASSERT_FALSE(was_destroyed); + EXPECT_FALSE(created->HasOneRef()); + + r1 = nullptr; + ASSERT_FALSE(was_destroyed); + created->AssertHasOneRef(); + + { + RefPtr r3 = r2; + EXPECT_FALSE(created->HasOneRef()); + { + RefPtr r4(r3); + r2 = nullptr; + ASSERT_FALSE(was_destroyed); + EXPECT_FALSE(created->HasOneRef()); + } + ASSERT_FALSE(was_destroyed); + EXPECT_TRUE(created->HasOneRef()); + created->AssertHasOneRef(); + + r1 = RefPtr(static_cast(r3.get())); + ASSERT_FALSE(was_destroyed); + EXPECT_FALSE(created->HasOneRef()); + } + ASSERT_FALSE(was_destroyed); + EXPECT_TRUE(created->HasOneRef()); + created->AssertHasOneRef(); + + EXPECT_EQ(created, r1.get()); + + r1 = nullptr; + EXPECT_TRUE(was_destroyed); +} + +class MyPublicClass : public RefCountedThreadSafe { + public: + // Overloaded constructors work with |MakeRefCounted()|. + MyPublicClass() : has_num_(false), num_(0) {} + explicit MyPublicClass(int num) : has_num_(true), num_(num) {} + + ~MyPublicClass() {} + + bool has_num() const { return has_num_; } + int num() const { return num_; } + + private: + bool has_num_; + int num_; + + FML_DISALLOW_COPY_AND_ASSIGN(MyPublicClass); +}; + +// You can also just keep constructors and destructors public. Make sure that +// works (mostly that it compiles). +TEST(RefCountedTest, PublicCtorAndDtor) { + RefPtr r1 = MakeRefCounted(); + ASSERT_TRUE(r1); + EXPECT_FALSE(r1->has_num()); + + RefPtr r2 = MakeRefCounted(123); + ASSERT_TRUE(r2); + EXPECT_TRUE(r2->has_num()); + EXPECT_EQ(123, r2->num()); + EXPECT_NE(r1.get(), r2.get()); + + r1 = r2; + EXPECT_TRUE(r1->has_num()); + EXPECT_EQ(123, r1->num()); + EXPECT_EQ(r1.get(), r2.get()); + + r2 = nullptr; + EXPECT_FALSE(r2); + EXPECT_TRUE(r1->has_num()); + EXPECT_EQ(123, r1->num()); + + r1 = nullptr; + EXPECT_FALSE(r1); +} + +// The danger with having a public constructor or destructor is that certain +// things will compile. You should get some protection by assertions in Debug +// builds. +#ifndef NDEBUG +TEST(RefCountedTest, DebugChecks) { + { + MyPublicClass* p = new MyPublicClass(); + EXPECT_DEATH_IF_SUPPORTED(delete p, "!adoption_required_"); + } + + { + MyPublicClass* p = new MyPublicClass(); + EXPECT_DEATH_IF_SUPPORTED(RefPtr r(p), + "!adoption_required_"); + } + + { + RefPtr r(MakeRefCounted()); + EXPECT_DEATH_IF_SUPPORTED(delete r.get(), "destruction_started_"); + } +} +#endif + +// TODO(vtl): Add (threaded) stress tests. + +} // namespace +} // namespace fml diff --git a/engine/src/flutter/fml/memory/ref_ptr.h b/engine/src/flutter/fml/memory/ref_ptr.h new file mode 100644 index 0000000000..098ea40920 --- /dev/null +++ b/engine/src/flutter/fml/memory/ref_ptr.h @@ -0,0 +1,250 @@ +// Copyright 2016 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Provides a smart pointer class for intrusively reference-counted objects. + +#ifndef FLUTTER_FML_MEMORY_REF_PTR_H_ +#define FLUTTER_FML_MEMORY_REF_PTR_H_ + +#include + +#include +#include + +#include "flutter/fml/macros.h" +#include "flutter/fml/memory/ref_ptr_internal.h" +#include "lib/fxl/logging.h" + +namespace fml { + +// A smart pointer class for intrusively reference-counted objects (e.g., those +// subclassing |RefCountedThreadSafe| -- see ref_counted.h). +// +// Such objects require *adoption* to obtain the first |RefPtr|, which is +// accomplished using |AdoptRef| (see below). (This is due to such objects being +// constructed with a reference count of 1. The adoption requirement is +// enforced, at least in Debug builds, by assertions.) +// +// E.g., if |Foo| is an intrusively reference-counted class: +// +// // The |AdoptRef| may be put in a static factory method (e.g., if |Foo|'s +// // constructor is private). +// RefPtr my_foo_ptr(AdoptRef(new Foo())); +// +// // Now OK, since "my Foo" has been adopted ... +// RefPtr another_ptr_to_my_foo(my_foo_ptr.get()); +// +// // ... though this would preferable in this situation. +// RefPtr yet_another_ptr_to_my_foo(my_foo_ptr); +// +// Unlike Chromium's |scoped_refptr|, |RefPtr| is only explicitly constructible +// from a plain pointer (and not assignable). It is however implicitly +// constructible from |nullptr|. So: +// +// RefPtr foo(plain_ptr_to_adopted_foo); // OK. +// foo = plain_ptr_to_adopted_foo; // Not OK (doesn't compile). +// foo = RefPtr(plain_ptr_to_adopted_foo); // OK. +// foo = nullptr; // OK. +// +// And if we have |void MyFunction(RefPtr foo)|, calling it using +// |MyFunction(nullptr)| is also valid. +// +// Implementation note: For copy/move constructors/operator=s, we often have +// templated versions, so that the operation can be done on a |RefPtr|, where +// |U| is a subclass of |T|. However, we also have non-templated versions with +// |U = T|, since the templated versions don't count as copy/move +// constructors/operator=s for the purposes of causing the default copy +// constructor/operator= to be deleted. E.g., if we didn't declare any +// non-templated versions, we'd get the default copy constructor/operator= (we'd +// only not get the default move constructor/operator= by virtue of having a +// destructor)! (In fact, it'd suffice to only declare a non-templated move +// constructor or move operator=, which would cause the copy +// constructor/operator= to be deleted, but for clarity we include explicit +// non-templated versions of everything.) +template +class RefPtr final { + public: + RefPtr() : ptr_(nullptr) {} + RefPtr(std::nullptr_t) : ptr_(nullptr) {} + + // Explicit constructor from a plain pointer (to an object that must have + // already been adopted). (Note that in |T::T()|, references to |this| cannot + // be taken, since the object being constructed will not have been adopted + // yet.) + template + explicit RefPtr(U* p) : ptr_(p) { + if (ptr_) + ptr_->AddRef(); + } + + // Copy constructor. + RefPtr(const RefPtr& r) : ptr_(r.ptr_) { + if (ptr_) + ptr_->AddRef(); + } + + template + RefPtr(const RefPtr& r) : ptr_(r.ptr_) { + if (ptr_) + ptr_->AddRef(); + } + + // Move constructor. + RefPtr(RefPtr&& r) : ptr_(r.ptr_) { r.ptr_ = nullptr; } + + template + RefPtr(RefPtr&& r) : ptr_(r.ptr_) { + r.ptr_ = nullptr; + } + + // Destructor. + ~RefPtr() { + if (ptr_) + ptr_->Release(); + } + + T* get() const { return ptr_; } + + T& operator*() const { + FXL_DCHECK(ptr_); + return *ptr_; + } + + T* operator->() const { + FXL_DCHECK(ptr_); + return ptr_; + } + + // Copy assignment. + RefPtr& operator=(const RefPtr& r) { + // Call |AddRef()| first so self-assignments work. + if (r.ptr_) + r.ptr_->AddRef(); + T* old_ptr = ptr_; + ptr_ = r.ptr_; + if (old_ptr) + old_ptr->Release(); + return *this; + } + + template + RefPtr& operator=(const RefPtr& r) { + // Call |AddRef()| first so self-assignments work. + if (r.ptr_) + r.ptr_->AddRef(); + T* old_ptr = ptr_; + ptr_ = r.ptr_; + if (old_ptr) + old_ptr->Release(); + return *this; + } + + // Move assignment. + // Note: Like |std::shared_ptr|, we support self-move and move assignment is + // equivalent to |RefPtr(std::move(r)).swap(*this)|. + RefPtr& operator=(RefPtr&& r) { + RefPtr(std::move(r)).swap(*this); + return *this; + } + + template + RefPtr& operator=(RefPtr&& r) { + RefPtr(std::move(r)).swap(*this); + return *this; + } + + void swap(RefPtr& r) { + T* p = ptr_; + ptr_ = r.ptr_; + r.ptr_ = p; + } + + // Returns a new |RefPtr| with the same contents as this pointer. Useful + // when a function takes a |RefPtr&&| argument and the caller wants to + // retain its reference (rather than moving it). + RefPtr Clone() const { return *this; } + + explicit operator bool() const { return !!ptr_; } + + template + bool operator==(const RefPtr& rhs) const { + return ptr_ == rhs.ptr_; + } + + template + bool operator!=(const RefPtr& rhs) const { + return !operator==(rhs); + } + + template + bool operator<(const RefPtr& rhs) const { + return ptr_ < rhs.ptr_; + } + + private: + template + friend class RefPtr; + + friend RefPtr AdoptRef(T*); + + enum AdoptTag { ADOPT }; + RefPtr(T* ptr, AdoptTag) : ptr_(ptr) { FXL_DCHECK(ptr_); } + + T* ptr_; +}; + +// Adopts a newly-created |T|. Typically used in a static factory method, like: +// +// // static +// RefPtr Foo::Create() { +// return AdoptRef(new Foo()); +// } +template +inline RefPtr AdoptRef(T* ptr) { +#ifndef NDEBUG + ptr->Adopt(); +#endif + return RefPtr(ptr, RefPtr::ADOPT); +} + +// Constructs a |RefPtr| from a plain pointer (to an object that must +// have already been adoped). Avoids having to spell out the full type name. +// +// Foo* foo = ...; +// auto foo_ref = Ref(foo); +// +// (|foo_ref| will be of type |RefPtr|.) +template +inline RefPtr Ref(T* ptr) { + return RefPtr(ptr); +} + +// Creates an intrusively reference counted |T|, producing a |RefPtr| (and +// performing the required adoption). Use like: +// +// auto my_foo = MakeRefCounted(ctor_arg1, ctor_arg2); +// +// (|my_foo| will be of type |RefPtr|.) +template +RefPtr MakeRefCounted(Args&&... args) { + return internal::MakeRefCountedHelper::MakeRefCounted( + std::forward(args)...); +} + +} // namespace fml + +// Inject custom std::hash<> function object for |RefPtr|. +namespace std { +template +struct hash> { + using argument_type = fml::RefPtr; + using result_type = std::size_t; + + result_type operator()(const argument_type& ptr) const { + return std::hash()(ptr.get()); + } +}; +} // namespace std + +#endif // FLUTTER_FML_MEMORY_REF_PTR_H_ diff --git a/engine/src/flutter/fml/memory/ref_ptr_internal.h b/engine/src/flutter/fml/memory/ref_ptr_internal.h new file mode 100644 index 0000000000..a72488cad6 --- /dev/null +++ b/engine/src/flutter/fml/memory/ref_ptr_internal.h @@ -0,0 +1,38 @@ +// Copyright 2016 The Fuchsia 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_MEMORY_REF_PTR_INTERNAL_H_ +#define FLUTTER_FML_MEMORY_REF_PTR_INTERNAL_H_ + +#include + +#include "flutter/fml/macros.h" + +namespace fml { + +template +class RefPtr; + +template +RefPtr AdoptRef(T* ptr); + +namespace internal { + +// This is a wrapper class that can be friended for a particular |T|, if you +// want to make |T|'s constructor private, but still use |MakeRefCounted()| +// (below). (You can't friend partial specializations.) See |MakeRefCounted()| +// and |FML_FRIEND_MAKE_REF_COUNTED()|. +template +class MakeRefCountedHelper final { + public: + template + static RefPtr MakeRefCounted(Args&&... args) { + return AdoptRef(new T(std::forward(args)...)); + } +}; + +} // namespace internal +} // namespace fml + +#endif // FLUTTER_FML_MEMORY_REF_PTR_INTERNAL_H_ diff --git a/engine/src/flutter/fml/memory/thread_checker.h b/engine/src/flutter/fml/memory/thread_checker.h index f22e9d1cfa..23e55f3e4d 100644 --- a/engine/src/flutter/fml/memory/thread_checker.h +++ b/engine/src/flutter/fml/memory/thread_checker.h @@ -8,7 +8,7 @@ #ifndef FLUTTER_FML_MEMORY_THREAD_CHECKER_H_ #define FLUTTER_FML_MEMORY_THREAD_CHECKER_H_ -#include "lib/fxl/build_config.h" +#include "flutter/fml/build_config.h" #if defined(OS_WIN) #include @@ -16,8 +16,8 @@ #include #endif +#include "flutter/fml/macros.h" #include "lib/fxl/logging.h" -#include "lib/fxl/macros.h" namespace fml { diff --git a/engine/src/flutter/fml/memory/weak_ptr.h b/engine/src/flutter/fml/memory/weak_ptr.h index 946ab645e0..cf9485452e 100644 --- a/engine/src/flutter/fml/memory/weak_ptr.h +++ b/engine/src/flutter/fml/memory/weak_ptr.h @@ -10,10 +10,10 @@ #include +#include "flutter/fml/memory/ref_counted.h" #include "flutter/fml/memory/thread_checker.h" #include "flutter/fml/memory/weak_ptr_internal.h" #include "lib/fxl/logging.h" -#include "lib/fxl/memory/ref_counted.h" namespace fml { @@ -103,12 +103,12 @@ class WeakPtr { friend class WeakPtrFactory; explicit WeakPtr(T* ptr, - fxl::RefPtr&& flag, + fml::RefPtr&& flag, DebugThreadChecker checker) : ptr_(ptr), flag_(std::move(flag)), checker_(checker) {} T* ptr_; - fxl::RefPtr flag_; + fml::RefPtr flag_; DebugThreadChecker checker_; // Copy/move construction/assignment supported. @@ -160,7 +160,7 @@ template class WeakPtrFactory { public: explicit WeakPtrFactory(T* ptr) - : ptr_(ptr), flag_(fxl::MakeRefCounted()) { + : ptr_(ptr), flag_(fml::MakeRefCounted()) { FXL_DCHECK(ptr_); } @@ -179,10 +179,10 @@ class WeakPtrFactory { // Note: See weak_ptr_internal.h for an explanation of why we store the // pointer here, instead of in the "flag". T* const ptr_; - fxl::RefPtr flag_; + fml::RefPtr flag_; DebugThreadChecker checker_; - FXL_DISALLOW_COPY_AND_ASSIGN(WeakPtrFactory); + FML_DISALLOW_COPY_AND_ASSIGN(WeakPtrFactory); }; } // namespace fml diff --git a/engine/src/flutter/fml/memory/weak_ptr_internal.h b/engine/src/flutter/fml/memory/weak_ptr_internal.h index 570bd90ddf..dca47f1a61 100644 --- a/engine/src/flutter/fml/memory/weak_ptr_internal.h +++ b/engine/src/flutter/fml/memory/weak_ptr_internal.h @@ -5,8 +5,8 @@ #ifndef FLUTTER_FML_MEMORY_WEAK_PTR_INTERNAL_H_ #define FLUTTER_FML_MEMORY_WEAK_PTR_INTERNAL_H_ -#include "lib/fxl/macros.h" -#include "lib/fxl/memory/ref_counted.h" +#include "flutter/fml/macros.h" +#include "flutter/fml/memory/ref_counted.h" namespace fml { namespace internal { @@ -19,7 +19,7 @@ namespace internal { // This class in not thread-safe, though references may be released on any // thread (allowing weak pointers to be destroyed/reset/reassigned on any // thread). -class FXL_EXPORT WeakPtrFlag : public fxl::RefCountedThreadSafe { +class FXL_EXPORT WeakPtrFlag : public fml::RefCountedThreadSafe { public: WeakPtrFlag(); @@ -32,7 +32,7 @@ class FXL_EXPORT WeakPtrFlag : public fxl::RefCountedThreadSafe { private: bool is_valid_; - FXL_DISALLOW_COPY_AND_ASSIGN(WeakPtrFlag); + FML_DISALLOW_COPY_AND_ASSIGN(WeakPtrFlag); }; } // namespace internal diff --git a/engine/src/flutter/fml/message_loop.h b/engine/src/flutter/fml/message_loop.h index 3bfb1c40c6..9b2a28ccc4 100644 --- a/engine/src/flutter/fml/message_loop.h +++ b/engine/src/flutter/fml/message_loop.h @@ -6,7 +6,6 @@ #define FLUTTER_FML_MESSAGE_LOOP_H_ #include "flutter/fml/macros.h" -#include "lib/fxl/macros.h" #include "lib/fxl/tasks/task_runner.h" namespace fml { @@ -52,7 +51,7 @@ class MessageLoop { fxl::RefPtr GetLoopImpl() const; - FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoop); + FML_DISALLOW_COPY_AND_ASSIGN(MessageLoop); }; } // namespace fml diff --git a/engine/src/flutter/fml/message_loop_impl.cc b/engine/src/flutter/fml/message_loop_impl.cc index df885f3792..d5f8e4e17c 100644 --- a/engine/src/flutter/fml/message_loop_impl.cc +++ b/engine/src/flutter/fml/message_loop_impl.cc @@ -9,8 +9,8 @@ #include #include +#include "flutter/fml/build_config.h" #include "flutter/fml/trace_event.h" -#include "lib/fxl/build_config.h" #if OS_MACOSX #include "flutter/fml/platform/darwin/message_loop_darwin.h" diff --git a/engine/src/flutter/fml/message_loop_impl.h b/engine/src/flutter/fml/message_loop_impl.h index 478cbd1f1a..9a513f238c 100644 --- a/engine/src/flutter/fml/message_loop_impl.h +++ b/engine/src/flutter/fml/message_loop_impl.h @@ -12,9 +12,9 @@ #include #include +#include "flutter/fml/macros.h" #include "flutter/fml/message_loop.h" #include "lib/fxl/functional/closure.h" -#include "lib/fxl/macros.h" #include "lib/fxl/memory/ref_counted.h" #include "lib/fxl/time/time_point.h" @@ -81,7 +81,7 @@ class MessageLoopImpl : public fxl::RefCountedThreadSafe { void RunExpiredTasks(); - FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl); + FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl); }; } // namespace fml diff --git a/engine/src/flutter/fml/native_library.h b/engine/src/flutter/fml/native_library.h index 7e48ed6d3a..a52d01a3e0 100644 --- a/engine/src/flutter/fml/native_library.h +++ b/engine/src/flutter/fml/native_library.h @@ -5,7 +5,7 @@ #ifndef FLUTTER_FML_NATIVE_LIBRARY_H_ #define FLUTTER_FML_NATIVE_LIBRARY_H_ -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" #include "lib/fxl/memory/ref_counted.h" #include "lib/fxl/memory/ref_ptr.h" @@ -42,7 +42,7 @@ class NativeLibrary : public fxl::RefCountedThreadSafe { Handle GetHandle() const; - FXL_DISALLOW_COPY_AND_ASSIGN(NativeLibrary); + FML_DISALLOW_COPY_AND_ASSIGN(NativeLibrary); FRIEND_REF_COUNTED_THREAD_SAFE(NativeLibrary); FRIEND_MAKE_REF_COUNTED(NativeLibrary); }; diff --git a/engine/src/flutter/fml/paths.cc b/engine/src/flutter/fml/paths.cc index 9b06b6847f..20c94e62fe 100644 --- a/engine/src/flutter/fml/paths.cc +++ b/engine/src/flutter/fml/paths.cc @@ -4,7 +4,7 @@ #include "flutter/fml/paths.h" -#include "lib/fxl/build_config.h" +#include "flutter/fml/build_config.h" namespace fml { namespace paths { diff --git a/engine/src/flutter/fml/platform/android/jni_util.h b/engine/src/flutter/fml/platform/android/jni_util.h index f4ecfc266d..bfd58ae0cd 100644 --- a/engine/src/flutter/fml/platform/android/jni_util.h +++ b/engine/src/flutter/fml/platform/android/jni_util.h @@ -9,8 +9,8 @@ #include +#include "flutter/fml/macros.h" #include "flutter/fml/platform/android/scoped_java_ref.h" -#include "lib/fxl/macros.h" namespace fml { namespace jni { diff --git a/engine/src/flutter/fml/platform/android/message_loop_android.cc b/engine/src/flutter/fml/platform/android/message_loop_android.cc index 0004d1e042..0720c5ef8a 100644 --- a/engine/src/flutter/fml/platform/android/message_loop_android.cc +++ b/engine/src/flutter/fml/platform/android/message_loop_android.cc @@ -8,7 +8,6 @@ #include #include "flutter/fml/platform/linux/timerfd.h" -#include "lib/fxl/files/eintr_wrapper.h" namespace fml { diff --git a/engine/src/flutter/fml/platform/android/message_loop_android.h b/engine/src/flutter/fml/platform/android/message_loop_android.h index 19c1eafe79..3467f41f27 100644 --- a/engine/src/flutter/fml/platform/android/message_loop_android.h +++ b/engine/src/flutter/fml/platform/android/message_loop_android.h @@ -9,9 +9,9 @@ #include +#include "flutter/fml/macros.h" #include "flutter/fml/message_loop_impl.h" #include "flutter/fml/unique_fd.h" -#include "lib/fxl/macros.h" #include "lib/fxl/memory/unique_object.h" namespace fml { @@ -42,7 +42,7 @@ class MessageLoopAndroid : public MessageLoopImpl { FRIEND_MAKE_REF_COUNTED(MessageLoopAndroid); FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopAndroid); - FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoopAndroid); + FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopAndroid); }; } // namespace fml diff --git a/engine/src/flutter/fml/platform/android/scoped_java_ref.h b/engine/src/flutter/fml/platform/android/scoped_java_ref.h index f98aebf51d..5bc9f1019b 100644 --- a/engine/src/flutter/fml/platform/android/scoped_java_ref.h +++ b/engine/src/flutter/fml/platform/android/scoped_java_ref.h @@ -8,7 +8,7 @@ #include #include -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" namespace fml { namespace jni { @@ -27,7 +27,7 @@ class ScopedJavaLocalFrame { // it's safe to cache the non-threadsafe JNIEnv* inside this object. JNIEnv* env_; - FXL_DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame); + FML_DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame); }; // Forward declare the generic java reference template class. @@ -65,7 +65,7 @@ class JavaRef { private: jobject obj_; - FXL_DISALLOW_COPY_AND_ASSIGN(JavaRef); + FML_DISALLOW_COPY_AND_ASSIGN(JavaRef); }; // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful @@ -83,7 +83,7 @@ class JavaRef : public JavaRef { JavaRef(JNIEnv* env, T obj) : JavaRef(env, obj) {} private: - FXL_DISALLOW_COPY_AND_ASSIGN(JavaRef); + FML_DISALLOW_COPY_AND_ASSIGN(JavaRef); }; // Holds a local reference to a Java object. The local reference is scoped diff --git a/engine/src/flutter/fml/platform/darwin/cf_utils.h b/engine/src/flutter/fml/platform/darwin/cf_utils.h index b5a3d07441..b8e64372e6 100644 --- a/engine/src/flutter/fml/platform/darwin/cf_utils.h +++ b/engine/src/flutter/fml/platform/darwin/cf_utils.h @@ -7,7 +7,7 @@ #include -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" namespace fml { @@ -43,7 +43,7 @@ class CFRef { private: T instance_; - FXL_DISALLOW_COPY_AND_ASSIGN(CFRef); + FML_DISALLOW_COPY_AND_ASSIGN(CFRef); }; } // namespace fml diff --git a/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h b/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h index 902aafd012..dde809860e 100644 --- a/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h +++ b/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h @@ -9,9 +9,9 @@ #include +#include "flutter/fml/macros.h" #include "flutter/fml/message_loop_impl.h" #include "flutter/fml/platform/darwin/cf_utils.h" -#include "lib/fxl/macros.h" namespace fml { @@ -35,7 +35,7 @@ class MessageLoopDarwin : public MessageLoopImpl { FRIEND_MAKE_REF_COUNTED(MessageLoopDarwin); FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopDarwin); - FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoopDarwin); + FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopDarwin); }; } // namespace fml diff --git a/engine/src/flutter/fml/platform/darwin/platform_version.h b/engine/src/flutter/fml/platform/darwin/platform_version.h index b92641655e..bec2ed7ed6 100644 --- a/engine/src/flutter/fml/platform/darwin/platform_version.h +++ b/engine/src/flutter/fml/platform/darwin/platform_version.h @@ -6,7 +6,7 @@ #define FLUTTER_FML_PLATFORM_DARWIN_PLATFORM_VERSION_H_ #include -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" namespace fml { diff --git a/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h b/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h index c3b939cea7..4cbc158b90 100644 --- a/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h +++ b/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h @@ -5,8 +5,8 @@ #ifndef FLUTTER_FML_PLATFORM_DARWIN_RESOURCE_MAPPING_DARWIN_H_ #define FLUTTER_FML_PLATFORM_DARWIN_RESOURCE_MAPPING_DARWIN_H_ +#include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "lib/fxl/macros.h" namespace fml { @@ -23,7 +23,7 @@ class ResourceMappingDarwin : public Mapping { private: FileMapping actual_; - FXL_DISALLOW_COPY_AND_ASSIGN(ResourceMappingDarwin); + FML_DISALLOW_COPY_AND_ASSIGN(ResourceMappingDarwin); }; } // namespace fml diff --git a/engine/src/flutter/fml/platform/darwin/scoped_block.h b/engine/src/flutter/fml/platform/darwin/scoped_block.h index e7a16e9d56..758bb0886d 100644 --- a/engine/src/flutter/fml/platform/darwin/scoped_block.h +++ b/engine/src/flutter/fml/platform/darwin/scoped_block.h @@ -7,7 +7,7 @@ #include -#include "lib/fxl/compiler_specific.h" +#include "flutter/fml/compiler_specific.h" namespace fml { @@ -72,7 +72,7 @@ class ScopedBlock { block_ = temp; } - B release() FXL_WARN_UNUSED_RESULT { + B release() FML_WARN_UNUSED_RESULT { B temp = block_; block_ = nullptr; return temp; diff --git a/engine/src/flutter/fml/platform/darwin/scoped_nsobject.h b/engine/src/flutter/fml/platform/darwin/scoped_nsobject.h index 91d60ae8f3..525185fa6d 100644 --- a/engine/src/flutter/fml/platform/darwin/scoped_nsobject.h +++ b/engine/src/flutter/fml/platform/darwin/scoped_nsobject.h @@ -10,8 +10,8 @@ // singled out because it is most typically included from other header files. #import -#include "lib/fxl/compiler_specific.h" -#include "lib/fxl/macros.h" +#include "flutter/fml/compiler_specific.h" +#include "flutter/fml/macros.h" @class NSAutoreleasePool; @@ -81,7 +81,7 @@ class scoped_nsprotocol { // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a // wrapper for [object_ release]. To force a scoped_nsprotocol<> to call // [object_ release], use scoped_nsprotocol<>::reset(). - NST release() FXL_WARN_UNUSED_RESULT { + NST release() FML_WARN_UNUSED_RESULT { NST temp = object_; object_ = nil; return temp; @@ -155,7 +155,7 @@ template <> class scoped_nsobject { private: explicit scoped_nsobject(NSAutoreleasePool* object = nil); - FXL_DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); + FML_DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); }; } // namespace fml diff --git a/engine/src/flutter/fml/platform/linux/message_loop_linux.cc b/engine/src/flutter/fml/platform/linux/message_loop_linux.cc index e80e7f9d14..2868f137e5 100644 --- a/engine/src/flutter/fml/platform/linux/message_loop_linux.cc +++ b/engine/src/flutter/fml/platform/linux/message_loop_linux.cc @@ -7,15 +7,15 @@ #include #include +#include "flutter/fml/eintr_wrapper.h" #include "flutter/fml/platform/linux/timerfd.h" -#include "lib/fxl/files/eintr_wrapper.h" namespace fml { static constexpr int kClockType = CLOCK_MONOTONIC; MessageLoopLinux::MessageLoopLinux() - : epoll_fd_(HANDLE_EINTR(::epoll_create(1 /* unused */))), + : epoll_fd_(FML_HANDLE_EINTR(::epoll_create(1 /* unused */))), timer_fd_(::timerfd_create(kClockType, TFD_NONBLOCK | TFD_CLOEXEC)), running_(false) { FXL_CHECK(epoll_fd_.is_valid()); @@ -49,7 +49,7 @@ void MessageLoopLinux::Run() { while (running_) { struct epoll_event event = {}; - int epoll_result = HANDLE_EINTR( + int epoll_result = FML_HANDLE_EINTR( ::epoll_wait(epoll_fd_.get(), &event, 1, -1 /* timeout */)); // Errors are fatal. diff --git a/engine/src/flutter/fml/platform/linux/message_loop_linux.h b/engine/src/flutter/fml/platform/linux/message_loop_linux.h index ba5e902d4a..dc5b862da9 100644 --- a/engine/src/flutter/fml/platform/linux/message_loop_linux.h +++ b/engine/src/flutter/fml/platform/linux/message_loop_linux.h @@ -7,9 +7,9 @@ #include +#include "flutter/fml/macros.h" #include "flutter/fml/message_loop_impl.h" #include "flutter/fml/unique_fd.h" -#include "lib/fxl/macros.h" namespace fml { @@ -35,7 +35,7 @@ class MessageLoopLinux : public MessageLoopImpl { FRIEND_MAKE_REF_COUNTED(MessageLoopLinux); FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopLinux); - FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoopLinux); + FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopLinux); }; } // namespace fml diff --git a/engine/src/flutter/fml/platform/linux/timerfd.cc b/engine/src/flutter/fml/platform/linux/timerfd.cc index 1aa6ebeb53..871b963b5b 100644 --- a/engine/src/flutter/fml/platform/linux/timerfd.cc +++ b/engine/src/flutter/fml/platform/linux/timerfd.cc @@ -7,7 +7,7 @@ #include #include -#include "lib/fxl/files/eintr_wrapper.h" +#include "flutter/fml/eintr_wrapper.h" #if FML_TIMERFD_AVAILABLE == 0 @@ -48,7 +48,7 @@ bool TimerRearm(int fd, fxl::TimePoint time_point) { bool TimerDrain(int fd) { // 8 bytes must be read from a signalled timer file descriptor when signalled. uint64_t fire_count = 0; - ssize_t size = HANDLE_EINTR(::read(fd, &fire_count, sizeof(uint64_t))); + ssize_t size = FML_HANDLE_EINTR(::read(fd, &fire_count, sizeof(uint64_t))); if (size != sizeof(uint64_t)) { return false; } diff --git a/engine/src/flutter/fml/platform/posix/file_posix.cc b/engine/src/flutter/fml/platform/posix/file_posix.cc index 028f49f4d1..58167082ce 100644 --- a/engine/src/flutter/fml/platform/posix/file_posix.cc +++ b/engine/src/flutter/fml/platform/posix/file_posix.cc @@ -8,7 +8,7 @@ #include #include -#include "lib/fxl/files/eintr_wrapper.h" +#include "flutter/fml/eintr_wrapper.h" namespace fml { @@ -47,11 +47,11 @@ fml::UniqueFD OpenFile(const fml::UniqueFD& base_directory, } return fml::UniqueFD{ - HANDLE_EINTR(::openat(base_directory.get(), path, flags))}; + FML_HANDLE_EINTR(::openat(base_directory.get(), path, flags))}; } fml::UniqueFD Duplicate(fml::UniqueFD::element_type descriptor) { - return fml::UniqueFD{HANDLE_EINTR(::dup(descriptor))}; + return fml::UniqueFD{FML_HANDLE_EINTR(::dup(descriptor))}; } bool IsDirectory(const fml::UniqueFD& directory) { diff --git a/engine/src/flutter/fml/platform/posix/mapping_posix.cc b/engine/src/flutter/fml/platform/posix/mapping_posix.cc index fc53d6a4f7..b9f8fe5de9 100644 --- a/engine/src/flutter/fml/platform/posix/mapping_posix.cc +++ b/engine/src/flutter/fml/platform/posix/mapping_posix.cc @@ -11,9 +11,9 @@ #include +#include "flutter/fml/build_config.h" +#include "flutter/fml/eintr_wrapper.h" #include "flutter/fml/unique_fd.h" -#include "lib/fxl/build_config.h" -#include "lib/fxl/files/eintr_wrapper.h" #if OS_MACOSX @@ -41,8 +41,9 @@ std::unique_ptr GetResourceMapping(const std::string& resource_name) { } FileMapping::FileMapping(const std::string& path, bool executable) - : FileMapping(fml::UniqueFD{HANDLE_EINTR(::open(path.c_str(), O_RDONLY))}, - executable) {} + : FileMapping( + fml::UniqueFD{FML_HANDLE_EINTR(::open(path.c_str(), O_RDONLY))}, + executable) {} FileMapping::FileMapping(const fml::UniqueFD& handle, bool executable) : size_(0), mapping_(nullptr) { diff --git a/engine/src/flutter/fml/platform/win/message_loop_win.h b/engine/src/flutter/fml/platform/win/message_loop_win.h index 1b02ad6cc2..22836c6874 100644 --- a/engine/src/flutter/fml/platform/win/message_loop_win.h +++ b/engine/src/flutter/fml/platform/win/message_loop_win.h @@ -9,8 +9,8 @@ #include +#include "flutter/fml/macros.h" #include "flutter/fml/message_loop_impl.h" -#include "lib/fxl/macros.h" #include "lib/fxl/memory/unique_object.h" namespace fml { @@ -38,7 +38,7 @@ class MessageLoopWin : public MessageLoopImpl { FRIEND_MAKE_REF_COUNTED(MessageLoopWin); FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopWin); - FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoopWin); + FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopWin); }; } // namespace fml diff --git a/engine/src/flutter/fml/task_runner.h b/engine/src/flutter/fml/task_runner.h index 3b3d2de016..b4304ddd2f 100644 --- a/engine/src/flutter/fml/task_runner.h +++ b/engine/src/flutter/fml/task_runner.h @@ -5,7 +5,7 @@ #ifndef FLUTTER_FML_TASK_RUNNER_H_ #define FLUTTER_FML_TASK_RUNNER_H_ -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" #include "lib/fxl/memory/ref_counted.h" #include "lib/fxl/tasks/task_runner.h" @@ -35,7 +35,7 @@ class TaskRunner final : public fxl::TaskRunner { FRIEND_MAKE_REF_COUNTED(TaskRunner); FRIEND_REF_COUNTED_THREAD_SAFE(TaskRunner); - FXL_DISALLOW_COPY_AND_ASSIGN(TaskRunner); + FML_DISALLOW_COPY_AND_ASSIGN(TaskRunner); }; } // namespace fml diff --git a/engine/src/flutter/fml/thread.cc b/engine/src/flutter/fml/thread.cc index de4ee6dd36..ea1060db03 100644 --- a/engine/src/flutter/fml/thread.cc +++ b/engine/src/flutter/fml/thread.cc @@ -6,7 +6,7 @@ #include "flutter/fml/thread.h" -#include "lib/fxl/build_config.h" +#include "flutter/fml/build_config.h" #if defined(OS_WIN) #include diff --git a/engine/src/flutter/fml/thread.h b/engine/src/flutter/fml/thread.h index 542871f788..494897413e 100644 --- a/engine/src/flutter/fml/thread.h +++ b/engine/src/flutter/fml/thread.h @@ -9,8 +9,8 @@ #include #include +#include "flutter/fml/macros.h" #include "flutter/fml/task_runner.h" -#include "lib/fxl/macros.h" namespace fml { @@ -31,7 +31,7 @@ class Thread { static void SetCurrentThreadName(const std::string& name); - FXL_DISALLOW_COPY_AND_ASSIGN(Thread); + FML_DISALLOW_COPY_AND_ASSIGN(Thread); }; } // namespace fml diff --git a/engine/src/flutter/fml/thread_local.h b/engine/src/flutter/fml/thread_local.h index c3c7295ef3..e450097abd 100644 --- a/engine/src/flutter/fml/thread_local.h +++ b/engine/src/flutter/fml/thread_local.h @@ -7,9 +7,9 @@ #include -#include "lib/fxl/build_config.h" +#include "flutter/fml/build_config.h" +#include "flutter/fml/macros.h" #include "lib/fxl/logging.h" -#include "lib/fxl/macros.h" #define FML_THREAD_LOCAL_PTHREADS OS_MACOSX || OS_LINUX || OS_ANDROID @@ -55,7 +55,7 @@ class ThreadLocal { ThreadLocalDestroyCallback destroy_; intptr_t value_; - FXL_DISALLOW_COPY_AND_ASSIGN(Box); + FML_DISALLOW_COPY_AND_ASSIGN(Box); }; static inline void ThreadLocalDestroy(void* value) { @@ -106,7 +106,7 @@ class ThreadLocal { pthread_key_t _key; ThreadLocalDestroyCallback destroy_; - FXL_DISALLOW_COPY_AND_ASSIGN(ThreadLocal); + FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocal); }; #else // FML_THREAD_LOCAL_PTHREADS @@ -144,7 +144,7 @@ class ThreadLocal { ThreadLocalDestroyCallback destroy_; intptr_t value_; - FXL_DISALLOW_COPY_AND_ASSIGN(ThreadLocal); + FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocal); }; #endif // FML_THREAD_LOCAL_PTHREADS diff --git a/engine/src/flutter/fml/trace_event.h b/engine/src/flutter/fml/trace_event.h index 832d099ce3..775dea4e6b 100644 --- a/engine/src/flutter/fml/trace_event.h +++ b/engine/src/flutter/fml/trace_event.h @@ -9,7 +9,7 @@ #include #include -#include "lib/fxl/macros.h" +#include "flutter/fml/macros.h" #ifndef TRACE_EVENT_HIDE_MACROS @@ -115,7 +115,7 @@ class ScopedInstantEnd { private: const std::string label_; - FXL_DISALLOW_COPY_AND_ASSIGN(ScopedInstantEnd); + FML_DISALLOW_COPY_AND_ASSIGN(ScopedInstantEnd); }; } // namespace tracing diff --git a/engine/src/flutter/fml/unique_fd.cc b/engine/src/flutter/fml/unique_fd.cc index bab79f7a9b..90c3aaa21e 100644 --- a/engine/src/flutter/fml/unique_fd.cc +++ b/engine/src/flutter/fml/unique_fd.cc @@ -4,7 +4,7 @@ #include "flutter/fml/unique_fd.h" -#include "lib/fxl/files/eintr_wrapper.h" +#include "flutter/fml/eintr_wrapper.h" namespace fml { namespace internal { @@ -24,7 +24,7 @@ void UniqueFDTraits::Free(HANDLE fd) { namespace unix { void UniqueFDTraits::Free(int fd) { - IGNORE_EINTR(fd); + FML_IGNORE_EINTR(fd); } } // namespace unix diff --git a/engine/src/flutter/fml/unique_fd.h b/engine/src/flutter/fml/unique_fd.h index 9f2d796579..48dd3544e4 100644 --- a/engine/src/flutter/fml/unique_fd.h +++ b/engine/src/flutter/fml/unique_fd.h @@ -5,8 +5,8 @@ #ifndef FLUTTER_FML_UNIQUE_FD_H_ #define FLUTTER_FML_UNIQUE_FD_H_ +#include "flutter/fml/build_config.h" #include "flutter/fml/unique_object.h" -#include "lib/fxl/build_config.h" #if OS_WIN diff --git a/engine/src/flutter/fml/unique_object.h b/engine/src/flutter/fml/unique_object.h index e2487a5388..4feabe57d6 100644 --- a/engine/src/flutter/fml/unique_object.h +++ b/engine/src/flutter/fml/unique_object.h @@ -7,9 +7,9 @@ #include -#include "lib/fxl/compiler_specific.h" +#include "flutter/fml/compiler_specific.h" +#include "flutter/fml/macros.h" #include "lib/fxl/logging.h" -#include "lib/fxl/macros.h" namespace fml { @@ -78,7 +78,7 @@ class UniqueObject { // Release the object. The return value is the current object held by this // object. After this operation, this object will hold an invalid value, and // will not own the object any more. - T release() FXL_WARN_UNUSED_RESULT { + T release() FML_WARN_UNUSED_RESULT { T old_generic = data_.generic; data_.generic = Traits::InvalidValue(); return old_generic; @@ -114,7 +114,7 @@ class UniqueObject { Data data_; - FXL_DISALLOW_COPY_AND_ASSIGN(UniqueObject); + FML_DISALLOW_COPY_AND_ASSIGN(UniqueObject); }; template