blob: 5a09c9612638f42a374430cac5d1c93cbb3d3aba [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromba150c32013-08-27 17:31:03 -070017#ifndef ART_RUNTIME_BASE_ALLOCATOR_H_
18#define ART_RUNTIME_BASE_ALLOCATOR_H_
Brian Carlstrom413e89f2013-10-21 23:53:49 -070019
Mathieu Chartierbad02672014-08-25 13:08:22 -070020#include <map>
21
22#include "atomic.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070023#include "base/macros.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070024#include "base/mutex.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "base/type_static_if.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070026
27namespace art {
28
Mathieu Chartierbad02672014-08-25 13:08:22 -070029static constexpr bool kEnableTrackingAllocator = false;
30
Brian Carlstrom413e89f2013-10-21 23:53:49 -070031class Allocator {
32 public:
33 static Allocator* GetMallocAllocator();
34 static Allocator* GetNoopAllocator();
35
36 Allocator() {}
37 virtual ~Allocator() {}
38
39 virtual void* Alloc(size_t) = 0;
40 virtual void Free(void*) = 0;
41
42 private:
43 DISALLOW_COPY_AND_ASSIGN(Allocator);
44};
45
Mathieu Chartierbad02672014-08-25 13:08:22 -070046// Used by TrackedAllocators.
47enum AllocatorTag {
48 kAllocatorTagHeap,
49 kAllocatorTagMonitorList,
50 kAllocatorTagClassTable,
51 kAllocatorTagInternTable,
52 kAllocatorTagMaps,
53 kAllocatorTagLOS,
54 kAllocatorTagSafeMap,
55 kAllocatorTagLOSMaps,
56 kAllocatorTagReferenceTable,
57 kAllocatorTagHeapBitmap,
58 kAllocatorTagHeapBitmapLOS,
59 kAllocatorTagMonitorPool,
60 kAllocatorTagLOSFreeList,
61 kAllocatorTagVerifier,
62 kAllocatorTagRememberedSet,
63 kAllocatorTagModUnionCardSet,
64 kAllocatorTagModUnionReferenceArray,
65 kAllocatorTagJNILibrarires,
66 kAllocatorTagCompileTimeClassPath,
67 kAllocatorTagOatFile,
68 kAllocatorTagDexFileVerifier,
Mathieu Chartier58553c72014-09-16 16:25:55 -070069 kAllocatorTagRosAlloc,
Mathieu Chartierbad02672014-08-25 13:08:22 -070070 kAllocatorTagCount, // Must always be last element.
71};
72std::ostream& operator<<(std::ostream& os, const AllocatorTag& tag);
73
Ian Rogers7e70b002014-10-08 11:47:24 -070074namespace TrackedAllocators {
Mathieu Chartierbad02672014-08-25 13:08:22 -070075
Ian Rogers7e70b002014-10-08 11:47:24 -070076// Running count of number of bytes used for this kind of allocation. Increased by allocations,
77// decreased by deallocations.
78extern Atomic<size_t> g_bytes_used[kAllocatorTagCount];
Mathieu Chartierbad02672014-08-25 13:08:22 -070079
Ian Rogers7e70b002014-10-08 11:47:24 -070080// Largest value of bytes used seen.
81extern volatile size_t g_max_bytes_used[kAllocatorTagCount];
82
83// Total number of bytes allocated of this kind.
84extern Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount];
85
86void Dump(std::ostream& os);
87
88inline void RegisterAllocation(AllocatorTag tag, size_t bytes) {
89 g_total_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes);
90 size_t new_bytes = g_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes) + bytes;
91 if (g_max_bytes_used[tag] < new_bytes) {
92 g_max_bytes_used[tag] = new_bytes;
93 }
94}
95
96inline void RegisterFree(AllocatorTag tag, size_t bytes) {
97 g_bytes_used[tag].FetchAndSubSequentiallyConsistent(bytes);
98}
99
100} // namespace TrackedAllocators
101
102// Tracking allocator for use with STL types, tracks how much memory is used.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700103template<class T, AllocatorTag kTag>
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800104class TrackingAllocatorImpl : public std::allocator<T> {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700105 public:
106 typedef typename std::allocator<T>::value_type value_type;
107 typedef typename std::allocator<T>::size_type size_type;
108 typedef typename std::allocator<T>::difference_type difference_type;
109 typedef typename std::allocator<T>::pointer pointer;
110 typedef typename std::allocator<T>::const_pointer const_pointer;
111 typedef typename std::allocator<T>::reference reference;
112 typedef typename std::allocator<T>::const_reference const_reference;
113
114 // Used internally by STL data structures.
115 template <class U>
116 TrackingAllocatorImpl(const TrackingAllocatorImpl<U, kTag>& alloc) throw() {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117 UNUSED(alloc);
Mathieu Chartierbad02672014-08-25 13:08:22 -0700118 }
119
120 // Used internally by STL data structures.
121 TrackingAllocatorImpl() throw() {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800122 static_assert(kTag < kAllocatorTagCount, "kTag must be less than kAllocatorTagCount");
Mathieu Chartierbad02672014-08-25 13:08:22 -0700123 }
124
125 // Enables an allocator for objects of one type to allocate storage for objects of another type.
126 // Used internally by STL data structures.
127 template <class U>
128 struct rebind {
129 typedef TrackingAllocatorImpl<U, kTag> other;
130 };
131
132 pointer allocate(size_type n, const_pointer hint = 0) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700133 UNUSED(hint);
Mathieu Chartierbad02672014-08-25 13:08:22 -0700134 const size_t size = n * sizeof(T);
135 TrackedAllocators::RegisterAllocation(GetTag(), size);
136 return reinterpret_cast<pointer>(malloc(size));
137 }
138
139 template <typename PT>
140 void deallocate(PT p, size_type n) {
141 const size_t size = n * sizeof(T);
142 TrackedAllocators::RegisterFree(GetTag(), size);
143 free(p);
144 }
145
Ian Rogers7e70b002014-10-08 11:47:24 -0700146 static constexpr AllocatorTag GetTag() {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700147 return kTag;
148 }
149};
150
151template<class T, AllocatorTag kTag>
152// C++ doesn't allow template typedefs. This is a workaround template typedef which is
153// TrackingAllocatorImpl<T> if kEnableTrackingAllocator is true, std::allocator<T> otherwise.
154class TrackingAllocator : public TypeStaticIf<kEnableTrackingAllocator,
155 TrackingAllocatorImpl<T, kTag>,
156 std::allocator<T>>::type {
157};
158
159template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
160class AllocationTrackingMultiMap : public std::multimap<
161 Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>> {
162};
163
Mathieu Chartier58553c72014-09-16 16:25:55 -0700164template<class Key, AllocatorTag kTag, class Compare = std::less<Key>>
165class AllocationTrackingSet : public std::set<Key, Compare, TrackingAllocator<Key, kTag>> {
166};
167
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700168} // namespace art
169
Brian Carlstromba150c32013-08-27 17:31:03 -0700170#endif // ART_RUNTIME_BASE_ALLOCATOR_H_