1 |
siliconforks |
332 |
/* -*- 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 |
siliconforks |
507 |
#include "jstask.h" |
51 |
siliconforks |
332 |
|
52 |
|
|
JS_BEGIN_EXTERN_C |
53 |
|
|
|
54 |
|
|
#define JSTRACE_XML 3 |
55 |
|
|
|
56 |
|
|
/* |
57 |
|
|
* One past the maximum trace kind. |
58 |
|
|
*/ |
59 |
|
|
#define JSTRACE_LIMIT 4 |
60 |
|
|
|
61 |
|
|
/* |
62 |
|
|
* We use the trace kinds as the types for all GC things except external |
63 |
|
|
* strings. |
64 |
|
|
*/ |
65 |
|
|
#define GCX_OBJECT JSTRACE_OBJECT /* JSObject */ |
66 |
|
|
#define GCX_DOUBLE JSTRACE_DOUBLE /* jsdouble */ |
67 |
|
|
#define GCX_STRING JSTRACE_STRING /* JSString */ |
68 |
|
|
#define GCX_XML JSTRACE_XML /* JSXML */ |
69 |
|
|
#define GCX_EXTERNAL_STRING JSTRACE_LIMIT /* JSString with external |
70 |
|
|
chars */ |
71 |
|
|
/* |
72 |
siliconforks |
460 |
* The number of defined GC types and the maximum limit for the number of |
73 |
|
|
* possible GC types. |
74 |
siliconforks |
332 |
*/ |
75 |
|
|
#define GCX_NTYPES (GCX_EXTERNAL_STRING + 8) |
76 |
|
|
#define GCX_LIMIT_LOG2 4 /* type index bits */ |
77 |
|
|
#define GCX_LIMIT JS_BIT(GCX_LIMIT_LOG2) |
78 |
|
|
|
79 |
|
|
/* GC flag definitions, must fit in 8 bits (type index goes in the low bits). */ |
80 |
|
|
#define GCF_TYPEMASK JS_BITMASK(GCX_LIMIT_LOG2) |
81 |
|
|
#define GCF_MARK JS_BIT(GCX_LIMIT_LOG2) |
82 |
|
|
#define GCF_FINAL JS_BIT(GCX_LIMIT_LOG2 + 1) |
83 |
|
|
#define GCF_LOCKSHIFT (GCX_LIMIT_LOG2 + 2) /* lock bit shift */ |
84 |
|
|
#define GCF_LOCK JS_BIT(GCF_LOCKSHIFT) /* lock request bit in API */ |
85 |
|
|
|
86 |
|
|
/* |
87 |
|
|
* Get the type of the external string or -1 if the string was not created |
88 |
|
|
* with JS_NewExternalString. |
89 |
|
|
*/ |
90 |
|
|
extern intN |
91 |
|
|
js_GetExternalStringGCType(JSString *str); |
92 |
|
|
|
93 |
|
|
extern JS_FRIEND_API(uint32) |
94 |
|
|
js_GetGCThingTraceKind(void *thing); |
95 |
|
|
|
96 |
|
|
/* |
97 |
|
|
* The sole purpose of the function is to preserve public API compatibility |
98 |
|
|
* in JS_GetStringBytes which takes only single JSString* argument. |
99 |
|
|
*/ |
100 |
|
|
JSRuntime* |
101 |
|
|
js_GetGCStringRuntime(JSString *str); |
102 |
|
|
|
103 |
|
|
#if 1 |
104 |
|
|
/* |
105 |
|
|
* Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles |
106 |
|
|
* loading oldval. XXX remove implied force, fix jsinterp.c's "second arg |
107 |
|
|
* ignored", etc. |
108 |
|
|
*/ |
109 |
|
|
#define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JS_TRUE) |
110 |
|
|
#else |
111 |
|
|
#define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JSVAL_IS_GCTHING(oldval)) |
112 |
|
|
#endif |
113 |
|
|
|
114 |
|
|
extern JSBool |
115 |
|
|
js_InitGC(JSRuntime *rt, uint32 maxbytes); |
116 |
|
|
|
117 |
|
|
extern void |
118 |
|
|
js_FinishGC(JSRuntime *rt); |
119 |
|
|
|
120 |
|
|
extern intN |
121 |
|
|
js_ChangeExternalStringFinalizer(JSStringFinalizeOp oldop, |
122 |
|
|
JSStringFinalizeOp newop); |
123 |
|
|
|
124 |
|
|
extern JSBool |
125 |
|
|
js_AddRoot(JSContext *cx, void *rp, const char *name); |
126 |
|
|
|
127 |
|
|
extern JSBool |
128 |
|
|
js_AddRootRT(JSRuntime *rt, void *rp, const char *name); |
129 |
|
|
|
130 |
|
|
extern JSBool |
131 |
|
|
js_RemoveRoot(JSRuntime *rt, void *rp); |
132 |
|
|
|
133 |
|
|
#ifdef DEBUG |
134 |
|
|
extern void |
135 |
|
|
js_DumpNamedRoots(JSRuntime *rt, |
136 |
|
|
void (*dump)(const char *name, void *rp, void *data), |
137 |
|
|
void *data); |
138 |
|
|
#endif |
139 |
|
|
|
140 |
|
|
extern uint32 |
141 |
|
|
js_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data); |
142 |
|
|
|
143 |
|
|
/* Table of pointers with count valid members. */ |
144 |
|
|
typedef struct JSPtrTable { |
145 |
|
|
size_t count; |
146 |
|
|
void **array; |
147 |
|
|
} JSPtrTable; |
148 |
|
|
|
149 |
|
|
extern JSBool |
150 |
|
|
js_RegisterCloseableIterator(JSContext *cx, JSObject *obj); |
151 |
|
|
|
152 |
|
|
/* |
153 |
|
|
* The private JSGCThing struct, which describes a gcFreeList element. |
154 |
|
|
*/ |
155 |
|
|
struct JSGCThing { |
156 |
|
|
JSGCThing *next; |
157 |
|
|
uint8 *flagp; |
158 |
|
|
}; |
159 |
|
|
|
160 |
|
|
#define GC_NBYTES_MAX (10 * sizeof(JSGCThing)) |
161 |
|
|
#define GC_NUM_FREELISTS (GC_NBYTES_MAX / sizeof(JSGCThing)) |
162 |
|
|
#define GC_FREELIST_NBYTES(i) (((i) + 1) * sizeof(JSGCThing)) |
163 |
|
|
#define GC_FREELIST_INDEX(n) (((n) / sizeof(JSGCThing)) - 1) |
164 |
|
|
|
165 |
|
|
/* |
166 |
|
|
* Allocates a new GC thing of the given size. After a successful allocation |
167 |
|
|
* the caller must fully initialize the thing before calling any function that |
168 |
|
|
* can potentially trigger GC. This will ensure that GC tracing never sees junk |
169 |
|
|
* values stored in the partially initialized thing. |
170 |
|
|
*/ |
171 |
siliconforks |
507 |
extern JSObject* |
172 |
|
|
js_NewGCObject(JSContext *cx, uintN flags); |
173 |
siliconforks |
332 |
|
174 |
siliconforks |
507 |
extern JSString* |
175 |
|
|
js_NewGCString(JSContext *cx, uintN flags); |
176 |
|
|
|
177 |
|
|
extern JSFunction* |
178 |
|
|
js_NewGCFunction(JSContext *cx, uintN flags); |
179 |
|
|
|
180 |
|
|
extern JSXML* |
181 |
|
|
js_NewGCXML(JSContext *cx, uintN flags); |
182 |
|
|
|
183 |
siliconforks |
332 |
/* |
184 |
|
|
* Allocate a new double jsval and store the result in *vp. vp must be a root. |
185 |
|
|
* The function does not copy the result into any weak root. |
186 |
|
|
*/ |
187 |
|
|
extern JSBool |
188 |
|
|
js_NewDoubleInRootedValue(JSContext *cx, jsdouble d, jsval *vp); |
189 |
|
|
|
190 |
|
|
/* |
191 |
|
|
* Return a pointer to a new GC-allocated and weakly-rooted jsdouble number, |
192 |
|
|
* or null when the allocation fails. |
193 |
|
|
*/ |
194 |
|
|
extern jsdouble * |
195 |
|
|
js_NewWeaklyRootedDouble(JSContext *cx, jsdouble d); |
196 |
|
|
|
197 |
siliconforks |
460 |
#ifdef JS_TRACER |
198 |
siliconforks |
332 |
extern JSBool |
199 |
siliconforks |
460 |
js_ReserveObjects(JSContext *cx, size_t nobjects); |
200 |
|
|
#endif |
201 |
|
|
|
202 |
|
|
extern JSBool |
203 |
siliconforks |
332 |
js_LockGCThingRT(JSRuntime *rt, void *thing); |
204 |
|
|
|
205 |
|
|
extern JSBool |
206 |
|
|
js_UnlockGCThingRT(JSRuntime *rt, void *thing); |
207 |
|
|
|
208 |
|
|
extern JSBool |
209 |
|
|
js_IsAboutToBeFinalized(JSContext *cx, void *thing); |
210 |
|
|
|
211 |
|
|
/* |
212 |
|
|
* Macro to test if a traversal is the marking phase of GC to avoid exposing |
213 |
|
|
* ScriptFilenameEntry to traversal implementations. |
214 |
|
|
*/ |
215 |
|
|
#define IS_GC_MARKING_TRACER(trc) ((trc)->callback == NULL) |
216 |
|
|
|
217 |
|
|
#if JS_HAS_XML_SUPPORT |
218 |
|
|
# define JS_IS_VALID_TRACE_KIND(kind) ((uint32)(kind) < JSTRACE_LIMIT) |
219 |
|
|
#else |
220 |
|
|
# define JS_IS_VALID_TRACE_KIND(kind) ((uint32)(kind) <= JSTRACE_STRING) |
221 |
|
|
#endif |
222 |
|
|
|
223 |
|
|
/* |
224 |
siliconforks |
507 |
* Trace jsval when JSVAL_IS_OBJECT(v) can be a GC thing pointer tagged as a |
225 |
|
|
* jsval. NB: punning an arbitrary JSString * as an untagged (object-tagged) |
226 |
|
|
* jsval no longer works due to static int and unit strings! |
227 |
siliconforks |
332 |
*/ |
228 |
|
|
extern void |
229 |
|
|
js_CallValueTracerIfGCThing(JSTracer *trc, jsval v); |
230 |
|
|
|
231 |
|
|
extern void |
232 |
|
|
js_TraceStackFrame(JSTracer *trc, JSStackFrame *fp); |
233 |
|
|
|
234 |
siliconforks |
460 |
extern JS_REQUIRES_STACK void |
235 |
siliconforks |
332 |
js_TraceRuntime(JSTracer *trc, JSBool allAtoms); |
236 |
|
|
|
237 |
siliconforks |
460 |
extern JS_REQUIRES_STACK JS_FRIEND_API(void) |
238 |
siliconforks |
332 |
js_TraceContext(JSTracer *trc, JSContext *acx); |
239 |
|
|
|
240 |
|
|
/* |
241 |
siliconforks |
460 |
* Schedule the GC call at a later safe point. |
242 |
|
|
*/ |
243 |
|
|
#ifndef JS_THREADSAFE |
244 |
|
|
# define js_TriggerGC(cx, gcLocked) js_TriggerGC (cx) |
245 |
|
|
#endif |
246 |
|
|
|
247 |
|
|
extern void |
248 |
|
|
js_TriggerGC(JSContext *cx, JSBool gcLocked); |
249 |
|
|
|
250 |
|
|
/* |
251 |
siliconforks |
332 |
* Kinds of js_GC invocation. |
252 |
|
|
*/ |
253 |
|
|
typedef enum JSGCInvocationKind { |
254 |
|
|
/* Normal invocation. */ |
255 |
|
|
GC_NORMAL = 0, |
256 |
|
|
|
257 |
|
|
/* |
258 |
|
|
* Called from js_DestroyContext for last JSContext in a JSRuntime, when |
259 |
|
|
* it is imperative that rt->gcPoke gets cleared early in js_GC. |
260 |
|
|
*/ |
261 |
|
|
GC_LAST_CONTEXT = 1, |
262 |
|
|
|
263 |
|
|
/* |
264 |
|
|
* Flag bit telling js_GC that the caller has already acquired rt->gcLock. |
265 |
|
|
* Currently, this flag is set for the invocation kinds that also preserve |
266 |
|
|
* atoms and weak roots, so we don't need another bit for GC_KEEP_ATOMS. |
267 |
|
|
*/ |
268 |
|
|
GC_LOCK_HELD = 0x10, |
269 |
|
|
GC_KEEP_ATOMS = GC_LOCK_HELD, |
270 |
|
|
|
271 |
|
|
/* |
272 |
|
|
* Called from js_SetProtoOrParent with a request to set an object's proto |
273 |
|
|
* or parent slot inserted on rt->setSlotRequests. |
274 |
|
|
*/ |
275 |
|
|
GC_SET_SLOT_REQUEST = GC_LOCK_HELD | 1, |
276 |
|
|
|
277 |
|
|
/* |
278 |
|
|
* Called from js_NewGCThing as a last-ditch GC attempt. See comments in |
279 |
|
|
* jsgc.c just before js_GC's definition for details. |
280 |
|
|
*/ |
281 |
|
|
GC_LAST_DITCH = GC_LOCK_HELD | 2 |
282 |
|
|
} JSGCInvocationKind; |
283 |
|
|
|
284 |
|
|
extern void |
285 |
|
|
js_GC(JSContext *cx, JSGCInvocationKind gckind); |
286 |
|
|
|
287 |
|
|
typedef struct JSGCArenaInfo JSGCArenaInfo; |
288 |
|
|
typedef struct JSGCArenaList JSGCArenaList; |
289 |
|
|
typedef struct JSGCChunkInfo JSGCChunkInfo; |
290 |
|
|
|
291 |
|
|
struct JSGCArenaList { |
292 |
|
|
JSGCArenaInfo *last; /* last allocated GC arena */ |
293 |
siliconforks |
507 |
uint32 lastCount; /* number of allocated things in the last |
294 |
siliconforks |
332 |
arena */ |
295 |
siliconforks |
507 |
uint32 thingSize; /* size of things to allocate on this list |
296 |
siliconforks |
332 |
*/ |
297 |
|
|
JSGCThing *freeList; /* list of free GC things */ |
298 |
|
|
}; |
299 |
|
|
|
300 |
|
|
typedef union JSGCDoubleCell JSGCDoubleCell; |
301 |
|
|
|
302 |
|
|
union JSGCDoubleCell { |
303 |
|
|
double number; |
304 |
|
|
JSGCDoubleCell *link; |
305 |
|
|
}; |
306 |
|
|
|
307 |
|
|
typedef struct JSGCDoubleArenaList { |
308 |
|
|
JSGCArenaInfo *first; /* first allocated GC arena */ |
309 |
|
|
jsbitmap *nextDoubleFlags; /* bitmask with flags to check for free |
310 |
|
|
things */ |
311 |
|
|
} JSGCDoubleArenaList; |
312 |
|
|
|
313 |
|
|
extern void |
314 |
siliconforks |
460 |
js_DestroyScriptsToGC(JSContext *cx, JSThreadData *data); |
315 |
|
|
|
316 |
siliconforks |
332 |
struct JSWeakRoots { |
317 |
|
|
/* Most recently created things by type, members of the GC's root set. */ |
318 |
|
|
void *newborn[GCX_NTYPES]; |
319 |
|
|
|
320 |
|
|
/* Atom root for the last-looked-up atom on this context. */ |
321 |
|
|
jsval lastAtom; |
322 |
|
|
|
323 |
|
|
/* Root for the result of the most recent js_InternalInvoke call. */ |
324 |
|
|
jsval lastInternalResult; |
325 |
|
|
}; |
326 |
|
|
|
327 |
|
|
#define JS_CLEAR_WEAK_ROOTS(wr) (memset((wr), 0, sizeof(JSWeakRoots))) |
328 |
|
|
|
329 |
siliconforks |
507 |
#ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */ |
330 |
|
|
#ifdef JS_THREADSAFE |
331 |
|
|
class JSFreePointerListTask : public JSBackgroundTask { |
332 |
|
|
void *head; |
333 |
|
|
public: |
334 |
|
|
JSFreePointerListTask() : head(NULL) {} |
335 |
|
|
|
336 |
|
|
void add(void* ptr) { |
337 |
|
|
*(void**)ptr = head; |
338 |
|
|
head = ptr; |
339 |
|
|
} |
340 |
|
|
|
341 |
|
|
void run() { |
342 |
|
|
void *ptr = head; |
343 |
|
|
while (ptr) { |
344 |
|
|
void *next = *(void **)ptr; |
345 |
|
|
js_free(ptr); |
346 |
|
|
ptr = next; |
347 |
|
|
} |
348 |
|
|
} |
349 |
|
|
}; |
350 |
|
|
#endif |
351 |
|
|
#endif /* __cplusplus */ |
352 |
|
|
|
353 |
siliconforks |
332 |
/* |
354 |
siliconforks |
507 |
* Free the chars held by str when it is finalized by the GC. When type is |
355 |
|
|
* less then zero, it denotes an internal string. Otherwise it denotes the |
356 |
|
|
* type of the external string allocated with JS_NewExternalString. |
357 |
siliconforks |
332 |
* |
358 |
siliconforks |
507 |
* This function always needs rt but can live with null cx. |
359 |
siliconforks |
332 |
*/ |
360 |
|
|
extern void |
361 |
siliconforks |
507 |
js_FinalizeStringRT(JSRuntime *rt, JSString *str, intN type, JSContext *cx); |
362 |
siliconforks |
332 |
|
363 |
|
|
#ifdef DEBUG_notme |
364 |
|
|
#define JS_GCMETER 1 |
365 |
|
|
#endif |
366 |
|
|
|
367 |
|
|
#ifdef JS_GCMETER |
368 |
|
|
|
369 |
|
|
typedef struct JSGCArenaStats { |
370 |
|
|
uint32 alloc; /* allocation attempts */ |
371 |
|
|
uint32 localalloc; /* allocations from local lists */ |
372 |
|
|
uint32 retry; /* allocation retries after running the GC */ |
373 |
|
|
uint32 fail; /* allocation failures */ |
374 |
|
|
uint32 nthings; /* live GC things */ |
375 |
|
|
uint32 maxthings; /* maximum of live GC cells */ |
376 |
|
|
double totalthings; /* live GC things the GC scanned so far */ |
377 |
|
|
uint32 narenas; /* number of arena in list before the GC */ |
378 |
|
|
uint32 newarenas; /* new arenas allocated before the last GC */ |
379 |
|
|
uint32 livearenas; /* number of live arenas after the last GC */ |
380 |
|
|
uint32 maxarenas; /* maximum of allocated arenas */ |
381 |
|
|
uint32 totalarenas; /* total number of arenas with live things that |
382 |
|
|
GC scanned so far */ |
383 |
|
|
} JSGCArenaStats; |
384 |
|
|
|
385 |
|
|
typedef struct JSGCStats { |
386 |
|
|
uint32 finalfail; /* finalizer calls allocator failures */ |
387 |
|
|
uint32 lockborn; /* things born locked */ |
388 |
|
|
uint32 lock; /* valid lock calls */ |
389 |
|
|
uint32 unlock; /* valid unlock calls */ |
390 |
|
|
uint32 depth; /* mark tail recursion depth */ |
391 |
|
|
uint32 maxdepth; /* maximum mark tail recursion depth */ |
392 |
|
|
uint32 cdepth; /* mark recursion depth of C functions */ |
393 |
|
|
uint32 maxcdepth; /* maximum mark recursion depth of C functions */ |
394 |
|
|
uint32 untraced; /* number of times tracing of GC thing's children were |
395 |
|
|
delayed due to a low C stack */ |
396 |
|
|
#ifdef DEBUG |
397 |
|
|
uint32 maxuntraced;/* maximum number of things with children to trace |
398 |
|
|
later */ |
399 |
|
|
#endif |
400 |
|
|
uint32 maxlevel; /* maximum GC nesting (indirect recursion) level */ |
401 |
|
|
uint32 poke; /* number of potentially useful GC calls */ |
402 |
|
|
uint32 afree; /* thing arenas freed so far */ |
403 |
|
|
uint32 stackseg; /* total extraordinary stack segments scanned */ |
404 |
|
|
uint32 segslots; /* total stack segment jsval slots scanned */ |
405 |
|
|
uint32 nclose; /* number of objects with close hooks */ |
406 |
|
|
uint32 maxnclose; /* max number of objects with close hooks */ |
407 |
|
|
uint32 closelater; /* number of close hooks scheduled to run */ |
408 |
|
|
uint32 maxcloselater; /* max number of close hooks scheduled to run */ |
409 |
|
|
|
410 |
|
|
JSGCArenaStats arenaStats[GC_NUM_FREELISTS]; |
411 |
|
|
JSGCArenaStats doubleArenaStats; |
412 |
|
|
} JSGCStats; |
413 |
|
|
|
414 |
|
|
extern JS_FRIEND_API(void) |
415 |
|
|
js_DumpGCStats(JSRuntime *rt, FILE *fp); |
416 |
|
|
|
417 |
|
|
#endif /* JS_GCMETER */ |
418 |
|
|
|
419 |
|
|
JS_END_EXTERN_C |
420 |
|
|
|
421 |
|
|
#endif /* jsgc_h___ */ |