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 #ifndef VM_SYMBOLS_H_ | 5 #ifndef VM_SYMBOLS_H_ |
6 #define VM_SYMBOLS_H_ | 6 #define VM_SYMBOLS_H_ |
7 | 7 |
8 #include "vm/object.h" | 8 #include "vm/object.h" |
9 #include "vm/snapshot_ids.h" | 9 #include "vm/snapshot_ids.h" |
10 | 10 |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 | 431 |
432 // Get symbol for scanner keyword. | 432 // Get symbol for scanner keyword. |
433 static const String& Keyword(Token::Kind keyword); | 433 static const String& Keyword(Token::Kind keyword); |
434 | 434 |
435 // Initialize frequently used symbols in the vm isolate. | 435 // Initialize frequently used symbols in the vm isolate. |
436 static void InitOnce(Isolate* isolate); | 436 static void InitOnce(Isolate* isolate); |
437 | 437 |
438 // Initialize and setup a symbol table for the isolate. | 438 // Initialize and setup a symbol table for the isolate. |
439 static void SetupSymbolTable(Isolate* isolate); | 439 static void SetupSymbolTable(Isolate* isolate); |
440 | 440 |
441 // Get number of symbols in an isolate's symbol table. | |
442 static intptr_t Size(Isolate* isolate); | |
443 | |
444 // Creates a Symbol given a C string that is assumed to contain | 441 // Creates a Symbol given a C string that is assumed to contain |
445 // UTF-8 encoded characters and '\0' is considered a termination character. | 442 // UTF-8 encoded characters and '\0' is considered a termination character. |
446 // TODO(7123) - Rename this to FromCString(....). | 443 // TODO(7123) - Rename this to FromCString(....). |
447 static RawString* New(const char* cstr) { | 444 static RawString* New(const char* cstr) { |
448 return New(cstr, strlen(cstr)); | 445 return New(cstr, strlen(cstr)); |
449 } | 446 } |
450 static RawString* New(const char* cstr, intptr_t length); | 447 static RawString* New(const char* cstr, intptr_t length); |
451 | 448 |
452 // Creates a new Symbol from an array of UTF-8 encoded characters. | 449 // Creates a new Symbol from an array of UTF-8 encoded characters. |
453 static RawString* FromUTF8(const uint8_t* utf8_array, intptr_t len); | 450 static RawString* FromUTF8(const uint8_t* utf8_array, intptr_t len); |
(...skipping 18 matching lines...) Expand all Loading... |
472 static RawString* FromCharCode(int32_t char_code); | 469 static RawString* FromCharCode(int32_t char_code); |
473 | 470 |
474 static RawString** PredefinedAddress() { | 471 static RawString** PredefinedAddress() { |
475 return reinterpret_cast<RawString**>(&predefined_); | 472 return reinterpret_cast<RawString**>(&predefined_); |
476 } | 473 } |
477 | 474 |
478 static void DumpStats(); | 475 static void DumpStats(); |
479 | 476 |
480 private: | 477 private: |
481 enum { | 478 enum { |
482 kInitialVMIsolateSymtabSize = 512, | 479 kInitialVMIsolateSymtabSize = 1024, |
483 kInitialSymtabSize = 2048 | 480 kInitialSymtabSize = 2048 |
484 }; | 481 }; |
485 | 482 |
486 // Helper functions to create a symbol given a string or set of characters. | 483 static void GetStats(Isolate* isolate, |
487 template<typename CharacterType, typename CallbackType> | 484 intptr_t* size, |
488 static RawString* NewSymbol(const CharacterType* characters, | 485 intptr_t* capacity); |
489 intptr_t len, | 486 |
490 CallbackType new_string); | 487 template<typename StringType> |
| 488 static RawString* NewSymbol(const StringType& str); |
491 | 489 |
492 // Add the string into the VM isolate symbol table. | 490 // Add the string into the VM isolate symbol table. |
493 static void Add(const Array& symbol_table, const String& str); | 491 static void AddToVMIsolate(const String& str); |
494 | 492 |
495 // Insert symbol into symbol table, growing it if necessary. | |
496 static void InsertIntoSymbolTable(const Array& symbol_table, | |
497 const String& symbol, | |
498 intptr_t index); | |
499 | |
500 // Grow the symbol table. | |
501 static void GrowSymbolTable(const Array& symbol_table); | |
502 | |
503 // Return index in symbol table if the symbol already exists or | |
504 // return the index into which the new symbol can be added. | |
505 template<typename T> | |
506 static intptr_t FindIndex(const Array& symbol_table, | |
507 const T* characters, | |
508 intptr_t len, | |
509 intptr_t hash); | |
510 static intptr_t FindIndex(const Array& symbol_table, | |
511 const String& str, | |
512 intptr_t begin_index, | |
513 intptr_t len, | |
514 intptr_t hash); | |
515 static intptr_t LookupVMSymbol(RawObject* obj); | 493 static intptr_t LookupVMSymbol(RawObject* obj); |
516 static RawObject* GetVMSymbol(intptr_t object_id); | 494 static RawObject* GetVMSymbol(intptr_t object_id); |
517 static bool IsVMSymbolId(intptr_t object_id) { | 495 static bool IsVMSymbolId(intptr_t object_id) { |
518 return (object_id >= kMaxPredefinedObjectIds && | 496 return (object_id >= kMaxPredefinedObjectIds && |
519 object_id < (kMaxPredefinedObjectIds + kMaxPredefinedId)); | 497 object_id < (kMaxPredefinedObjectIds + kMaxPredefinedId)); |
520 } | 498 } |
521 | 499 |
522 // List of Latin1 characters stored in the vm isolate as symbols | 500 // List of Latin1 characters stored in the vm isolate as symbols |
523 // in order to make Symbols::FromCharCode fast. This structure is | 501 // in order to make Symbols::FromCharCode fast. This structure is |
524 // used in generated dart code for direct access to these objects. | 502 // used in generated dart code for direct access to these objects. |
525 static RawString* predefined_[kNumberOfOneCharCodeSymbols]; | 503 static RawString* predefined_[kNumberOfOneCharCodeSymbols]; |
526 | 504 |
527 // List of handles for predefined symbols. | 505 // List of handles for predefined symbols. |
528 static String* symbol_handles_[kMaxPredefinedId]; | 506 static String* symbol_handles_[kMaxPredefinedId]; |
529 | 507 |
530 // Statistics used to measure the efficiency of the symbol table. | |
531 static const intptr_t kMaxCollisionBuckets = 10; | |
532 static intptr_t num_of_grows_; | |
533 static intptr_t collision_count_[kMaxCollisionBuckets]; | |
534 | |
535 friend class String; | 508 friend class String; |
536 friend class SnapshotReader; | 509 friend class SnapshotReader; |
537 friend class SnapshotWriter; | 510 friend class SnapshotWriter; |
538 friend class ApiMessageReader; | 511 friend class ApiMessageReader; |
539 | 512 |
540 DISALLOW_COPY_AND_ASSIGN(Symbols); | 513 DISALLOW_COPY_AND_ASSIGN(Symbols); |
541 }; | 514 }; |
542 | 515 |
543 } // namespace dart | 516 } // namespace dart |
544 | 517 |
545 #endif // VM_SYMBOLS_H_ | 518 #endif // VM_SYMBOLS_H_ |
OLD | NEW |