Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4178)

Unified Diff: base/win/scoped_handle.cc

Issue 10453082: Base: Add a handle verifier to ScopedHandle. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/win/scoped_handle.h ('k') | chrome/service/cloud_print/print_system_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/win/scoped_handle.cc
===================================================================
--- base/win/scoped_handle.cc (revision 0)
+++ base/win/scoped_handle.cc (revision 0)
@@ -0,0 +1,88 @@
+// 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/win/scoped_handle.h"
+
+#include <map>
+#include <set>
+
+#include "base/debug/alias.h"
+#include "base/lazy_instance.h"
+#include "base/synchronization/lock.h"
+#include "base/win/windows_version.h"
+
+namespace {
+
+struct Info {
+ const void* owner;
+ const void* pc;
+ DWORD thread_id;
+};
+typedef std::map<HANDLE, Info> HandleMap;
+
+base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<std::set<const void*> >::Leaky g_owner_set =
+ LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+namespace base {
+namespace win {
+
+// Static.
+void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
+ const void* pc) {
+ if (OSInfo::GetInstance()->version() > VERSION_XP)
+ return;
+
+ // Grab the thread id before the lock.
+ DWORD thread_id = GetCurrentThreadId();
+
+ AutoLock(g_lock.Get());
Nico 2012/06/05 19:58:44 This produces a temporary that lives for only one
+
+ if (handle == INVALID_HANDLE_VALUE) {
+ // Cannot track this handle.
+ g_owner_set.Get().insert(owner);
+ return;
+ }
+
+ Info handle_info = { owner, pc, thread_id };
+ std::pair<HANDLE, Info> item(handle, handle_info);
+ std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item);
+ if (!result.second) {
+ Info other = result.first->second;
+ debug::Alias(&other);
+ CHECK(false);
+ }
+}
+
+// Static.
+void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
+ const void* pc) {
+ if (OSInfo::GetInstance()->version() > VERSION_XP)
+ return;
+
+ AutoLock(g_lock.Get());
+ HandleMap::iterator i = g_handle_map.Get().find(handle);
+ if (i == g_handle_map.Get().end()) {
+ std::set<const void*>::iterator j = g_owner_set.Get().find(owner);
+ if (j != g_owner_set.Get().end()) {
+ g_owner_set.Get().erase(j);
+ return;
+ }
+ CHECK(false);
+ }
+
+ Info other = i->second;
+ if (other.owner != owner) {
+ debug::Alias(&other);
+ CHECK(false);
+ }
+
+ g_handle_map.Get().erase(i);
+}
+
+} // namespace win
+} // namespace base
Property changes on: base\win\scoped_handle.cc
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « base/win/scoped_handle.h ('k') | chrome/service/cloud_print/print_system_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698