| 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/isolate.h" | 7 #include "vm/isolate.h" |
| 8 #include "vm/object.h" | 8 #include "vm/object.h" |
| 9 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
| 10 #include "vm/raw_object.h" | 10 #include "vm/raw_object.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 // Create and setup a symbol table in the vm isolate. | 35 // Create and setup a symbol table in the vm isolate. |
| 36 SetupSymbolTable(isolate); | 36 SetupSymbolTable(isolate); |
| 37 | 37 |
| 38 // Turn off population of symbols in the VM symbol table, so that we | 38 // Turn off population of symbols in the VM symbol table, so that we |
| 39 // don't find these symbols while doing a Symbols::New(...). | 39 // don't find these symbols while doing a Symbols::New(...). |
| 40 // Create all predefined symbols. | 40 // Create all predefined symbols. |
| 41 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxId); | 41 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxId); |
| 42 const Array& symbol_table = | 42 const Array& symbol_table = |
| 43 Array::Handle(isolate->object_store()->symbol_table()); | 43 Array::Handle(isolate->object_store()->symbol_table()); |
| 44 OneByteString& str = OneByteString::Handle(); | 44 dart::OneByteString& str = OneByteString::Handle(); |
| 45 | 45 |
| 46 for (intptr_t i = 1; i < Symbols::kMaxId; i++) { | 46 for (intptr_t i = 1; i < Symbols::kMaxId; i++) { |
| 47 str = OneByteString::New(names[i], Heap::kOld); | 47 str = OneByteString::New(names[i], Heap::kOld); |
| 48 Add(symbol_table, str); | 48 Add(symbol_table, str); |
| 49 predefined_[i] = str.raw(); | 49 predefined_[i] = str.raw(); |
| 50 } | 50 } |
| 51 Object::RegisterSingletonClassNames(); | 51 Object::RegisterSingletonClassNames(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 | 54 |
| 55 void Symbols::SetupSymbolTable(Isolate* isolate) { | 55 void Symbols::SetupSymbolTable(Isolate* isolate) { |
| 56 ASSERT(isolate != NULL); | 56 ASSERT(isolate != NULL); |
| 57 | 57 |
| 58 // Setup the symbol table used within the String class. | 58 // Setup the symbol table used within the String class. |
| 59 const int initial_size = (isolate == Dart::vm_isolate()) ? | 59 const int initial_size = (isolate == Dart::vm_isolate()) ? |
| 60 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 60 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| 61 const Array& array = Array::Handle(Array::New(initial_size + 1)); | 61 const Array& array = Array::Handle(Array::New(initial_size + 1)); |
| 62 | 62 |
| 63 // Last element contains the count of used slots. | 63 // Last element contains the count of used slots. |
| 64 array.SetAt(initial_size, Smi::Handle(Smi::New(0))); | 64 array.SetAt(initial_size, Smi::Handle(Smi::New(0))); |
| 65 isolate->object_store()->set_symbol_table(array); | 65 isolate->object_store()->set_symbol_table(array); |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 intptr_t Symbols::Size(Isolate* isolate) { | 69 intptr_t Symbols::Size(Isolate* isolate) { |
| 70 ASSERT(isolate != NULL); | 70 ASSERT(isolate != NULL); |
| 71 Array& symbol_table = Array::Handle(isolate, | 71 Array& symbol_table = Array::Handle(isolate, |
| 72 isolate->object_store()->symbol_table()); | 72 isolate->object_store()->symbol_table()); |
| 73 intptr_t table_size_index = symbol_table.Length() - 1; | 73 intptr_t table_size_index = symbol_table.Length() - 1; |
| 74 Smi& used = Smi::Handle(); | 74 dart::Smi& used = Smi::Handle(); |
| 75 used ^= symbol_table.At(table_size_index); | 75 used ^= symbol_table.At(table_size_index); |
| 76 return used.Value(); | 76 return used.Value(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 void Symbols::Add(const Array& symbol_table, const String& str) { | 80 void Symbols::Add(const Array& symbol_table, const String& str) { |
| 81 // Should only be run by the vm isolate. | 81 // Should only be run by the vm isolate. |
| 82 ASSERT(Isolate::Current() == Dart::vm_isolate()); | 82 ASSERT(Isolate::Current() == Dart::vm_isolate()); |
| 83 intptr_t hash = str.Hash(); | 83 intptr_t hash = str.Hash(); |
| 84 intptr_t index = FindIndex(symbol_table, str, 0, str.Length(), hash); | 84 intptr_t index = FindIndex(symbol_table, str, 0, str.Length(), hash); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 196 |
| 197 | 197 |
| 198 void Symbols::GrowSymbolTable(const Array& symbol_table) { | 198 void Symbols::GrowSymbolTable(const Array& symbol_table) { |
| 199 // TODO(iposva): Avoid exponential growth. | 199 // TODO(iposva): Avoid exponential growth. |
| 200 intptr_t table_size = symbol_table.Length() - 1; | 200 intptr_t table_size = symbol_table.Length() - 1; |
| 201 intptr_t new_table_size = table_size * 2; | 201 intptr_t new_table_size = table_size * 2; |
| 202 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); | 202 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); |
| 203 // Copy all elements from the original symbol table to the newly allocated | 203 // Copy all elements from the original symbol table to the newly allocated |
| 204 // array. | 204 // array. |
| 205 String& element = String::Handle(); | 205 String& element = String::Handle(); |
| 206 Object& new_element = Object::Handle(); | 206 dart::Object& new_element = Object::Handle(); |
| 207 for (intptr_t i = 0; i < table_size; i++) { | 207 for (intptr_t i = 0; i < table_size; i++) { |
| 208 element ^= symbol_table.At(i); | 208 element ^= symbol_table.At(i); |
| 209 if (!element.IsNull()) { | 209 if (!element.IsNull()) { |
| 210 intptr_t hash = element.Hash(); | 210 intptr_t hash = element.Hash(); |
| 211 intptr_t index = hash % new_table_size; | 211 intptr_t index = hash % new_table_size; |
| 212 new_element = new_symbol_table.At(index); | 212 new_element = new_symbol_table.At(index); |
| 213 while (!new_element.IsNull()) { | 213 while (!new_element.IsNull()) { |
| 214 index = (index + 1) % new_table_size; // Move to next element. | 214 index = (index + 1) % new_table_size; // Move to next element. |
| 215 new_element = new_symbol_table.At(index); | 215 new_element = new_symbol_table.At(index); |
| 216 } | 216 } |
| 217 new_symbol_table.SetAt(index, element); | 217 new_symbol_table.SetAt(index, element); |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 // Copy used count. | 220 // Copy used count. |
| 221 new_element = symbol_table.At(table_size); | 221 new_element = symbol_table.At(table_size); |
| 222 new_symbol_table.SetAt(new_table_size, new_element); | 222 new_symbol_table.SetAt(new_table_size, new_element); |
| 223 // Remember the new symbol table now. | 223 // Remember the new symbol table now. |
| 224 Isolate::Current()->object_store()->set_symbol_table(new_symbol_table); | 224 Isolate::Current()->object_store()->set_symbol_table(new_symbol_table); |
| 225 } | 225 } |
| 226 | 226 |
| 227 | 227 |
| 228 void Symbols::InsertIntoSymbolTable(const Array& symbol_table, | 228 void Symbols::InsertIntoSymbolTable(const Array& symbol_table, |
| 229 const String& symbol, | 229 const String& symbol, |
| 230 intptr_t index) { | 230 intptr_t index) { |
| 231 intptr_t table_size = symbol_table.Length() - 1; | 231 intptr_t table_size = symbol_table.Length() - 1; |
| 232 symbol.SetCanonical(); // Mark object as being canonical. | 232 symbol.SetCanonical(); // Mark object as being canonical. |
| 233 symbol_table.SetAt(index, symbol); // Remember the new symbol. | 233 symbol_table.SetAt(index, symbol); // Remember the new symbol. |
| 234 Smi& used = Smi::Handle(); | 234 dart::Smi& used = Smi::Handle(); |
| 235 used ^= symbol_table.At(table_size); | 235 used ^= symbol_table.At(table_size); |
| 236 intptr_t used_elements = used.Value() + 1; // One more element added. | 236 intptr_t used_elements = used.Value() + 1; // One more element added. |
| 237 used = Smi::New(used_elements); | 237 used = Smi::New(used_elements); |
| 238 symbol_table.SetAt(table_size, used); // Update used count. | 238 symbol_table.SetAt(table_size, used); // Update used count. |
| 239 | 239 |
| 240 // Rehash if symbol_table is 75% full. | 240 // Rehash if symbol_table is 75% full. |
| 241 if (used_elements > ((table_size / 4) * 3)) { | 241 if (used_elements > ((table_size / 4) * 3)) { |
| 242 GrowSymbolTable(symbol_table); | 242 GrowSymbolTable(symbol_table); |
| 243 } | 243 } |
| 244 } | 244 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 } | 306 } |
| 307 | 307 |
| 308 | 308 |
| 309 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 309 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 310 ASSERT(IsVMSymbolId(object_id)); | 310 ASSERT(IsVMSymbolId(object_id)); |
| 311 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 311 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 312 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 312 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 313 } | 313 } |
| 314 | 314 |
| 315 } // namespace dart | 315 } // namespace dart |
| OLD | NEW |