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 "components/metrics/leak_detector/custom_allocator.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 namespace metrics { |
| 10 namespace leak_detector { |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Wrappers around new and delete. |
| 15 void* DefaultAlloc(size_t size) { |
| 16 return new char[size]; |
| 17 } |
| 18 void DefaultFree(void* ptr, size_t /* size */) { |
| 19 delete[] reinterpret_cast<char*>(ptr); |
| 20 } |
| 21 |
| 22 CustomAllocator::AllocFunc g_alloc_func = nullptr; |
| 23 CustomAllocator::FreeFunc g_free_func = nullptr; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 // static |
| 28 void CustomAllocator::Initialize() { |
| 29 Initialize(&DefaultAlloc, &DefaultFree); |
| 30 } |
| 31 |
| 32 // static |
| 33 void CustomAllocator::Initialize(AllocFunc alloc_func, FreeFunc free_func) { |
| 34 g_alloc_func = alloc_func; |
| 35 g_free_func = free_func; |
| 36 } |
| 37 |
| 38 // static |
| 39 bool CustomAllocator::Shutdown() { |
| 40 g_alloc_func = nullptr; |
| 41 g_free_func = nullptr; |
| 42 return true; |
| 43 } |
| 44 |
| 45 // static |
| 46 bool CustomAllocator::IsInitialized() { |
| 47 return g_alloc_func && g_free_func; |
| 48 } |
| 49 |
| 50 // static |
| 51 void* CustomAllocator::Allocate(size_t size) { |
| 52 return g_alloc_func(size); |
| 53 } |
| 54 |
| 55 // static |
| 56 void CustomAllocator::Free(void* ptr, size_t size) { |
| 57 g_free_func(ptr, size); |
| 58 } |
| 59 |
| 60 } // namespace leak_detector |
| 61 } // namespace metrics |
OLD | NEW |