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

Side by Side Diff: third_party/tcmalloc/chromium/src/google/malloc_hook.h

Issue 7050034: Merge google-perftools r109 (the current contents of third_party/tcmalloc/vendor) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 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
OLDNEW
1 // Copyright (c) 2005, Google Inc. 1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 // --- 30 // ---
31 // Author: Sanjay Ghemawat 31 // Author: Sanjay Ghemawat
32 // 32 //
33 // Some of our malloc implementations can invoke the following hooks 33 // Some of our malloc implementations can invoke the following hooks whenever
34 // whenever memory is allocated or deallocated. If the hooks are 34 // memory is allocated or deallocated. MallocHook is thread-safe, and things
35 // NULL, they are not invoked. MallocHook is thread-safe, and things 35 // you do before calling AddFooHook(MyHook) are visible to any resulting calls
36 // you do before calling SetFooHook(MyHook) are visible to any 36 // to MyHook. Hooks must be thread-safe. If you write:
37 // resulting calls to MyHook. Hooks must be thread-safe, and if you
38 // write:
39 // 37 //
40 // MallocHook::NewHook old_new_hook_ = NULL; 38 // CHECK(MallocHook::AddNewHook(&MyNewHook));
41 // ...
42 // old_new_hook_ = MallocHook::SetNewHook(&MyNewHook);
43 // 39 //
44 // old_new_hook_ could still be NULL the first couple times MyNewHook 40 // MyNewHook will be invoked in subsequent calls in the current thread, but
45 // is called. 41 // there are no guarantees on when it might be invoked in other threads.
42 //
43 // There are a limited number of slots available for each hook type. Add*Hook
44 // will return false if there are no slots available. Remove*Hook will return
45 // false if the given hook was not already installed.
46 //
47 // The order in which individual hooks are called in Invoke*Hook is undefined.
48 //
49 // It is safe for a hook to remove itself within Invoke*Hook and add other
50 // hooks. Any hooks added inside a hook invocation (for the same hook type)
51 // will not be invoked for the current invocation.
46 // 52 //
47 // One important user of these hooks is the heap profiler. 53 // One important user of these hooks is the heap profiler.
48 // 54 //
49 // CAVEAT: If you add new MallocHook::Invoke* calls (not for chaining hooks), 55 // CAVEAT: If you add new MallocHook::Invoke* calls then those calls must be
50 // then those calls must be directly in the code of the (de)allocation 56 // directly in the code of the (de)allocation function that is provided to the
51 // function that is provided to the user and that function must have 57 // user and that function must have an ATTRIBUTE_SECTION(malloc_hook) attribute.
52 // an ATTRIBUTE_SECTION(malloc_hook) attribute.
53 // 58 //
54 // Note: Get*Hook() and Invoke*Hook() functions are defined in 59 // Note: the Invoke*Hook() functions are defined in malloc_hook-inl.h. If you
55 // malloc_hook-inl.h. If you need to get or invoke a hook (which you 60 // need to invoke a hook (which you shouldn't unless you're part of tcmalloc),
56 // shouldn't unless you're part of tcmalloc), be sure to #include 61 // be sure to #include malloc_hook-inl.h in addition to malloc_hook.h.
57 // malloc_hook-inl.h in addition to malloc_hook.h.
58 // 62 //
59 // NOTE FOR C USERS: If you want to use malloc_hook functionality from 63 // NOTE FOR C USERS: If you want to use malloc_hook functionality from
60 // a C program, #include malloc_hook_c.h instead of this file. 64 // a C program, #include malloc_hook_c.h instead of this file.
61 //
62 // TODO(csilvers): support a non-inlined function called
63 // Assert*HookIs()? This is the context in which I normally see
64 // Get*Hook() called in non-tcmalloc code.
65 65
66 #ifndef _MALLOC_HOOK_H_ 66 #ifndef _MALLOC_HOOK_H_
67 #define _MALLOC_HOOK_H_ 67 #define _MALLOC_HOOK_H_
68 68
69 #include <stddef.h> 69 #include <stddef.h>
70 #include <sys/types.h> 70 #include <sys/types.h>
71 extern "C" { 71 extern "C" {
72 #include <google/malloc_hook_c.h> // a C version of the malloc_hook interface 72 #include <google/malloc_hook_c.h> // a C version of the malloc_hook interface
73 } 73 }
74 74
75 // Annoying stuff for windows -- makes sure clients can import these functions 75 // Annoying stuff for windows -- makes sure clients can import these functions
76 #ifndef PERFTOOLS_DLL_DECL 76 #ifndef PERFTOOLS_DLL_DECL
77 # ifdef _WIN32 77 # ifdef _WIN32
78 # define PERFTOOLS_DLL_DECL __declspec(dllimport) 78 # define PERFTOOLS_DLL_DECL __declspec(dllimport)
79 # else 79 # else
80 # define PERFTOOLS_DLL_DECL 80 # define PERFTOOLS_DLL_DECL
81 # endif 81 # endif
82 #endif 82 #endif
83 83
84 // Note: malloc_hook_c.h defines MallocHook_*Hook and 84 // Note: malloc_hook_c.h defines MallocHook_*Hook and
85 // MallocHook_Set*Hook. The version of these inside the MallocHook 85 // MallocHook_{Add,Remove}*Hook. The version of these inside the MallocHook
86 // class are defined in terms of the malloc_hook_c version. See 86 // class are defined in terms of the malloc_hook_c version. See malloc_hook_c.h
87 // malloc_hook_c.h for details of these types/functions. 87 // for details of these types/functions.
88 88
89 class PERFTOOLS_DLL_DECL MallocHook { 89 class PERFTOOLS_DLL_DECL MallocHook {
90 public: 90 public:
91 // The NewHook is invoked whenever an object is allocated. 91 // The NewHook is invoked whenever an object is allocated.
92 // It may be passed NULL if the allocator returned NULL. 92 // It may be passed NULL if the allocator returned NULL.
93 typedef MallocHook_NewHook NewHook; 93 typedef MallocHook_NewHook NewHook;
94 inline static NewHook GetNewHook(); 94 inline static bool AddNewHook(NewHook hook) {
95 inline static NewHook SetNewHook(NewHook hook) { 95 return MallocHook_AddNewHook(hook);
96 return MallocHook_SetNewHook(hook); 96 }
97 inline static bool RemoveNewHook(NewHook hook) {
98 return MallocHook_RemoveNewHook(hook);
97 } 99 }
98 inline static void InvokeNewHook(const void* p, size_t s); 100 inline static void InvokeNewHook(const void* p, size_t s);
99 101
100 // The DeleteHook is invoked whenever an object is deallocated. 102 // The DeleteHook is invoked whenever an object is deallocated.
101 // It may be passed NULL if the caller is trying to delete NULL. 103 // It may be passed NULL if the caller is trying to delete NULL.
102 typedef MallocHook_DeleteHook DeleteHook; 104 typedef MallocHook_DeleteHook DeleteHook;
103 inline static DeleteHook GetDeleteHook(); 105 inline static bool AddDeleteHook(DeleteHook hook) {
104 inline static DeleteHook SetDeleteHook(DeleteHook hook) { 106 return MallocHook_AddDeleteHook(hook);
105 return MallocHook_SetDeleteHook(hook); 107 }
108 inline static bool RemoveDeleteHook(DeleteHook hook) {
109 return MallocHook_RemoveDeleteHook(hook);
106 } 110 }
107 inline static void InvokeDeleteHook(const void* p); 111 inline static void InvokeDeleteHook(const void* p);
108 112
109 // The PreMmapHook is invoked with mmap or mmap64 arguments just 113 // The PreMmapHook is invoked with mmap or mmap64 arguments just
110 // before the call is actually made. Such a hook may be useful 114 // before the call is actually made. Such a hook may be useful
111 // in memory limited contexts, to catch allocations that will exceed 115 // in memory limited contexts, to catch allocations that will exceed
112 // a memory limit, and take outside actions to increase that limit. 116 // a memory limit, and take outside actions to increase that limit.
113 typedef MallocHook_PreMmapHook PreMmapHook; 117 typedef MallocHook_PreMmapHook PreMmapHook;
114 inline static PreMmapHook GetPreMmapHook(); 118 inline static bool AddPreMmapHook(PreMmapHook hook) {
115 inline static PreMmapHook SetPreMmapHook(PreMmapHook hook) { 119 return MallocHook_AddPreMmapHook(hook);
116 return MallocHook_SetPreMmapHook(hook); 120 }
121 inline static bool RemovePreMmapHook(PreMmapHook hook) {
122 return MallocHook_RemovePreMmapHook(hook);
117 } 123 }
118 inline static void InvokePreMmapHook(const void* start, 124 inline static void InvokePreMmapHook(const void* start,
119 size_t size, 125 size_t size,
120 int protection, 126 int protection,
121 int flags, 127 int flags,
122 int fd, 128 int fd,
123 off_t offset); 129 off_t offset);
124 130
131 // The MmapReplacement is invoked after the PreMmapHook but before
132 // the call is actually made. The MmapReplacement should return true
133 // if it handled the call, or false if it is still necessary to
134 // call mmap/mmap64.
135 // This should be used only by experts, and users must be be
136 // extremely careful to avoid recursive calls to mmap. The replacement
137 // should be async signal safe.
138 // Only one MmapReplacement is supported. After setting an MmapReplacement
139 // you must call RemoveMmapReplacement before calling SetMmapReplacement
140 // again.
141 typedef MallocHook_MmapReplacement MmapReplacement;
142 inline static bool SetMmapReplacement(MmapReplacement hook) {
143 return MallocHook_SetMmapReplacement(hook);
144 }
145 inline static bool RemoveMmapReplacement(MmapReplacement hook) {
146 return MallocHook_RemoveMmapReplacement(hook);
147 }
148 inline static bool InvokeMmapReplacement(const void* start,
149 size_t size,
150 int protection,
151 int flags,
152 int fd,
153 off_t offset,
154 void** result);
155
156
125 // The MmapHook is invoked whenever a region of memory is mapped. 157 // The MmapHook is invoked whenever a region of memory is mapped.
126 // It may be passed MAP_FAILED if the mmap failed. 158 // It may be passed MAP_FAILED if the mmap failed.
127 typedef MallocHook_MmapHook MmapHook; 159 typedef MallocHook_MmapHook MmapHook;
128 inline static MmapHook GetMmapHook(); 160 inline static bool AddMmapHook(MmapHook hook) {
129 inline static MmapHook SetMmapHook(MmapHook hook) { 161 return MallocHook_AddMmapHook(hook);
130 return MallocHook_SetMmapHook(hook); 162 }
163 inline static bool RemoveMmapHook(MmapHook hook) {
164 return MallocHook_RemoveMmapHook(hook);
131 } 165 }
132 inline static void InvokeMmapHook(const void* result, 166 inline static void InvokeMmapHook(const void* result,
133 const void* start, 167 const void* start,
134 size_t size, 168 size_t size,
135 int protection, 169 int protection,
136 int flags, 170 int flags,
137 int fd, 171 int fd,
138 off_t offset); 172 off_t offset);
139 173
174 // The MunmapReplacement is invoked with munmap arguments just before
175 // the call is actually made. The MunmapReplacement should return true
176 // if it handled the call, or false if it is still necessary to
177 // call munmap.
178 // This should be used only by experts. The replacement should be
179 // async signal safe.
180 // Only one MunmapReplacement is supported. After setting an
181 // MunmapReplacement you must call RemoveMunmapReplacement before
182 // calling SetMunmapReplacement again.
183 typedef MallocHook_MunmapReplacement MunmapReplacement;
184 inline static bool SetMunmapReplacement(MunmapReplacement hook) {
185 return MallocHook_SetMunmapReplacement(hook);
186 }
187 inline static bool RemoveMunmapReplacement(MunmapReplacement hook) {
188 return MallocHook_RemoveMunmapReplacement(hook);
189 }
190 inline static bool InvokeMunmapReplacement(const void* p,
191 size_t size,
192 int* result);
193
140 // The MunmapHook is invoked whenever a region of memory is unmapped. 194 // The MunmapHook is invoked whenever a region of memory is unmapped.
141 typedef MallocHook_MunmapHook MunmapHook; 195 typedef MallocHook_MunmapHook MunmapHook;
142 inline static MunmapHook GetMunmapHook(); 196 inline static bool AddMunmapHook(MunmapHook hook) {
143 inline static MunmapHook SetMunmapHook(MunmapHook hook) { 197 return MallocHook_AddMunmapHook(hook);
144 return MallocHook_SetMunmapHook(hook); 198 }
199 inline static bool RemoveMunmapHook(MunmapHook hook) {
200 return MallocHook_RemoveMunmapHook(hook);
145 } 201 }
146 inline static void InvokeMunmapHook(const void* p, size_t size); 202 inline static void InvokeMunmapHook(const void* p, size_t size);
147 203
148 // The MremapHook is invoked whenever a region of memory is remapped. 204 // The MremapHook is invoked whenever a region of memory is remapped.
149 typedef MallocHook_MremapHook MremapHook; 205 typedef MallocHook_MremapHook MremapHook;
150 inline static MremapHook GetMremapHook(); 206 inline static bool AddMremapHook(MremapHook hook) {
151 inline static MremapHook SetMremapHook(MremapHook hook) { 207 return MallocHook_AddMremapHook(hook);
152 return MallocHook_SetMremapHook(hook); 208 }
209 inline static bool RemoveMremapHook(MremapHook hook) {
210 return MallocHook_RemoveMremapHook(hook);
153 } 211 }
154 inline static void InvokeMremapHook(const void* result, 212 inline static void InvokeMremapHook(const void* result,
155 const void* old_addr, 213 const void* old_addr,
156 size_t old_size, 214 size_t old_size,
157 size_t new_size, 215 size_t new_size,
158 int flags, 216 int flags,
159 const void* new_addr); 217 const void* new_addr);
160 218
161 // The PreSbrkHook is invoked just before sbrk is called -- except when 219 // The PreSbrkHook is invoked just before sbrk is called -- except when
162 // the increment is 0. This is because sbrk(0) is often called 220 // the increment is 0. This is because sbrk(0) is often called
163 // to get the top of the memory stack, and is not actually a 221 // to get the top of the memory stack, and is not actually a
164 // memory-allocation call. It may be useful in memory-limited contexts, 222 // memory-allocation call. It may be useful in memory-limited contexts,
165 // to catch allocations that will exceed the limit and take outside 223 // to catch allocations that will exceed the limit and take outside
166 // actions to increase such a limit. 224 // actions to increase such a limit.
167 typedef MallocHook_PreSbrkHook PreSbrkHook; 225 typedef MallocHook_PreSbrkHook PreSbrkHook;
168 inline static PreSbrkHook GetPreSbrkHook(); 226 inline static bool AddPreSbrkHook(PreSbrkHook hook) {
169 inline static PreSbrkHook SetPreSbrkHook(PreSbrkHook hook) { 227 return MallocHook_AddPreSbrkHook(hook);
170 return MallocHook_SetPreSbrkHook(hook); 228 }
229 inline static bool RemovePreSbrkHook(PreSbrkHook hook) {
230 return MallocHook_RemovePreSbrkHook(hook);
171 } 231 }
172 inline static void InvokePreSbrkHook(std::ptrdiff_t increment); 232 inline static void InvokePreSbrkHook(std::ptrdiff_t increment);
173 233
174 // The SbrkHook is invoked whenever sbrk is called -- except when 234 // The SbrkHook is invoked whenever sbrk is called -- except when
175 // the increment is 0. This is because sbrk(0) is often called 235 // the increment is 0. This is because sbrk(0) is often called
176 // to get the top of the memory stack, and is not actually a 236 // to get the top of the memory stack, and is not actually a
177 // memory-allocation call. 237 // memory-allocation call.
178 typedef MallocHook_SbrkHook SbrkHook; 238 typedef MallocHook_SbrkHook SbrkHook;
179 inline static SbrkHook GetSbrkHook(); 239 inline static bool AddSbrkHook(SbrkHook hook) {
180 inline static SbrkHook SetSbrkHook(SbrkHook hook) { 240 return MallocHook_AddSbrkHook(hook);
181 return MallocHook_SetSbrkHook(hook); 241 }
242 inline static bool RemoveSbrkHook(SbrkHook hook) {
243 return MallocHook_RemoveSbrkHook(hook);
182 } 244 }
183 inline static void InvokeSbrkHook(const void* result, std::ptrdiff_t increment ); 245 inline static void InvokeSbrkHook(const void* result, std::ptrdiff_t increment );
184 246
185 // Get the current stack trace. Try to skip all routines up to and 247 // Get the current stack trace. Try to skip all routines up to and
186 // and including the caller of MallocHook::Invoke*. 248 // and including the caller of MallocHook::Invoke*.
187 // Use "skip_count" (similarly to GetStackTrace from stacktrace.h) 249 // Use "skip_count" (similarly to GetStackTrace from stacktrace.h)
188 // as a hint about how many routines to skip if better information 250 // as a hint about how many routines to skip if better information
189 // is not available. 251 // is not available.
190 inline static int GetCallerStackTrace(void** result, int max_depth, 252 inline static int GetCallerStackTrace(void** result, int max_depth,
191 int skip_count) { 253 int skip_count) {
192 return MallocHook_GetCallerStackTrace(result, max_depth, skip_count); 254 return MallocHook_GetCallerStackTrace(result, max_depth, skip_count);
193 } 255 }
194 256
195 // Unhooked versions of mmap() and munmap(). These should be used 257 // Unhooked versions of mmap() and munmap(). These should be used
196 // only by experts, since they bypass heapchecking, etc. 258 // only by experts, since they bypass heapchecking, etc.
259 // Note: These do not run hooks, but they still use the MmapReplacement
260 // and MunmapReplacement.
197 static void* UnhookedMMap(void *start, size_t length, int prot, int flags, 261 static void* UnhookedMMap(void *start, size_t length, int prot, int flags,
198 int fd, off_t offset); 262 int fd, off_t offset);
199 static int UnhookedMUnmap(void *start, size_t length); 263 static int UnhookedMUnmap(void *start, size_t length);
264
265 // The following are DEPRECATED.
266 inline static NewHook GetNewHook();
267 inline static NewHook SetNewHook(NewHook hook) {
268 return MallocHook_SetNewHook(hook);
269 }
270
271 inline static DeleteHook GetDeleteHook();
272 inline static DeleteHook SetDeleteHook(DeleteHook hook) {
273 return MallocHook_SetDeleteHook(hook);
274 }
275
276 inline static PreMmapHook GetPreMmapHook();
277 inline static PreMmapHook SetPreMmapHook(PreMmapHook hook) {
278 return MallocHook_SetPreMmapHook(hook);
279 }
280
281 inline static MmapHook GetMmapHook();
282 inline static MmapHook SetMmapHook(MmapHook hook) {
283 return MallocHook_SetMmapHook(hook);
284 }
285
286 inline static MunmapHook GetMunmapHook();
287 inline static MunmapHook SetMunmapHook(MunmapHook hook) {
288 return MallocHook_SetMunmapHook(hook);
289 }
290
291 inline static MremapHook GetMremapHook();
292 inline static MremapHook SetMremapHook(MremapHook hook) {
293 return MallocHook_SetMremapHook(hook);
294 }
295
296 inline static PreSbrkHook GetPreSbrkHook();
297 inline static PreSbrkHook SetPreSbrkHook(PreSbrkHook hook) {
298 return MallocHook_SetPreSbrkHook(hook);
299 }
300
301 inline static SbrkHook GetSbrkHook();
302 inline static SbrkHook SetSbrkHook(SbrkHook hook) {
303 return MallocHook_SetSbrkHook(hook);
304 }
305 // End of DEPRECATED methods.
306
307 private:
308 // Slow path versions of Invoke*Hook.
309 static void InvokeNewHookSlow(const void* p, size_t s);
310 static void InvokeDeleteHookSlow(const void* p);
311 static void InvokePreMmapHookSlow(const void* start,
312 size_t size,
313 int protection,
314 int flags,
315 int fd,
316 off_t offset);
317 static void InvokeMmapHookSlow(const void* result,
318 const void* start,
319 size_t size,
320 int protection,
321 int flags,
322 int fd,
323 off_t offset);
324 static bool InvokeMmapReplacementSlow(const void* start,
325 size_t size,
326 int protection,
327 int flags,
328 int fd,
329 off_t offset,
330 void** result);
331 static void InvokeMunmapHookSlow(const void* p, size_t size);
332 static bool InvokeMunmapReplacementSlow(const void* p,
333 size_t size,
334 int* result);
335 static void InvokeMremapHookSlow(const void* result,
336 const void* old_addr,
337 size_t old_size,
338 size_t new_size,
339 int flags,
340 const void* new_addr);
341 static void InvokePreSbrkHookSlow(std::ptrdiff_t increment);
342 static void InvokeSbrkHookSlow(const void* result, std::ptrdiff_t increment);
200 }; 343 };
201 344
202 #endif /* _MALLOC_HOOK_H_ */ 345 #endif /* _MALLOC_HOOK_H_ */
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/google/malloc_extension.h ('k') | third_party/tcmalloc/chromium/src/google/malloc_hook_c.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698