home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / lib / msgc / include / prgc.h < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.4 KB  |  401 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef prgc_h___
  20. #define prgc_h___
  21.  
  22. /*
  23. ** API to NSPR gc memory system.
  24. */
  25. #include "prtypes.h"
  26. #include "prmon.h"
  27. #include "prthread.h"
  28. #include <stdio.h>
  29.  
  30. #if defined(WIN16)
  31. #define GCPTR __far
  32. #else
  33. #define GCPTR
  34. #endif
  35.  
  36.  
  37. PR_BEGIN_EXTERN_C
  38.  
  39. /*
  40. ** Initialize the garbage collector.
  41. **     "flags" is the trace flags (see below).
  42. **     "initialHeapSize" is the initial size of the heap and may be zero
  43. **        if the default is desired.
  44. **    "segmentSize" is the size of each segment of memory added to the
  45. **       heap when the heap is grown.
  46. */
  47. PR_EXTERN(void) PR_InitGC(
  48.     PRWord flags, PRInt32 initialHeapSize, PRInt32 segmentSize, PRThreadScope scope);
  49.  
  50. /*
  51. ** Shuts down gc and frees up all memory associated with it. 
  52. */
  53. PR_EXTERN(void) PR_ShutdownGC(PRBool finalizeOnExit);
  54.  
  55. /*
  56. ** This walk function will be called for every gc object in the
  57. ** heap as it is walked. If it returns non-zero, the walk is terminated.
  58. */
  59. typedef PRInt32 (*PRWalkFun)(void GCPTR* obj, void* data);
  60.  
  61. /*
  62. ** GC Type record. This defines all of the GC operations used on a
  63. ** particular object type. These structures are passed to
  64. ** PR_RegisterType.
  65. */
  66. typedef struct GCType {
  67.     /*
  68.     ** Scan an object that is in the GC heap and call PR_LiveObject on
  69.     ** all of the pointers in it. If this slot is null then the object
  70.     ** won't be scanned (i.e. it has no embedded pointers).
  71.     */
  72.     void (PR_CALLBACK *scan)(void GCPTR *obj);
  73.  
  74.     /*
  75.     ** Finalize an object that has no references. This is called by the
  76.     ** GC after it has determined where the object debris is but before
  77.     ** it has moved the debris to the logical "free list". The object is
  78.     ** marked alive for this call and removed from the list of objects
  79.     ** that need finalization (finalization only happens once for an
  80.     ** object). If this slot is null then the object doesn't need
  81.     ** finalization.
  82.     */
  83.     void (PR_CALLBACK *finalize)(void GCPTR *obj);
  84.  
  85.     /*
  86.     ** Dump out an object during a PR_DumpGCHeap(). This is used as a
  87.     ** debugging tool.
  88.     */
  89.     void (PR_CALLBACK *dump)(FILE *out, void GCPTR *obj, PRBool detailed, PRIntn indentLevel);
  90.  
  91.     /*
  92.     ** Add object to summary table.
  93.     */
  94.     void (PR_CALLBACK *summarize)(void GCPTR *obj, PRUint32 bytes);
  95.  
  96.     /*
  97.     ** Free hook called by GC when the object is being freed.
  98.     */
  99.     void (PR_CALLBACK *free)(void *obj);
  100.  
  101.     /* Weak pointer support: If the object has a weak pointer (Note:
  102.        at most one), this function is used to get the weak link's
  103.        offset from the start of the body of a gc object */
  104.     PRUint32 (PR_CALLBACK *getWeakLinkOffset)(void *obj);
  105.  
  106.     /* Descriptive character for dumping this GCType */
  107.     char kindChar;
  108.  
  109.     /*
  110.     ** Walker routine. This routine should apply fun(obj->ptr, data)
  111.     ** for every gc pointer within the object.
  112.     */
  113.     PRInt32 (PR_CALLBACK *walk)(void GCPTR *obj, PRWalkFun fun, void* data);
  114. } GCType;
  115.  
  116. /*
  117. ** This data structure must be added as the hash table passed to 
  118. ** the summarize method of GCType.
  119. */ 
  120. typedef struct PRSummaryEntry {
  121.     void* clazz;
  122.     PRInt32 instancesCount;
  123.     PRInt32 totalSize;
  124. } PRSummaryEntry;
  125.  
  126. /*
  127. ** This function pointer must be registered by users of nspr
  128. ** to produce the finally summary after all object in the
  129. ** heap have been visited.
  130. */
  131. typedef void (PR_CALLBACK *PRSummaryPrinter)(FILE *out, void* closure);
  132.  
  133. PR_EXTERN(void) PR_CALLBACK PR_RegisterSummaryPrinter(PRSummaryPrinter fun, void* closure);
  134.  
  135. typedef void PR_CALLBACK GCRootFinder(void *arg);
  136. typedef void PR_CALLBACK GCBeginFinalizeHook(void *arg);
  137. typedef void PR_CALLBACK GCEndFinalizeHook(void *arg);
  138. typedef void PR_CALLBACK GCBeginGCHook(void *arg);
  139. typedef void PR_CALLBACK GCEndGCHook(void *arg);
  140.  
  141. typedef enum { PR_GCBEGIN, PR_GCEND } GCLockHookArg;
  142.  
  143. typedef void PR_CALLBACK GCLockHookFunc(GCLockHookArg arg1, void *arg2);
  144.  
  145. typedef struct GCLockHook GCLockHook;
  146.  
  147. struct GCLockHook {
  148.   GCLockHookFunc* func;
  149.   void* arg;
  150.   GCLockHook* next;
  151.   GCLockHook* prev;
  152. };
  153.  
  154.  
  155. /*
  156. ** Hooks which are called at the beginning and end of the GC process.
  157. ** The begin hooks are called before the root finding step. The hooks are
  158. ** called with threading disabled, so it is now allowed to re-enter the
  159. ** kernel. The end hooks are called after the gc has finished but before
  160. ** the finalizer has run.
  161. */
  162. PR_EXTERN(void) PR_CALLBACK PR_SetBeginGCHook(GCBeginGCHook *hook, void *arg);
  163. PR_EXTERN(void) PR_CALLBACK PR_GetBeginGCHook(GCBeginGCHook **hook, void **arg);
  164. PR_EXTERN(void) PR_CALLBACK PR_SetEndGCHook(GCBeginGCHook *hook, void *arg);
  165. PR_EXTERN(void) PR_CALLBACK PR_GetEndGCHook(GCEndGCHook **hook, void **arg);
  166.  
  167. /*
  168. ** Called before SuspendAll is called by dogc, so that GC thread can hold
  169. ** all the locks before hand to avoid any deadlocks
  170. */
  171.  
  172. /*
  173. PR_EXTERN(void) PR_SetGCLockHook(GCLockHook *hook, void *arg);
  174. PR_EXTERN(void) PR_GetGCLockHook(GCLockHook **hook, void **arg);
  175. */
  176.  
  177. PR_EXTERN(int) PR_RegisterGCLockHook(GCLockHookFunc *hook, void *arg);
  178.  
  179. /*
  180. ** Hooks which are called at the beginning and end of the GC finalization
  181. ** process. After the GC has identified all of the dead objects in the
  182. ** heap, it looks for objects that need finalization. Before it calls the
  183. ** first finalization proc (see the GCType structure above) it calls the
  184. ** begin hook. When it has finalized the last object it calls the end
  185. ** hook.
  186. */
  187. PR_EXTERN(void) PR_SetBeginFinalizeHook(GCBeginFinalizeHook *hook, void *arg);
  188. PR_EXTERN(void) PR_GetBeginFinalizeHook(GCBeginFinalizeHook **hook, void **arg);
  189. PR_EXTERN(void) PR_SetEndFinalizeHook(GCBeginFinalizeHook *hook, void *arg);
  190. PR_EXTERN(void) PR_GetEndFinalizeHook(GCEndFinalizeHook **hook, void **arg);
  191.  
  192. /*
  193. ** Register a GC type. Return's the index into the GC internal type
  194. ** table. The returned value is passed to PR_AllocMemory. After the call,
  195. ** the "type" memory belongs to the GC (the caller must not free it or
  196. ** change it).
  197. */
  198. PR_EXTERN(PRInt32) PR_RegisterType(GCType *type);
  199.  
  200. /*
  201. ** Register a root finder with the collector. The collector will call
  202. ** these functions to identify all of the roots before collection
  203. ** proceeds. "arg" is passed to the function when it is called.
  204. */
  205. PR_EXTERN(PRStatus) PR_RegisterRootFinder(GCRootFinder func, char *name, void *arg);
  206.  
  207. /*
  208. ** Allocate some GC'able memory. The object must be at least bytes in
  209. ** size. The type index function for the object is specified. "flags"
  210. ** specifies some control flags. If PR_ALLOC_CLEAN is set then the memory
  211. ** is zero'd before being returned. If PR_ALLOC_DOUBLE is set then the
  212. ** allocated memory is double aligned.
  213. **
  214. ** Any memory cell that you store a pointer to something allocated by
  215. ** this call must be findable by the GC. Use the PR_RegisterRootFinder to
  216. ** register new places where the GC will look for pointers into the heap.
  217. ** The GC already knows how to scan any NSPR threads or monitors.
  218. */
  219. PR_EXTERN(PRWord GCPTR *)PR_AllocMemory(
  220.     PRWord bytes, PRInt32 typeIndex, PRWord flags);
  221. PR_EXTERN(PRWord GCPTR *)PR_AllocSimpleMemory(
  222.     PRWord bytes, PRInt32 typeIndex);
  223.  
  224. /*
  225. ** This function can be used to cause PR_AllocMemory to always return
  226. ** NULL. This may be useful in low memory situations when we're trying to
  227. ** shutdown applets.
  228. */
  229. PR_EXTERN(void) PR_EnableAllocation(PRBool yesOrNo);
  230.  
  231. /* flags bits */
  232. #define PR_ALLOC_CLEAN 0x1
  233. #define PR_ALLOC_DOUBLE 0x2
  234. #define PR_ALLOC_ZERO_HANDLE 0x4              /* XXX yes, it's a hack */
  235.  
  236. /*
  237. ** Force a garbage collection right now. Return when it completes.
  238. */
  239. PR_EXTERN(void) PR_GC(void);
  240.  
  241. /*
  242. ** Force a finalization right now. Return when finalization has
  243. ** completed. Finalization completes when there are no more objects
  244. ** pending finalization. This does not mean there are no objects in the
  245. ** gc heap that will need finalization should a collection be done after
  246. ** this call.
  247. */
  248. PR_EXTERN(void) PR_ForceFinalize(void);
  249.  
  250. /*
  251. ** Dump the GC heap out to the given file. This will stop the system dead
  252. ** in its tracks while it is occuring.
  253. */
  254. PR_EXTERN(void) PR_DumpGCHeap(FILE *out, PRBool detailed);
  255.  
  256. /*
  257. ** Wrapper for PR_DumpGCHeap
  258. */
  259. PR_EXTERN(void) PR_DumpMemory(PRBool detailed);
  260.  
  261. /*
  262. ** Dump summary of objects allocated.
  263. */
  264. PR_EXTERN(void) PR_DumpMemorySummary(void);
  265.  
  266. /*
  267. ** Dump the application heaps.
  268. */
  269. PR_EXTERN(void) PR_DumpApplicationHeaps(void);
  270.  
  271. /*
  272. ** Helper function used by dump routines to do the indentation in a
  273. ** consistent fashion.
  274. */
  275. PR_EXTERN(void) PR_DumpIndent(FILE *out, PRIntn indent);
  276.  
  277. /*
  278. ** The GCInfo structure contains all of the GC state...
  279. **
  280. ** busyMemory:
  281. **    The amount of GC heap memory that is busy at this instant. Busy
  282. **    doesn't mean alive, it just means that it has been
  283. **    allocated. Immediately after a collection busy means how much is
  284. **    alive.
  285. **
  286. ** freeMemory:
  287. **    The amount of GC heap memory that is as yet unallocated.
  288. **
  289. ** allocMemory:
  290. **    The sum of free and busy memory in the GC heap.
  291. **
  292. ** maxMemory:
  293. **    The maximum size that the GC heap is allowed to grow.
  294. **
  295. ** lowSeg:
  296. **    The lowest segment currently used in the GC heap.
  297. **
  298. ** highSeg:
  299. **    The highest segment currently used in the GC heap.  
  300. **    The lowSeg and highSeg members are used for a "quick test" of whether 
  301. **    a pointer falls within the GC heap. [ see GC_IN_HEAP(...) ]
  302. **
  303. ** lock:
  304. **    Monitor used for syncronization within the GC.
  305. **
  306. ** finalizer:
  307. **    Thread in which the GC finalizer is running.
  308. **
  309. ** liveBlock:
  310. **    Object scanning functions call through this function pointer to
  311. **    register a potential block of pointers with the collector. (This is
  312. **    currently not at all different than processRoot.)
  313. **
  314. ** livePointer:
  315. **    Object scanning functions call through this function pointer to
  316. **    register a single pointer with the collector.
  317. **
  318. ** processRootBlock:
  319. **    When a root finder identifies a root it should call through this
  320. **    function pointer so that the GC can process the root. The call takes
  321. **    a base address and count which the gc will examine for valid heap
  322. **    pointers.
  323. **
  324. ** processRootPointer:
  325. **    When a root finder identifies a root it should call through this
  326. **    function pointer so that the GC can process the root. The call takes
  327. **    a single pointer value.
  328. */
  329. typedef struct GCInfoStr {
  330.     PRWord  flags;         /* trace flags (see below)               */
  331.     PRWord  busyMemory;    /* memory in use right now               */
  332.     PRWord  freeMemory;    /* memory free right now                 */
  333.     PRWord  allocMemory;   /* sum of busy & free memory             */
  334.     PRWord  maxMemory;     /* max memory we are allowed to allocate */
  335.     PRWord *lowSeg;        /* lowest segment in the GC heap         */
  336.     PRWord *highSeg;       /* higest segment in the GC heap         */
  337.  
  338.     PRMonitor *lock;
  339.     PRThread  *finalizer;
  340.  
  341.     void (PR_CALLBACK *liveBlock)(void **base, PRInt32 count);
  342.     void (PR_CALLBACK *livePointer)(void *ptr);
  343.     void (PR_CALLBACK *processRootBlock)(void **base, PRInt32 count);
  344.     void (PR_CALLBACK *processRootPointer)(void *ptr);
  345.     FILE* dumpOutput;
  346. #ifdef GCTIMINGHOOK
  347.     void (*gcTimingHook)(int32 gcTime);
  348. #endif
  349. } GCInfo;
  350.  
  351. PR_EXTERN(GCInfo *) PR_GetGCInfo(void);
  352. PR_EXTERN(PRBool) PR_GC_In_Heap(void GCPTR *object);
  353.  
  354. /*
  355. ** Simple bounds check to see if a pointer is anywhere near the GC heap.
  356. ** Used to avoid calls to PR_ProcessRoot and PR_LiveObject by object
  357. ** scanning code.
  358. */
  359. #if !defined(XP_PC) || defined(_WIN32)
  360. #define GC_IN_HEAP(_info, _p) (((PRWord*)(_p) >= (_info)->lowSeg) && \
  361.                                ((PRWord*)(_p) <  (_info)->highSeg))
  362. #else
  363. /*
  364. ** The simple bounds check, above, doesn't work in Win16, because we don't
  365. ** maintain: lowSeg == MIN(all segments) and highSeg == MAX(all segments).
  366. ** So we have to do a little better.
  367. */
  368. #define GC_IN_HEAP(_info, _p) PR_GC_In_Heap(_p)
  369. #endif
  370.  
  371. PR_EXTERN(PRWord) PR_GetObjectHeader(void *ptr);
  372.  
  373. PR_EXTERN(PRWord) PR_SetObjectHeader(void *ptr, PRWord newUserBits);
  374.  
  375. /************************************************************************/
  376.  
  377. /* Trace flags (passed to PR_InitGC or in environment GCLOG) */
  378. #define GC_TRACE    0x0001
  379. #define GC_ROOTS    0x0002
  380. #define GC_LIVE     0x0004
  381. #define GC_ALLOC    0x0008
  382. #define GC_MARK     0x0010
  383. #define GC_SWEEP    0x0020
  384. #define GC_DEBUG    0x0040
  385. #define GC_FINAL    0x0080
  386.  
  387. #if defined(DEBUG_kipp) || defined(DEBUG_warren)
  388. #define GC_CHECK    0x0100
  389. #endif
  390.  
  391. #ifdef DEBUG
  392. #define GCTRACE(x, y) if (PR_GetGCInfo()->flags & x) GCTrace y
  393. PR_EXTERN(void) GCTrace(char *fmt, ...);
  394. #else
  395. #define GCTRACE(x, y)
  396. #endif
  397.  
  398. PR_END_EXTERN_C
  399.  
  400. #endif /* prgc_h___ */
  401.