| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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_HYDROGEN_UNIQUE_H_ | 5 #ifndef V8_HYDROGEN_UNIQUE_H_ |
| 6 #define V8_HYDROGEN_UNIQUE_H_ | 6 #define V8_HYDROGEN_UNIQUE_H_ |
| 7 | 7 |
| 8 #include "src/handles.h" | 8 #include "src/handles.h" |
| 9 #include "src/objects.h" | 9 #include "src/objects.h" |
| 10 #include "src/string-stream.h" | 10 #include "src/string-stream.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 return Unique<T>(reinterpret_cast<Address>(NULL), handle); | 111 return Unique<T>(reinterpret_cast<Address>(NULL), handle); |
| 112 } | 112 } |
| 113 | 113 |
| 114 static Unique<T> CreateImmovable(Handle<T> handle) { | 114 static Unique<T> CreateImmovable(Handle<T> handle) { |
| 115 return Unique<T>(reinterpret_cast<Address>(*handle), handle); | 115 return Unique<T>(reinterpret_cast<Address>(*handle), handle); |
| 116 } | 116 } |
| 117 | 117 |
| 118 friend class UniqueSet<T>; // Uses internal details for speed. | 118 friend class UniqueSet<T>; // Uses internal details for speed. |
| 119 template <class U> | 119 template <class U> |
| 120 friend class Unique; // For comparing raw_address values. | 120 friend class Unique; // For comparing raw_address values. |
| 121 template <class U> | |
| 122 friend class PrintableUnique; // For automatic up casting. | |
| 123 | 121 |
| 124 protected: | 122 protected: |
| 125 Unique<T>() : raw_address_(NULL) { } | 123 Unique<T>() : raw_address_(NULL) { } |
| 126 | 124 |
| 127 Address raw_address_; | 125 Address raw_address_; |
| 128 Handle<T> handle_; | 126 Handle<T> handle_; |
| 129 | 127 |
| 130 friend class SideEffectsTracker; | 128 friend class SideEffectsTracker; |
| 131 }; | 129 }; |
| 132 | 130 |
| 133 | 131 |
| 134 // TODO(danno): At some point if all of the uses of Unique end up using | |
| 135 // PrintableUnique, then we should merge PrintableUnique into Unique and | |
| 136 // predicate generating the printable string on a "am I tracing" check. | |
| 137 template <class T> | |
| 138 class PrintableUnique : public Unique<T> { | |
| 139 public: | |
| 140 // TODO(titzer): make private and introduce a uniqueness scope. | |
| 141 PrintableUnique() : string_("null") {} | |
| 142 | |
| 143 // TODO(titzer): make private and introduce a uniqueness scope. | |
| 144 explicit PrintableUnique(Zone* zone, Handle<T> handle) : Unique<T>(handle) { | |
| 145 InitializeString(zone); | |
| 146 } | |
| 147 | |
| 148 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. | |
| 149 PrintableUnique(Zone* zone, Address raw_address, Handle<T> handle) | |
| 150 : Unique<T>(raw_address, handle) { | |
| 151 InitializeString(zone); | |
| 152 } | |
| 153 | |
| 154 // Constructor for handling automatic up casting. | |
| 155 // Eg. PrintableUnique<JSFunction> can be passed when PrintableUnique<Object> | |
| 156 // is expected. | |
| 157 template <class S> | |
| 158 PrintableUnique(PrintableUnique<S> uniq) // NOLINT | |
| 159 : Unique<T>(Handle<T>()) { | |
| 160 #ifdef DEBUG | |
| 161 T* a = NULL; | |
| 162 S* b = NULL; | |
| 163 a = b; // Fake assignment to enforce type checks. | |
| 164 USE(a); | |
| 165 #endif | |
| 166 this->raw_address_ = uniq.raw_address_; | |
| 167 this->handle_ = uniq.handle_; | |
| 168 string_ = uniq.string(); | |
| 169 } | |
| 170 | |
| 171 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. | |
| 172 static PrintableUnique<T> CreateUninitialized(Zone* zone, Handle<T> handle) { | |
| 173 return PrintableUnique<T>(zone, reinterpret_cast<Address>(NULL), handle); | |
| 174 } | |
| 175 | |
| 176 static PrintableUnique<T> CreateImmovable(Zone* zone, Handle<T> handle) { | |
| 177 return PrintableUnique<T>(zone, reinterpret_cast<Address>(*handle), handle); | |
| 178 } | |
| 179 | |
| 180 const char* string() const { return string_; } | |
| 181 | |
| 182 private: | |
| 183 const char* string_; | |
| 184 | |
| 185 void InitializeString(Zone* zone) { | |
| 186 // The stringified version of the parameter must be calculated when the | |
| 187 // Operator is constructed to avoid accessing the heap. | |
| 188 HeapStringAllocator temp_allocator; | |
| 189 StringStream stream(&temp_allocator); | |
| 190 this->handle_->ShortPrint(&stream); | |
| 191 SmartArrayPointer<const char> desc_string = stream.ToCString(); | |
| 192 const char* desc_chars = desc_string.get(); | |
| 193 int length = static_cast<int>(strlen(desc_chars)); | |
| 194 char* desc_copy = zone->NewArray<char>(length + 1); | |
| 195 memcpy(desc_copy, desc_chars, length + 1); | |
| 196 string_ = desc_copy; | |
| 197 } | |
| 198 }; | |
| 199 | |
| 200 | |
| 201 template <typename T> | 132 template <typename T> |
| 202 class UniqueSet FINAL : public ZoneObject { | 133 class UniqueSet FINAL : public ZoneObject { |
| 203 public: | 134 public: |
| 204 // Constructor. A new set will be empty. | 135 // Constructor. A new set will be empty. |
| 205 UniqueSet() : size_(0), capacity_(0), array_(NULL) { } | 136 UniqueSet() : size_(0), capacity_(0), array_(NULL) { } |
| 206 | 137 |
| 207 // Capacity constructor. A new set will be empty. | 138 // Capacity constructor. A new set will be empty. |
| 208 UniqueSet(int capacity, Zone* zone) | 139 UniqueSet(int capacity, Zone* zone) |
| 209 : size_(0), capacity_(capacity), | 140 : size_(0), capacity_(capacity), |
| 210 array_(zone->NewArray<Unique<T> >(capacity)) { | 141 array_(zone->NewArray<Unique<T> >(capacity)) { |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 } | 338 } |
| 408 capacity_ = new_capacity; | 339 capacity_ = new_capacity; |
| 409 array_ = new_array; | 340 array_ = new_array; |
| 410 } | 341 } |
| 411 } | 342 } |
| 412 }; | 343 }; |
| 413 | 344 |
| 414 } } // namespace v8::internal | 345 } } // namespace v8::internal |
| 415 | 346 |
| 416 #endif // V8_HYDROGEN_UNIQUE_H_ | 347 #endif // V8_HYDROGEN_UNIQUE_H_ |
| OLD | NEW |