| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/symbols.h" | 5 #include "vm/symbols.h" |
| 6 | 6 |
| 7 #include "vm/handles.h" | 7 #include "vm/handles.h" |
| 8 #include "vm/handles_impl.h" | 8 #include "vm/handles_impl.h" |
| 9 #include "vm/isolate.h" | 9 #include "vm/isolate.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| 11 #include "vm/object_store.h" | 11 #include "vm/object_store.h" |
| 12 #include "vm/raw_object.h" | 12 #include "vm/raw_object.h" |
| 13 #include "vm/snapshot_ids.h" | 13 #include "vm/snapshot_ids.h" |
| 14 #include "vm/unicode.h" | 14 #include "vm/unicode.h" |
| 15 #include "vm/visitor.h" | 15 #include "vm/visitor.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 RawString* Symbols::predefined_[Symbols::kMaxId]; | 19 RawString* Symbols::predefined_[Symbols::kNumberOfOneCharCodeSymbols]; |
| 20 | 20 String* Symbols::symbol_handles_[Symbols::kMaxPredefinedId]; |
| 21 #define DEFINE_SYMBOL_HANDLE(symbol) \ | |
| 22 String* Symbols::symbol##_handle_ = NULL; | |
| 23 PREDEFINED_SYMBOL_HANDLES_LIST(DEFINE_SYMBOL_HANDLE) | |
| 24 #undef DEFINE_SYMBOL_HANDLE | |
| 25 | 21 |
| 26 static const char* names[] = { | 22 static const char* names[] = { |
| 27 NULL, | 23 NULL, |
| 28 | 24 |
| 29 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ | 25 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ |
| 30 literal, | 26 literal, |
| 31 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) | 27 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) |
| 32 #undef DEFINE_SYMBOL_LITERAL | 28 #undef DEFINE_SYMBOL_LITERAL |
| 33 }; | 29 }; |
| 34 | 30 |
| 35 intptr_t Symbols::num_of_grows_; | 31 intptr_t Symbols::num_of_grows_; |
| 36 intptr_t Symbols::collision_count_[kMaxCollisionBuckets]; | 32 intptr_t Symbols::collision_count_[kMaxCollisionBuckets]; |
| 37 | 33 |
| 38 DEFINE_FLAG(bool, dump_symbol_stats, false, "Dump symbol table statistics"); | 34 DEFINE_FLAG(bool, dump_symbol_stats, false, "Dump symbol table statistics"); |
| 39 | 35 |
| 40 | 36 |
| 41 const char* Symbols::Name(SymbolId symbol) { | 37 const char* Symbols::Name(SymbolId symbol) { |
| 42 ASSERT((symbol > kIllegal) && (symbol < kMaxPredefinedId)); | 38 ASSERT((symbol > kIllegal) && (symbol < kNullCharId)); |
| 43 return names[symbol]; | 39 return names[symbol]; |
| 44 } | 40 } |
| 45 | 41 |
| 46 | 42 |
| 47 void Symbols::InitOnce(Isolate* isolate) { | 43 void Symbols::InitOnce(Isolate* isolate) { |
| 48 // Should only be run by the vm isolate. | 44 // Should only be run by the vm isolate. |
| 49 ASSERT(isolate == Dart::vm_isolate()); | 45 ASSERT(isolate == Dart::vm_isolate()); |
| 50 | 46 |
| 51 if (FLAG_dump_symbol_stats) { | 47 if (FLAG_dump_symbol_stats) { |
| 52 num_of_grows_ = 0; | 48 num_of_grows_ = 0; |
| 53 for (intptr_t i = 0; i < kMaxCollisionBuckets; i++) { | 49 for (intptr_t i = 0; i < kMaxCollisionBuckets; i++) { |
| 54 collision_count_[i] = 0; | 50 collision_count_[i] = 0; |
| 55 } | 51 } |
| 56 } | 52 } |
| 57 | 53 |
| 58 // Create and setup a symbol table in the vm isolate. | 54 // Create and setup a symbol table in the vm isolate. |
| 59 SetupSymbolTable(isolate); | 55 SetupSymbolTable(isolate); |
| 60 | 56 |
| 61 // Create all predefined symbols. | 57 // Create all predefined symbols. |
| 62 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxPredefinedId); | 58 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kNullCharId); |
| 63 ObjectStore* object_store = isolate->object_store(); | 59 ObjectStore* object_store = isolate->object_store(); |
| 64 Array& symbol_table = Array::Handle(); | 60 Array& symbol_table = Array::Handle(); |
| 65 dart::String& str = String::Handle(); | |
| 66 | 61 |
| 67 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { | 62 |
| 63 // First set up all the predefined string symbols. |
| 64 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { |
| 68 // The symbol_table needs to be reloaded as it might have grown in the | 65 // The symbol_table needs to be reloaded as it might have grown in the |
| 69 // previous iteration. | 66 // previous iteration. |
| 70 symbol_table = object_store->symbol_table(); | 67 symbol_table = object_store->symbol_table(); |
| 71 str = OneByteString::New(names[i], Heap::kOld); | 68 String* str = reinterpret_cast<String*>(Dart::AllocateReadOnlyHandle()); |
| 72 Add(symbol_table, str); | 69 *str = OneByteString::New(names[i], Heap::kOld); |
| 73 predefined_[i] = str.raw(); | 70 Add(symbol_table, *str); |
| 71 symbol_handles_[i] = str; |
| 74 } | 72 } |
| 75 Object::RegisterSingletonClassNames(); | 73 Object::RegisterSingletonClassNames(); |
| 76 | 74 |
| 77 // Add Latin1 characters as Symbols, so that Symbols::FromCharCode is fast. | 75 // Add Latin1 characters as Symbols, so that Symbols::FromCharCode is fast. |
| 78 for (intptr_t c = 0; c <= kMaxOneCharCodeSymbol; c++) { | 76 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) { |
| 79 // The symbol_table needs to be reloaded as it might have grown in the | 77 // The symbol_table needs to be reloaded as it might have grown in the |
| 80 // previous iteration. | 78 // previous iteration. |
| 81 symbol_table = object_store->symbol_table(); | 79 symbol_table = object_store->symbol_table(); |
| 82 ASSERT(kMaxPredefinedId + c < kMaxId); | 80 intptr_t idx = (kNullCharId + c); |
| 81 ASSERT(idx < kMaxPredefinedId); |
| 83 ASSERT(Utf::IsLatin1(c)); | 82 ASSERT(Utf::IsLatin1(c)); |
| 84 uint8_t ch = static_cast<uint8_t>(c); | 83 uint8_t ch = static_cast<uint8_t>(c); |
| 85 str = OneByteString::New(&ch, 1, Heap::kOld); | 84 String* str = reinterpret_cast<String*>(Dart::AllocateReadOnlyHandle()); |
| 86 Add(symbol_table, str); | 85 *str = OneByteString::New(&ch, 1, Heap::kOld); |
| 87 predefined_[kMaxPredefinedId + c] = str.raw(); | 86 Add(symbol_table, *str); |
| 87 predefined_[c] = str->raw(); |
| 88 symbol_handles_[idx] = str; |
| 88 } | 89 } |
| 89 | |
| 90 #define INITIALIZE_SYMBOL_HANDLE(symbol) \ | |
| 91 symbol##_handle_ = reinterpret_cast<String*>( \ | |
| 92 Dart::AllocateReadOnlyHandle()); \ | |
| 93 *symbol##_handle_ = symbol(); | |
| 94 PREDEFINED_SYMBOL_HANDLES_LIST(INITIALIZE_SYMBOL_HANDLE) | |
| 95 #undef INITIALIZE_SYMBOL_HANDLE | |
| 96 } | 90 } |
| 97 | 91 |
| 98 | 92 |
| 99 void Symbols::SetupSymbolTable(Isolate* isolate) { | 93 void Symbols::SetupSymbolTable(Isolate* isolate) { |
| 100 ASSERT(isolate != NULL); | 94 ASSERT(isolate != NULL); |
| 101 | 95 |
| 102 // Setup the symbol table used within the String class. | 96 // Setup the symbol table used within the String class. |
| 103 const int initial_size = (isolate == Dart::vm_isolate()) ? | 97 const int initial_size = (isolate == Dart::vm_isolate()) ? |
| 104 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 98 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| 105 const Array& array = Array::Handle(Array::New(initial_size + 1)); | 99 const Array& array = Array::Handle(Array::New(initial_size + 1)); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 265 } |
| 272 ASSERT(symbol.IsSymbol()); | 266 ASSERT(symbol.IsSymbol()); |
| 273 return symbol.raw(); | 267 return symbol.raw(); |
| 274 } | 268 } |
| 275 | 269 |
| 276 | 270 |
| 277 RawString* Symbols::FromCharCode(int32_t char_code) { | 271 RawString* Symbols::FromCharCode(int32_t char_code) { |
| 278 if (char_code > kMaxOneCharCodeSymbol) { | 272 if (char_code > kMaxOneCharCodeSymbol) { |
| 279 return FromUTF32(&char_code, 1); | 273 return FromUTF32(&char_code, 1); |
| 280 } | 274 } |
| 281 return predefined_[kNullCharId + char_code]; | 275 return predefined_[char_code]; |
| 282 } | 276 } |
| 283 | 277 |
| 284 | 278 |
| 285 void Symbols::DumpStats() { | 279 void Symbols::DumpStats() { |
| 286 if (FLAG_dump_symbol_stats) { | 280 if (FLAG_dump_symbol_stats) { |
| 287 intptr_t table_size = 0; | 281 intptr_t table_size = 0; |
| 288 dart::Smi& used = Smi::Handle(); | 282 dart::Smi& used = Smi::Handle(); |
| 289 Array& symbol_table = Array::Handle(Array::null()); | 283 Array& symbol_table = Array::Handle(Array::null()); |
| 290 | 284 |
| 291 // First dump VM symbol table stats. | 285 // First dump VM symbol table stats. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 if (num_collisions >= kMaxCollisionBuckets) { | 428 if (num_collisions >= kMaxCollisionBuckets) { |
| 435 num_collisions = (kMaxCollisionBuckets - 1); | 429 num_collisions = (kMaxCollisionBuckets - 1); |
| 436 } | 430 } |
| 437 collision_count_[num_collisions] += 1; | 431 collision_count_[num_collisions] += 1; |
| 438 } | 432 } |
| 439 return index; // Index of symbol if found or slot into which to add symbol. | 433 return index; // Index of symbol if found or slot into which to add symbol. |
| 440 } | 434 } |
| 441 | 435 |
| 442 | 436 |
| 443 intptr_t Symbols::LookupVMSymbol(RawObject* obj) { | 437 intptr_t Symbols::LookupVMSymbol(RawObject* obj) { |
| 444 for (intptr_t i = 1; i < Symbols::kMaxId; i++) { | 438 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { |
| 445 if (predefined_[i] == obj) { | 439 if (symbol_handles_[i]->raw() == obj) { |
| 446 return (i + kMaxPredefinedObjectIds); | 440 return (i + kMaxPredefinedObjectIds); |
| 447 } | 441 } |
| 448 } | 442 } |
| 449 return kInvalidIndex; | 443 return kInvalidIndex; |
| 450 } | 444 } |
| 451 | 445 |
| 452 | 446 |
| 453 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 447 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 454 ASSERT(IsVMSymbolId(object_id)); | 448 ASSERT(IsVMSymbolId(object_id)); |
| 455 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 449 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 456 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 450 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
| 451 return symbol_handles_[i]->raw(); |
| 452 } |
| 453 return Object::null(); |
| 457 } | 454 } |
| 458 | 455 |
| 459 } // namespace dart | 456 } // namespace dart |
| OLD | NEW |