blob: b33a62e67f694b217dfda14a4369b1a9843439fa [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 kGrowableArraySuspendLaunchPads,
35 kGrowableArraySwitchTables,
36 kGrowableArrayFillArrayData,
37 kGrowableArraySuccessorBlocks,
38 kGrowableArrayPredecessors,
Dave Allisonbcec6fb2014-01-17 12:52:22 -080039 kGrowableArraySlowPaths,
buzbee862a7602013-04-05 10:58:54 -070040 kGNumListKinds
41};
42
43template<typename T>
44class GrowableArray {
45 public:
buzbee862a7602013-04-05 10:58:54 -070046 class Iterator {
47 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070048 explicit Iterator(GrowableArray* g_list)
buzbee862a7602013-04-05 10:58:54 -070049 : idx_(0),
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070050 g_list_(g_list) {}
buzbee862a7602013-04-05 10:58:54 -070051
52 // NOTE: returns 0/NULL when no next.
53 // TODO: redo to make usage consistent with other iterators.
54 T Next() {
55 if (idx_ >= g_list_->Size()) {
56 return 0;
57 } else {
58 return g_list_->Get(idx_++);
59 }
60 }
61
62 void Reset() {
63 idx_ = 0;
64 }
65
buzbee862a7602013-04-05 10:58:54 -070066 private:
67 size_t idx_;
68 GrowableArray* const g_list_;
69 };
70
71 GrowableArray(ArenaAllocator* arena, size_t init_length, OatListKind kind = kGrowableArrayMisc)
72 : arena_(arena),
buzbeea5abf702013-04-12 14:39:29 -070073 num_allocated_(init_length),
buzbee862a7602013-04-05 10:58:54 -070074 num_used_(0),
75 kind_(kind) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070076 elem_list_ = static_cast<T*>(arena_->Alloc(sizeof(T) * init_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000077 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070078 };
79
80
81 // Expand the list size to at least new length.
82 void Resize(size_t new_length) {
83 if (new_length <= num_allocated_) return;
buzbeea5abf702013-04-12 14:39:29 -070084 // If it's a small list double the size, else grow 1.5x.
85 size_t target_length =
86 (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1);
buzbee862a7602013-04-05 10:58:54 -070087 if (new_length > target_length) {
88 target_length = new_length;
89 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070090 T* new_array = static_cast<T*>(arena_->Alloc(sizeof(T) * target_length,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000091 kArenaAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070092 memcpy(new_array, elem_list_, sizeof(T) * num_allocated_);
buzbee862a7602013-04-05 10:58:54 -070093 num_allocated_ = target_length;
94 elem_list_ = new_array;
95 };
96
97 // NOTE: does not return storage, just resets use count.
98 void Reset() {
99 num_used_ = 0;
100 }
101
102 // Insert an element to the end of a list, resizing if necessary.
103 void Insert(T elem) {
104 if (num_used_ == num_allocated_) {
105 Resize(num_used_ + 1);
106 }
107 elem_list_[num_used_++] = elem;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000108 }
109
110 void InsertAt(size_t index, T elem) {
111 DCHECK(index <= Size());
112 Insert(elem);
113 for (size_t i = Size() - 1; i > index; --i) {
114 elem_list_[i] = elem_list_[i - 1];
115 }
116 elem_list_[index] = elem;
117 }
118
119 void Add(T elem) {
120 Insert(elem);
121 }
buzbee862a7602013-04-05 10:58:54 -0700122
123 T Get(size_t index) const {
124 DCHECK_LT(index, num_used_);
125 return elem_list_[index];
126 };
127
128 // Overwrite existing element at position index. List must be large enough.
129 void Put(size_t index, T elem) {
130 DCHECK_LT(index, num_used_);
131 elem_list_[index] = elem;
132 }
133
134 void Increment(size_t index) {
135 DCHECK_LT(index, num_used_);
136 elem_list_[index]++;
137 }
138
buzbeebd663de2013-09-10 15:41:31 -0700139 /*
140 * Remove an existing element from list. If there are more than one copy
141 * of the element, only the first one encountered will be deleted.
142 */
143 // TODO: consider renaming this.
buzbee862a7602013-04-05 10:58:54 -0700144 void Delete(T element) {
145 bool found = false;
146 for (size_t i = 0; i < num_used_ - 1; i++) {
147 if (!found && elem_list_[i] == element) {
148 found = true;
149 }
150 if (found) {
151 elem_list_[i] = elem_list_[i+1];
152 }
153 }
154 // We should either have found the element, or it was the last (unscanned) element.
155 DCHECK(found || (element == elem_list_[num_used_ - 1]));
156 num_used_--;
157 };
158
159 size_t GetNumAllocated() const { return num_allocated_; }
160
161 size_t Size() const { return num_used_; }
162
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 bool IsEmpty() const { return num_used_ == 0; }
164
165 T Pop() {
166 DCHECK_GE(num_used_, (size_t)0);
167 return elem_list_[--num_used_];
168 }
169
170 T Peek() const {
171 DCHECK_GE(num_used_, (size_t)0);
172 return elem_list_[num_used_ - 1];
173 }
174
buzbeebd663de2013-09-10 15:41:31 -0700175 void SetSize(size_t new_size) {
176 Resize(new_size);
177 num_used_ = new_size;
178 }
179
buzbee862a7602013-04-05 10:58:54 -0700180 T* GetRawStorage() const { return elem_list_; }
181
182 static void* operator new(size_t size, ArenaAllocator* arena) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000183 return arena->Alloc(sizeof(GrowableArray<T>), kArenaAllocGrowableArray);
buzbee862a7602013-04-05 10:58:54 -0700184 };
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700185 static void operator delete(void* p) {} // Nop.
buzbee862a7602013-04-05 10:58:54 -0700186
187 private:
188 ArenaAllocator* const arena_;
189 size_t num_allocated_;
190 size_t num_used_;
191 OatListKind kind_;
192 T* elem_list_;
193};
194
195} // namespace art
196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197#endif // ART_COMPILER_UTILS_GROWABLE_ARRAY_H_