buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 17 | #ifndef ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_ |
| 18 | #define ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 22 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 23 | #include "base/macros.h" |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 24 | #include "base/mutex.h" |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 25 | #include "mem_map.h" |
Andreas Gampe | 6601882 | 2014-05-05 20:47:19 -0700 | [diff] [blame] | 26 | #include "utils.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 30 | class Arena; |
| 31 | class ArenaPool; |
| 32 | class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 33 | class ArenaStack; |
| 34 | class ScopedArenaAllocator; |
| 35 | class MemStats; |
| 36 | |
| 37 | static constexpr bool kArenaAllocatorCountAllocations = false; |
| 38 | |
| 39 | // Type of allocation for memory tuning. |
| 40 | enum ArenaAllocKind { |
| 41 | kArenaAllocMisc, |
| 42 | kArenaAllocBB, |
| 43 | kArenaAllocLIR, |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 44 | kArenaAllocLIRResourceMask, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 45 | 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 | |
| 59 | template <bool kCount> |
| 60 | class ArenaAllocatorStatsImpl; |
| 61 | |
| 62 | template <> |
| 63 | class 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 | |
| 78 | template <bool kCount> |
| 79 | class 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 Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 95 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 96 | static const char* const kAllocNames[]; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 100 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 101 | class 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_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 111 | 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 Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 131 | friend class ArenaStack; |
| 132 | friend class ScopedArenaAllocator; |
| 133 | template <bool kCount> friend class ArenaAllocatorStatsImpl; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 134 | DISALLOW_COPY_AND_ASSIGN(Arena); |
| 135 | }; |
| 136 | |
| 137 | class ArenaPool { |
| 138 | public: |
| 139 | ArenaPool(); |
| 140 | ~ArenaPool(); |
| 141 | Arena* AllocArena(size_t size); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 142 | void FreeArenaChain(Arena* first); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 143 | |
| 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 Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 150 | class ArenaAllocator : private ArenaAllocatorStats { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 151 | public: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 152 | explicit ArenaAllocator(ArenaPool* pool); |
| 153 | ~ArenaAllocator(); |
| 154 | |
| 155 | // Returns zeroed memory. |
| 156 | void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE { |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 157 | if (UNLIKELY(running_on_valgrind_)) { |
| 158 | return AllocValgrind(bytes, kind); |
| 159 | } |
Vladimir Marko | 22a0ef8 | 2014-06-10 14:47:51 +0100 | [diff] [blame] | 160 | bytes = RoundUp(bytes, 8); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 161 | if (UNLIKELY(ptr_ + bytes > end_)) { |
| 162 | // Obtain a new block. |
| 163 | ObtainNewArenaForAllocation(bytes); |
| 164 | if (UNLIKELY(ptr_ == nullptr)) { |
| 165 | return nullptr; |
| 166 | } |
| 167 | } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 168 | ArenaAllocatorStats::RecordAlloc(bytes, kind); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 169 | uint8_t* ret = ptr_; |
| 170 | ptr_ += bytes; |
| 171 | return ret; |
| 172 | } |
| 173 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 174 | template <typename T> T* AllocArray(size_t length) { |
| 175 | return static_cast<T*>(Alloc(length * sizeof(T), kArenaAllocMisc)); |
| 176 | } |
| 177 | |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 178 | void* AllocValgrind(size_t bytes, ArenaAllocKind kind); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 179 | void ObtainNewArenaForAllocation(size_t allocation_size); |
| 180 | size_t BytesAllocated() const; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 181 | MemStats GetMemStats() const; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 182 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 183 | private: |
| 184 | void UpdateBytesAllocated(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 185 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 186 | ArenaPool* pool_; |
| 187 | uint8_t* begin_; |
| 188 | uint8_t* end_; |
| 189 | uint8_t* ptr_; |
| 190 | Arena* arena_head_; |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 191 | bool running_on_valgrind_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 192 | |
| 193 | DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 194 | }; // ArenaAllocator |
| 195 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 196 | class 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 Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 207 | }; // MemStats |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 208 | |
| 209 | } // namespace art |
| 210 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 211 | #endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_ |