diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-25 20:33:42 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-25 20:33:42 +0000 |
commit | 89836e2574a390828deb9bf86f0d169f7aacfd39 (patch) | |
tree | d41fc75cbd5d348051cf66fe12a2bf0c8f9987d4 /base | |
parent | 1a8f5d1d016723fb1fcc49b1fc5290fa1b7f2706 (diff) | |
download | chromium_src-89836e2574a390828deb9bf86f0d169f7aacfd39.zip chromium_src-89836e2574a390828deb9bf86f0d169f7aacfd39.tar.gz chromium_src-89836e2574a390828deb9bf86f0d169f7aacfd39.tar.bz2 |
Provide autorelease pool management on the Mac in all types of message pumps.
Fix a few unit tests to use autorelease pools as needed.
Review URL: http://codereview.chromium.org/3805
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2603 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.xcodeproj/project.pbxproj | 6 | ||||
-rw-r--r-- | base/build/base.vcproj | 4 | ||||
-rw-r--r-- | base/message_pump_default.cc | 3 | ||||
-rw-r--r-- | base/message_pump_libevent.cc | 7 | ||||
-rw-r--r-- | base/message_pump_mac.mm | 13 | ||||
-rw-r--r-- | base/platform_test_mac.mm | 4 | ||||
-rw-r--r-- | base/scoped_nsautorelease_pool.h | 44 | ||||
-rw-r--r-- | base/scoped_nsautorelease_pool.mm | 19 | ||||
-rw-r--r-- | base/sys_info_unittest.cc | 9 |
9 files changed, 99 insertions, 10 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index 7cd3048..4abf9c5 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -82,6 +82,7 @@ 7B85062F0E5B559A00730B43 /* libzlib.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B165D6E0E55081400185273 /* libzlib.a */; }; 7BA355EB0E898C100023C8B9 /* message_pump_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7BA355EA0E898C100023C8B9 /* message_pump_mac.mm */; }; 7BA3562C0E898F880023C8B9 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7BA3562B0E898F7B0023C8B9 /* AppKit.framework */; }; + 7BA35DD30E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7BA35DD20E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm */; }; 7BAE30E50E6D939F00C3F750 /* atomicops_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BAE30E10E6D939800C3F750 /* atomicops_unittest.cc */; }; 7BAE30E60E6D939F00C3F750 /* simple_thread_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BAE30E40E6D939800C3F750 /* simple_thread_unittest.cc */; }; 7BAE30E70E6D93A300C3F750 /* simple_thread.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BAE30E20E6D939800C3F750 /* simple_thread.cc */; }; @@ -385,6 +386,8 @@ 7BA355E90E898C100023C8B9 /* message_pump_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = message_pump_mac.h; sourceTree = "<group>"; }; 7BA355EA0E898C100023C8B9 /* message_pump_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = message_pump_mac.mm; sourceTree = "<group>"; }; 7BA3562B0E898F7B0023C8B9 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = "<group>"; }; + 7BA35DD10E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scoped_nsautorelease_pool.h; sourceTree = "<group>"; }; + 7BA35DD20E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = scoped_nsautorelease_pool.mm; sourceTree = "<group>"; }; 7BAE30E10E6D939800C3F750 /* atomicops_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = atomicops_unittest.cc; sourceTree = "<group>"; }; 7BAE30E20E6D939800C3F750 /* simple_thread.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = simple_thread.cc; sourceTree = "<group>"; }; 7BAE30E30E6D939800C3F750 /* simple_thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = simple_thread.h; sourceTree = "<group>"; }; @@ -922,6 +925,8 @@ 8254035F0D92D27C0006B936 /* revocable_store.h */, E49F1A5D0E4CD6E200386AEC /* run_all_unittests.cc */, 7B5AD60D0D9DD8050012BCF1 /* scoped_cftyperef.h */, + 7BA35DD10E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.h */, + 7BA35DD20E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm */, 825403610D92D27C0006B936 /* scoped_ptr.h */, 7BD8F4CF0E65B4A900034DE9 /* scoped_ptr_unittest.cc */, 825403620D92D27C0006B936 /* sha2.cc */, @@ -1343,6 +1348,7 @@ 824654DF0DC26521007C2BAA /* prtime.cc in Sources */, 7B836C050E55BBB800F6AD31 /* ref_counted.cc in Sources */, 8246548C0DC259DB007C2BAA /* revocable_store.cc in Sources */, + 7BA35DD30E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm in Sources */, ABF4B9BC0DC2BD1000A6E319 /* sha2.cc in Sources */, ABF4B9BE0DC2BD1500A6E319 /* sha512.cc in Sources */, 824653740DC12D0E007C2BAA /* shared_memory_posix.cc in Sources */, diff --git a/base/build/base.vcproj b/base/build/base.vcproj index c80e372..c188a0f 100644 --- a/base/build/base.vcproj +++ b/base/build/base.vcproj @@ -594,6 +594,10 @@ > </File> <File + RelativePath="..\scoped_nsautorelease_pool.h" + > + </File> + <File RelativePath="..\scoped_ptr.h" > </File> diff --git a/base/message_pump_default.cc b/base/message_pump_default.cc index 2fa5d94..d1ecd12 100644 --- a/base/message_pump_default.cc +++ b/base/message_pump_default.cc @@ -5,6 +5,7 @@ #include "base/message_pump_default.h" #include "base/logging.h" +#include "base/scoped_nsautorelease_pool.h" namespace base { @@ -17,6 +18,8 @@ void MessagePumpDefault::Run(Delegate* delegate) { DCHECK(keep_running_) << "Quit must have been called outside of Run!"; for (;;) { + ScopedNSAutoreleasePool autorelease_pool; + bool did_work = delegate->DoWork(); if (!keep_running_) break; diff --git a/base/message_pump_libevent.cc b/base/message_pump_libevent.cc index 38e7db6..12cb6d1 100644 --- a/base/message_pump_libevent.cc +++ b/base/message_pump_libevent.cc @@ -4,12 +4,13 @@ #include "base/message_pump_libevent.h" +#include <fcntl.h> + #include "base/logging.h" +#include "base/scoped_nsautorelease_pool.h" #include "base/time.h" #include "third_party/libevent/event.h" -#include <fcntl.h> - namespace base { // Return 0 on success @@ -112,6 +113,8 @@ void MessagePumpLibevent::Run(Delegate* delegate) { in_run_ = true; for (;;) { + ScopedNSAutoreleasePool autorelease_pool; + bool did_work = delegate->DoWork(); if (!keep_running_) break; diff --git a/base/message_pump_mac.mm b/base/message_pump_mac.mm index 495f828..1a8f7b5 100644 --- a/base/message_pump_mac.mm +++ b/base/message_pump_mac.mm @@ -8,6 +8,8 @@ #import <Foundation/Foundation.h> #include <float.h> +#include "base/scoped_nsautorelease_pool.h" + namespace { void NoOp(void* info) { @@ -216,7 +218,13 @@ void MessagePumpCFRunLoop::DoRun(Delegate* delegate) { int last_innermost_quittable = innermost_quittable_; innermost_quittable_ = nesting_level_ + 1; - CFRunLoopRun(); + // This is completely identical to calling CFRunLoopRun(), except autorelease + // pool management is introduced. + int result; + do { + ScopedNSAutoreleasePool autorelease_pool; + result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, DBL_MAX, false); + } while (result != kCFRunLoopRunStopped && result != kCFRunLoopRunFinished); // Restore the previous state of the object. innermost_quittable_ = last_innermost_quittable; @@ -315,7 +323,7 @@ void MessagePumpNSApplication::DoRun(Delegate* delegate) { } else { running_own_loop_ = true; while (keep_running_) { - NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init]; + ScopedNSAutoreleasePool autorelease_pool; NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode @@ -323,7 +331,6 @@ void MessagePumpNSApplication::DoRun(Delegate* delegate) { if (event) { [NSApp sendEvent:event]; } - [autorelease_pool drain]; } keep_running_ = true; } diff --git a/base/platform_test_mac.mm b/base/platform_test_mac.mm index 57dc50d..f3802c1 100644 --- a/base/platform_test_mac.mm +++ b/base/platform_test_mac.mm @@ -4,12 +4,12 @@ #include "base/platform_test.h" -#import <Cocoa/Cocoa.h> +#import <Foundation/Foundation.h> void PlatformTest::SetUp() { pool_ = [[NSAutoreleasePool alloc] init]; } void PlatformTest::TearDown() { - [pool_ release]; + [pool_ drain]; } diff --git a/base/scoped_nsautorelease_pool.h b/base/scoped_nsautorelease_pool.h new file mode 100644 index 0000000..dff5362 --- /dev/null +++ b/base/scoped_nsautorelease_pool.h @@ -0,0 +1,44 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_SCOPED_NSAUTORELEASE_POOL_H_ +#define BASE_SCOPED_NSAUTORELEASE_POOL_H_ + +#include "base/basictypes.h" + +#if defined(OS_MACOSX) +#if defined(__OBJC__) +@class NSAutoreleasePool; +#else // __OBJC__ +class NSAutoreleasePool; +#endif // __OBJC__ +#endif // OS_MACOSX + +namespace base { + +// On the Mac, ScopedNSAutoreleasePool allocates an NSAutoreleasePool when +// instantiated and sends it a -drain message when destroyed. This allows an +// autorelease pool to be maintained in ordinary C++ code without bringing in +// any direct Objective-C dependency. +// +// On other platforms, ScopedNSAutoreleasePool is an empty object with no +// effects. This allows it to be used directly in cross-platform code without +// ugly #ifdefs. +class ScopedNSAutoreleasePool { +#if defined(OS_MACOSX) + public: + ScopedNSAutoreleasePool(); + ~ScopedNSAutoreleasePool(); + + private: + NSAutoreleasePool* autorelease_pool_; +#endif // OS_MACOSX + + private: + DISALLOW_COPY_AND_ASSIGN(ScopedNSAutoreleasePool); +}; + +} // namespace base + +#endif // BASE_SCOPED_NSAUTORELEASE_POOL_H_ diff --git a/base/scoped_nsautorelease_pool.mm b/base/scoped_nsautorelease_pool.mm new file mode 100644 index 0000000..c4ae517 --- /dev/null +++ b/base/scoped_nsautorelease_pool.mm @@ -0,0 +1,19 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/scoped_nsautorelease_pool.h" + +#import <Foundation/Foundation.h> + +namespace base { + +ScopedNSAutoreleasePool::ScopedNSAutoreleasePool() + : autorelease_pool_([[NSAutoreleasePool alloc] init]) { +} + +ScopedNSAutoreleasePool::~ScopedNSAutoreleasePool() { + [autorelease_pool_ drain]; +} + +} // namespace base diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc index c10b1b7..3ad2ae0 100644 --- a/base/sys_info_unittest.cc +++ b/base/sys_info_unittest.cc @@ -3,21 +3,24 @@ // found in the LICENSE file. #include "base/file_util.h" +#include "base/platform_test.h" #include "base/sys_info.h" #include "testing/gtest/include/gtest/gtest.h" -TEST(SysInfoTest, NumProcs) { +typedef PlatformTest SysInfoTest; + +TEST_F(SysInfoTest, NumProcs) { // We aren't actually testing that it's correct, just that it's sane. EXPECT_GE(base::SysInfo::NumberOfProcessors(), 1); } -TEST(SysInfoTest, AmountOfMem) { +TEST_F(SysInfoTest, AmountOfMem) { // We aren't actually testing that it's correct, just that it's sane. EXPECT_GT(base::SysInfo::AmountOfPhysicalMemory(), 0); EXPECT_GT(base::SysInfo::AmountOfPhysicalMemoryMB(), 0); } -TEST(SysInfoTest, AmountOfFreeDiskSpace) { +TEST_F(SysInfoTest, AmountOfFreeDiskSpace) { // We aren't actually testing that it's correct, just that it's sane. std::wstring tmp_path; ASSERT_TRUE(file_util::GetTempDir(&tmp_path)); |