Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_LIST_H_ |
| 6 | #define V8_LIST_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/checks.h" |
| 11 | #include "src/utils.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 12 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | template<typename T> class Vector; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | // The list is a template for very light-weight lists. We are not |
| 20 | // using the STL because we want full control over space and speed of |
| 21 | // the code. This implementation is based on code by Robert Griesemer |
| 22 | // and Rob Pike. |
| 23 | // |
| 24 | // The list is parameterized by the type of its elements (T) and by an |
| 25 | // allocation policy (P). The policy is used for allocating lists in |
| 26 | // the C free store or the zone; see zone.h. |
| 27 | |
| 28 | // Forward defined as |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | // template <typename T, |
| 30 | // class AllocationPolicy = FreeStoreAllocationPolicy> class List; |
| 31 | template <typename T, class AllocationPolicy> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | class List { |
| 33 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | explicit List(AllocationPolicy allocator = AllocationPolicy()) { |
| 35 | Initialize(0, allocator); |
| 36 | } |
| 37 | INLINE(explicit List(int capacity, |
| 38 | AllocationPolicy allocator = AllocationPolicy())) { |
| 39 | Initialize(capacity, allocator); |
| 40 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | INLINE(~List()) { DeleteData(data_); } |
| 42 | |
| 43 | // Deallocates memory used by the list and leaves the list in a consistent |
| 44 | // empty state. |
| 45 | void Free() { |
| 46 | DeleteData(data_); |
| 47 | Initialize(0); |
| 48 | } |
| 49 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | INLINE(void* operator new(size_t size, |
| 51 | AllocationPolicy allocator = AllocationPolicy())) { |
| 52 | return allocator.New(static_cast<int>(size)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 53 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 54 | INLINE(void operator delete(void* p)) { |
| 55 | AllocationPolicy::Delete(p); |
| 56 | } |
| 57 | |
| 58 | // Please the MSVC compiler. We should never have to execute this. |
| 59 | INLINE(void operator delete(void* p, AllocationPolicy allocator)) { |
| 60 | UNREACHABLE(); |
| 61 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | |
| 63 | // Returns a reference to the element at index i. This reference is |
| 64 | // not safe to use after operations that can change the list's |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 65 | // backing store (e.g. Add). |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 66 | inline T& operator[](int i) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | DCHECK(0 <= i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | SLOW_DCHECK(static_cast<unsigned>(i) < static_cast<unsigned>(length_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | return data_[i]; |
| 70 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 71 | inline T& at(int i) const { return operator[](i); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | inline T& last() const { return at(length_ - 1); } |
| 73 | inline T& first() const { return at(0); } |
| 74 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | typedef T* iterator; |
| 76 | inline iterator begin() const { return &data_[0]; } |
| 77 | inline iterator end() const { return &data_[length_]; } |
| 78 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | INLINE(bool is_empty() const) { return length_ == 0; } |
| 80 | INLINE(int length() const) { return length_; } |
| 81 | INLINE(int capacity() const) { return capacity_; } |
| 82 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 83 | Vector<T> ToVector() const { return Vector<T>(data_, length_); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 85 | Vector<const T> ToConstVector() const { |
| 86 | return Vector<const T>(data_, length_); |
| 87 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
| 89 | // Adds a copy of the given 'element' to the end of the list, |
| 90 | // expanding the list if necessary. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 91 | void Add(const T& element, AllocationPolicy allocator = AllocationPolicy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | |
| 93 | // Add all the elements from the argument list to this list. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | void AddAll(const List<T, AllocationPolicy>& other, |
| 95 | AllocationPolicy allocator = AllocationPolicy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 97 | // Add all the elements from the vector to this list. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | void AddAll(const Vector<T>& other, |
| 99 | AllocationPolicy allocator = AllocationPolicy()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 100 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 101 | // Inserts the element at the specific index. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | void InsertAt(int index, const T& element, |
| 103 | AllocationPolicy allocator = AllocationPolicy()); |
| 104 | |
| 105 | // Overwrites the element at the specific index. |
| 106 | void Set(int index, const T& element); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 107 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | // Added 'count' elements with the value 'value' and returns a |
| 109 | // vector that allows access to the elements. The vector is valid |
| 110 | // until the next change is made to this list. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | Vector<T> AddBlock(T value, int count, |
| 112 | AllocationPolicy allocator = AllocationPolicy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
| 114 | // Removes the i'th element without deleting it even if T is a |
| 115 | // pointer type; moves all elements above i "down". Returns the |
| 116 | // removed element. This function's complexity is linear in the |
| 117 | // size of the list. |
| 118 | T Remove(int i); |
| 119 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 120 | // Remove the given element from the list. Returns whether or not |
| 121 | // the input is included in the list in the first place. |
| 122 | bool RemoveElement(const T& elm); |
| 123 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | // Removes the last element without deleting it even if T is a |
| 125 | // pointer type. Returns the removed element. |
| 126 | INLINE(T RemoveLast()) { return Remove(length_ - 1); } |
| 127 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | // Deletes current list contents and allocates space for 'length' elements. |
| 129 | INLINE(void Allocate(int length, |
| 130 | AllocationPolicy allocator = AllocationPolicy())); |
| 131 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | // Clears the list by setting the length to zero. Even if T is a |
| 133 | // pointer type, clearing the list doesn't delete the entries. |
| 134 | INLINE(void Clear()); |
| 135 | |
| 136 | // Drops all but the first 'pos' elements from the list. |
| 137 | INLINE(void Rewind(int pos)); |
| 138 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 139 | // Drop the last 'count' elements from the list. |
| 140 | INLINE(void RewindBy(int count)) { Rewind(length_ - count); } |
| 141 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 142 | // Swaps the contents of the two lists. |
| 143 | INLINE(void Swap(List<T, AllocationPolicy>* list)); |
| 144 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | // Halve the capacity if fill level is less than a quarter. |
| 146 | INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy())); |
| 147 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 148 | bool Contains(const T& elm) const; |
| 149 | int CountOccurrences(const T& elm, int start, int end) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | |
| 151 | // Iterate through all list entries, starting at index 0. |
| 152 | void Iterate(void (*callback)(T* x)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 153 | template<class Visitor> |
| 154 | void Iterate(Visitor* visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | |
| 156 | // Sort all list entries (using QuickSort) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | template <typename CompareFunction> |
| 158 | void Sort(CompareFunction cmp, size_t start, size_t length); |
| 159 | template <typename CompareFunction> |
| 160 | void Sort(CompareFunction cmp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | void Sort(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | template <typename CompareFunction> |
| 163 | void StableSort(CompareFunction cmp, size_t start, size_t length); |
| 164 | template <typename CompareFunction> |
| 165 | void StableSort(CompareFunction cmp); |
| 166 | void StableSort(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 168 | INLINE(void Initialize(int capacity, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 169 | AllocationPolicy allocator = AllocationPolicy())) { |
| 170 | DCHECK(capacity >= 0); |
| 171 | data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL; |
| 172 | capacity_ = capacity; |
| 173 | length_ = 0; |
| 174 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | T* data_; |
| 178 | int capacity_; |
| 179 | int length_; |
| 180 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | INLINE(T* NewData(int n, AllocationPolicy allocator)) { |
| 182 | return static_cast<T*>(allocator.New(n * sizeof(T))); |
| 183 | } |
| 184 | INLINE(void DeleteData(T* data)) { |
| 185 | AllocationPolicy::Delete(data); |
| 186 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | |
| 188 | // Increase the capacity of a full list, and add an element. |
| 189 | // List must be full already. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 190 | void ResizeAdd(const T& element, AllocationPolicy allocator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | |
| 192 | // Inlined implementation of ResizeAdd, shared by inlined and |
| 193 | // non-inlined versions of ResizeAdd. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 194 | void ResizeAddInternal(const T& element, AllocationPolicy allocator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | |
| 196 | // Resize the list. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 197 | void Resize(int new_capacity, AllocationPolicy allocator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | |
| 199 | DISALLOW_COPY_AND_ASSIGN(List); |
| 200 | }; |
| 201 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 202 | |
| 203 | template<typename T, class P> |
| 204 | size_t GetMemoryUsedByList(const List<T, P>& list) { |
| 205 | return list.length() * sizeof(T) + sizeof(list); |
| 206 | } |
| 207 | |
| 208 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 209 | class Map; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 210 | class FieldType; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 211 | class Code; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 212 | template<typename T> class Handle; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 213 | typedef List<Map*> MapList; |
| 214 | typedef List<Code*> CodeList; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 215 | typedef List<Handle<Map> > MapHandleList; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 216 | typedef List<Handle<FieldType> > TypeHandleList; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 217 | typedef List<Handle<Code> > CodeHandleList; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 218 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 219 | // Perform binary search for an element in an already sorted |
| 220 | // list. Returns the index of the element of -1 if it was not found. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 221 | // |cmp| is a predicate that takes a pointer to an element of the List |
| 222 | // and returns +1 if it is greater, -1 if it is less than the element |
| 223 | // being searched. |
| 224 | template <typename T, class P> |
| 225 | int SortedListBSearch(const List<T>& list, P cmp); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 226 | template <typename T> |
| 227 | int SortedListBSearch(const List<T>& list, T elem); |
| 228 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 229 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 230 | } // namespace internal |
| 231 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 233 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | #endif // V8_LIST_H_ |