blob: 032eabc7df2383b8be24d3ac604bf725488f7556 [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,
44 kArenaAllocMIR,
45 kArenaAllocDFInfo,
46 kArenaAllocGrowableArray,
47 kArenaAllocGrowableBitMap,
48 kArenaAllocDalvikToSSAMap,
49 kArenaAllocDebugInfo,
50 kArenaAllocSuccessor,
51 kArenaAllocRegAlloc,
52 kArenaAllocData,
53 kArenaAllocPredecessors,
54 kArenaAllocSTL,
55 kNumArenaAllocKinds
56};
57
58template <bool kCount>
59class ArenaAllocatorStatsImpl;
60
61template <>
62class ArenaAllocatorStatsImpl<false> {
63 public:
64 ArenaAllocatorStatsImpl() = default;
65 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
66 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
67
68 void Copy(const ArenaAllocatorStatsImpl& other) { UNUSED(other); }
69 void RecordAlloc(size_t bytes, ArenaAllocKind kind) { UNUSED(bytes); UNUSED(kind); }
70 size_t NumAllocations() const { return 0u; }
71 size_t BytesAllocated() const { return 0u; }
72 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const {
73 UNUSED(os); UNUSED(first); UNUSED(lost_bytes_adjustment);
74 }
75};
76
77template <bool kCount>
78class ArenaAllocatorStatsImpl {
79 public:
80 ArenaAllocatorStatsImpl();
81 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
82 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
83
84 void Copy(const ArenaAllocatorStatsImpl& other);
85 void RecordAlloc(size_t bytes, ArenaAllocKind kind);
86 size_t NumAllocations() const;
87 size_t BytesAllocated() const;
88 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const;
89
90 private:
91 size_t num_allocations_;
92 // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL.
93 size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds.
Vladimir Markobd9e9db2014-03-07 19:41:05 +000094
95 static const char* kAllocNames[kNumArenaAllocKinds];
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000096};
97
98typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats;
buzbee862a7602013-04-05 10:58:54 -070099
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700100class Arena {
101 public:
102 static constexpr size_t kDefaultSize = 128 * KB;
103 explicit Arena(size_t size = kDefaultSize);
104 ~Arena();
105 void Reset();
106 uint8_t* Begin() {
107 return memory_;
buzbee862a7602013-04-05 10:58:54 -0700108 }
109
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700110 uint8_t* End() {
111 return memory_ + size_;
112 }
113
114 size_t Size() const {
115 return size_;
116 }
117
118 size_t RemainingSpace() const {
119 return Size() - bytes_allocated_;
120 }
121
122 private:
123 size_t bytes_allocated_;
124 uint8_t* memory_;
125 size_t size_;
126 MemMap* map_;
127 Arena* next_;
128 friend class ArenaPool;
129 friend class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000130 friend class ArenaStack;
131 friend class ScopedArenaAllocator;
132 template <bool kCount> friend class ArenaAllocatorStatsImpl;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700133 DISALLOW_COPY_AND_ASSIGN(Arena);
134};
135
136class ArenaPool {
137 public:
138 ArenaPool();
139 ~ArenaPool();
140 Arena* AllocArena(size_t size);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000141 void FreeArenaChain(Arena* first);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700142
143 private:
144 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
145 Arena* free_arenas_ GUARDED_BY(lock_);
146 DISALLOW_COPY_AND_ASSIGN(ArenaPool);
147};
148
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000149class ArenaAllocator : private ArenaAllocatorStats {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700150 public:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700151 explicit ArenaAllocator(ArenaPool* pool);
152 ~ArenaAllocator();
153
154 // Returns zeroed memory.
155 void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE {
Mathieu Chartier75165d02013-09-12 14:00:31 -0700156 if (UNLIKELY(running_on_valgrind_)) {
157 return AllocValgrind(bytes, kind);
158 }
Andreas Gampe66018822014-05-05 20:47:19 -0700159 bytes = RoundUp(bytes, 4);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700160 if (UNLIKELY(ptr_ + bytes > end_)) {
161 // Obtain a new block.
162 ObtainNewArenaForAllocation(bytes);
163 if (UNLIKELY(ptr_ == nullptr)) {
164 return nullptr;
165 }
166 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000167 ArenaAllocatorStats::RecordAlloc(bytes, kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700168 uint8_t* ret = ptr_;
169 ptr_ += bytes;
170 return ret;
171 }
172
Mathieu Chartier75165d02013-09-12 14:00:31 -0700173 void* AllocValgrind(size_t bytes, ArenaAllocKind kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700174 void ObtainNewArenaForAllocation(size_t allocation_size);
175 size_t BytesAllocated() const;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000176 MemStats GetMemStats() const;
buzbee862a7602013-04-05 10:58:54 -0700177
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700178 private:
179 void UpdateBytesAllocated();
buzbee862a7602013-04-05 10:58:54 -0700180
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700181 ArenaPool* pool_;
182 uint8_t* begin_;
183 uint8_t* end_;
184 uint8_t* ptr_;
185 Arena* arena_head_;
Mathieu Chartier75165d02013-09-12 14:00:31 -0700186 bool running_on_valgrind_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700187
188 DISALLOW_COPY_AND_ASSIGN(ArenaAllocator);
buzbee862a7602013-04-05 10:58:54 -0700189}; // ArenaAllocator
190
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000191class MemStats {
192 public:
193 MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
194 ssize_t lost_bytes_adjustment = 0);
195 void Dump(std::ostream& os) const;
196
197 private:
198 const char* const name_;
199 const ArenaAllocatorStats* const stats_;
200 const Arena* const first_arena_;
201 const ssize_t lost_bytes_adjustment_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700202}; // MemStats
buzbee862a7602013-04-05 10:58:54 -0700203
204} // namespace art
205
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206#endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_