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

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: Address final review comments 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
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/object.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 2403 matching lines...) Expand 10 before | Expand all | Expand 10 after
2414 void set_value(double value) const; 2414 void set_value(double value) const;
2415 2415
2416 HEAP_OBJECT_IMPLEMENTATION(Double, Number); 2416 HEAP_OBJECT_IMPLEMENTATION(Double, Number);
2417 friend class Class; 2417 friend class Class;
2418 }; 2418 };
2419 2419
2420 2420
2421 // String may not be '\0' terminated. 2421 // String may not be '\0' terminated.
2422 class String : public Instance { 2422 class String : public Instance {
2423 public: 2423 public:
2424 typedef void (*PeerFinalizer)(void *peer);
2425
2424 // We use 30 bits for the hash code so that we consistently use a 2426 // We use 30 bits for the hash code so that we consistently use a
2425 // 32bit Smi representation for the hash code on all architectures. 2427 // 32bit Smi representation for the hash code on all architectures.
2426 static const intptr_t kHashBits = 30; 2428 static const intptr_t kHashBits = 30;
2427 2429
2430 static const intptr_t kOneByteChar = 1;
2431 static const intptr_t kTwoByteChar = 2;
2432 static const intptr_t kFourByteChar = 4;
2433
2428 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } 2434 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); }
2429 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } 2435 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); }
2430 2436
2431 virtual intptr_t Hash() const; 2437 virtual intptr_t Hash() const;
2432 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } 2438 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); }
2433 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); 2439 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len);
2434 static intptr_t Hash(const uint8_t* characters, intptr_t len); 2440 static intptr_t Hash(const uint8_t* characters, intptr_t len);
2435 static intptr_t Hash(const uint16_t* characters, intptr_t len); 2441 static intptr_t Hash(const uint16_t* characters, intptr_t len);
2436 static intptr_t Hash(const uint32_t* characters, intptr_t len); 2442 static intptr_t Hash(const uint32_t* characters, intptr_t len);
2437 2443
2438 virtual int32_t CharAt(intptr_t index) const; 2444 virtual int32_t CharAt(intptr_t index) const;
2439 2445
2446 virtual intptr_t CharSize() const;
2447
2440 bool Equals(const String& str, intptr_t begin_index, intptr_t len) const; 2448 bool Equals(const String& str, intptr_t begin_index, intptr_t len) const;
2441 bool Equals(const char* str) const; 2449 bool Equals(const char* str) const;
2442 bool Equals(const uint8_t* characters, intptr_t len) const; 2450 bool Equals(const uint8_t* characters, intptr_t len) const;
2443 bool Equals(const uint16_t* characters, intptr_t len) const; 2451 bool Equals(const uint16_t* characters, intptr_t len) const;
2444 bool Equals(const uint32_t* characters, intptr_t len) const; 2452 bool Equals(const uint32_t* characters, intptr_t len) const;
2445 2453
2446 virtual bool Equals(const Instance& other) const; 2454 virtual bool Equals(const Instance& other) const;
2447 2455
2448 intptr_t CompareTo(const String& other) const; 2456 intptr_t CompareTo(const String& other) const;
2449 2457
2450 bool StartsWith(const String& other) const; 2458 bool StartsWith(const String& other) const;
2451 2459
2452 virtual RawInstance* Canonicalize() const; 2460 virtual RawInstance* Canonicalize() const;
2453 2461
2454 bool IsSymbol() const; 2462 bool IsSymbol() const;
2455 2463
2456 static RawString* New(const char* str, Heap::Space space = Heap::kNew); 2464 static RawString* New(const char* str, Heap::Space space = Heap::kNew);
2457 static RawString* New(const uint8_t* characters, 2465 static RawString* New(const uint8_t* characters,
2458 intptr_t len, 2466 intptr_t len,
2459 Heap::Space space = Heap::kNew); 2467 Heap::Space space = Heap::kNew);
2460 static RawString* New(const uint16_t* characters, 2468 static RawString* New(const uint16_t* characters,
2461 intptr_t len, 2469 intptr_t len,
2462 Heap::Space space = Heap::kNew); 2470 Heap::Space space = Heap::kNew);
2463 static RawString* New(const uint32_t* characters, 2471 static RawString* New(const uint32_t* characters,
2464 intptr_t len, 2472 intptr_t len,
2465 Heap::Space space = Heap::kNew); 2473 Heap::Space space = Heap::kNew);
2466 static RawString* New(const String& str, Heap::Space space = Heap::kNew); 2474 static RawString* New(const String& str, Heap::Space space = Heap::kNew);
2467 2475
2476 static RawString* NewExternal(const uint8_t* characters,
2477 intptr_t len,
2478 void* peer,
2479 PeerFinalizer callback,
2480 Heap::Space = Heap::kNew);
2481 static RawString* NewExternal(const uint16_t* characters,
2482 intptr_t len,
2483 void* peer,
2484 PeerFinalizer callback,
2485 Heap::Space = Heap::kNew);
2486 static RawString* NewExternal(const uint32_t* characters,
2487 intptr_t len,
2488 void* peer,
2489 PeerFinalizer callback,
2490 Heap::Space = Heap::kNew);
2491
2468 static void Copy(const String& dst, 2492 static void Copy(const String& dst,
2469 intptr_t dst_offset, 2493 intptr_t dst_offset,
2470 const uint8_t* characters, 2494 const uint8_t* characters,
2471 intptr_t len); 2495 intptr_t len);
2472 static void Copy(const String& dst, 2496 static void Copy(const String& dst,
2473 intptr_t dst_offset, 2497 intptr_t dst_offset,
2474 const uint16_t* characters, 2498 const uint16_t* characters,
2475 intptr_t len); 2499 intptr_t len);
2476 static void Copy(const String& dst, 2500 static void Copy(const String& dst,
2477 intptr_t dst_offset, 2501 intptr_t dst_offset,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2540 HEAP_OBJECT_IMPLEMENTATION(String, Instance); 2564 HEAP_OBJECT_IMPLEMENTATION(String, Instance);
2541 }; 2565 };
2542 2566
2543 2567
2544 class OneByteString : public String { 2568 class OneByteString : public String {
2545 public: 2569 public:
2546 virtual int32_t CharAt(intptr_t index) const { 2570 virtual int32_t CharAt(intptr_t index) const {
2547 return *CharAddr(index); 2571 return *CharAddr(index);
2548 } 2572 }
2549 2573
2574 virtual intptr_t CharSize() const {
2575 return kOneByteChar;
2576 }
2577
2550 static intptr_t data_offset() { return OFFSET_OF(RawOneByteString, data_); } 2578 static intptr_t data_offset() { return OFFSET_OF(RawOneByteString, data_); }
2551 2579
2552 static intptr_t InstanceSize() { 2580 static intptr_t InstanceSize() {
2553 ASSERT(sizeof(RawOneByteString) == OFFSET_OF(RawOneByteString, data_)); 2581 ASSERT(sizeof(RawOneByteString) == OFFSET_OF(RawOneByteString, data_));
2554 return 0; 2582 return 0;
2555 } 2583 }
2556 2584
2557 static intptr_t InstanceSize(intptr_t len) { 2585 static intptr_t InstanceSize(intptr_t len) {
2558 return RoundedAllocationSize(sizeof(RawOneByteString) + len); 2586 return RoundedAllocationSize(sizeof(RawOneByteString) + len);
2559 } 2587 }
(...skipping 12 matching lines...) Expand all
2572 static RawOneByteString* New(const OneByteString& str, 2600 static RawOneByteString* New(const OneByteString& str,
2573 Heap::Space space); 2601 Heap::Space space);
2574 2602
2575 static RawOneByteString* Concat(const String& str1, 2603 static RawOneByteString* Concat(const String& str1,
2576 const String& str2, 2604 const String& str2,
2577 Heap::Space space); 2605 Heap::Space space);
2578 static RawOneByteString* ConcatAll(const Array& strings, 2606 static RawOneByteString* ConcatAll(const Array& strings,
2579 intptr_t len, 2607 intptr_t len,
2580 Heap::Space space); 2608 Heap::Space space);
2581 2609
2582 static RawString* SubString(const OneByteString& str,
2583 intptr_t begin_index,
2584 intptr_t length,
2585 Heap::Space space);
2586
2587 static RawOneByteString* Transform(int32_t (*mapping)(int32_t ch), 2610 static RawOneByteString* Transform(int32_t (*mapping)(int32_t ch),
2588 const String& str, 2611 const String& str,
2589 Heap::Space space); 2612 Heap::Space space);
2590 2613
2591 private: 2614 private:
2592 uint8_t* CharAddr(intptr_t index) const { 2615 uint8_t* CharAddr(intptr_t index) const {
2593 // TODO(iposva): Determine if we should throw an exception here. 2616 // TODO(iposva): Determine if we should throw an exception here.
2594 ASSERT((index >= 0) && (index < Length())); 2617 ASSERT((index >= 0) && (index < Length()));
2595 return &raw_ptr()->data_[index]; 2618 return &raw_ptr()->data_[index];
2596 } 2619 }
2597 2620
2598 HEAP_OBJECT_IMPLEMENTATION(OneByteString, String); 2621 HEAP_OBJECT_IMPLEMENTATION(OneByteString, String);
2599 friend class Class; 2622 friend class Class;
2600 friend class String; 2623 friend class String;
2601 }; 2624 };
2602 2625
2603 2626
2604 class TwoByteString : public String { 2627 class TwoByteString : public String {
2605 public: 2628 public:
2606 virtual int32_t CharAt(intptr_t index) const { 2629 virtual int32_t CharAt(intptr_t index) const {
2607 return *CharAddr(index); 2630 return *CharAddr(index);
2608 } 2631 }
2609 2632
2633 virtual intptr_t CharSize() const {
2634 return kTwoByteChar;
2635 }
2636
2610 static intptr_t InstanceSize() { 2637 static intptr_t InstanceSize() {
2611 ASSERT(sizeof(RawTwoByteString) == OFFSET_OF(RawTwoByteString, data_)); 2638 ASSERT(sizeof(RawTwoByteString) == OFFSET_OF(RawTwoByteString, data_));
2612 return 0; 2639 return 0;
2613 } 2640 }
2614 2641
2615 static intptr_t InstanceSize(intptr_t len) { 2642 static intptr_t InstanceSize(intptr_t len) {
2616 return RoundedAllocationSize(sizeof(RawTwoByteString) + (2 * len)); 2643 return RoundedAllocationSize(sizeof(RawTwoByteString) + (2 * len));
2617 } 2644 }
2618 2645
2619 static RawTwoByteString* New(intptr_t len, 2646 static RawTwoByteString* New(intptr_t len,
2620 Heap::Space space); 2647 Heap::Space space);
2621 static RawTwoByteString* New(const uint16_t* characters, 2648 static RawTwoByteString* New(const uint16_t* characters,
2622 intptr_t len, 2649 intptr_t len,
2623 Heap::Space space); 2650 Heap::Space space);
2624 static RawTwoByteString* New(const uint32_t* characters, 2651 static RawTwoByteString* New(const uint32_t* characters,
2625 intptr_t len, 2652 intptr_t len,
2626 Heap::Space space); 2653 Heap::Space space);
2627 static RawTwoByteString* New(const TwoByteString& str, 2654 static RawTwoByteString* New(const TwoByteString& str,
2628 Heap::Space space); 2655 Heap::Space space);
2629 2656
2630 static RawTwoByteString* Concat(const String& str1, 2657 static RawTwoByteString* Concat(const String& str1,
2631 const String& str2, 2658 const String& str2,
2632 Heap::Space space); 2659 Heap::Space space);
2633 static RawTwoByteString* ConcatAll(const Array& strings, 2660 static RawTwoByteString* ConcatAll(const Array& strings,
2634 intptr_t len, 2661 intptr_t len,
2635 Heap::Space space); 2662 Heap::Space space);
2636 2663
2637 static RawString* SubString(const TwoByteString& str,
2638 intptr_t begin_index,
2639 intptr_t length,
2640 Heap::Space space);
2641
2642 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), 2664 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch),
2643 const String& str, 2665 const String& str,
2644 Heap::Space space); 2666 Heap::Space space);
2645 2667
2646 private: 2668 private:
2647 uint16_t* CharAddr(intptr_t index) const { 2669 uint16_t* CharAddr(intptr_t index) const {
2648 ASSERT((index >= 0) && (index < Length())); 2670 ASSERT((index >= 0) && (index < Length()));
2649 return &raw_ptr()->data_[index]; 2671 return &raw_ptr()->data_[index];
2650 } 2672 }
2651 2673
2652 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); 2674 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String);
2653 friend class Class; 2675 friend class Class;
2654 friend class String; 2676 friend class String;
2655 }; 2677 };
2656 2678
2657 2679
2658 class FourByteString : public String { 2680 class FourByteString : public String {
2659 public: 2681 public:
2660 virtual int32_t CharAt(intptr_t index) const { 2682 virtual int32_t CharAt(intptr_t index) const {
2661 return *CharAddr(index); 2683 return *CharAddr(index);
2662 } 2684 }
2663 2685
2686 virtual intptr_t CharSize() const {
2687 return kFourByteChar;
2688 }
2689
2664 static intptr_t InstanceSize() { 2690 static intptr_t InstanceSize() {
2665 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_)); 2691 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_));
2666 return 0; 2692 return 0;
2667 } 2693 }
2668 2694
2669 static intptr_t InstanceSize(intptr_t len) { 2695 static intptr_t InstanceSize(intptr_t len) {
2670 return RoundedAllocationSize(sizeof(RawFourByteString) + (4 * len)); 2696 return RoundedAllocationSize(sizeof(RawFourByteString) + (4 * len));
2671 } 2697 }
2672 2698
2673 static RawFourByteString* New(intptr_t len, 2699 static RawFourByteString* New(intptr_t len,
2674 Heap::Space space); 2700 Heap::Space space);
2675 static RawFourByteString* New(const uint32_t* characters, 2701 static RawFourByteString* New(const uint32_t* characters,
2676 intptr_t len, 2702 intptr_t len,
2677 Heap::Space space); 2703 Heap::Space space);
2678 static RawFourByteString* New(const FourByteString& str, 2704 static RawFourByteString* New(const FourByteString& str,
2679 Heap::Space space); 2705 Heap::Space space);
2680 2706
2681 static RawFourByteString* Concat(const String& str1, 2707 static RawFourByteString* Concat(const String& str1,
2682 const String& str2, 2708 const String& str2,
2683 Heap::Space space); 2709 Heap::Space space);
2684 static RawFourByteString* ConcatAll(const Array& strings, 2710 static RawFourByteString* ConcatAll(const Array& strings,
2685 intptr_t len, 2711 intptr_t len,
2686 Heap::Space space); 2712 Heap::Space space);
2687 2713
2688 static RawString* SubString(const FourByteString& str,
2689 intptr_t begin_index,
2690 intptr_t length,
2691 Heap::Space space);
2692
2693 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch), 2714 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch),
2694 const String& str, 2715 const String& str,
2695 Heap::Space space); 2716 Heap::Space space);
2696 2717
2697 private: 2718 private:
2698 uint32_t* CharAddr(intptr_t index) const { 2719 uint32_t* CharAddr(intptr_t index) const {
2699 ASSERT((index >= 0) && (index < Length())); 2720 ASSERT((index >= 0) && (index < Length()));
2700 return &raw_ptr()->data_[index]; 2721 return &raw_ptr()->data_[index];
2701 } 2722 }
2702 2723
2703 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String); 2724 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String);
2704 friend class Class; 2725 friend class Class;
2705 friend class String; 2726 friend class String;
2706 }; 2727 };
2707 2728
2708 2729
2730 class ExternalOneByteString : public String {
2731 public:
2732 virtual int32_t CharAt(intptr_t index) const {
2733 return *CharAddr(index);
2734 }
2735
2736 virtual intptr_t CharSize() const {
2737 return kOneByteChar;
2738 }
2739
2740 static intptr_t InstanceSize() {
2741 return RoundedAllocationSize(sizeof(RawExternalOneByteString));
2742 }
2743
2744 static RawExternalOneByteString* New(const uint8_t* characters,
2745 intptr_t len,
2746 void* peer,
2747 PeerFinalizer callback,
2748 Heap::Space space);
2749
2750 private:
2751 const uint8_t* CharAddr(intptr_t index) const {
2752 // TODO(iposva): Determine if we should throw an exception here.
2753 ASSERT((index >= 0) && (index < Length()));
2754 return &(raw_ptr()->external_data_->data_[index]);
2755 }
2756
2757 void SetExternalData(ExternalStringData<uint8_t>* data) {
2758 raw_ptr()->external_data_ = data;
2759 }
2760
2761 HEAP_OBJECT_IMPLEMENTATION(ExternalOneByteString, String);
2762 friend class Class;
2763 friend class String;
2764 };
2765
2766
2767 class ExternalTwoByteString : public String {
2768 public:
2769 virtual int32_t CharAt(intptr_t index) const {
2770 return *CharAddr(index);
2771 }
2772
2773 virtual intptr_t CharSize() const {
2774 return kTwoByteChar;
2775 }
2776
2777 static intptr_t InstanceSize() {
2778 return RoundedAllocationSize(sizeof(RawExternalTwoByteString));
2779 }
2780
2781 static RawExternalTwoByteString* New(const uint16_t* characters,
2782 intptr_t len,
2783 void* peer,
2784 PeerFinalizer callback,
2785 Heap::Space space = Heap::kNew);
2786
2787 private:
2788 const uint16_t* CharAddr(intptr_t index) const {
2789 // TODO(iposva): Determine if we should throw an exception here.
2790 ASSERT((index >= 0) && (index < Length()));
2791 return &(raw_ptr()->external_data_->data_[index]);
2792 }
2793
2794 void SetExternalData(ExternalStringData<uint16_t>* data) {
2795 raw_ptr()->external_data_ = data;
2796 }
2797
2798 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String);
2799 friend class Class;
2800 friend class String;
2801 };
2802
2803
2804 class ExternalFourByteString : public String {
2805 public:
2806 virtual int32_t CharAt(intptr_t index) const {
2807 return *CharAddr(index);
2808 }
2809
2810 virtual intptr_t CharSize() const {
2811 return kFourByteChar;
2812 }
2813
2814 static intptr_t InstanceSize() {
2815 return RoundedAllocationSize(sizeof(RawExternalFourByteString));
2816 }
2817
2818 static RawExternalFourByteString* New(const uint32_t* characters,
2819 intptr_t len,
2820 void* peer,
2821 PeerFinalizer callback,
2822 Heap::Space space = Heap::kNew);
2823
2824 private:
2825 const uint32_t* CharAddr(intptr_t index) const {
2826 // TODO(iposva): Determine if we should throw an exception here.
2827 ASSERT((index >= 0) && (index < Length()));
2828 return &(raw_ptr()->external_data_->data_[index]);
2829 }
2830
2831 void SetExternalData(ExternalStringData<uint32_t>* data) {
2832 raw_ptr()->external_data_ = data;
2833 }
2834
2835 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String);
2836 friend class Class;
2837 friend class String;
2838 };
2839
2840
2709 class Bool : public Instance { 2841 class Bool : public Instance {
2710 public: 2842 public:
2711 bool value() const { 2843 bool value() const {
2712 return raw_ptr()->value_; 2844 return raw_ptr()->value_;
2713 } 2845 }
2714 2846
2715 static intptr_t InstanceSize() { 2847 static intptr_t InstanceSize() {
2716 return RoundedAllocationSize(sizeof(RawBool)); 2848 return RoundedAllocationSize(sizeof(RawBool));
2717 } 2849 }
2718 2850
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
3101 } 3233 }
3102 3234
3103 3235
3104 void Context::SetAt(intptr_t index, const Instance& value) const { 3236 void Context::SetAt(intptr_t index, const Instance& value) const {
3105 StorePointer(InstanceAddr(index), value.raw()); 3237 StorePointer(InstanceAddr(index), value.raw());
3106 } 3238 }
3107 3239
3108 } // namespace dart 3240 } // namespace dart
3109 3241
3110 #endif // VM_OBJECT_H_ 3242 #endif // VM_OBJECT_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698