blob: e6c53dab249f9bd4e7389db8e9fe8eef1037561f [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_GROWABLE_ARRAY_H_
18#define ART_COMPILER_UTILS_GROWABLE_ARRAY_H_
buzbee862a7602013-04-05 10:58:54 -070019
20#include <stdint.h>
21#include <stddef.h>
buzbee862a7602013-04-05 10:58:54 -070022#include "arena_allocator.h"
23
24namespace art {
25
buzbee862a7602013-04-05 10:58:54 -070026// Type of growable list for memory tuning.
27enum 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 Allisonbcec6fb2014-01-17 12:52:22 -080040 kGrowableArraySlowPaths,
buzbee862a7602013-04-05 10:58:54 -070041 kGNumListKinds
42};
43
44template<typename T>
45class GrowableArray {
46 public:
buzbee862a7602013-04-05 10:58:54 -070047 class Iterator {
48 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070049 explicit Iterator(GrowableArray* g_list)
buzbee862a7602013-04-05 10:58:54 -070050 : idx_(0),
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070051 g_list_(g_list) {}
buzbee862a7602013-04-05 10:58:54 -070052
Jean Christophe Beyler1ceea7e2014-04-15 16:18:48 -070053 explicit Iterator()
54 : idx_(0),
55 g_list_(nullptr) {}
56
buzbee862a7602013-04-05 10:58:54 -070057 // NOTE: returns 0/NULL when no next.
58 // TODO: redo to make usage consistent with other iterators.
59 T Next() {
Jean Christophe Beyler1ceea7e2014-04-15 16:18:48 -070060 DCHECK(g_list_ != nullptr);
buzbee862a7602013-04-05 10:58:54 -070061 if (idx_ >= g_list_->Size()) {
62 return 0;
63 } else {
64 return g_list_->Get(idx_++);
65 }
66 }
67
68 void Reset() {
69 idx_ = 0;
70 }
71
Jean Christophe Beyler1ceea7e2014-04-15 16:18:48 -070072 void Reset(GrowableArray* g_list) {
73 idx_ = 0;
74 g_list_ = g_list;
75 }
76
77 size_t GetIndex() const {
78 return idx_;
79 }
80
buzbee862a7602013-04-05 10:58:54 -070081 private:
82 size_t idx_;
83 GrowableArray* const g_list_;
84 };
85
86 GrowableArray(ArenaAllocator* arena, size_t init_length, OatListKind kind = kGrowableArrayMisc)
87 : arena_(arena),
buzbeea5abf702013-04-12 14:39:29 -070088 num_allocated_(init_length),
buzbee862a7602013-04-05 10:58:54 -070089 num_used_(0),
90 kind_(kind) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070091 elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000092 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070093 };
94
95
96 // Expand the list size to at least new length.
97 void Resize(size_t new_length) {
98 if (new_length <= num_allocated_) return;
buzbeea5abf702013-04-12 14:39:29 -070099 // If it's a small list double the size, else grow 1.5x.
100 size_t target_length =
101 (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1);
buzbee862a7602013-04-05 10:58:54 -0700102 if (new_length > target_length) {
103 target_length = new_length;
104 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700105 T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000106 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -0700107 memcpy(new_array, elem_list_, sizeof(T) * num_allocated_);
buzbee862a7602013-04-05 10:58:54 -0700108 num_allocated_ = target_length;
109 elem_list_ = new_array;
110 };
111
112 // NOTE: does not return storage, just resets use count.
113 void Reset() {
114 num_used_ = 0;
115 }
116
117 // Insert an element to the end of a list, resizing if necessary.
118 void Insert(T elem) {
119 if (num_used_ == num_allocated_) {
120 Resize(num_used_ + 1);
121 }
122 elem_list_[num_used_++] = elem;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123 }
124
125 void InsertAt(size_t index, T elem) {
126 DCHECK(index <= Size());
127 Insert(elem);
128 for (size_t i = Size() - 1; i > index; --i) {
129 elem_list_[i] = elem_list_[i - 1];
130 }
131 elem_list_[index] = elem;
132 }
133
134 void Add(T elem) {
135 Insert(elem);
136 }
buzbee862a7602013-04-05 10:58:54 -0700137
138 T Get(size_t index) const {
139 DCHECK_LT(index, num_used_);
140 return elem_list_[index];
141 };
142
143 // Overwrite existing element at position index. List must be large enough.
144 void Put(size_t index, T elem) {
145 DCHECK_LT(index, num_used_);
146 elem_list_[index] = elem;
147 }
148
149 void Increment(size_t index) {
150 DCHECK_LT(index, num_used_);
151 elem_list_[index]++;
152 }
153
buzbeebd663de2013-09-10 15:41:31 -0700154 /*
155 * Remove an existing element from list. If there are more than one copy
156 * of the element, only the first one encountered will be deleted.
157 */
158 // TODO: consider renaming this.
buzbee862a7602013-04-05 10:58:54 -0700159 void Delete(T element) {
160 bool found = false;
161 for (size_t i = 0; i < num_used_ - 1; i++) {
162 if (!found && elem_list_[i] == element) {
163 found = true;
164 }
165 if (found) {
166 elem_list_[i] = elem_list_[i+1];
167 }
168 }
169 // We should either have found the element, or it was the last (unscanned) element.
170 DCHECK(found || (element == elem_list_[num_used_ - 1]));
171 num_used_--;
172 };
173
174 size_t GetNumAllocated() const { return num_allocated_; }
175
176 size_t Size() const { return num_used_; }
177
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000178 bool IsEmpty() const { return num_used_ == 0; }
179
180 T Pop() {
181 DCHECK_GE(num_used_, (size_t)0);
182 return elem_list_[--num_used_];
183 }
184
185 T Peek() const {
186 DCHECK_GE(num_used_, (size_t)0);
187 return elem_list_[num_used_ - 1];
188 }
189
buzbeebd663de2013-09-10 15:41:31 -0700190 void SetSize(size_t new_size) {
191 Resize(new_size);
192 num_used_ = new_size;
193 }
194
buzbee862a7602013-04-05 10:58:54 -0700195 T* GetRawStorage() const { return elem_list_; }
196
197 static void* operator new(size_t size, ArenaAllocator* arena) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000198 return arena->Alloc(sizeof(GrowableArray<T>), kArenaAllocGrowableArray);
buzbee862a7602013-04-05 10:58:54 -0700199 };
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700200 static void operator delete(void* p) {} // Nop.
buzbee862a7602013-04-05 10:58:54 -0700201
202 private:
203 ArenaAllocator* const arena_;
204 size_t num_allocated_;
205 size_t num_used_;
206 OatListKind kind_;
207 T* elem_list_;
208};
209
210} // namespace art
211
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000212#endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_