OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 * There is no guarantee that the backing store is contiguous (and, as a | 489 * There is no guarantee that the backing store is contiguous (and, as a |
490 * consequence, no guarantees that consecutively added elements are adjacent | 490 * consequence, no guarantees that consecutively added elements are adjacent |
491 * in memory). The collector may move elements unless it has guaranteed not | 491 * in memory). The collector may move elements unless it has guaranteed not |
492 * to. | 492 * to. |
493 */ | 493 */ |
494 template <typename T, int growth_factor = 2, int max_growth = 1 * MB> | 494 template <typename T, int growth_factor = 2, int max_growth = 1 * MB> |
495 class Collector { | 495 class Collector { |
496 public: | 496 public: |
497 explicit Collector(int initial_capacity = kMinCapacity) | 497 explicit Collector(int initial_capacity = kMinCapacity) |
498 : index_(0), size_(0) { | 498 : index_(0), size_(0) { |
499 if (initial_capacity < kMinCapacity) { | |
500 initial_capacity = kMinCapacity; | |
501 } | |
502 current_chunk_ = Vector<T>::New(initial_capacity); | 499 current_chunk_ = Vector<T>::New(initial_capacity); |
503 } | 500 } |
504 | 501 |
505 virtual ~Collector() { | 502 virtual ~Collector() { |
506 // Free backing store (in reverse allocation order). | 503 // Free backing store (in reverse allocation order). |
507 current_chunk_.Dispose(); | 504 current_chunk_.Dispose(); |
508 for (int i = chunks_.length() - 1; i >= 0; i--) { | 505 for (int i = chunks_.length() - 1; i >= 0; i--) { |
509 chunks_.at(i).Dispose(); | 506 chunks_.at(i).Dispose(); |
510 } | 507 } |
511 } | 508 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 protected: | 590 protected: |
594 static const int kMinCapacity = 16; | 591 static const int kMinCapacity = 16; |
595 List<Vector<T> > chunks_; | 592 List<Vector<T> > chunks_; |
596 Vector<T> current_chunk_; // Block of memory currently being written into. | 593 Vector<T> current_chunk_; // Block of memory currently being written into. |
597 int index_; // Current index in current chunk. | 594 int index_; // Current index in current chunk. |
598 int size_; // Total number of elements in collector. | 595 int size_; // Total number of elements in collector. |
599 | 596 |
600 // Creates a new current chunk, and stores the old chunk in the chunks_ list. | 597 // Creates a new current chunk, and stores the old chunk in the chunks_ list. |
601 void Grow(int min_capacity) { | 598 void Grow(int min_capacity) { |
602 ASSERT(growth_factor > 1); | 599 ASSERT(growth_factor > 1); |
603 int growth = current_chunk_.length() * (growth_factor - 1); | 600 int new_capacity; |
604 if (growth > max_growth) { | 601 int current_length = current_chunk_.length(); |
605 growth = max_growth; | 602 if (current_length < kMinCapacity) { |
606 } | 603 // The collector started out as empty. |
607 int new_capacity = current_chunk_.length() + growth; | 604 new_capacity = min_capacity * growth_factor; |
608 if (new_capacity < min_capacity) { | 605 if (new_capacity < kMinCapacity) new_capacity = kMinCapacity; |
609 new_capacity = min_capacity + growth; | 606 } else { |
| 607 int growth = current_length * (growth_factor - 1); |
| 608 if (growth > max_growth) { |
| 609 growth = max_growth; |
| 610 } |
| 611 new_capacity = current_length + growth; |
| 612 if (new_capacity < min_capacity) { |
| 613 new_capacity = min_capacity + growth; |
| 614 } |
610 } | 615 } |
611 Vector<T> new_chunk = Vector<T>::New(new_capacity); | 616 Vector<T> new_chunk = Vector<T>::New(new_capacity); |
612 int new_index = PrepareGrow(new_chunk); | 617 int new_index = PrepareGrow(new_chunk); |
613 if (index_ > 0) { | 618 if (index_ > 0) { |
614 chunks_.Add(current_chunk_.SubVector(0, index_)); | 619 chunks_.Add(current_chunk_.SubVector(0, index_)); |
615 } else { | 620 } else { |
616 // Can happen if the call to PrepareGrow moves everything into | 621 // Can happen if the call to PrepareGrow moves everything into |
617 // the new chunk. | 622 // the new chunk. |
618 current_chunk_.Dispose(); | 623 current_chunk_.Dispose(); |
619 } | 624 } |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 int position_; | 886 int position_; |
882 | 887 |
883 bool is_finalized() const { return position_ < 0; } | 888 bool is_finalized() const { return position_ < 0; } |
884 private: | 889 private: |
885 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder); | 890 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder); |
886 }; | 891 }; |
887 | 892 |
888 } } // namespace v8::internal | 893 } } // namespace v8::internal |
889 | 894 |
890 #endif // V8_UTILS_H_ | 895 #endif // V8_UTILS_H_ |
OLD | NEW |