blob: a5f68af2b3b8da404d376f6f185ea199157a6a59 [file] [log] [blame]
barraclough@apple.come3138982014-12-02 20:30:17 +00001/*
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.comddb46d22018-10-15 14:24:49 +000026#pragma once
barraclough@apple.come3138982014-12-02 20:30:17 +000027
cdumez@apple.comb44b2082017-06-18 19:49:12 +000028#include <wtf/Function.h>
barraclough@apple.come3138982014-12-02 20:30:17 +000029#include <wtf/Noncopyable.h>
30#include <wtf/RefPtr.h>
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +000031#include <wtf/SetForScope.h>
barraclough@apple.come3138982014-12-02 20:30:17 +000032
33namespace WTF {
34
barraclough@apple.com0eab8422016-02-29 18:55:21 +000035enum class RefCounterEvent { Decrement, Increment };
36
barraclough@apple.com6a5fe172016-02-25 22:24:32 +000037template<typename T>
barraclough@apple.come3138982014-12-02 20:30:17 +000038class RefCounter {
cdumez@apple.comc38c3a42019-02-08 02:38:46 +000039 WTF_MAKE_FAST_ALLOCATED;
barraclough@apple.come3138982014-12-02 20:30:17 +000040 WTF_MAKE_NONCOPYABLE(RefCounter);
barraclough@apple.comcfa70942014-12-19 00:51:47 +000041
barraclough@apple.come3138982014-12-02 20:30:17 +000042 class Count {
43 WTF_MAKE_NONCOPYABLE(Count);
44 public:
barraclough@apple.com1ffd6282016-02-26 00:45:35 +000045 void ref();
46 void deref();
barraclough@apple.come3138982014-12-02 20:30:17 +000047
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +000048 void refCounterWasDeleted();
49
barraclough@apple.come3138982014-12-02 20:30:17 +000050 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.coma5dc51a2016-02-26 18:20:07 +000060 size_t m_value;
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +000061 bool m_inValueDidChange { false };
barraclough@apple.come3138982014-12-02 20:30:17 +000062 };
63
barraclough@apple.comcfa70942014-12-19 00:51:47 +000064public:
barraclough@apple.com692326a2016-02-26 00:54:13 +000065 using Token = RefPtr<Count>;
cdumez@apple.comb44b2082017-06-18 19:49:12 +000066 using ValueChangeFunction = WTF::Function<void (RefCounterEvent)>;
barraclough@apple.comcfa70942014-12-19 00:51:47 +000067
cdumez@apple.comb44b2082017-06-18 19:49:12 +000068 RefCounter(ValueChangeFunction&& = nullptr);
barraclough@apple.com1ffd6282016-02-26 00:45:35 +000069 ~RefCounter();
barraclough@apple.come3138982014-12-02 20:30:17 +000070
barraclough@apple.com6a5fe172016-02-25 22:24:32 +000071 Token count() const
barraclough@apple.come3138982014-12-02 20:30:17 +000072 {
barraclough@apple.com692326a2016-02-26 00:54:13 +000073 return m_count;
barraclough@apple.come3138982014-12-02 20:30:17 +000074 }
75
barraclough@apple.coma5dc51a2016-02-26 18:20:07 +000076 size_t value() const
barraclough@apple.come3138982014-12-02 20:30:17 +000077 {
78 return m_count->m_value;
79 }
80
81private:
barraclough@apple.coma5dc51a2016-02-26 18:20:07 +000082 ValueChangeFunction m_valueDidChange;
barraclough@apple.come3138982014-12-02 20:30:17 +000083 Count* m_count;
84};
85
barraclough@apple.com6a5fe172016-02-25 22:24:32 +000086template<typename T>
87inline void RefCounter<T>::Count::ref()
88{
barraclough@apple.com6a5fe172016-02-25 22:24:32 +000089 ++m_value;
barraclough@apple.coma5dc51a2016-02-26 18:20:07 +000090 if (m_refCounter && m_refCounter->m_valueDidChange)
barraclough@apple.com0eab8422016-02-29 18:55:21 +000091 m_refCounter->m_valueDidChange(RefCounterEvent::Increment);
barraclough@apple.com6a5fe172016-02-25 22:24:32 +000092}
93
94template<typename T>
95inline void RefCounter<T>::Count::deref()
96{
97 ASSERT(m_value);
barraclough@apple.com6a5fe172016-02-25 22:24:32 +000098
barraclough@apple.coma5dc51a2016-02-26 18:20:07 +000099 --m_value;
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +0000100
101 if (m_refCounter && m_refCounter->m_valueDidChange) {
102 SetForScope<bool> inCallback(m_inValueDidChange, true);
barraclough@apple.com0eab8422016-02-29 18:55:21 +0000103 m_refCounter->m_valueDidChange(RefCounterEvent::Decrement);
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +0000104 }
barraclough@apple.com6a5fe172016-02-25 22:24:32 +0000105
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.coma5dc51a2016-02-26 18:20:07 +0000110 if (!m_refCounter && !m_value)
barraclough@apple.com6a5fe172016-02-25 22:24:32 +0000111 delete this;
112}
113
114template<typename T>
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +0000115inline 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
131template<typename T>
cdumez@apple.comb44b2082017-06-18 19:49:12 +0000132inline RefCounter<T>::RefCounter(ValueChangeFunction&& valueDidChange)
133 : m_valueDidChange(WTFMove(valueDidChange))
barraclough@apple.com6a5fe172016-02-25 22:24:32 +0000134 , m_count(new Count(*this))
135{
136}
137
138template<typename T>
139inline RefCounter<T>::~RefCounter()
140{
timothy_horton@apple.com99440ee2017-11-08 22:50:59 +0000141 m_count->refCounterWasDeleted();
142
barraclough@apple.com6a5fe172016-02-25 22:24:32 +0000143}
144
barraclough@apple.come3138982014-12-02 20:30:17 +0000145} // namespace WTF
146
147using WTF::RefCounter;
barraclough@apple.com0eab8422016-02-29 18:55:21 +0000148using WTF::RefCounterEvent;