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 ***** */
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"
49 JS_BEGIN_EXTERN_C
51 /* GC thing type indexes. */
52 #define GCX_OBJECT 0 /* JSObject */
53 #define GCX_STRING 1 /* JSString */
54 #define GCX_DOUBLE 2 /* jsdouble */
55 #define GCX_MUTABLE_STRING 3 /* JSString that's mutable --
56 single-threaded only! */
57 #define GCX_EXTERNAL_STRING 4 /* JSString w/ external chars */
58 #define GCX_NTYPES_LOG2 3 /* type index bits */
59 #define GCX_NTYPES JS_BIT(GCX_NTYPES_LOG2)
61 /* GC flag definitions, must fit in 8 bits (type index goes in the low bits). */
62 #define GCF_TYPEMASK JS_BITMASK(GCX_NTYPES_LOG2)
63 #define GCF_MARK JS_BIT(GCX_NTYPES_LOG2)
64 #define GCF_FINAL JS_BIT(GCX_NTYPES_LOG2 + 1)
65 #define GCF_LOCKSHIFT (GCX_NTYPES_LOG2 + 2) /* lock bit shift and mask */
66 #define GCF_LOCKMASK (JS_BITMASK(8 - GCF_LOCKSHIFT) << GCF_LOCKSHIFT)
67 #define GCF_LOCK JS_BIT(GCF_LOCKSHIFT) /* lock request bit in API */
69 /* Pseudo-flag that modifies GCX_STRING to make GCX_MUTABLE_STRING. */
70 #define GCF_MUTABLE 2
72 #if (GCX_STRING | GCF_MUTABLE) != GCX_MUTABLE_STRING
73 # error "mutable string type index botch!"
74 #endif
76 extern uint8 *
77 js_GetGCThingFlags(void *thing);
79 /* These are compatible with JSDHashEntryStub. */
80 struct JSGCRootHashEntry {
81 JSDHashEntryHdr hdr;
82 void *root;
83 const char *name;
84 };
86 struct JSGCLockHashEntry {
87 JSDHashEntryHdr hdr;
88 const JSGCThing *thing;
89 uint32 count;
90 };
92 #if 1
93 /*
94 * Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles
95 * loading oldval. XXX remove implied force, fix jsinterp.c's "second arg
96 * ignored", etc.
97 */
98 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JS_TRUE)
99 #else
100 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JSVAL_IS_GCTHING(oldval))
101 #endif
103 extern intN
104 js_ChangeExternalStringFinalizer(JSStringFinalizeOp oldop,
105 JSStringFinalizeOp newop);
107 extern JSBool
108 js_InitGC(JSRuntime *rt, uint32 maxbytes);
110 extern void
111 js_FinishGC(JSRuntime *rt);
113 extern JSBool
114 js_AddRoot(JSContext *cx, void *rp, const char *name);
116 extern JSBool
117 js_AddRootRT(JSRuntime *rt, void *rp, const char *name);
119 extern JSBool
120 js_RemoveRoot(JSRuntime *rt, void *rp);
122 extern void *
123 js_AllocGCThing(JSContext *cx, uintN flags);
125 extern JSBool
126 js_LockGCThing(JSContext *cx, void *thing);
128 extern JSBool
129 js_LockGCThingRT(JSRuntime *rt, void *thing);
131 extern JSBool
132 js_UnlockGCThingRT(JSRuntime *rt, void *thing);
134 extern JSBool
135 js_IsAboutToBeFinalized(JSContext *cx, void *thing);
137 extern void
138 js_MarkAtom(JSContext *cx, JSAtom *atom, void *arg);
140 /* We avoid a large number of unnecessary calls by doing the flag check first */
141 #define GC_MARK_ATOM(cx, atom, arg) \
142 JS_BEGIN_MACRO \
143 if (!((atom)->flags & ATOM_MARK)) \
144 js_MarkAtom(cx, atom, arg); \
145 JS_END_MACRO
147 extern void
148 js_MarkGCThing(JSContext *cx, void *thing, void *arg);
150 #ifdef GC_MARK_DEBUG
152 typedef struct GCMarkNode GCMarkNode;
154 struct GCMarkNode {
155 void *thing;
156 const char *name;
157 GCMarkNode *next;
158 GCMarkNode *prev;
159 };
161 #define GC_MARK(cx_, thing_, name_, prev_) \
162 JS_BEGIN_MACRO \
163 GCMarkNode node_; \
164 node_.thing = thing_; \
165 node_.name = name_; \
166 node_.next = NULL; \
167 node_.prev = prev_; \
168 if (prev_) ((GCMarkNode *)(prev_))->next = &node_; \
169 js_MarkGCThing(cx_, thing_, &node_); \
170 JS_END_MACRO
172 #else /* !GC_MARK_DEBUG */
174 #define GC_MARK(cx, thing, name, prev) js_MarkGCThing(cx, thing, NULL)
176 #endif /* !GC_MARK_DEBUG */
178 /*
179 * Flags to modify how a GC marks and sweeps:
180 * GC_KEEP_ATOMS Don't sweep unmarked atoms, they may be in use by the
181 * compiler, or by an API function that calls js_Atomize,
182 * when the GC is called from js_AllocGCThing, due to a
183 * malloc failure or the runtime GC-thing limit.
184 * GC_LAST_CONTEXT Called from js_DestroyContext for last JSContext in a
185 * JSRuntime, when it is imperative that rt->gcPoke gets
186 * cleared early in js_GC, if it is set.
187 * GC_ALREADY_LOCKED rt->gcLock is already held on entry to js_GC, and kept
188 * on return to its caller.
189 */
190 #define GC_KEEP_ATOMS 0x1
191 #define GC_LAST_CONTEXT 0x2
192 #define GC_ALREADY_LOCKED 0x4
194 extern void
195 js_ForceGC(JSContext *cx, uintN gcflags);
197 extern void
198 js_GC(JSContext *cx, uintN gcflags);
200 #ifdef JS_GCMETER
202 typedef struct JSGCStats {
203 uint32 alloc; /* number of allocation attempts */
204 uint32 freelen; /* gcFreeList length */
205 uint32 recycle; /* number of things recycled through gcFreeList */
206 uint32 retry; /* allocation attempt retries after running the GC */
207 uint32 fail; /* allocation failures */
208 uint32 finalfail; /* finalizer calls allocator failures */
209 uint32 lock; /* valid lock calls */
210 uint32 unlock; /* valid unlock calls */
211 uint32 stuck; /* stuck reference counts seen by lock calls */
212 uint32 unstuck; /* unlock calls that saw a stuck lock count */
213 uint32 depth; /* mark recursion depth */
214 uint32 maxdepth; /* maximum mark recursion depth */
215 uint32 maxlevel; /* maximum GC nesting (indirect recursion) level */
216 uint32 poke; /* number of potentially useful GC calls */
217 uint32 nopoke; /* useless GC calls where js_PokeGC was not set */
218 uint32 afree; /* thing arenas freed so far */
219 uint32 stackseg; /* total extraordinary stack segments scanned */
220 uint32 segslots; /* total stack segment jsval slots scanned */
221 } JSGCStats;
223 extern void
224 js_DumpGCStats(JSRuntime *rt, FILE *fp);
226 #endif /* JS_GCMETER */
228 JS_END_EXTERN_C
230 #endif /* jsgc_h___ */