Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(904)

Side by Side Diff: components/metrics/leak_detector/stl_allocator.h

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add OWNERS file Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_
7
8 #include <stddef.h>
9
10 #include <limits>
11 #include <memory>
12
13 #include "base/logging.h"
14
15 // Generic allocator class for STL objects.
16 // deallocate() to use the template class Alloc's allocation.
17 // that uses a given type-less allocator Alloc, which must provide:
18 // static void* Alloc::Allocate(size_t size);
19 // static void Alloc::Free(void* ptr, size_t size);
20 //
21 // Inherits from the default allocator, std::allocator. Overrides allocate() and
22 // deallocate() and some other functions.
23 //
24 // STLAllocator<T, MyAlloc> provides the same thread-safety guarantees as
25 // MyAlloc.
26 //
27 // Usage example:
28 // set<T, less<T>, STLAllocator<T, MyAlloc> > my_set;
29
30 template <typename T, class Alloc>
31 class STLAllocator : public std::allocator<T> {
32 public:
33 typedef size_t size_type;
34 typedef T* pointer;
35
36 template <class T1> struct rebind {
37 typedef STLAllocator<T1, Alloc> other;
38 };
39
40 STLAllocator() {}
41 explicit STLAllocator(const STLAllocator&) {}
42 template <class T1> STLAllocator(const STLAllocator<T1, Alloc>&) {}
43 ~STLAllocator() {}
44
45 pointer allocate(size_type n, const void* = 0) {
46 // Make sure the computation of the total allocation size does not cause an
47 // integer overflow.
48 RAW_CHECK(n < max_size());
49 return static_cast<T*>(Alloc::Allocate(n * sizeof(T)));
50 }
51
52 void deallocate(pointer p, size_type n) {
53 Alloc::Free(p, n * sizeof(T));
54 }
55
56 size_type max_size() const {
57 return std::numeric_limits<size_t>::max() / sizeof(T);
58 }
59 };
60
61 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698