Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / tools / memory_watcher / memory_watcher.h
blob8f5f1c2eb6fd5dfe0c0478cec7099d53cf760397
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.
5 // MemoryWatcher.
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_
14 #include <map>
15 #include <functional>
17 #include "base/synchronization/lock.h"
18 #include "tools/memory_watcher/memory_hook.h"
20 class CallStack;
21 class AllocationStack;
23 // The MemoryWatcher installs allocation hooks and monitors
24 // allocations and frees.
25 class MemoryWatcher : MemoryObserver {
26 public:
27 struct StackTrack {
28 CallStack* stack;
29 int count;
30 int size;
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;
40 MemoryWatcher();
41 virtual ~MemoryWatcher();
43 // Dump all tracked pointers still in use.
44 void DumpLeaks();
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);
53 private:
54 // Opens the logfile which we create.
55 void OpenLogFile();
57 // Close the logfile.
58 void CloseLogFile();
60 // Hook the memory hooks.
61 void Hook();
63 // Unhooks our memory hooks.
64 void Unhook();
66 // Check to see if this thread is already processing a block, and should not
67 // recurse.
68 bool LockedRecursionDetected() const;
70 // This is for logging.
71 FILE* file_;
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
82 // malloc/free.
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
89 // logs).
90 std::string log_name_;
95 #endif // TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_