OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 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 21 matching lines...) Expand all Loading... | |
32 #define Heap_h | 32 #define Heap_h |
33 | 33 |
34 #include "heap/AddressSanitizer.h" | 34 #include "heap/AddressSanitizer.h" |
35 #include "heap/HeapExport.h" | 35 #include "heap/HeapExport.h" |
36 #include "heap/ThreadState.h" | 36 #include "heap/ThreadState.h" |
37 #include "heap/Visitor.h" | 37 #include "heap/Visitor.h" |
38 | 38 |
39 #include "wtf/Assertions.h" | 39 #include "wtf/Assertions.h" |
40 #include "wtf/OwnPtr.h" | 40 #include "wtf/OwnPtr.h" |
41 #include "wtf/PassRefPtr.h" | 41 #include "wtf/PassRefPtr.h" |
42 #include "wtf/ThreadSafeRefCounted.h" | |
42 | 43 |
43 #include <stdint.h> | 44 #include <stdint.h> |
44 | 45 |
45 namespace WebCore { | 46 namespace WebCore { |
46 | 47 |
47 const size_t blinkPageSizeLog2 = 17; | 48 const size_t blinkPageSizeLog2 = 17; |
48 const size_t blinkPageSize = 1 << blinkPageSizeLog2; | 49 const size_t blinkPageSize = 1 << blinkPageSizeLog2; |
49 const size_t blinkPageOffsetMask = blinkPageSize - 1; | 50 const size_t blinkPageOffsetMask = blinkPageSize - 1; |
50 const size_t blinkPageBaseMask = ~blinkPageOffsetMask; | 51 const size_t blinkPageBaseMask = ~blinkPageOffsetMask; |
51 // Double precision floats are more efficient when 8 byte aligned, so we 8 byte | 52 // Double precision floats are more efficient when 8 byte aligned, so we 8 byte |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 static const int numberOfEntriesLog2 = 12; | 529 static const int numberOfEntriesLog2 = 12; |
529 static const int numberOfEntries = 1 << numberOfEntriesLog2; | 530 static const int numberOfEntries = 1 << numberOfEntriesLog2; |
530 | 531 |
531 static size_t hash(Address); | 532 static size_t hash(Address); |
532 | 533 |
533 WTF::OwnPtr<HeapContainsCache::Entry[]> m_entries; | 534 WTF::OwnPtr<HeapContainsCache::Entry[]> m_entries; |
534 | 535 |
535 friend class ThreadState; | 536 friend class ThreadState; |
536 }; | 537 }; |
537 | 538 |
539 // FIXME(oilpan): The current implementation requires the deallocating | |
540 // thread to be the same as the thread on which this instance was first | |
541 // allocated. Only the refs/derefs that are not 0 -> 1 or 1 -> 0 are | |
542 // thread safe. | |
haraken
2014/03/27 11:44:05
Probably you can eliminate this restriction by usi
keishi
2014/04/03 06:53:19
Done.
| |
543 // This is currently used by the WebAudio code and the WebDatabase code. | |
haraken
2014/03/27 11:44:05
Remove the "WebDatabase code" part. I think tkent-
keishi
2014/04/03 06:53:19
Done.
| |
544 // We should attempt to restructure the WebAudio code so that the main thread | |
545 // alone determines life-time and receives messages about life-time from the | |
546 // audio thread. | |
547 template<typename T> | |
548 class ThreadSafeRefCountedGarbageCollected : public WTF::ThreadSafeRefCountedBas e, public GarbageCollectedFinalized<T> { | |
Mads Ager (chromium)
2014/03/27 11:06:49
I think it was like this on the branch as well, bu
keishi
2014/04/03 06:53:19
Done.
| |
549 WTF_MAKE_NONCOPYABLE(ThreadSafeRefCountedGarbageCollected); | |
550 | |
551 public: | |
552 ThreadSafeRefCountedGarbageCollected() | |
553 { | |
554 #ifndef NDEBUG | |
555 m_threadState = ThreadState::current(); | |
556 #endif | |
557 m_keepAlive = new Persistent<T>(static_cast<T*>(this)); | |
558 } | |
559 | |
560 // Override ref to deal with a case where a reference count goes up | |
561 // from 0 to 1. This can happen in the following scenario: | |
562 // (1) The reference count becomes 0, but on-stack pointers keep references to the object. | |
563 // (2) The on-stack pointer is assigned to a RefPtr. The reference count bec omes 1. | |
564 // In this case, we have to resurrect m_keepAlive. | |
565 void ref() | |
566 { | |
567 MutexLocker lock(m_mutex); | |
568 if (UNLIKELY(!refCount())) { | |
569 ASSERT(!m_keepAlive); | |
570 ASSERT(m_threadState->contains(reinterpret_cast<Address>(this))); | |
571 ASSERT(m_threadState == ThreadState::current()); | |
572 m_keepAlive = new Persistent<T>(static_cast<T*>(this)); | |
573 } | |
574 WTF::ThreadSafeRefCountedBase::ref(); | |
575 } | |
576 | |
577 // Override deref to deal with our own deallocation based on ref counting. | |
578 void deref() | |
579 { | |
580 MutexLocker lock(m_mutex); | |
581 if (derefBase()) { | |
haraken
2014/03/27 11:44:05
I suspect this leaks memory, because currently der
keishi
2014/04/03 06:53:19
haraken fixed this in r170256
| |
582 ASSERT(m_threadState == ThreadState::current()); | |
583 m_keepAlive = 0; | |
haraken
2014/03/27 11:44:05
m_keepAlive = nullptr;
keishi
2014/04/03 06:53:19
Done.
| |
584 } | |
585 } | |
586 | |
587 using GarbageCollectedFinalized<T>::operator new; | |
588 using GarbageCollectedFinalized<T>::operator delete; | |
589 | |
590 protected: | |
591 ~ThreadSafeRefCountedGarbageCollected() { } | |
592 | |
593 private: | |
594 Persistent<T>* m_keepAlive; | |
haraken
2014/03/27 11:44:05
Shall we use an OwnPtr<Persistent<T> > ?
keishi
2014/04/03 06:53:19
Done.
| |
595 mutable Mutex m_mutex; | |
596 #ifndef NDEBUG | |
597 ThreadState* m_threadState; | |
598 #endif | |
599 }; | |
600 | |
538 // The CallbackStack contains all the visitor callbacks used to trace and mark | 601 // The CallbackStack contains all the visitor callbacks used to trace and mark |
539 // objects. A specific CallbackStack instance contains at most bufferSize elemen ts. | 602 // objects. A specific CallbackStack instance contains at most bufferSize elemen ts. |
540 // If more space is needed a new CallbackStack instance is created and chained | 603 // If more space is needed a new CallbackStack instance is created and chained |
541 // together with the former instance. I.e. a logical CallbackStack can be made o f | 604 // together with the former instance. I.e. a logical CallbackStack can be made o f |
542 // multiple chained CallbackStack object instances. | 605 // multiple chained CallbackStack object instances. |
543 // There are two logical callback stacks. One containing all the marking callbac ks and | 606 // There are two logical callback stacks. One containing all the marking callbac ks and |
544 // one containing the weak pointer callbacks. | 607 // one containing the weak pointer callbacks. |
545 class CallbackStack { | 608 class CallbackStack { |
546 public: | 609 public: |
547 CallbackStack(CallbackStack** first) | 610 CallbackStack(CallbackStack** first) |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1000 delete m_keepAlive; | 1063 delete m_keepAlive; |
1001 m_keepAlive = 0; | 1064 m_keepAlive = 0; |
1002 } | 1065 } |
1003 } | 1066 } |
1004 | 1067 |
1005 bool hasOneRef() | 1068 bool hasOneRef() |
1006 { | 1069 { |
1007 return m_refCount == 1; | 1070 return m_refCount == 1; |
1008 } | 1071 } |
1009 | 1072 |
1073 int refCount() | |
Mads Ager (chromium)
2014/03/27 11:06:49
Do we need this? When something is RefCountedGarba
keishi
2014/04/03 06:53:19
Done.
| |
1074 { | |
1075 return m_refCount; | |
1076 } | |
1077 | |
1010 protected: | 1078 protected: |
1011 ~RefCountedGarbageCollected() { } | 1079 ~RefCountedGarbageCollected() { } |
1012 | 1080 |
1013 private: | 1081 private: |
1014 int m_refCount; | 1082 int m_refCount; |
1015 Persistent<T>* m_keepAlive; | 1083 Persistent<T>* m_keepAlive; |
1016 }; | 1084 }; |
1017 | 1085 |
1018 template<typename T> | 1086 template<typename T> |
1019 T* adoptRefCountedGarbageCollected(T* ptr) | 1087 T* adoptRefCountedGarbageCollected(T* ptr) |
(...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1734 // to export. This forces it to export all the methods from ThreadHeap. | 1802 // to export. This forces it to export all the methods from ThreadHeap. |
1735 template<> void ThreadHeap<FinalizedHeapObjectHeader>::addPageToHeap(const GCInf o*); | 1803 template<> void ThreadHeap<FinalizedHeapObjectHeader>::addPageToHeap(const GCInf o*); |
1736 template<> void ThreadHeap<HeapObjectHeader>::addPageToHeap(const GCInfo*); | 1804 template<> void ThreadHeap<HeapObjectHeader>::addPageToHeap(const GCInfo*); |
1737 extern template class HEAP_EXPORT ThreadHeap<FinalizedHeapObjectHeader>; | 1805 extern template class HEAP_EXPORT ThreadHeap<FinalizedHeapObjectHeader>; |
1738 extern template class HEAP_EXPORT ThreadHeap<HeapObjectHeader>; | 1806 extern template class HEAP_EXPORT ThreadHeap<HeapObjectHeader>; |
1739 #endif | 1807 #endif |
1740 | 1808 |
1741 } | 1809 } |
1742 | 1810 |
1743 #endif // Heap_h | 1811 #endif // Heap_h |
OLD | NEW |