1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is [Open Source Virtual Machine.].
17 * The Initial Developer of the Original Code is
18 * Adobe System Incorporated.
19 * Portions created by the Initial Developer are Copyright (C) 1993-2006
20 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
43 #ifndef _WRITE_BARRIER_H_
44 #define _WRITE_BARRIER_H_
46 // inline write barrier
47 #define WB(gc, container, addr, value) gc->privateWriteBarrier(container, addr, (const void *) (value))
49 // fast manual RC write barrier
50 #define WBRC(gc, container, addr, value) gc->privateWriteBarrierRC(container, addr, (const void *) (value))
52 // fast-path versions for writing NULL (useful in dtors)
54 inline void write_null(void* p
) { *(uintptr_t*)(p
) = 0; }
55 #define WB_NULL(addr) write_null((void*)addr)
57 #define WBRC_NULL(addr) MMgc::GC::WriteBarrierRC_dtor(addr)
59 // declare write barrier
60 // put spaces around the template arg to avoid possible digraph warnings
61 #define DWB(type) MMgc::WriteBarrier< type >
63 // declare an optimized RCObject write barrier
64 // put spaces around the template arg to avoid possible digraph warnings
65 #define DRCWB(type) MMgc::WriteBarrierRC< type >
67 #ifdef MMGC_POLICY_PROFILING
68 #define POLICY_PROFILING_ONLY(x) x
70 #define POLICY_PROFILING_ONLY(x)
76 REALLY_INLINE
void GC::WriteBarrierWriteRC(const void *address
, const void *value
)
78 RCObject
*rc
= (RCObject
*)Pointer(*(RCObject
**)address
);
80 GCAssert(IsRCObject(rc
));
81 GCAssert(rc
== FindBeginningGuarded(rc
));
84 GCAssert(IsPointerIntoGCObject(address
));
85 MMGC_WB_EDGE(address
, value
);
86 *(uintptr_t*)address
= (uintptr_t) value
;
87 rc
= (RCObject
*)Pointer(value
);
89 GCAssert(IsRCObject(rc
));
90 GCAssert(rc
== FindBeginningGuarded(value
));
96 REALLY_INLINE
void GC::WriteBarrierWriteRC_ctor(const void *address
, const void *value
)
98 // assume existing contents of address are potentially uninitialized,
99 // so don't bother even making an assertion.
100 GCAssert(IsPointerIntoGCObject(address
));
101 MMGC_WB_EDGE(address
, value
);
102 *(uintptr_t*)address
= (uintptr_t) value
;
103 RCObject
*rc
= (RCObject
*)Pointer(value
);
105 GCAssert(IsRCObject(rc
));
106 GCAssert(rc
== FindBeginningGuarded(value
));
112 REALLY_INLINE
void GC::WriteBarrierWriteRC_dtor(const void *address
)
114 GCAssert(IsPointerIntoGCObject(address
));
115 RCObject
*rc
= (RCObject
*)Pointer(*(RCObject
**)address
);
116 MMGC_WB_EDGE(address
, NULL
);
118 GCAssert(IsRCObject(rc
));
119 GCAssert(rc
== FindBeginningGuarded(rc
));
121 *(uintptr_t*)address
= 0;
126 REALLY_INLINE
void GC::WriteBarrierWrite(const void *address
, const void *value
)
128 GCAssert(!IsRCObject(value
));
129 GCAssert(IsPointerIntoGCObject(address
));
130 MMGC_WB_EDGE(address
, value
);
131 *(uintptr_t*)address
= (uintptr_t) value
;
135 REALLY_INLINE
void GC::InlineWriteBarrierTrap(const void *container
)
138 GCAssert(IsPointerToGCPage(container
));
140 POLICY_PROFILING_ONLY(int stage
=0;)
141 // If the object is black then it needs to be gray, because we just stored
142 // something into it.
144 // It's good to check 'marking' because it provides a performance boost if it is
145 // true less than maybe 2/3 of the time - it avoids more expensive computation.
146 // We don't check 'collecting' because that's much less often true; it's checked
147 // inside WriteBarrierHit. We make it a precondition for this function because
148 // some computations can be elided if it is checked earlier.
150 // Testing shows that it's /sometimes/ useful to check the right hand side of the
151 // assignment for NULL, but this depends on the program and for the time being
152 // the right hand side isn't available here and isn't checked.
154 if (IsMarkedThenMakeQueued(container
))
156 POLICY_PROFILING_ONLY(stage
=1;)
157 WriteBarrierHit(container
);
159 POLICY_PROFILING_ONLY( policy
.signalWriteBarrierWork(stage
); )
162 REALLY_INLINE
void GC::privateInlineWriteBarrier(const void *container
, const void *address
, const void *value
)
164 GCAssert(!container
|| IsPointerToGCPage(container
));
165 GCAssert(((uintptr_t)address
& 3) == 0);
167 if (container
&& marking
) {
168 GCAssert(address
>= container
);
169 GCAssert(address
< (char*)container
+ Size(container
));
170 InlineWriteBarrierTrap(container
);
172 WriteBarrierWrite(address
, value
);
175 REALLY_INLINE
void GC::privateInlineWriteBarrier(const void *address
, const void *value
)
177 GCAssert(((uintptr_t)address
& 3) == 0);
180 const void* container
= FindBeginningFast(address
);
182 GCAssert(IsPointerToGCPage(container
));
183 GCAssert(address
>= container
);
184 GCAssert(address
< (char*)container
+ Size(container
));
186 InlineWriteBarrierTrap(container
);
188 WriteBarrierWrite(address
, value
);
191 REALLY_INLINE
void GC::privateInlineWriteBarrierRC(const void *container
, const void *address
, const void *value
)
193 GCAssert(IsPointerToGCPage(container
));
194 GCAssert(((uintptr_t)container
& 3) == 0);
195 GCAssert(((uintptr_t)address
& 2) == 0);
196 GCAssert(address
>= container
);
197 GCAssert(address
< (char*)container
+ Size(container
));
200 InlineWriteBarrierTrap(container
);
201 WriteBarrierWriteRC(address
, value
);
204 REALLY_INLINE
void GC::privateInlineWriteBarrierRC(const void *address
, const void *value
)
207 const void* container
= FindBeginningFast(address
);
209 GCAssert(IsPointerToGCPage(container
));
210 GCAssert(((uintptr_t)container
& 3) == 0);
211 GCAssert(((uintptr_t)address
& 2) == 0);
212 GCAssert(address
>= container
);
213 GCAssert(address
< (char*)container
+ Size(container
));
215 InlineWriteBarrierTrap(container
);
217 WriteBarrierWriteRC(address
, value
);
220 REALLY_INLINE
void GC::ConservativeWriteBarrierNoSubstitute(const void *address
, const void *value
)
222 (void)value
; // Can't get rid of this parameter now; part of an existing API
225 privateConservativeWriteBarrierNoSubstitute(address
);
229 * WB is a smart pointer write barrier meant to be used on any field of a GC object that
230 * may point to another GC object. A write barrier may only be avoided if if the field is
231 * const and no allocations occur between the construction of the object holding the field
232 * and the assignment.
234 template<class T
> class WriteBarrier
237 // Always pay for a single real function call; then inline & optimize massively in WriteBarrier()
241 GC::WriteBarrier(&t
, (const void*)tNew
); // updates 't'
245 explicit REALLY_INLINE
WriteBarrier() : t(0)
249 explicit REALLY_INLINE
WriteBarrier(T _t
)
254 REALLY_INLINE
~WriteBarrier()
259 REALLY_INLINE T
operator=(const WriteBarrier
<T
>& wb
)
264 REALLY_INLINE T
operator=(T tNew
)
269 // BEHOLD ... The weird power of C++ operator overloading
270 REALLY_INLINE
operator T() const { return t
; }
272 // let us peek at it without a cast
273 REALLY_INLINE T
value() const { return t
; }
275 REALLY_INLINE
operator ZeroPtr
<T
>() const { return t
; }
277 REALLY_INLINE
bool operator!=(T other
) const { return other
!= t
; }
279 REALLY_INLINE T
operator->() const
285 // Private constructor to prevent its use and someone adding it, GCC creates
286 // WriteBarriers on the stack with it
287 WriteBarrier(const WriteBarrier
<T
>& toCopy
); // unimplemented
293 * WriteBarrierRC is a write barrier for naked (not pointer swizzled) RC objects.
294 * the only thing going in and out of the slot is NULL or a valid RCObject
296 template<class T
> class WriteBarrierRC
299 // Always pay for a single real function call; then inline & optimize massively in WriteBarrierRC()
300 REALLY_INLINE T
set(const T tNew
)
302 GC::WriteBarrierRC(&t
, (const void*)tNew
); // updates 't'
307 explicit REALLY_INLINE
WriteBarrierRC() : t(0)
311 explicit REALLY_INLINE
WriteBarrierRC(const T _t
) // : t(0) -- not necessary, WriteBarrierRC_ctor handles it
313 GC::WriteBarrierRC_ctor(&t
, (const void*)_t
);
316 REALLY_INLINE
~WriteBarrierRC()
318 GC::WriteBarrierRC_dtor(&t
);
321 REALLY_INLINE
void set(MMgc::GC
* gc
, void* container
, T newValue
)
323 WBRC(gc
, container
, &t
, newValue
);
326 REALLY_INLINE T
operator=(const WriteBarrierRC
<T
>& wb
)
331 REALLY_INLINE T
operator=(T tNew
)
336 REALLY_INLINE T
value() const { return t
; }
338 REALLY_INLINE
operator T() const { return t
; }
340 REALLY_INLINE
operator ZeroPtr
<T
>() const { return t
; }
342 REALLY_INLINE
bool operator!=(T other
) const { return other
!= t
; }
344 REALLY_INLINE T
operator->() const
349 // Clear() clears the smart pointer without decrementing the reference count of any object stored in
350 // the smart pointer. It is essentially useful in situations where the caller has already deleted
351 // the object and needs to ensure that the destruction of the smart pointer does not access the
354 REALLY_INLINE
void Clear() { t
= 0; }
357 // Private constructor to prevent its use and someone adding it, GCC creates
358 // WriteBarrierRCs on the stack with it
359 WriteBarrierRC(const WriteBarrierRC
<T
>& toCopy
);
365 #endif // _WRITE_BARRIER_H_