OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef V8_COLLECTOR_H_ | |
6 #define V8_COLLECTOR_H_ | |
7 | |
8 #include "src/list.h" | |
vogelheim
2016/03/01 12:50:03
Also src/vector.h?
Also src/checks.h? (for DCHECK
| |
9 | |
10 namespace v8 { | |
11 namespace internal { | |
12 | |
13 /* | |
14 * A class that collects values into a backing store. | |
15 * Specialized versions of the class can allow access to the backing store | |
16 * in different ways. | |
17 * There is no guarantee that the backing store is contiguous (and, as a | |
18 * consequence, no guarantees that consecutively added elements are adjacent | |
19 * in memory). The collector may move elements unless it has guaranteed not | |
20 * to. | |
21 */ | |
22 template <typename T, int growth_factor = 2, int max_growth = 1 * MB> | |
23 class Collector { | |
24 public: | |
25 explicit Collector(int initial_capacity = kMinCapacity) | |
26 : index_(0), size_(0) { | |
27 current_chunk_ = Vector<T>::New(initial_capacity); | |
28 } | |
29 | |
30 virtual ~Collector() { | |
31 // Free backing store (in reverse allocation order). | |
32 current_chunk_.Dispose(); | |
33 for (int i = chunks_.length() - 1; i >= 0; i--) { | |
34 chunks_.at(i).Dispose(); | |
35 } | |
36 } | |
37 | |
38 // Add a single element. | |
39 inline void Add(T value) { | |
40 if (index_ >= current_chunk_.length()) { | |
41 Grow(1); | |
42 } | |
43 current_chunk_[index_] = value; | |
44 index_++; | |
45 size_++; | |
46 } | |
47 | |
48 // Add a block of contiguous elements and return a Vector backed by the | |
49 // memory area. | |
50 // A basic Collector will keep this vector valid as long as the Collector | |
51 // is alive. | |
52 inline Vector<T> AddBlock(int size, T initial_value) { | |
53 DCHECK(size > 0); | |
54 if (size > current_chunk_.length() - index_) { | |
55 Grow(size); | |
56 } | |
57 T* position = current_chunk_.start() + index_; | |
58 index_ += size; | |
59 size_ += size; | |
60 for (int i = 0; i < size; i++) { | |
61 position[i] = initial_value; | |
62 } | |
63 return Vector<T>(position, size); | |
64 } | |
65 | |
66 // Add a contiguous block of elements and return a vector backed | |
67 // by the added block. | |
68 // A basic Collector will keep this vector valid as long as the Collector | |
69 // is alive. | |
70 inline Vector<T> AddBlock(Vector<const T> source) { | |
71 if (source.length() > current_chunk_.length() - index_) { | |
72 Grow(source.length()); | |
73 } | |
74 T* position = current_chunk_.start() + index_; | |
75 index_ += source.length(); | |
76 size_ += source.length(); | |
77 for (int i = 0; i < source.length(); i++) { | |
78 position[i] = source[i]; | |
79 } | |
80 return Vector<T>(position, source.length()); | |
81 } | |
82 | |
83 // Write the contents of the collector into the provided vector. | |
84 void WriteTo(Vector<T> destination) { | |
85 DCHECK(size_ <= destination.length()); | |
86 int position = 0; | |
87 for (int i = 0; i < chunks_.length(); i++) { | |
88 Vector<T> chunk = chunks_.at(i); | |
89 for (int j = 0; j < chunk.length(); j++) { | |
90 destination[position] = chunk[j]; | |
91 position++; | |
92 } | |
93 } | |
94 for (int i = 0; i < index_; i++) { | |
95 destination[position] = current_chunk_[i]; | |
96 position++; | |
97 } | |
98 } | |
99 | |
100 // Allocate a single contiguous vector, copy all the collected | |
101 // elements to the vector, and return it. | |
102 // The caller is responsible for freeing the memory of the returned | |
103 // vector (e.g., using Vector::Dispose). | |
104 Vector<T> ToVector() { | |
105 Vector<T> new_store = Vector<T>::New(size_); | |
106 WriteTo(new_store); | |
107 return new_store; | |
108 } | |
109 | |
110 // Resets the collector to be empty. | |
111 virtual void Reset() { | |
112 for (int i = chunks_.length() - 1; i >= 0; i--) { | |
113 chunks_.at(i).Dispose(); | |
114 } | |
115 chunks_.Rewind(0); | |
116 index_ = 0; | |
117 size_ = 0; | |
118 } | |
119 | |
120 // Total number of elements added to collector so far. | |
121 inline int size() { return size_; } | |
122 | |
123 protected: | |
124 static const int kMinCapacity = 16; | |
125 List<Vector<T> > chunks_; | |
126 Vector<T> current_chunk_; // Block of memory currently being written into. | |
127 int index_; // Current index in current chunk. | |
128 int size_; // Total number of elements in collector. | |
129 | |
130 // Creates a new current chunk, and stores the old chunk in the chunks_ list. | |
131 void Grow(int min_capacity) { | |
132 DCHECK(growth_factor > 1); | |
133 int new_capacity; | |
134 int current_length = current_chunk_.length(); | |
135 if (current_length < kMinCapacity) { | |
136 // The collector started out as empty. | |
137 new_capacity = min_capacity * growth_factor; | |
138 if (new_capacity < kMinCapacity) new_capacity = kMinCapacity; | |
139 } else { | |
140 int growth = current_length * (growth_factor - 1); | |
141 if (growth > max_growth) { | |
142 growth = max_growth; | |
143 } | |
144 new_capacity = current_length + growth; | |
145 if (new_capacity < min_capacity) { | |
146 new_capacity = min_capacity + growth; | |
147 } | |
148 } | |
149 NewChunk(new_capacity); | |
150 DCHECK(index_ + min_capacity <= current_chunk_.length()); | |
151 } | |
152 | |
153 // Before replacing the current chunk, give a subclass the option to move | |
154 // some of the current data into the new chunk. The function may update | |
155 // the current index_ value to represent data no longer in the current chunk. | |
156 // Returns the initial index of the new chunk (after copied data). | |
157 virtual void NewChunk(int new_capacity) { | |
158 Vector<T> new_chunk = Vector<T>::New(new_capacity); | |
159 if (index_ > 0) { | |
160 chunks_.Add(current_chunk_.SubVector(0, index_)); | |
161 } else { | |
162 current_chunk_.Dispose(); | |
163 } | |
164 current_chunk_ = new_chunk; | |
165 index_ = 0; | |
166 } | |
167 }; | |
168 | |
169 /* | |
170 * A collector that allows sequences of values to be guaranteed to | |
171 * stay consecutive. | |
172 * If the backing store grows while a sequence is active, the current | |
173 * sequence might be moved, but after the sequence is ended, it will | |
174 * not move again. | |
175 * NOTICE: Blocks allocated using Collector::AddBlock(int) can move | |
176 * as well, if inside an active sequence where another element is added. | |
177 */ | |
178 template <typename T, int growth_factor = 2, int max_growth = 1 * MB> | |
179 class SequenceCollector : public Collector<T, growth_factor, max_growth> { | |
180 public: | |
181 explicit SequenceCollector(int initial_capacity) | |
182 : Collector<T, growth_factor, max_growth>(initial_capacity), | |
183 sequence_start_(kNoSequence) {} | |
184 | |
185 virtual ~SequenceCollector() {} | |
186 | |
187 void StartSequence() { | |
188 DCHECK(sequence_start_ == kNoSequence); | |
189 sequence_start_ = this->index_; | |
190 } | |
191 | |
192 Vector<T> EndSequence() { | |
193 DCHECK(sequence_start_ != kNoSequence); | |
194 int sequence_start = sequence_start_; | |
195 sequence_start_ = kNoSequence; | |
196 if (sequence_start == this->index_) return Vector<T>(); | |
197 return this->current_chunk_.SubVector(sequence_start, this->index_); | |
198 } | |
199 | |
200 // Drops the currently added sequence, and all collected elements in it. | |
201 void DropSequence() { | |
202 DCHECK(sequence_start_ != kNoSequence); | |
203 int sequence_length = this->index_ - sequence_start_; | |
204 this->index_ = sequence_start_; | |
205 this->size_ -= sequence_length; | |
206 sequence_start_ = kNoSequence; | |
207 } | |
208 | |
209 virtual void Reset() { | |
210 sequence_start_ = kNoSequence; | |
211 this->Collector<T, growth_factor, max_growth>::Reset(); | |
212 } | |
213 | |
214 private: | |
215 static const int kNoSequence = -1; | |
216 int sequence_start_; | |
217 | |
218 // Move the currently active sequence to the new chunk. | |
219 virtual void NewChunk(int new_capacity) { | |
220 if (sequence_start_ == kNoSequence) { | |
221 // Fall back on default behavior if no sequence has been started. | |
222 this->Collector<T, growth_factor, max_growth>::NewChunk(new_capacity); | |
223 return; | |
224 } | |
225 int sequence_length = this->index_ - sequence_start_; | |
226 Vector<T> new_chunk = Vector<T>::New(sequence_length + new_capacity); | |
227 DCHECK(sequence_length < new_chunk.length()); | |
228 for (int i = 0; i < sequence_length; i++) { | |
229 new_chunk[i] = this->current_chunk_[sequence_start_ + i]; | |
230 } | |
231 if (sequence_start_ > 0) { | |
232 this->chunks_.Add(this->current_chunk_.SubVector(0, sequence_start_)); | |
233 } else { | |
234 this->current_chunk_.Dispose(); | |
235 } | |
236 this->current_chunk_ = new_chunk; | |
237 this->index_ = sequence_length; | |
238 sequence_start_ = 0; | |
239 } | |
240 }; | |
241 | |
242 } // namespace internal | |
243 } // namespace v8 | |
244 | |
245 #endif // V8_COLLECTOR_H_ | |
OLD | NEW |