diff options
Diffstat (limited to 'deps/v8/src/list.h')
-rw-r--r-- | deps/v8/src/list.h | 66 |
1 files changed, 45 insertions, 21 deletions
diff --git a/deps/v8/src/list.h b/deps/v8/src/list.h index adddea41f0..3ca4a3fba8 100644 --- a/deps/v8/src/list.h +++ b/deps/v8/src/list.h @@ -45,12 +45,18 @@ namespace internal { // the C free store or the zone; see zone.h. // Forward defined as -// template <typename T, class P = FreeStoreAllocationPolicy> class List; -template <typename T, class P> +// template <typename T, +// class AllocationPolicy = FreeStoreAllocationPolicy> class List; +template <typename T, class AllocationPolicy> class List { public: - List() { Initialize(0); } - INLINE(explicit List(int capacity)) { Initialize(capacity); } + explicit List(AllocationPolicy allocator = AllocationPolicy()) { + Initialize(0, allocator); + } + INLINE(explicit List(int capacity, + AllocationPolicy allocator = AllocationPolicy())) { + Initialize(capacity, allocator); + } INLINE(~List()) { DeleteData(data_); } // Deallocates memory used by the list and leaves the list in a consistent @@ -60,10 +66,13 @@ class List { Initialize(0); } - INLINE(void* operator new(size_t size)) { - return P::New(static_cast<int>(size)); + INLINE(void* operator new(size_t size, + AllocationPolicy allocator = AllocationPolicy())) { + return allocator.New(static_cast<int>(size)); + } + INLINE(void operator delete(void* p)) { + AllocationPolicy::Delete(p); } - INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } // Returns a reference to the element at index i. This reference is // not safe to use after operations that can change the list's @@ -87,21 +96,25 @@ class List { // Adds a copy of the given 'element' to the end of the list, // expanding the list if necessary. - void Add(const T& element); + void Add(const T& element, AllocationPolicy allocator = AllocationPolicy()); // Add all the elements from the argument list to this list. - void AddAll(const List<T, P>& other); + void AddAll(const List<T, AllocationPolicy>& other, + AllocationPolicy allocator = AllocationPolicy()); // Add all the elements from the vector to this list. - void AddAll(const Vector<T>& other); + void AddAll(const Vector<T>& other, + AllocationPolicy allocator = AllocationPolicy()); // Inserts the element at the specific index. - void InsertAt(int index, const T& element); + void InsertAt(int index, const T& element, + AllocationPolicy allocator = AllocationPolicy()); // 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. - Vector<T> AddBlock(T value, int count); + Vector<T> AddBlock(T value, int count, + AllocationPolicy allocator = AllocationPolicy()); // Removes the i'th element without deleting it even if T is a // pointer type; moves all elements above i "down". Returns the @@ -117,6 +130,10 @@ class List { // pointer type. Returns the removed element. INLINE(T RemoveLast()) { return Remove(length_ - 1); } + // Deletes current list contents and allocates space for 'length' elements. + INLINE(void Allocate(int length, + AllocationPolicy allocator = AllocationPolicy())); + // 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()); @@ -139,26 +156,31 @@ class List { void Sort(int (*cmp)(const T* x, const T* y)); void Sort(); - INLINE(void Initialize(int capacity)); + INLINE(void Initialize(int capacity, + AllocationPolicy allocator = AllocationPolicy())); private: T* data_; int capacity_; int length_; - INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } - INLINE(void DeleteData(T* data)) { P::Delete(data); } + INLINE(T* NewData(int n, AllocationPolicy allocator)) { + return static_cast<T*>(allocator.New(n * sizeof(T))); + } + INLINE(void DeleteData(T* data)) { + AllocationPolicy::Delete(data); + } // Increase the capacity of a full list, and add an element. // List must be full already. - void ResizeAdd(const T& element); + void ResizeAdd(const T& element, AllocationPolicy allocator); // Inlined implementation of ResizeAdd, shared by inlined and // non-inlined versions of ResizeAdd. - void ResizeAddInternal(const T& element); + void ResizeAddInternal(const T& element, AllocationPolicy allocator); // Resize the list. - void Resize(int new_capacity); + void Resize(int new_capacity, AllocationPolicy allocator); DISALLOW_COPY_AND_ASSIGN(List); }; @@ -173,9 +195,11 @@ typedef List<Handle<Code> > CodeHandleList; // Perform binary search for an element in an already sorted // list. Returns the index of the element of -1 if it was not found. -template <typename T> -int SortedListBSearch( - const List<T>& list, T elem, int (*cmp)(const T* x, const T* y)); +// |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); template <typename T> int SortedListBSearch(const List<T>& list, T elem); |