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

Unified 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: Mac build fixes: const arg in comparator, rm const in func return type 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/metrics/leak_detector/ranked_list_unittest.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..bfef0a38244d048b49c29a27c30ba05004cc097b
--- /dev/null
+++ b/components/metrics/leak_detector/stl_allocator.h
@@ -0,0 +1,61 @@
+// 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 <limits>
+#include <memory>
+
+#include "base/logging.h"
+
+// Generic allocator class for STL objects.
+// deallocate() to use the template class Alloc's allocation.
+// 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);
+//
+// Inherits from the default allocator, std::allocator. Overrides allocate() and
+// deallocate() and some other functions.
+//
+// STLAllocator<T, MyAlloc> provides the same thread-safety guarantees as
+// MyAlloc.
+//
+// Usage example:
+// set<T, less<T>, STLAllocator<T, MyAlloc> > my_set;
+
+template <typename T, class Alloc>
+class STLAllocator : public std::allocator<T> {
+ public:
+ typedef size_t size_type;
+ typedef T* pointer;
+
+ template <class T1>
+ struct rebind {
+ typedef STLAllocator<T1, Alloc> other;
+ };
+
+ STLAllocator() {}
+ explicit STLAllocator(const STLAllocator&) {}
+ template <class T1>
+ STLAllocator(const STLAllocator<T1, Alloc>&) {}
+ ~STLAllocator() {}
+
+ 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 < max_size());
+ 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 std::numeric_limits<size_t>::max() / sizeof(T);
+ }
+};
+
+#endif // COMPONENTS_METRICS_LEAK_DETECTOR_STL_ALLOCATOR_H_
« no previous file with comments | « components/metrics/leak_detector/ranked_list_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698