blob: 95dd4079246f6ef5815d4682959f029733e08da0 [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>
104class TrackingAllocatorImpl {
105 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() {
117 }
118
119 // Used internally by STL data structures.
120 TrackingAllocatorImpl() throw() {
121 COMPILE_ASSERT(kTag < kAllocatorTagCount, must_be_less_than_count);
122 }
123
124 // Enables an allocator for objects of one type to allocate storage for objects of another type.
125 // Used internally by STL data structures.
126 template <class U>
127 struct rebind {
128 typedef TrackingAllocatorImpl<U, kTag> other;
129 };
130
131 pointer allocate(size_type n, const_pointer hint = 0) {
132 const size_t size = n * sizeof(T);
133 TrackedAllocators::RegisterAllocation(GetTag(), size);
134 return reinterpret_cast<pointer>(malloc(size));
135 }
136
137 template <typename PT>
138 void deallocate(PT p, size_type n) {
139 const size_t size = n * sizeof(T);
140 TrackedAllocators::RegisterFree(GetTag(), size);
141 free(p);
142 }
143
Ian Rogers7e70b002014-10-08 11:47:24 -0700144 static constexpr AllocatorTag GetTag() {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700145 return kTag;
146 }
147};
148
149template<class T, AllocatorTag kTag>
150// C++ doesn't allow template typedefs. This is a workaround template typedef which is
151// TrackingAllocatorImpl<T> if kEnableTrackingAllocator is true, std::allocator<T> otherwise.
152class TrackingAllocator : public TypeStaticIf<kEnableTrackingAllocator,
153 TrackingAllocatorImpl<T, kTag>,
154 std::allocator<T>>::type {
155};
156
157template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
158class AllocationTrackingMultiMap : public std::multimap<
159 Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>> {
160};
161
Mathieu Chartier58553c72014-09-16 16:25:55 -0700162template<class Key, AllocatorTag kTag, class Compare = std::less<Key>>
163class AllocationTrackingSet : public std::set<Key, Compare, TrackingAllocator<Key, kTag>> {
164};
165
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700166} // namespace art
167
Brian Carlstromba150c32013-08-27 17:31:03 -0700168#endif // ART_RUNTIME_BASE_ALLOCATOR_H_