2011-05-03 08:23:58 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_LIST_H_
|
|
|
|
#define V8_LIST_H_
|
|
|
|
|
2011-05-03 08:23:58 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// The list is a template for very light-weight lists. We are not
|
|
|
|
// using the STL because we want full control over space and speed of
|
|
|
|
// the code. This implementation is based on code by Robert Griesemer
|
|
|
|
// and Rob Pike.
|
|
|
|
//
|
|
|
|
// The list is parameterized by the type of its elements (T) and by an
|
|
|
|
// allocation policy (P). The policy is used for allocating lists in
|
|
|
|
// the C free store or the zone; see zone.h.
|
|
|
|
|
|
|
|
// Forward defined as
|
2012-06-04 14:42:58 +00:00
|
|
|
// template <typename T,
|
|
|
|
// class AllocationPolicy = FreeStoreAllocationPolicy> class List;
|
|
|
|
template <typename T, class AllocationPolicy>
|
2013-09-02 11:39:23 +00:00
|
|
|
class List {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2013-09-02 11:39:23 +00:00
|
|
|
explicit List(AllocationPolicy allocator = AllocationPolicy()) {
|
2012-06-04 14:42:58 +00:00
|
|
|
Initialize(0, allocator);
|
|
|
|
}
|
|
|
|
INLINE(explicit List(int capacity,
|
2013-09-02 11:39:23 +00:00
|
|
|
AllocationPolicy allocator = AllocationPolicy())) {
|
2012-06-04 14:42:58 +00:00
|
|
|
Initialize(capacity, allocator);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
INLINE(~List()) { DeleteData(data_); }
|
|
|
|
|
2009-09-28 13:53:03 +00:00
|
|
|
// Deallocates memory used by the list and leaves the list in a consistent
|
|
|
|
// empty state.
|
|
|
|
void Free() {
|
|
|
|
DeleteData(data_);
|
|
|
|
Initialize(0);
|
|
|
|
}
|
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
INLINE(void* operator new(size_t size,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy())) {
|
|
|
|
return allocator.New(static_cast<int>(size));
|
|
|
|
}
|
|
|
|
INLINE(void operator delete(void* p)) {
|
2013-09-02 11:39:23 +00:00
|
|
|
AllocationPolicy::Delete(p);
|
2009-11-11 09:50:06 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-06-18 12:48:21 +00:00
|
|
|
// Please the MSVC compiler. We should never have to execute this.
|
|
|
|
INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2009-03-05 08:10:42 +00:00
|
|
|
// Returns a reference to the element at index i. This reference is
|
|
|
|
// not safe to use after operations that can change the list's
|
2012-01-16 12:38:59 +00:00
|
|
|
// backing store (e.g. Add).
|
2010-09-24 08:25:31 +00:00
|
|
|
inline T& operator[](int i) const {
|
2010-01-21 12:10:56 +00:00
|
|
|
ASSERT(0 <= i);
|
2013-10-25 11:10:28 +00:00
|
|
|
SLOW_ASSERT(i < length_);
|
2008-07-03 15:10:15 +00:00
|
|
|
return data_[i];
|
|
|
|
}
|
2010-09-24 08:25:31 +00:00
|
|
|
inline T& at(int i) const { return operator[](i); }
|
2009-09-04 07:34:25 +00:00
|
|
|
inline T& last() const { return at(length_ - 1); }
|
|
|
|
inline T& first() const { return at(0); }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-02-07 11:55:11 +00:00
|
|
|
typedef T* iterator;
|
|
|
|
inline iterator begin() const { return &data_[0]; }
|
|
|
|
inline iterator end() const { return &data_[length_]; }
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
INLINE(bool is_empty() const) { return length_ == 0; }
|
|
|
|
INLINE(int length() const) { return length_; }
|
2009-03-30 13:03:32 +00:00
|
|
|
INLINE(int capacity() const) { return capacity_; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-04-29 12:08:33 +00:00
|
|
|
Vector<T> ToVector() const { return Vector<T>(data_, length_); }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); }
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Adds a copy of the given 'element' to the end of the list,
|
|
|
|
// expanding the list if necessary.
|
2012-06-04 14:42:58 +00:00
|
|
|
void Add(const T& element, AllocationPolicy allocator = AllocationPolicy());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-22 11:52:24 +00:00
|
|
|
// Add all the elements from the argument list to this list.
|
2012-06-04 14:42:58 +00:00
|
|
|
void AddAll(const List<T, AllocationPolicy>& other,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy());
|
2009-05-22 11:52:24 +00:00
|
|
|
|
2011-04-29 12:08:33 +00:00
|
|
|
// Add all the elements from the vector to this list.
|
2012-06-04 14:42:58 +00:00
|
|
|
void AddAll(const Vector<T>& other,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy());
|
2011-04-29 12:08:33 +00:00
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Inserts the element at the specific index.
|
2012-06-04 14:42:58 +00:00
|
|
|
void InsertAt(int index, const T& element,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy());
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2013-05-03 08:48:53 +00:00
|
|
|
// Overwrites the element at the specific index.
|
|
|
|
void Set(int index, const T& element);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Added 'count' elements with the value 'value' and returns a
|
|
|
|
// vector that allows access to the elements. The vector is valid
|
|
|
|
// until the next change is made to this list.
|
2012-06-04 14:42:58 +00:00
|
|
|
Vector<T> AddBlock(T value, int count,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy());
|
2009-03-04 10:34:36 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Removes the i'th element without deleting it even if T is a
|
|
|
|
// pointer type; moves all elements above i "down". Returns the
|
2009-03-04 10:34:36 +00:00
|
|
|
// removed element. This function's complexity is linear in the
|
|
|
|
// size of the list.
|
2008-07-03 15:10:15 +00:00
|
|
|
T Remove(int i);
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Remove the given element from the list. Returns whether or not
|
|
|
|
// the input is included in the list in the first place.
|
|
|
|
bool RemoveElement(const T& elm);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Removes the last element without deleting it even if T is a
|
|
|
|
// pointer type. Returns the removed element.
|
|
|
|
INLINE(T RemoveLast()) { return Remove(length_ - 1); }
|
|
|
|
|
2012-05-09 14:34:13 +00:00
|
|
|
// Deletes current list contents and allocates space for 'length' elements.
|
2012-06-04 14:42:58 +00:00
|
|
|
INLINE(void Allocate(int length,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy()));
|
2012-05-09 14:34:13 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Clears the list by setting the length to zero. Even if T is a
|
|
|
|
// pointer type, clearing the list doesn't delete the entries.
|
|
|
|
INLINE(void Clear());
|
|
|
|
|
|
|
|
// Drops all but the first 'pos' elements from the list.
|
|
|
|
INLINE(void Rewind(int pos));
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Drop the last 'count' elements from the list.
|
|
|
|
INLINE(void RewindBy(int count)) { Rewind(length_ - count); }
|
|
|
|
|
2013-01-14 13:19:27 +00:00
|
|
|
// Halve the capacity if fill level is less than a quarter.
|
|
|
|
INLINE(void Trim(AllocationPolicy allocator = AllocationPolicy()));
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
bool Contains(const T& elm) const;
|
|
|
|
int CountOccurrences(const T& elm, int start, int end) const;
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Iterate through all list entries, starting at index 0.
|
|
|
|
void Iterate(void (*callback)(T* x));
|
2010-08-09 11:37:24 +00:00
|
|
|
template<class Visitor>
|
|
|
|
void Iterate(Visitor* visitor);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Sort all list entries (using QuickSort)
|
|
|
|
void Sort(int (*cmp)(const T* x, const T* y));
|
2008-11-25 11:07:48 +00:00
|
|
|
void Sort();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
INLINE(void Initialize(int capacity,
|
|
|
|
AllocationPolicy allocator = AllocationPolicy()));
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* data_;
|
|
|
|
int capacity_;
|
|
|
|
int length_;
|
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
INLINE(T* NewData(int n, AllocationPolicy allocator)) {
|
|
|
|
return static_cast<T*>(allocator.New(n * sizeof(T)));
|
|
|
|
}
|
|
|
|
INLINE(void DeleteData(T* data)) {
|
2013-09-02 11:39:23 +00:00
|
|
|
AllocationPolicy::Delete(data);
|
2012-06-04 14:42:58 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-01 14:37:28 +00:00
|
|
|
// Increase the capacity of a full list, and add an element.
|
|
|
|
// List must be full already.
|
2012-06-04 14:42:58 +00:00
|
|
|
void ResizeAdd(const T& element, AllocationPolicy allocator);
|
2009-05-01 14:37:28 +00:00
|
|
|
|
|
|
|
// Inlined implementation of ResizeAdd, shared by inlined and
|
|
|
|
// non-inlined versions of ResizeAdd.
|
2012-06-04 14:42:58 +00:00
|
|
|
void ResizeAddInternal(const T& element, AllocationPolicy allocator);
|
2009-05-22 11:52:24 +00:00
|
|
|
|
|
|
|
// Resize the list.
|
2012-06-04 14:42:58 +00:00
|
|
|
void Resize(int new_capacity, AllocationPolicy allocator);
|
2009-05-22 11:52:24 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(List);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2013-12-03 09:48:30 +00:00
|
|
|
|
|
|
|
template<typename T, class P>
|
|
|
|
size_t GetMemoryUsedByList(const List<T, P>& list) {
|
|
|
|
return list.length() * sizeof(T) + sizeof(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-18 13:17:29 +00:00
|
|
|
class Map;
|
2014-01-10 12:19:01 +00:00
|
|
|
template<class> class TypeImpl;
|
|
|
|
struct HeapTypeConfig;
|
2014-01-21 16:22:52 +00:00
|
|
|
typedef TypeImpl<HeapTypeConfig> HeapType;
|
2011-05-18 13:17:29 +00:00
|
|
|
class Code;
|
2011-10-21 11:42:54 +00:00
|
|
|
template<typename T> class Handle;
|
2011-05-18 13:17:29 +00:00
|
|
|
typedef List<Map*> MapList;
|
|
|
|
typedef List<Code*> CodeList;
|
2011-10-21 11:42:54 +00:00
|
|
|
typedef List<Handle<Map> > MapHandleList;
|
2014-01-21 16:22:52 +00:00
|
|
|
typedef List<Handle<HeapType> > TypeHandleList;
|
2011-10-21 11:42:54 +00:00
|
|
|
typedef List<Handle<Code> > CodeHandleList;
|
2011-05-18 13:17:29 +00:00
|
|
|
|
2011-09-13 08:31:21 +00:00
|
|
|
// Perform binary search for an element in an already sorted
|
|
|
|
// list. Returns the index of the element of -1 if it was not found.
|
2012-04-10 11:24:09 +00:00
|
|
|
// |cmp| is a predicate that takes a pointer to an element of the List
|
|
|
|
// and returns +1 if it is greater, -1 if it is less than the element
|
|
|
|
// being searched.
|
|
|
|
template <typename T, class P>
|
|
|
|
int SortedListBSearch(const List<T>& list, P cmp);
|
2011-09-13 08:31:21 +00:00
|
|
|
template <typename T>
|
|
|
|
int SortedListBSearch(const List<T>& list, T elem);
|
|
|
|
|
2011-10-21 11:42:54 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
2011-09-13 08:31:21 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif // V8_LIST_H_
|