OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_SMALL_POINTER_LIST_H_ | 5 #ifndef V8_SMALL_POINTER_LIST_H_ |
6 #define V8_SMALL_POINTER_LIST_H_ | 6 #define V8_SMALL_POINTER_LIST_H_ |
7 | 7 |
8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
9 #include "src/globals.h" | 9 #include "src/globals.h" |
10 #include "src/zone.h" | 10 #include "src/zone.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 if (list()->capacity() >= capacity) return; | 31 if (list()->capacity() >= capacity) return; |
32 int old_length = list()->length(); | 32 int old_length = list()->length(); |
33 list()->AddBlock(NULL, capacity - list()->capacity(), zone); | 33 list()->AddBlock(NULL, capacity - list()->capacity(), zone); |
34 list()->Rewind(old_length); | 34 list()->Rewind(old_length); |
35 return; | 35 return; |
36 } | 36 } |
37 PointerList* list = new(zone) PointerList(capacity, zone); | 37 PointerList* list = new(zone) PointerList(capacity, zone); |
38 if ((data_ & kTagMask) == kSingletonTag) { | 38 if ((data_ & kTagMask) == kSingletonTag) { |
39 list->Add(single_value(), zone); | 39 list->Add(single_value(), zone); |
40 } | 40 } |
41 ASSERT(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment)); | 41 DCHECK(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment)); |
42 data_ = reinterpret_cast<intptr_t>(list) | kListTag; | 42 data_ = reinterpret_cast<intptr_t>(list) | kListTag; |
43 } | 43 } |
44 | 44 |
45 void Clear() { | 45 void Clear() { |
46 data_ = kEmptyTag; | 46 data_ = kEmptyTag; |
47 } | 47 } |
48 | 48 |
49 void Sort() { | 49 void Sort() { |
50 if ((data_ & kTagMask) == kListTag) { | 50 if ((data_ & kTagMask) == kListTag) { |
51 list()->Sort(compare_value); | 51 list()->Sort(compare_value); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 bool is_empty() const { return length() == 0; } | 55 bool is_empty() const { return length() == 0; } |
56 | 56 |
57 int length() const { | 57 int length() const { |
58 if ((data_ & kTagMask) == kEmptyTag) return 0; | 58 if ((data_ & kTagMask) == kEmptyTag) return 0; |
59 if ((data_ & kTagMask) == kSingletonTag) return 1; | 59 if ((data_ & kTagMask) == kSingletonTag) return 1; |
60 return list()->length(); | 60 return list()->length(); |
61 } | 61 } |
62 | 62 |
63 void Add(T* pointer, Zone* zone) { | 63 void Add(T* pointer, Zone* zone) { |
64 ASSERT(IsAligned(reinterpret_cast<intptr_t>(pointer), kPointerAlignment)); | 64 DCHECK(IsAligned(reinterpret_cast<intptr_t>(pointer), kPointerAlignment)); |
65 if ((data_ & kTagMask) == kEmptyTag) { | 65 if ((data_ & kTagMask) == kEmptyTag) { |
66 data_ = reinterpret_cast<intptr_t>(pointer) | kSingletonTag; | 66 data_ = reinterpret_cast<intptr_t>(pointer) | kSingletonTag; |
67 return; | 67 return; |
68 } | 68 } |
69 if ((data_ & kTagMask) == kSingletonTag) { | 69 if ((data_ & kTagMask) == kSingletonTag) { |
70 PointerList* list = new(zone) PointerList(2, zone); | 70 PointerList* list = new(zone) PointerList(2, zone); |
71 list->Add(single_value(), zone); | 71 list->Add(single_value(), zone); |
72 list->Add(pointer, zone); | 72 list->Add(pointer, zone); |
73 ASSERT(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment)); | 73 DCHECK(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment)); |
74 data_ = reinterpret_cast<intptr_t>(list) | kListTag; | 74 data_ = reinterpret_cast<intptr_t>(list) | kListTag; |
75 return; | 75 return; |
76 } | 76 } |
77 list()->Add(pointer, zone); | 77 list()->Add(pointer, zone); |
78 } | 78 } |
79 | 79 |
80 // Note: returns T* and not T*& (unlike List from list.h). | 80 // Note: returns T* and not T*& (unlike List from list.h). |
81 // This makes the implementation simpler and more const correct. | 81 // This makes the implementation simpler and more const correct. |
82 T* at(int i) const { | 82 T* at(int i) const { |
83 ASSERT((data_ & kTagMask) != kEmptyTag); | 83 DCHECK((data_ & kTagMask) != kEmptyTag); |
84 if ((data_ & kTagMask) == kSingletonTag) { | 84 if ((data_ & kTagMask) == kSingletonTag) { |
85 ASSERT(i == 0); | 85 DCHECK(i == 0); |
86 return single_value(); | 86 return single_value(); |
87 } | 87 } |
88 return list()->at(i); | 88 return list()->at(i); |
89 } | 89 } |
90 | 90 |
91 // See the note above. | 91 // See the note above. |
92 T* operator[](int i) const { return at(i); } | 92 T* operator[](int i) const { return at(i); } |
93 | 93 |
94 // Remove the given element from the list (if present). | 94 // Remove the given element from the list (if present). |
95 void RemoveElement(T* pointer) { | 95 void RemoveElement(T* pointer) { |
96 if ((data_ & kTagMask) == kEmptyTag) return; | 96 if ((data_ & kTagMask) == kEmptyTag) return; |
97 if ((data_ & kTagMask) == kSingletonTag) { | 97 if ((data_ & kTagMask) == kSingletonTag) { |
98 if (pointer == single_value()) { | 98 if (pointer == single_value()) { |
99 data_ = kEmptyTag; | 99 data_ = kEmptyTag; |
100 } | 100 } |
101 return; | 101 return; |
102 } | 102 } |
103 list()->RemoveElement(pointer); | 103 list()->RemoveElement(pointer); |
104 } | 104 } |
105 | 105 |
106 T* RemoveLast() { | 106 T* RemoveLast() { |
107 ASSERT((data_ & kTagMask) != kEmptyTag); | 107 DCHECK((data_ & kTagMask) != kEmptyTag); |
108 if ((data_ & kTagMask) == kSingletonTag) { | 108 if ((data_ & kTagMask) == kSingletonTag) { |
109 T* result = single_value(); | 109 T* result = single_value(); |
110 data_ = kEmptyTag; | 110 data_ = kEmptyTag; |
111 return result; | 111 return result; |
112 } | 112 } |
113 return list()->RemoveLast(); | 113 return list()->RemoveLast(); |
114 } | 114 } |
115 | 115 |
116 void Rewind(int pos) { | 116 void Rewind(int pos) { |
117 if ((data_ & kTagMask) == kEmptyTag) { | 117 if ((data_ & kTagMask) == kEmptyTag) { |
118 ASSERT(pos == 0); | 118 DCHECK(pos == 0); |
119 return; | 119 return; |
120 } | 120 } |
121 if ((data_ & kTagMask) == kSingletonTag) { | 121 if ((data_ & kTagMask) == kSingletonTag) { |
122 ASSERT(pos == 0 || pos == 1); | 122 DCHECK(pos == 0 || pos == 1); |
123 if (pos == 0) { | 123 if (pos == 0) { |
124 data_ = kEmptyTag; | 124 data_ = kEmptyTag; |
125 } | 125 } |
126 return; | 126 return; |
127 } | 127 } |
128 list()->Rewind(pos); | 128 list()->Rewind(pos); |
129 } | 129 } |
130 | 130 |
131 int CountOccurrences(T* pointer, int start, int end) const { | 131 int CountOccurrences(T* pointer, int start, int end) const { |
132 if ((data_ & kTagMask) == kEmptyTag) return 0; | 132 if ((data_ & kTagMask) == kEmptyTag) return 0; |
(...skipping 15 matching lines...) Expand all Loading... |
148 | 148 |
149 static const intptr_t kEmptyTag = 1; | 149 static const intptr_t kEmptyTag = 1; |
150 static const intptr_t kSingletonTag = 0; | 150 static const intptr_t kSingletonTag = 0; |
151 static const intptr_t kListTag = 2; | 151 static const intptr_t kListTag = 2; |
152 static const intptr_t kTagMask = 3; | 152 static const intptr_t kTagMask = 3; |
153 static const intptr_t kValueMask = ~kTagMask; | 153 static const intptr_t kValueMask = ~kTagMask; |
154 | 154 |
155 STATIC_ASSERT(kTagMask + 1 <= kPointerAlignment); | 155 STATIC_ASSERT(kTagMask + 1 <= kPointerAlignment); |
156 | 156 |
157 T* single_value() const { | 157 T* single_value() const { |
158 ASSERT((data_ & kTagMask) == kSingletonTag); | 158 DCHECK((data_ & kTagMask) == kSingletonTag); |
159 STATIC_ASSERT(kSingletonTag == 0); | 159 STATIC_ASSERT(kSingletonTag == 0); |
160 return reinterpret_cast<T*>(data_); | 160 return reinterpret_cast<T*>(data_); |
161 } | 161 } |
162 | 162 |
163 PointerList* list() const { | 163 PointerList* list() const { |
164 ASSERT((data_ & kTagMask) == kListTag); | 164 DCHECK((data_ & kTagMask) == kListTag); |
165 return reinterpret_cast<PointerList*>(data_ & kValueMask); | 165 return reinterpret_cast<PointerList*>(data_ & kValueMask); |
166 } | 166 } |
167 | 167 |
168 intptr_t data_; | 168 intptr_t data_; |
169 | 169 |
170 DISALLOW_COPY_AND_ASSIGN(SmallPointerList); | 170 DISALLOW_COPY_AND_ASSIGN(SmallPointerList); |
171 }; | 171 }; |
172 | 172 |
173 } } // namespace v8::internal | 173 } } // namespace v8::internal |
174 | 174 |
175 #endif // V8_SMALL_POINTER_LIST_H_ | 175 #endif // V8_SMALL_POINTER_LIST_H_ |
OLD | NEW |