blob: ad255b8694a2a017b7615fd7e864f13240520bc4 [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>
Dan Albert627f9172015-03-04 15:06:16 -080021#include <set>
Mathieu Chartier7b05e172015-10-15 17:47:48 -070022#include <unordered_map>
Mathieu Chartierbad02672014-08-25 13:08:22 -070023
24#include "atomic.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070025#include "base/macros.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070026#include "base/mutex.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "base/type_static_if.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070028
29namespace art {
30
Mathieu Chartierbad02672014-08-25 13:08:22 -070031static constexpr bool kEnableTrackingAllocator = false;
32
Brian Carlstrom413e89f2013-10-21 23:53:49 -070033class Allocator {
34 public:
35 static Allocator* GetMallocAllocator();
36 static Allocator* GetNoopAllocator();
37
38 Allocator() {}
39 virtual ~Allocator() {}
40
41 virtual void* Alloc(size_t) = 0;
42 virtual void Free(void*) = 0;
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(Allocator);
46};
47
Mathieu Chartierbad02672014-08-25 13:08:22 -070048// Used by TrackedAllocators.
49enum AllocatorTag {
50 kAllocatorTagHeap,
51 kAllocatorTagMonitorList,
52 kAllocatorTagClassTable,
53 kAllocatorTagInternTable,
Igor Murashkine2facc52015-07-10 13:49:08 -070054 kAllocatorTagLambdaBoxTable,
Mathieu Chartierbad02672014-08-25 13:08:22 -070055 kAllocatorTagMaps,
56 kAllocatorTagLOS,
57 kAllocatorTagSafeMap,
58 kAllocatorTagLOSMaps,
59 kAllocatorTagReferenceTable,
60 kAllocatorTagHeapBitmap,
61 kAllocatorTagHeapBitmapLOS,
62 kAllocatorTagMonitorPool,
63 kAllocatorTagLOSFreeList,
64 kAllocatorTagVerifier,
65 kAllocatorTagRememberedSet,
66 kAllocatorTagModUnionCardSet,
67 kAllocatorTagModUnionReferenceArray,
Andreas Gampe0a7993e2014-12-05 11:16:26 -080068 kAllocatorTagJNILibraries,
Mathieu Chartierbad02672014-08-25 13:08:22 -070069 kAllocatorTagCompileTimeClassPath,
70 kAllocatorTagOatFile,
71 kAllocatorTagDexFileVerifier,
Mathieu Chartier58553c72014-09-16 16:25:55 -070072 kAllocatorTagRosAlloc,
Mathieu Chartierbad02672014-08-25 13:08:22 -070073 kAllocatorTagCount, // Must always be last element.
74};
75std::ostream& operator<<(std::ostream& os, const AllocatorTag& tag);
76
Ian Rogers7e70b002014-10-08 11:47:24 -070077namespace TrackedAllocators {
Mathieu Chartierbad02672014-08-25 13:08:22 -070078
Ian Rogers7e70b002014-10-08 11:47:24 -070079// Running count of number of bytes used for this kind of allocation. Increased by allocations,
80// decreased by deallocations.
81extern Atomic<size_t> g_bytes_used[kAllocatorTagCount];
Mathieu Chartierbad02672014-08-25 13:08:22 -070082
Ian Rogers7e70b002014-10-08 11:47:24 -070083// Largest value of bytes used seen.
84extern volatile size_t g_max_bytes_used[kAllocatorTagCount];
85
86// Total number of bytes allocated of this kind.
87extern Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount];
88
89void Dump(std::ostream& os);
90
91inline void RegisterAllocation(AllocatorTag tag, size_t bytes) {
92 g_total_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes);
93 size_t new_bytes = g_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes) + bytes;
94 if (g_max_bytes_used[tag] < new_bytes) {
95 g_max_bytes_used[tag] = new_bytes;
96 }
97}
98
99inline void RegisterFree(AllocatorTag tag, size_t bytes) {
100 g_bytes_used[tag].FetchAndSubSequentiallyConsistent(bytes);
101}
102
103} // namespace TrackedAllocators
104
105// Tracking allocator for use with STL types, tracks how much memory is used.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700106template<class T, AllocatorTag kTag>
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800107class TrackingAllocatorImpl : public std::allocator<T> {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700108 public:
109 typedef typename std::allocator<T>::value_type value_type;
110 typedef typename std::allocator<T>::size_type size_type;
111 typedef typename std::allocator<T>::difference_type difference_type;
112 typedef typename std::allocator<T>::pointer pointer;
113 typedef typename std::allocator<T>::const_pointer const_pointer;
114 typedef typename std::allocator<T>::reference reference;
115 typedef typename std::allocator<T>::const_reference const_reference;
116
117 // Used internally by STL data structures.
118 template <class U>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100119 TrackingAllocatorImpl(const TrackingAllocatorImpl<U, kTag>& alloc ATTRIBUTE_UNUSED) noexcept {}
Mathieu Chartierbad02672014-08-25 13:08:22 -0700120
121 // Used internally by STL data structures.
Andreas Gampe758a8012015-04-03 21:28:42 -0700122 TrackingAllocatorImpl() noexcept {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800123 static_assert(kTag < kAllocatorTagCount, "kTag must be less than kAllocatorTagCount");
Mathieu Chartierbad02672014-08-25 13:08:22 -0700124 }
125
126 // Enables an allocator for objects of one type to allocate storage for objects of another type.
127 // Used internally by STL data structures.
128 template <class U>
129 struct rebind {
130 typedef TrackingAllocatorImpl<U, kTag> other;
131 };
132
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100133 pointer allocate(size_type n, const_pointer hint ATTRIBUTE_UNUSED = 0) {
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.
Mathieu Chartier7b05e172015-10-15 17:47:48 -0700154using TrackingAllocator = typename TypeStaticIf<kEnableTrackingAllocator,
155 TrackingAllocatorImpl<T, kTag>,
156 std::allocator<T>>::type;
Mathieu Chartierbad02672014-08-25 13:08:22 -0700157
158template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
Mathieu Chartier7b05e172015-10-15 17:47:48 -0700159using AllocationTrackingMultiMap = std::multimap<
160 Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>>;
Mathieu Chartierbad02672014-08-25 13:08:22 -0700161
Mathieu Chartier58553c72014-09-16 16:25:55 -0700162template<class Key, AllocatorTag kTag, class Compare = std::less<Key>>
Mathieu Chartier7b05e172015-10-15 17:47:48 -0700163using AllocationTrackingSet = std::set<Key, Compare, TrackingAllocator<Key, kTag>>;
164
165template<class Key,
166 class T,
167 AllocatorTag kTag,
168 class Hash = std::hash<Key>,
169 class Pred = std::equal_to<Key>>
170using AllocationTrackingUnorderedMap = std::unordered_map<
171 Key, T, Hash, Pred, TrackingAllocator<std::pair<const Key, T>, kTag>>;
Mathieu Chartier58553c72014-09-16 16:25:55 -0700172
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700173} // namespace art
174
Brian Carlstromba150c32013-08-27 17:31:03 -0700175#endif // ART_RUNTIME_BASE_ALLOCATOR_H_