]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Protect.h
2 * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include "Collector.h"
30 inline void gcProtect(JSCell
* val
)
32 Heap::heap(val
)->protect(val
);
35 inline void gcUnprotect(JSCell
* val
)
37 Heap::heap(val
)->unprotect(val
);
40 inline void gcProtectNullTolerant(JSCell
* val
)
46 inline void gcUnprotectNullTolerant(JSCell
* val
)
52 inline void gcProtect(JSValue value
)
54 if (value
&& value
.isCell())
55 gcProtect(asCell(value
));
58 inline void gcUnprotect(JSValue value
)
60 if (value
&& value
.isCell())
61 gcUnprotect(asCell(value
));
64 // FIXME: Share more code with RefPtr template? The only differences are the ref/deref operation
65 // and the implicit conversion to raw pointer
66 template <class T
> class ProtectedPtr
{
68 ProtectedPtr() : m_ptr(0) {}
70 ProtectedPtr(const ProtectedPtr
&);
73 template <class U
> ProtectedPtr(const ProtectedPtr
<U
>&);
75 T
* get() const { return m_ptr
; }
76 operator T
*() const { return m_ptr
; }
77 operator JSValue() const { return JSValue(m_ptr
); }
78 T
* operator->() const { return m_ptr
; }
80 operator bool() const { return m_ptr
; }
81 bool operator!() const { return !m_ptr
; }
83 ProtectedPtr
& operator=(const ProtectedPtr
&);
84 ProtectedPtr
& operator=(T
*);
90 class ProtectedJSValue
{
93 ProtectedJSValue(JSValue value
);
94 ProtectedJSValue(const ProtectedJSValue
&);
97 template <class U
> ProtectedJSValue(const ProtectedPtr
<U
>&);
99 JSValue
get() const { return m_value
; }
100 operator JSValue() const { return m_value
; }
101 JSValue
operator->() const { return m_value
; }
103 operator bool() const { return m_value
; }
104 bool operator!() const { return !m_value
; }
106 ProtectedJSValue
& operator=(const ProtectedJSValue
&);
107 ProtectedJSValue
& operator=(JSValue
);
113 template <class T
> inline ProtectedPtr
<T
>::ProtectedPtr(T
* ptr
)
116 gcProtectNullTolerant(m_ptr
);
119 template <class T
> inline ProtectedPtr
<T
>::ProtectedPtr(const ProtectedPtr
& o
)
122 gcProtectNullTolerant(m_ptr
);
125 template <class T
> inline ProtectedPtr
<T
>::~ProtectedPtr()
127 gcUnprotectNullTolerant(m_ptr
);
130 template <class T
> template <class U
> inline ProtectedPtr
<T
>::ProtectedPtr(const ProtectedPtr
<U
>& o
)
133 gcProtectNullTolerant(m_ptr
);
136 template <class T
> inline ProtectedPtr
<T
>& ProtectedPtr
<T
>::operator=(const ProtectedPtr
<T
>& o
)
139 gcProtectNullTolerant(optr
);
140 gcUnprotectNullTolerant(m_ptr
);
145 template <class T
> inline ProtectedPtr
<T
>& ProtectedPtr
<T
>::operator=(T
* optr
)
147 gcProtectNullTolerant(optr
);
148 gcUnprotectNullTolerant(m_ptr
);
153 inline ProtectedJSValue::ProtectedJSValue(JSValue value
)
159 inline ProtectedJSValue::ProtectedJSValue(const ProtectedJSValue
& o
)
165 inline ProtectedJSValue::~ProtectedJSValue()
167 gcUnprotect(m_value
);
170 template <class U
> ProtectedJSValue::ProtectedJSValue(const ProtectedPtr
<U
>& o
)
176 inline ProtectedJSValue
& ProtectedJSValue::operator=(const ProtectedJSValue
& o
)
178 JSValue ovalue
= o
.m_value
;
180 gcUnprotect(m_value
);
185 inline ProtectedJSValue
& ProtectedJSValue::operator=(JSValue ovalue
)
188 gcUnprotect(m_value
);
193 template <class T
> inline bool operator==(const ProtectedPtr
<T
>& a
, const ProtectedPtr
<T
>& b
) { return a
.get() == b
.get(); }
194 template <class T
> inline bool operator==(const ProtectedPtr
<T
>& a
, const T
* b
) { return a
.get() == b
; }
195 template <class T
> inline bool operator==(const T
* a
, const ProtectedPtr
<T
>& b
) { return a
== b
.get(); }
197 template <class T
> inline bool operator!=(const ProtectedPtr
<T
>& a
, const ProtectedPtr
<T
>& b
) { return a
.get() != b
.get(); }
198 template <class T
> inline bool operator!=(const ProtectedPtr
<T
>& a
, const T
* b
) { return a
.get() != b
; }
199 template <class T
> inline bool operator!=(const T
* a
, const ProtectedPtr
<T
>& b
) { return a
!= b
.get(); }
201 inline bool operator==(const ProtectedJSValue
& a
, const ProtectedJSValue
& b
) { return a
.get() == b
.get(); }
202 inline bool operator==(const ProtectedJSValue
& a
, const JSValue b
) { return a
.get() == b
; }
203 template <class T
> inline bool operator==(const ProtectedJSValue
& a
, const ProtectedPtr
<T
>& b
) { return a
.get() == JSValue(b
.get()); }
204 inline bool operator==(const JSValue a
, const ProtectedJSValue
& b
) { return a
== b
.get(); }
205 template <class T
> inline bool operator==(const ProtectedPtr
<T
>& a
, const ProtectedJSValue
& b
) { return JSValue(a
.get()) == b
.get(); }
207 inline bool operator!=(const ProtectedJSValue
& a
, const ProtectedJSValue
& b
) { return a
.get() != b
.get(); }
208 inline bool operator!=(const ProtectedJSValue
& a
, const JSValue b
) { return a
.get() != b
; }
209 template <class T
> inline bool operator!=(const ProtectedJSValue
& a
, const ProtectedPtr
<T
>& b
) { return a
.get() != JSValue(b
.get()); }
210 inline bool operator!=(const JSValue a
, const ProtectedJSValue
& b
) { return a
!= b
.get(); }
211 template <class T
> inline bool operator!=(const ProtectedPtr
<T
>& a
, const ProtectedJSValue
& b
) { return JSValue(a
.get()) != b
.get(); }