OLD | NEW |
(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 } |
OLD | NEW |