Chromium Code Reviews| Index: components/metrics/leak_detector/custom_allocator.h |
| diff --git a/components/metrics/leak_detector/custom_allocator.h b/components/metrics/leak_detector/custom_allocator.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..24f55fb1cab3420444d1fdd1f49b1a6a0cbfa35f |
| --- /dev/null |
| +++ b/components/metrics/leak_detector/custom_allocator.h |
| @@ -0,0 +1,48 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_METRICS_LEAK_DETECTOR_CUSTOM_ALLOCATOR_H_ |
| +#define COMPONENTS_METRICS_LEAK_DETECTOR_CUSTOM_ALLOCATOR_H_ |
| + |
| +#include <stddef.h> |
| + |
| +#include <type_traits> |
| + |
| +namespace metrics { |
| +namespace leak_detector { |
| + |
| +// Custom allocator class to be passed to STLAllocator as a template argument. |
| +// |
| +// By default, CustomAllocator uses the default allocator (new/delete), but the |
| +// caller of Initialize ()can provide a pair of alternative alloc/ free |
| +// functions to use as an external allocator. |
|
jar (doing other things)
2015/11/14 02:58:37
I'm not yet clear how you "hook in," but how do yo
Simon Que
2015/11/17 00:28:48
It was originally included in this CL, but since i
|
| +// |
| +// This is a stateless class, but there is static data within the module that |
| +// needs to be created and deleted. |
| +class CustomAllocator { |
| + public: |
| + using AllocFunc = std::add_pointer<void*(size_t)>::type; |
| + using FreeFunc = std::add_pointer<void(void*, size_t)>::type; |
| + |
| + // Initialize CustomAllocator to use the default allocator. |
| + static void Initialize(); |
| + |
| + // Initialize CustomAllocator to use the given alloc/free functions. |
| + static void Initialize(AllocFunc alloc_func, FreeFunc free_func); |
| + |
| + // Performs any cleanup required, e.g. unset custom functions. Returns true |
| + // on success or false if something failed. |
| + static bool Shutdown(); |
| + |
| + static bool IsInitialized(); |
| + |
| + // These functions must match the specifications in STLAllocator. |
| + static void* Allocate(size_t size); |
| + static void Free(void* ptr, size_t size); |
| +}; |
| + |
| +} // namespace leak_detector |
| +} // namespace metrics |
| + |
| +#endif // COMPONENTS_METRICS_LEAK_DETECTOR_CUSTOM_ALLOCATOR_H_ |