Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/list-inl.h

Issue 39148: In List::Add, correctly handle the case of adding a reference to a... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/list.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16 matching lines...) Expand all
27 27
28 #ifndef V8_LIST_INL_H_ 28 #ifndef V8_LIST_INL_H_
29 #define V8_LIST_INL_H_ 29 #define V8_LIST_INL_H_
30 30
31 #include "list.h" 31 #include "list.h"
32 32
33 namespace v8 { namespace internal { 33 namespace v8 { namespace internal {
34 34
35 35
36 template<typename T, class P> 36 template<typename T, class P>
37 T& List<T, P>::Add(const T& element) { 37 void List<T, P>::Add(const T& element) {
38 if (length_ >= capacity_) { 38 if (length_ < capacity_) {
39 data_[length_++] = element;
40 } else {
39 // Grow the list capacity by 50%, but make sure to let it grow 41 // Grow the list capacity by 50%, but make sure to let it grow
40 // even when the capacity is zero (possible initial case). 42 // even when the capacity is zero (possible initial case).
41 int new_capacity = 1 + capacity_ + (capacity_ >> 1); 43 int new_capacity = 1 + capacity_ + (capacity_ >> 1);
42 T* new_data = NewData(new_capacity); 44 T* new_data = NewData(new_capacity);
43 memcpy(new_data, data_, capacity_ * sizeof(T)); 45 memcpy(new_data, data_, capacity_ * sizeof(T));
46 // Since the element reference could be an element of the list,
Kasper Lund 2009/03/05 06:21:03 It would be nice with a test of this functionality
47 // assign it to the new backing store before deleting the old.
48 new_data[length_++] = element;
44 DeleteData(data_); 49 DeleteData(data_);
45 data_ = new_data; 50 data_ = new_data;
46 capacity_ = new_capacity; 51 capacity_ = new_capacity;
47 } 52 }
48 return data_[length_++] = element;
49 } 53 }
50 54
51 55
52 template<typename T, class P> 56 template<typename T, class P>
53 Vector<T> List<T, P>::AddBlock(const T& element, int count) { 57 Vector<T> List<T, P>::AddBlock(T value, int count) {
54 int start = length_; 58 int start = length_;
55 for (int i = 0; i < count; i++) 59 for (int i = 0; i < count; i++) Add(value);
56 Add(element);
57 return Vector<T>(&data_[start], count); 60 return Vector<T>(&data_[start], count);
58 } 61 }
59 62
60 63
61 template<typename T, class P> 64 template<typename T, class P>
62 T& List<T, P>::Insert(int i, const T& element) {
63 int free_index = length_ - 1;
64 Add(last()); // Add grows the list if necessary.
65 while (free_index > i) {
66 data_[free_index] = data_[free_index - 1];
67 free_index--;
68 }
69 data_[free_index] = element;
70 }
71
72
73 template<typename T, class P>
74 T List<T, P>::Remove(int i) { 65 T List<T, P>::Remove(int i) {
75 T element = at(i); 66 T element = at(i);
76 length_--; 67 length_--;
77 while (i < length_) { 68 while (i < length_) {
78 data_[i] = data_[i + 1]; 69 data_[i] = data_[i + 1];
79 i++; 70 i++;
80 } 71 }
81 return element; 72 return element;
82 } 73 }
83 74
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 ASSERT(capacity >= 0); 123 ASSERT(capacity >= 0);
133 data_ = (capacity > 0) ? NewData(capacity) : NULL; 124 data_ = (capacity > 0) ? NewData(capacity) : NULL;
134 capacity_ = capacity; 125 capacity_ = capacity;
135 length_ = 0; 126 length_ = 0;
136 } 127 }
137 128
138 129
139 } } // namespace v8::internal 130 } } // namespace v8::internal
140 131
141 #endif // V8_LIST_INL_H_ 132 #endif // V8_LIST_INL_H_
OLDNEW
« no previous file with comments | « src/list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698