1/*
2 * Copyright (C) 2017 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 "AuxiliaryBarrier.h"
29#include <wtf/CagedPtr.h>
30
31namespace JSC {
32
33class JSCell;
34class VM;
35
36// This is a convenient combo of AuxiliaryBarrier and CagedPtr.
37
38template<Gigacage::Kind passedKind, typename T>
39class CagedBarrierPtr {
40public:
41 static constexpr Gigacage::Kind kind = passedKind;
42 typedef T Type;
43
44 CagedBarrierPtr() { }
45
46 template<typename U>
47 CagedBarrierPtr(VM& vm, JSCell* cell, U&& value)
48 {
49 m_barrier.set(vm, cell, std::forward<U>(value));
50 }
51
52 void clear() { m_barrier.clear(); }
53
54 template<typename U>
55 void set(VM& vm, JSCell* cell, U&& value)
56 {
57 m_barrier.set(vm, cell, std::forward<U>(value));
58 }
59
60 T* get() const { return m_barrier.get().get(); }
61 T* getMayBeNull() const { return m_barrier.get().getMayBeNull(); }
62
63 bool operator==(const CagedBarrierPtr& other) const
64 {
65 return getMayBeNull() == other.getMayBeNull();
66 }
67
68 bool operator!=(const CagedBarrierPtr& other) const
69 {
70 return !(*this == other);
71 }
72
73 explicit operator bool() const
74 {
75 return *this != CagedBarrierPtr();
76 }
77
78 template<typename U>
79 void setWithoutBarrier(U&& value) { m_barrier.setWithoutBarrier(std::forward<U>(value)); }
80
81 T& operator*() const { return *get(); }
82 T* operator->() const { return get(); }
83
84 template<typename IndexType>
85 T& operator[](IndexType index) const { return get()[index]; }
86
87private:
88 AuxiliaryBarrier<CagedPtr<kind, T>> m_barrier;
89};
90
91template<Gigacage::Kind passedKind>
92class CagedBarrierPtr<passedKind, void> {
93public:
94 static constexpr Gigacage::Kind kind = passedKind;
95 typedef void Type;
96
97 CagedBarrierPtr() { }
98
99 template<typename U>
100 CagedBarrierPtr(VM& vm, JSCell* cell, U&& value)
101 {
102 m_barrier.set(vm, cell, std::forward<U>(value));
103 }
104
105 void clear() { m_barrier.clear(); }
106
107 template<typename U>
108 void set(VM& vm, JSCell* cell, U&& value)
109 {
110 m_barrier.set(vm, cell, std::forward<U>(value));
111 }
112
113 void* get() const { return m_barrier.get().get(); }
114 void* getMayBeNull() const { return m_barrier.get().getMayBeNull(); }
115
116 bool operator==(const CagedBarrierPtr& other) const
117 {
118 return getMayBeNull() == other.getMayBeNull();
119 }
120
121 bool operator!=(const CagedBarrierPtr& other) const
122 {
123 return !(*this == other);
124 }
125
126 explicit operator bool() const
127 {
128 return *this != CagedBarrierPtr();
129 }
130
131 template<typename U>
132 void setWithoutBarrier(U&& value) { m_barrier.setWithoutBarrier(std::forward<U>(value)); }
133
134private:
135 AuxiliaryBarrier<CagedPtr<kind, void>> m_barrier;
136};
137
138} // namespace JSC
139