Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 } | 469 } |
| 470 | 470 |
| 471 template <typename T> | 471 template <typename T> |
| 472 inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, | 472 inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, |
| 473 int length) { | 473 int length) { |
| 474 return Vector< Handle<Object> >( | 474 return Vector< Handle<Object> >( |
| 475 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); | 475 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); |
| 476 } | 476 } |
| 477 | 477 |
| 478 | 478 |
| 479 /* | |
| 480 * A class that collects values into a backing store. | |
| 481 * Specialized versions of the class can allow access to the backing store | |
| 482 * in different ways. | |
| 483 * There is no guarantee that the backing store is contiguous (and, as a | |
| 484 * consequence, no guarantees that consecutively added elements are adjacent | |
| 485 * in memory). The collector may move elements unless it has guaranteed not | |
| 486 * to. | |
| 487 */ | |
| 488 template <typename T> | |
| 489 class Collector { | |
| 490 public: | |
| 491 Collector(int initial_capacity = kMinCapacity, | |
| 492 int growth_factor = 2, | |
| 493 int max_growth = 1 * MB) | |
| 494 : growth_factor_(growth_factor), max_growth_(max_growth) { | |
| 495 if (initial_capacity < kMinCapacity) { | |
| 496 initial_capacity = kMinCapacity; | |
| 497 } | |
| 498 current_chunk_ = NewArray<T>(initial_capacity); | |
| 499 current_capacity_ = initial_capacity; | |
| 500 index_ = 0; | |
| 501 } | |
| 502 | |
| 503 virtual ~Collector() { | |
| 504 // Free backing store (in reverse allocation order). | |
| 505 DeleteArray(current_chunk_); | |
| 506 for (int i = chunks_.length() - 1; i >= 0; i--) { | |
| 507 chunks_.at(i).Dispose(); | |
| 508 } | |
| 509 } | |
| 510 | |
| 511 // Add a single element. | |
| 512 inline void Add(T value) { | |
| 513 if (index_ >= current_capacity_) { | |
| 514 Grow(1); | |
| 515 } | |
| 516 current_chunk_[index_] = value; | |
| 517 index_++; | |
| 518 } | |
| 519 | |
| 520 // Add a block of contiguous elements and return a Vector backed by the | |
| 521 // memory area. | |
| 522 // A basic Collector will keep this vector valid as long as the Collector | |
| 523 // is alive. | |
| 524 inline Vector<T> AddBlock(int size, T initial_value) { | |
| 525 if (index_ + size > current_capacity_) { | |
| 526 Grow(size); | |
| 527 } | |
| 528 T* position = current_chunk_ + index_; | |
| 529 index_ += size; | |
| 530 for (int i = 0; i < size; i++) { | |
| 531 position[i] = initial_value; | |
| 532 } | |
| 533 return Vector<T>(position, size); | |
| 534 } | |
| 535 | |
| 536 | |
| 537 // Allocate a single contiguous vector, copy all the collected | |
| 538 // elements to the vector, and return it. | |
| 539 // The caller is responsible for freeing the memory of the returned | |
| 540 // vector (e.g., using Vector::Dispose). | |
| 541 Vector<T> ToVector() { | |
|
Mads Ager (chromium)
2010/08/24 09:24:51
You should move some of these methods to utils.cc
| |
| 542 // Find the total length. | |
| 543 int total_length = index_; | |
| 544 for (int i = 0; i < chunks_.length(); i++) { | |
| 545 total_length += chunks_.at(i).length(); | |
| 546 } | |
| 547 T* new_store = NewArray<T>(total_length); | |
| 548 int position = 0; | |
| 549 for (int i = 0; i < chunks_.length(); i++) { | |
| 550 Vector<T> chunk = chunks_.at(i); | |
| 551 for (int j = 0; j < chunk.length(); j++) { | |
| 552 new_store[position] = chunk[j]; | |
| 553 position++; | |
| 554 } | |
| 555 } | |
| 556 for (int i = 0; i < index_; i++) { | |
| 557 new_store[position] = current_chunk_[i]; | |
| 558 position++; | |
| 559 } | |
| 560 return Vector<T>(new_store, total_length); | |
| 561 } | |
| 562 | |
| 563 protected: | |
| 564 static const int kMinCapacity = 16; | |
| 565 List<Vector<T> > chunks_; | |
| 566 T* current_chunk_; | |
| 567 int growth_factor_; | |
| 568 int max_growth_; | |
| 569 int current_capacity_; | |
| 570 int index_; | |
| 571 | |
| 572 // Creates a new current chunk, and stores the old chunk in the chunks_ list. | |
| 573 void Grow(int min_capacity) { | |
| 574 ASSERT(growth_factor_ > 1); | |
| 575 int growth = current_capacity_ * (growth_factor_ - 1); | |
| 576 if (growth > max_growth_) { | |
| 577 growth = max_growth_; | |
| 578 } | |
| 579 int new_capacity = current_capacity_ + growth; | |
| 580 if (new_capacity < min_capacity) { | |
| 581 new_capacity = min_capacity; | |
| 582 } | |
| 583 T* new_chunk = NewArray<T>(new_capacity); | |
| 584 int new_index = PrepareGrow(Vector<T>(new_chunk, new_capacity)); | |
| 585 chunks_.Add(Vector<T>(current_chunk_, index_)); | |
| 586 current_chunk_ = new_chunk; | |
| 587 current_capacity_ = new_capacity; | |
| 588 index_ = new_index; | |
| 589 ASSERT(index_ + min_capacity <= current_capacity_); | |
| 590 } | |
| 591 | |
| 592 // Before replacing the current chunk, give a subclass the option to move | |
| 593 // some of the current data into the new chunk. The function may update | |
| 594 // the current index_ value to represent data no longer in the current chunk. | |
| 595 // Returns the initial index of the new chunk (after copied data). | |
| 596 virtual int PrepareGrow(Vector<T> new_chunk) { | |
| 597 return 0; | |
| 598 } | |
| 599 }; | |
| 600 | |
| 601 | |
| 602 /* | |
| 603 * A collector that allows sequences of values to be guaranteed to | |
| 604 * stay consecutive. | |
| 605 * If the backing store grows while a sequence is active, the current | |
| 606 * sequence might be moved, but after the sequence is ended, it will | |
| 607 * not move again. | |
| 608 * NOTICE: Blocks allocated using Collector::AddBlock(int) can move | |
| 609 * as well, if inside an active sequence where another element is added. | |
| 610 */ | |
| 611 template <typename T> | |
| 612 class SequenceCollector : public Collector<T> { | |
| 613 public: | |
| 614 SequenceCollector(int initial_capacity, | |
| 615 int growth_factor = 2, | |
| 616 int max_growth = 1 * MB) | |
| 617 : Collector<T>(initial_capacity, growth_factor, max_growth), | |
| 618 sequence_start_(kNoSequence) { } | |
| 619 | |
| 620 virtual ~SequenceCollector() {} | |
| 621 | |
| 622 void StartSequence() { | |
| 623 ASSERT(sequence_start_ == kNoSequence); | |
| 624 sequence_start_ = this->index_; | |
| 625 } | |
| 626 | |
| 627 Vector<T> EndSequence() { | |
| 628 ASSERT(sequence_start_ != kNoSequence); | |
| 629 int sequence_start = sequence_start_; | |
| 630 sequence_start_ = kNoSequence; | |
| 631 return Vector<T>(this->current_chunk_ + sequence_start, | |
| 632 this->index_ - sequence_start); | |
| 633 } | |
| 634 | |
| 635 private: | |
| 636 static const int kNoSequence = -1; | |
| 637 int sequence_start_; | |
| 638 | |
| 639 // Move the currently active sequence to the new chunk. | |
| 640 virtual int PrepareGrow(Vector<T> new_chunk) { | |
| 641 if (sequence_start_ != kNoSequence) { | |
| 642 int sequence_length = this->index_ - sequence_start_; | |
| 643 // The new chunk is always larger than the current chunk, so there | |
| 644 // is room for the copy. | |
| 645 ASSERT(sequence_length < new_chunk.length()); | |
| 646 for (int i = 0; i < sequence_length; i++) { | |
| 647 new_chunk[i] = this->current_chunk_[sequence_start_ + i]; | |
| 648 } | |
| 649 this->index_ = sequence_start_; | |
| 650 sequence_start_ = 0; | |
| 651 return sequence_length; | |
| 652 } | |
| 653 return 0; | |
| 654 } | |
| 655 }; | |
| 656 | |
| 657 | |
| 479 // Simple support to read a file into a 0-terminated C-string. | 658 // Simple support to read a file into a 0-terminated C-string. |
| 480 // The returned buffer must be freed by the caller. | 659 // The returned buffer must be freed by the caller. |
| 481 // On return, *exits tells whether the file existed. | 660 // On return, *exits tells whether the file existed. |
| 482 Vector<const char> ReadFile(const char* filename, | 661 Vector<const char> ReadFile(const char* filename, |
| 483 bool* exists, | 662 bool* exists, |
| 484 bool verbose = true); | 663 bool verbose = true); |
| 485 | 664 |
| 486 | 665 |
| 487 // Simple wrapper that allows an ExternalString to refer to a | 666 // Simple wrapper that allows an ExternalString to refer to a |
| 488 // Vector<const char>. Doesn't assume ownership of the data. | 667 // Vector<const char>. Doesn't assume ownership of the data. |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 740 } | 919 } |
| 741 | 920 |
| 742 template <class Dest, class Source> | 921 template <class Dest, class Source> |
| 743 inline Dest BitCast(Source* source) { | 922 inline Dest BitCast(Source* source) { |
| 744 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); | 923 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); |
| 745 } | 924 } |
| 746 | 925 |
| 747 } } // namespace v8::internal | 926 } } // namespace v8::internal |
| 748 | 927 |
| 749 #endif // V8_UTILS_H_ | 928 #endif // V8_UTILS_H_ |
| OLD | NEW |