1// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_LIST_H_
29#define V8_LIST_H_
30
31#include "utils.h"
32
33namespace v8 {
34namespace internal {
35
36
37// ----------------------------------------------------------------------------
38// The list is a template for very light-weight lists. We are not
39// using the STL because we want full control over space and speed of
40// the code. This implementation is based on code by Robert Griesemer
41// and Rob Pike.
42//
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
45// the C free store or the zone; see zone.h.
46
47// Forward defined as
48// template <typename T,
49//           class AllocationPolicy = FreeStoreAllocationPolicy> class List;
50template <typename T, class AllocationPolicy>
51class List {
52 public:
53  explicit List(AllocationPolicy allocator = AllocationPolicy()) {
54    Initialize(0, allocator);
55  }
56  INLINE(explicit List(int capacity,
57                       AllocationPolicy allocator = AllocationPolicy())) {
58    Initialize(capacity, allocator);
59  }
60  INLINE(~List()) { DeleteData(data_); }
61
62  // Deallocates memory used by the list and leaves the list in a consistent
63  // empty state.
64  void Free() {
65    DeleteData(data_);
66    Initialize(0);
67  }
68
69  INLINE(void* operator new(size_t size,
70                            AllocationPolicy allocator = AllocationPolicy())) {
71    return allocator.New(static_cast<int>(size));
72  }
73  INLINE(void operator delete(void* p)) {
74    AllocationPolicy::Delete(p);
75  }
76
77  // Please the MSVC compiler.  We should never have to execute this.
78  INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
79    UNREACHABLE();
80  }
81
82  // Returns a reference to the element at index i.  This reference is
83  // not safe to use after operations that can change the list's
84  // backing store (e.g. Add).
85  inline T& operator[](int i) const {
86    ASSERT(0 <= i);
87    SLOW_ASSERT(i < length_);
88    return data_[i];
89  }
90  inline T& at(int i) const { return operator[](i); }
91  inline T& last() const { return at(length_ - 1); }
92  inline T& first() const { return at(0); }
93
94  INLINE(bool is_empty() const) { return length_ == 0; }
95  INLINE(int length() const) { return length_; }
96  INLINE(int capacity() const) { return capacity_; }
97
98  Vector<T> ToVector() const { return Vector<T>(data_, length_); }
99
100  Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
101
102  // Adds a copy of the given 'element' to the end of the list,
103  // expanding the list if necessary.
104  void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
105
106  // Add all the elements from the argument list to this list.
107  void AddAll(const List<T, AllocationPolicy>& other,
108              AllocationPolicy allocator = AllocationPolicy());
109
110  // Add all the elements from the vector to this list.
111  void AddAll(const Vector<T>& other,
112              AllocationPolicy allocator = AllocationPolicy());
113
114  // Inserts the element at the specific index.
115  void InsertAt(int index, const T& element,
116                AllocationPolicy allocator = AllocationPolicy());
117
118  // Overwrites the element at the specific index.
119  void Set(int index, const T& element);
120
121  // Added 'count' elements with the value 'value' and returns a
122  // vector that allows access to the elements.  The vector is valid
123  // until the next change is made to this list.
124  Vector<T> AddBlock(T value, int count,
125                     AllocationPolicy allocator = AllocationPolicy());
126
127  // Removes the i'th element without deleting it even if T is a
128  // pointer type; moves all elements above i "down". Returns the
129  // removed element.  This function's complexity is linear in the
130  // size of the list.
131  T Remove(int i);
132
133  // Remove the given element from the list. Returns whether or not
134  // the input is included in the list in the first place.
135  bool RemoveElement(const T& elm);
136
137  // Removes the last element without deleting it even if T is a
138  // pointer type. Returns the removed element.
139  INLINE(T RemoveLast()) { return Remove(length_ - 1); }
140
141  // Deletes current list contents and allocates space for 'length' elements.
142  INLINE(void Allocate(int length,
143                       AllocationPolicy allocator = AllocationPolicy()));
144
145  // Clears the list by setting the length to zero. Even if T is a
146  // pointer type, clearing the list doesn't delete the entries.
147  INLINE(void Clear());
148
149  // Drops all but the first 'pos' elements from the list.
150  INLINE(void Rewind(int pos));
151
152  // Drop the last 'count' elements from the list.
153  INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
154
155  // Halve the capacity if fill level is less than a quarter.
156  INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
157
158  bool Contains(const T& elm) const;
159  int CountOccurrences(const T& elm, int start, int end) const;
160
161  // Iterate through all list entries, starting at index 0.
162  void Iterate(void (*callback)(T* x));
163  template<class Visitor>
164  void Iterate(Visitor* visitor);
165
166  // Sort all list entries (using QuickSort)
167  void Sort(int (*cmp)(const T* x, const T* y));
168  void Sort();
169
170  INLINE(void Initialize(int capacity,
171                         AllocationPolicy allocator = AllocationPolicy()));
172
173 private:
174  T* data_;
175  int capacity_;
176  int length_;
177
178  INLINE(T* NewData(int n, AllocationPolicy allocator))  {
179    return static_cast<T*>(allocator.New(n * sizeof(T)));
180  }
181  INLINE(void DeleteData(T* data))  {
182    AllocationPolicy::Delete(data);
183  }
184
185  // Increase the capacity of a full list, and add an element.
186  // List must be full already.
187  void ResizeAdd(const T& element, AllocationPolicy allocator);
188
189  // Inlined implementation of ResizeAdd, shared by inlined and
190  // non-inlined versions of ResizeAdd.
191  void ResizeAddInternal(const T& element, AllocationPolicy allocator);
192
193  // Resize the list.
194  void Resize(int new_capacity, AllocationPolicy allocator);
195
196  DISALLOW_COPY_AND_ASSIGN(List);
197};
198
199
200template<typename T, class P>
201size_t GetMemoryUsedByList(const List<T, P>& list) {
202  return list.length() * sizeof(T) + sizeof(list);
203}
204
205
206class Map;
207class Type;
208class Code;
209template<typename T> class Handle;
210typedef List<Map*> MapList;
211typedef List<Code*> CodeList;
212typedef List<Handle<Map> > MapHandleList;
213typedef List<Handle<Type> > TypeHandleList;
214typedef List<Handle<Code> > CodeHandleList;
215
216// Perform binary search for an element in an already sorted
217// list. Returns the index of the element of -1 if it was not found.
218// |cmp| is a predicate that takes a pointer to an element of the List
219// and returns +1 if it is greater, -1 if it is less than the element
220// being searched.
221template <typename T, class P>
222int SortedListBSearch(const List<T>& list, P cmp);
223template <typename T>
224int SortedListBSearch(const List<T>& list, T elem);
225
226
227} }  // namespace v8::internal
228
229
230#endif  // V8_LIST_H_
231