blob: 2c3e966d32d60b5263e3f0656cc8a06abb9dc68b [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"
25#include "utils.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
74class TrackedAllocators {
75 public:
76 static bool Add(uint32_t tag, AtomicInteger* bytes_used);
77 static void Dump(std::ostream& os);
78 static void RegisterAllocation(AllocatorTag tag, uint64_t bytes) {
79 total_bytes_used_[tag].FetchAndAddSequentiallyConsistent(bytes);
80 uint64_t new_bytes = bytes_used_[tag].FetchAndAddSequentiallyConsistent(bytes) + bytes;
81 max_bytes_used_[tag].StoreRelaxed(std::max(max_bytes_used_[tag].LoadRelaxed(), new_bytes));
82 }
83 static void RegisterFree(AllocatorTag tag, uint64_t bytes) {
84 bytes_used_[tag].FetchAndSubSequentiallyConsistent(bytes);
85 }
86
87 private:
88 static Atomic<uint64_t> bytes_used_[kAllocatorTagCount];
89 static Atomic<uint64_t> max_bytes_used_[kAllocatorTagCount];
90 static Atomic<uint64_t> total_bytes_used_[kAllocatorTagCount];
91};
92
93// Tracking allocator, tracks how much memory is used.
94template<class T, AllocatorTag kTag>
95class TrackingAllocatorImpl {
96 public:
97 typedef typename std::allocator<T>::value_type value_type;
98 typedef typename std::allocator<T>::size_type size_type;
99 typedef typename std::allocator<T>::difference_type difference_type;
100 typedef typename std::allocator<T>::pointer pointer;
101 typedef typename std::allocator<T>::const_pointer const_pointer;
102 typedef typename std::allocator<T>::reference reference;
103 typedef typename std::allocator<T>::const_reference const_reference;
104
105 // Used internally by STL data structures.
106 template <class U>
107 TrackingAllocatorImpl(const TrackingAllocatorImpl<U, kTag>& alloc) throw() {
108 }
109
110 // Used internally by STL data structures.
111 TrackingAllocatorImpl() throw() {
112 COMPILE_ASSERT(kTag < kAllocatorTagCount, must_be_less_than_count);
113 }
114
115 // Enables an allocator for objects of one type to allocate storage for objects of another type.
116 // Used internally by STL data structures.
117 template <class U>
118 struct rebind {
119 typedef TrackingAllocatorImpl<U, kTag> other;
120 };
121
122 pointer allocate(size_type n, const_pointer hint = 0) {
123 const size_t size = n * sizeof(T);
124 TrackedAllocators::RegisterAllocation(GetTag(), size);
125 return reinterpret_cast<pointer>(malloc(size));
126 }
127
128 template <typename PT>
129 void deallocate(PT p, size_type n) {
130 const size_t size = n * sizeof(T);
131 TrackedAllocators::RegisterFree(GetTag(), size);
132 free(p);
133 }
134
135 static AllocatorTag GetTag() {
136 return kTag;
137 }
138};
139
140template<class T, AllocatorTag kTag>
141// C++ doesn't allow template typedefs. This is a workaround template typedef which is
142// TrackingAllocatorImpl<T> if kEnableTrackingAllocator is true, std::allocator<T> otherwise.
143class TrackingAllocator : public TypeStaticIf<kEnableTrackingAllocator,
144 TrackingAllocatorImpl<T, kTag>,
145 std::allocator<T>>::type {
146};
147
148template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
149class AllocationTrackingMultiMap : public std::multimap<
150 Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>> {
151};
152
Mathieu Chartier58553c72014-09-16 16:25:55 -0700153template<class Key, AllocatorTag kTag, class Compare = std::less<Key>>
154class AllocationTrackingSet : public std::set<Key, Compare, TrackingAllocator<Key, kTag>> {
155};
156
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700157} // namespace art
158
Brian Carlstromba150c32013-08-27 17:31:03 -0700159#endif // ART_RUNTIME_BASE_ALLOCATOR_H_