// 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_STL_ALLOCATOR_H_ #define COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_ #include #include #include #include "base/logging.h" // Generic allocator class for STL objects. // deallocate() to use the template class Alloc's allocation. // that uses a given type-less allocator Alloc, which must provide: // static void* Alloc::Allocate(size_t size); // static void Alloc::Free(void* ptr, size_t size); // // Inherits from the default allocator, std::allocator. Overrides allocate() and // deallocate() and some other functions. // // STLAllocator provides the same thread-safety guarantees as // MyAlloc. // // Usage example: // set, STLAllocator > my_set; template class STLAllocator : public std::allocator { public: typedef size_t size_type; typedef T* pointer; template struct rebind { typedef STLAllocator other; }; STLAllocator() {} explicit STLAllocator(const STLAllocator&) {} template STLAllocator(const STLAllocator&) {} ~STLAllocator() {} pointer allocate(size_type n, const void* = 0) { // Make sure the computation of the total allocation size does not cause an // integer overflow. RAW_CHECK(n < max_size()); return static_cast(Alloc::Allocate(n * sizeof(T))); } void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); } size_type max_size() const { return std::numeric_limits::max() / sizeof(T); } }; #endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_