1 | /* |
2 | * Copyright (C) 2011 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/Assertions.h> |
29 | #include "Handle.h" |
30 | #include "HandleSet.h" |
31 | |
32 | namespace JSC { |
33 | |
34 | class VM; |
35 | |
36 | // A strongly referenced handle that prevents the object it points to from being garbage collected. |
37 | template <typename T> class Strong : public Handle<T> { |
38 | using Handle<T>::slot; |
39 | using Handle<T>::setSlot; |
40 | template <typename U> friend class Strong; |
41 | |
42 | public: |
43 | typedef typename Handle<T>::ExternalType ExternalType; |
44 | |
45 | Strong() |
46 | : Handle<T>() |
47 | { |
48 | } |
49 | |
50 | Strong(VM&, ExternalType = ExternalType()); |
51 | |
52 | Strong(VM&, Handle<T>); |
53 | |
54 | Strong(const Strong& other) |
55 | : Handle<T>() |
56 | { |
57 | if (!other.slot()) |
58 | return; |
59 | setSlot(HandleSet::heapFor(other.slot())->allocate()); |
60 | set(other.get()); |
61 | } |
62 | |
63 | template <typename U> Strong(const Strong<U>& other) |
64 | : Handle<T>() |
65 | { |
66 | if (!other.slot()) |
67 | return; |
68 | setSlot(HandleSet::heapFor(other.slot())->allocate()); |
69 | set(other.get()); |
70 | } |
71 | |
72 | enum HashTableDeletedValueTag { HashTableDeletedValue }; |
73 | bool isHashTableDeletedValue() const { return slot() == hashTableDeletedValue(); } |
74 | Strong(HashTableDeletedValueTag) |
75 | : Handle<T>(hashTableDeletedValue()) |
76 | { |
77 | } |
78 | |
79 | ~Strong() |
80 | { |
81 | clear(); |
82 | } |
83 | |
84 | bool operator!() const { return !slot() || !*slot(); } |
85 | |
86 | explicit operator bool() const { return !!*this; } |
87 | |
88 | void swap(Strong& other) |
89 | { |
90 | Handle<T>::swap(other); |
91 | } |
92 | |
93 | ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); } |
94 | |
95 | void set(VM&, ExternalType); |
96 | |
97 | template <typename U> Strong& operator=(const Strong<U>& other) |
98 | { |
99 | if (!other.slot()) { |
100 | clear(); |
101 | return *this; |
102 | } |
103 | |
104 | set(*HandleSet::heapFor(other.slot())->vm(), other.get()); |
105 | return *this; |
106 | } |
107 | |
108 | Strong& operator=(const Strong& other) |
109 | { |
110 | if (!other.slot()) { |
111 | clear(); |
112 | return *this; |
113 | } |
114 | |
115 | set(*HandleSet::heapFor(other.slot())->vm(), other.get()); |
116 | return *this; |
117 | } |
118 | |
119 | void clear() |
120 | { |
121 | if (!slot()) |
122 | return; |
123 | HandleSet::heapFor(slot())->deallocate(slot()); |
124 | setSlot(0); |
125 | } |
126 | |
127 | private: |
128 | static HandleSlot hashTableDeletedValue() { return reinterpret_cast<HandleSlot>(-1); } |
129 | |
130 | void set(ExternalType externalType) |
131 | { |
132 | ASSERT(slot()); |
133 | JSValue value = HandleTypes<T>::toJSValue(externalType); |
134 | HandleSet::heapFor(slot())->writeBarrier(slot(), value); |
135 | *slot() = value; |
136 | } |
137 | }; |
138 | |
139 | template<class T> inline void swap(Strong<T>& a, Strong<T>& b) |
140 | { |
141 | a.swap(b); |
142 | } |
143 | |
144 | } // namespace JSC |
145 | |
146 | namespace WTF { |
147 | |
148 | template<typename T> struct VectorTraits<JSC::Strong<T>> : SimpleClassVectorTraits { |
149 | static const bool canCompareWithMemcmp = false; |
150 | }; |
151 | |
152 | template<typename P> struct HashTraits<JSC::Strong<P>> : SimpleClassHashTraits<JSC::Strong<P>> { }; |
153 | |
154 | } // namespace WTF |
155 | |