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