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 | |
| 23 | #include "base/mutex.h" |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 24 | #include "mem_map.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 28 | class Arena; |
| 29 | class ArenaPool; |
| 30 | class ArenaAllocator; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 31 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 32 | class Arena { |
| 33 | public: |
| 34 | static constexpr size_t kDefaultSize = 128 * KB; |
| 35 | explicit Arena(size_t size = kDefaultSize); |
| 36 | ~Arena(); |
| 37 | void Reset(); |
| 38 | uint8_t* Begin() { |
| 39 | return memory_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 42 | uint8_t* End() { |
| 43 | return memory_ + size_; |
| 44 | } |
| 45 | |
| 46 | size_t Size() const { |
| 47 | return size_; |
| 48 | } |
| 49 | |
| 50 | size_t RemainingSpace() const { |
| 51 | return Size() - bytes_allocated_; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | size_t bytes_allocated_; |
| 56 | uint8_t* memory_; |
| 57 | size_t size_; |
| 58 | MemMap* map_; |
| 59 | Arena* next_; |
| 60 | friend class ArenaPool; |
| 61 | friend class ArenaAllocator; |
| 62 | DISALLOW_COPY_AND_ASSIGN(Arena); |
| 63 | }; |
| 64 | |
| 65 | class ArenaPool { |
| 66 | public: |
| 67 | ArenaPool(); |
| 68 | ~ArenaPool(); |
| 69 | Arena* AllocArena(size_t size); |
| 70 | void FreeArena(Arena* arena); |
| 71 | |
| 72 | private: |
| 73 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 74 | Arena* free_arenas_ GUARDED_BY(lock_); |
| 75 | DISALLOW_COPY_AND_ASSIGN(ArenaPool); |
| 76 | }; |
| 77 | |
| 78 | class ArenaAllocator { |
| 79 | public: |
| 80 | // Type of allocation for memory tuning. |
| 81 | enum ArenaAllocKind { |
| 82 | kAllocMisc, |
| 83 | kAllocBB, |
| 84 | kAllocLIR, |
| 85 | kAllocMIR, |
| 86 | kAllocDFInfo, |
| 87 | kAllocGrowableArray, |
| 88 | kAllocGrowableBitMap, |
| 89 | kAllocDalvikToSSAMap, |
| 90 | kAllocDebugInfo, |
| 91 | kAllocSuccessor, |
| 92 | kAllocRegAlloc, |
| 93 | kAllocData, |
| 94 | kAllocPredecessors, |
| 95 | kNumAllocKinds |
| 96 | }; |
| 97 | |
| 98 | static constexpr bool kCountAllocations = false; |
| 99 | |
| 100 | explicit ArenaAllocator(ArenaPool* pool); |
| 101 | ~ArenaAllocator(); |
| 102 | |
| 103 | // Returns zeroed memory. |
| 104 | void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE { |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 105 | if (UNLIKELY(running_on_valgrind_)) { |
| 106 | return AllocValgrind(bytes, kind); |
| 107 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 108 | bytes = (bytes + 3) & ~3; |
| 109 | if (UNLIKELY(ptr_ + bytes > end_)) { |
| 110 | // Obtain a new block. |
| 111 | ObtainNewArenaForAllocation(bytes); |
| 112 | if (UNLIKELY(ptr_ == nullptr)) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | } |
| 116 | if (kCountAllocations) { |
| 117 | alloc_stats_[kind] += bytes; |
| 118 | ++num_allocations_; |
| 119 | } |
| 120 | uint8_t* ret = ptr_; |
| 121 | ptr_ += bytes; |
| 122 | return ret; |
| 123 | } |
| 124 | |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 125 | void* AllocValgrind(size_t bytes, ArenaAllocKind kind); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 126 | void ObtainNewArenaForAllocation(size_t allocation_size); |
| 127 | size_t BytesAllocated() const; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 128 | void DumpMemStats(std::ostream& os) const; |
| 129 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 130 | private: |
| 131 | void UpdateBytesAllocated(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 132 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 133 | ArenaPool* pool_; |
| 134 | uint8_t* begin_; |
| 135 | uint8_t* end_; |
| 136 | uint8_t* ptr_; |
| 137 | Arena* arena_head_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 138 | size_t num_allocations_; |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 139 | size_t alloc_stats_[kNumAllocKinds]; // Bytes used by various allocation kinds. |
| 140 | bool running_on_valgrind_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 141 | |
| 142 | DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 143 | }; // ArenaAllocator |
| 144 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 145 | struct MemStats { |
| 146 | public: |
| 147 | void Dump(std::ostream& os) const { |
| 148 | arena_.DumpMemStats(os); |
| 149 | } |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 150 | explicit MemStats(const ArenaAllocator &arena) : arena_(arena) {} |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 151 | private: |
| 152 | const ArenaAllocator &arena_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 153 | }; // MemStats |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 154 | |
| 155 | } // namespace art |
| 156 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame^] | 157 | #endif // ART_COMPILER_UTILS_ARENA_ALLOCATOR_H_ |