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