blob: 3cedb66abeaf5c2b309ca6b03fd2a1fd567d52de [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
Vladimir Markoca420e42018-01-03 13:37:04 +000020#include <type_traits>
21
Mathieu Chartierbad02672014-08-25 13:08:22 -070022#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"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070025
26namespace art {
27
Mathieu Chartierbad02672014-08-25 13:08:22 -070028static constexpr bool kEnableTrackingAllocator = false;
29
Brian Carlstrom413e89f2013-10-21 23:53:49 -070030class Allocator {
31 public:
32 static Allocator* GetMallocAllocator();
33 static Allocator* GetNoopAllocator();
34
35 Allocator() {}
36 virtual ~Allocator() {}
37
38 virtual void* Alloc(size_t) = 0;
39 virtual void Free(void*) = 0;
40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(Allocator);
43};
44
Mathieu Chartierbad02672014-08-25 13:08:22 -070045// Used by TrackedAllocators.
46enum AllocatorTag {
47 kAllocatorTagHeap,
48 kAllocatorTagMonitorList,
49 kAllocatorTagClassTable,
50 kAllocatorTagInternTable,
51 kAllocatorTagMaps,
52 kAllocatorTagLOS,
53 kAllocatorTagSafeMap,
54 kAllocatorTagLOSMaps,
55 kAllocatorTagReferenceTable,
56 kAllocatorTagHeapBitmap,
57 kAllocatorTagHeapBitmapLOS,
58 kAllocatorTagMonitorPool,
59 kAllocatorTagLOSFreeList,
60 kAllocatorTagVerifier,
61 kAllocatorTagRememberedSet,
62 kAllocatorTagModUnionCardSet,
63 kAllocatorTagModUnionReferenceArray,
Andreas Gampe0a7993e2014-12-05 11:16:26 -080064 kAllocatorTagJNILibraries,
Mathieu Chartierbad02672014-08-25 13:08:22 -070065 kAllocatorTagCompileTimeClassPath,
66 kAllocatorTagOatFile,
67 kAllocatorTagDexFileVerifier,
Mathieu Chartier58553c72014-09-16 16:25:55 -070068 kAllocatorTagRosAlloc,
Mathieu Chartierbad02672014-08-25 13:08:22 -070069 kAllocatorTagCount, // Must always be last element.
70};
71std::ostream& operator<<(std::ostream& os, const AllocatorTag& tag);
72
Ian Rogers7e70b002014-10-08 11:47:24 -070073namespace TrackedAllocators {
Mathieu Chartierbad02672014-08-25 13:08:22 -070074
Ian Rogers7e70b002014-10-08 11:47:24 -070075// Running count of number of bytes used for this kind of allocation. Increased by allocations,
76// decreased by deallocations.
77extern Atomic<size_t> g_bytes_used[kAllocatorTagCount];
Mathieu Chartierbad02672014-08-25 13:08:22 -070078
Ian Rogers7e70b002014-10-08 11:47:24 -070079// Largest value of bytes used seen.
80extern volatile size_t g_max_bytes_used[kAllocatorTagCount];
81
82// Total number of bytes allocated of this kind.
83extern Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount];
84
85void Dump(std::ostream& os);
86
87inline void RegisterAllocation(AllocatorTag tag, size_t bytes) {
88 g_total_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes);
89 size_t new_bytes = g_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes) + bytes;
90 if (g_max_bytes_used[tag] < new_bytes) {
91 g_max_bytes_used[tag] = new_bytes;
92 }
93}
94
95inline void RegisterFree(AllocatorTag tag, size_t bytes) {
96 g_bytes_used[tag].FetchAndSubSequentiallyConsistent(bytes);
97}
98
99} // namespace TrackedAllocators
100
101// Tracking allocator for use with STL types, tracks how much memory is used.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700102template<class T, AllocatorTag kTag>
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800103class TrackingAllocatorImpl : public std::allocator<T> {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700104 public:
105 typedef typename std::allocator<T>::value_type value_type;
106 typedef typename std::allocator<T>::size_type size_type;
107 typedef typename std::allocator<T>::difference_type difference_type;
108 typedef typename std::allocator<T>::pointer pointer;
109 typedef typename std::allocator<T>::const_pointer const_pointer;
110 typedef typename std::allocator<T>::reference reference;
111 typedef typename std::allocator<T>::const_reference const_reference;
112
113 // Used internally by STL data structures.
114 template <class U>
Igor Murashkin5573c372017-11-16 13:34:30 -0800115 TrackingAllocatorImpl(
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700116 const TrackingAllocatorImpl<U, kTag>& alloc ATTRIBUTE_UNUSED) noexcept {}
Mathieu Chartierbad02672014-08-25 13:08:22 -0700117
118 // Used internally by STL data structures.
Andreas Gampe758a8012015-04-03 21:28:42 -0700119 TrackingAllocatorImpl() noexcept {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800120 static_assert(kTag < kAllocatorTagCount, "kTag must be less than kAllocatorTagCount");
Mathieu Chartierbad02672014-08-25 13:08:22 -0700121 }
122
123 // Enables an allocator for objects of one type to allocate storage for objects of another type.
124 // Used internally by STL data structures.
125 template <class U>
126 struct rebind {
127 typedef TrackingAllocatorImpl<U, kTag> other;
128 };
129
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100130 pointer allocate(size_type n, const_pointer hint ATTRIBUTE_UNUSED = 0) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700131 const size_t size = n * sizeof(T);
132 TrackedAllocators::RegisterAllocation(GetTag(), size);
133 return reinterpret_cast<pointer>(malloc(size));
134 }
135
136 template <typename PT>
137 void deallocate(PT p, size_type n) {
138 const size_t size = n * sizeof(T);
139 TrackedAllocators::RegisterFree(GetTag(), size);
140 free(p);
141 }
142
Ian Rogers7e70b002014-10-08 11:47:24 -0700143 static constexpr AllocatorTag GetTag() {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700144 return kTag;
145 }
146};
147
148template<class T, AllocatorTag kTag>
149// C++ doesn't allow template typedefs. This is a workaround template typedef which is
150// TrackingAllocatorImpl<T> if kEnableTrackingAllocator is true, std::allocator<T> otherwise.
Vladimir Markoca420e42018-01-03 13:37:04 +0000151using TrackingAllocator = typename std::conditional<kEnableTrackingAllocator,
152 TrackingAllocatorImpl<T, kTag>,
153 std::allocator<T>>::type;
Mathieu Chartierbad02672014-08-25 13:08:22 -0700154
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700155} // namespace art
156
Brian Carlstromba150c32013-08-27 17:31:03 -0700157#endif // ART_RUNTIME_BASE_ALLOCATOR_H_