]>
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.
26 #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(JSValuePtr value
)
54 if (value
&& value
.isCell())
55 gcProtect(asCell(value
));
58 inline void gcUnprotect(JSValuePtr 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 JSValuePtr() const { return JSValuePtr(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 ProtectedJSValuePtr
{
92 ProtectedJSValuePtr() {}
93 ProtectedJSValuePtr(JSValuePtr value
);
94 ProtectedJSValuePtr(const ProtectedJSValuePtr
&);
95 ~ProtectedJSValuePtr();
97 template <class U
> ProtectedJSValuePtr(const ProtectedPtr
<U
>&);
99 JSValuePtr
get() const { return m_value
; }
100 operator JSValuePtr() const { return m_value
; }
101 JSValuePtr
operator->() const { return m_value
; }
103 operator bool() const { return m_value
; }
104 bool operator!() const { return !m_value
; }
106 ProtectedJSValuePtr
& operator=(const ProtectedJSValuePtr
&);
107 ProtectedJSValuePtr
& operator=(JSValuePtr
);
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 ProtectedJSValuePtr::ProtectedJSValuePtr(JSValuePtr value
)
159 inline ProtectedJSValuePtr::ProtectedJSValuePtr(const ProtectedJSValuePtr
& o
)
165 inline ProtectedJSValuePtr::~ProtectedJSValuePtr()
167 gcUnprotect(m_value
);
170 template <class U
> ProtectedJSValuePtr::ProtectedJSValuePtr(const ProtectedPtr
<U
>& o
)
176 inline ProtectedJSValuePtr
& ProtectedJSValuePtr::operator=(const ProtectedJSValuePtr
& o
)
178 JSValuePtr ovalue
= o
.m_value
;
180 gcUnprotect(m_value
);
185 inline ProtectedJSValuePtr
& ProtectedJSValuePtr::operator=(JSValuePtr 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 ProtectedJSValuePtr
& a
, const ProtectedJSValuePtr
& b
) { return a
.get() == b
.get(); }
202 inline bool operator==(const ProtectedJSValuePtr
& a
, const JSValuePtr b
) { return a
.get() == b
; }
203 template <class T
> inline bool operator==(const ProtectedJSValuePtr
& a
, const ProtectedPtr
<T
>& b
) { return a
.get() == JSValuePtr(b
.get()); }
204 inline bool operator==(const JSValuePtr a
, const ProtectedJSValuePtr
& b
) { return a
== b
.get(); }
205 template <class T
> inline bool operator==(const ProtectedPtr
<T
>& a
, const ProtectedJSValuePtr
& b
) { return JSValuePtr(a
.get()) == b
.get(); }
207 inline bool operator!=(const ProtectedJSValuePtr
& a
, const ProtectedJSValuePtr
& b
) { return a
.get() != b
.get(); }
208 inline bool operator!=(const ProtectedJSValuePtr
& a
, const JSValuePtr b
) { return a
.get() != b
; }
209 template <class T
> inline bool operator!=(const ProtectedJSValuePtr
& a
, const ProtectedPtr
<T
>& b
) { return a
.get() != JSValuePtr(b
.get()); }
210 inline bool operator!=(const JSValuePtr a
, const ProtectedJSValuePtr
& b
) { return a
!= b
.get(); }
211 template <class T
> inline bool operator!=(const ProtectedPtr
<T
>& a
, const ProtectedJSValuePtr
& b
) { return JSValuePtr(a
.get()) != b
.get(); }