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

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: Fix style, comments, RAW_CHECK in stl_allocator.h Created 5 years, 4 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 "base/logging.h"
11
12 // Generic allocator class for STL objects
13 // that uses a given type-less allocator Alloc, which must provide:
14 // static void* Alloc::Allocate(size_t size);
15 // static void Alloc::Free(void* ptr, size_t size);
16 //
17 // STL_Allocator<T, MyAlloc> provides the same thread-safety
18 // guarantees as MyAlloc.
19 //
20 // Usage example:
21 // set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set;
22 // CAVEAT: Parts of the code below are probably specific
23 // to the STL version(s) we are using.
24 // The code is simply lifted from what std::allocator<> provides.
25 template <typename T, class Alloc>
26 class STL_Allocator {
27 public:
28 typedef size_t size_type;
29 typedef ptrdiff_t difference_type;
30 typedef T* pointer;
31 typedef const T* const_pointer;
32 typedef T& reference;
33 typedef const T& const_reference;
34 typedef T value_type;
35
36 template <class T1> struct rebind {
37 typedef STL_Allocator<T1, Alloc> other;
38 };
39
40 STL_Allocator() {}
41 STL_Allocator(const STL_Allocator&) {}
jar (doing other things) 2015/08/21 02:48:43 nit: explicit?
Simon Que 2015/08/21 21:59:20 Done.
42 template <class T1> STL_Allocator(const STL_Allocator<T1, Alloc>&) {}
43 ~STL_Allocator() {}
44
45 pointer address(reference x) const { return &x; }
46 const_pointer address(const_reference x) const { return &x; }
47
48 pointer allocate(size_type n, const void* = 0) {
49 // Make sure the computation of the total allocation size does not cause an
50 // integer overflow.
51 RAW_CHECK((n * sizeof(T)) / sizeof(T) == n);
jar (doing other things) 2015/08/21 02:48:43 nit: Clearer and faster is probably: n <= max_siz
Simon Que 2015/08/21 05:44:05 That wouldn't catch the overflow of |n * sizeof(T)
jar (doing other things) 2015/08/21 17:32:31 I must be missing something. Isn't max_size() the
Simon Que 2015/08/21 21:59:20 How could size_t n be > max_size()? Checking n <=
jar (doing other things) 2015/08/22 00:21:47 max_size() is defined on line 56 below. It is the
Simon Que 2015/08/23 23:30:53 Done.
52 return static_cast<T*>(Alloc::Allocate(n * sizeof(T)));
53 }
54 void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); }
55
56 size_type max_size() const { return size_t(-1) / sizeof(T); }
jar (doing other things) 2015/08/21 02:48:43 Rather than size_t(-1), you should probably use st
Simon Que 2015/08/21 21:59:20 Done.
57
58 void construct(pointer p, const T& val) { ::new(p) T(val); }
59 void construct(pointer p) { ::new(p) T(); }
60 void destroy(pointer p) { p->~T(); }
61
62 // There's no state, so these allocators always return the same value.
63 bool operator==(const STL_Allocator&) const { return true; }
64 bool operator!=(const STL_Allocator&) const { return false; }
65 };
66
67 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698