Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #ifndef V8_SMALL_POINTER_LIST_H_ | |
| 29 #define V8_SMALL_POINTER_LIST_H_ | |
| 30 | |
| 31 #include "checks.h" | |
| 32 #include "v8globals.h" | |
| 33 #include "zone.h" | |
| 34 | |
| 35 namespace v8 { | |
| 36 namespace internal { | |
| 37 | |
| 38 // SmallPointerList is a list optimized for storing no or just a | |
| 39 // single value. When more values are given it falls back to ZoneList. | |
| 40 // | |
| 41 // The interface tries to be as close to List from list.h as possible. | |
| 42 template <typename T> | |
| 43 class SmallPointerList { | |
| 44 public: | |
| 45 SmallPointerList() : data_(kEmptyTag) {} | |
| 46 | |
| 47 bool is_empty() const { return length() == 0; } | |
| 48 | |
| 49 int length() const { | |
| 50 if ((data_ & kTagMask) == kEmptyTag) return 0; | |
| 51 if ((data_ & kTagMask) == kSingletonTag) return 1; | |
| 52 return list()->length(); | |
| 53 } | |
| 54 | |
| 55 void Add(T* pointer) { | |
| 56 ASSERT(IsAligned(reinterpret_cast<intptr_t>(pointer), kPointerAlignment)); | |
| 57 if ((data_ & kTagMask) == kEmptyTag) { | |
| 58 data_ = reinterpret_cast<intptr_t>(pointer) | kSingletonTag; | |
| 59 return; | |
| 60 } | |
| 61 if ((data_ & kTagMask) == kSingletonTag) { | |
| 62 PointerList* list = new PointerList(2); | |
|
Kevin Millikin (Chromium)
2011/03/23 07:38:44
We should count the lengths of use lists that are
Vitaly Repeshko
2011/03/23 14:42:32
When compiling the V8 benchmark the number of valu
| |
| 63 list->Add(single_value()); | |
| 64 list->Add(pointer); | |
| 65 ASSERT(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment)); | |
| 66 data_ = reinterpret_cast<intptr_t>(list) | kListTag; | |
| 67 return; | |
| 68 } | |
| 69 list()->Add(pointer); | |
| 70 } | |
| 71 | |
| 72 // Note: returns T* and not T*& (unlike List from list.h). | |
| 73 // This makes the implementation simpler and more const correct. | |
| 74 T* at(int i) const { | |
| 75 ASSERT((data_ & kTagMask) != kEmptyTag); | |
| 76 if ((data_ & kTagMask) == kSingletonTag) { | |
| 77 ASSERT(i == 0); | |
| 78 return single_value(); | |
| 79 } | |
| 80 return list()->at(i); | |
| 81 } | |
| 82 | |
| 83 // See the note above. | |
| 84 T* operator[](int i) const { return at(i); } | |
| 85 | |
| 86 void RemoveElement(T* pointer) { | |
|
Kevin Millikin (Chromium)
2011/03/23 07:38:44
Maybe a short comment that the intended semantics
Vitaly Repeshko
2011/03/23 14:42:32
Done.
| |
| 87 if ((data_ & kTagMask) == kEmptyTag) return; | |
| 88 if ((data_ & kTagMask) == kSingletonTag) { | |
| 89 if (pointer == single_value()) { | |
| 90 data_ = kEmptyTag; | |
| 91 } | |
| 92 return; | |
| 93 } | |
| 94 list()->RemoveElement(pointer); | |
| 95 } | |
| 96 | |
| 97 T* RemoveLast() { | |
| 98 ASSERT((data_ & kTagMask) != kEmptyTag); | |
| 99 if ((data_ & kTagMask) == kSingletonTag) { | |
| 100 T* result = single_value(); | |
| 101 data_ = kEmptyTag; | |
| 102 return result; | |
| 103 } | |
| 104 return list()->RemoveLast(); | |
| 105 } | |
| 106 | |
| 107 void Rewind(int pos) { | |
| 108 if ((data_ & kTagMask) == kEmptyTag) { | |
| 109 ASSERT(pos == 0); | |
| 110 return; | |
| 111 } | |
| 112 if ((data_ & kTagMask) == kSingletonTag) { | |
| 113 ASSERT(pos == 0 || pos == 1); | |
| 114 if (pos == 0) { | |
| 115 data_ = kEmptyTag; | |
| 116 } | |
| 117 return; | |
| 118 } | |
| 119 list()->Rewind(pos); | |
| 120 } | |
| 121 | |
| 122 int CountOccurrences(T* pointer, int start, int end) const { | |
| 123 if ((data_ & kTagMask) == kEmptyTag) return 0; | |
| 124 if ((data_ & kTagMask) == kSingletonTag) { | |
| 125 if (start == 0 && end >= 0) { | |
| 126 return (single_value() == pointer) ? 1 : 0; | |
| 127 } | |
| 128 return 0; | |
| 129 } | |
| 130 return list()->CountOccurrences(pointer, start, end); | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 typedef ZoneList<T*> PointerList; | |
| 135 | |
| 136 static const intptr_t kEmptyTag = 1; | |
| 137 static const intptr_t kSingletonTag = 0; | |
| 138 static const intptr_t kListTag = 2; | |
| 139 static const intptr_t kTagMask = 3; | |
| 140 static const intptr_t kValueMask = ~kTagMask; | |
| 141 | |
| 142 STATIC_ASSERT(kTagMask + 1 <= kPointerAlignment); | |
| 143 | |
| 144 T* single_value() const { | |
| 145 ASSERT((data_ & kTagMask) == kSingletonTag); | |
| 146 STATIC_ASSERT(kSingletonTag == 0); | |
| 147 return reinterpret_cast<T*>(data_); | |
| 148 } | |
| 149 | |
| 150 PointerList* list() const { | |
| 151 ASSERT((data_ & kTagMask) == kListTag); | |
| 152 return reinterpret_cast<PointerList*>(data_ & kValueMask); | |
| 153 } | |
| 154 | |
| 155 intptr_t data_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(SmallPointerList); | |
| 158 }; | |
| 159 | |
| 160 } } // namespace v8::internal | |
| 161 | |
| 162 #endif // V8_SMALL_POINTER_LIST_H_ | |
| OLD | NEW |