1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-18 12:28:31 +01:00

Add ScopedNSAutoreleasePool from Chromium.

This commit is contained in:
John Maguire 2012-01-31 13:54:03 +01:00
parent 1434cf0115
commit 3e65d3d5f0
7 changed files with 74 additions and 11 deletions

1
debian/copyright vendored
View File

@ -47,6 +47,7 @@ License: GPL-2+
Files: src/core/scoped_nsobject.h Files: src/core/scoped_nsobject.h
src/core/scoped_cftyperef.h src/core/scoped_cftyperef.h
src/core/scoped_nsautorelease_pool.*
Copyright: 2011, The Chromium Authors Copyright: 2011, The Chromium Authors
License: BSD-Google License: BSD-Google

View File

@ -728,6 +728,7 @@ optional_source(APPLE
core/macfslistener.mm core/macfslistener.mm
core/macglobalshortcutbackend.mm core/macglobalshortcutbackend.mm
core/mac_startup.mm core/mac_startup.mm
core/scoped_nsautorelease_pool.mm
devices/macdevicelister.mm devices/macdevicelister.mm
ui/globalshortcutgrabber.mm ui/globalshortcutgrabber.mm
ui/macscreensaver.cpp ui/macscreensaver.cpp

View File

@ -21,7 +21,6 @@
#import <AppKit/NSNibDeclarations.h> #import <AppKit/NSNibDeclarations.h>
#import <AppKit/NSViewController.h> #import <AppKit/NSViewController.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSBundle.h> #import <Foundation/NSBundle.h>
#import <Foundation/NSError.h> #import <Foundation/NSError.h>
#import <Foundation/NSFileManager.h> #import <Foundation/NSFileManager.h>
@ -47,6 +46,7 @@
#include "utilities.h" #include "utilities.h"
#include "core/logging.h" #include "core/logging.h"
#include "core/scoped_cftyperef.h" #include "core/scoped_cftyperef.h"
#include "core/scoped_nsautorelease_pool.h"
#ifdef HAVE_SPARKLE #ifdef HAVE_SPARKLE
#import <Sparkle/SUUpdater.h> #import <Sparkle/SUUpdater.h>
@ -85,7 +85,7 @@ static bool BreakpadCallback(int, int, mach_port_t, void*) {
} }
static BreakpadRef InitBreakpad() { static BreakpadRef InitBreakpad() {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; ScopedNSAutoreleasePool pool;
BreakpadRef breakpad = nil; BreakpadRef breakpad = nil;
NSDictionary* plist = [[NSBundle mainBundle] infoDictionary]; NSDictionary* plist = [[NSBundle mainBundle] infoDictionary];
if (plist) { if (plist) {
@ -236,7 +236,7 @@ static BreakpadRef InitBreakpad() {
namespace mac { namespace mac {
void MacMain() { void MacMain() {
[[NSAutoreleasePool alloc] init]; ScopedNSAutoreleasePool pool;
// Creates and sets the magic global variable so QApplication will find it. // Creates and sets the magic global variable so QApplication will find it.
[MacApplication sharedApplication]; [MacApplication sharedApplication];
#ifdef HAVE_SPARKLE #ifdef HAVE_SPARKLE
@ -273,8 +273,7 @@ QString GetResourcesPath() {
} }
QString GetApplicationSupportPath() { QString GetApplicationSupportPath() {
NSAutoreleasePool* pool = [NSAutoreleasePool alloc]; ScopedNSAutoreleasePool pool;
[pool init];
NSArray* paths = NSSearchPathForDirectoriesInDomains( NSArray* paths = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSApplicationSupportDirectory,
NSUserDomainMask, NSUserDomainMask,
@ -286,13 +285,11 @@ QString GetApplicationSupportPath() {
} else { } else {
ret = "~/Library/Application Support"; ret = "~/Library/Application Support";
} }
[pool drain];
return ret; return ret;
} }
QString GetMusicDirectory() { QString GetMusicDirectory() {
NSAutoreleasePool* pool = [NSAutoreleasePool alloc]; ScopedNSAutoreleasePool pool;
[pool init];
NSArray* paths = NSSearchPathForDirectoriesInDomains( NSArray* paths = NSSearchPathForDirectoriesInDomains(
NSMusicDirectory, NSMusicDirectory,
NSUserDomainMask, NSUserDomainMask,
@ -304,7 +301,6 @@ QString GetMusicDirectory() {
} else { } else {
ret = "~/Music"; ret = "~/Music";
} }
[pool drain];
return ret; return ret;
} }

View File

@ -0,0 +1,38 @@
// Copyright (c) 2011 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_MAC_SCOPED_NSAUTORELEASE_POOL_H_
#define BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_
#include <QObject>
#if defined(__OBJC__)
@class NSAutoreleasePool;
#else // __OBJC__
class NSAutoreleasePool;
#endif // __OBJC__
// 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.
class ScopedNSAutoreleasePool {
public:
ScopedNSAutoreleasePool();
~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:
NSAutoreleasePool* autorelease_pool_;
private:
Q_DISABLE_COPY(ScopedNSAutoreleasePool);
};
#endif // BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_

View File

@ -0,0 +1,24 @@
// Copyright (c) 2010 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 "scoped_nsautorelease_pool.h"
#import <Foundation/Foundation.h>
ScopedNSAutoreleasePool::ScopedNSAutoreleasePool()
: autorelease_pool_([[NSAutoreleasePool alloc] init]) {
Q_ASSERT(autorelease_pool_);
}
ScopedNSAutoreleasePool::~ScopedNSAutoreleasePool() {
[autorelease_pool_ drain];
}
// Cycle the internal pool, allowing everything there to get cleaned up and
// start anew.
void ScopedNSAutoreleasePool::Recycle() {
[autorelease_pool_ drain];
autorelease_pool_ = [[NSAutoreleasePool alloc] init];
Q_ASSERT(autorelease_pool_);
}

View File

@ -20,6 +20,7 @@
#include "mtpconnection.h" #include "mtpconnection.h"
#include "core/logging.h" #include "core/logging.h"
#include "core/scoped_cftyperef.h" #include "core/scoped_cftyperef.h"
#include "core/scoped_nsautorelease_pool.h"
#include "core/scoped_nsobject.h" #include "core/scoped_nsobject.h"
#include <CoreFoundation/CFRunLoop.h> #include <CoreFoundation/CFRunLoop.h>
@ -31,7 +32,6 @@
#include <IOKit/storage/IOCDMedia.h> #include <IOKit/storage/IOCDMedia.h>
#import <AppKit/NSWorkspace.h> #import <AppKit/NSWorkspace.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h> #import <Foundation/NSDictionary.h>
#import <Foundation/NSNotification.h> #import <Foundation/NSNotification.h>
#import <Foundation/NSPathUtilities.h> #import <Foundation/NSPathUtilities.h>
@ -113,7 +113,7 @@ MacDeviceLister::~MacDeviceLister() {
} }
void MacDeviceLister::Init() { void MacDeviceLister::Init() {
[[NSAutoreleasePool alloc] init]; ScopedNSAutoreleasePool pool;
// Populate MTP Device list. // Populate MTP Device list.
if (sMTPDeviceList.empty()) { if (sMTPDeviceList.empty()) {

View File

@ -24,6 +24,8 @@
#import <GrowlApplicationBridge.h> #import <GrowlApplicationBridge.h>
#include "core/scoped_nsautorelease_pool.h"
@interface GrowlInterface :NSObject <GrowlApplicationBridgeDelegate> { @interface GrowlInterface :NSObject <GrowlApplicationBridgeDelegate> {
} }
-(void) SendGrowlAlert:(NSString*)message title:(NSString*)title image:(NSData*)image; -(void) SendGrowlAlert:(NSString*)message title:(NSString*)title image:(NSData*)image;
@ -120,6 +122,7 @@ class OSD::GrowlNotificationWrapper {
private: private:
GrowlInterface* growl_interface_; GrowlInterface* growl_interface_;
ScopedNSAutoreleasePool pool_;
}; };
void OSD::Init() { void OSD::Init() {