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

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: LeakDetectorImpl stores addrs as uintptr_t; Implement move semantics for RankedList Created 5 years, 3 months 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
12 #include "base/logging.h"
13
14 // Generic allocator class for STL objects
15 // that uses a given type-less allocator Alloc, which must provide:
16 // static void* Alloc::Allocate(size_t size);
17 // static void Alloc::Free(void* ptr, size_t size);
18 //
19 // STL_Allocator<T, MyAlloc> provides the same thread-safety
20 // guarantees as MyAlloc.
21 //
22 // Usage example:
23 // set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set;
24 // CAVEAT: Parts of the code below are probably specific
25 // to the STL version(s) we are using.
26 // The code is simply lifted from what std::allocator<> provides.
27 template <typename T, class Alloc>
28 class STL_Allocator {
29 public:
30 typedef size_t size_type;
31 typedef ptrdiff_t difference_type;
32 typedef T* pointer;
33 typedef const T* const_pointer;
34 typedef T& reference;
35 typedef const T& const_reference;
36 typedef T value_type;
37
38 template <class T1> struct rebind {
39 typedef STL_Allocator<T1, Alloc> other;
40 };
41
42 STL_Allocator() {}
43 explicit STL_Allocator(const STL_Allocator&) {}
44 template <class T1> STL_Allocator(const STL_Allocator<T1, Alloc>&) {}
45 ~STL_Allocator() {}
46
47 pointer address(reference x) const { return &x; }
48 const_pointer address(const_reference x) const { return &x; }
49
50 pointer allocate(size_type n, const void* = 0) {
51 // Make sure the computation of the total allocation size does not cause an
52 // integer overflow.
53 RAW_CHECK(n < max_size());
54 return static_cast<T*>(Alloc::Allocate(n * sizeof(T)));
55 }
56 void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); }
57
58 size_type max_size() const {
59 return std::numeric_limits<size_t>::max() / sizeof(T);
60 }
61
62 void construct(pointer p, const T& val) { ::new(p) T(val); }
63 void construct(pointer p) { ::new(p) T(); }
64 void destroy(pointer p) { p->~T(); }
65
66 // There's no state, so these allocators always return the same value.
67 bool operator==(const STL_Allocator&) const { return true; }
68 bool operator!=(const STL_Allocator&) const { return false; }
69 };
70
71 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698