Back to index

lightning-sunbird  0.9+nobinonly
jsgc.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef jsgc_h___
00041 #define jsgc_h___
00042 /*
00043  * JS Garbage Collector.
00044  */
00045 #include "jsprvtd.h"
00046 #include "jspubtd.h"
00047 #include "jsdhash.h"
00048 #include "jsutil.h"
00049 
00050 JS_BEGIN_EXTERN_C
00051 
00052 /* GC thing type indexes. */
00053 #define GCX_OBJECT              0               /* JSObject */
00054 #define GCX_STRING              1               /* JSString */
00055 #define GCX_DOUBLE              2               /* jsdouble */
00056 #define GCX_MUTABLE_STRING      3               /* JSString that's mutable --
00057                                                    single-threaded only! */
00058 #define GCX_PRIVATE             4               /* private (unscanned) data */
00059 #define GCX_NAMESPACE           5               /* JSXMLNamespace */
00060 #define GCX_QNAME               6               /* JSXMLQName */
00061 #define GCX_XML                 7               /* JSXML */
00062 #define GCX_EXTERNAL_STRING     8               /* JSString w/ external chars */
00063 
00064 #define GCX_NTYPES_LOG2         4               /* type index bits */
00065 #define GCX_NTYPES              JS_BIT(GCX_NTYPES_LOG2)
00066 
00067 /* GC flag definitions, must fit in 8 bits (type index goes in the low bits). */
00068 #define GCF_TYPEMASK    JS_BITMASK(GCX_NTYPES_LOG2)
00069 #define GCF_MARK        JS_BIT(GCX_NTYPES_LOG2)
00070 #define GCF_FINAL       JS_BIT(GCX_NTYPES_LOG2 + 1)
00071 #define GCF_SYSTEM      JS_BIT(GCX_NTYPES_LOG2 + 2)
00072 #define GCF_LOCKSHIFT   (GCX_NTYPES_LOG2 + 3)   /* lock bit shift */
00073 #define GCF_LOCK        JS_BIT(GCF_LOCKSHIFT)   /* lock request bit in API */
00074 
00075 /* Pseudo-flag that modifies GCX_STRING to make GCX_MUTABLE_STRING. */
00076 #define GCF_MUTABLE     2
00077 
00078 #if (GCX_STRING | GCF_MUTABLE) != GCX_MUTABLE_STRING
00079 # error "mutable string type index botch!"
00080 #endif
00081 
00082 extern uint8 *
00083 js_GetGCThingFlags(void *thing);
00084 
00085 /*
00086  * The sole purpose of the function is to preserve public API compatibility
00087  * in JS_GetStringBytes which takes only single JSString* argument.
00088  */
00089 JSRuntime*
00090 js_GetGCStringRuntime(JSString *str);
00091 
00092 #if 1
00093 /*
00094  * Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles
00095  * loading oldval.  XXX remove implied force, fix jsinterp.c's "second arg
00096  * ignored", etc.
00097  */
00098 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JS_TRUE)
00099 #else
00100 #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JSVAL_IS_GCTHING(oldval))
00101 #endif
00102 
00103 extern intN
00104 js_ChangeExternalStringFinalizer(JSStringFinalizeOp oldop,
00105                                  JSStringFinalizeOp newop);
00106 
00107 extern JSBool
00108 js_InitGC(JSRuntime *rt, uint32 maxbytes);
00109 
00110 extern void
00111 js_FinishGC(JSRuntime *rt);
00112 
00113 extern JSBool
00114 js_AddRoot(JSContext *cx, void *rp, const char *name);
00115 
00116 extern JSBool
00117 js_AddRootRT(JSRuntime *rt, void *rp, const char *name);
00118 
00119 extern JSBool
00120 js_RemoveRoot(JSRuntime *rt, void *rp);
00121 
00122 #ifdef DEBUG
00123 extern void
00124 js_DumpNamedRoots(JSRuntime *rt,
00125                   void (*dump)(const char *name, void *rp, void *data),
00126                   void *data);
00127 #endif
00128 
00129 extern uint32
00130 js_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
00131 
00132 /* Table of pointers with count valid members. */
00133 typedef struct JSPtrTable {
00134     size_t      count;
00135     void        **array;
00136 } JSPtrTable;
00137 
00138 extern JSBool
00139 js_RegisterCloseableIterator(JSContext *cx, JSObject *obj);
00140 
00141 #if JS_HAS_GENERATORS
00142 
00143 /*
00144  * Runtime state to support generators' close hooks.
00145  */
00146 typedef struct JSGCCloseState {
00147     /*
00148      * Singly linked list of generators that are reachable from GC roots or
00149      * were created after the last GC.
00150      */
00151     JSGenerator         *reachableList;
00152 
00153     /*
00154      * Head of the queue of generators that have already become unreachable but
00155      * whose close hooks are not yet run.
00156      */
00157     JSGenerator         *todoQueue;
00158 
00159 #ifndef JS_THREADSAFE
00160     /*
00161      * Flag indicating that the current thread is excuting a close hook for
00162      * single thread case.
00163      */
00164     JSBool              runningCloseHook;
00165 #endif
00166 } JSGCCloseState;
00167 
00168 extern void
00169 js_RegisterGenerator(JSContext *cx, JSGenerator *gen);
00170 
00171 extern JSBool
00172 js_RunCloseHooks(JSContext *cx);
00173 
00174 #endif
00175 
00176 /*
00177  * The private JSGCThing struct, which describes a gcFreeList element.
00178  */
00179 struct JSGCThing {
00180     JSGCThing   *next;
00181     uint8       *flagp;
00182 };
00183 
00184 #define GC_NBYTES_MAX           (10 * sizeof(JSGCThing))
00185 #define GC_NUM_FREELISTS        (GC_NBYTES_MAX / sizeof(JSGCThing))
00186 #define GC_FREELIST_NBYTES(i)   (((i) + 1) * sizeof(JSGCThing))
00187 #define GC_FREELIST_INDEX(n)    (((n) / sizeof(JSGCThing)) - 1)
00188 
00189 extern void *
00190 js_NewGCThing(JSContext *cx, uintN flags, size_t nbytes);
00191 
00192 extern JSBool
00193 js_LockGCThing(JSContext *cx, void *thing);
00194 
00195 extern JSBool
00196 js_LockGCThingRT(JSRuntime *rt, void *thing);
00197 
00198 extern JSBool
00199 js_UnlockGCThingRT(JSRuntime *rt, void *thing);
00200 
00201 extern JSBool
00202 js_IsAboutToBeFinalized(JSContext *cx, void *thing);
00203 
00204 extern void
00205 js_MarkAtom(JSContext *cx, JSAtom *atom);
00206 
00207 /* We avoid a large number of unnecessary calls by doing the flag check first */
00208 #define GC_MARK_ATOM(cx, atom)                                                \
00209     JS_BEGIN_MACRO                                                            \
00210         if (!((atom)->flags & ATOM_MARK))                                     \
00211             js_MarkAtom(cx, atom);                                            \
00212     JS_END_MACRO
00213 
00214 /*
00215  * Always use GC_MARK macro and never call js_MarkGCThing directly so
00216  * when GC_MARK_DEBUG is defined the dump of live GC things does not miss
00217  * a thing.
00218  */
00219 extern void
00220 js_MarkGCThing(JSContext *cx, void *thing);
00221 
00222 #ifdef GC_MARK_DEBUG
00223 
00224 # define GC_MARK(cx, thing, name) js_MarkNamedGCThing(cx, thing, name)
00225 
00226 extern void
00227 js_MarkNamedGCThing(JSContext *cx, void *thing, const char *name);
00228 
00229 extern JS_FRIEND_DATA(FILE *) js_DumpGCHeap;
00230 JS_EXTERN_DATA(void *) js_LiveThingToFind;
00231 
00232 #else
00233 
00234 # define GC_MARK(cx, thing, name) js_MarkGCThing(cx, thing)
00235 
00236 #endif
00237 
00238 extern void
00239 js_MarkStackFrame(JSContext *cx, JSStackFrame *fp);
00240 
00241 /*
00242  * Kinds of js_GC invocation.
00243  */
00244 typedef enum JSGCInvocationKind {
00245     /* Normal invocation. */
00246     GC_NORMAL,
00247 
00248     /*
00249      * Called from js_DestroyContext for last JSContext in a JSRuntime, when
00250      * it is imperative that rt->gcPoke gets cleared early in js_GC.
00251      */
00252     GC_LAST_CONTEXT,
00253 
00254     /*
00255      * Called from js_NewGCThing as a last-ditch GC attempt. See comments
00256      * before js_GC definition for details.
00257      */
00258     GC_LAST_DITCH
00259 } JSGCInvocationKind;
00260 
00261 extern void
00262 js_GC(JSContext *cx, JSGCInvocationKind gckind);
00263 
00264 /* Call this after succesful malloc of memory for GC-related things. */
00265 extern void
00266 js_UpdateMallocCounter(JSContext *cx, size_t nbytes);
00267 
00268 #ifdef DEBUG_notme
00269 #define JS_GCMETER 1
00270 #endif
00271 
00272 #ifdef JS_GCMETER
00273 
00274 typedef struct JSGCStats {
00275 #ifdef JS_THREADSAFE
00276     uint32  localalloc; /* number of succeeded allocations from local lists */
00277 #endif
00278     uint32  alloc;      /* number of allocation attempts */
00279     uint32  retry;      /* allocation attempt retries after running the GC */
00280     uint32  retryhalt;  /* allocation retries halted by the branch callback */
00281     uint32  fail;       /* allocation failures */
00282     uint32  finalfail;  /* finalizer calls allocator failures */
00283     uint32  lockborn;   /* things born locked */
00284     uint32  lock;       /* valid lock calls */
00285     uint32  unlock;     /* valid unlock calls */
00286     uint32  depth;      /* mark tail recursion depth */
00287     uint32  maxdepth;   /* maximum mark tail recursion depth */
00288     uint32  cdepth;     /* mark recursion depth of C functions */
00289     uint32  maxcdepth;  /* maximum mark recursion depth of C functions */
00290     uint32  unscanned;  /* mark C stack overflows or number of times
00291                            GC things were put in unscanned bag */
00292 #ifdef DEBUG
00293     uint32  maxunscanned;       /* maximum size of unscanned bag */
00294 #endif
00295     uint32  maxlevel;   /* maximum GC nesting (indirect recursion) level */
00296     uint32  poke;       /* number of potentially useful GC calls */
00297     uint32  nopoke;     /* useless GC calls where js_PokeGC was not set */
00298     uint32  afree;      /* thing arenas freed so far */
00299     uint32  stackseg;   /* total extraordinary stack segments scanned */
00300     uint32  segslots;   /* total stack segment jsval slots scanned */
00301     uint32  nclose;     /* number of objects with close hooks */
00302     uint32  maxnclose;  /* max number of objects with close hooks */
00303     uint32  closelater; /* number of close hooks scheduled to run */
00304     uint32  maxcloselater; /* max number of close hooks scheduled to run */
00305 } JSGCStats;
00306 
00307 extern JS_FRIEND_API(void)
00308 js_DumpGCStats(JSRuntime *rt, FILE *fp);
00309 
00310 #endif /* JS_GCMETER */
00311 
00312 typedef struct JSGCArena JSGCArena;
00313 typedef struct JSGCArenaList JSGCArenaList;
00314 
00315 #ifdef JS_GCMETER
00316 typedef struct JSGCArenaStats JSGCArenaStats;
00317 
00318 struct JSGCArenaStats {
00319     uint32  narenas;        /* number of arena in list */
00320     uint32  maxarenas;      /* maximun number of allocated arenas */
00321     uint32  nthings;        /* number of allocates JSGCThing */
00322     uint32  maxthings;      /* maximum number number of allocates JSGCThing */
00323     uint32  totalnew;       /* number of succeeded calls to js_NewGCThing */
00324     uint32  freelen;        /* freeList lengths */
00325     uint32  recycle;        /* number of things recycled through freeList */
00326     uint32  totalarenas;    /* total number of arenas with live things that
00327                                GC scanned so far */
00328     uint32  totalfreelen;   /* total number of things that GC put to free
00329                                list so far */
00330 };
00331 #endif
00332 
00333 struct JSGCArenaList {
00334     JSGCArena   *last;          /* last allocated GC arena */
00335     uint16      lastLimit;      /* end offset of allocated so far things in
00336                                    the last arena */
00337     uint16      thingSize;      /* size of things to allocate on this list */
00338     JSGCThing   *freeList;      /* list of free GC things */
00339 #ifdef JS_GCMETER
00340     JSGCArenaStats stats;
00341 #endif
00342 };
00343 
00344 struct JSWeakRoots {
00345     /* Most recently created things by type, members of the GC's root set. */
00346     JSGCThing           *newborn[GCX_NTYPES];
00347 
00348     /* Atom root for the last-looked-up atom on this context. */
00349     JSAtom              *lastAtom;
00350 
00351     /* Root for the result of the most recent js_InternalInvoke call. */
00352     jsval               lastInternalResult;
00353 };
00354 
00355 JS_STATIC_ASSERT(JSVAL_NULL == 0);
00356 #define JS_CLEAR_WEAK_ROOTS(wr) (memset((wr), 0, sizeof(JSWeakRoots)))
00357 
00358 JS_END_EXTERN_C
00359 
00360 #endif /* jsgc_h___ */