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 30 matching lines...) Expand all Loading... |
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, | 48 // template <typename T, |
49 // class AllocationPolicy = FreeStoreAllocationPolicy> class List; | 49 // class AllocationPolicy = FreeStoreAllocationPolicy> class List; |
50 template <typename T, class AllocationPolicy> | 50 template <typename T, class AllocationPolicy> |
51 class List : private AllocationPolicy::Deleter { | 51 class List { |
52 public: | 52 public: |
53 explicit List(AllocationPolicy allocator = AllocationPolicy()) | 53 explicit List(AllocationPolicy allocator = AllocationPolicy()) { |
54 : AllocationPolicy::Deleter(allocator) { | |
55 Initialize(0, allocator); | 54 Initialize(0, allocator); |
56 } | 55 } |
57 INLINE(explicit List(int capacity, | 56 INLINE(explicit List(int capacity, |
58 AllocationPolicy allocator = AllocationPolicy())) | 57 AllocationPolicy allocator = AllocationPolicy())) { |
59 : AllocationPolicy::Deleter(allocator) { | |
60 Initialize(capacity, allocator); | 58 Initialize(capacity, allocator); |
61 } | 59 } |
62 INLINE(~List()) { DeleteData(data_); } | 60 INLINE(~List()) { DeleteData(data_); } |
63 | 61 |
64 // Deallocates memory used by the list and leaves the list in a consistent | 62 // Deallocates memory used by the list and leaves the list in a consistent |
65 // empty state. | 63 // empty state. |
66 void Free() { | 64 void Free() { |
67 DeleteData(data_); | 65 DeleteData(data_); |
68 Initialize(0); | 66 Initialize(0); |
69 } | 67 } |
70 | 68 |
71 INLINE(void* operator new(size_t size, | 69 INLINE(void* operator new(size_t size, |
72 AllocationPolicy allocator = AllocationPolicy())) { | 70 AllocationPolicy allocator = AllocationPolicy())) { |
73 return allocator.New(static_cast<int>(size)); | 71 return allocator.New(static_cast<int>(size)); |
74 } | 72 } |
75 INLINE(void operator delete(void* p)) { | 73 INLINE(void operator delete(void* p)) { |
76 AllocationPolicy::Deleter::Delete(p); | 74 AllocationPolicy::Delete(p); |
77 } | 75 } |
78 | 76 |
79 // Please the MSVC compiler. We should never have to execute this. | 77 // Please the MSVC compiler. We should never have to execute this. |
80 INLINE(void operator delete(void* p, AllocationPolicy allocator)) { | 78 INLINE(void operator delete(void* p, AllocationPolicy allocator)) { |
81 UNREACHABLE(); | 79 UNREACHABLE(); |
82 } | 80 } |
83 | 81 |
84 // Delete via the instance Deleter | |
85 static void Delete(List* p) { | |
86 if (p == NULL) return; | |
87 p->~List(); | |
88 p->AllocationPolicy::Deleter::Delete(p); | |
89 } | |
90 | |
91 // Returns a reference to the element at index i. This reference is | 82 // Returns a reference to the element at index i. This reference is |
92 // not safe to use after operations that can change the list's | 83 // not safe to use after operations that can change the list's |
93 // backing store (e.g. Add). | 84 // backing store (e.g. Add). |
94 inline T& operator[](int i) const { | 85 inline T& operator[](int i) const { |
95 ASSERT(0 <= i); | 86 ASSERT(0 <= i); |
96 ASSERT(i < length_); | 87 ASSERT(i < length_); |
97 return data_[i]; | 88 return data_[i]; |
98 } | 89 } |
99 inline T& at(int i) const { return operator[](i); } | 90 inline T& at(int i) const { return operator[](i); } |
100 inline T& last() const { return at(length_ - 1); } | 91 inline T& last() const { return at(length_ - 1); } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 172 |
182 private: | 173 private: |
183 T* data_; | 174 T* data_; |
184 int capacity_; | 175 int capacity_; |
185 int length_; | 176 int length_; |
186 | 177 |
187 INLINE(T* NewData(int n, AllocationPolicy allocator)) { | 178 INLINE(T* NewData(int n, AllocationPolicy allocator)) { |
188 return static_cast<T*>(allocator.New(n * sizeof(T))); | 179 return static_cast<T*>(allocator.New(n * sizeof(T))); |
189 } | 180 } |
190 INLINE(void DeleteData(T* data)) { | 181 INLINE(void DeleteData(T* data)) { |
191 this->AllocationPolicy::Deleter::Delete(data); | 182 AllocationPolicy::Delete(data); |
192 } | 183 } |
193 | 184 |
194 // Increase the capacity of a full list, and add an element. | 185 // Increase the capacity of a full list, and add an element. |
195 // List must be full already. | 186 // List must be full already. |
196 void ResizeAdd(const T& element, AllocationPolicy allocator); | 187 void ResizeAdd(const T& element, AllocationPolicy allocator); |
197 | 188 |
198 // Inlined implementation of ResizeAdd, shared by inlined and | 189 // Inlined implementation of ResizeAdd, shared by inlined and |
199 // non-inlined versions of ResizeAdd. | 190 // non-inlined versions of ResizeAdd. |
200 void ResizeAddInternal(const T& element, AllocationPolicy allocator); | 191 void ResizeAddInternal(const T& element, AllocationPolicy allocator); |
201 | 192 |
(...skipping 19 matching lines...) Expand all Loading... |
221 template <typename T, class P> | 212 template <typename T, class P> |
222 int SortedListBSearch(const List<T>& list, P cmp); | 213 int SortedListBSearch(const List<T>& list, P cmp); |
223 template <typename T> | 214 template <typename T> |
224 int SortedListBSearch(const List<T>& list, T elem); | 215 int SortedListBSearch(const List<T>& list, T elem); |
225 | 216 |
226 | 217 |
227 } } // namespace v8::internal | 218 } } // namespace v8::internal |
228 | 219 |
229 | 220 |
230 #endif // V8_LIST_H_ | 221 #endif // V8_LIST_H_ |
OLD | NEW |