blob: 659b4f76a0021a3e1f62ab99b91d014ac40bbc43 [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,
buzbee862a7602013-04-05 10:58:54 -070034 kGrowableArraySwitchTables,
35 kGrowableArrayFillArrayData,
36 kGrowableArraySuccessorBlocks,
37 kGrowableArrayPredecessors,
Dave Allisonbcec6fb2014-01-17 12:52:22 -080038 kGrowableArraySlowPaths,
buzbee862a7602013-04-05 10:58:54 -070039 kGNumListKinds
40};
41
42template<typename T>
43class GrowableArray {
44 public:
buzbee862a7602013-04-05 10:58:54 -070045 class Iterator {
46 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070047 explicit Iterator(GrowableArray* g_list)
buzbee862a7602013-04-05 10:58:54 -070048 : idx_(0),
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070049 g_list_(g_list) {}
buzbee862a7602013-04-05 10:58:54 -070050
Jean Christophe Beyler1ceea7e2014-04-15 16:18:48 -070051 explicit Iterator()
52 : idx_(0),
53 g_list_(nullptr) {}
54
buzbee862a7602013-04-05 10:58:54 -070055 // NOTE: returns 0/NULL when no next.
56 // TODO: redo to make usage consistent with other iterators.
57 T Next() {
Jean Christophe Beyler1ceea7e2014-04-15 16:18:48 -070058 DCHECK(g_list_ != nullptr);
buzbee862a7602013-04-05 10:58:54 -070059 if (idx_ >= g_list_->Size()) {
60 return 0;
61 } else {
62 return g_list_->Get(idx_++);
63 }
64 }
65
66 void Reset() {
67 idx_ = 0;
68 }
69
Jean Christophe Beyler1ceea7e2014-04-15 16:18:48 -070070 void Reset(GrowableArray* g_list) {
71 idx_ = 0;
72 g_list_ = g_list;
73 }
74
75 size_t GetIndex() const {
76 return idx_;
77 }
78
buzbee862a7602013-04-05 10:58:54 -070079 private:
80 size_t idx_;
81 GrowableArray* const g_list_;
82 };
83
84 GrowableArray(ArenaAllocator* arena, size_t init_length, OatListKind kind = kGrowableArrayMisc)
85 : arena_(arena),
buzbeea5abf702013-04-12 14:39:29 -070086 num_allocated_(init_length),
buzbee862a7602013-04-05 10:58:54 -070087 num_used_(0),
88 kind_(kind) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070089 elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000090 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070091 };
92
93
94 // Expand the list size to at least new length.
95 void Resize(size_t new_length) {
96 if (new_length <= num_allocated_) return;
buzbeea5abf702013-04-12 14:39:29 -070097 // If it's a small list double the size, else grow 1.5x.
98 size_t target_length =
99 (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1);
buzbee862a7602013-04-05 10:58:54 -0700100 if (new_length > target_length) {
101 target_length = new_length;
102 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700103 T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000104 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -0700105 memcpy(new_array, elem_list_, sizeof(T) * num_allocated_);
buzbee862a7602013-04-05 10:58:54 -0700106 num_allocated_ = target_length;
107 elem_list_ = new_array;
108 };
109
110 // NOTE: does not return storage, just resets use count.
111 void Reset() {
112 num_used_ = 0;
113 }
114
115 // Insert an element to the end of a list, resizing if necessary.
116 void Insert(T elem) {
117 if (num_used_ == num_allocated_) {
118 Resize(num_used_ + 1);
119 }
120 elem_list_[num_used_++] = elem;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000121 }
122
123 void InsertAt(size_t index, T elem) {
124 DCHECK(index <= Size());
125 Insert(elem);
126 for (size_t i = Size() - 1; i > index; --i) {
127 elem_list_[i] = elem_list_[i - 1];
128 }
129 elem_list_[index] = elem;
130 }
131
132 void Add(T elem) {
133 Insert(elem);
134 }
buzbee862a7602013-04-05 10:58:54 -0700135
136 T Get(size_t index) const {
137 DCHECK_LT(index, num_used_);
138 return elem_list_[index];
139 };
140
141 // Overwrite existing element at position index. List must be large enough.
142 void Put(size_t index, T elem) {
143 DCHECK_LT(index, num_used_);
144 elem_list_[index] = elem;
145 }
146
147 void Increment(size_t index) {
148 DCHECK_LT(index, num_used_);
149 elem_list_[index]++;
150 }
151
buzbeebd663de2013-09-10 15:41:31 -0700152 /*
153 * Remove an existing element from list. If there are more than one copy
154 * of the element, only the first one encountered will be deleted.
155 */
156 // TODO: consider renaming this.
buzbee862a7602013-04-05 10:58:54 -0700157 void Delete(T element) {
158 bool found = false;
159 for (size_t i = 0; i < num_used_ - 1; i++) {
160 if (!found && elem_list_[i] == element) {
161 found = true;
162 }
163 if (found) {
164 elem_list_[i] = elem_list_[i+1];
165 }
166 }
167 // We should either have found the element, or it was the last (unscanned) element.
168 DCHECK(found || (element == elem_list_[num_used_ - 1]));
169 num_used_--;
170 };
171
172 size_t GetNumAllocated() const { return num_allocated_; }
173
174 size_t Size() const { return num_used_; }
175
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176 bool IsEmpty() const { return num_used_ == 0; }
177
178 T Pop() {
179 DCHECK_GE(num_used_, (size_t)0);
180 return elem_list_[--num_used_];
181 }
182
183 T Peek() const {
184 DCHECK_GE(num_used_, (size_t)0);
185 return elem_list_[num_used_ - 1];
186 }
187
buzbeebd663de2013-09-10 15:41:31 -0700188 void SetSize(size_t new_size) {
189 Resize(new_size);
190 num_used_ = new_size;
191 }
192
buzbee862a7602013-04-05 10:58:54 -0700193 T* GetRawStorage() const { return elem_list_; }
194
195 static void* operator new(size_t size, ArenaAllocator* arena) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000196 return arena->Alloc(sizeof(GrowableArray<T>), kArenaAllocGrowableArray);
buzbee862a7602013-04-05 10:58:54 -0700197 };
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700198 static void operator delete(void* p) {} // Nop.
buzbee862a7602013-04-05 10:58:54 -0700199
200 private:
201 ArenaAllocator* const arena_;
202 size_t num_allocated_;
203 size_t num_used_;
204 OatListKind kind_;
205 T* elem_list_;
206};
207
208} // namespace art
209
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210#endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_