blob: c17c4ecfee580805fbdb4ae2d2744c409cf32318 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_LIST_H_
6#define V8_LIST_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/checks.h"
9#include "src/utils.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000010
Steve Blocka7e24c12009-10-30 11:49:00 +000011namespace v8 {
12namespace internal {
13
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014template<typename T> class Vector;
Steve Blocka7e24c12009-10-30 11:49:00 +000015
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 Murdochb8a8cc12014-11-26 15:28:44 +000027// template <typename T,
28// class AllocationPolicy = FreeStoreAllocationPolicy> class List;
29template <typename T, class AllocationPolicy>
Steve Blocka7e24c12009-10-30 11:49:00 +000030class List {
31 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 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 Blocka7e24c12009-10-30 11:49:00 +000039 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 Murdochb8a8cc12014-11-26 15:28:44 +000048 INLINE(void* operator new(size_t size,
49 AllocationPolicy allocator = AllocationPolicy())) {
50 return allocator.New(static_cast<int>(size));
Steve Blockd0582a62009-12-15 09:54:21 +000051 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 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 Blocka7e24c12009-10-30 11:49:00 +000060
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 Murdoch3ef787d2012-04-12 10:51:47 +010063 // backing store (e.g. Add).
Kristian Monsen0d5e1162010-09-30 15:31:59 +010064 inline T& operator[](int i) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 DCHECK(0 <= i);
66 SLOW_DCHECK(i < length_);
Steve Blocka7e24c12009-10-30 11:49:00 +000067 return data_[i];
68 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +010069 inline T& at(int i) const { return operator[](i); }
Steve Blocka7e24c12009-10-30 11:49:00 +000070 inline T& last() const { return at(length_ - 1); }
71 inline T& first() const { return at(0); }
72
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 typedef T* iterator;
74 inline iterator begin() const { return &data_[0]; }
75 inline iterator end() const { return &data_[length_]; }
76
Steve Blocka7e24c12009-10-30 11:49:00 +000077 INLINE(bool is_empty() const) { return length_ == 0; }
78 INLINE(int length() const) { return length_; }
79 INLINE(int capacity() const) { return capacity_; }
80
Ben Murdoch257744e2011-11-30 15:57:28 +000081 Vector<T> ToVector() const { return Vector<T>(data_, length_); }
Steve Blocka7e24c12009-10-30 11:49:00 +000082
Emily Bernierd0a1eb72015-03-24 16:35:39 -040083 Vector<const T> ToConstVector() const {
84 return Vector<const T>(data_, length_);
85 }
Steve Blocka7e24c12009-10-30 11:49:00 +000086
87 // Adds a copy of the given 'element' to the end of the list,
88 // expanding the list if necessary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +000090
91 // Add all the elements from the argument list to this list.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 void AddAll(const List<T, AllocationPolicy>& other,
93 AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +000094
Ben Murdoch257744e2011-11-30 15:57:28 +000095 // Add all the elements from the vector to this list.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 void AddAll(const Vector<T>& other,
97 AllocationPolicy allocator = AllocationPolicy());
Ben Murdoch257744e2011-11-30 15:57:28 +000098
Ben Murdochb0fe1622011-05-05 13:52:32 +010099 // Inserts the element at the specific index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 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 Murdochb0fe1622011-05-05 13:52:32 +0100105
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000109 Vector<T> AddBlock(T value, int count,
110 AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
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 Murdochb0fe1622011-05-05 13:52:32 +0100118 // 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 Blocka7e24c12009-10-30 11:49:00 +0000122 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000126 // Deletes current list contents and allocates space for 'length' elements.
127 INLINE(void Allocate(int length,
128 AllocationPolicy allocator = AllocationPolicy()));
129
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100137 // Drop the last 'count' elements from the list.
138 INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
139
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 // Halve the capacity if fill level is less than a quarter.
141 INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
142
Ben Murdochb0fe1622011-05-05 13:52:32 +0100143 bool Contains(const T& elm) const;
144 int CountOccurrences(const T& elm, int start, int end) const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
146 // Iterate through all list entries, starting at index 0.
147 void Iterate(void (*callback)(T* x));
Iain Merrick75681382010-08-19 15:07:18 +0100148 template<class Visitor>
149 void Iterate(Visitor* visitor);
Steve Blocka7e24c12009-10-30 11:49:00 +0000150
151 // Sort all list entries (using QuickSort)
152 void Sort(int (*cmp)(const T* x, const T* y));
153 void Sort();
154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 INLINE(void Initialize(int capacity,
156 AllocationPolicy allocator = AllocationPolicy()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
158 private:
159 T* data_;
160 int capacity_;
161 int length_;
162
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 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 Blocka7e24c12009-10-30 11:49:00 +0000169
170 // Increase the capacity of a full list, and add an element.
171 // List must be full already.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 void ResizeAdd(const T& element, AllocationPolicy allocator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000173
174 // Inlined implementation of ResizeAdd, shared by inlined and
175 // non-inlined versions of ResizeAdd.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 void ResizeAddInternal(const T& element, AllocationPolicy allocator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000177
178 // Resize the list.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 void Resize(int new_capacity, AllocationPolicy allocator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000180
181 DISALLOW_COPY_AND_ASSIGN(List);
182};
183
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184
185template<typename T, class P>
186size_t GetMemoryUsedByList(const List<T, P>& list) {
187 return list.length() * sizeof(T) + sizeof(list);
188}
189
190
Ben Murdoch257744e2011-11-30 15:57:28 +0000191class Map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192template<class> class TypeImpl;
193struct HeapTypeConfig;
194typedef TypeImpl<HeapTypeConfig> HeapType;
Ben Murdoch257744e2011-11-30 15:57:28 +0000195class Code;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100196template<typename T> class Handle;
Ben Murdoch257744e2011-11-30 15:57:28 +0000197typedef List<Map*> MapList;
198typedef List<Code*> CodeList;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100199typedef List<Handle<Map> > MapHandleList;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200typedef List<Handle<HeapType> > TypeHandleList;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100201typedef List<Handle<Code> > CodeHandleList;
Ben Murdoch257744e2011-11-30 15:57:28 +0000202
Ben Murdoch589d6972011-11-30 16:04:58 +0000203// 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 Murdochb8a8cc12014-11-26 15:28:44 +0000205// |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.
208template <typename T, class P>
209int SortedListBSearch(const List<T>& list, P cmp);
Ben Murdoch589d6972011-11-30 16:04:58 +0000210template <typename T>
211int SortedListBSearch(const List<T>& list, T elem);
212
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100213
Steve Blocka7e24c12009-10-30 11:49:00 +0000214} } // namespace v8::internal
215
Ben Murdoch589d6972011-11-30 16:04:58 +0000216
Steve Blocka7e24c12009-10-30 11:49:00 +0000217#endif // V8_LIST_H_