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

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: Finish native peer support 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 2337 matching lines...) Expand 10 before | Expand all | Expand 10 after
2348 }; 2348 };
2349 2349
2350 2350
2351 // String may not be '\0' terminated. 2351 // String may not be '\0' terminated.
2352 class String : public Instance { 2352 class String : public Instance {
2353 public: 2353 public:
2354 // We use 30 bits for the hash code so that we consistently use a 2354 // We use 30 bits for the hash code so that we consistently use a
2355 // 32bit Smi representation for the hash code on all architectures. 2355 // 32bit Smi representation for the hash code on all architectures.
2356 static const intptr_t kHashBits = 30; 2356 static const intptr_t kHashBits = 30;
2357 2357
2358 static const intptr_t kOneByteChar = 1;
2359 static const intptr_t kTwoByteChar = 2;
2360 static const intptr_t kFourByteChar = 4;
2361
2358 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } 2362 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); }
2359 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } 2363 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); }
2360 2364
2361 virtual intptr_t Hash() const; 2365 virtual intptr_t Hash() const;
2362 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } 2366 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); }
2363 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); 2367 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len);
2364 static intptr_t Hash(const uint8_t* characters, intptr_t len); 2368 static intptr_t Hash(const uint8_t* characters, intptr_t len);
2365 static intptr_t Hash(const uint16_t* characters, intptr_t len); 2369 static intptr_t Hash(const uint16_t* characters, intptr_t len);
2366 static intptr_t Hash(const uint32_t* characters, intptr_t len); 2370 static intptr_t Hash(const uint32_t* characters, intptr_t len);
2367 2371
2368 virtual int32_t CharAt(intptr_t index) const; 2372 virtual int32_t CharAt(intptr_t index) const;
2369 2373
2374 virtual intptr_t CharSize() const;
2375
2370 bool Equals(const String& str, intptr_t begin_index, intptr_t len) const; 2376 bool Equals(const String& str, intptr_t begin_index, intptr_t len) const;
2371 bool Equals(const char* str) const; 2377 bool Equals(const char* str) const;
2372 bool Equals(const uint8_t* characters, intptr_t len) const; 2378 bool Equals(const uint8_t* characters, intptr_t len) const;
2373 bool Equals(const uint16_t* characters, intptr_t len) const; 2379 bool Equals(const uint16_t* characters, intptr_t len) const;
2374 bool Equals(const uint32_t* characters, intptr_t len) const; 2380 bool Equals(const uint32_t* characters, intptr_t len) const;
2375 2381
2376 virtual bool Equals(const Instance& other) const; 2382 virtual bool Equals(const Instance& other) const;
2377 2383
2378 intptr_t CompareTo(const String& other) const; 2384 intptr_t CompareTo(const String& other) const;
2379 2385
2380 bool StartsWith(const String& other) const; 2386 bool StartsWith(const String& other) const;
2381 2387
2382 virtual RawInstance* Canonicalize() const; 2388 virtual RawInstance* Canonicalize() const;
2383 2389
2384 bool IsSymbol() const; 2390 bool IsSymbol() const;
2385 2391
2386 static RawString* New(const char* str, Heap::Space space = Heap::kNew); 2392 static RawString* New(const char* str, Heap::Space space = Heap::kNew);
2387 static RawString* New(const uint8_t* characters, 2393 static RawString* New(const uint8_t* characters,
2388 intptr_t len, 2394 intptr_t len,
2389 Heap::Space space = Heap::kNew); 2395 Heap::Space space = Heap::kNew);
2390 static RawString* New(const uint16_t* characters, 2396 static RawString* New(const uint16_t* characters,
2391 intptr_t len, 2397 intptr_t len,
2392 Heap::Space space = Heap::kNew); 2398 Heap::Space space = Heap::kNew);
2393 static RawString* New(const uint32_t* characters, 2399 static RawString* New(const uint32_t* characters,
2394 intptr_t len, 2400 intptr_t len,
2395 Heap::Space space = Heap::kNew); 2401 Heap::Space space = Heap::kNew);
2396 static RawString* New(const String& str, Heap::Space space = Heap::kNew); 2402 static RawString* New(const String& str, Heap::Space space = Heap::kNew);
2397 2403
2404 static RawString* NewExternal(const uint8_t* characters,
2405 intptr_t len,
2406 void* peer,
2407 Heap::Space = Heap::kNew);
2408 static RawString* NewExternal(const uint16_t* characters,
2409 intptr_t len,
2410 void* peer,
2411 Heap::Space = Heap::kNew);
2412 static RawString* NewExternal(const uint32_t* characters,
2413 intptr_t len,
2414 void* peer,
2415 Heap::Space = Heap::kNew);
2416
2398 static void Copy(const String& dst, 2417 static void Copy(const String& dst,
2399 intptr_t dst_offset, 2418 intptr_t dst_offset,
2400 const uint8_t* characters, 2419 const uint8_t* characters,
2401 intptr_t len); 2420 intptr_t len);
2402 static void Copy(const String& dst, 2421 static void Copy(const String& dst,
2403 intptr_t dst_offset, 2422 intptr_t dst_offset,
2404 const uint16_t* characters, 2423 const uint16_t* characters,
2405 intptr_t len); 2424 intptr_t len);
2406 static void Copy(const String& dst, 2425 static void Copy(const String& dst,
2407 intptr_t dst_offset, 2426 intptr_t dst_offset,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2465 HEAP_OBJECT_IMPLEMENTATION(String, Instance); 2484 HEAP_OBJECT_IMPLEMENTATION(String, Instance);
2466 }; 2485 };
2467 2486
2468 2487
2469 class OneByteString : public String { 2488 class OneByteString : public String {
2470 public: 2489 public:
2471 virtual int32_t CharAt(intptr_t index) const { 2490 virtual int32_t CharAt(intptr_t index) const {
2472 return *CharAddr(index); 2491 return *CharAddr(index);
2473 } 2492 }
2474 2493
2494 virtual intptr_t CharSize() const {
2495 return kOneByteChar;
2496 }
2497
2475 static intptr_t InstanceSize() { 2498 static intptr_t InstanceSize() {
2476 ASSERT(sizeof(RawOneByteString) == OFFSET_OF(RawOneByteString, data_)); 2499 ASSERT(sizeof(RawOneByteString) == OFFSET_OF(RawOneByteString, data_));
2477 return 0; 2500 return 0;
2478 } 2501 }
2479 2502
2480 static intptr_t InstanceSize(intptr_t len) { 2503 static intptr_t InstanceSize(intptr_t len) {
2481 return RoundedAllocationSize(sizeof(RawOneByteString) + len); 2504 return RoundedAllocationSize(sizeof(RawOneByteString) + len);
2482 } 2505 }
2483 2506
2484 static RawOneByteString* New(intptr_t len, 2507 static RawOneByteString* New(intptr_t len,
(...skipping 10 matching lines...) Expand all
2495 static RawOneByteString* New(const OneByteString& str, 2518 static RawOneByteString* New(const OneByteString& str,
2496 Heap::Space space); 2519 Heap::Space space);
2497 2520
2498 static RawOneByteString* Concat(const String& str1, 2521 static RawOneByteString* Concat(const String& str1,
2499 const String& str2, 2522 const String& str2,
2500 Heap::Space space); 2523 Heap::Space space);
2501 static RawOneByteString* ConcatAll(const Array& strings, 2524 static RawOneByteString* ConcatAll(const Array& strings,
2502 intptr_t len, 2525 intptr_t len,
2503 Heap::Space space); 2526 Heap::Space space);
2504 2527
2505 static RawString* SubString(const OneByteString& str,
2506 intptr_t begin_index,
2507 intptr_t length,
2508 Heap::Space space);
2509
2510 static RawOneByteString* Transform(int32_t (*mapping)(int32_t ch), 2528 static RawOneByteString* Transform(int32_t (*mapping)(int32_t ch),
2511 const String& str, 2529 const String& str,
2512 Heap::Space space); 2530 Heap::Space space);
2513 2531
2514 private: 2532 private:
2515 uint8_t* CharAddr(intptr_t index) const { 2533 uint8_t* CharAddr(intptr_t index) const {
2516 // TODO(iposva): Determine if we should throw an exception here. 2534 // TODO(iposva): Determine if we should throw an exception here.
2517 ASSERT((index >= 0) && (index < Length())); 2535 ASSERT((index >= 0) && (index < Length()));
2518 return &raw_ptr()->data_[index]; 2536 return &raw_ptr()->data_[index];
2519 } 2537 }
2520 2538
2521 HEAP_OBJECT_IMPLEMENTATION(OneByteString, String); 2539 HEAP_OBJECT_IMPLEMENTATION(OneByteString, String);
2522 friend class Class; 2540 friend class Class;
2523 friend class String; 2541 friend class String;
2524 }; 2542 };
2525 2543
2526 2544
2527 class TwoByteString : public String { 2545 class TwoByteString : public String {
2528 public: 2546 public:
2529 virtual int32_t CharAt(intptr_t index) const { 2547 virtual int32_t CharAt(intptr_t index) const {
2530 return *CharAddr(index); 2548 return *CharAddr(index);
2531 } 2549 }
2532 2550
2551 virtual intptr_t CharSize() const {
2552 return kTwoByteChar;
2553 }
2554
2533 static intptr_t InstanceSize() { 2555 static intptr_t InstanceSize() {
2534 ASSERT(sizeof(RawTwoByteString) == OFFSET_OF(RawTwoByteString, data_)); 2556 ASSERT(sizeof(RawTwoByteString) == OFFSET_OF(RawTwoByteString, data_));
2535 return 0; 2557 return 0;
2536 } 2558 }
2537 2559
2538 static intptr_t InstanceSize(intptr_t len) { 2560 static intptr_t InstanceSize(intptr_t len) {
2539 return RoundedAllocationSize(sizeof(RawTwoByteString) + (2 * len)); 2561 return RoundedAllocationSize(sizeof(RawTwoByteString) + (2 * len));
2540 } 2562 }
2541 2563
2542 static RawTwoByteString* New(intptr_t len, 2564 static RawTwoByteString* New(intptr_t len,
2543 Heap::Space space); 2565 Heap::Space space);
2544 static RawTwoByteString* New(const uint16_t* characters, 2566 static RawTwoByteString* New(const uint16_t* characters,
2545 intptr_t len, 2567 intptr_t len,
2546 Heap::Space space); 2568 Heap::Space space);
2547 static RawTwoByteString* New(const uint32_t* characters, 2569 static RawTwoByteString* New(const uint32_t* characters,
2548 intptr_t len, 2570 intptr_t len,
2549 Heap::Space space); 2571 Heap::Space space);
2550 static RawTwoByteString* New(const TwoByteString& str, 2572 static RawTwoByteString* New(const TwoByteString& str,
2551 Heap::Space space); 2573 Heap::Space space);
2552 2574
2553 static RawTwoByteString* Concat(const String& str1, 2575 static RawTwoByteString* Concat(const String& str1,
2554 const String& str2, 2576 const String& str2,
2555 Heap::Space space); 2577 Heap::Space space);
2556 static RawTwoByteString* ConcatAll(const Array& strings, 2578 static RawTwoByteString* ConcatAll(const Array& strings,
2557 intptr_t len, 2579 intptr_t len,
2558 Heap::Space space); 2580 Heap::Space space);
2559 2581
2560 static RawString* SubString(const TwoByteString& str,
2561 intptr_t begin_index,
2562 intptr_t length,
2563 Heap::Space space);
2564
2565 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), 2582 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch),
2566 const String& str, 2583 const String& str,
2567 Heap::Space space); 2584 Heap::Space space);
2568 2585
2569 private: 2586 private:
2570 uint16_t* CharAddr(intptr_t index) const { 2587 uint16_t* CharAddr(intptr_t index) const {
2571 ASSERT((index >= 0) && (index < Length())); 2588 ASSERT((index >= 0) && (index < Length()));
2572 return &raw_ptr()->data_[index]; 2589 return &raw_ptr()->data_[index];
2573 } 2590 }
2574 2591
2575 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); 2592 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String);
2576 friend class Class; 2593 friend class Class;
2577 friend class String; 2594 friend class String;
2578 }; 2595 };
2579 2596
2580 2597
2581 class FourByteString : public String { 2598 class FourByteString : public String {
2582 public: 2599 public:
2583 virtual int32_t CharAt(intptr_t index) const { 2600 virtual int32_t CharAt(intptr_t index) const {
2584 return *CharAddr(index); 2601 return *CharAddr(index);
2585 } 2602 }
2586 2603
2604 virtual intptr_t CharSize() const {
2605 return kFourByteChar;
2606 }
2607
2587 static intptr_t InstanceSize() { 2608 static intptr_t InstanceSize() {
2588 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_)); 2609 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_));
2589 return 0; 2610 return 0;
2590 } 2611 }
2591 2612
2592 static intptr_t InstanceSize(intptr_t len) { 2613 static intptr_t InstanceSize(intptr_t len) {
2593 return RoundedAllocationSize(sizeof(RawFourByteString) + (4 * len)); 2614 return RoundedAllocationSize(sizeof(RawFourByteString) + (4 * len));
2594 } 2615 }
2595 2616
2596 static RawFourByteString* New(intptr_t len, 2617 static RawFourByteString* New(intptr_t len,
2597 Heap::Space space); 2618 Heap::Space space);
2598 static RawFourByteString* New(const uint32_t* characters, 2619 static RawFourByteString* New(const uint32_t* characters,
2599 intptr_t len, 2620 intptr_t len,
2600 Heap::Space space); 2621 Heap::Space space);
2601 static RawFourByteString* New(const FourByteString& str, 2622 static RawFourByteString* New(const FourByteString& str,
2602 Heap::Space space); 2623 Heap::Space space);
2603 2624
2604 static RawFourByteString* Concat(const String& str1, 2625 static RawFourByteString* Concat(const String& str1,
2605 const String& str2, 2626 const String& str2,
2606 Heap::Space space); 2627 Heap::Space space);
2607 static RawFourByteString* ConcatAll(const Array& strings, 2628 static RawFourByteString* ConcatAll(const Array& strings,
2608 intptr_t len, 2629 intptr_t len,
2609 Heap::Space space); 2630 Heap::Space space);
2610 2631
2611 static RawString* SubString(const FourByteString& str,
2612 intptr_t begin_index,
2613 intptr_t length,
2614 Heap::Space space);
2615
2616 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch), 2632 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch),
2617 const String& str, 2633 const String& str,
2618 Heap::Space space); 2634 Heap::Space space);
2619 2635
2620 private: 2636 private:
2621 uint32_t* CharAddr(intptr_t index) const { 2637 uint32_t* CharAddr(intptr_t index) const {
2622 ASSERT((index >= 0) && (index < Length())); 2638 ASSERT((index >= 0) && (index < Length()));
2623 return &raw_ptr()->data_[index]; 2639 return &raw_ptr()->data_[index];
2624 } 2640 }
2625 2641
2626 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String); 2642 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String);
2627 friend class Class; 2643 friend class Class;
2628 friend class String; 2644 friend class String;
2629 }; 2645 };
2630 2646
2631 2647
2648 class ExternalOneByteString : public String {
2649 public:
2650 virtual int32_t CharAt(intptr_t index) const {
2651 return *CharAddr(index);
2652 }
2653
2654 virtual intptr_t CharSize() const {
2655 return kOneByteChar;
2656 }
2657
2658 void* Peer() const {
2659 return raw_ptr()->peer_;
2660 }
2661
2662 static intptr_t InstanceSize() {
2663 return RoundedAllocationSize(sizeof(RawExternalOneByteString));
2664 }
2665
2666 static RawExternalOneByteString* New(const uint8_t* characters,
2667 intptr_t len,
2668 void* peer,
2669 Heap::Space space);
2670
2671 private:
2672 uint8_t* CharAddr(intptr_t index) const {
2673 // TODO(iposva): Determine if we should throw an exception here.
2674 ASSERT((index >= 0) && (index < Length()));
2675 return &raw_ptr()->data_[index];
2676 }
2677
2678 void SetData(const uint8_t* characters) {
2679 raw_ptr()->data_ = const_cast<uint8_t*>(characters);
2680 }
2681
2682 void SetPeer(void* peer) {
2683 raw_ptr()->peer_ = peer;
2684 }
2685
2686 HEAP_OBJECT_IMPLEMENTATION(ExternalOneByteString, String);
2687 friend class Class;
2688 friend class String;
2689 };
2690
2691
2692 class ExternalTwoByteString : public String {
2693 public:
2694 virtual int32_t CharAt(intptr_t index) const {
2695 return *CharAddr(index);
2696 }
2697
2698 virtual intptr_t CharSize() const {
2699 return kTwoByteChar;
2700 }
2701
2702 static intptr_t InstanceSize() {
2703 return RoundedAllocationSize(sizeof(RawExternalTwoByteString));
2704 }
2705
2706 void* Peer() const {
2707 return raw_ptr()->peer_;
2708 }
2709
2710 static RawExternalTwoByteString* New(const uint16_t* characters,
2711 intptr_t len,
2712 void* peer,
2713 Heap::Space space = Heap::kNew);
2714
2715 private:
2716 uint16_t* CharAddr(intptr_t index) const {
2717 // TODO(iposva): Determine if we should throw an exception here.
2718 ASSERT((index >= 0) && (index < Length()));
2719 return &raw_ptr()->data_[index];
2720 }
2721
2722 void SetData(const uint16_t* characters) {
2723 raw_ptr()->data_ = const_cast<uint16_t*>(characters);
2724 }
2725
2726 void SetPeer(void* peer) {
2727 raw_ptr()->peer_ = peer;
2728 }
2729
2730 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String);
2731 friend class Class;
2732 friend class String;
2733 };
2734
2735
2736 class ExternalFourByteString : public String {
2737 public:
2738 virtual int32_t CharAt(intptr_t index) const {
2739 return *CharAddr(index);
2740 }
2741
2742 virtual intptr_t CharSize() const {
2743 return kFourByteChar;
2744 }
2745
2746 void* Peer() const {
2747 return raw_ptr()->peer_;
2748 }
2749
2750 static intptr_t InstanceSize() {
2751 return RoundedAllocationSize(sizeof(RawExternalFourByteString));
2752 }
2753
2754 static RawExternalFourByteString* New(const uint32_t* characters,
2755 intptr_t len,
2756 void* peer,
2757 Heap::Space space = Heap::kNew);
2758
2759 private:
2760 uint32_t* CharAddr(intptr_t index) const {
2761 // TODO(iposva): Determine if we should throw an exception here.
2762 ASSERT((index >= 0) && (index < Length()));
2763 return &raw_ptr()->data_[index];
2764 }
2765
2766 void SetData(const uint32_t* characters) {
2767 raw_ptr()->data_ = const_cast<uint32_t*>(characters);
2768 }
2769
2770 void SetPeer(void* peer) {
2771 raw_ptr()->peer_ = peer;
2772 }
2773
2774 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String);
2775 friend class Class;
2776 friend class String;
2777 };
2778
2779
2632 class Bool : public Instance { 2780 class Bool : public Instance {
2633 public: 2781 public:
2634 bool value() const { 2782 bool value() const {
2635 return raw_ptr()->value_; 2783 return raw_ptr()->value_;
2636 } 2784 }
2637 2785
2638 static intptr_t InstanceSize() { 2786 static intptr_t InstanceSize() {
2639 return RoundedAllocationSize(sizeof(RawBool)); 2787 return RoundedAllocationSize(sizeof(RawBool));
2640 } 2788 }
2641 2789
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
2960 } 3108 }
2961 3109
2962 3110
2963 void Context::SetAt(intptr_t index, const Instance& value) const { 3111 void Context::SetAt(intptr_t index, const Instance& value) const {
2964 StorePointer(InstanceAddr(index), value.raw()); 3112 StorePointer(InstanceAddr(index), value.raw());
2965 } 3113 }
2966 3114
2967 } // namespace dart 3115 } // namespace dart
2968 3116
2969 #endif // VM_OBJECT_H_ 3117 #endif // VM_OBJECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698