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" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 29 | class Arena; |
| 30 | class ArenaPool; |
| 31 | class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 32 | class ArenaStack; |
| 33 | class ScopedArenaAllocator; |
| 34 | class MemStats; |
| 35 | |
| 36 | static constexpr bool kArenaAllocatorCountAllocations = false; |
| 37 | |
| 38 | // Type of allocation for memory tuning. |
| 39 | enum 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 | |
| 57 | template <bool kCount> |
| 58 | class ArenaAllocatorStatsImpl; |
| 59 | |
| 60 | template <> |
| 61 | class 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 | |
| 76 | template <bool kCount> |
| 77 | class 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 Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 93 | |
| 94 | static const char* kAllocNames[kNumArenaAllocKinds]; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 98 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 99 | class 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_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 109 | 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 Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 129 | friend class ArenaStack; |
| 130 | friend class ScopedArenaAllocator; |
| 131 | template <bool kCount> friend class ArenaAllocatorStatsImpl; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 132 | DISALLOW_COPY_AND_ASSIGN(Arena); |
| 133 | }; |
| 134 | |
| 135 | class ArenaPool { |
| 136 | public: |
| 137 | ArenaPool(); |
| 138 | ~ArenaPool(); |
| 139 | Arena* AllocArena(size_t size); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 140 | void FreeArenaChain(Arena* first); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 141 | |
| 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 Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 148 | class ArenaAllocator : private ArenaAllocatorStats { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 149 | public: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 150 | explicit ArenaAllocator(ArenaPool* pool); |
| 151 | ~ArenaAllocator(); |
| 152 | |
| 153 | // Returns zeroed memory. |
| 154 | void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE { |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 155 | if (UNLIKELY(running_on_valgrind_)) { |
| 156 | return AllocValgrind(bytes, kind); |
| 157 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 158 | 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 Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 166 | ArenaAllocatorStats::RecordAlloc(bytes, kind); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 167 | uint8_t* ret = ptr_; |
| 168 | ptr_ += bytes; |
| 169 | return ret; |
| 170 | } |
| 171 | |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 172 | void* AllocValgrind(size_t bytes, ArenaAllocKind kind); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 173 | void ObtainNewArenaForAllocation(size_t allocation_size); |
| 174 | size_t BytesAllocated() const; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 175 | MemStats GetMemStats() const; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 176 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 177 | private: |
| 178 | void UpdateBytesAllocated(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 179 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 180 | ArenaPool* pool_; |
| 181 | uint8_t* begin_; |
| 182 | uint8_t* end_; |
| 183 | uint8_t* ptr_; |
| 184 | Arena* arena_head_; |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 185 | bool running_on_valgrind_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 186 | |
| 187 | DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 188 | }; // ArenaAllocator |
| 189 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 190 | class 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 Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 201 | }; // MemStats |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 202 | |
| 203 | } // namespace art |
| 204 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 205 | #endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_ |