blob: a6b74f77f51321a95f2cdd7ddee5dd935b3d3c24 [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"
buzbee862a7602013-04-05 10:58:54 -070026
27namespace art {
28
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070029class Arena;
30class ArenaPool;
31class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000032class ArenaStack;
33class ScopedArenaAllocator;
34class MemStats;
35
36static constexpr bool kArenaAllocatorCountAllocations = false;
37
38// Type of allocation for memory tuning.
39enum ArenaAllocKind {
40 kArenaAllocMisc,
41 kArenaAllocBB,
42 kArenaAllocLIR,
43 kArenaAllocMIR,
44 kArenaAllocDFInfo,
45 kArenaAllocGrowableArray,
46 kArenaAllocGrowableBitMap,
47 kArenaAllocDalvikToSSAMap,
48 kArenaAllocDebugInfo,
49 kArenaAllocSuccessor,
50 kArenaAllocRegAlloc,
51 kArenaAllocData,
52 kArenaAllocPredecessors,
53 kArenaAllocSTL,
54 kNumArenaAllocKinds
55};
56
57template <bool kCount>
58class ArenaAllocatorStatsImpl;
59
60template <>
61class ArenaAllocatorStatsImpl<false> {
62 public:
63 ArenaAllocatorStatsImpl() = default;
64 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
65 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
66
67 void Copy(const ArenaAllocatorStatsImpl& other) { UNUSED(other); }
68 void RecordAlloc(size_t bytes, ArenaAllocKind kind) { UNUSED(bytes); UNUSED(kind); }
69 size_t NumAllocations() const { return 0u; }
70 size_t BytesAllocated() const { return 0u; }
71 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const {
72 UNUSED(os); UNUSED(first); UNUSED(lost_bytes_adjustment);
73 }
74};
75
76template <bool kCount>
77class ArenaAllocatorStatsImpl {
78 public:
79 ArenaAllocatorStatsImpl();
80 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
81 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
82
83 void Copy(const ArenaAllocatorStatsImpl& other);
84 void RecordAlloc(size_t bytes, ArenaAllocKind kind);
85 size_t NumAllocations() const;
86 size_t BytesAllocated() const;
87 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const;
88
89 private:
90 size_t num_allocations_;
91 // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL.
92 size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds.
93};
94
95typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats;
buzbee862a7602013-04-05 10:58:54 -070096
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070097class Arena {
98 public:
99 static constexpr size_t kDefaultSize = 128 * KB;
100 explicit Arena(size_t size = kDefaultSize);
101 ~Arena();
102 void Reset();
103 uint8_t* Begin() {
104 return memory_;
buzbee862a7602013-04-05 10:58:54 -0700105 }
106
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700107 uint8_t* End() {
108 return memory_ + size_;
109 }
110
111 size_t Size() const {
112 return size_;
113 }
114
115 size_t RemainingSpace() const {
116 return Size() - bytes_allocated_;
117 }
118
119 private:
120 size_t bytes_allocated_;
121 uint8_t* memory_;
122 size_t size_;
123 MemMap* map_;
124 Arena* next_;
125 friend class ArenaPool;
126 friend class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000127 friend class ArenaStack;
128 friend class ScopedArenaAllocator;
129 template <bool kCount> friend class ArenaAllocatorStatsImpl;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700130 DISALLOW_COPY_AND_ASSIGN(Arena);
131};
132
133class ArenaPool {
134 public:
135 ArenaPool();
136 ~ArenaPool();
137 Arena* AllocArena(size_t size);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000138 void FreeArenaChain(Arena* first);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700139
140 private:
141 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
142 Arena* free_arenas_ GUARDED_BY(lock_);
143 DISALLOW_COPY_AND_ASSIGN(ArenaPool);
144};
145
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000146class ArenaAllocator : private ArenaAllocatorStats {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700147 public:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700148 explicit ArenaAllocator(ArenaPool* pool);
149 ~ArenaAllocator();
150
151 // Returns zeroed memory.
152 void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE {
Mathieu Chartier75165d02013-09-12 14:00:31 -0700153 if (UNLIKELY(running_on_valgrind_)) {
154 return AllocValgrind(bytes, kind);
155 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700156 bytes = (bytes + 3) & ~3;
157 if (UNLIKELY(ptr_ + bytes > end_)) {
158 // Obtain a new block.
159 ObtainNewArenaForAllocation(bytes);
160 if (UNLIKELY(ptr_ == nullptr)) {
161 return nullptr;
162 }
163 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000164 ArenaAllocatorStats::RecordAlloc(bytes, kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700165 uint8_t* ret = ptr_;
166 ptr_ += bytes;
167 return ret;
168 }
169
Mathieu Chartier75165d02013-09-12 14:00:31 -0700170 void* AllocValgrind(size_t bytes, ArenaAllocKind kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700171 void ObtainNewArenaForAllocation(size_t allocation_size);
172 size_t BytesAllocated() const;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000173 MemStats GetMemStats() const;
buzbee862a7602013-04-05 10:58:54 -0700174
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700175 private:
176 void UpdateBytesAllocated();
buzbee862a7602013-04-05 10:58:54 -0700177
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700178 ArenaPool* pool_;
179 uint8_t* begin_;
180 uint8_t* end_;
181 uint8_t* ptr_;
182 Arena* arena_head_;
Mathieu Chartier75165d02013-09-12 14:00:31 -0700183 bool running_on_valgrind_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700184
185 DISALLOW_COPY_AND_ASSIGN(ArenaAllocator);
buzbee862a7602013-04-05 10:58:54 -0700186}; // ArenaAllocator
187
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000188class MemStats {
189 public:
190 MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
191 ssize_t lost_bytes_adjustment = 0);
192 void Dump(std::ostream& os) const;
193
194 private:
195 const char* const name_;
196 const ArenaAllocatorStats* const stats_;
197 const Arena* const first_arena_;
198 const ssize_t lost_bytes_adjustment_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700199}; // MemStats
buzbee862a7602013-04-05 10:58:54 -0700200
201} // namespace art
202
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203#endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_