Chromium Code Reviews| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 // | 42 // |
| 43 // The list is parameterized by the type of its elements (T) and by an | 43 // The list is parameterized by the type of its elements (T) and by an |
| 44 // allocation policy (P). The policy is used for allocating lists in | 44 // allocation policy (P). The policy is used for allocating lists in |
| 45 // the C free store or the zone; see zone.h. | 45 // the C free store or the zone; see zone.h. |
| 46 | 46 |
| 47 // Forward defined as | 47 // Forward defined as |
| 48 // template <typename T, class P = FreeStoreAllocationPolicy> class List; | 48 // template <typename T, class P = FreeStoreAllocationPolicy> class List; |
| 49 template <typename T, class P> | 49 template <typename T, class P> |
| 50 class List { | 50 class List { |
| 51 public: | 51 public: |
| 52 List() { Initialize(0); } | 52 typedef typename P::Alloc Allocator; |
|
danno
2012/05/25 11:03:37
AllocationPolicy instead of P
| |
| 53 INLINE(explicit List(int capacity)) { Initialize(capacity); } | 53 typedef typename P::Dealloc Deallocator; |
| 54 explicit List(Allocator allocator = Allocator(), | |
| 55 Deallocator deallocator = Deallocator()) | |
| 56 : deallocator_(deallocator) { | |
| 57 Initialize(0, allocator); | |
| 58 } | |
| 59 INLINE(explicit List(int capacity, Allocator allocator = Allocator(), | |
| 60 Deallocator deallocator = Deallocator())) | |
| 61 : deallocator_(deallocator) { | |
| 62 Initialize(capacity, allocator); | |
| 63 } | |
| 54 INLINE(~List()) { DeleteData(data_); } | 64 INLINE(~List()) { DeleteData(data_); } |
| 55 | 65 |
| 56 // Deallocates memory used by the list and leaves the list in a consistent | 66 // Deallocates memory used by the list and leaves the list in a consistent |
| 57 // empty state. | 67 // empty state. |
| 58 void Free() { | 68 void Free() { |
| 59 DeleteData(data_); | 69 DeleteData(data_); |
| 60 Initialize(0); | 70 Initialize(0); |
| 61 } | 71 } |
| 62 | 72 |
| 63 INLINE(void* operator new(size_t size)) { | 73 INLINE(void* operator new(size_t size, Allocator allocator = Allocator())) { |
| 64 return P::New(static_cast<int>(size)); | 74 return allocator.New(static_cast<int>(size)); |
| 65 } | 75 } |
| 66 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } | 76 INLINE(void operator delete(void* p)) { |
| 77 reinterpret_cast<List*>(p)->deallocator_.Delete(p); | |
| 78 } | |
| 67 | 79 |
| 68 // Returns a reference to the element at index i. This reference is | 80 // Returns a reference to the element at index i. This reference is |
| 69 // not safe to use after operations that can change the list's | 81 // not safe to use after operations that can change the list's |
| 70 // backing store (e.g. Add). | 82 // backing store (e.g. Add). |
| 71 inline T& operator[](int i) const { | 83 inline T& operator[](int i) const { |
| 72 ASSERT(0 <= i); | 84 ASSERT(0 <= i); |
| 73 ASSERT(i < length_); | 85 ASSERT(i < length_); |
| 74 return data_[i]; | 86 return data_[i]; |
| 75 } | 87 } |
| 76 inline T& at(int i) const { return operator[](i); } | 88 inline T& at(int i) const { return operator[](i); } |
| 77 inline T& last() const { return at(length_ - 1); } | 89 inline T& last() const { return at(length_ - 1); } |
| 78 inline T& first() const { return at(0); } | 90 inline T& first() const { return at(0); } |
| 79 | 91 |
| 80 INLINE(bool is_empty() const) { return length_ == 0; } | 92 INLINE(bool is_empty() const) { return length_ == 0; } |
| 81 INLINE(int length() const) { return length_; } | 93 INLINE(int length() const) { return length_; } |
| 82 INLINE(int capacity() const) { return capacity_; } | 94 INLINE(int capacity() const) { return capacity_; } |
| 83 | 95 |
| 84 Vector<T> ToVector() const { return Vector<T>(data_, length_); } | 96 Vector<T> ToVector() const { return Vector<T>(data_, length_); } |
| 85 | 97 |
| 86 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); } | 98 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); } |
| 87 | 99 |
| 88 // Adds a copy of the given 'element' to the end of the list, | 100 // Adds a copy of the given 'element' to the end of the list, |
| 89 // expanding the list if necessary. | 101 // expanding the list if necessary. |
| 90 void Add(const T& element); | 102 void Add(const T& element, Allocator allocator = Allocator()); |
| 91 | 103 |
| 92 // Add all the elements from the argument list to this list. | 104 // Add all the elements from the argument list to this list. |
| 93 void AddAll(const List<T, P>& other); | 105 void AddAll(const List<T, P>& other, Allocator allocator = Allocator()); |
| 94 | 106 |
| 95 // Add all the elements from the vector to this list. | 107 // Add all the elements from the vector to this list. |
| 96 void AddAll(const Vector<T>& other); | 108 void AddAll(const Vector<T>& other, Allocator allocator = Allocator()); |
| 97 | 109 |
| 98 // Inserts the element at the specific index. | 110 // Inserts the element at the specific index. |
| 99 void InsertAt(int index, const T& element); | 111 void InsertAt(int index, const T& element, Allocator allocator = Allocator()); |
| 100 | 112 |
| 101 // Added 'count' elements with the value 'value' and returns a | 113 // Added 'count' elements with the value 'value' and returns a |
| 102 // vector that allows access to the elements. The vector is valid | 114 // vector that allows access to the elements. The vector is valid |
| 103 // until the next change is made to this list. | 115 // until the next change is made to this list. |
| 104 Vector<T> AddBlock(T value, int count); | 116 Vector<T> AddBlock(T value, int count, Allocator allocator = Allocator()); |
| 105 | 117 |
| 106 // Removes the i'th element without deleting it even if T is a | 118 // Removes the i'th element without deleting it even if T is a |
| 107 // pointer type; moves all elements above i "down". Returns the | 119 // pointer type; moves all elements above i "down". Returns the |
| 108 // removed element. This function's complexity is linear in the | 120 // removed element. This function's complexity is linear in the |
| 109 // size of the list. | 121 // size of the list. |
| 110 T Remove(int i); | 122 T Remove(int i); |
| 111 | 123 |
| 112 // Remove the given element from the list. Returns whether or not | 124 // Remove the given element from the list. Returns whether or not |
| 113 // the input is included in the list in the first place. | 125 // the input is included in the list in the first place. |
| 114 bool RemoveElement(const T& elm); | 126 bool RemoveElement(const T& elm); |
| 115 | 127 |
| 116 // Removes the last element without deleting it even if T is a | 128 // Removes the last element without deleting it even if T is a |
| 117 // pointer type. Returns the removed element. | 129 // pointer type. Returns the removed element. |
| 118 INLINE(T RemoveLast()) { return Remove(length_ - 1); } | 130 INLINE(T RemoveLast()) { return Remove(length_ - 1); } |
| 119 | 131 |
| 120 // Deletes current list contents and allocates space for 'length' elements. | 132 // Deletes current list contents and allocates space for 'length' elements. |
| 121 INLINE(void Allocate(int length)); | 133 INLINE(void Allocate(int length, Allocator allocator = Allocator())); |
| 122 | 134 |
| 123 // Clears the list by setting the length to zero. Even if T is a | 135 // Clears the list by setting the length to zero. Even if T is a |
| 124 // pointer type, clearing the list doesn't delete the entries. | 136 // pointer type, clearing the list doesn't delete the entries. |
| 125 INLINE(void Clear()); | 137 INLINE(void Clear()); |
| 126 | 138 |
| 127 // Drops all but the first 'pos' elements from the list. | 139 // Drops all but the first 'pos' elements from the list. |
| 128 INLINE(void Rewind(int pos)); | 140 INLINE(void Rewind(int pos)); |
| 129 | 141 |
| 130 // Drop the last 'count' elements from the list. | 142 // Drop the last 'count' elements from the list. |
| 131 INLINE(void RewindBy(int count)) { Rewind(length_ - count); } | 143 INLINE(void RewindBy(int count)) { Rewind(length_ - count); } |
| 132 | 144 |
| 133 bool Contains(const T& elm) const; | 145 bool Contains(const T& elm) const; |
| 134 int CountOccurrences(const T& elm, int start, int end) const; | 146 int CountOccurrences(const T& elm, int start, int end) const; |
| 135 | 147 |
| 136 // Iterate through all list entries, starting at index 0. | 148 // Iterate through all list entries, starting at index 0. |
| 137 void Iterate(void (*callback)(T* x)); | 149 void Iterate(void (*callback)(T* x)); |
| 138 template<class Visitor> | 150 template<class Visitor> |
| 139 void Iterate(Visitor* visitor); | 151 void Iterate(Visitor* visitor); |
| 140 | 152 |
| 141 // Sort all list entries (using QuickSort) | 153 // Sort all list entries (using QuickSort) |
| 142 void Sort(int (*cmp)(const T* x, const T* y)); | 154 void Sort(int (*cmp)(const T* x, const T* y)); |
| 143 void Sort(); | 155 void Sort(); |
| 144 | 156 |
| 145 INLINE(void Initialize(int capacity)); | 157 INLINE(void Initialize(int capacity, Allocator allocator = Allocator())); |
| 146 | 158 |
| 147 private: | 159 private: |
| 148 T* data_; | 160 T* data_; |
| 149 int capacity_; | 161 int capacity_; |
| 150 int length_; | 162 int length_; |
| 163 Deallocator deallocator_; | |
| 151 | 164 |
| 152 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } | 165 INLINE(T* NewData(int n, Allocator allocator)) { |
| 153 INLINE(void DeleteData(T* data)) { P::Delete(data); } | 166 return static_cast<T*>(allocator.New(n * sizeof(T))); |
| 167 } | |
| 168 INLINE(void DeleteData(T* data)) { | |
| 169 deallocator_.Delete(data); | |
| 170 } | |
| 154 | 171 |
| 155 // Increase the capacity of a full list, and add an element. | 172 // Increase the capacity of a full list, and add an element. |
| 156 // List must be full already. | 173 // List must be full already. |
| 157 void ResizeAdd(const T& element); | 174 void ResizeAdd(const T& element, Allocator alloc); |
| 158 | 175 |
| 159 // Inlined implementation of ResizeAdd, shared by inlined and | 176 // Inlined implementation of ResizeAdd, shared by inlined and |
| 160 // non-inlined versions of ResizeAdd. | 177 // non-inlined versions of ResizeAdd. |
| 161 void ResizeAddInternal(const T& element); | 178 void ResizeAddInternal(const T& element, Allocator alloc); |
| 162 | 179 |
| 163 // Resize the list. | 180 // Resize the list. |
| 164 void Resize(int new_capacity); | 181 void Resize(int new_capacity, Allocator alloc); |
| 165 | 182 |
| 166 DISALLOW_COPY_AND_ASSIGN(List); | 183 DISALLOW_COPY_AND_ASSIGN(List); |
| 167 }; | 184 }; |
| 168 | 185 |
| 169 class Map; | 186 class Map; |
| 170 class Code; | 187 class Code; |
| 171 template<typename T> class Handle; | 188 template<typename T> class Handle; |
| 172 typedef List<Map*> MapList; | 189 typedef List<Map*> MapList; |
| 173 typedef List<Code*> CodeList; | 190 typedef List<Code*> CodeList; |
| 174 typedef List<Handle<Map> > MapHandleList; | 191 typedef List<Handle<Map> > MapHandleList; |
| 175 typedef List<Handle<Code> > CodeHandleList; | 192 typedef List<Handle<Code> > CodeHandleList; |
| 176 | 193 |
| 177 // Perform binary search for an element in an already sorted | 194 // Perform binary search for an element in an already sorted |
| 178 // list. Returns the index of the element of -1 if it was not found. | 195 // list. Returns the index of the element of -1 if it was not found. |
| 179 // |cmp| is a predicate that takes a pointer to an element of the List | 196 // |cmp| is a predicate that takes a pointer to an element of the List |
| 180 // and returns +1 if it is greater, -1 if it is less than the element | 197 // and returns +1 if it is greater, -1 if it is less than the element |
| 181 // being searched. | 198 // being searched. |
| 182 template <typename T, class P> | 199 template <typename T, class P> |
| 183 int SortedListBSearch(const List<T>& list, P cmp); | 200 int SortedListBSearch(const List<T>& list, P cmp); |
| 184 template <typename T> | 201 template <typename T> |
| 185 int SortedListBSearch(const List<T>& list, T elem); | 202 int SortedListBSearch(const List<T>& list, T elem); |
| 186 | 203 |
| 187 | 204 |
| 188 } } // namespace v8::internal | 205 } } // namespace v8::internal |
| 189 | 206 |
| 190 | 207 |
| 191 #endif // V8_LIST_H_ | 208 #endif // V8_LIST_H_ |
| OLD | NEW |