| OLD | NEW |
| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 // The C++ methods below call the C version (MallocHook_*), and thus |
| 85 // convert between an int and a bool. Windows complains about this |
| 86 // (a "performance warning") which we don't care about, so we suppress. |
| 87 #ifdef _MSC_VER |
| 88 #pragma warning(push) |
| 89 #pragma warning(disable:4800) |
| 90 #endif |
| 91 |
| 84 // Note: malloc_hook_c.h defines MallocHook_*Hook and | 92 // Note: malloc_hook_c.h defines MallocHook_*Hook and |
| 85 // MallocHook_{Add,Remove}*Hook. The version of these inside the MallocHook | 93 // MallocHook_{Add,Remove}*Hook. The version of these inside the MallocHook |
| 86 // class are defined in terms of the malloc_hook_c version. See malloc_hook_c.h | 94 // class are defined in terms of the malloc_hook_c version. See malloc_hook_c.h |
| 87 // for details of these types/functions. | 95 // for details of these types/functions. |
| 88 | 96 |
| 89 class PERFTOOLS_DLL_DECL MallocHook { | 97 class PERFTOOLS_DLL_DECL MallocHook { |
| 90 public: | 98 public: |
| 91 // The NewHook is invoked whenever an object is allocated. | 99 // The NewHook is invoked whenever an object is allocated. |
| 92 // It may be passed NULL if the allocator returned NULL. | 100 // It may be passed NULL if the allocator returned NULL. |
| 93 typedef MallocHook_NewHook NewHook; | 101 typedef MallocHook_NewHook NewHook; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 // memory-allocation call. It may be useful in memory-limited contexts, | 230 // memory-allocation call. It may be useful in memory-limited contexts, |
| 223 // to catch allocations that will exceed the limit and take outside | 231 // to catch allocations that will exceed the limit and take outside |
| 224 // actions to increase such a limit. | 232 // actions to increase such a limit. |
| 225 typedef MallocHook_PreSbrkHook PreSbrkHook; | 233 typedef MallocHook_PreSbrkHook PreSbrkHook; |
| 226 inline static bool AddPreSbrkHook(PreSbrkHook hook) { | 234 inline static bool AddPreSbrkHook(PreSbrkHook hook) { |
| 227 return MallocHook_AddPreSbrkHook(hook); | 235 return MallocHook_AddPreSbrkHook(hook); |
| 228 } | 236 } |
| 229 inline static bool RemovePreSbrkHook(PreSbrkHook hook) { | 237 inline static bool RemovePreSbrkHook(PreSbrkHook hook) { |
| 230 return MallocHook_RemovePreSbrkHook(hook); | 238 return MallocHook_RemovePreSbrkHook(hook); |
| 231 } | 239 } |
| 232 inline static void InvokePreSbrkHook(std::ptrdiff_t increment); | 240 inline static void InvokePreSbrkHook(ptrdiff_t increment); |
| 233 | 241 |
| 234 // The SbrkHook is invoked whenever sbrk is called -- except when | 242 // The SbrkHook is invoked whenever sbrk is called -- except when |
| 235 // the increment is 0. This is because sbrk(0) is often called | 243 // the increment is 0. This is because sbrk(0) is often called |
| 236 // to get the top of the memory stack, and is not actually a | 244 // to get the top of the memory stack, and is not actually a |
| 237 // memory-allocation call. | 245 // memory-allocation call. |
| 238 typedef MallocHook_SbrkHook SbrkHook; | 246 typedef MallocHook_SbrkHook SbrkHook; |
| 239 inline static bool AddSbrkHook(SbrkHook hook) { | 247 inline static bool AddSbrkHook(SbrkHook hook) { |
| 240 return MallocHook_AddSbrkHook(hook); | 248 return MallocHook_AddSbrkHook(hook); |
| 241 } | 249 } |
| 242 inline static bool RemoveSbrkHook(SbrkHook hook) { | 250 inline static bool RemoveSbrkHook(SbrkHook hook) { |
| 243 return MallocHook_RemoveSbrkHook(hook); | 251 return MallocHook_RemoveSbrkHook(hook); |
| 244 } | 252 } |
| 245 inline static void InvokeSbrkHook(const void* result, std::ptrdiff_t increment
); | 253 inline static void InvokeSbrkHook(const void* result, ptrdiff_t increment); |
| 246 | 254 |
| 247 // Get the current stack trace. Try to skip all routines up to and | 255 // Get the current stack trace. Try to skip all routines up to and |
| 248 // and including the caller of MallocHook::Invoke*. | 256 // and including the caller of MallocHook::Invoke*. |
| 249 // Use "skip_count" (similarly to GetStackTrace from stacktrace.h) | 257 // Use "skip_count" (similarly to GetStackTrace from stacktrace.h) |
| 250 // as a hint about how many routines to skip if better information | 258 // as a hint about how many routines to skip if better information |
| 251 // is not available. | 259 // is not available. |
| 252 inline static int GetCallerStackTrace(void** result, int max_depth, | 260 inline static int GetCallerStackTrace(void** result, int max_depth, |
| 253 int skip_count) { | 261 int skip_count) { |
| 254 return MallocHook_GetCallerStackTrace(result, max_depth, skip_count); | 262 return MallocHook_GetCallerStackTrace(result, max_depth, skip_count); |
| 255 } | 263 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 static void InvokeMunmapHookSlow(const void* p, size_t size); | 339 static void InvokeMunmapHookSlow(const void* p, size_t size); |
| 332 static bool InvokeMunmapReplacementSlow(const void* p, | 340 static bool InvokeMunmapReplacementSlow(const void* p, |
| 333 size_t size, | 341 size_t size, |
| 334 int* result); | 342 int* result); |
| 335 static void InvokeMremapHookSlow(const void* result, | 343 static void InvokeMremapHookSlow(const void* result, |
| 336 const void* old_addr, | 344 const void* old_addr, |
| 337 size_t old_size, | 345 size_t old_size, |
| 338 size_t new_size, | 346 size_t new_size, |
| 339 int flags, | 347 int flags, |
| 340 const void* new_addr); | 348 const void* new_addr); |
| 341 static void InvokePreSbrkHookSlow(std::ptrdiff_t increment); | 349 static void InvokePreSbrkHookSlow(ptrdiff_t increment); |
| 342 static void InvokeSbrkHookSlow(const void* result, std::ptrdiff_t increment); | 350 static void InvokeSbrkHookSlow(const void* result, ptrdiff_t increment); |
| 343 }; | 351 }; |
| 344 | 352 |
| 353 #ifdef _MSC_VER |
| 354 #pragma warning(pop) |
| 355 #endif |
| 356 |
| 357 |
| 345 #endif /* _MALLOC_HOOK_H_ */ | 358 #endif /* _MALLOC_HOOK_H_ */ |
| OLD | NEW |