barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
achristensen@apple.com | ddb46d2 | 2018-10-15 14:24:49 +0000 | [diff] [blame] | 26 | #pragma once |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 27 | |
cdumez@apple.com | b44b208 | 2017-06-18 19:49:12 +0000 | [diff] [blame] | 28 | #include <wtf/Function.h> |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 29 | #include <wtf/Noncopyable.h> |
| 30 | #include <wtf/RefPtr.h> |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 31 | #include <wtf/SetForScope.h> |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 32 | |
| 33 | namespace WTF { |
| 34 | |
barraclough@apple.com | 0eab842 | 2016-02-29 18:55:21 +0000 | [diff] [blame] | 35 | enum class RefCounterEvent { Decrement, Increment }; |
| 36 | |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 37 | template<typename T> |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 38 | class RefCounter { |
cdumez@apple.com | c38c3a4 | 2019-02-08 02:38:46 +0000 | [diff] [blame] | 39 | WTF_MAKE_FAST_ALLOCATED; |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 40 | WTF_MAKE_NONCOPYABLE(RefCounter); |
barraclough@apple.com | cfa7094 | 2014-12-19 00:51:47 +0000 | [diff] [blame] | 41 | |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 42 | class Count { |
| 43 | WTF_MAKE_NONCOPYABLE(Count); |
| 44 | public: |
barraclough@apple.com | 1ffd628 | 2016-02-26 00:45:35 +0000 | [diff] [blame] | 45 | void ref(); |
| 46 | void deref(); |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 47 | |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 48 | void refCounterWasDeleted(); |
| 49 | |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 50 | private: |
| 51 | friend class RefCounter; |
| 52 | |
| 53 | Count(RefCounter& refCounter) |
| 54 | : m_refCounter(&refCounter) |
| 55 | , m_value(0) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | RefCounter* m_refCounter; |
barraclough@apple.com | a5dc51a | 2016-02-26 18:20:07 +0000 | [diff] [blame] | 60 | size_t m_value; |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 61 | bool m_inValueDidChange { false }; |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
barraclough@apple.com | cfa7094 | 2014-12-19 00:51:47 +0000 | [diff] [blame] | 64 | public: |
barraclough@apple.com | 692326a | 2016-02-26 00:54:13 +0000 | [diff] [blame] | 65 | using Token = RefPtr<Count>; |
cdumez@apple.com | b44b208 | 2017-06-18 19:49:12 +0000 | [diff] [blame] | 66 | using ValueChangeFunction = WTF::Function<void (RefCounterEvent)>; |
barraclough@apple.com | cfa7094 | 2014-12-19 00:51:47 +0000 | [diff] [blame] | 67 | |
cdumez@apple.com | b44b208 | 2017-06-18 19:49:12 +0000 | [diff] [blame] | 68 | RefCounter(ValueChangeFunction&& = nullptr); |
barraclough@apple.com | 1ffd628 | 2016-02-26 00:45:35 +0000 | [diff] [blame] | 69 | ~RefCounter(); |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 70 | |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 71 | Token count() const |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 72 | { |
barraclough@apple.com | 692326a | 2016-02-26 00:54:13 +0000 | [diff] [blame] | 73 | return m_count; |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 74 | } |
| 75 | |
barraclough@apple.com | a5dc51a | 2016-02-26 18:20:07 +0000 | [diff] [blame] | 76 | size_t value() const |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 77 | { |
| 78 | return m_count->m_value; |
| 79 | } |
| 80 | |
| 81 | private: |
barraclough@apple.com | a5dc51a | 2016-02-26 18:20:07 +0000 | [diff] [blame] | 82 | ValueChangeFunction m_valueDidChange; |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 83 | Count* m_count; |
| 84 | }; |
| 85 | |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 86 | template<typename T> |
| 87 | inline void RefCounter<T>::Count::ref() |
| 88 | { |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 89 | ++m_value; |
barraclough@apple.com | a5dc51a | 2016-02-26 18:20:07 +0000 | [diff] [blame] | 90 | if (m_refCounter && m_refCounter->m_valueDidChange) |
barraclough@apple.com | 0eab842 | 2016-02-29 18:55:21 +0000 | [diff] [blame] | 91 | m_refCounter->m_valueDidChange(RefCounterEvent::Increment); |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | template<typename T> |
| 95 | inline void RefCounter<T>::Count::deref() |
| 96 | { |
| 97 | ASSERT(m_value); |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 98 | |
barraclough@apple.com | a5dc51a | 2016-02-26 18:20:07 +0000 | [diff] [blame] | 99 | --m_value; |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 100 | |
| 101 | if (m_refCounter && m_refCounter->m_valueDidChange) { |
| 102 | SetForScope<bool> inCallback(m_inValueDidChange, true); |
barraclough@apple.com | 0eab842 | 2016-02-29 18:55:21 +0000 | [diff] [blame] | 103 | m_refCounter->m_valueDidChange(RefCounterEvent::Decrement); |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 104 | } |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 105 | |
| 106 | // The Count object is kept alive so long as either the RefCounter that created it remains |
| 107 | // allocated, or so long as its reference count is non-zero. |
| 108 | // If the RefCounter has already been deallocted then delete the Count when its reference |
| 109 | // count reaches zero. |
barraclough@apple.com | a5dc51a | 2016-02-26 18:20:07 +0000 | [diff] [blame] | 110 | if (!m_refCounter && !m_value) |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 111 | delete this; |
| 112 | } |
| 113 | |
| 114 | template<typename T> |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 115 | inline void RefCounter<T>::Count::refCounterWasDeleted() |
| 116 | { |
| 117 | // The Count object is kept alive so long as either the RefCounter that created it remains |
| 118 | // allocated, or so long as its reference count is non-zero. |
| 119 | // If the reference count of the Count is already zero then delete it now, otherwise |
| 120 | // clear its m_refCounter pointer. |
| 121 | |
| 122 | m_refCounter = nullptr; |
| 123 | |
| 124 | if (m_inValueDidChange) |
| 125 | return; |
| 126 | |
| 127 | if (!m_value) |
| 128 | delete this; |
| 129 | } |
| 130 | |
| 131 | template<typename T> |
cdumez@apple.com | b44b208 | 2017-06-18 19:49:12 +0000 | [diff] [blame] | 132 | inline RefCounter<T>::RefCounter(ValueChangeFunction&& valueDidChange) |
| 133 | : m_valueDidChange(WTFMove(valueDidChange)) |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 134 | , m_count(new Count(*this)) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | template<typename T> |
| 139 | inline RefCounter<T>::~RefCounter() |
| 140 | { |
timothy_horton@apple.com | 99440ee | 2017-11-08 22:50:59 +0000 | [diff] [blame] | 141 | m_count->refCounterWasDeleted(); |
| 142 | |
barraclough@apple.com | 6a5fe17 | 2016-02-25 22:24:32 +0000 | [diff] [blame] | 143 | } |
| 144 | |
barraclough@apple.com | e313898 | 2014-12-02 20:30:17 +0000 | [diff] [blame] | 145 | } // namespace WTF |
| 146 | |
| 147 | using WTF::RefCounter; |
barraclough@apple.com | 0eab842 | 2016-02-29 18:55:21 +0000 | [diff] [blame] | 148 | using WTF::RefCounterEvent; |