blob: 7bfbb6f93bb4dfa978d07e54f0436bfe38c0e1f3 [file] [log] [blame]
buzbee862a7602013-04-05 10:58:54 -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
Nicolas Geoffray818f2102014-02-18 16:43:35 +000017#ifndef ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_
18#define ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_
buzbee862a7602013-04-05 10:58:54 -070019
20#include <stdint.h>
21#include <stddef.h>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070022
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000023#include "base/macros.h"
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070024#include "base/mutex.h"
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070025#include "mem_map.h"
Andreas Gampe66018822014-05-05 20:47:19 -070026#include "utils.h"
Vladimir Marko8081d2b2014-07-31 15:33:43 +010027#include "utils/debug_stack.h"
buzbee862a7602013-04-05 10:58:54 -070028
29namespace art {
30
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070031class Arena;
32class ArenaPool;
33class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000034class ArenaStack;
35class ScopedArenaAllocator;
36class MemStats;
37
Vladimir Marko8081d2b2014-07-31 15:33:43 +010038template <typename T>
39class ArenaAllocatorAdapter;
40
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000041static constexpr bool kArenaAllocatorCountAllocations = false;
42
43// Type of allocation for memory tuning.
44enum ArenaAllocKind {
45 kArenaAllocMisc,
46 kArenaAllocBB,
47 kArenaAllocLIR,
Vladimir Marko8dea81c2014-06-06 14:50:36 +010048 kArenaAllocLIRResourceMask,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000049 kArenaAllocMIR,
50 kArenaAllocDFInfo,
51 kArenaAllocGrowableArray,
52 kArenaAllocGrowableBitMap,
53 kArenaAllocDalvikToSSAMap,
54 kArenaAllocDebugInfo,
55 kArenaAllocSuccessor,
56 kArenaAllocRegAlloc,
57 kArenaAllocData,
58 kArenaAllocPredecessors,
59 kArenaAllocSTL,
60 kNumArenaAllocKinds
61};
62
63template <bool kCount>
64class ArenaAllocatorStatsImpl;
65
66template <>
67class ArenaAllocatorStatsImpl<false> {
68 public:
69 ArenaAllocatorStatsImpl() = default;
70 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
71 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
72
73 void Copy(const ArenaAllocatorStatsImpl& other) { UNUSED(other); }
74 void RecordAlloc(size_t bytes, ArenaAllocKind kind) { UNUSED(bytes); UNUSED(kind); }
75 size_t NumAllocations() const { return 0u; }
76 size_t BytesAllocated() const { return 0u; }
77 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const {
78 UNUSED(os); UNUSED(first); UNUSED(lost_bytes_adjustment);
79 }
80};
81
82template <bool kCount>
83class ArenaAllocatorStatsImpl {
84 public:
85 ArenaAllocatorStatsImpl();
86 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
87 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
88
89 void Copy(const ArenaAllocatorStatsImpl& other);
90 void RecordAlloc(size_t bytes, ArenaAllocKind kind);
91 size_t NumAllocations() const;
92 size_t BytesAllocated() const;
93 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const;
94
95 private:
96 size_t num_allocations_;
97 // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL.
98 size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds.
Vladimir Markobd9e9db2014-03-07 19:41:05 +000099
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100100 static const char* const kAllocNames[];
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000101};
102
103typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats;
buzbee862a7602013-04-05 10:58:54 -0700104
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700105class Arena {
106 public:
107 static constexpr size_t kDefaultSize = 128 * KB;
108 explicit Arena(size_t size = kDefaultSize);
109 ~Arena();
110 void Reset();
111 uint8_t* Begin() {
112 return memory_;
buzbee862a7602013-04-05 10:58:54 -0700113 }
114
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700115 uint8_t* End() {
116 return memory_ + size_;
117 }
118
119 size_t Size() const {
120 return size_;
121 }
122
123 size_t RemainingSpace() const {
124 return Size() - bytes_allocated_;
125 }
126
127 private:
128 size_t bytes_allocated_;
129 uint8_t* memory_;
130 size_t size_;
131 MemMap* map_;
132 Arena* next_;
133 friend class ArenaPool;
134 friend class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000135 friend class ArenaStack;
136 friend class ScopedArenaAllocator;
137 template <bool kCount> friend class ArenaAllocatorStatsImpl;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700138 DISALLOW_COPY_AND_ASSIGN(Arena);
139};
140
141class ArenaPool {
142 public:
143 ArenaPool();
144 ~ArenaPool();
145 Arena* AllocArena(size_t size);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000146 void FreeArenaChain(Arena* first);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700147
148 private:
149 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
150 Arena* free_arenas_ GUARDED_BY(lock_);
151 DISALLOW_COPY_AND_ASSIGN(ArenaPool);
152};
153
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100154class ArenaAllocator : private DebugStackRefCounter, private ArenaAllocatorStats {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700155 public:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700156 explicit ArenaAllocator(ArenaPool* pool);
157 ~ArenaAllocator();
158
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100159 // Get adapter for use in STL containers. See arena_containers.h .
160 ArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL);
161
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700162 // Returns zeroed memory.
163 void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE {
Mathieu Chartier75165d02013-09-12 14:00:31 -0700164 if (UNLIKELY(running_on_valgrind_)) {
165 return AllocValgrind(bytes, kind);
166 }
Vladimir Marko22a0ef82014-06-10 14:47:51 +0100167 bytes = RoundUp(bytes, 8);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700168 if (UNLIKELY(ptr_ + bytes > end_)) {
169 // Obtain a new block.
170 ObtainNewArenaForAllocation(bytes);
171 if (UNLIKELY(ptr_ == nullptr)) {
172 return nullptr;
173 }
174 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000175 ArenaAllocatorStats::RecordAlloc(bytes, kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700176 uint8_t* ret = ptr_;
177 ptr_ += bytes;
178 return ret;
179 }
180
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100181 template <typename T> T* AllocArray(size_t length) {
182 return static_cast<T*>(Alloc(length * sizeof(T), kArenaAllocMisc));
183 }
184
Mathieu Chartier75165d02013-09-12 14:00:31 -0700185 void* AllocValgrind(size_t bytes, ArenaAllocKind kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700186 void ObtainNewArenaForAllocation(size_t allocation_size);
187 size_t BytesAllocated() const;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000188 MemStats GetMemStats() const;
buzbee862a7602013-04-05 10:58:54 -0700189
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700190 private:
191 void UpdateBytesAllocated();
buzbee862a7602013-04-05 10:58:54 -0700192
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700193 ArenaPool* pool_;
194 uint8_t* begin_;
195 uint8_t* end_;
196 uint8_t* ptr_;
197 Arena* arena_head_;
Mathieu Chartier75165d02013-09-12 14:00:31 -0700198 bool running_on_valgrind_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700199
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100200 template <typename U>
201 friend class ArenaAllocatorAdapter;
202
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700203 DISALLOW_COPY_AND_ASSIGN(ArenaAllocator);
buzbee862a7602013-04-05 10:58:54 -0700204}; // ArenaAllocator
205
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000206class MemStats {
207 public:
208 MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
209 ssize_t lost_bytes_adjustment = 0);
210 void Dump(std::ostream& os) const;
211
212 private:
213 const char* const name_;
214 const ArenaAllocatorStats* const stats_;
215 const Arena* const first_arena_;
216 const ssize_t lost_bytes_adjustment_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700217}; // MemStats
buzbee862a7602013-04-05 10:58:54 -0700218
219} // namespace art
220
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221#endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_