blob: f4bcb1d44de37a64f5438da610786ec201a3cb5b [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"
buzbee862a7602013-04-05 10:58:54 -070027
28namespace art {
29
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070030class Arena;
31class ArenaPool;
32class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000033class ArenaStack;
34class ScopedArenaAllocator;
35class MemStats;
36
37static constexpr bool kArenaAllocatorCountAllocations = false;
38
39// Type of allocation for memory tuning.
40enum ArenaAllocKind {
41 kArenaAllocMisc,
42 kArenaAllocBB,
43 kArenaAllocLIR,
Vladimir Marko8dea81c2014-06-06 14:50:36 +010044 kArenaAllocLIRResourceMask,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000045 kArenaAllocMIR,
46 kArenaAllocDFInfo,
47 kArenaAllocGrowableArray,
48 kArenaAllocGrowableBitMap,
49 kArenaAllocDalvikToSSAMap,
50 kArenaAllocDebugInfo,
51 kArenaAllocSuccessor,
52 kArenaAllocRegAlloc,
53 kArenaAllocData,
54 kArenaAllocPredecessors,
55 kArenaAllocSTL,
56 kNumArenaAllocKinds
57};
58
59template <bool kCount>
60class ArenaAllocatorStatsImpl;
61
62template <>
63class ArenaAllocatorStatsImpl<false> {
64 public:
65 ArenaAllocatorStatsImpl() = default;
66 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
67 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
68
69 void Copy(const ArenaAllocatorStatsImpl& other) { UNUSED(other); }
70 void RecordAlloc(size_t bytes, ArenaAllocKind kind) { UNUSED(bytes); UNUSED(kind); }
71 size_t NumAllocations() const { return 0u; }
72 size_t BytesAllocated() const { return 0u; }
73 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const {
74 UNUSED(os); UNUSED(first); UNUSED(lost_bytes_adjustment);
75 }
76};
77
78template <bool kCount>
79class ArenaAllocatorStatsImpl {
80 public:
81 ArenaAllocatorStatsImpl();
82 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
83 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
84
85 void Copy(const ArenaAllocatorStatsImpl& other);
86 void RecordAlloc(size_t bytes, ArenaAllocKind kind);
87 size_t NumAllocations() const;
88 size_t BytesAllocated() const;
89 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const;
90
91 private:
92 size_t num_allocations_;
93 // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL.
94 size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds.
Vladimir Markobd9e9db2014-03-07 19:41:05 +000095
Vladimir Marko8dea81c2014-06-06 14:50:36 +010096 static const char* const kAllocNames[];
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000097};
98
99typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats;
buzbee862a7602013-04-05 10:58:54 -0700100
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700101class Arena {
102 public:
103 static constexpr size_t kDefaultSize = 128 * KB;
104 explicit Arena(size_t size = kDefaultSize);
105 ~Arena();
106 void Reset();
107 uint8_t* Begin() {
108 return memory_;
buzbee862a7602013-04-05 10:58:54 -0700109 }
110
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700111 uint8_t* End() {
112 return memory_ + size_;
113 }
114
115 size_t Size() const {
116 return size_;
117 }
118
119 size_t RemainingSpace() const {
120 return Size() - bytes_allocated_;
121 }
122
123 private:
124 size_t bytes_allocated_;
125 uint8_t* memory_;
126 size_t size_;
127 MemMap* map_;
128 Arena* next_;
129 friend class ArenaPool;
130 friend class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000131 friend class ArenaStack;
132 friend class ScopedArenaAllocator;
133 template <bool kCount> friend class ArenaAllocatorStatsImpl;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700134 DISALLOW_COPY_AND_ASSIGN(Arena);
135};
136
137class ArenaPool {
138 public:
139 ArenaPool();
140 ~ArenaPool();
141 Arena* AllocArena(size_t size);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000142 void FreeArenaChain(Arena* first);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700143
144 private:
145 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
146 Arena* free_arenas_ GUARDED_BY(lock_);
147 DISALLOW_COPY_AND_ASSIGN(ArenaPool);
148};
149
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000150class ArenaAllocator : private ArenaAllocatorStats {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700151 public:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700152 explicit ArenaAllocator(ArenaPool* pool);
153 ~ArenaAllocator();
154
155 // Returns zeroed memory.
156 void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE {
Mathieu Chartier75165d02013-09-12 14:00:31 -0700157 if (UNLIKELY(running_on_valgrind_)) {
158 return AllocValgrind(bytes, kind);
159 }
Vladimir Marko22a0ef82014-06-10 14:47:51 +0100160 bytes = RoundUp(bytes, 8);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700161 if (UNLIKELY(ptr_ + bytes > end_)) {
162 // Obtain a new block.
163 ObtainNewArenaForAllocation(bytes);
164 if (UNLIKELY(ptr_ == nullptr)) {
165 return nullptr;
166 }
167 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000168 ArenaAllocatorStats::RecordAlloc(bytes, kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700169 uint8_t* ret = ptr_;
170 ptr_ += bytes;
171 return ret;
172 }
173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100174 template <typename T> T* AllocArray(size_t length) {
175 return static_cast<T*>(Alloc(length * sizeof(T), kArenaAllocMisc));
176 }
177
Mathieu Chartier75165d02013-09-12 14:00:31 -0700178 void* AllocValgrind(size_t bytes, ArenaAllocKind kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700179 void ObtainNewArenaForAllocation(size_t allocation_size);
180 size_t BytesAllocated() const;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000181 MemStats GetMemStats() const;
buzbee862a7602013-04-05 10:58:54 -0700182
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700183 private:
184 void UpdateBytesAllocated();
buzbee862a7602013-04-05 10:58:54 -0700185
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700186 ArenaPool* pool_;
187 uint8_t* begin_;
188 uint8_t* end_;
189 uint8_t* ptr_;
190 Arena* arena_head_;
Mathieu Chartier75165d02013-09-12 14:00:31 -0700191 bool running_on_valgrind_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700192
193 DISALLOW_COPY_AND_ASSIGN(ArenaAllocator);
buzbee862a7602013-04-05 10:58:54 -0700194}; // ArenaAllocator
195
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000196class MemStats {
197 public:
198 MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
199 ssize_t lost_bytes_adjustment = 0);
200 void Dump(std::ostream& os) const;
201
202 private:
203 const char* const name_;
204 const ArenaAllocatorStats* const stats_;
205 const Arena* const first_arena_;
206 const ssize_t lost_bytes_adjustment_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700207}; // MemStats
buzbee862a7602013-04-05 10:58:54 -0700208
209} // namespace art
210
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000211#endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_