Index: src/utils.h |
diff --git a/src/utils.h b/src/utils.h |
index 2885c5200e6983202ef742b3216c8d24443fb00e..8eddb1391d15ff1e6fd5db12d81f531eeeaac34a 100644 |
--- a/src/utils.h |
+++ b/src/utils.h |
@@ -476,6 +476,185 @@ inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, |
} |
+/* |
+ * A class that collects values into a backing store. |
+ * Specialized versions of the class can allow access to the backing store |
+ * in different ways. |
+ * There is no guarantee that the backing store is contiguous (and, as a |
+ * consequence, no guarantees that consecutively added elements are adjacent |
+ * in memory). The collector may move elements unless it has guaranteed not |
+ * to. |
+ */ |
+template <typename T> |
+class Collector { |
+ public: |
+ Collector(int initial_capacity = kMinCapacity, |
+ int growth_factor = 2, |
+ int max_growth = 1 * MB) |
+ : growth_factor_(growth_factor), max_growth_(max_growth) { |
+ if (initial_capacity < kMinCapacity) { |
+ initial_capacity = kMinCapacity; |
+ } |
+ current_chunk_ = NewArray<T>(initial_capacity); |
+ current_capacity_ = initial_capacity; |
+ index_ = 0; |
+ } |
+ |
+ virtual ~Collector() { |
+ // Free backing store (in reverse allocation order). |
+ DeleteArray(current_chunk_); |
+ for (int i = chunks_.length() - 1; i >= 0; i--) { |
+ chunks_.at(i).Dispose(); |
+ } |
+ } |
+ |
+ // Add a single element. |
+ inline void Add(T value) { |
+ if (index_ >= current_capacity_) { |
+ Grow(1); |
+ } |
+ current_chunk_[index_] = value; |
+ index_++; |
+ } |
+ |
+ // Add a block of contiguous elements and return a Vector backed by the |
+ // memory area. |
+ // A basic Collector will keep this vector valid as long as the Collector |
+ // is alive. |
+ inline Vector<T> AddBlock(int size, T initial_value) { |
+ if (index_ + size > current_capacity_) { |
+ Grow(size); |
+ } |
+ T* position = current_chunk_ + index_; |
+ index_ += size; |
+ for (int i = 0; i < size; i++) { |
+ position[i] = initial_value; |
+ } |
+ return Vector<T>(position, size); |
+ } |
+ |
+ |
+ // Allocate a single contiguous vector, copy all the collected |
+ // elements to the vector, and return it. |
+ // The caller is responsible for freeing the memory of the returned |
+ // vector (e.g., using Vector::Dispose). |
+ Vector<T> ToVector() { |
Mads Ager (chromium)
2010/08/24 09:24:51
You should move some of these methods to utils.cc
|
+ // Find the total length. |
+ int total_length = index_; |
+ for (int i = 0; i < chunks_.length(); i++) { |
+ total_length += chunks_.at(i).length(); |
+ } |
+ T* new_store = NewArray<T>(total_length); |
+ int position = 0; |
+ for (int i = 0; i < chunks_.length(); i++) { |
+ Vector<T> chunk = chunks_.at(i); |
+ for (int j = 0; j < chunk.length(); j++) { |
+ new_store[position] = chunk[j]; |
+ position++; |
+ } |
+ } |
+ for (int i = 0; i < index_; i++) { |
+ new_store[position] = current_chunk_[i]; |
+ position++; |
+ } |
+ return Vector<T>(new_store, total_length); |
+ } |
+ |
+ protected: |
+ static const int kMinCapacity = 16; |
+ List<Vector<T> > chunks_; |
+ T* current_chunk_; |
+ int growth_factor_; |
+ int max_growth_; |
+ int current_capacity_; |
+ int index_; |
+ |
+ // Creates a new current chunk, and stores the old chunk in the chunks_ list. |
+ void Grow(int min_capacity) { |
+ ASSERT(growth_factor_ > 1); |
+ int growth = current_capacity_ * (growth_factor_ - 1); |
+ if (growth > max_growth_) { |
+ growth = max_growth_; |
+ } |
+ int new_capacity = current_capacity_ + growth; |
+ if (new_capacity < min_capacity) { |
+ new_capacity = min_capacity; |
+ } |
+ T* new_chunk = NewArray<T>(new_capacity); |
+ int new_index = PrepareGrow(Vector<T>(new_chunk, new_capacity)); |
+ chunks_.Add(Vector<T>(current_chunk_, index_)); |
+ current_chunk_ = new_chunk; |
+ current_capacity_ = new_capacity; |
+ index_ = new_index; |
+ ASSERT(index_ + min_capacity <= current_capacity_); |
+ } |
+ |
+ // Before replacing the current chunk, give a subclass the option to move |
+ // some of the current data into the new chunk. The function may update |
+ // the current index_ value to represent data no longer in the current chunk. |
+ // Returns the initial index of the new chunk (after copied data). |
+ virtual int PrepareGrow(Vector<T> new_chunk) { |
+ return 0; |
+ } |
+}; |
+ |
+ |
+/* |
+ * A collector that allows sequences of values to be guaranteed to |
+ * stay consecutive. |
+ * If the backing store grows while a sequence is active, the current |
+ * sequence might be moved, but after the sequence is ended, it will |
+ * not move again. |
+ * NOTICE: Blocks allocated using Collector::AddBlock(int) can move |
+ * as well, if inside an active sequence where another element is added. |
+ */ |
+template <typename T> |
+class SequenceCollector : public Collector<T> { |
+ public: |
+ SequenceCollector(int initial_capacity, |
+ int growth_factor = 2, |
+ int max_growth = 1 * MB) |
+ : Collector<T>(initial_capacity, growth_factor, max_growth), |
+ sequence_start_(kNoSequence) { } |
+ |
+ virtual ~SequenceCollector() {} |
+ |
+ void StartSequence() { |
+ ASSERT(sequence_start_ == kNoSequence); |
+ sequence_start_ = this->index_; |
+ } |
+ |
+ Vector<T> EndSequence() { |
+ ASSERT(sequence_start_ != kNoSequence); |
+ int sequence_start = sequence_start_; |
+ sequence_start_ = kNoSequence; |
+ return Vector<T>(this->current_chunk_ + sequence_start, |
+ this->index_ - sequence_start); |
+ } |
+ |
+ private: |
+ static const int kNoSequence = -1; |
+ int sequence_start_; |
+ |
+ // Move the currently active sequence to the new chunk. |
+ virtual int PrepareGrow(Vector<T> new_chunk) { |
+ if (sequence_start_ != kNoSequence) { |
+ int sequence_length = this->index_ - sequence_start_; |
+ // The new chunk is always larger than the current chunk, so there |
+ // is room for the copy. |
+ ASSERT(sequence_length < new_chunk.length()); |
+ for (int i = 0; i < sequence_length; i++) { |
+ new_chunk[i] = this->current_chunk_[sequence_start_ + i]; |
+ } |
+ this->index_ = sequence_start_; |
+ sequence_start_ = 0; |
+ return sequence_length; |
+ } |
+ return 0; |
+ } |
+}; |
+ |
+ |
// Simple support to read a file into a 0-terminated C-string. |
// The returned buffer must be freed by the caller. |
// On return, *exits tells whether the file existed. |