Commit cc619a34 authored by ager@chromium.org's avatar ager@chromium.org

Remove list copy constructor (for which there was no corresponding

assignment operator) and add an AddAll method to lists instead.
Review URL: http://codereview.chromium.org/115705

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 6244c02f
...@@ -480,8 +480,10 @@ void BreakTarget::set_direction(Directionality direction) { ...@@ -480,8 +480,10 @@ void BreakTarget::set_direction(Directionality direction) {
void BreakTarget::CopyTo(BreakTarget* destination) { void BreakTarget::CopyTo(BreakTarget* destination) {
ASSERT(destination != NULL); ASSERT(destination != NULL);
destination->direction_ = direction_; destination->direction_ = direction_;
destination->reaching_frames_ = reaching_frames_; destination->reaching_frames_.Rewind(0);
destination->merge_labels_ = merge_labels_; destination->reaching_frames_.AddAll(reaching_frames_);
destination->merge_labels_.Rewind(0);
destination->merge_labels_.AddAll(merge_labels_);
destination->entry_frame_ = entry_frame_; destination->entry_frame_ = entry_frame_;
destination->entry_label_ = entry_label_; destination->entry_label_ = entry_label_;
destination->expected_height_ = expected_height_; destination->expected_height_ = expected_height_;
......
...@@ -34,32 +34,23 @@ namespace v8 { namespace internal { ...@@ -34,32 +34,23 @@ namespace v8 { namespace internal {
template<typename T, class P> template<typename T, class P>
List<T, P>::List(const List<T, P>& other) { void List<T, P>::Add(const T& element) {
ASSERT(other.capacity() >= 0); if (length_ < capacity_) {
capacity_ = other.capacity(); data_[length_++] = element;
length_ = other.length();
if (capacity_ > 0) {
data_ = NewData(capacity_);
int copy_size = length_ * sizeof(T);
const int kMinMemCpySize = 64;
if (copy_size < kMinMemCpySize) {
for (int i = 0; i < length_; i++) data_[i] = other.data_[i];
} else {
memcpy(data_, other.data_, copy_size);
}
} else { } else {
data_ = NULL; List<T, P>::ResizeAdd(element);
} }
} }
template<typename T, class P> template<typename T, class P>
void List<T, P>::Add(const T& element) { void List<T, P>::AddAll(const List<T, P>& other) {
if (length_ < capacity_) { int result_length = length_ + other.length_;
data_[length_++] = element; if (capacity_ < result_length) Resize(result_length);
} else { for (int i = 0; i < other.length_; i++) {
List<T, P>::ResizeAdd(element); data_[length_ + i] = other.data_[i];
} }
length_ = result_length;
} }
...@@ -77,11 +68,18 @@ void List<T, P>::ResizeAddInternal(const T& element) { ...@@ -77,11 +68,18 @@ void List<T, P>::ResizeAddInternal(const T& element) {
// Grow the list capacity by 50%, but make sure to let it grow // Grow the list capacity by 50%, but make sure to let it grow
// even when the capacity is zero (possible initial case). // even when the capacity is zero (possible initial case).
int new_capacity = 1 + capacity_ + (capacity_ >> 1); int new_capacity = 1 + capacity_ + (capacity_ >> 1);
// Since the element reference could be an element of the list, copy
// it out of the old backing storage before resizing.
T temp = element;
Resize(new_capacity);
data_[length_++] = temp;
}
template<typename T, class P>
void List<T, P>::Resize(int new_capacity) {
T* new_data = List<T, P>::NewData(new_capacity); T* new_data = List<T, P>::NewData(new_capacity);
memcpy(new_data, data_, capacity_ * sizeof(T)); memcpy(new_data, data_, capacity_ * sizeof(T));
// Since the element reference could be an element of the list,
// assign it to the new backing store before deleting the old.
new_data[length_++] = element;
List<T, P>::DeleteData(data_); List<T, P>::DeleteData(data_);
data_ = new_data; data_ = new_data;
capacity_ = new_capacity; capacity_ = new_capacity;
......
...@@ -48,7 +48,6 @@ class List { ...@@ -48,7 +48,6 @@ class List {
public: public:
INLINE(explicit List(int capacity)) { Initialize(capacity); } INLINE(explicit List(int capacity)) { Initialize(capacity); }
INLINE(explicit List(const List<T, P>& other));
INLINE(~List()) { DeleteData(data_); } INLINE(~List()) { DeleteData(data_); }
INLINE(void* operator new(size_t size)) { return P::New(size); } INLINE(void* operator new(size_t size)) { return P::New(size); }
...@@ -78,6 +77,9 @@ class List { ...@@ -78,6 +77,9 @@ class List {
// expanding the list if necessary. // expanding the list if necessary.
void Add(const T& element); void Add(const T& element);
// Add all the elements from the argument list to this list.
void AddAll(const List<T, P>& other);
// Added 'count' elements with the value 'value' and returns a // Added 'count' elements with the value 'value' and returns a
// vector that allows access to the elements. The vector is valid // vector that allows access to the elements. The vector is valid
// until the next change is made to this list. // until the next change is made to this list.
...@@ -126,6 +128,11 @@ class List { ...@@ -126,6 +128,11 @@ class List {
// Inlined implementation of ResizeAdd, shared by inlined and // Inlined implementation of ResizeAdd, shared by inlined and
// non-inlined versions of ResizeAdd. // non-inlined versions of ResizeAdd.
void ResizeAddInternal(const T& element); void ResizeAddInternal(const T& element);
// Resize the list.
void Resize(int new_capacity);
DISALLOW_COPY_AND_ASSIGN(List);
}; };
class FrameElement; class FrameElement;
......
...@@ -37,8 +37,9 @@ namespace v8 { namespace internal { ...@@ -37,8 +37,9 @@ namespace v8 { namespace internal {
// When cloned, a frame is a deep copy of the original. // When cloned, a frame is a deep copy of the original.
VirtualFrame::VirtualFrame(VirtualFrame* original) VirtualFrame::VirtualFrame(VirtualFrame* original)
: elements_(original->elements_), : elements_(original->elements_.length()),
stack_pointer_(original->stack_pointer_) { stack_pointer_(original->stack_pointer_) {
elements_.AddAll(original->elements_);
// Copy register locations from original. // Copy register locations from original.
memcpy(&register_locations_, memcpy(&register_locations_,
original->register_locations_, original->register_locations_,
......
...@@ -65,3 +65,37 @@ TEST(ListAdd) { ...@@ -65,3 +65,37 @@ TEST(ListAdd) {
list.Add(list[0]); list.Add(list[0]);
CHECK_EQ(1, list[4]); CHECK_EQ(1, list[4]);
} }
// Test that we can add all elements from a list to another list.
TEST(ListAddAll) {
List<int, ZeroingAllocationPolicy> list(4);
list.Add(0);
list.Add(1);
list.Add(2);
CHECK_EQ(3, list.length());
for (int i = 0; i < 3; i++) {
CHECK_EQ(i, list[i]);
}
List<int, ZeroingAllocationPolicy> other_list(4);
// Add no elements to list since other_list is empty.
list.AddAll(other_list);
CHECK_EQ(3, list.length());
for (int i = 0; i < 3; i++) {
CHECK_EQ(i, list[i]);
}
// Add three elements to other_list.
other_list.Add(0);
other_list.Add(1);
other_list.Add(2);
// Copy the three elements from other_list to list.
list.AddAll(other_list);
CHECK_EQ(6, list.length());
for (int i = 0; i < 6; i++) {
CHECK_EQ(i % 3, list[i]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment