Skip to content

Commit

Permalink
Move scoped_nsautorelease_pool to base/apple, leave a forwarding header
Browse files Browse the repository at this point in the history
Crashpad is not yet updated for the new location, so leave a
forwarding header to be removed later.

Bug: 1444927
Change-Id: I143ff8d594afde9c9f5079c477f38d87337e2092
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4784010
Commit-Queue: Avi Drissman <avi@chromium.org>
Owners-Override: Avi Drissman <avi@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1184446}
  • Loading branch information
Avi Drissman authored and Chromium LUCI CQ committed Aug 16, 2023
1 parent 7b8024a commit 5289ab2
Show file tree
Hide file tree
Showing 54 changed files with 163 additions and 144 deletions.
4 changes: 2 additions & 2 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,8 @@ component("base") {
"apple/scoped_mach_port.h",
"apple/scoped_mach_vm.cc",
"apple/scoped_mach_vm.h",
"apple/scoped_nsautorelease_pool.cc",
"apple/scoped_nsautorelease_pool.h",
"apple/scoped_nsobject.h",
"apple/scoped_objc_class_swizzler.h",
"apple/scoped_objc_class_swizzler.mm",
Expand All @@ -1974,8 +1976,6 @@ component("base") {
"files/file_util_apple.mm",
"mac/foundation_util.h",
"mac/foundation_util.mm",
"mac/scoped_nsautorelease_pool.cc",
"mac/scoped_nsautorelease_pool.h",
"memory/platform_shared_memory_mapper_apple.cc",
"memory/platform_shared_memory_region_apple.cc",
"message_loop/message_pump_apple.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"

// Note that this uses the direct runtime interface to the autorelease pool.
// https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support
Expand All @@ -13,7 +13,7 @@ void* objc_autoreleasePoolPush(void);
void objc_autoreleasePoolPop(void* pool);
}

namespace base::mac {
namespace base::apple {

ScopedNSAutoreleasePool::ScopedNSAutoreleasePool()
: autorelease_pool_(objc_autoreleasePoolPush()) {}
Expand All @@ -31,4 +31,4 @@ void ScopedNSAutoreleasePool::Recycle() {
autorelease_pool_ = objc_autoreleasePoolPush();
}

} // namespace base::mac
} // namespace base::apple
55 changes: 55 additions & 0 deletions base/apple/scoped_nsautorelease_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_APPLE_SCOPED_NSAUTORELEASE_POOL_H_
#define BASE_APPLE_SCOPED_NSAUTORELEASE_POOL_H_

#include "base/base_export.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/threading/thread_checker.h"

namespace base::apple {

// ScopedNSAutoreleasePool creates an autorelease pool when instantiated and
// pops it when destroyed. This allows an autorelease pool to be maintained in
// ordinary C++ code without bringing in any direct Objective-C dependency.
//
// Before using, please be aware that the semantics of autorelease pools do not
// match the semantics of a C++ class. In particular, recycling or destructing a
// pool lower on the stack destroys all pools higher on the stack, which does
// not mesh well with the existence of C++ objects for each pool.
//
// TODO(https://crbug.com/1424190): Enforce stack-only use via the
// STACK_ALLOCATED annotation.
//
// Use this class only in C++ code; use @autoreleasepool in Obj-C(++) code.

class BASE_EXPORT ScopedNSAutoreleasePool {
public:
ScopedNSAutoreleasePool();

ScopedNSAutoreleasePool(const ScopedNSAutoreleasePool&) = delete;
ScopedNSAutoreleasePool& operator=(const ScopedNSAutoreleasePool&) = delete;
ScopedNSAutoreleasePool(ScopedNSAutoreleasePool&&) = delete;
ScopedNSAutoreleasePool& operator=(ScopedNSAutoreleasePool&&) = delete;

~ScopedNSAutoreleasePool();

// Clear out the pool in case its position on the stack causes it to be alive
// for long periods of time (such as the entire length of the app). Only use
// then when you're certain the items currently in the pool are no longer
// needed.
void Recycle();

private:
// This field is not a raw_ptr<> because it is a pointer to an Objective-C
// object.
RAW_PTR_EXCLUSION void* autorelease_pool_ GUARDED_BY_CONTEXT(thread_checker_);

THREAD_CHECKER(thread_checker_);
};

} // namespace base::apple

#endif // BASE_APPLE_SCOPED_NSAUTORELEASE_POOL_H_
51 changes: 9 additions & 42 deletions base/mac/scoped_nsautorelease_pool.h
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
// Copyright 2011 The Chromium Authors
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_
#define BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_

#include "base/base_export.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/threading/thread_checker.h"
#include "base/apple/scoped_nsautorelease_pool.h"

// This is a forwarding header so that Crashpad can continue to build correctly
// until mini_chromium and then it are updated and rolled.

// TODO(https://crbug.com/1444927): Update mini_chromium, update Crashpad, roll
// Crashpad, and then delete this forwarding header.

namespace base::mac {

// ScopedNSAutoreleasePool creates an autorelease pool when instantiated and
// pops it when destroyed. This allows an autorelease pool to be maintained in
// ordinary C++ code without bringing in any direct Objective-C dependency.
//
// Before using, please be aware that the semantics of autorelease pools do not
// match the semantics of a C++ class. In particular, recycling or destructing a
// pool lower on the stack destroys all pools higher on the stack, which does
// not mesh well with the existence of C++ objects for each pool.
//
// TODO(https://crbug.com/1424190): Enforce stack-only use via the
// STACK_ALLOCATED annotation.
//
// Use this class only in C++ code; use @autoreleasepool in Obj-C(++) code.

class BASE_EXPORT ScopedNSAutoreleasePool {
public:
ScopedNSAutoreleasePool();

ScopedNSAutoreleasePool(const ScopedNSAutoreleasePool&) = delete;
ScopedNSAutoreleasePool& operator=(const ScopedNSAutoreleasePool&) = delete;
ScopedNSAutoreleasePool(ScopedNSAutoreleasePool&&) = delete;
ScopedNSAutoreleasePool& operator=(ScopedNSAutoreleasePool&&) = delete;

~ScopedNSAutoreleasePool();

// Clear out the pool in case its position on the stack causes it to be alive
// for long periods of time (such as the entire length of the app). Only use
// then when you're certain the items currently in the pool are no longer
// needed.
void Recycle();

private:
// This field is not a raw_ptr<> because it is a pointer to an Objective-C
// object.
RAW_PTR_EXCLUSION void* autorelease_pool_ GUARDED_BY_CONTEXT(thread_checker_);

THREAD_CHECKER(thread_checker_);
};
using ScopedNSAutoreleasePool = base::apple::ScopedNSAutoreleasePool;

} // namespace base::mac

Expand Down
4 changes: 2 additions & 2 deletions base/message_loop/message_pump_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include <memory>

#include "base/apple/call_with_eh_frame.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#include "base/auto_reset.h"
#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_policy.h"
#include "base/metrics/histogram_samples.h"
Expand Down Expand Up @@ -86,7 +86,7 @@ explicit OptionalAutoreleasePool(MessagePumpCFRunLoopBase* pump) {
OptionalAutoreleasePool& operator=(const OptionalAutoreleasePool&) = delete;

private:
absl::optional<base::mac::ScopedNSAutoreleasePool> pool_;
absl::optional<base::apple::ScopedNSAutoreleasePool> pool_;
};

class MessagePumpCFRunLoopBase::ScopedModeEnabler {
Expand Down
4 changes: 2 additions & 2 deletions base/message_loop/message_pump_default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "base/apple/mach_logging.h"
#include "base/apple/scoped_mach_port.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#include "base/threading/threading_features.h"
#endif

Expand All @@ -34,7 +34,7 @@ void MessagePumpDefault::Run(Delegate* delegate) {

for (;;) {
#if BUILDFLAG(IS_APPLE)
mac::ScopedNSAutoreleasePool autorelease_pool;
apple::ScopedNSAutoreleasePool autorelease_pool;
#endif

Delegate::NextWorkInfo next_work_info = delegate->DoWork();
Expand Down
6 changes: 3 additions & 3 deletions base/message_loop/message_pump_kqueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <atomic>

#include "base/apple/mach_logging.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#include "base/auto_reset.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/notreached.h"
#include "base/posix/eintr_wrapper.h"
#include "base/time/time_override.h"
Expand Down Expand Up @@ -162,7 +162,7 @@ void MessagePumpKqueue::Run(Delegate* delegate) {
RunSimplified(delegate);
} else {
while (keep_running_) {
mac::ScopedNSAutoreleasePool pool;
apple::ScopedNSAutoreleasePool pool;

bool do_more_work = DoInternalWork(delegate, nullptr);
if (!keep_running_)
Expand Down Expand Up @@ -195,7 +195,7 @@ void MessagePumpKqueue::RunSimplified(Delegate* delegate) {
DoInternalWork(delegate, nullptr);

while (keep_running_) {
mac::ScopedNSAutoreleasePool pool;
apple::ScopedNSAutoreleasePool pool;

Delegate::NextWorkInfo next_work_info = delegate->DoWork();
if (!keep_running_)
Expand Down
4 changes: 2 additions & 2 deletions base/task/thread_pool/worker_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#endif

#if BUILDFLAG(IS_APPLE)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif

#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && \
Expand Down Expand Up @@ -442,7 +442,7 @@ void WorkerThread::RunWorker() {
bool got_work_this_wakeup = false;
while (!ShouldExit()) {
#if BUILDFLAG(IS_APPLE)
mac::ScopedNSAutoreleasePool autorelease_pool;
apple::ScopedNSAutoreleasePool autorelease_pool;
#endif
absl::optional<WatchHangsInScope> hang_watch_scope;
if (watch_for_hangs)
Expand Down
2 changes: 1 addition & 1 deletion base/test/launcher/test_launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
#endif

#if BUILDFLAG(IS_APPLE)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif

#if BUILDFLAG(IS_WIN)
Expand Down
4 changes: 2 additions & 2 deletions base/test/test_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#include "testing/multiprocess_func_list.h"

#if BUILDFLAG(IS_APPLE)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif // BUILDFLAG(IS_APPLE)

#if BUILDFLAG(IS_IOS)
Expand Down Expand Up @@ -363,7 +363,7 @@ TestSuite::~TestSuite() {
// Initialize(). See bug 6436.
int TestSuite::Run() {
#if BUILDFLAG(IS_APPLE)
mac::ScopedNSAutoreleasePool scoped_pool;
apple::ScopedNSAutoreleasePool scoped_pool;
#endif

std::string client_func =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#include "third_party/widevine/cdm/buildflags.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif

#if BUILDFLAG(ENABLE_PLUGINS)
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/extensions/app_background_page_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#endif

#if BUILDFLAG(IS_MAC)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif

using extensions::Extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#include "services/network/test/test_utils.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_LACROS)
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/sessions/session_restore_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
#include "ui/gfx/color_palette.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#endif

#if defined(USE_AURA)
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ui/browser_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
#include "ui/base/ui_base_features.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#include "chrome/browser/ui/cocoa/test/run_loop_testing.h"
#include "ui/accelerated_widget_mac/ca_transaction_observer.h"
#include "ui/base/test/scoped_fake_nswindow_fullscreen.h"
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h"

#include "base/apple/scoped_nsautorelease_pool.h"
#include "base/containers/adapters.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
Expand Down Expand Up @@ -414,7 +414,7 @@ - (void)windowMainStatusChanged:(NSNotification*)notification {
// must be drained before the window finishes -dealloc. In this method, an
// autorelease is sent by the invocation of [NSApp windows].
// http://crbug.com/406944.
base::mac::ScopedNSAutoreleasePool pool;
base::apple::ScopedNSAutoreleasePool pool;

NSString* name = [notification name];
if ([name isEqualToString:NSWindowDidBecomeMainNotification]) {
Expand Down
4 changes: 2 additions & 2 deletions chrome/test/base/chrome_test_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

#if BUILDFLAG(IS_MAC)
#include "base/apple/bundle_locations.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#include "chrome/browser/chrome_browser_application_mac.h"
#endif

Expand Down Expand Up @@ -71,7 +71,7 @@ ChromeTestSuite::~ChromeTestSuite() = default;

void ChromeTestSuite::Initialize() {
#if BUILDFLAG(IS_MAC)
base::mac::ScopedNSAutoreleasePool autorelease_pool;
base::apple::ScopedNSAutoreleasePool autorelease_pool;
chrome_browser_application_mac::RegisterBrowserCrApp();
#endif

Expand Down
4 changes: 2 additions & 2 deletions chrome/test/base/in_process_browser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
#include "ui/base/ui_base_features.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/apple/scoped_nsautorelease_pool.h"
#include "chrome/test/base/scoped_bundle_swizzler_mac.h"
#include "services/device/public/cpp/test/fake_geolocation_manager.h"
#endif
Expand Down Expand Up @@ -854,7 +854,7 @@ void InProcessBrowserTest::PreRunTestOnMainThread() {
// deallocation via an autorelease pool (such as browser window closure and
// browser shutdown). To avoid this, the following pool is recycled after each
// time code is directly executed.
autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool;
autorelease_pool_ = new base::apple::ScopedNSAutoreleasePool;
#endif

// Pump any pending events that were created as a result of creating a
Expand Down

0 comments on commit 5289ab2

Please sign in to comment.