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
26#pragma once
27
28#include <wtf/Function.h>
29#include <wtf/Noncopyable.h>
30#include <wtf/RefPtr.h>
31#include <wtf/SetForScope.h>
32
33namespace WTF {
34
35enum class RefCounterEvent { Decrement, Increment };
36
37template<typename T>
38class RefCounter {
39 WTF_MAKE_FAST_ALLOCATED;
40 WTF_MAKE_NONCOPYABLE(RefCounter);
41
42 class Count {
43 WTF_MAKE_NONCOPYABLE(Count);
44 public:
45 void ref();
46 void deref();
47
48 void refCounterWasDeleted();
49
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;
60 size_t m_value;
61 bool m_inValueDidChange { false };
62 };
63
64public:
65 using Token = RefPtr<Count>;
66 using ValueChangeFunction = WTF::Function<void (RefCounterEvent)>;
67
68 RefCounter(ValueChangeFunction&& = nullptr);
69 ~RefCounter();
70
71 Token count() const
72 {
73 return m_count;
74 }
75
76 size_t value() const
77 {
78 return m_count->m_value;
79 }
80
81private:
82 ValueChangeFunction m_valueDidChange;
83 Count* m_count;
84};
85
86template<typename T>
87inline void RefCounter<T>::Count::ref()
88{
89 ++m_value;
90 if (m_refCounter && m_refCounter->m_valueDidChange)
91 m_refCounter->m_valueDidChange(RefCounterEvent::Increment);
92}
93
94template<typename T>
95inline void RefCounter<T>::Count::deref()
96{
97 ASSERT(m_value);
98
99 --m_value;
100
101 if (m_refCounter && m_refCounter->m_valueDidChange) {
102 SetForScope<bool> inCallback(m_inValueDidChange, true);
103 m_refCounter->m_valueDidChange(RefCounterEvent::Decrement);
104 }
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.
110 if (!m_refCounter && !m_value)
111 delete this;
112}
113
114template<typename T>
115inline 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>
132inline RefCounter<T>::RefCounter(ValueChangeFunction&& valueDidChange)
133 : m_valueDidChange(WTFMove(valueDidChange))
134 , m_count(new Count(*this))
135{
136}
137
138template<typename T>
139inline RefCounter<T>::~RefCounter()
140{
141 m_count->refCounterWasDeleted();
142
143}
144
145} // namespace WTF
146
147using WTF::RefCounter;
148using WTF::RefCounterEvent;
149