/[jscoverage]/trunk/js/jsgc.h
ViewVC logotype

Contents of /trunk/js/jsgc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 460 - (show annotations)
Sat Sep 26 23:15:22 2009 UTC (12 years, 9 months ago) by siliconforks
File MIME type: text/plain
File size: 14712 byte(s)
Upgrade to SpiderMonkey from Firefox 3.5.3.

1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 #ifndef jsgc_h___
41 #define jsgc_h___
42 /*
43 * JS Garbage Collector.
44 */
45 #include "jsprvtd.h"
46 #include "jspubtd.h"
47 #include "jsdhash.h"
48 #include "jsbit.h"
49 #include "jsutil.h"
50
51 JS_BEGIN_EXTERN_C
52
53 #define JSTRACE_XML 3
54
55 /*
56 * One past the maximum trace kind.
57 */
58 #define JSTRACE_LIMIT 4
59
60 /*
61 * We use the trace kinds as the types for all GC things except external
62 * strings.
63 */
64 #define GCX_OBJECT JSTRACE_OBJECT /* JSObject */
65 #define GCX_DOUBLE JSTRACE_DOUBLE /* jsdouble */
66 #define GCX_STRING JSTRACE_STRING /* JSString */
67 #define GCX_XML JSTRACE_XML /* JSXML */
68 #define GCX_EXTERNAL_STRING JSTRACE_LIMIT /* JSString with external
69 chars */
70 /*
71 * The number of defined GC types and the maximum limit for the number of
72 * possible GC types.
73 */
74 #define GCX_NTYPES (GCX_EXTERNAL_STRING + 8)
75 #define GCX_LIMIT_LOG2 4 /* type index bits */
76 #define GCX_LIMIT JS_BIT(GCX_LIMIT_LOG2)
77
78 /* GC flag definitions, must fit in 8 bits (type index goes in the low bits). */
79 #define GCF_TYPEMASK JS_BITMASK(GCX_LIMIT_LOG2)
80 #define GCF_MARK JS_BIT(GCX_LIMIT_LOG2)
81 #define GCF_FINAL JS_BIT(GCX_LIMIT_LOG2 + 1)
82 #define GCF_LOCKSHIFT (GCX_LIMIT_LOG2 + 2) /* lock bit shift */
83 #define GCF_LOCK JS_BIT(GCF_LOCKSHIFT) /* lock request bit in API */
84
85 /*
86 * Get the type of the external string or -1 if the string was not created
87 * with JS_NewExternalString.
88 */
89 extern intN
90 js_GetExternalStringGCType(JSString *str);
91
92 extern JS_FRIEND_API(uint32)
93 js_GetGCThingTraceKind(void *thing);
94
95 /*
96 * The sole purpose of the function is to preserve public API compatibility
97 * in JS_GetStringBytes which takes only single JSString* argument.
98 */
99 JSRuntime*
100 js_GetGCStringRuntime(JSString *str);
101
102 #if 1
103 /*
104 * Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles
105 * loading oldval. XXX remove implied force, fix jsinterp.c's "second arg
106 * ignored", etc.
107 */
108 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JS_TRUE)
109 #else
110 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JSVAL_IS_GCTHING(oldval))
111 #endif
112
113 /*
114 * Write barrier macro monitoring property update from oldval to newval in
115 * scope->object.
116 *
117 * Since oldval is used only for the branded scope case, and the oldval actual
118 * argument expression is typically not used otherwise by callers, performance
119 * benefits if oldval is *not* evaluated into a callsite temporary variable,
120 * and instead passed to GC_WRITE_BARRIER for conditional evaluation (we rely
121 * on modern compilers to do a good CSE job). Yay, C macros.
122 */
123 #define GC_WRITE_BARRIER(cx,scope,oldval,newval) \
124 JS_BEGIN_MACRO \
125 if (SCOPE_IS_BRANDED(scope) && \
126 (oldval) != (newval) && \
127 (VALUE_IS_FUNCTION(cx,oldval) || VALUE_IS_FUNCTION(cx,newval))) { \
128 js_MakeScopeShapeUnique(cx, scope); \
129 } \
130 GC_POKE(cx, oldval); \
131 JS_END_MACRO
132
133 extern JSBool
134 js_InitGC(JSRuntime *rt, uint32 maxbytes);
135
136 extern void
137 js_FinishGC(JSRuntime *rt);
138
139 extern intN
140 js_ChangeExternalStringFinalizer(JSStringFinalizeOp oldop,
141 JSStringFinalizeOp newop);
142
143 extern JSBool
144 js_AddRoot(JSContext *cx, void *rp, const char *name);
145
146 extern JSBool
147 js_AddRootRT(JSRuntime *rt, void *rp, const char *name);
148
149 extern JSBool
150 js_RemoveRoot(JSRuntime *rt, void *rp);
151
152 #ifdef DEBUG
153 extern void
154 js_DumpNamedRoots(JSRuntime *rt,
155 void (*dump)(const char *name, void *rp, void *data),
156 void *data);
157 #endif
158
159 extern uint32
160 js_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
161
162 /* Table of pointers with count valid members. */
163 typedef struct JSPtrTable {
164 size_t count;
165 void **array;
166 } JSPtrTable;
167
168 extern JSBool
169 js_RegisterCloseableIterator(JSContext *cx, JSObject *obj);
170
171 /*
172 * The private JSGCThing struct, which describes a gcFreeList element.
173 */
174 struct JSGCThing {
175 JSGCThing *next;
176 uint8 *flagp;
177 };
178
179 #define GC_NBYTES_MAX (10 * sizeof(JSGCThing))
180 #define GC_NUM_FREELISTS (GC_NBYTES_MAX / sizeof(JSGCThing))
181 #define GC_FREELIST_NBYTES(i) (((i) + 1) * sizeof(JSGCThing))
182 #define GC_FREELIST_INDEX(n) (((n) / sizeof(JSGCThing)) - 1)
183
184 /*
185 * Allocates a new GC thing of the given size. After a successful allocation
186 * the caller must fully initialize the thing before calling any function that
187 * can potentially trigger GC. This will ensure that GC tracing never sees junk
188 * values stored in the partially initialized thing.
189 */
190 extern void *
191 js_NewGCThing(JSContext *cx, uintN flags, size_t nbytes);
192
193 /*
194 * Allocate a new double jsval and store the result in *vp. vp must be a root.
195 * The function does not copy the result into any weak root.
196 */
197 extern JSBool
198 js_NewDoubleInRootedValue(JSContext *cx, jsdouble d, jsval *vp);
199
200 /*
201 * Return a pointer to a new GC-allocated and weakly-rooted jsdouble number,
202 * or null when the allocation fails.
203 */
204 extern jsdouble *
205 js_NewWeaklyRootedDouble(JSContext *cx, jsdouble d);
206
207 #ifdef JS_TRACER
208 extern JSBool
209 js_ReserveObjects(JSContext *cx, size_t nobjects);
210 #endif
211
212 extern JSBool
213 js_LockGCThingRT(JSRuntime *rt, void *thing);
214
215 extern JSBool
216 js_UnlockGCThingRT(JSRuntime *rt, void *thing);
217
218 extern JSBool
219 js_IsAboutToBeFinalized(JSContext *cx, void *thing);
220
221 /*
222 * Macro to test if a traversal is the marking phase of GC to avoid exposing
223 * ScriptFilenameEntry to traversal implementations.
224 */
225 #define IS_GC_MARKING_TRACER(trc) ((trc)->callback == NULL)
226
227 #if JS_HAS_XML_SUPPORT
228 # define JS_IS_VALID_TRACE_KIND(kind) ((uint32)(kind) < JSTRACE_LIMIT)
229 #else
230 # define JS_IS_VALID_TRACE_KIND(kind) ((uint32)(kind) <= JSTRACE_STRING)
231 #endif
232
233 /*
234 * Trace jsval when JSVAL_IS_OBJECT(v) can be an arbitrary GC thing casted as
235 * JSVAL_OBJECT and js_GetGCThingTraceKind has to be used to find the real
236 * type behind v.
237 */
238 extern void
239 js_CallValueTracerIfGCThing(JSTracer *trc, jsval v);
240
241 extern void
242 js_TraceStackFrame(JSTracer *trc, JSStackFrame *fp);
243
244 extern JS_REQUIRES_STACK void
245 js_TraceRuntime(JSTracer *trc, JSBool allAtoms);
246
247 extern JS_REQUIRES_STACK JS_FRIEND_API(void)
248 js_TraceContext(JSTracer *trc, JSContext *acx);
249
250 /*
251 * Schedule the GC call at a later safe point.
252 */
253 #ifndef JS_THREADSAFE
254 # define js_TriggerGC(cx, gcLocked) js_TriggerGC (cx)
255 #endif
256
257 extern void
258 js_TriggerGC(JSContext *cx, JSBool gcLocked);
259
260 /*
261 * Kinds of js_GC invocation.
262 */
263 typedef enum JSGCInvocationKind {
264 /* Normal invocation. */
265 GC_NORMAL = 0,
266
267 /*
268 * Called from js_DestroyContext for last JSContext in a JSRuntime, when
269 * it is imperative that rt->gcPoke gets cleared early in js_GC.
270 */
271 GC_LAST_CONTEXT = 1,
272
273 /*
274 * Flag bit telling js_GC that the caller has already acquired rt->gcLock.
275 * Currently, this flag is set for the invocation kinds that also preserve
276 * atoms and weak roots, so we don't need another bit for GC_KEEP_ATOMS.
277 */
278 GC_LOCK_HELD = 0x10,
279 GC_KEEP_ATOMS = GC_LOCK_HELD,
280
281 /*
282 * Called from js_SetProtoOrParent with a request to set an object's proto
283 * or parent slot inserted on rt->setSlotRequests.
284 */
285 GC_SET_SLOT_REQUEST = GC_LOCK_HELD | 1,
286
287 /*
288 * Called from js_NewGCThing as a last-ditch GC attempt. See comments in
289 * jsgc.c just before js_GC's definition for details.
290 */
291 GC_LAST_DITCH = GC_LOCK_HELD | 2
292 } JSGCInvocationKind;
293
294 extern void
295 js_GC(JSContext *cx, JSGCInvocationKind gckind);
296
297 /* Call this after succesful malloc of memory for GC-related things. */
298 extern void
299 js_UpdateMallocCounter(JSContext *cx, size_t nbytes);
300
301 typedef struct JSGCArenaInfo JSGCArenaInfo;
302 typedef struct JSGCArenaList JSGCArenaList;
303 typedef struct JSGCChunkInfo JSGCChunkInfo;
304
305 struct JSGCArenaList {
306 JSGCArenaInfo *last; /* last allocated GC arena */
307 uint16 lastCount; /* number of allocated things in the last
308 arena */
309 uint16 thingSize; /* size of things to allocate on this list
310 */
311 JSGCThing *freeList; /* list of free GC things */
312 };
313
314 typedef union JSGCDoubleCell JSGCDoubleCell;
315
316 union JSGCDoubleCell {
317 double number;
318 JSGCDoubleCell *link;
319 };
320
321 typedef struct JSGCDoubleArenaList {
322 JSGCArenaInfo *first; /* first allocated GC arena */
323 jsbitmap *nextDoubleFlags; /* bitmask with flags to check for free
324 things */
325 } JSGCDoubleArenaList;
326
327 typedef struct JSGCFreeListSet JSGCFreeListSet;
328
329 struct JSGCFreeListSet {
330 JSGCThing *array[GC_NUM_FREELISTS];
331 JSGCFreeListSet *link;
332 };
333
334 extern const JSGCFreeListSet js_GCEmptyFreeListSet;
335
336 extern void
337 js_RevokeGCLocalFreeLists(JSContext *cx);
338
339 extern void
340 js_DestroyScriptsToGC(JSContext *cx, JSThreadData *data);
341
342 struct JSWeakRoots {
343 /* Most recently created things by type, members of the GC's root set. */
344 void *newborn[GCX_NTYPES];
345
346 /* Atom root for the last-looked-up atom on this context. */
347 jsval lastAtom;
348
349 /* Root for the result of the most recent js_InternalInvoke call. */
350 jsval lastInternalResult;
351 };
352
353 #define JS_CLEAR_WEAK_ROOTS(wr) (memset((wr), 0, sizeof(JSWeakRoots)))
354
355 /*
356 * Increase runtime->gcBytes by sz bytes to account for an allocation outside
357 * the GC that will be freed only after the GC is run. The function may run
358 * the last ditch GC to ensure that gcBytes does not exceed gcMaxBytes. It will
359 * fail if the latter is not possible.
360 *
361 * This function requires that runtime->gcLock is held on entry. On successful
362 * return the lock is still held and on failure it will be released with
363 * the error reported.
364 */
365 extern JSBool
366 js_AddAsGCBytes(JSContext *cx, size_t sz);
367
368 extern void
369 js_RemoveAsGCBytes(JSRuntime* rt, size_t sz);
370
371 #ifdef DEBUG_notme
372 #define JS_GCMETER 1
373 #endif
374
375 #ifdef JS_GCMETER
376
377 typedef struct JSGCArenaStats {
378 uint32 alloc; /* allocation attempts */
379 uint32 localalloc; /* allocations from local lists */
380 uint32 retry; /* allocation retries after running the GC */
381 uint32 fail; /* allocation failures */
382 uint32 nthings; /* live GC things */
383 uint32 maxthings; /* maximum of live GC cells */
384 double totalthings; /* live GC things the GC scanned so far */
385 uint32 narenas; /* number of arena in list before the GC */
386 uint32 newarenas; /* new arenas allocated before the last GC */
387 uint32 livearenas; /* number of live arenas after the last GC */
388 uint32 maxarenas; /* maximum of allocated arenas */
389 uint32 totalarenas; /* total number of arenas with live things that
390 GC scanned so far */
391 } JSGCArenaStats;
392
393 typedef struct JSGCStats {
394 uint32 finalfail; /* finalizer calls allocator failures */
395 uint32 lockborn; /* things born locked */
396 uint32 lock; /* valid lock calls */
397 uint32 unlock; /* valid unlock calls */
398 uint32 depth; /* mark tail recursion depth */
399 uint32 maxdepth; /* maximum mark tail recursion depth */
400 uint32 cdepth; /* mark recursion depth of C functions */
401 uint32 maxcdepth; /* maximum mark recursion depth of C functions */
402 uint32 untraced; /* number of times tracing of GC thing's children were
403 delayed due to a low C stack */
404 #ifdef DEBUG
405 uint32 maxuntraced;/* maximum number of things with children to trace
406 later */
407 #endif
408 uint32 maxlevel; /* maximum GC nesting (indirect recursion) level */
409 uint32 poke; /* number of potentially useful GC calls */
410 uint32 afree; /* thing arenas freed so far */
411 uint32 stackseg; /* total extraordinary stack segments scanned */
412 uint32 segslots; /* total stack segment jsval slots scanned */
413 uint32 nclose; /* number of objects with close hooks */
414 uint32 maxnclose; /* max number of objects with close hooks */
415 uint32 closelater; /* number of close hooks scheduled to run */
416 uint32 maxcloselater; /* max number of close hooks scheduled to run */
417
418 JSGCArenaStats arenaStats[GC_NUM_FREELISTS];
419 JSGCArenaStats doubleArenaStats;
420 } JSGCStats;
421
422 extern JS_FRIEND_API(void)
423 js_DumpGCStats(JSRuntime *rt, FILE *fp);
424
425 #endif /* JS_GCMETER */
426
427 JS_END_EXTERN_C
428
429 #endif /* jsgc_h___ */

  ViewVC Help
Powered by ViewVC 1.1.24