Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "UncensoredAllocator.h"
8 :
9 : #include "mozilla/Assertions.h"
10 : #include "mozilla/Unused.h"
11 :
12 : #include "MainThreadUtils.h"
13 : #include "jsfriendapi.h"
14 : #include "nsDebug.h"
15 : #include "prlock.h"
16 : #ifdef MOZ_REPLACE_MALLOC
17 : #include "replace_malloc_bridge.h"
18 : #endif
19 :
20 : namespace mozilla {
21 :
22 : #ifdef MOZ_REPLACE_MALLOC
23 : MOZ_THREAD_LOCAL(bool) MallocHook::mEnabledTLS;
24 : NativeProfiler* MallocHook::mNativeProfiler;
25 : malloc_hook_table_t MallocHook::mMallocHook;
26 : #endif
27 :
28 0 : AutoUseUncensoredAllocator::AutoUseUncensoredAllocator()
29 : {
30 : #ifdef MOZ_REPLACE_MALLOC
31 0 : MallocHook::mEnabledTLS.set(false);
32 : #endif
33 0 : }
34 :
35 0 : AutoUseUncensoredAllocator::~AutoUseUncensoredAllocator()
36 : {
37 : #ifdef MOZ_REPLACE_MALLOC
38 0 : MallocHook::mEnabledTLS.set(true);
39 : #endif
40 0 : }
41 :
42 : bool
43 0 : MallocHook::Enabled()
44 : {
45 : #ifdef MOZ_REPLACE_MALLOC
46 0 : return mEnabledTLS.get() && mNativeProfiler;
47 : #else
48 : return false;
49 : #endif
50 : }
51 :
52 : void*
53 0 : MallocHook::SampleNative(void* aAddr, size_t aSize)
54 : {
55 : #ifdef MOZ_REPLACE_MALLOC
56 0 : if (MallocHook::Enabled()) {
57 0 : mNativeProfiler->sampleNative(aAddr, aSize);
58 : }
59 : #endif
60 0 : return aAddr;
61 : }
62 :
63 : void
64 0 : MallocHook::RemoveNative(void* aAddr)
65 : {
66 : #ifdef MOZ_REPLACE_MALLOC
67 0 : if (MallocHook::Enabled()) {
68 0 : mNativeProfiler->removeNative(aAddr);
69 : }
70 : #endif
71 0 : }
72 :
73 : void
74 0 : MallocHook::Initialize()
75 : {
76 : #ifdef MOZ_REPLACE_MALLOC
77 0 : MOZ_ASSERT(NS_IsMainThread());
78 0 : mMallocHook.free_hook = RemoveNative;
79 0 : mMallocHook.malloc_hook = SampleNative;
80 0 : ReplaceMallocBridge* bridge = ReplaceMallocBridge::Get(3);
81 0 : if (bridge) {
82 0 : mozilla::Unused << bridge->RegisterHook("memory-profiler", nullptr, nullptr);
83 : }
84 :
85 0 : bool success = mEnabledTLS.init();
86 0 : if (NS_WARN_IF(!success)) {
87 0 : return;
88 : }
89 : #endif
90 : }
91 :
92 : void
93 0 : MallocHook::Enable(NativeProfiler* aNativeProfiler)
94 : {
95 : #ifdef MOZ_REPLACE_MALLOC
96 0 : MOZ_ASSERT(NS_IsMainThread());
97 0 : ReplaceMallocBridge* bridge = ReplaceMallocBridge::Get(3);
98 0 : if (bridge) {
99 : const malloc_table_t* alloc_funcs =
100 0 : bridge->RegisterHook("memory-profiler", nullptr, &mMallocHook);
101 0 : if (alloc_funcs) {
102 0 : mNativeProfiler = aNativeProfiler;
103 : }
104 : }
105 : #endif
106 0 : }
107 :
108 : void
109 0 : MallocHook::Disable()
110 : {
111 : #ifdef MOZ_REPLACE_MALLOC
112 0 : MOZ_ASSERT(NS_IsMainThread());
113 0 : ReplaceMallocBridge* bridge = ReplaceMallocBridge::Get(3);
114 0 : if (bridge) {
115 0 : bridge->RegisterHook("memory-profiler", nullptr, nullptr);
116 0 : mNativeProfiler = nullptr;
117 : }
118 : #endif
119 0 : }
120 :
121 : } // namespace mozilla
|