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 } |
499 current_chunk_ = Vector<T>::New(initial_capacity); | 502 current_chunk_ = Vector<T>::New(initial_capacity); |
500 } | 503 } |
501 | 504 |
502 virtual ~Collector() { | 505 virtual ~Collector() { |
503 // Free backing store (in reverse allocation order). | 506 // Free backing store (in reverse allocation order). |
504 current_chunk_.Dispose(); | 507 current_chunk_.Dispose(); |
505 for (int i = chunks_.length() - 1; i >= 0; i--) { | 508 for (int i = chunks_.length() - 1; i >= 0; i--) { |
506 chunks_.at(i).Dispose(); | 509 chunks_.at(i).Dispose(); |
507 } | 510 } |
508 } | 511 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 protected: | 593 protected: |
591 static const int kMinCapacity = 16; | 594 static const int kMinCapacity = 16; |
592 List<Vector<T> > chunks_; | 595 List<Vector<T> > chunks_; |
593 Vector<T> current_chunk_; // Block of memory currently being written into. | 596 Vector<T> current_chunk_; // Block of memory currently being written into. |
594 int index_; // Current index in current chunk. | 597 int index_; // Current index in current chunk. |
595 int size_; // Total number of elements in collector. | 598 int size_; // Total number of elements in collector. |
596 | 599 |
597 // Creates a new current chunk, and stores the old chunk in the chunks_ list. | 600 // Creates a new current chunk, and stores the old chunk in the chunks_ list. |
598 void Grow(int min_capacity) { | 601 void Grow(int min_capacity) { |
599 ASSERT(growth_factor > 1); | 602 ASSERT(growth_factor > 1); |
600 int new_capacity; | 603 int growth = current_chunk_.length() * (growth_factor - 1); |
601 int current_length = current_chunk_.length(); | 604 if (growth > max_growth) { |
602 if (current_length < kMinCapacity) { | 605 growth = max_growth; |
603 // The collector started out as empty. | 606 } |
604 new_capacity = min_capacity * growth_factor; | 607 int new_capacity = current_chunk_.length() + growth; |
605 if (new_capacity < kMinCapacity) new_capacity = kMinCapacity; | 608 if (new_capacity < min_capacity) { |
606 } else { | 609 new_capacity = min_capacity + growth; |
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 } | |
615 } | 610 } |
616 Vector<T> new_chunk = Vector<T>::New(new_capacity); | 611 Vector<T> new_chunk = Vector<T>::New(new_capacity); |
617 int new_index = PrepareGrow(new_chunk); | 612 int new_index = PrepareGrow(new_chunk); |
618 if (index_ > 0) { | 613 if (index_ > 0) { |
619 chunks_.Add(current_chunk_.SubVector(0, index_)); | 614 chunks_.Add(current_chunk_.SubVector(0, index_)); |
620 } else { | 615 } else { |
621 // Can happen if the call to PrepareGrow moves everything into | 616 // Can happen if the call to PrepareGrow moves everything into |
622 // the new chunk. | 617 // the new chunk. |
623 current_chunk_.Dispose(); | 618 current_chunk_.Dispose(); |
624 } | 619 } |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 int position_; | 881 int position_; |
887 | 882 |
888 bool is_finalized() const { return position_ < 0; } | 883 bool is_finalized() const { return position_ < 0; } |
889 private: | 884 private: |
890 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder); | 885 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder); |
891 }; | 886 }; |
892 | 887 |
893 } } // namespace v8::internal | 888 } } // namespace v8::internal |
894 | 889 |
895 #endif // V8_UTILS_H_ | 890 #endif // V8_UTILS_H_ |
OLD | NEW |