Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 559 Member() : m_raw(nullptr) | 559 Member() : m_raw(nullptr) |
| 560 { | 560 { |
| 561 } | 561 } |
| 562 | 562 |
| 563 Member(std::nullptr_t) : m_raw(nullptr) | 563 Member(std::nullptr_t) : m_raw(nullptr) |
| 564 { | 564 { |
| 565 } | 565 } |
| 566 | 566 |
| 567 Member(T* raw) : m_raw(raw) | 567 Member(T* raw) : m_raw(raw) |
| 568 { | 568 { |
| 569 // HashTable can store a special value to Member<> to represent | 569 checkPointer(); |
| 570 // a deleted entry. The special value is not aligned to the allocation | |
| 571 // granularity. The following ASSERT is checking that m_raw is either of : | |
| 572 // - nullptr | |
| 573 // - a special value to represent a deleted entry of a HashTable | |
| 574 // - a valid pointer to the heap | |
| 575 ASSERT(!m_raw || reinterpret_cast<intptr_t>(m_raw) % allocationGranulari ty || Heap::findPageFromAddress(m_raw)); | |
| 576 } | 570 } |
| 577 | 571 |
| 578 explicit Member(T& raw) : m_raw(&raw) | 572 explicit Member(T& raw) : m_raw(&raw) |
| 579 { | 573 { |
| 580 // See the comment above. | 574 checkPointer(); |
| 581 ASSERT(!m_raw || reinterpret_cast<intptr_t>(m_raw) % allocationGranulari ty || Heap::findPageFromAddress(m_raw)); | |
| 582 } | 575 } |
| 583 | 576 |
| 584 template<typename U> | 577 template<typename U> |
| 585 Member(const RawPtr<U>& other) : m_raw(other.get()) | 578 Member(const RawPtr<U>& other) : m_raw(other.get()) |
| 586 { | 579 { |
| 580 checkPointer(); | |
| 587 } | 581 } |
| 588 | 582 |
| 589 Member(WTF::HashTableDeletedValueType) : m_raw(reinterpret_cast<T*>(-1)) | 583 Member(WTF::HashTableDeletedValueType) : m_raw(reinterpret_cast<T*>(-1)) |
| 590 { | 584 { |
| 591 } | 585 } |
| 592 | 586 |
| 593 bool isHashTableDeletedValue() const { return m_raw == reinterpret_cast<T*>( -1); } | 587 bool isHashTableDeletedValue() const { return m_raw == reinterpret_cast<T*>( -1); } |
| 594 | 588 |
| 595 template<typename U> | 589 template<typename U> |
| 596 Member(const Persistent<U>& other) : m_raw(other) { } | 590 Member(const Persistent<U>& other) : m_raw(other) |
| 591 { | |
| 592 checkPointer(); | |
| 593 } | |
| 597 | 594 |
| 598 Member(const Member& other) : m_raw(other) { } | 595 Member(const Member& other) : m_raw(other) |
| 596 { | |
| 597 checkPointer(); | |
| 598 } | |
| 599 | 599 |
| 600 template<typename U> | 600 template<typename U> |
| 601 Member(const Member<U>& other) : m_raw(other) { } | 601 Member(const Member<U>& other) : m_raw(other) |
| 602 { | |
| 603 checkPointer(); | |
| 604 } | |
| 602 | 605 |
| 603 T* release() | 606 T* release() |
| 604 { | 607 { |
| 605 T* result = m_raw; | 608 T* result = m_raw; |
| 606 m_raw = nullptr; | 609 m_raw = nullptr; |
| 607 return result; | 610 return result; |
| 608 } | 611 } |
| 609 | 612 |
| 610 bool operator!() const { return !m_raw; } | 613 bool operator!() const { return !m_raw; } |
| 611 | 614 |
| 612 operator T*() const { return m_raw; } | 615 operator T*() const { return m_raw; } |
| 613 | 616 |
| 614 T* operator->() const { return m_raw; } | 617 T* operator->() const { return m_raw; } |
| 615 T& operator*() const { return *m_raw; } | 618 T& operator*() const { return *m_raw; } |
| 616 template<typename U> | 619 template<typename U> |
| 617 operator RawPtr<U>() const { return m_raw; } | 620 operator RawPtr<U>() const { return m_raw; } |
| 618 | 621 |
| 619 template<typename U> | 622 template<typename U> |
| 620 Member& operator=(const Persistent<U>& other) | 623 Member& operator=(const Persistent<U>& other) |
| 621 { | 624 { |
| 622 m_raw = other; | 625 m_raw = other; |
| 626 checkPointer(); | |
| 623 return *this; | 627 return *this; |
| 624 } | 628 } |
| 625 | 629 |
| 626 template<typename U> | 630 template<typename U> |
| 627 Member& operator=(const Member<U>& other) | 631 Member& operator=(const Member<U>& other) |
| 628 { | 632 { |
| 629 m_raw = other; | 633 m_raw = other; |
| 634 checkPointer(); | |
| 630 return *this; | 635 return *this; |
| 631 } | 636 } |
| 632 | 637 |
| 633 template<typename U> | 638 template<typename U> |
| 634 Member& operator=(U* other) | 639 Member& operator=(U* other) |
| 635 { | 640 { |
| 636 m_raw = other; | 641 m_raw = other; |
| 642 checkPointer(); | |
| 637 return *this; | 643 return *this; |
| 638 } | 644 } |
| 639 | 645 |
| 640 template<typename U> | 646 template<typename U> |
| 641 Member& operator=(RawPtr<U> other) | 647 Member& operator=(RawPtr<U> other) |
| 642 { | 648 { |
| 643 m_raw = other; | 649 m_raw = other; |
| 650 checkPointer(); | |
| 644 return *this; | 651 return *this; |
| 645 } | 652 } |
| 646 | 653 |
| 647 Member& operator=(std::nullptr_t) | 654 Member& operator=(std::nullptr_t) |
| 648 { | 655 { |
| 649 m_raw = nullptr; | 656 m_raw = nullptr; |
| 650 return *this; | 657 return *this; |
| 651 } | 658 } |
| 652 | 659 |
| 653 void swap(Member<T>& other) { std::swap(m_raw, other.m_raw); } | 660 void swap(Member<T>& other) |
| 661 { | |
| 662 std::swap(m_raw, other.m_raw); | |
| 663 checkPointer(); | |
| 664 } | |
| 654 | 665 |
| 655 T* get() const { return m_raw; } | 666 T* get() const { return m_raw; } |
| 656 | 667 |
| 657 void clear() { m_raw = nullptr; } | 668 void clear() { m_raw = nullptr; } |
| 658 | 669 |
| 659 | 670 |
| 660 protected: | 671 protected: |
| 672 void checkPointer() | |
| 673 { | |
| 674 if (!m_raw) | |
| 675 return; | |
| 676 // HashTable can store a special value (which is not aligned to the | |
| 677 // allocation granularity) to Member<> to represent a deleted entry. | |
| 678 // Thus we treat a pointer that is not aligned to the granularity | |
| 679 // as a valid pointer. | |
| 680 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity) | |
| 681 return; | |
| 682 | |
| 683 // TODO(haraken): What we really want to check here is that the pointer | |
|
haraken
2015/05/26 13:27:53
I welcome any idea to fix this TODO.
| |
| 684 // is a traceable object. In other words, the pointer is either of: | |
| 685 // | |
| 686 // (a) a pointer to the head of an on-heap object. | |
| 687 // (b) a pointer to the head of an on-heap mixin object. | |
| 688 // | |
| 689 // We can check it by calling visitor->isHeapObjectAlive(m_raw), | |
| 690 // but we cannot call it here because it requres to include T.h. | |
| 691 // So we currently implement only the check for (a). | |
| 692 if (!IsGarbageCollectedMixin<T>::value) | |
| 693 HeapObjectHeader::fromPayload(m_raw)->checkHeader(); | |
| 694 } | |
| 695 | |
| 661 T* m_raw; | 696 T* m_raw; |
| 662 | 697 |
| 663 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait; | 698 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait; |
| 664 friend class Visitor; | 699 friend class Visitor; |
| 700 | |
| 665 }; | 701 }; |
| 666 | 702 |
| 667 // WeakMember is similar to Member in that it is used to point to other oilpan | 703 // WeakMember is similar to Member in that it is used to point to other oilpan |
| 668 // heap allocated objects. | 704 // heap allocated objects. |
| 669 // However instead of creating a strong pointer to the object, the WeakMember cr eates | 705 // However instead of creating a strong pointer to the object, the WeakMember cr eates |
| 670 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to | 706 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to |
| 671 // to a heap allocated object are weak the object will be garbage collected. At the | 707 // to a heap allocated object are weak the object will be garbage collected. At the |
| 672 // time of GC the weak pointers will automatically be set to null. | 708 // time of GC the weak pointers will automatically be set to null. |
| 673 template<typename T> | 709 template<typename T> |
| 674 class WeakMember : public Member<T> { | 710 class WeakMember : public Member<T> { |
| 675 public: | 711 public: |
| 676 WeakMember() : Member<T>() { } | 712 WeakMember() : Member<T>() { } |
| 677 | 713 |
| 678 WeakMember(std::nullptr_t) : Member<T>(nullptr) { } | 714 WeakMember(std::nullptr_t) : Member<T>(nullptr) { } |
| 679 | 715 |
| 680 WeakMember(T* raw) : Member<T>(raw) { } | 716 WeakMember(T* raw) : Member<T>(raw) { } |
| 681 | 717 |
| 682 WeakMember(WTF::HashTableDeletedValueType x) : Member<T>(x) { } | 718 WeakMember(WTF::HashTableDeletedValueType x) : Member<T>(x) { } |
| 683 | 719 |
| 684 template<typename U> | 720 template<typename U> |
| 685 WeakMember(const Persistent<U>& other) : Member<T>(other) { } | 721 WeakMember(const Persistent<U>& other) : Member<T>(other) { } |
| 686 | 722 |
| 687 template<typename U> | 723 template<typename U> |
| 688 WeakMember(const Member<U>& other) : Member<T>(other) { } | 724 WeakMember(const Member<U>& other) : Member<T>(other) { } |
| 689 | 725 |
| 690 template<typename U> | 726 template<typename U> |
| 691 WeakMember& operator=(const Persistent<U>& other) | 727 WeakMember& operator=(const Persistent<U>& other) |
| 692 { | 728 { |
| 693 this->m_raw = other; | 729 this->m_raw = other; |
| 730 this->checkPointer(); | |
| 694 return *this; | 731 return *this; |
| 695 } | 732 } |
| 696 | 733 |
| 697 template<typename U> | 734 template<typename U> |
| 698 WeakMember& operator=(const Member<U>& other) | 735 WeakMember& operator=(const Member<U>& other) |
| 699 { | 736 { |
| 700 this->m_raw = other; | 737 this->m_raw = other; |
| 738 this->checkPointer(); | |
| 701 return *this; | 739 return *this; |
| 702 } | 740 } |
| 703 | 741 |
| 704 template<typename U> | 742 template<typename U> |
| 705 WeakMember& operator=(U* other) | 743 WeakMember& operator=(U* other) |
| 706 { | 744 { |
| 707 this->m_raw = other; | 745 this->m_raw = other; |
| 746 this->checkPointer(); | |
| 708 return *this; | 747 return *this; |
| 709 } | 748 } |
| 710 | 749 |
| 711 template<typename U> | 750 template<typename U> |
| 712 WeakMember& operator=(const RawPtr<U>& other) | 751 WeakMember& operator=(const RawPtr<U>& other) |
| 713 { | 752 { |
| 714 this->m_raw = other; | 753 this->m_raw = other; |
| 754 this->checkPointer(); | |
| 715 return *this; | 755 return *this; |
| 716 } | 756 } |
| 717 | 757 |
| 718 WeakMember& operator=(std::nullptr_t) | 758 WeakMember& operator=(std::nullptr_t) |
| 719 { | 759 { |
| 720 this->m_raw = nullptr; | 760 this->m_raw = nullptr; |
| 721 return *this; | 761 return *this; |
| 722 } | 762 } |
| 723 | 763 |
| 724 private: | 764 private: |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1138 struct ParamStorageTraits<RawPtr<T>> : public PointerParamStorageTraits<T*, blin k::IsGarbageCollectedType<T>::value> { | 1178 struct ParamStorageTraits<RawPtr<T>> : public PointerParamStorageTraits<T*, blin k::IsGarbageCollectedType<T>::value> { |
| 1139 static_assert(sizeof(T), "T must be fully defined"); | 1179 static_assert(sizeof(T), "T must be fully defined"); |
| 1140 }; | 1180 }; |
| 1141 | 1181 |
| 1142 template<typename T> | 1182 template<typename T> |
| 1143 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; | 1183 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; |
| 1144 | 1184 |
| 1145 } // namespace WTF | 1185 } // namespace WTF |
| 1146 | 1186 |
| 1147 #endif | 1187 #endif |
| OLD | NEW |