Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: runtime/vm/object.h

Issue 8383029: Implement external strings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fully displace external string metadata Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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_OBJECT_H_ 5 #ifndef VM_OBJECT_H_
6 #define VM_OBJECT_H_ 6 #define VM_OBJECT_H_
7 7
8 #include "vm/assert.h" 8 #include "vm/assert.h"
9 #include "vm/dart.h" 9 #include "vm/dart.h"
10 #include "vm/globals.h" 10 #include "vm/globals.h"
(...skipping 2360 matching lines...) Expand 10 before | Expand all | Expand 10 after
2371 void set_value(double value) const; 2371 void set_value(double value) const;
2372 2372
2373 HEAP_OBJECT_IMPLEMENTATION(Double, Number); 2373 HEAP_OBJECT_IMPLEMENTATION(Double, Number);
2374 friend class Class; 2374 friend class Class;
2375 }; 2375 };
2376 2376
2377 2377
2378 // String may not be '\0' terminated. 2378 // String may not be '\0' terminated.
2379 class String : public Instance { 2379 class String : public Instance {
2380 public: 2380 public:
2381 typedef void (*PeerFinalizer)(void *peer);
siva 2011/11/18 23:26:09 dart_api.h is included in this file so can we use
cshapiro 2011/11/19 01:08:29 Good idea. I am thinking of getting rid of this t
2382
2381 // We use 30 bits for the hash code so that we consistently use a 2383 // We use 30 bits for the hash code so that we consistently use a
2382 // 32bit Smi representation for the hash code on all architectures. 2384 // 32bit Smi representation for the hash code on all architectures.
2383 static const intptr_t kHashBits = 30; 2385 static const intptr_t kHashBits = 30;
2384 2386
2387 static const intptr_t kOneByteChar = 1;
2388 static const intptr_t kTwoByteChar = 2;
2389 static const intptr_t kFourByteChar = 4;
2390
2385 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } 2391 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); }
2386 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } 2392 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); }
2387 2393
2388 virtual intptr_t Hash() const; 2394 virtual intptr_t Hash() const;
2389 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } 2395 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); }
2390 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); 2396 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len);
2391 static intptr_t Hash(const uint8_t* characters, intptr_t len); 2397 static intptr_t Hash(const uint8_t* characters, intptr_t len);
2392 static intptr_t Hash(const uint16_t* characters, intptr_t len); 2398 static intptr_t Hash(const uint16_t* characters, intptr_t len);
2393 static intptr_t Hash(const uint32_t* characters, intptr_t len); 2399 static intptr_t Hash(const uint32_t* characters, intptr_t len);
2394 2400
2395 virtual int32_t CharAt(intptr_t index) const; 2401 virtual int32_t CharAt(intptr_t index) const;
2396 2402
2403 virtual intptr_t CharSize() const;
2404
2397 bool Equals(const String& str, intptr_t begin_index, intptr_t len) const; 2405 bool Equals(const String& str, intptr_t begin_index, intptr_t len) const;
2398 bool Equals(const char* str) const; 2406 bool Equals(const char* str) const;
2399 bool Equals(const uint8_t* characters, intptr_t len) const; 2407 bool Equals(const uint8_t* characters, intptr_t len) const;
2400 bool Equals(const uint16_t* characters, intptr_t len) const; 2408 bool Equals(const uint16_t* characters, intptr_t len) const;
2401 bool Equals(const uint32_t* characters, intptr_t len) const; 2409 bool Equals(const uint32_t* characters, intptr_t len) const;
2402 2410
2403 virtual bool Equals(const Instance& other) const; 2411 virtual bool Equals(const Instance& other) const;
2404 2412
2405 intptr_t CompareTo(const String& other) const; 2413 intptr_t CompareTo(const String& other) const;
2406 2414
2407 bool StartsWith(const String& other) const; 2415 bool StartsWith(const String& other) const;
2408 2416
2409 virtual RawInstance* Canonicalize() const; 2417 virtual RawInstance* Canonicalize() const;
2410 2418
2411 bool IsSymbol() const; 2419 bool IsSymbol() const;
2412 2420
2413 static RawString* New(const char* str, Heap::Space space = Heap::kNew); 2421 static RawString* New(const char* str, Heap::Space space = Heap::kNew);
2414 static RawString* New(const uint8_t* characters, 2422 static RawString* New(const uint8_t* characters,
2415 intptr_t len, 2423 intptr_t len,
2416 Heap::Space space = Heap::kNew); 2424 Heap::Space space = Heap::kNew);
2417 static RawString* New(const uint16_t* characters, 2425 static RawString* New(const uint16_t* characters,
2418 intptr_t len, 2426 intptr_t len,
2419 Heap::Space space = Heap::kNew); 2427 Heap::Space space = Heap::kNew);
2420 static RawString* New(const uint32_t* characters, 2428 static RawString* New(const uint32_t* characters,
2421 intptr_t len, 2429 intptr_t len,
2422 Heap::Space space = Heap::kNew); 2430 Heap::Space space = Heap::kNew);
2423 static RawString* New(const String& str, Heap::Space space = Heap::kNew); 2431 static RawString* New(const String& str, Heap::Space space = Heap::kNew);
2424 2432
2433 static RawString* NewExternal(const uint8_t* characters,
2434 intptr_t len,
2435 void* peer,
2436 PeerFinalizer callback,
2437 Heap::Space = Heap::kNew);
2438 static RawString* NewExternal(const uint16_t* characters,
2439 intptr_t len,
2440 void* peer,
2441 PeerFinalizer callback,
2442 Heap::Space = Heap::kNew);
2443 static RawString* NewExternal(const uint32_t* characters,
2444 intptr_t len,
2445 void* peer,
2446 PeerFinalizer callback,
2447 Heap::Space = Heap::kNew);
2448
2425 static void Copy(const String& dst, 2449 static void Copy(const String& dst,
2426 intptr_t dst_offset, 2450 intptr_t dst_offset,
2427 const uint8_t* characters, 2451 const uint8_t* characters,
2428 intptr_t len); 2452 intptr_t len);
2429 static void Copy(const String& dst, 2453 static void Copy(const String& dst,
2430 intptr_t dst_offset, 2454 intptr_t dst_offset,
2431 const uint16_t* characters, 2455 const uint16_t* characters,
2432 intptr_t len); 2456 intptr_t len);
2433 static void Copy(const String& dst, 2457 static void Copy(const String& dst,
2434 intptr_t dst_offset, 2458 intptr_t dst_offset,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2492 HEAP_OBJECT_IMPLEMENTATION(String, Instance); 2516 HEAP_OBJECT_IMPLEMENTATION(String, Instance);
2493 }; 2517 };
2494 2518
2495 2519
2496 class OneByteString : public String { 2520 class OneByteString : public String {
2497 public: 2521 public:
2498 virtual int32_t CharAt(intptr_t index) const { 2522 virtual int32_t CharAt(intptr_t index) const {
2499 return *CharAddr(index); 2523 return *CharAddr(index);
2500 } 2524 }
2501 2525
2526 virtual intptr_t CharSize() const {
2527 return kOneByteChar;
2528 }
2529
2502 static intptr_t data_offset() { return OFFSET_OF(RawOneByteString, data_); } 2530 static intptr_t data_offset() { return OFFSET_OF(RawOneByteString, data_); }
2503 2531
2504 static intptr_t InstanceSize() { 2532 static intptr_t InstanceSize() {
2505 ASSERT(sizeof(RawOneByteString) == OFFSET_OF(RawOneByteString, data_)); 2533 ASSERT(sizeof(RawOneByteString) == OFFSET_OF(RawOneByteString, data_));
2506 return 0; 2534 return 0;
2507 } 2535 }
2508 2536
2509 static intptr_t InstanceSize(intptr_t len) { 2537 static intptr_t InstanceSize(intptr_t len) {
2510 return RoundedAllocationSize(sizeof(RawOneByteString) + len); 2538 return RoundedAllocationSize(sizeof(RawOneByteString) + len);
2511 } 2539 }
(...skipping 12 matching lines...) Expand all
2524 static RawOneByteString* New(const OneByteString& str, 2552 static RawOneByteString* New(const OneByteString& str,
2525 Heap::Space space); 2553 Heap::Space space);
2526 2554
2527 static RawOneByteString* Concat(const String& str1, 2555 static RawOneByteString* Concat(const String& str1,
2528 const String& str2, 2556 const String& str2,
2529 Heap::Space space); 2557 Heap::Space space);
2530 static RawOneByteString* ConcatAll(const Array& strings, 2558 static RawOneByteString* ConcatAll(const Array& strings,
2531 intptr_t len, 2559 intptr_t len,
2532 Heap::Space space); 2560 Heap::Space space);
2533 2561
2534 static RawString* SubString(const OneByteString& str,
2535 intptr_t begin_index,
2536 intptr_t length,
2537 Heap::Space space);
2538
2539 static RawOneByteString* Transform(int32_t (*mapping)(int32_t ch), 2562 static RawOneByteString* Transform(int32_t (*mapping)(int32_t ch),
2540 const String& str, 2563 const String& str,
2541 Heap::Space space); 2564 Heap::Space space);
2542 2565
2543 private: 2566 private:
2544 uint8_t* CharAddr(intptr_t index) const { 2567 uint8_t* CharAddr(intptr_t index) const {
2545 // TODO(iposva): Determine if we should throw an exception here. 2568 // TODO(iposva): Determine if we should throw an exception here.
2546 ASSERT((index >= 0) && (index < Length())); 2569 ASSERT((index >= 0) && (index < Length()));
2547 return &raw_ptr()->data_[index]; 2570 return &raw_ptr()->data_[index];
2548 } 2571 }
2549 2572
2550 HEAP_OBJECT_IMPLEMENTATION(OneByteString, String); 2573 HEAP_OBJECT_IMPLEMENTATION(OneByteString, String);
2551 friend class Class; 2574 friend class Class;
2552 friend class String; 2575 friend class String;
2553 }; 2576 };
2554 2577
2555 2578
2556 class TwoByteString : public String { 2579 class TwoByteString : public String {
2557 public: 2580 public:
2558 virtual int32_t CharAt(intptr_t index) const { 2581 virtual int32_t CharAt(intptr_t index) const {
2559 return *CharAddr(index); 2582 return *CharAddr(index);
2560 } 2583 }
2561 2584
2585 virtual intptr_t CharSize() const {
2586 return kTwoByteChar;
2587 }
2588
2562 static intptr_t InstanceSize() { 2589 static intptr_t InstanceSize() {
2563 ASSERT(sizeof(RawTwoByteString) == OFFSET_OF(RawTwoByteString, data_)); 2590 ASSERT(sizeof(RawTwoByteString) == OFFSET_OF(RawTwoByteString, data_));
2564 return 0; 2591 return 0;
2565 } 2592 }
2566 2593
2567 static intptr_t InstanceSize(intptr_t len) { 2594 static intptr_t InstanceSize(intptr_t len) {
2568 return RoundedAllocationSize(sizeof(RawTwoByteString) + (2 * len)); 2595 return RoundedAllocationSize(sizeof(RawTwoByteString) + (2 * len));
2569 } 2596 }
2570 2597
2571 static RawTwoByteString* New(intptr_t len, 2598 static RawTwoByteString* New(intptr_t len,
2572 Heap::Space space); 2599 Heap::Space space);
2573 static RawTwoByteString* New(const uint16_t* characters, 2600 static RawTwoByteString* New(const uint16_t* characters,
2574 intptr_t len, 2601 intptr_t len,
2575 Heap::Space space); 2602 Heap::Space space);
2576 static RawTwoByteString* New(const uint32_t* characters, 2603 static RawTwoByteString* New(const uint32_t* characters,
2577 intptr_t len, 2604 intptr_t len,
2578 Heap::Space space); 2605 Heap::Space space);
2579 static RawTwoByteString* New(const TwoByteString& str, 2606 static RawTwoByteString* New(const TwoByteString& str,
2580 Heap::Space space); 2607 Heap::Space space);
2581 2608
2582 static RawTwoByteString* Concat(const String& str1, 2609 static RawTwoByteString* Concat(const String& str1,
2583 const String& str2, 2610 const String& str2,
2584 Heap::Space space); 2611 Heap::Space space);
2585 static RawTwoByteString* ConcatAll(const Array& strings, 2612 static RawTwoByteString* ConcatAll(const Array& strings,
2586 intptr_t len, 2613 intptr_t len,
2587 Heap::Space space); 2614 Heap::Space space);
2588 2615
2589 static RawString* SubString(const TwoByteString& str,
2590 intptr_t begin_index,
2591 intptr_t length,
2592 Heap::Space space);
2593
2594 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), 2616 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch),
2595 const String& str, 2617 const String& str,
2596 Heap::Space space); 2618 Heap::Space space);
2597 2619
2598 private: 2620 private:
2599 uint16_t* CharAddr(intptr_t index) const { 2621 uint16_t* CharAddr(intptr_t index) const {
2600 ASSERT((index >= 0) && (index < Length())); 2622 ASSERT((index >= 0) && (index < Length()));
2601 return &raw_ptr()->data_[index]; 2623 return &raw_ptr()->data_[index];
2602 } 2624 }
2603 2625
2604 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); 2626 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String);
2605 friend class Class; 2627 friend class Class;
2606 friend class String; 2628 friend class String;
2607 }; 2629 };
2608 2630
2609 2631
2610 class FourByteString : public String { 2632 class FourByteString : public String {
2611 public: 2633 public:
2612 virtual int32_t CharAt(intptr_t index) const { 2634 virtual int32_t CharAt(intptr_t index) const {
2613 return *CharAddr(index); 2635 return *CharAddr(index);
2614 } 2636 }
2615 2637
2638 virtual intptr_t CharSize() const {
2639 return kFourByteChar;
2640 }
2641
2616 static intptr_t InstanceSize() { 2642 static intptr_t InstanceSize() {
2617 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_)); 2643 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_));
2618 return 0; 2644 return 0;
2619 } 2645 }
2620 2646
2621 static intptr_t InstanceSize(intptr_t len) { 2647 static intptr_t InstanceSize(intptr_t len) {
2622 return RoundedAllocationSize(sizeof(RawFourByteString) + (4 * len)); 2648 return RoundedAllocationSize(sizeof(RawFourByteString) + (4 * len));
2623 } 2649 }
2624 2650
2625 static RawFourByteString* New(intptr_t len, 2651 static RawFourByteString* New(intptr_t len,
2626 Heap::Space space); 2652 Heap::Space space);
2627 static RawFourByteString* New(const uint32_t* characters, 2653 static RawFourByteString* New(const uint32_t* characters,
2628 intptr_t len, 2654 intptr_t len,
2629 Heap::Space space); 2655 Heap::Space space);
2630 static RawFourByteString* New(const FourByteString& str, 2656 static RawFourByteString* New(const FourByteString& str,
2631 Heap::Space space); 2657 Heap::Space space);
2632 2658
2633 static RawFourByteString* Concat(const String& str1, 2659 static RawFourByteString* Concat(const String& str1,
2634 const String& str2, 2660 const String& str2,
2635 Heap::Space space); 2661 Heap::Space space);
2636 static RawFourByteString* ConcatAll(const Array& strings, 2662 static RawFourByteString* ConcatAll(const Array& strings,
2637 intptr_t len, 2663 intptr_t len,
2638 Heap::Space space); 2664 Heap::Space space);
2639 2665
2640 static RawString* SubString(const FourByteString& str,
2641 intptr_t begin_index,
2642 intptr_t length,
2643 Heap::Space space);
2644
2645 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch), 2666 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch),
2646 const String& str, 2667 const String& str,
2647 Heap::Space space); 2668 Heap::Space space);
2648 2669
2649 private: 2670 private:
2650 uint32_t* CharAddr(intptr_t index) const { 2671 uint32_t* CharAddr(intptr_t index) const {
2651 ASSERT((index >= 0) && (index < Length())); 2672 ASSERT((index >= 0) && (index < Length()));
2652 return &raw_ptr()->data_[index]; 2673 return &raw_ptr()->data_[index];
2653 } 2674 }
2654 2675
2655 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String); 2676 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String);
2656 friend class Class; 2677 friend class Class;
2657 friend class String; 2678 friend class String;
2658 }; 2679 };
2659 2680
2660 2681
2682 class ExternalOneByteString : public String {
2683 public:
2684 virtual int32_t CharAt(intptr_t index) const {
2685 return *CharAddr(index);
2686 }
2687
2688 virtual intptr_t CharSize() const {
2689 return kOneByteChar;
2690 }
2691
2692 static intptr_t InstanceSize() {
2693 return RoundedAllocationSize(sizeof(RawExternalOneByteString));
2694 }
2695
2696 static RawExternalOneByteString* New(const uint8_t* characters,
2697 intptr_t len,
2698 void* peer,
2699 PeerFinalizer callback,
2700 Heap::Space space);
2701
2702 private:
2703 const uint8_t* CharAddr(intptr_t index) const {
2704 // TODO(iposva): Determine if we should throw an exception here.
2705 ASSERT((index >= 0) && (index < Length()));
2706 return &raw_ptr()->external_data_->data_[index];
siva 2011/11/18 23:26:09 I normally put a paren around as follows: return &
cshapiro 2011/11/19 01:08:29 Sure, why not? Fixed here and in the two cases be
2707 }
2708
2709 void SetExternalData(RawExternalStringData<uint8_t>* data) {
2710 raw_ptr()->external_data_ = data;
2711 }
2712
2713 HEAP_OBJECT_IMPLEMENTATION(ExternalOneByteString, String);
2714 friend class Class;
2715 friend class String;
2716 };
2717
2718
2719 class ExternalTwoByteString : public String {
2720 public:
2721 virtual int32_t CharAt(intptr_t index) const {
2722 return *CharAddr(index);
2723 }
2724
2725 virtual intptr_t CharSize() const {
2726 return kTwoByteChar;
2727 }
2728
2729 static intptr_t InstanceSize() {
2730 return RoundedAllocationSize(sizeof(RawExternalTwoByteString));
2731 }
2732
2733 static RawExternalTwoByteString* New(const uint16_t* characters,
2734 intptr_t len,
2735 void* peer,
2736 PeerFinalizer callback,
2737 Heap::Space space = Heap::kNew);
2738
2739 private:
2740 const uint16_t* CharAddr(intptr_t index) const {
2741 // TODO(iposva): Determine if we should throw an exception here.
2742 ASSERT((index >= 0) && (index < Length()));
2743 return &raw_ptr()->external_data_->data_[index];
2744 }
2745
2746 void SetExternalData(RawExternalStringData<uint16_t>* data) {
2747 raw_ptr()->external_data_ = data;
2748 }
2749
2750 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String);
2751 friend class Class;
2752 friend class String;
2753 };
2754
2755
2756 class ExternalFourByteString : public String {
2757 public:
2758 virtual int32_t CharAt(intptr_t index) const {
2759 return *CharAddr(index);
2760 }
2761
2762 virtual intptr_t CharSize() const {
2763 return kFourByteChar;
2764 }
2765
2766 static intptr_t InstanceSize() {
2767 return RoundedAllocationSize(sizeof(RawExternalFourByteString));
2768 }
2769
2770 static RawExternalFourByteString* New(const uint32_t* characters,
2771 intptr_t len,
2772 void* peer,
2773 PeerFinalizer callback,
2774 Heap::Space space = Heap::kNew);
2775
2776 private:
2777 const uint32_t* CharAddr(intptr_t index) const {
2778 // TODO(iposva): Determine if we should throw an exception here.
2779 ASSERT((index >= 0) && (index < Length()));
2780 return &raw_ptr()->external_data_->data_[index];
2781 }
2782
2783 void SetExternalData(RawExternalStringData<uint32_t>* data) {
2784 raw_ptr()->external_data_ = data;
2785 }
2786
2787 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String);
2788 friend class Class;
2789 friend class String;
2790 };
2791
2792
2661 class Bool : public Instance { 2793 class Bool : public Instance {
2662 public: 2794 public:
2663 bool value() const { 2795 bool value() const {
2664 return raw_ptr()->value_; 2796 return raw_ptr()->value_;
2665 } 2797 }
2666 2798
2667 static intptr_t InstanceSize() { 2799 static intptr_t InstanceSize() {
2668 return RoundedAllocationSize(sizeof(RawBool)); 2800 return RoundedAllocationSize(sizeof(RawBool));
2669 } 2801 }
2670 2802
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
2989 } 3121 }
2990 3122
2991 3123
2992 void Context::SetAt(intptr_t index, const Instance& value) const { 3124 void Context::SetAt(intptr_t index, const Instance& value) const {
2993 StorePointer(InstanceAddr(index), value.raw()); 3125 StorePointer(InstanceAddr(index), value.raw());
2994 } 3126 }
2995 3127
2996 } // namespace dart 3128 } // namespace dart
2997 3129
2998 #endif // VM_OBJECT_H_ 3130 #endif // VM_OBJECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698