OLD | NEW |
---|---|
(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 "base/logging.h" | |
9 | |
10 // Generic allocator class for STL objects | |
11 // that uses a given type-less allocator Alloc, which must provide: | |
12 // static void* Alloc::Allocate(size_t size); | |
13 // static void Alloc::Free(void* ptr, size_t size); | |
14 // | |
15 // STL_Allocator<T, MyAlloc> provides the same thread-safety | |
16 // guarantees as MyAlloc. | |
17 // | |
18 // Usage example: | |
19 // set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set; | |
20 // CAVEAT: Parts of the code below are probably specific | |
21 // to the STL version(s) we are using. | |
22 // The code is simply lifted from what std::allocator<> provides. | |
23 template <typename T, class Alloc> | |
24 class STL_Allocator { | |
25 public: | |
26 typedef size_t size_type; | |
27 typedef ptrdiff_t difference_type; | |
28 typedef T* pointer; | |
29 typedef const T* const_pointer; | |
30 typedef T& reference; | |
31 typedef const T& const_reference; | |
32 typedef T value_type; | |
33 | |
34 template <class T1> struct rebind { | |
35 typedef STL_Allocator<T1, Alloc> other; | |
36 }; | |
37 | |
38 STL_Allocator() { } | |
39 STL_Allocator(const STL_Allocator&) { } | |
40 template <class T1> STL_Allocator(const STL_Allocator<T1, Alloc>&) { } | |
41 ~STL_Allocator() { } | |
42 | |
43 pointer address(reference x) const { return &x; } | |
44 const_pointer address(const_reference x) const { return &x; } | |
45 | |
46 pointer allocate(size_type n, const void* = 0) { | |
47 CHECK_EQ((n * sizeof(T)) / sizeof(T), n); | |
48 // << "n=" << n << " is too big to allocate"; | |
Simon Que
2015/08/20 00:29:02
Should clean this up.
Simon Que
2015/08/20 04:21:55
Done.
| |
49 //RAW_DCHECK((n * sizeof(T)) / sizeof(T) == n, "n is too big to allocate"); | |
50 return static_cast<T*>(Alloc::Allocate(n * sizeof(T))); | |
51 } | |
52 void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); } | |
53 | |
54 size_type max_size() const { return size_t(-1) / sizeof(T); } | |
55 | |
56 void construct(pointer p, const T& val) { ::new(p) T(val); } | |
57 void construct(pointer p) { ::new(p) T(); } | |
58 void destroy(pointer p) { p->~T(); } | |
59 | |
60 // There's no state, so these allocators always return the same value. | |
61 bool operator==(const STL_Allocator&) const { return true; } | |
62 bool operator!=(const STL_Allocator&) const { return false; } | |
63 }; | |
64 | |
65 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_ | |
OLD | NEW |