blob: c4684a71f665d70f3ae34741192fa12ec5d9e895 [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
17#ifndef ART_SRC_COMPILER_DEX_GROWABLE_LIST_H_
18#define ART_SRC_COMPILER_DEX_GROWABLE_LIST_H_
19
20#include <stdint.h>
21#include <stddef.h>
22#include "compiler_enums.h"
23#include "arena_allocator.h"
24
25namespace art {
26
27struct CompilationUnit;
28
29// Type of growable list for memory tuning.
30enum OatListKind {
31 kGrowableArrayMisc = 0,
32 kGrowableArrayBlockList,
33 kGrowableArraySSAtoDalvikMap,
34 kGrowableArrayDfsOrder,
35 kGrowableArrayDfsPostOrder,
36 kGrowableArrayDomPostOrderTraversal,
37 kGrowableArrayThrowLaunchPads,
38 kGrowableArraySuspendLaunchPads,
39 kGrowableArraySwitchTables,
40 kGrowableArrayFillArrayData,
41 kGrowableArraySuccessorBlocks,
42 kGrowableArrayPredecessors,
43 kGNumListKinds
44};
45
46template<typename T>
47class GrowableArray {
48 public:
49
50 class Iterator {
51 public:
52 Iterator(GrowableArray* g_list)
53 : idx_(0),
54 g_list_(g_list) {};
55
56 // NOTE: returns 0/NULL when no next.
57 // TODO: redo to make usage consistent with other iterators.
58 T Next() {
59 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
70 static void* operator new(size_t size, ArenaAllocator* arena) {
71 return arena->NewMem(sizeof(GrowableArray::Iterator), true, ArenaAllocator::kAllocGrowableArray);
72 };
73 static void operator delete(void* p) {}; // Nop.
74
75 private:
76 size_t idx_;
77 GrowableArray* const g_list_;
78 };
79
80 GrowableArray(ArenaAllocator* arena, size_t init_length, OatListKind kind = kGrowableArrayMisc)
81 : arena_(arena),
buzbeea5abf702013-04-12 14:39:29 -070082 num_allocated_(init_length),
buzbee862a7602013-04-05 10:58:54 -070083 num_used_(0),
84 kind_(kind) {
85 elem_list_ = static_cast<T*>(arena_->NewMem(sizeof(T) * init_length, true,
buzbeea5abf702013-04-12 14:39:29 -070086 ArenaAllocator::kAllocGrowableArray));
buzbee862a7602013-04-05 10:58:54 -070087 };
88
89
90 // Expand the list size to at least new length.
91 void Resize(size_t new_length) {
92 if (new_length <= num_allocated_) return;
buzbeea5abf702013-04-12 14:39:29 -070093 // If it's a small list double the size, else grow 1.5x.
94 size_t target_length =
95 (num_allocated_ < 128) ? num_allocated_ << 1 : num_allocated_ + (num_allocated_ >> 1);
buzbee862a7602013-04-05 10:58:54 -070096 if (new_length > target_length) {
97 target_length = new_length;
98 }
99 T* new_array = static_cast<T*>(arena_->NewMem(sizeof(T) * target_length, true,
100 ArenaAllocator::kAllocGrowableArray));
101 memcpy(new_array, elem_list_, sizeof(T) * num_allocated_);
buzbee862a7602013-04-05 10:58:54 -0700102 num_allocated_ = target_length;
103 elem_list_ = new_array;
104 };
105
106 // NOTE: does not return storage, just resets use count.
107 void Reset() {
108 num_used_ = 0;
109 }
110
111 // Insert an element to the end of a list, resizing if necessary.
112 void Insert(T elem) {
113 if (num_used_ == num_allocated_) {
114 Resize(num_used_ + 1);
115 }
116 elem_list_[num_used_++] = elem;
117 };
118
119 T Get(size_t index) const {
120 DCHECK_LT(index, num_used_);
121 return elem_list_[index];
122 };
123
124 // Overwrite existing element at position index. List must be large enough.
125 void Put(size_t index, T elem) {
126 DCHECK_LT(index, num_used_);
127 elem_list_[index] = elem;
128 }
129
130 void Increment(size_t index) {
131 DCHECK_LT(index, num_used_);
132 elem_list_[index]++;
133 }
134
135 void Delete(T element) {
136 bool found = false;
137 for (size_t i = 0; i < num_used_ - 1; i++) {
138 if (!found && elem_list_[i] == element) {
139 found = true;
140 }
141 if (found) {
142 elem_list_[i] = elem_list_[i+1];
143 }
144 }
145 // We should either have found the element, or it was the last (unscanned) element.
146 DCHECK(found || (element == elem_list_[num_used_ - 1]));
147 num_used_--;
148 };
149
150 size_t GetNumAllocated() const { return num_allocated_; }
151
152 size_t Size() const { return num_used_; }
153
154 T* GetRawStorage() const { return elem_list_; }
155
156 static void* operator new(size_t size, ArenaAllocator* arena) {
157 return arena->NewMem(sizeof(GrowableArray<T>), true, ArenaAllocator::kAllocGrowableArray);
158 };
159 static void operator delete(void* p) {}; // Nop.
160
161 private:
162 ArenaAllocator* const arena_;
163 size_t num_allocated_;
164 size_t num_used_;
165 OatListKind kind_;
166 T* elem_list_;
167};
168
169} // namespace art
170
171#endif // ART_SRC_COMPILER_DEX_GROWABLE_LIST_H_