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

Side by Side Diff: third_party/tcmalloc/chromium/src/custom_allocator.cc

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: LeakDetectorImpl stores addrs as uintptr_t; Implement move semantics for RankedList Created 5 years, 3 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <gperftools/custom_allocator.h>
6
7 #include "base/low_level_alloc.h"
8
9 static LowLevelAlloc::Arena* g_arena = nullptr;
10
11 bool g_is_initalized_for_unit_test = false;
12
13 // static
14 void CustomAllocator::Initialize() {
15 g_arena = LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena());
16 }
17
18 // static
19 bool CustomAllocator::Shutdown() {
20 if (!g_is_initalized_for_unit_test)
21 return LowLevelAlloc::DeleteArena(g_arena);
22 g_is_initalized_for_unit_test = false;
23 return true;
24 }
25
26 // static
27 bool CustomAllocator::IsInitialized() {
28 return g_arena || g_is_initalized_for_unit_test;
29 }
30
31 void CustomAllocator::InitializeForUnitTest() {
32 g_is_initalized_for_unit_test = true;
33 }
34
35 // static
36 void* CustomAllocator::Allocate(size_t size) {
37 if (g_is_initalized_for_unit_test)
38 return new char[size];
39
40 if (!g_arena)
41 return nullptr;
42
43 return LowLevelAlloc::AllocWithArena(size, g_arena);
44 }
45
46 // static
47 void CustomAllocator::Free(void* ptr, size_t /* size */) {
48 if (g_is_initalized_for_unit_test)
49 delete [] reinterpret_cast<char*>(ptr);
50 else
51 LowLevelAlloc::Free(ptr);
52 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698