blob: 18a5bce77dc285bed070d58b5a2eb0a9abca4eae [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.
Vladimir Markobd9e9db2014-03-07 19:41:05 +000093
94 static const char* kAllocNames[kNumArenaAllocKinds];
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000095};
96
97typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats;
buzbee862a7602013-04-05 10:58:54 -070098
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070099class Arena {
100 public:
101 static constexpr size_t kDefaultSize = 128 * KB;
102 explicit Arena(size_t size = kDefaultSize);
103 ~Arena();
104 void Reset();
105 uint8_t* Begin() {
106 return memory_;
buzbee862a7602013-04-05 10:58:54 -0700107 }
108
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700109 uint8_t* End() {
110 return memory_ + size_;
111 }
112
113 size_t Size() const {
114 return size_;
115 }
116
117 size_t RemainingSpace() const {
118 return Size() - bytes_allocated_;
119 }
120
121 private:
122 size_t bytes_allocated_;
123 uint8_t* memory_;
124 size_t size_;
125 MemMap* map_;
126 Arena* next_;
127 friend class ArenaPool;
128 friend class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000129 friend class ArenaStack;
130 friend class ScopedArenaAllocator;
131 template <bool kCount> friend class ArenaAllocatorStatsImpl;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700132 DISALLOW_COPY_AND_ASSIGN(Arena);
133};
134
135class ArenaPool {
136 public:
137 ArenaPool();
138 ~ArenaPool();
139 Arena* AllocArena(size_t size);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000140 void FreeArenaChain(Arena* first);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700141
142 private:
143 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
144 Arena* free_arenas_ GUARDED_BY(lock_);
145 DISALLOW_COPY_AND_ASSIGN(ArenaPool);
146};
147
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000148class ArenaAllocator : private ArenaAllocatorStats {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700149 public:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700150 explicit ArenaAllocator(ArenaPool* pool);
151 ~ArenaAllocator();
152
153 // Returns zeroed memory.
154 void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE {
Mathieu Chartier75165d02013-09-12 14:00:31 -0700155 if (UNLIKELY(running_on_valgrind_)) {
156 return AllocValgrind(bytes, kind);
157 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700158 bytes = (bytes + 3) & ~3;
159 if (UNLIKELY(ptr_ + bytes > end_)) {
160 // Obtain a new block.
161 ObtainNewArenaForAllocation(bytes);
162 if (UNLIKELY(ptr_ == nullptr)) {
163 return nullptr;
164 }
165 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000166 ArenaAllocatorStats::RecordAlloc(bytes, kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700167 uint8_t* ret = ptr_;
168 ptr_ += bytes;
169 return ret;
170 }
171
Mathieu Chartier75165d02013-09-12 14:00:31 -0700172 void* AllocValgrind(size_t bytes, ArenaAllocKind kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700173 void ObtainNewArenaForAllocation(size_t allocation_size);
174 size_t BytesAllocated() const;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000175 MemStats GetMemStats() const;
buzbee862a7602013-04-05 10:58:54 -0700176
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700177 private:
178 void UpdateBytesAllocated();
buzbee862a7602013-04-05 10:58:54 -0700179
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700180 ArenaPool* pool_;
181 uint8_t* begin_;
182 uint8_t* end_;
183 uint8_t* ptr_;
184 Arena* arena_head_;
Mathieu Chartier75165d02013-09-12 14:00:31 -0700185 bool running_on_valgrind_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700186
187 DISALLOW_COPY_AND_ASSIGN(ArenaAllocator);
buzbee862a7602013-04-05 10:58:54 -0700188}; // ArenaAllocator
189
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000190class MemStats {
191 public:
192 MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
193 ssize_t lost_bytes_adjustment = 0);
194 void Dump(std::ostream& os) const;
195
196 private:
197 const char* const name_;
198 const ArenaAllocatorStats* const stats_;
199 const Arena* const first_arena_;
200 const ssize_t lost_bytes_adjustment_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700201}; // MemStats
buzbee862a7602013-04-05 10:58:54 -0700202
203} // namespace art
204
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000205#endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_