• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   }
INLINE(~List ())39   INLINE(~List()) { DeleteData(data_); }
40 
41   // Deallocates memory used by the list and leaves the list in a consistent
42   // empty state.
Free()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   }
INLINE(void operator delete (void * p))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.
INLINE(void operator delete (void * p,AllocationPolicy allocator))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);
66     SLOW_DCHECK(i < length_);
67     return data_[i];
68   }
at(int i)69   inline T& at(int i) const { return operator[](i); }
last()70   inline T& last() const { return at(length_ - 1); }
first()71   inline T& first() const { return at(0); }
72 
73   typedef T* iterator;
begin()74   inline iterator begin() const { return &data_[0]; }
end()75   inline iterator end() const { return &data_[length_]; }
76 
INLINE(bool is_empty ()const)77   INLINE(bool is_empty() const) { return length_ == 0; }
INLINE(int length ()const)78   INLINE(int length() const) { return length_; }
INLINE(int capacity ()const)79   INLINE(int capacity() const) { return capacity_; }
80 
ToVector()81   Vector<T> ToVector() const { return Vector<T>(data_, length_); }
82 
ToConstVector()83   Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
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.
INLINE(T RemoveLast ())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.
INLINE(void RewindBy (int count))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:
157   T* data_;
158   int capacity_;
159   int length_;
160 
INLINE(T * NewData (int n,AllocationPolicy allocator))161   INLINE(T* NewData(int n, AllocationPolicy allocator))  {
162     return static_cast<T*>(allocator.New(n * sizeof(T)));
163   }
INLINE(void DeleteData (T * data))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 
179   DISALLOW_COPY_AND_ASSIGN(List);
180 };
181 
182 
183 template<typename T, class P>
GetMemoryUsedByList(const List<T,P> & list)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;
192 typedef TypeImpl<HeapTypeConfig> HeapType;
193 class Code;
194 template<typename T> class Handle;
195 typedef List<Map*> MapList;
196 typedef List<Code*> CodeList;
197 typedef List<Handle<Map> > MapHandleList;
198 typedef List<Handle<HeapType> > TypeHandleList;
199 typedef List<Handle<Code> > CodeHandleList;
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_
216