| 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 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 * There is no guarantee that the backing store is contiguous (and, as a | 496 * There is no guarantee that the backing store is contiguous (and, as a |
| 497 * consequence, no guarantees that consecutively added elements are adjacent | 497 * consequence, no guarantees that consecutively added elements are adjacent |
| 498 * in memory). The collector may move elements unless it has guaranteed not | 498 * in memory). The collector may move elements unless it has guaranteed not |
| 499 * to. | 499 * to. |
| 500 */ | 500 */ |
| 501 template <typename T, int growth_factor = 2, int max_growth = 1 * MB> | 501 template <typename T, int growth_factor = 2, int max_growth = 1 * MB> |
| 502 class Collector { | 502 class Collector { |
| 503 public: | 503 public: |
| 504 explicit Collector(int initial_capacity = kMinCapacity) | 504 explicit Collector(int initial_capacity = kMinCapacity) |
| 505 : index_(0), size_(0) { | 505 : index_(0), size_(0) { |
| 506 if (initial_capacity < kMinCapacity) { | |
| 507 initial_capacity = kMinCapacity; | |
| 508 } | |
| 509 current_chunk_ = Vector<T>::New(initial_capacity); | 506 current_chunk_ = Vector<T>::New(initial_capacity); |
| 510 } | 507 } |
| 511 | 508 |
| 512 virtual ~Collector() { | 509 virtual ~Collector() { |
| 513 // Free backing store (in reverse allocation order). | 510 // Free backing store (in reverse allocation order). |
| 514 current_chunk_.Dispose(); | 511 current_chunk_.Dispose(); |
| 515 for (int i = chunks_.length() - 1; i >= 0; i--) { | 512 for (int i = chunks_.length() - 1; i >= 0; i--) { |
| 516 chunks_.at(i).Dispose(); | 513 chunks_.at(i).Dispose(); |
| 517 } | 514 } |
| 518 } | 515 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 protected: | 597 protected: |
| 601 static const int kMinCapacity = 16; | 598 static const int kMinCapacity = 16; |
| 602 List<Vector<T> > chunks_; | 599 List<Vector<T> > chunks_; |
| 603 Vector<T> current_chunk_; // Block of memory currently being written into. | 600 Vector<T> current_chunk_; // Block of memory currently being written into. |
| 604 int index_; // Current index in current chunk. | 601 int index_; // Current index in current chunk. |
| 605 int size_; // Total number of elements in collector. | 602 int size_; // Total number of elements in collector. |
| 606 | 603 |
| 607 // Creates a new current chunk, and stores the old chunk in the chunks_ list. | 604 // Creates a new current chunk, and stores the old chunk in the chunks_ list. |
| 608 void Grow(int min_capacity) { | 605 void Grow(int min_capacity) { |
| 609 ASSERT(growth_factor > 1); | 606 ASSERT(growth_factor > 1); |
| 610 int growth = current_chunk_.length() * (growth_factor - 1); | 607 int new_capacity; |
| 611 if (growth > max_growth) { | 608 int current_length = current_chunk_.length(); |
| 612 growth = max_growth; | 609 if (current_length < kMinCapacity) { |
| 613 } | 610 // The collector started out as empty. |
| 614 int new_capacity = current_chunk_.length() + growth; | 611 new_capacity = min_capacity * growth_factor; |
| 615 if (new_capacity < min_capacity) { | 612 if (new_capacity < kMinCapacity) new_capacity = kMinCapacity; |
| 616 new_capacity = min_capacity + growth; | 613 } else { |
| 614 int growth = current_length * (growth_factor - 1); |
| 615 if (growth > max_growth) { |
| 616 growth = max_growth; |
| 617 } |
| 618 new_capacity = current_length + growth; |
| 619 if (new_capacity < min_capacity) { |
| 620 new_capacity = min_capacity + growth; |
| 621 } |
| 617 } | 622 } |
| 618 Vector<T> new_chunk = Vector<T>::New(new_capacity); | 623 Vector<T> new_chunk = Vector<T>::New(new_capacity); |
| 619 int new_index = PrepareGrow(new_chunk); | 624 int new_index = PrepareGrow(new_chunk); |
| 620 if (index_ > 0) { | 625 if (index_ > 0) { |
| 621 chunks_.Add(current_chunk_.SubVector(0, index_)); | 626 chunks_.Add(current_chunk_.SubVector(0, index_)); |
| 622 } else { | 627 } else { |
| 623 // Can happen if the call to PrepareGrow moves everything into | 628 // Can happen if the call to PrepareGrow moves everything into |
| 624 // the new chunk. | 629 // the new chunk. |
| 625 current_chunk_.Dispose(); | 630 current_chunk_.Dispose(); |
| 626 } | 631 } |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); | 917 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); |
| 913 return 1 << element; | 918 return 1 << element; |
| 914 } | 919 } |
| 915 | 920 |
| 916 T bits_; | 921 T bits_; |
| 917 }; | 922 }; |
| 918 | 923 |
| 919 } } // namespace v8::internal | 924 } } // namespace v8::internal |
| 920 | 925 |
| 921 #endif // V8_UTILS_H_ | 926 #endif // V8_UTILS_H_ |
| OLD | NEW |