Index: components/metrics/leak_detector/stl_allocator.h |
diff --git a/components/metrics/leak_detector/stl_allocator.h b/components/metrics/leak_detector/stl_allocator.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6658845e8463d75f97328399c253b42a2cc82927 |
--- /dev/null |
+++ b/components/metrics/leak_detector/stl_allocator.h |
@@ -0,0 +1,67 @@ |
+// 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 <stddef.h> |
+ |
+#include "base/logging.h" |
+ |
+// Generic allocator class for STL objects |
+// 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); |
+// |
+// STL_Allocator<T, MyAlloc> provides the same thread-safety |
+// guarantees as MyAlloc. |
+// |
+// Usage example: |
+// set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set; |
+// CAVEAT: Parts of the code below are probably specific |
+// to the STL version(s) we are using. |
+// The code is simply lifted from what std::allocator<> provides. |
+template <typename T, class Alloc> |
+class STL_Allocator { |
+ public: |
+ typedef size_t size_type; |
+ typedef ptrdiff_t difference_type; |
+ typedef T* pointer; |
+ typedef const T* const_pointer; |
+ typedef T& reference; |
+ typedef const T& const_reference; |
+ typedef T value_type; |
+ |
+ template <class T1> struct rebind { |
+ typedef STL_Allocator<T1, Alloc> other; |
+ }; |
+ |
+ STL_Allocator() {} |
+ 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.
|
+ template <class T1> STL_Allocator(const STL_Allocator<T1, Alloc>&) {} |
+ ~STL_Allocator() {} |
+ |
+ pointer address(reference x) const { return &x; } |
+ const_pointer address(const_reference x) const { return &x; } |
+ |
+ 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 * 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.
|
+ return static_cast<T*>(Alloc::Allocate(n * sizeof(T))); |
+ } |
+ void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); } |
+ |
+ 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.
|
+ |
+ void construct(pointer p, const T& val) { ::new(p) T(val); } |
+ void construct(pointer p) { ::new(p) T(); } |
+ void destroy(pointer p) { p->~T(); } |
+ |
+ // There's no state, so these allocators always return the same value. |
+ bool operator==(const STL_Allocator&) const { return true; } |
+ bool operator!=(const STL_Allocator&) const { return false; } |
+}; |
+ |
+#endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_ |