1 // Copyright (c) 2006-2008 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.
6 // The MemoryWatcher is a library that can be linked into any
7 // win32 application. It will override the default memory allocators
8 // and track call stacks for any allocations that are made. It can
9 // then be used to see what memory is in use.
11 #ifndef TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_
12 #define TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_
17 #include "base/synchronization/lock.h"
18 #include "tools/memory_watcher/memory_hook.h"
21 class AllocationStack
;
23 // The MemoryWatcher installs allocation hooks and monitors
24 // allocations and frees.
25 class MemoryWatcher
: MemoryObserver
{
33 typedef std::map
<int32
, AllocationStack
*, std::less
<int32
>,
34 PrivateHookAllocator
<int32
> > CallStackMap
;
35 typedef std::map
<int32
, StackTrack
, std::less
<int32
>,
36 PrivateHookAllocator
<int32
> > CallStackIdMap
;
37 typedef std::basic_string
<char, std::char_traits
<char>,
38 PrivateHookAllocator
<char> > PrivateAllocatorString
;
41 virtual ~MemoryWatcher();
43 // Dump all tracked pointers still in use.
46 // MemoryObserver interface.
47 virtual void OnTrack(HANDLE heap
, int32 id
, int32 size
);
48 virtual void OnUntrack(HANDLE heap
, int32 id
, int32 size
);
50 // Sets a name that appears in the generated file name.
51 void SetLogName(char* log_name
);
54 // Opens the logfile which we create.
60 // Hook the memory hooks.
63 // Unhooks our memory hooks.
66 // Check to see if this thread is already processing a block, and should not
68 bool LockedRecursionDetected() const;
70 // This is for logging.
73 bool hooked_
; // True when this class has the memory_hooks hooked.
75 // Either 0, or else the threadID for a thread that is actively working on
76 // a stack track. Used to avoid recursive tracking.
77 DWORD active_thread_id_
;
79 base::Lock block_map_lock_
;
80 // The block_map provides quick lookups based on the allocation
81 // pointer. This is important for having fast round trips through
83 CallStackMap
*block_map_
;
85 // The file name for that log.
86 std::string file_name_
;
88 // An optional name that appears in the log file name (used to differentiate
90 std::string log_name_
;
95 #endif // TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_