| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 #include "wtf/Allocator.h" | 33 #include "wtf/Allocator.h" |
| 34 #include "wtf/Atomics.h" | 34 #include "wtf/Atomics.h" |
| 35 #include "wtf/DynamicAnnotations.h" | 35 #include "wtf/DynamicAnnotations.h" |
| 36 #include "wtf/Noncopyable.h" | 36 #include "wtf/Noncopyable.h" |
| 37 #include "wtf/WTFExport.h" | 37 #include "wtf/WTFExport.h" |
| 38 | 38 |
| 39 namespace WTF { | 39 namespace WTF { |
| 40 | 40 |
| 41 class WTF_EXPORT ThreadSafeRefCountedBase { | 41 class WTF_EXPORT ThreadSafeRefCountedBase { |
| 42 WTF_MAKE_NONCOPYABLE(ThreadSafeRefCountedBase); | 42 WTF_MAKE_NONCOPYABLE(ThreadSafeRefCountedBase); |
| 43 USING_FAST_MALLOC(ThreadSafeRefCountedBase); | 43 USING_FAST_MALLOC(ThreadSafeRefCountedBase); |
| 44 public: | 44 |
| 45 ThreadSafeRefCountedBase(int initialRefCount = 1) | 45 public: |
| 46 : m_refCount(initialRefCount) | 46 ThreadSafeRefCountedBase(int initialRefCount = 1) |
| 47 { | 47 : m_refCount(initialRefCount) {} |
| 48 |
| 49 void ref() { atomicIncrement(&m_refCount); } |
| 50 |
| 51 bool hasOneRef() { return refCount() == 1; } |
| 52 |
| 53 int refCount() const { return static_cast<int const volatile&>(m_refCount); } |
| 54 |
| 55 protected: |
| 56 // Returns whether the pointer should be freed or not. |
| 57 bool derefBase() { |
| 58 WTF_ANNOTATE_HAPPENS_BEFORE(&m_refCount); |
| 59 if (atomicDecrement(&m_refCount) <= 0) { |
| 60 WTF_ANNOTATE_HAPPENS_AFTER(&m_refCount); |
| 61 return true; |
| 48 } | 62 } |
| 63 return false; |
| 64 } |
| 49 | 65 |
| 50 void ref() | 66 private: |
| 51 { | 67 int m_refCount; |
| 52 atomicIncrement(&m_refCount); | |
| 53 } | |
| 54 | |
| 55 bool hasOneRef() | |
| 56 { | |
| 57 return refCount() == 1; | |
| 58 } | |
| 59 | |
| 60 int refCount() const | |
| 61 { | |
| 62 return static_cast<int const volatile &>(m_refCount); | |
| 63 } | |
| 64 | |
| 65 protected: | |
| 66 // Returns whether the pointer should be freed or not. | |
| 67 bool derefBase() | |
| 68 { | |
| 69 WTF_ANNOTATE_HAPPENS_BEFORE(&m_refCount); | |
| 70 if (atomicDecrement(&m_refCount) <= 0) { | |
| 71 WTF_ANNOTATE_HAPPENS_AFTER(&m_refCount); | |
| 72 return true; | |
| 73 } | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 int m_refCount; | |
| 79 }; | 68 }; |
| 80 | 69 |
| 81 template<class T> class ThreadSafeRefCounted : public ThreadSafeRefCountedBase { | 70 template <class T> |
| 82 public: | 71 class ThreadSafeRefCounted : public ThreadSafeRefCountedBase { |
| 83 void deref() | 72 public: |
| 84 { | 73 void deref() { |
| 85 if (derefBase()) | 74 if (derefBase()) |
| 86 delete static_cast<T*>(this); | 75 delete static_cast<T*>(this); |
| 87 } | 76 } |
| 88 | 77 |
| 89 protected: | 78 protected: |
| 90 ThreadSafeRefCounted() | 79 ThreadSafeRefCounted() {} |
| 91 { | |
| 92 } | |
| 93 }; | 80 }; |
| 94 | 81 |
| 95 } // namespace WTF | 82 } // namespace WTF |
| 96 | 83 |
| 97 using WTF::ThreadSafeRefCounted; | 84 using WTF::ThreadSafeRefCounted; |
| 98 | 85 |
| 99 #endif // ThreadSafeRefCounted_h | 86 #endif // ThreadSafeRefCounted_h |
| OLD | NEW |