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, bool shouldTag = false>
39class CagedBarrierPtr {
40public:
41 static constexpr Gigacage::Kind kind = passedKind;
42 using Type = T;
43 using CagedType = CagedPtr<kind, Type, shouldTag>;
44
45 CagedBarrierPtr() = default;
46
47 template<typename U>
48 CagedBarrierPtr(VM& vm, JSCell* cell, U&& value, unsigned size)
49 {
50 m_barrier.set(vm, cell, CagedType(std::forward<U>(value), size));
51 }
52
53 void clear() { m_barrier.clear(); }
54
55 template<typename U>
56 void set(VM& vm, JSCell* cell, U&& value, unsigned size)
57 {
58 m_barrier.set(vm, cell, CagedType(std::forward<U>(value), size));
59 }
60
61 T* get(unsigned size) const { return m_barrier.get().get(size); }
62 T* getMayBeNull(unsigned size) const { return m_barrier.get().getMayBeNull(size); }
63 T* getUnsafe() const { return m_barrier.get().getUnsafe(); }
64
65 // We need the template here so that the type of U is deduced at usage time rather than class time. U should always be T.
66 template<typename U = T>
67 typename std::enable_if<!std::is_same<void, U>::value, T>::type&
68 /* T& */ at(unsigned index, unsigned size) const { return get(size)[index]; }
69
70 bool operator==(const CagedBarrierPtr& other) const
71 {
72 return m_barrier.get() == other.m_barrier.get();
73 }
74
75 bool operator!=(const CagedBarrierPtr& other) const
76 {
77 return !(*this == other);
78 }
79
80 explicit operator bool() const
81 {
82 return !!m_barrier.get();
83 }
84
85 template<typename U>
86 void setWithoutBarrier(U&& value, unsigned size) { m_barrier.setWithoutBarrier(CagedType(std::forward<U>(value), size)); }
87
88private:
89 AuxiliaryBarrier<CagedType> m_barrier;
90};
91
92} // namespace JSC
93