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

Side by Side 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, 6 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 unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/win/scoped_handle.h"
6
7 #include <map>
8 #include <set>
9
10 #include "base/debug/alias.h"
11 #include "base/lazy_instance.h"
12 #include "base/synchronization/lock.h"
13 #include "base/win/windows_version.h"
14
15 namespace {
16
17 struct Info {
18 const void* owner;
19 const void* pc;
20 DWORD thread_id;
alexeypa (please no reviews) 2012/05/30 20:19:30 I think |pc| and |thread_id| can be hosted inside
21 };
22 typedef std::map<HANDLE, Info> HandleMap;
23
24 base::LazyInstance<HandleMap> g_handle_map = LAZY_INSTANCE_INITIALIZER;
25 base::LazyInstance<std::set<const void*> > g_owner_set =
alexeypa (please no reviews) 2012/05/30 20:19:30 |g_owner_set| is a way too heavy for the job it is
rvargas (doing something else) 2012/05/30 21:39:01 Background: this is not a long term feature (it sh
26 LAZY_INSTANCE_INITIALIZER;
27 base::LazyInstance<base::Lock> g_lock = LAZY_INSTANCE_INITIALIZER;
28
29 } // namespace
30
31 namespace base {
32 namespace win {
33
34 // Static.
35 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
36 const void* pc) {
37 if (OSInfo::GetInstance()->version() > VERSION_XP)
38 return;
39
40 // Grab the thread id before the lock.
41 DWORD thread_id = GetCurrentThreadId();
42
43 AutoLock(g_lock.Get());
44
45 if (handle == INVALID_HANDLE_VALUE) {
46 // Cannot track this handle.
47 g_owner_set.Get().insert(owner);
rvargas (doing something else) 2012/05/30 22:12:33 I still hope this is not the common case.
48 return;
49 }
50
51 HandleMap::iterator i = g_handle_map.Get().find(handle);
alexeypa (please no reviews) 2012/05/30 20:19:30 I think a simple linked list would be much simpler
52 if (i != g_handle_map.Get().end()) {
53 Info other = i->second;
54 debug::Alias(&other);
55 CHECK(false);
56 }
57
58 Info handle_info = { owner, pc, thread_id };
59 g_handle_map.Get()[handle] = handle_info;
alexeypa (please no reviews) 2012/05/30 20:19:30 This will do a second lookup in the map. it is bet
rvargas (doing something else) 2012/05/30 22:12:33 Done.
60 }
61
62 // Static.
63 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
64 const void* pc) {
65 if (OSInfo::GetInstance()->version() > VERSION_XP)
66 return;
67
68 AutoLock(g_lock.Get());
69 HandleMap::iterator i = g_handle_map.Get().find(handle);
70 if (i == g_handle_map.Get().end()) {
71 std::set<const void*>::iterator j = g_owner_set.Get().find(owner);
72 if (j != g_owner_set.Get().end()) {
73 g_owner_set.Get().erase(j);
74 return;
75 }
76 CHECK(false);
77 }
78
79 Info other = i->second;
80 if (other.owner != owner) {
81 debug::Alias(&other);
82 CHECK(false);
83 }
84
85 g_handle_map.Get().erase(i);
86 }
87
88 } // namespace win
89 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698