V8 Project
list.h
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_LIST_H_
6 #define V8_LIST_H_
7 
8 #include "src/checks.h"
9 #include "src/utils.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 template<typename T> class Vector;
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
27 // template <typename T,
28 // class AllocationPolicy = FreeStoreAllocationPolicy> class List;
29 template <typename T, class AllocationPolicy>
30 class List {
31  public:
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  }
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 
48  INLINE(void* operator new(size_t size,
49  AllocationPolicy allocator = AllocationPolicy())) {
50  return allocator.New(static_cast<int>(size));
51  }
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  }
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
63  // backing store (e.g. Add).
64  inline T& operator[](int i) const {
65  DCHECK(0 <= i);
67  return data_[i];
68  }
69  inline T& at(int i) const { return operator[](i); }
70  inline T& last() const { return at(length_ - 1); }
71  inline T& first() const { return at(0); }
72 
73  typedef T* iterator;
74  inline iterator begin() const { return &data_[0]; }
75  inline iterator end() const { return &data_[length_]; }
76 
77  INLINE(bool is_empty() const) { return length_ == 0; }
78  INLINE(int length() const) { return length_; }
79  INLINE(int capacity() const) { return capacity_; }
80 
81  Vector<T> ToVector() const { return Vector<T>(data_, length_); }
82 
84 
85  // Adds a copy of the given 'element' to the end of the list,
86  // expanding the list if necessary.
87  void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
88 
89  // Add all the elements from the argument list to this list.
90  void AddAll(const List<T, AllocationPolicy>& other,
91  AllocationPolicy allocator = AllocationPolicy());
92 
93  // Add all the elements from the vector to this list.
94  void AddAll(const Vector<T>& other,
95  AllocationPolicy allocator = AllocationPolicy());
96 
97  // Inserts the element at the specific index.
98  void InsertAt(int index, const T& element,
99  AllocationPolicy allocator = AllocationPolicy());
100 
101  // Overwrites the element at the specific index.
102  void Set(int index, const T& element);
103 
104  // Added 'count' elements with the value 'value' and returns a
105  // vector that allows access to the elements. The vector is valid
106  // until the next change is made to this list.
107  Vector<T> AddBlock(T value, int count,
108  AllocationPolicy allocator = AllocationPolicy());
109 
110  // Removes the i'th element without deleting it even if T is a
111  // pointer type; moves all elements above i "down". Returns the
112  // removed element. This function's complexity is linear in the
113  // size of the list.
114  T Remove(int i);
115 
116  // Remove the given element from the list. Returns whether or not
117  // the input is included in the list in the first place.
118  bool RemoveElement(const T& elm);
119 
120  // Removes the last element without deleting it even if T is a
121  // pointer type. Returns the removed element.
122  INLINE(T RemoveLast()) { return Remove(length_ - 1); }
123 
124  // Deletes current list contents and allocates space for 'length' elements.
125  INLINE(void Allocate(int length,
126  AllocationPolicy allocator = AllocationPolicy()));
127 
128  // Clears the list by setting the length to zero. Even if T is a
129  // pointer type, clearing the list doesn't delete the entries.
130  INLINE(void Clear());
131 
132  // Drops all but the first 'pos' elements from the list.
133  INLINE(void Rewind(int pos));
134 
135  // Drop the last 'count' elements from the list.
136  INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
137 
138  // Halve the capacity if fill level is less than a quarter.
139  INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
140 
141  bool Contains(const T& elm) const;
142  int CountOccurrences(const T& elm, int start, int end) const;
143 
144  // Iterate through all list entries, starting at index 0.
145  void Iterate(void (*callback)(T* x));
146  template<class Visitor>
147  void Iterate(Visitor* visitor);
148 
149  // Sort all list entries (using QuickSort)
150  void Sort(int (*cmp)(const T* x, const T* y));
151  void Sort();
152 
153  INLINE(void Initialize(int capacity,
154  AllocationPolicy allocator = AllocationPolicy()));
155 
156  private:
159  int length_;
160 
161  INLINE(T* NewData(int n, AllocationPolicy allocator)) {
162  return static_cast<T*>(allocator.New(n * sizeof(T)));
163  }
164  INLINE(void DeleteData(T* data)) {
165  AllocationPolicy::Delete(data);
166  }
167 
168  // Increase the capacity of a full list, and add an element.
169  // List must be full already.
170  void ResizeAdd(const T& element, AllocationPolicy allocator);
171 
172  // Inlined implementation of ResizeAdd, shared by inlined and
173  // non-inlined versions of ResizeAdd.
174  void ResizeAddInternal(const T& element, AllocationPolicy allocator);
175 
176  // Resize the list.
177  void Resize(int new_capacity, AllocationPolicy allocator);
178 
180 };
181 
182 
183 template<typename T, class P>
184 size_t GetMemoryUsedByList(const List<T, P>& list) {
185  return list.length() * sizeof(T) + sizeof(list);
186 }
187 
188 
189 class Map;
190 template<class> class TypeImpl;
191 struct HeapTypeConfig;
193 class Code;
194 template<typename T> class Handle;
200 
201 // Perform binary search for an element in an already sorted
202 // list. Returns the index of the element of -1 if it was not found.
203 // |cmp| is a predicate that takes a pointer to an element of the List
204 // and returns +1 if it is greater, -1 if it is less than the element
205 // being searched.
206 template <typename T, class P>
207 int SortedListBSearch(const List<T>& list, P cmp);
208 template <typename T>
209 int SortedListBSearch(const List<T>& list, T elem);
210 
211 
212 } } // namespace v8::internal
213 
214 
215 #endif // V8_LIST_H_
#define SLOW_DCHECK(condition)
Definition: checks.h:30
T & operator[](int i) const
Definition: list.h:64
void AddAll(const Vector< T > &other, AllocationPolicy allocator=AllocationPolicy())
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:17
INLINE(int length() const)
Definition: list.h:78
INLINE(void operator delete(void *p, AllocationPolicy allocator))
Definition: list.h:57
DISALLOW_COPY_AND_ASSIGN(List)
INLINE(void DeleteData(T *data))
Definition: list.h:164
void Set(int index, const T &element)
Definition: list-inl.h:85
bool RemoveElement(const T &elm)
Definition: list-inl.h:115
T & at(int i) const
Definition: list.h:69
INLINE(bool is_empty() const)
Definition: list.h:77
INLINE(void RewindBy(int count))
Definition: list.h:136
INLINE(explicit List(int capacity, AllocationPolicy allocator=AllocationPolicy()))
Definition: list.h:35
INLINE(int capacity() const)
Definition: list.h:79
iterator end() const
Definition: list.h:75
Vector< const T > ToConstVector()
Definition: list.h:83
void AddAll(const List< T, AllocationPolicy > &other, AllocationPolicy allocator=AllocationPolicy())
T Remove(int i)
Definition: list-inl.h:103
void Iterate(void(*callback)(T *x))
Definition: list-inl.h:161
T & first() const
Definition: list.h:71
INLINE(void Clear())
INLINE(void Rewind(int pos))
INLINE(T RemoveLast())
Definition: list.h:122
int CountOccurrences(const T &elm, int start, int end) const
Definition: list-inl.h:184
void InsertAt(int index, const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:92
void Resize(int new_capacity, AllocationPolicy allocator)
Definition: list-inl.h:66
INLINE(void Allocate(int length, AllocationPolicy allocator=AllocationPolicy()))
void ResizeAdd(const T &element, AllocationPolicy allocator)
Definition: list-inl.h:46
bool Contains(const T &elm) const
Definition: list-inl.h:174
Vector< T > AddBlock(T value, int count, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:77
INLINE(~List())
Definition: list.h:39
INLINE(T *NewData(int n, AllocationPolicy allocator))
Definition: list.h:161
INLINE(void *operator new(size_t size, AllocationPolicy allocator=AllocationPolicy()))
Definition: list.h:48
INLINE(void Trim(AllocationPolicy allocator=AllocationPolicy()))
void ResizeAddInternal(const T &element, AllocationPolicy allocator)
Definition: list-inl.h:52
List(AllocationPolicy allocator=AllocationPolicy())
Definition: list.h:32
Vector< T > ToVector() const
Definition: list.h:81
void Free()
Definition: list.h:43
INLINE(void operator delete(void *p))
Definition: list.h:52
T & last() const
Definition: list.h:70
INLINE(void Initialize(int capacity, AllocationPolicy allocator=AllocationPolicy()))
iterator begin() const
Definition: list.h:74
enable harmony numeric enable harmony object literal extensions Optimize object size
#define UNREACHABLE()
Definition: logging.h:30
#define DCHECK(condition)
Definition: logging.h:205
List< Code * > CodeList
Definition: list.h:196
size_t GetMemoryUsedByList(const List< T, P > &list)
Definition: list.h:184
List< Handle< Code > > CodeHandleList
Definition: list.h:199
TypeImpl< HeapTypeConfig > HeapType
Definition: list.h:191
int SortedListBSearch(const List< T > &list, P cmp)
Definition: list-inl.h:219
List< Map * > MapList
Definition: list.h:194
List< Handle< Map > > MapHandleList
Definition: list.h:197
List< Handle< HeapType > > TypeHandleList
Definition: list.h:198
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
#define T(name, string, precedence)
Definition: token.cc:25