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

Side by Side Diff: base/allocator/allocator_extension.cc

Issue 1665553002: metrics: Connect leak detector to allocator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing include Created 4 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "base/allocator/allocator_extension.h" 5 #include "base/allocator/allocator_extension.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 #if defined(USE_TCMALLOC) 9 #if defined(USE_TCMALLOC)
10 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" 10 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
11 #include "third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h" 11 #include "third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h"
12 #include "third_party/tcmalloc/chromium/src/gperftools/malloc_hook.h"
12 #endif 13 #endif
13 14
14 namespace base { 15 namespace base {
15 namespace allocator { 16 namespace allocator {
16 17
17 void ReleaseFreeMemory() { 18 void ReleaseFreeMemory() {
18 #if defined(USE_TCMALLOC) 19 #if defined(USE_TCMALLOC)
19 ::MallocExtension::instance()->ReleaseFreeMemory(); 20 ::MallocExtension::instance()->ReleaseFreeMemory();
20 #endif 21 #endif
21 } 22 }
22 23
23 bool GetNumericProperty(const char* name, size_t* value) { 24 bool GetNumericProperty(const char* name, size_t* value) {
24 #if defined(USE_TCMALLOC) 25 #if defined(USE_TCMALLOC)
25 return ::MallocExtension::instance()->GetNumericProperty(name, value); 26 return ::MallocExtension::instance()->GetNumericProperty(name, value);
26 #endif 27 #endif
27 return false; 28 return false;
28 } 29 }
29 30
30 bool IsHeapProfilerRunning() { 31 bool IsHeapProfilerRunning() {
31 #if defined(USE_TCMALLOC) 32 #if defined(USE_TCMALLOC)
32 return ::IsHeapProfilerRunning(); 33 return ::IsHeapProfilerRunning();
33 #endif 34 #endif
34 return false; 35 return false;
35 } 36 }
36 37
38 AllocHookFunc SetSingleAllocHook(AllocHookFunc func) {
39 #if defined(USE_TCMALLOC)
40 return MallocHook::SetNewHook(func);
Primiano Tucci (use gerrit) 2016/03/01 21:10:28 Just looked at malloc_hook.h, it looks SetNewHook
Simon Que 2016/03/02 06:25:09 I specifically went with the deprecated version be
Primiano Tucci (use gerrit) 2016/03/02 06:39:54 Ok let's keep these deprecated ones. Worst case wh
41 #endif
42 return nullptr;
43 }
44
45 FreeHookFunc SetSingleFreeHook(FreeHookFunc func) {
46 #if defined(USE_TCMALLOC)
47 return MallocHook::SetDeleteHook(func);
Primiano Tucci (use gerrit) 2016/03/01 21:10:27 And RemoveNewHook here
48 #endif
49 return nullptr;
50 }
51
52 AllocHookFunc GetSingleAllocHook() {
53 #if defined(USE_TCMALLOC)
54 // Due to the way things are implemented in tcmalloc's
55 // gperftools/malloc_hook.h, MallocHook::GetNewHook() cannot be called.
56 // Instead, overwrite the stored callback with nullptr and get the stored
57 // callback as a return value from SetNewHook(). Then write it back as the
58 // stored value. Same goes for GetSingleFreeHook().
59 //
60 // This implementation should not have too much overhead as long as
61 // MallocHook's deprecated hook registration functions are trivial.
62 AllocHookFunc existing_hook = MallocHook::SetNewHook(nullptr);
Primiano Tucci (use gerrit) 2016/03/01 21:10:27 This is racy by design, how do you prevent that so
Simon Que 2016/03/02 06:25:09 Doesn't quite work that way, since the non-depreca
Primiano Tucci (use gerrit) 2016/03/02 06:39:54 Is this problem solved (i.e. can we get rid of the
63 MallocHook::SetNewHook(existing_hook);
64 return existing_hook;
65 #endif
66 return nullptr;
67 }
68
69 FreeHookFunc GetSingleFreeHook() {
70 #if defined(USE_TCMALLOC)
71 FreeHookFunc existing_hook = MallocHook::SetDeleteHook(nullptr);
72 MallocHook::SetDeleteHook(existing_hook);
73 return existing_hook;
74 #endif
75 return nullptr;
76 }
77
78 int GetCallStack(void** stack, int max_stack_size, int /* skip_count */) {
79 #if defined(USE_TCMALLOC)
80 // |skip_count| is not used by the following function. Instead, the number of
81 // frames to skip is determined internally.
82 // See MallocHook_GetCallerStackTrace() in
83 // third_party/tcmalloc/chromium/src/malloc_hook.cc.
84 return MallocHook::GetCallerStackTrace(stack, max_stack_size, 0);
85 #endif
86 return 0;
87 }
88
37 } // namespace allocator 89 } // namespace allocator
38 } // namespace base 90 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698