5 #ifndef V8_COLLECTOR_H_ 6 #define V8_COLLECTOR_H_ 10 #include "src/checks.h" 11 #include "src/vector.h" 25 template <
typename T,
int growth_factor = 2,
int max_growth = 1 * MB>
28 explicit Collector(
int initial_capacity = kMinCapacity)
29 : index_(0), size_(0) {
35 current_chunk_.Dispose();
36 for (
auto rit = chunks_.rbegin(); rit != chunks_.rend(); ++rit) {
42 inline void Add(
T value) {
43 if (index_ >= current_chunk_.length()) {
46 current_chunk_[index_] = value;
55 inline Vector<T> AddBlock(
int size,
T initial_value) {
57 if (size > current_chunk_.length() - index_) {
60 T* position = current_chunk_.start() + index_;
63 for (
int i = 0;
i < size;
i++) {
64 position[
i] = initial_value;
74 if (source.length() > current_chunk_.length() - index_) {
75 Grow(source.length());
77 T* position = current_chunk_.start() + index_;
78 index_ += source.length();
79 size_ += source.length();
80 for (
int i = 0;
i < source.length();
i++) {
81 position[
i] = source[
i];
83 return Vector<T>(position, source.length());
88 DCHECK(size_ <= destination.length());
91 for (
int j = 0; j < chunk.length(); j++) {
92 destination[position] = chunk[j];
96 for (
int i = 0;
i < index_;
i++) {
97 destination[position] = current_chunk_[
i];
113 virtual void Reset() {
114 for (
auto rit = chunks_.rbegin(); rit != chunks_.rend(); ++rit) {
123 inline int size() {
return size_; }
126 static const int kMinCapacity = 16;
127 std::vector<Vector<T>> chunks_;
133 void Grow(
int min_capacity) {
134 DCHECK_GT(growth_factor, 1);
136 int current_length = current_chunk_.length();
137 if (current_length < kMinCapacity) {
139 new_capacity = min_capacity * growth_factor;
140 if (new_capacity < kMinCapacity) new_capacity = kMinCapacity;
142 int growth = current_length * (growth_factor - 1);
143 if (growth > max_growth) {
146 new_capacity = current_length + growth;
147 if (new_capacity < min_capacity) {
148 new_capacity = min_capacity + growth;
151 NewChunk(new_capacity);
152 DCHECK(index_ + min_capacity <= current_chunk_.length());
159 virtual void NewChunk(
int new_capacity) {
162 chunks_.push_back(current_chunk_.SubVector(0, index_));
164 current_chunk_.Dispose();
166 current_chunk_ = new_chunk;
180 template <
typename T,
int growth_factor = 2,
int max_growth = 1 * MB>
185 sequence_start_(kNoSequence) {}
189 void StartSequence() {
190 DCHECK_EQ(sequence_start_, kNoSequence);
191 sequence_start_ = this->index_;
195 DCHECK_NE(sequence_start_, kNoSequence);
196 int sequence_start = sequence_start_;
197 sequence_start_ = kNoSequence;
198 if (sequence_start == this->index_)
return Vector<T>();
199 return this->current_chunk_.SubVector(sequence_start, this->index_);
203 void DropSequence() {
204 DCHECK_NE(sequence_start_, kNoSequence);
205 int sequence_length = this->index_ - sequence_start_;
206 this->index_ = sequence_start_;
207 this->size_ -= sequence_length;
208 sequence_start_ = kNoSequence;
211 void Reset()
override {
212 sequence_start_ = kNoSequence;
217 static const int kNoSequence = -1;
221 void NewChunk(
int new_capacity)
override {
222 if (sequence_start_ == kNoSequence) {
227 int sequence_length = this->index_ - sequence_start_;
229 DCHECK(sequence_length < new_chunk.length());
230 for (
int i = 0;
i < sequence_length;
i++) {
231 new_chunk[
i] = this->current_chunk_[sequence_start_ +
i];
233 if (sequence_start_ > 0) {
234 this->chunks_.push_back(
235 this->current_chunk_.SubVector(0, sequence_start_));
237 this->current_chunk_.Dispose();
239 this->current_chunk_ = new_chunk;
240 this->index_ = sequence_length;
248 #endif // V8_COLLECTOR_H_