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_GROWABLE_ARRAY_H_ |
| 18 | #define ART_COMPILER_UTILS_GROWABLE_ARRAY_H_ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 22 | #include "arena_allocator.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 26 | // Type of growable list for memory tuning. |
| 27 | enum OatListKind { |
| 28 | kGrowableArrayMisc = 0, |
| 29 | kGrowableArrayBlockList, |
| 30 | kGrowableArraySSAtoDalvikMap, |
| 31 | kGrowableArrayDfsOrder, |
| 32 | kGrowableArrayDfsPostOrder, |
| 33 | kGrowableArrayDomPostOrderTraversal, |
| 34 | kGrowableArrayThrowLaunchPads, |
| 35 | kGrowableArraySuspendLaunchPads, |
| 36 | kGrowableArraySwitchTables, |
| 37 | kGrowableArrayFillArrayData, |
| 38 | kGrowableArraySuccessorBlocks, |
| 39 | kGrowableArrayPredecessors, |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 40 | kGrowableArraySlowPaths, |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 41 | kGNumListKinds |
| 42 | }; |
| 43 | |
| 44 | template<typename T> |
| 45 | class GrowableArray { |
| 46 | public: |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 47 | class Iterator { |
| 48 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 49 | explicit Iterator(GrowableArray* g_list) |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 50 | : idx_(0), |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 51 | g_list_(g_list) {} |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 52 | |
| 53 | // NOTE: returns 0/NULL when no next. |
| 54 | // TODO: redo to make usage consistent with other iterators. |
| 55 | T Next() { |
| 56 | if (idx_ >= g_list_->Size()) { |
| 57 | return 0; |
| 58 | } else { |
| 59 | return g_list_->Get(idx_++); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Reset() { |
| 64 | idx_ = 0; |
| 65 | } |
| 66 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 67 | private: |
| 68 | size_t idx_; |
| 69 | GrowableArray* const g_list_; |
| 70 | }; |
| 71 | |
| 72 | GrowableArray(ArenaAllocator* arena, size_t init_length, OatListKind kind = kGrowableArrayMisc) |
| 73 | : arena_(arena), |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame] | 74 | num_allocated_(init_length), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 75 | num_used_(0), |
| 76 | kind_(kind) { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 77 | elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 78 | kArenaAllocGrowableArray)); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
| 82 | // Expand the list size to at least new length. |
| 83 | void Resize(size_t new_length) { |
| 84 | if (new_length <= num_allocated_) return; |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame] | 85 | // If it's a small list double the size, else grow 1.5x. |
| 86 | size_t target_length = |
| 87 | (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 88 | if (new_length > target_length) { |
| 89 | target_length = new_length; |
| 90 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 91 | T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 92 | kArenaAllocGrowableArray)); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 93 | memcpy(new_array, elem_list_, sizeof(T) * num_allocated_); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 94 | num_allocated_ = target_length; |
| 95 | elem_list_ = new_array; |
| 96 | }; |
| 97 | |
| 98 | // NOTE: does not return storage, just resets use count. |
| 99 | void Reset() { |
| 100 | num_used_ = 0; |
| 101 | } |
| 102 | |
| 103 | // Insert an element to the end of a list, resizing if necessary. |
| 104 | void Insert(T elem) { |
| 105 | if (num_used_ == num_allocated_) { |
| 106 | Resize(num_used_ + 1); |
| 107 | } |
| 108 | elem_list_[num_used_++] = elem; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void InsertAt(size_t index, T elem) { |
| 112 | DCHECK(index <= Size()); |
| 113 | Insert(elem); |
| 114 | for (size_t i = Size() - 1; i > index; --i) { |
| 115 | elem_list_[i] = elem_list_[i - 1]; |
| 116 | } |
| 117 | elem_list_[index] = elem; |
| 118 | } |
| 119 | |
| 120 | void Add(T elem) { |
| 121 | Insert(elem); |
| 122 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 123 | |
| 124 | T Get(size_t index) const { |
| 125 | DCHECK_LT(index, num_used_); |
| 126 | return elem_list_[index]; |
| 127 | }; |
| 128 | |
| 129 | // Overwrite existing element at position index. List must be large enough. |
| 130 | void Put(size_t index, T elem) { |
| 131 | DCHECK_LT(index, num_used_); |
| 132 | elem_list_[index] = elem; |
| 133 | } |
| 134 | |
| 135 | void Increment(size_t index) { |
| 136 | DCHECK_LT(index, num_used_); |
| 137 | elem_list_[index]++; |
| 138 | } |
| 139 | |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 140 | /* |
| 141 | * Remove an existing element from list. If there are more than one copy |
| 142 | * of the element, only the first one encountered will be deleted. |
| 143 | */ |
| 144 | // TODO: consider renaming this. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 145 | void Delete(T element) { |
| 146 | bool found = false; |
| 147 | for (size_t i = 0; i < num_used_ - 1; i++) { |
| 148 | if (!found && elem_list_[i] == element) { |
| 149 | found = true; |
| 150 | } |
| 151 | if (found) { |
| 152 | elem_list_[i] = elem_list_[i+1]; |
| 153 | } |
| 154 | } |
| 155 | // We should either have found the element, or it was the last (unscanned) element. |
| 156 | DCHECK(found || (element == elem_list_[num_used_ - 1])); |
| 157 | num_used_--; |
| 158 | }; |
| 159 | |
| 160 | size_t GetNumAllocated() const { return num_allocated_; } |
| 161 | |
| 162 | size_t Size() const { return num_used_; } |
| 163 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 164 | bool IsEmpty() const { return num_used_ == 0; } |
| 165 | |
| 166 | T Pop() { |
| 167 | DCHECK_GE(num_used_, (size_t)0); |
| 168 | return elem_list_[--num_used_]; |
| 169 | } |
| 170 | |
| 171 | T Peek() const { |
| 172 | DCHECK_GE(num_used_, (size_t)0); |
| 173 | return elem_list_[num_used_ - 1]; |
| 174 | } |
| 175 | |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 176 | void SetSize(size_t new_size) { |
| 177 | Resize(new_size); |
| 178 | num_used_ = new_size; |
| 179 | } |
| 180 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 181 | T* GetRawStorage() const { return elem_list_; } |
| 182 | |
| 183 | static void* operator new(size_t size, ArenaAllocator* arena) { |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 184 | return arena->Alloc(sizeof(GrowableArray<T>), kArenaAllocGrowableArray); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 185 | }; |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 186 | static void operator delete(void* p) {} // Nop. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 187 | |
| 188 | private: |
| 189 | ArenaAllocator* const arena_; |
| 190 | size_t num_allocated_; |
| 191 | size_t num_used_; |
| 192 | OatListKind kind_; |
| 193 | T* elem_list_; |
| 194 | }; |
| 195 | |
| 196 | } // namespace art |
| 197 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 198 | #endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_ |