blob: 3422625282b28c0dbfd21aed3e27c6e04a58c179 [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 Chartierbad02672014-08-25 13:08:22 -070022
23#include "atomic.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070024#include "base/macros.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070025#include "base/mutex.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "base/type_static_if.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070027
28namespace art {
29
Mathieu Chartierbad02672014-08-25 13:08:22 -070030static constexpr bool kEnableTrackingAllocator = false;
31
Brian Carlstrom413e89f2013-10-21 23:53:49 -070032class Allocator {
33 public:
34 static Allocator* GetMallocAllocator();
35 static Allocator* GetNoopAllocator();
36
37 Allocator() {}
38 virtual ~Allocator() {}
39
40 virtual void* Alloc(size_t) = 0;
41 virtual void Free(void*) = 0;
42
43 private:
44 DISALLOW_COPY_AND_ASSIGN(Allocator);
45};
46
Mathieu Chartierbad02672014-08-25 13:08:22 -070047// Used by TrackedAllocators.
48enum AllocatorTag {
49 kAllocatorTagHeap,
50 kAllocatorTagMonitorList,
51 kAllocatorTagClassTable,
52 kAllocatorTagInternTable,
Igor Murashkine2facc52015-07-10 13:49:08 -070053 kAllocatorTagLambdaBoxTable,
Mathieu Chartierbad02672014-08-25 13:08:22 -070054 kAllocatorTagMaps,
55 kAllocatorTagLOS,
56 kAllocatorTagSafeMap,
57 kAllocatorTagLOSMaps,
58 kAllocatorTagReferenceTable,
59 kAllocatorTagHeapBitmap,
60 kAllocatorTagHeapBitmapLOS,
61 kAllocatorTagMonitorPool,
62 kAllocatorTagLOSFreeList,
63 kAllocatorTagVerifier,
64 kAllocatorTagRememberedSet,
65 kAllocatorTagModUnionCardSet,
66 kAllocatorTagModUnionReferenceArray,
Andreas Gampe0a7993e2014-12-05 11:16:26 -080067 kAllocatorTagJNILibraries,
Mathieu Chartierbad02672014-08-25 13:08:22 -070068 kAllocatorTagCompileTimeClassPath,
69 kAllocatorTagOatFile,
70 kAllocatorTagDexFileVerifier,
Mathieu Chartier58553c72014-09-16 16:25:55 -070071 kAllocatorTagRosAlloc,
Mathieu Chartierbad02672014-08-25 13:08:22 -070072 kAllocatorTagCount, // Must always be last element.
73};
74std::ostream& operator<<(std::ostream& os, const AllocatorTag& tag);
75
Ian Rogers7e70b002014-10-08 11:47:24 -070076namespace TrackedAllocators {
Mathieu Chartierbad02672014-08-25 13:08:22 -070077
Ian Rogers7e70b002014-10-08 11:47:24 -070078// Running count of number of bytes used for this kind of allocation. Increased by allocations,
79// decreased by deallocations.
80extern Atomic<size_t> g_bytes_used[kAllocatorTagCount];
Mathieu Chartierbad02672014-08-25 13:08:22 -070081
Ian Rogers7e70b002014-10-08 11:47:24 -070082// Largest value of bytes used seen.
83extern volatile size_t g_max_bytes_used[kAllocatorTagCount];
84
85// Total number of bytes allocated of this kind.
86extern Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount];
87
88void Dump(std::ostream& os);
89
90inline void RegisterAllocation(AllocatorTag tag, size_t bytes) {
91 g_total_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes);
92 size_t new_bytes = g_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes) + bytes;
93 if (g_max_bytes_used[tag] < new_bytes) {
94 g_max_bytes_used[tag] = new_bytes;
95 }
96}
97
98inline void RegisterFree(AllocatorTag tag, size_t bytes) {
99 g_bytes_used[tag].FetchAndSubSequentiallyConsistent(bytes);
100}
101
102} // namespace TrackedAllocators
103
104// Tracking allocator for use with STL types, tracks how much memory is used.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700105template<class T, AllocatorTag kTag>
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800106class TrackingAllocatorImpl : public std::allocator<T> {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700107 public:
108 typedef typename std::allocator<T>::value_type value_type;
109 typedef typename std::allocator<T>::size_type size_type;
110 typedef typename std::allocator<T>::difference_type difference_type;
111 typedef typename std::allocator<T>::pointer pointer;
112 typedef typename std::allocator<T>::const_pointer const_pointer;
113 typedef typename std::allocator<T>::reference reference;
114 typedef typename std::allocator<T>::const_reference const_reference;
115
116 // Used internally by STL data structures.
117 template <class U>
Andreas Gampe758a8012015-04-03 21:28:42 -0700118 TrackingAllocatorImpl(const TrackingAllocatorImpl<U, kTag>& alloc) noexcept {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700119 UNUSED(alloc);
Mathieu Chartierbad02672014-08-25 13:08:22 -0700120 }
121
122 // Used internally by STL data structures.
Andreas Gampe758a8012015-04-03 21:28:42 -0700123 TrackingAllocatorImpl() noexcept {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800124 static_assert(kTag < kAllocatorTagCount, "kTag must be less than kAllocatorTagCount");
Mathieu Chartierbad02672014-08-25 13:08:22 -0700125 }
126
127 // Enables an allocator for objects of one type to allocate storage for objects of another type.
128 // Used internally by STL data structures.
129 template <class U>
130 struct rebind {
131 typedef TrackingAllocatorImpl<U, kTag> other;
132 };
133
134 pointer allocate(size_type n, const_pointer hint = 0) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700135 UNUSED(hint);
Mathieu Chartierbad02672014-08-25 13:08:22 -0700136 const size_t size = n * sizeof(T);
137 TrackedAllocators::RegisterAllocation(GetTag(), size);
138 return reinterpret_cast<pointer>(malloc(size));
139 }
140
141 template <typename PT>
142 void deallocate(PT p, size_type n) {
143 const size_t size = n * sizeof(T);
144 TrackedAllocators::RegisterFree(GetTag(), size);
145 free(p);
146 }
147
Ian Rogers7e70b002014-10-08 11:47:24 -0700148 static constexpr AllocatorTag GetTag() {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700149 return kTag;
150 }
151};
152
153template<class T, AllocatorTag kTag>
154// C++ doesn't allow template typedefs. This is a workaround template typedef which is
155// TrackingAllocatorImpl<T> if kEnableTrackingAllocator is true, std::allocator<T> otherwise.
156class TrackingAllocator : public TypeStaticIf<kEnableTrackingAllocator,
157 TrackingAllocatorImpl<T, kTag>,
158 std::allocator<T>>::type {
159};
160
161template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
162class AllocationTrackingMultiMap : public std::multimap<
163 Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>> {
164};
165
Mathieu Chartier58553c72014-09-16 16:25:55 -0700166template<class Key, AllocatorTag kTag, class Compare = std::less<Key>>
167class AllocationTrackingSet : public std::set<Key, Compare, TrackingAllocator<Key, kTag>> {
168};
169
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700170} // namespace art
171
Brian Carlstromba150c32013-08-27 17:31:03 -0700172#endif // ART_RUNTIME_BASE_ALLOCATOR_H_