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.
5 #include "base/win/scoped_handle.h"
9 #include "base/debug/alias.h"
10 #include "base/lazy_instance.h"
11 #include "base/synchronization/lock.h"
12 #include "base/win/windows_version.h"
22 typedef std::map
<HANDLE
, Info
> HandleMap
;
24 base::LazyInstance
<HandleMap
>::Leaky g_handle_map
= LAZY_INSTANCE_INITIALIZER
;
25 base::LazyInstance
<base::Lock
>::Leaky g_lock
= LAZY_INSTANCE_INITIALIZER
;
33 void VerifierTraits::StartTracking(HANDLE handle
, const void* owner
,
34 const void* pc1
, const void* pc2
) {
35 // Grab the thread id before the lock.
36 DWORD thread_id
= GetCurrentThreadId();
38 AutoLock
lock(g_lock
.Get());
40 Info handle_info
= { owner
, pc1
, pc2
, thread_id
};
41 std::pair
<HANDLE
, Info
> item(handle
, handle_info
);
42 std::pair
<HandleMap::iterator
, bool> result
= g_handle_map
.Get().insert(item
);
44 Info other
= result
.first
->second
;
51 void VerifierTraits::StopTracking(HANDLE handle
, const void* owner
,
52 const void* pc1
, const void* pc2
) {
53 AutoLock
lock(g_lock
.Get());
54 HandleMap::iterator i
= g_handle_map
.Get().find(handle
);
55 if (i
== g_handle_map
.Get().end())
58 Info other
= i
->second
;
59 if (other
.owner
!= owner
) {
64 g_handle_map
.Get().erase(i
);