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