0

Fix some potential Mach port leaks from mach_host_self using a new ScopedMachPort class.

BUG=119379
TEST=none


Review URL: https://chromiumcodereview.appspot.com/11188003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162225 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
rsesek@chromium.org
2012-10-16 20:17:58 +00:00
parent 6361c61b55
commit 431d0a1458
6 changed files with 73 additions and 3 deletions

@ -216,6 +216,8 @@
'mac/scoped_cftyperef.h',
'mac/scoped_ioobject.h',
'mac/scoped_launch_data.h',
'mac/scoped_mach_port.cc',
'mac/scoped_mach_port.h',
'mac/scoped_nsautorelease_pool.h',
'mac/scoped_nsautorelease_pool.mm',
'mac/scoped_nsexception_enabler.h',
@ -620,6 +622,7 @@
['include', '^mac/foundation_util\\.'],
['include', '^mac/mac_logging\\.'],
['include', '^mac/objc_property_releaser\\.'],
['include', '^mac/scoped_mach_port\\.'],
['include', '^mac/scoped_nsautorelease_pool\\.'],
['include', '^message_pump_mac\\.'],
['include', '^threading/platform_thread_mac\\.'],

@ -0,0 +1,20 @@
// Copyright (c) 2012 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/mac/scoped_mach_port.h"
namespace base {
namespace mac {
ScopedMachPort::ScopedMachPort(mach_port_t port) : port_(port) {
}
ScopedMachPort::~ScopedMachPort() {
if (port_ != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), port_);
}
}
} // namespace mac
} // namespace base

@ -0,0 +1,42 @@
// Copyright (c) 2012 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_MACH_PORT_H_
#define BASE_MAC_SCOPED_MACH_PORT_H_
#include <mach/mach.h>
#include "base/basictypes.h"
#include "base/base_export.h"
namespace base {
namespace mac {
// A class for managing the life of a Mach port, releasing via
// mach_port_deallocate either its send and/or receive rights.
class BASE_EXPORT ScopedMachPort {
public:
// Creates a scoper by taking ownership of the port.
explicit ScopedMachPort(mach_port_t port);
~ScopedMachPort();
operator mach_port_t() const {
return port_;
}
mach_port_t get() const {
return port_;
}
private:
mach_port_t port_;
DISALLOW_COPY_AND_ASSIGN(ScopedMachPort);
};
} // namespace mac
} // namespace base
#endif // BASE_MAC_SCOPED_MACH_PORT_H_

@ -34,6 +34,7 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_mach_port.h"
#include "base/string_util.h"
#include "base/sys_info.h"
#include "base/threading/thread_local.h"
@ -468,7 +469,7 @@ mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const {
// Bytes committed by the system.
size_t GetSystemCommitCharge() {
host_name_port_t host = mach_host_self();
base::mac::ScopedMachPort host(mach_host_self());
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
vm_statistics_data_t data;
kern_return_t kr = host_statistics(host, HOST_VM_INFO,

@ -10,6 +10,7 @@
#include <sys/types.h>
#include "base/logging.h"
#include "base/mac/scoped_mach_port.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/sys_string_conversions.h"
@ -55,7 +56,8 @@ void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
int64 SysInfo::AmountOfPhysicalMemory() {
struct host_basic_info hostinfo;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
int result = host_info(mach_host_self(),
base::mac::ScopedMachPort host(mach_host_self());
int result = host_info(host,
HOST_BASIC_INFO,
reinterpret_cast<host_info_t>(&hostinfo),
&count);

@ -12,6 +12,7 @@
#include <sys/types.h>
#include "base/logging.h"
#include "base/mac/scoped_mach_port.h"
#include "base/stringprintf.h"
namespace base {
@ -44,7 +45,8 @@ void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
int64 SysInfo::AmountOfPhysicalMemory() {
struct host_basic_info hostinfo;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
int result = host_info(mach_host_self(),
base::mac::ScopedMachPort host(mach_host_self());
int result = host_info(host,
HOST_BASIC_INFO,
reinterpret_cast<host_info_t>(&hostinfo),
&count);