| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // The list is a template for very light-weight lists. We are not | 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 | 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 | 40 // the code. This implementation is based on code by Robert Griesemer |
| 41 // and Rob Pike. | 41 // and Rob Pike. |
| 42 // | 42 // |
| 43 // The list is parameterized by the type of its elements (T) and by an | 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 | 44 // allocation policy (P). The policy is used for allocating lists in |
| 45 // the C free store or the zone; see zone.h. | 45 // the C free store or the zone; see zone.h. |
| 46 | 46 |
| 47 // Forward defined as | 47 // Forward defined as |
| 48 // template <typename T, class P = FreeStoreAllocationPolicy> class List; | 48 // template <typename T, class Allocator = FreeStoreAllocator> class List; |
| 49 template <typename T, class P> | 49 // |
| 50 class List { | 50 // Implementation note: List privately inherits from Allocator to |
| 51 // allow empty base class optimization. |
| 52 template <typename T, class Allocator> |
| 53 class List : private Allocator { |
| 51 public: | 54 public: |
| 52 | 55 explicit List(const Allocator& allocator = Allocator()) |
| 53 List() { Initialize(0); } | 56 : Allocator(allocator) { Initialize(0); } |
| 54 INLINE(explicit List(int capacity)) { Initialize(capacity); } | 57 INLINE(explicit List(int capacity, const Allocator& allocator = Allocator())) |
| 58 : Allocator(allocator) { |
| 59 Initialize(capacity); |
| 60 } |
| 55 INLINE(~List()) { DeleteData(data_); } | 61 INLINE(~List()) { DeleteData(data_); } |
| 56 | 62 |
| 57 // Deallocates memory used by the list and leaves the list in a consistent | 63 // Deallocates memory used by the list and leaves the list in a consistent |
| 58 // empty state. | 64 // empty state. |
| 59 void Free() { | 65 void Free() { |
| 60 DeleteData(data_); | 66 DeleteData(data_); |
| 61 Initialize(0); | 67 Initialize(0); |
| 62 } | 68 } |
| 63 | 69 |
| 64 INLINE(void* operator new(size_t size)) { | 70 INLINE(void* operator new(size_t size)) { |
| 65 return P::New(static_cast<int>(size)); | 71 return Allocator().New(static_cast<int>(size)); |
| 66 } | 72 } |
| 67 INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); } | 73 INLINE(void operator delete(void* p, size_t)) { |
| 74 return Allocator().Delete(p); |
| 75 } |
| 68 | 76 |
| 69 // Returns a reference to the element at index i. This reference is | 77 // Returns a reference to the element at index i. This reference is |
| 70 // not safe to use after operations that can change the list's | 78 // not safe to use after operations that can change the list's |
| 71 // backing store (eg, Add). | 79 // backing store (eg, Add). |
| 72 inline T& operator[](int i) const { | 80 inline T& operator[](int i) const { |
| 73 ASSERT(0 <= i); | 81 ASSERT(0 <= i); |
| 74 ASSERT(i < length_); | 82 ASSERT(i < length_); |
| 75 return data_[i]; | 83 return data_[i]; |
| 76 } | 84 } |
| 77 inline T& at(int i) const { return operator[](i); } | 85 inline T& at(int i) const { return operator[](i); } |
| 78 inline T& last() const { return at(length_ - 1); } | 86 inline T& last() const { return at(length_ - 1); } |
| 79 inline T& first() const { return at(0); } | 87 inline T& first() const { return at(0); } |
| 80 | 88 |
| 81 INLINE(bool is_empty() const) { return length_ == 0; } | 89 INLINE(bool is_empty() const) { return length_ == 0; } |
| 82 INLINE(int length() const) { return length_; } | 90 INLINE(int length() const) { return length_; } |
| 83 INLINE(int capacity() const) { return capacity_; } | 91 INLINE(int capacity() const) { return capacity_; } |
| 84 | 92 |
| 85 Vector<T> ToVector() const { return Vector<T>(data_, length_); } | 93 Vector<T> ToVector() const { return Vector<T>(data_, length_); } |
| 86 | 94 |
| 87 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); } | 95 Vector<const T> ToConstVector() { return Vector<const T>(data_, length_); } |
| 88 | 96 |
| 89 // Adds a copy of the given 'element' to the end of the list, | 97 // Adds a copy of the given 'element' to the end of the list, |
| 90 // expanding the list if necessary. | 98 // expanding the list if necessary. |
| 91 void Add(const T& element); | 99 void Add(const T& element); |
| 92 | 100 |
| 93 // Add all the elements from the argument list to this list. | 101 // Add all the elements from the argument list to this list. |
| 94 void AddAll(const List<T, P>& other); | 102 void AddAll(const List<T, Allocator>& other); |
| 95 | 103 |
| 96 // Add all the elements from the vector to this list. | 104 // Add all the elements from the vector to this list. |
| 97 void AddAll(const Vector<T>& other); | 105 void AddAll(const Vector<T>& other); |
| 98 | 106 |
| 99 // Inserts the element at the specific index. | 107 // Inserts the element at the specific index. |
| 100 void InsertAt(int index, const T& element); | 108 void InsertAt(int index, const T& element); |
| 101 | 109 |
| 102 // Added 'count' elements with the value 'value' and returns a | 110 // Added 'count' elements with the value 'value' and returns a |
| 103 // vector that allows access to the elements. The vector is valid | 111 // vector that allows access to the elements. The vector is valid |
| 104 // until the next change is made to this list. | 112 // until the next change is made to this list. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 void Sort(int (*cmp)(const T* x, const T* y)); | 148 void Sort(int (*cmp)(const T* x, const T* y)); |
| 141 void Sort(); | 149 void Sort(); |
| 142 | 150 |
| 143 INLINE(void Initialize(int capacity)); | 151 INLINE(void Initialize(int capacity)); |
| 144 | 152 |
| 145 private: | 153 private: |
| 146 T* data_; | 154 T* data_; |
| 147 int capacity_; | 155 int capacity_; |
| 148 int length_; | 156 int length_; |
| 149 | 157 |
| 150 INLINE(T* NewData(int n)) { return static_cast<T*>(P::New(n * sizeof(T))); } | 158 INLINE(T* NewData(int n)) { |
| 151 INLINE(void DeleteData(T* data)) { P::Delete(data); } | 159 return static_cast<T*>(this->Allocator::New(n * sizeof(T))); |
| 160 } |
| 161 INLINE(void DeleteData(T* data)) { this->Allocator::Delete(data); } |
| 152 | 162 |
| 153 // Increase the capacity of a full list, and add an element. | 163 // Increase the capacity of a full list, and add an element. |
| 154 // List must be full already. | 164 // List must be full already. |
| 155 void ResizeAdd(const T& element); | 165 void ResizeAdd(const T& element); |
| 156 | 166 |
| 157 // Inlined implementation of ResizeAdd, shared by inlined and | 167 // Inlined implementation of ResizeAdd, shared by inlined and |
| 158 // non-inlined versions of ResizeAdd. | 168 // non-inlined versions of ResizeAdd. |
| 159 void ResizeAddInternal(const T& element); | 169 void ResizeAddInternal(const T& element); |
| 160 | 170 |
| 161 // Resize the list. | 171 // Resize the list. |
| 162 void Resize(int new_capacity); | 172 void Resize(int new_capacity); |
| 163 | 173 |
| 164 DISALLOW_COPY_AND_ASSIGN(List); | 174 DISALLOW_COPY_AND_ASSIGN(List); |
| 165 }; | 175 }; |
| 166 | 176 |
| 167 class Map; | 177 class Map; |
| 168 class Code; | 178 class Code; |
| 169 typedef List<Map*> MapList; | 179 typedef List<Map*> MapList; |
| 170 typedef List<Code*> CodeList; | 180 typedef List<Code*> CodeList; |
| 171 | 181 |
| 172 } } // namespace v8::internal | 182 } } // namespace v8::internal |
| 173 | 183 |
| 174 #endif // V8_LIST_H_ | 184 #endif // V8_LIST_H_ |
| OLD | NEW |