OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Static class for hooking Win32 API routines. For now, | 5 // Static class for hooking Win32 API routines. For now, |
6 // we only add one watcher at a time. | 6 // we only add one watcher at a time. |
7 // | 7 // |
8 // TODO(mbelshe): Support multiple watchers. | 8 // TODO(mbelshe): Support multiple watchers. |
9 | 9 |
10 #ifndef MEMORY_WATCHER_MEMORY_HOOK_ | 10 #ifndef MEMORY_WATCHER_MEMORY_HOOK_ |
(...skipping 17 matching lines...) Expand all Loading... |
28 typedef T* pointer; | 28 typedef T* pointer; |
29 typedef const T* const_pointer; | 29 typedef const T* const_pointer; |
30 typedef T& reference; | 30 typedef T& reference; |
31 typedef const T& const_reference; | 31 typedef const T& const_reference; |
32 typedef T value_type; | 32 typedef T value_type; |
33 | 33 |
34 PrivateHookAllocator() {} | 34 PrivateHookAllocator() {} |
35 | 35 |
36 // Allocate memory for STL. | 36 // Allocate memory for STL. |
37 pointer allocate(size_type n, const void * = 0) { | 37 pointer allocate(size_type n, const void * = 0) { |
38 return reinterpret_cast<T*>(MemoryHook::Alloc(n * sizeof(T))); | 38 return reinterpret_cast<T*>(MemoryHook::Alloc(n * sizeof(T))); |
39 } | 39 } |
40 | 40 |
41 // Deallocate memory for STL. | 41 // Deallocate memory for STL. |
42 void deallocate(void* p, size_type) { | 42 void deallocate(void* p, size_type) { |
43 if (p) | 43 if (p) |
44 MemoryHook::Free(p); | 44 MemoryHook::Free(p); |
45 } | 45 } |
46 | 46 |
47 // Construct the object | 47 // Construct the object |
48 void construct(pointer p, const T& val) { | 48 void construct(pointer p, const T& val) { |
49 new (reinterpret_cast<T*>(p))T(val); | 49 new (reinterpret_cast<T*>(p))T(val); |
50 } | 50 } |
51 | 51 |
52 // Destruct an object | 52 // Destruct an object |
53 void destroy(pointer p) { p->~T(); } | 53 void destroy(pointer p) { p->~T(); } |
54 | 54 |
55 size_type max_size() const { return size_t(-1); } | 55 size_type max_size() const { return size_t(-1); } |
56 | 56 |
57 template <class U> | 57 template <class U> |
58 struct rebind { typedef PrivateHookAllocator<U> other; }; | 58 struct rebind { typedef PrivateHookAllocator<U> other; }; |
59 | 59 |
60 template <class U> | 60 template <class U> |
61 PrivateHookAllocator(const PrivateHookAllocator<U>&) {} | 61 PrivateHookAllocator(const PrivateHookAllocator<U>&) {} |
62 }; | 62 }; |
63 | 63 |
64 // Classes which monitor memory from these hooks implement | 64 // Classes which monitor memory from these hooks implement |
65 // the MemoryObserver interface. | 65 // the MemoryObserver interface. |
66 class MemoryObserver { | 66 class MemoryObserver { |
67 public: | 67 public: |
| 68 virtual ~MemoryObserver() {} |
| 69 |
68 // Track a pointer. Will capture the current StackTrace. | 70 // Track a pointer. Will capture the current StackTrace. |
69 virtual void OnTrack(HANDLE heap, int32 id, int32 size) = 0; | 71 virtual void OnTrack(HANDLE heap, int32 id, int32 size) = 0; |
70 | 72 |
71 // Untrack a pointer, removing it from our list. | 73 // Untrack a pointer, removing it from our list. |
72 virtual void OnUntrack(HANDLE heap, int32 id, int32 size) = 0; | 74 virtual void OnUntrack(HANDLE heap, int32 id, int32 size) = 0; |
73 }; | 75 }; |
74 | 76 |
75 class MemoryHook : MemoryObserver { | 77 class MemoryHook : MemoryObserver { |
76 public: | 78 public: |
77 // Initialize the MemoryHook. Must be called before | 79 // Initialize the MemoryHook. Must be called before |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // Close our private heap. | 131 // Close our private heap. |
130 bool CloseHeap(); | 132 bool CloseHeap(); |
131 | 133 |
132 MemoryObserver* watcher_; | 134 MemoryObserver* watcher_; |
133 HANDLE heap_; // An internal accounting heap. | 135 HANDLE heap_; // An internal accounting heap. |
134 static bool hooked_; | 136 static bool hooked_; |
135 static MemoryHook* global_hook_; | 137 static MemoryHook* global_hook_; |
136 }; | 138 }; |
137 | 139 |
138 #endif // MEMORY_WATCHER_MEMORY_HOOK_ | 140 #endif // MEMORY_WATCHER_MEMORY_HOOK_ |
OLD | NEW |