Code

fix 1243587 and misc fixes
[inkscape.git] / src / dom / js / jsgc.h
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_PRIVATE             4               /* private (unscanned) data */
58 #define GCX_NAMESPACE           5               /* JSXMLNamespace */
59 #define GCX_QNAME               6               /* JSXMLQName */
60 #define GCX_XML                 7               /* JSXML */
61 #define GCX_EXTERNAL_STRING     8               /* JSString w/ external chars */
63 #define GCX_NTYPES_LOG2         4               /* type index bits */
64 #define GCX_NTYPES              JS_BIT(GCX_NTYPES_LOG2)
66 /* GC flag definitions, must fit in 8 bits (type index goes in the low bits). */
67 #define GCF_TYPEMASK    JS_BITMASK(GCX_NTYPES_LOG2)
68 #define GCF_MARK        JS_BIT(GCX_NTYPES_LOG2)
69 #define GCF_FINAL       JS_BIT(GCX_NTYPES_LOG2 + 1)
70 #define GCF_SYSTEM      JS_BIT(GCX_NTYPES_LOG2 + 2)
71 #define GCF_LOCKSHIFT   (GCX_NTYPES_LOG2 + 3)   /* lock bit shift */
72 #define GCF_LOCK        JS_BIT(GCF_LOCKSHIFT)   /* lock request bit in API */
74 /* Pseudo-flag that modifies GCX_STRING to make GCX_MUTABLE_STRING. */
75 #define GCF_MUTABLE     2
77 #if (GCX_STRING | GCF_MUTABLE) != GCX_MUTABLE_STRING
78 # error "mutable string type index botch!"
79 #endif
81 extern uint8 *
82 js_GetGCThingFlags(void *thing);
84 /* These are compatible with JSDHashEntryStub. */
85 struct JSGCRootHashEntry {
86     JSDHashEntryHdr hdr;
87     void            *root;
88     const char      *name;
89 };
91 struct JSGCLockHashEntry {
92     JSDHashEntryHdr hdr;
93     const JSGCThing *thing;
94     uint32          count;
95 };
97 #if 1
98 /*
99  * Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles
100  * loading oldval.  XXX remove implied force, fix jsinterp.c's "second arg
101  * ignored", etc.
102  */
103 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JS_TRUE)
104 #else
105 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JSVAL_IS_GCTHING(oldval))
106 #endif
108 extern intN
109 js_ChangeExternalStringFinalizer(JSStringFinalizeOp oldop,
110                                  JSStringFinalizeOp newop);
112 extern JSBool
113 js_InitGC(JSRuntime *rt, uint32 maxbytes);
115 extern void
116 js_FinishGC(JSRuntime *rt);
118 extern JSBool
119 js_AddRoot(JSContext *cx, void *rp, const char *name);
121 extern JSBool
122 js_AddRootRT(JSRuntime *rt, void *rp, const char *name);
124 extern JSBool
125 js_RemoveRoot(JSRuntime *rt, void *rp);
127 /*
128  * The private JSGCThing struct, which describes a gcFreeList element.
129  */
130 struct JSGCThing {
131     JSGCThing   *next;
132     uint8       *flagp;
133 };
135 #define GC_NBYTES_MAX           (10 * sizeof(JSGCThing))
136 #define GC_NUM_FREELISTS        (GC_NBYTES_MAX / sizeof(JSGCThing))
137 #define GC_FREELIST_NBYTES(i)   (((i) + 1) * sizeof(JSGCThing))
138 #define GC_FREELIST_INDEX(n)    (((n) / sizeof(JSGCThing)) - 1)
140 extern void *
141 js_NewGCThing(JSContext *cx, uintN flags, size_t nbytes);
143 extern JSBool
144 js_LockGCThing(JSContext *cx, void *thing);
146 extern JSBool
147 js_LockGCThingRT(JSRuntime *rt, void *thing);
149 extern JSBool
150 js_UnlockGCThingRT(JSRuntime *rt, void *thing);
152 extern JSBool
153 js_IsAboutToBeFinalized(JSContext *cx, void *thing);
155 extern void
156 js_MarkAtom(JSContext *cx, JSAtom *atom, void *arg);
158 /* We avoid a large number of unnecessary calls by doing the flag check first */
159 #define GC_MARK_ATOM(cx, atom, arg)                                           \
160     JS_BEGIN_MACRO                                                            \
161         if (!((atom)->flags & ATOM_MARK))                                     \
162             js_MarkAtom(cx, atom, arg);                                       \
163     JS_END_MACRO
165 extern void
166 js_MarkGCThing(JSContext *cx, void *thing, void *arg);
168 #ifdef GC_MARK_DEBUG
170 typedef struct GCMarkNode GCMarkNode;
172 struct GCMarkNode {
173     void        *thing;
174     const char  *name;
175     GCMarkNode  *next;
176     GCMarkNode  *prev;
177 };
179 #define GC_MARK(cx_, thing_, name_, prev_)                                    \
180     JS_BEGIN_MACRO                                                            \
181         GCMarkNode node_;                                                     \
182         node_.thing = thing_;                                                 \
183         node_.name  = name_;                                                  \
184         node_.next  = NULL;                                                   \
185         node_.prev  = prev_;                                                  \
186         if (prev_) ((GCMarkNode *)(prev_))->next = &node_;                    \
187         js_MarkGCThing(cx_, thing_, &node_);                                  \
188     JS_END_MACRO
190 extern JS_FRIEND_DATA(FILE *) js_DumpGCHeap;
191 JS_EXTERN_DATA(void *) js_LiveThingToFind;
193 #else  /* !GC_MARK_DEBUG */
195 #define GC_MARK(cx, thing, name, prev)   js_MarkGCThing(cx, thing, NULL)
197 #endif /* !GC_MARK_DEBUG */
199 /*
200  * Flags to modify how a GC marks and sweeps:
201  *   GC_KEEP_ATOMS      Don't sweep unmarked atoms, they may be in use by the
202  *                      compiler, or by an API function that calls js_Atomize,
203  *                      when the GC is called from js_NewGCThing, due to a
204  *                      malloc failure or the runtime GC-thing limit.
205  *   GC_LAST_CONTEXT    Called from js_DestroyContext for last JSContext in a
206  *                      JSRuntime, when it is imperative that rt->gcPoke gets
207  *                      cleared early in js_GC, if it is set.
208  *   GC_ALREADY_LOCKED  rt->gcLock is already held on entry to js_GC, and kept
209  *                      on return to its caller.
210  */
211 #define GC_KEEP_ATOMS       0x1
212 #define GC_LAST_CONTEXT     0x2
213 #define GC_ALREADY_LOCKED   0x4
215 extern void
216 js_ForceGC(JSContext *cx, uintN gcflags);
218 extern void
219 js_GC(JSContext *cx, uintN gcflags);
221 #ifdef DEBUG_notme
222 #define JS_GCMETER 1
223 #endif
225 #ifdef JS_GCMETER
227 typedef struct JSGCStats {
228     uint32  alloc;      /* number of allocation attempts */
229     uint32  freelen[GC_NUM_FREELISTS];
230                         /* gcFreeList lengths */
231     uint32  recycle[GC_NUM_FREELISTS];
232                         /* number of things recycled through gcFreeList */
233     uint32  retry;      /* allocation attempt retries after running the GC */
234     uint32  retryhalt;  /* allocation retries halted by the branch callback */
235     uint32  fail;       /* allocation failures */
236     uint32  finalfail;  /* finalizer calls allocator failures */
237     uint32  lockborn;   /* things born locked */
238     uint32  lock;       /* valid lock calls */
239     uint32  unlock;     /* valid unlock calls */
240     uint32  depth;      /* mark tail recursion depth */
241     uint32  maxdepth;   /* maximum mark tail recursion depth */
242     uint32  cdepth;     /* mark recursion depth of C functions */
243     uint32  maxcdepth;  /* maximum mark recursion depth of C functions */
244     uint32  dswmark;    /* mark C stack overflows => Deutsch-Schorr-Waite */
245     uint32  dswdepth;   /* DSW mark depth */
246     uint32  maxdswdepth;/* maximum DSW mark depth */
247     uint32  dswup;      /* DSW moves up the mark spanning tree */
248     uint32  dswupstep;  /* steps in obj->slots to find DSW-reversed pointer */
249     uint32  maxlevel;   /* maximum GC nesting (indirect recursion) level */
250     uint32  poke;       /* number of potentially useful GC calls */
251     uint32  nopoke;     /* useless GC calls where js_PokeGC was not set */
252     uint32  afree;      /* thing arenas freed so far */
253     uint32  stackseg;   /* total extraordinary stack segments scanned */
254     uint32  segslots;   /* total stack segment jsval slots scanned */
255 } JSGCStats;
257 extern JS_FRIEND_API(void)
258 js_DumpGCStats(JSRuntime *rt, FILE *fp);
260 #endif /* JS_GCMETER */
262 #ifdef DEBUG_notme
263 #define TOO_MUCH_GC 1
264 #endif
266 #ifdef WAY_TOO_MUCH_GC
267 #define TOO_MUCH_GC 1
268 #endif
270 JS_END_EXTERN_C
272 #endif /* jsgc_h___ */