home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / j2sdk / files / linux / j2sdklin.bin / jdk1.3.1 / include-old / gc.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-06  |  15.7 KB  |  469 lines

  1. /*
  2.  * @(#)gc.h    1.31 00/02/02
  3.  *
  4.  * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the proprietary information of Sun Microsystems, Inc.  
  7.  * Use is subject to license terms.
  8.  * 
  9.  */
  10.  
  11. #ifndef _JAVASOFT_GC_H_
  12. #define _JAVASOFT_GC_H_
  13.  
  14. #include "gc_md.h"
  15. #include "oobj.h"
  16. #include "util.h"
  17. #include "interpreter.h"
  18. #include "monitor.h"
  19.  
  20. /*
  21.  * Utility function to lock all systems locks
  22.  */
  23. void lock_for_debugger(struct execenv *ee);
  24. void unlock_for_debugger(struct execenv *ee);
  25.  
  26. /*
  27.  * Calls FreeClass and clears out the ClassClass content inside the HEAP_LOCK.
  28.  */
  29. void GCFreeClass(ClassClass *cb);
  30.  
  31. struct Hjava_lang_String;
  32. struct Hjava_lang_String * internString(struct Hjava_lang_String *str);
  33.  
  34. bool_t isObject(void *p);
  35. bool_t isHandle(void *p);
  36.  
  37. void pin_object(void *obj);
  38. void unpin_object(void *obj);
  39. int pinned_object(void *obj);
  40.  
  41. /*
  42.  * Lock against heap modification.
  43.  */
  44. extern sys_mon_t *_heap_lock;
  45. #define HEAP_LOCK_INIT()    monitorRegister(_heap_lock, "Heap lock")
  46. #define HEAP_LOCK(self)        sysMonitorEnter(self, _heap_lock)
  47. #define HEAP_UNLOCK(self)   sysMonitorExit(self, _heap_lock)
  48. #define HEAP_LOCKED(self)   sysMonitorEntered(self, _heap_lock)
  49.  
  50. /*
  51.  * Heap parameters.
  52.  */
  53. extern float minHeapFreePercent;
  54. extern float maxHeapFreePercent;
  55. extern long minHeapExpansion;
  56. extern long maxHeapExpansion;
  57.  
  58. /*
  59.  * Time of most recent garbage collection, in millisecond ticks
  60.  */
  61. extern jlong timeOfLastGC;
  62.  
  63. /*
  64.  * Define this if you want the mark phase to detect pointers into the
  65.  * interior of objects.
  66.  */
  67. /* #define CHECK_INTERIOR_POINTERS */
  68.  
  69. #define OBJECTGRAIN     2 * sizeof (void *)
  70. #define HANDLEGRAIN     2 * sizeof (void *)
  71. #define BITSPERCHAR     8
  72.  
  73. /*
  74.  * Types of overflows: we might respond to an overflow of a particular
  75.  * error differently, e.g. expanding only the overflowing area.
  76.  */
  77. #define OVERFLOW_NONE     0
  78. #define OVERFLOW_OBJECTS 1
  79. #define OVERFLOW_HANDLES 2
  80.  
  81. /*
  82.  * Possible actions to take on overflows.  manageAllocFailure()
  83.  * decides between these.
  84.  */
  85. #define OVERFLOW_ACT_FAIL    0
  86. #define OVERFLOW_ACT_GC        1
  87. #define OVERFLOW_ACT_EXPAND    2
  88. #define OVERFLOW_ACT_REFS    3
  89. #define OVERFLOW_ACT_DESPERATE    4
  90.  
  91. /*
  92.  * The size of a contiguous region to move in the compacter.  If you set
  93.  * to 0, then the compacter will never move more than one object at time.
  94.  * There's a tradeoff involved in picking a value for this.  The code to
  95.  * move multiple objects requires multiple passes over the object, one to
  96.  * find the extent of the region to move, the actual move and finally a
  97.  * pass to swap the handle back.  If you slide a really large region of
  98.  * memory you'll probably blow the cache, so a relatively small value is
  99.  * good.
  100.  */
  101. #ifndef GC_SLIDE_LENGTH
  102. #define GC_SLIDE_LENGTH 256
  103. #endif
  104.  
  105. /* 
  106.  * These are the flag bits used by the garbage collector.  They may be
  107.  * overridden by definitions placed in the gc_md.h file.
  108.  */
  109. #ifndef OBJ_SWAPPED
  110. #define OBJ_SWAPPED  ((unsigned)1<<2)
  111. #endif
  112.  
  113. #ifndef OBJ_PINNED
  114. #define OBJ_PINNED   ((unsigned)1<<1)
  115. #endif
  116.  
  117. #ifndef OBJ_FREE
  118. #define OBJ_FREE     ((unsigned)1<<0)
  119. #endif
  120.  
  121. #ifndef FAST_MONITOR
  122.  
  123. /*
  124.  * Memory block header (bottom three bits are flags), and swapped isn't
  125.  * strictly necessary for the CONTIGUOUS_HEAPS implemented.  It's in there
  126.  * right now in DEBUG mode and for compatibility with PAGED_HEAPS.
  127.  *
  128.  * ----------------------------------------------
  129.  * | <--- length ---> | swapped | pinned | free |
  130.  * ----------------------------------------------
  131.  *  31              3          2        1      0
  132.  */
  133. typedef int32_t hdr;
  134.  
  135. #define obj_geth(p) (*((hdr *)(p)))
  136. #define obj_seth(p, h) (*((hdr *)(p)) = (h))
  137.  
  138. #define obj_swap(hp) \
  139. if (1) { \
  140.     OBJECT T = *hp->obj; \
  141.     *hp->obj = (OBJECT) hp; \
  142.     hp->obj = (OBJECT *) T; \
  143. } else ((void) 0)
  144.  
  145. #else  /* FAST_MONITOR */
  146.  
  147. /*
  148.  * Memory block header (bottom three bits are flags), and swapped isn't
  149.  * strictly necessary for the CONTIGUOUS_HEAPS implemented.  It's in there
  150.  * right now in DEBUG mode and for compatibility with PAGED_HEAPS.
  151.  *
  152.  * ----------------------------------------------
  153.  * | <--- length ---> | swapped | pinned | free |
  154.  * ----------------------------------------------
  155.  *  31              3          2        1      0
  156.  * -------------------------------------------------------------------
  157.  * | heavy   | (struct execenv *ee) or (sys_mon_t *mid) |    zero    |
  158.  * -------------------------------------------------------------------
  159.  *         31 30                                       3   2   1   0
  160.  */
  161. typedef int64_t hdr;
  162. typedef int32_t ohdr;                       /* Old object header */
  163.  
  164. #define obj_geth(p) (*((int32_t *)(p)))
  165. #define obj_seth(p, h) (*((int32_t *)(p)) = (h))
  166.  
  167. #define obj_swap(hp) \
  168. if (1) { \
  169.     OBJECT T = *(hp->obj - 1); \
  170.     *(hp->obj - 1) = (OBJECT) hp; \
  171.     hp->obj = (OBJECT *) T; \
  172. } else ((void) 0)
  173.  
  174. #endif /* FAST_MONITOR */
  175.  
  176. /*
  177.  * These macros operate on the header word of an object.
  178.  */
  179. #define h_len(h) ((h) & ~(OBJ_SWAPPED | OBJ_FREE | OBJ_PINNED))
  180. #define h_setlf(h, l, f) (h = (l)|(f))
  181. #define h_bumplen(h, l) ((h) += (l))
  182. #define h_free(h) ((h) & OBJ_FREE)
  183. #define h_setfree(h) (h |= OBJ_FREE)
  184. #define h_clearfree(h) (h &= ~OBJ_FREE)
  185. #define h_hbf(h) (h & OBJ_HBF)
  186. #define h_sethbf(h) (h |= OBJ_HBF)
  187. #define h_pinned(h) (h & OBJ_PINNED)
  188. #define h_pin(h) (h |= OBJ_PINNED)
  189. #define h_unpin(h) (h &= ~OBJ_PINNED)
  190. #define h_swapped(h) (h & OBJ_SWAPPED)
  191. #define h_setswapped(h) (h |= OBJ_SWAPPED)
  192. #define h_clearswapped(h) (h &= ~OBJ_SWAPPED)
  193.  
  194. /*
  195.  * These macros operate on a pointer to the header word of an object.
  196.  */
  197. #define obj_len(p) (h_len(obj_geth(p)))
  198. #define obj_setlf(p, l, f) (h_setlf(obj_geth(p), (l), (f)))
  199. #define obj_bumplen(p, l) (h_bumplen(obj_geth(p), (l)))
  200. #define obj_free(p) (h_free(obj_geth(p)))
  201. #define obj_setfree(p) (h_setfree(obj_geth(p)))
  202. #define obj_clearfree(p) (h_clearfree(obj_geth(p)))
  203. #define obj_hbf(p) (h_hbf(obj_geth(p)))
  204. #define obj_sethbf(p) (h_sethbf(obj_geth(p)))
  205. #define obj_pinned(p) (h_pinned(obj_geth(p)))
  206. #define obj_pin(p) (h_pin((obj_geth(p))))
  207. #define obj_unpin(p) (h_unpin(obj_geth(p)))
  208. #define obj_swapped(p) (h_swapped(obj_geth(p)))
  209. #define obj_setswapped(p) (h_setswapped(obj_geth(p)))
  210. #define obj_clearswapped(p) (h_clearswapped((obj_geth(p))))
  211.  
  212.  
  213. #ifndef PAGED_HEAPS /************ CONTIGUOUS HEAPS: ********************/
  214.  
  215. #define ValidObject(p) (IS_ALIGNED(((uintptr_t)(p)), OBJECTGRAIN) &&    \
  216.              (unsigned char *)(p) >= opmin &&              \
  217.              (unsigned char *)(p) <  opmax)
  218. #define ValidHandle(p) (IS_ALIGNED(((uintptr_t)(p)), sizeof(JHandle)) && \
  219.              (unsigned char *)(p) >= hpmin &&             \
  220.              (unsigned char *)(p) <= hpmax)
  221. /* ValidHorO() assumes OBJECTGRAIN=sizeof(JHandle)... */
  222. #define ValidHorO(p)   (IS_ALIGNED(((uintptr_t)(p)), OBJECTGRAIN) &&    \
  223.              (unsigned char *)(p) >= hpmin &&        \
  224.              (unsigned char *)(p) <= opmax)
  225. #define SetLimits()                            \
  226.     register unsigned char *const opmin = opool,                \
  227.                            *const opmax = opoollimit,                \
  228.                    *const hpmin = hpool,                      \
  229.                    *const hpmax = hpoollimit-sizeof(JHandle)
  230.  
  231. #define POP_FREE_HANDLE(hp)                         \
  232.     hp = (JHandle *)hpoolfreelist;                     \
  233.     if (hp) {                                \
  234.         hpoolfreelist = (unsigned char *)hp->methods;            \
  235.     }
  236.  
  237. #define PUSH_FREE_HANDLE(hp) \
  238.     hp->methods = (struct methodtable *)hpoolfreelist; \
  239.     hpoolfreelist = (unsigned char *)hp;
  240.  
  241. /* Mark bit access assumes contiguity of handles and objects */
  242. #define MARKINDEX(p)    (((unsigned char *)(p) - hpmin) >> 8)
  243. #define BITOFFSET(p)    ((((unsigned char *)(p) - hpmin) >> 3) & 0x1f)
  244.  
  245. #define MarkPtr(p)    (markbits[MARKINDEX(p)] |= 1 << BITOFFSET(p))
  246. #define ClearMarkPtr(p) (markbits[MARKINDEX(p)] &= ~(1 << BITOFFSET(p)))
  247. #define IsMarked(p)    ((markbits[MARKINDEX(p)] >> BITOFFSET(p)) & 1)
  248.  
  249. /* set the second word in an object (from ptr to header) to 0x55555555 */
  250. #define CHECK_WORD_INDEX 1
  251.  
  252. #define MAP_OVER_HANDLES_FROM_START(MO_hp) {        \
  253.     JHandle *MOH_limit = (JHandle *) hpmax;        \
  254.     for (MO_hp = (JHandle *) hpool; MO_hp <= MOH_limit; MO_hp++) {
  255.  
  256. #define END_MAP_OVER_HANDLES_FROM_START            \
  257.     } /* end for */                    \
  258. } /* end MAP_OVER_HANDLES_FROM_START */
  259.  
  260. #define MAP_OVER_OBJECTS_FROM_START(p) {        \
  261.     unsigned char *MOO_limit = opmax;            \
  262.     unsigned char *MOO_start = opmin;            \
  263.     for (p = opmin;                    \
  264.      p < MOO_limit;                    \
  265.      p += obj_len(p)) {
  266.  
  267. #define END_MAP_OVER_OBJECTS_FROM_START            \
  268.     } /* end for */                    \
  269. } /* end END_MAP_OVER_OBJECTS_FROM_START */
  270.  
  271.  
  272. #else /************ PAGED HEAPS: ********************/
  273.  
  274. /*
  275.  * To override this macro, define a platform specific version
  276.  * in gc_md.h on your platform.
  277.  */
  278. #ifndef PTR_2_PAGENUM
  279. #define PTR_2_PAGENUM(ptr)    (ptr >> PTR_2_PAGE_SHIFT)
  280. #endif     /* PTR_2_PAGENUM */
  281.  
  282. /* gc philosophy makes it necessary to detect if an arbitrary int is
  283.  * (possibly) a handle or object ref.
  284.  * A value is (possibly) valid if it is properly aligned, and it 
  285.  * points into a page that has a page map entry of the proper type.
  286.  */
  287. /* assumes ValidHorO already */
  288. #define GetPageMapEntry(p)                          \
  289.          (page_map[PTR_2_PAGENUM((uintptr_t)(p) - (uintptr_t)mem_base)])
  290.  
  291. #define ValidObject(p) (IS_ALIGNED(((uintptr_t)(p)), OBJECTGRAIN) && \
  292.          (void *)(p) >= mem_base &&                        \
  293.          (void *)(p) <  mem_top &&                        \
  294.          (GetPageMapEntry((p)).chunk_size > 0))
  295. #define ValidHandle(p) (IS_ALIGNED(((uintptr_t)(p)), HANDLEGRAIN) && \
  296.          ((void *)(p) >= mem_base) &&                    \
  297.          ((void *)(p) <  mem_top) &&                    \
  298.          (GetPageMapEntry((p)).chunk_size < 0))
  299. /* ValidHorO() assumes OBJECTGRAIN == HANDLEGRAIN... */
  300. #define ValidHorO(p) (IS_ALIGNED(((uintptr_t)(p)), HANDLEGRAIN) && \
  301.          (void *)(p) >= mem_base &&                        \
  302.          (void *)(p) <  mem_top &&                        \
  303.          (GetPageMapEntry((p)).chunk_size != 0))
  304.  
  305. #define SetLimits() int SL_dufus = 0
  306.  
  307. /* assumes ValidHorO already */
  308. #define ChunkBase(p)    (void *)                        \
  309.          (((int)(p) & ~(PAGE_ALIGNMENT - 1)) -            \
  310.           (GetPageMapEntry((p)).page_number << PTR_2_PAGE_SHIFT))
  311.          
  312. /* curHanBlkP must be set in advance!!! */
  313. #define POP_FREE_HANDLE(hp)                         \
  314.     hp = (JHandle *)curHanBlkP->freePtr;                     \
  315.     if (hp) {                                    \
  316.         curHanBlkP->freePtr = (unsigned char *)hp->methods;            \
  317.     }
  318.  
  319. /* Can only be called within a MAP_OVER_HANDLES_FROM_START loop 
  320.  * - uses MOH_chunk instead of curHanBlkP for efficiency.
  321.  */
  322. #define PUSH_FREE_HANDLE(hp) \
  323.     hp->methods = (struct methodtable *)MOH_chunk->freePtr; \
  324.     MOH_chunk->freePtr = (unsigned char *)hp;
  325.  
  326. #ifndef MARKINDEX
  327. #define MARKINDEX(p)    (((int)(p) & (PAGE_ALIGNMENT - 1)) >> HANDLEGRAIN)
  328. #endif
  329. #ifndef BITOFFSET
  330. #define BITOFFSET(p)    ((((int)(p) & (PAGE_ALIGNMENT - 1)) >> ((sizeof(int))-1)) & 0x1f)
  331. #endif
  332.  
  333. #define MarkPtr(p)    (GetPageMapEntry(p).mark_bits[MARKINDEX(p)] |=       \
  334.                          1 << BITOFFSET(p))
  335. #define ClearMarkPtr(p) (GetPageMapEntry(p).mark_bits[MARKINDEX(p)] &= \
  336.               ~(1 << BITOFFSET(p)))
  337. #define IsMarked(p)    ((GetPageMapEntry(p).mark_bits[MARKINDEX(p)]       \
  338.                           >> BITOFFSET(p)) & 1)
  339.  
  340. /* # of bytes of markbits we need per page: */
  341. #define MARK_BITS_SIZE        (PAGE_ALIGNMENT / (OBJECTGRAIN * BITSPERCHAR))
  342.  
  343. /*
  344.  * Part of Java memory management and garbage collection.
  345.  *
  346.  * This supports a discontiguous gcable heap, which is useful for the
  347.  * Mac OS, or other platforms without good memory mapping support.
  348.  *
  349.  * CHUNKS:
  350.  * Memory is requested from the OS in "Chunks" of n pages, which are
  351.  * PAGE_ALIGNMENT aligned and sized. Handles and objects are allocated out of
  352.  * different chunks.  When more memory is needed, additional chunks can be
  353.  * allocated.  When chunks are free, they may be returned to  the OS.  Chunks
  354.  * don't need to be contiguous.  Handle chunks and object chunks are linked 
  355.  * into seperate, doubly linked lists, which are sorted by chunk address.  On
  356.  * platforms without real "memalign" support, there may be unaligned (wasted)
  357.  * space that precedes the true chunk that we can use for something else 
  358.  * (markbits come to mind).
  359.  */
  360.  
  361. /* fields marked ### MUST BE OBJECT AND/OR HANDLE GRAIN ALIGNED */
  362. typedef struct CHUNK_BLK {    /* a chunk of pages */
  363.     void* chunkHandle;        /* OS handle to this chunk */
  364.     struct CHUNK_BLK *nextPtr;    /* ptr to next chunk header */
  365.     struct CHUNK_BLK *prevPtr;    /* ptr to previous chunk header */
  366.     long chunkFlags;        /* misc flags */
  367.     long allocSize;        /* == (endPtr - startPtr)### */
  368.     long freeCnt;        /* # of free bytes in this chunk */
  369.     unsigned char *startPtr;    /* ptr to starting byte### */
  370.     unsigned char *endPtr;    /* ptr past last byte### */
  371.     unsigned char *freePtr;    /* ptr to first free space CANDIDATE 
  372.     * (may not really be free), or it might be a ptr to a free list
  373.     * of objects, depending on phase of the moon.
  374.     */
  375.     /* users may modify start and end ptrs, but not this one: */
  376.     unsigned char *physEndPtr;                
  377. #ifdef WASTED_SPACE_IN_LEADER
  378.     /* WARNING:  clearLocalMarkBits assumes that only markbits are stored
  379.           * in the waste !!!
  380.           */
  381.     unsigned char *wasteStartPtr; /* ptr to starting wasted byte */
  382.     unsigned char *wasteFreePtr;  /* ptr to first free wasted byte */
  383.                       /* wasteEndPtr == the ChunkBlk pointer */
  384. #endif /* WASTED_SPACE_IN_LEADER*/
  385. } ChunkBlk, *ChunkBlkP;
  386.  
  387. /* CHUNK_BLK->chunkFlags bits: */
  388.      /* set this bit in chunkFlags if any objects in the chunk are pinned */
  389. #define CHUNK_PINNED 1        
  390.  
  391. /* doubly-linked list of handle chunks, in address order: */
  392. extern ChunkBlkP firstHanBlkP;      
  393. extern ChunkBlkP lastHanBlkP;
  394. extern ChunkBlkP curHanBlkP;
  395. /* doubly-linked list of object chunks, in address order: */
  396. extern ChunkBlkP firstObjBlkP;
  397. extern ChunkBlkP lastObjBlkP;
  398. extern ChunkBlkP curObjBlkP;
  399.  
  400. /* store this into the last two words of the chunk to detect overwrites */
  401. /* Odd to make a poor pointer, 111 in the low bits looks like a swapped 
  402.  * free block if a header! */
  403. #define ALMOST_WORD    0x77777777
  404. #define ULTIMATE_WORD  0xBAADDEED  /* Why not. */
  405. /* set the third word in an object (from ptr to header) to 0x55555555 */
  406. #define CHECK_WORD_INDEX 2       
  407.  
  408. /* Macros to abstract out looping over all handles or objects.
  409.  * Note that you can't "break" out of this loop. Use goto or return instead.
  410.  */
  411. #define MAP_OVER_HANDLES_FROM_START(MO_hp) {        \
  412.     ChunkBlkP MOH_chunk = firstHanBlkP;            \
  413.     JHandle *MOH_limit;                    \
  414.     do {                        \
  415.     for (MO_hp = (JHandle *)MOH_chunk->startPtr,    \
  416.          MOH_limit = (JHandle *)MOH_chunk->endPtr;    \
  417.          MO_hp < MOH_limit; MO_hp++) {
  418.  
  419. #define END_MAP_OVER_HANDLES_FROM_START            \
  420.     }  /* end for */                \
  421.     MOH_chunk = MOH_chunk->nextPtr;            \
  422.     } while (MOH_chunk != firstHanBlkP);        \
  423. } /* end MAP_OVER_HANDLES_FROM_START */
  424.  
  425. #define MAP_OVER_OBJECTS_FROM_START(MO_p)   {        \
  426.     ChunkBlkP MOO_chunk = firstObjBlkP;            \
  427.     unsigned char *MOO_limit;                \
  428.     unsigned char *MOO_start;                \
  429.     do {                        \
  430.     for ((MO_p) = MOO_chunk->startPtr,         \
  431.          MOO_start = MOO_chunk->startPtr,        \
  432.          MOO_limit = MOO_chunk->endPtr;        \
  433.          (MO_p) < MOO_limit;            \
  434.          (MO_p) += obj_len(MO_p)) {
  435.  
  436. #define END_MAP_OVER_OBJECTS_FROM_START            \
  437.     }  /* end for */                \
  438.     MOO_chunk = MOO_chunk->nextPtr;            \
  439.     } while (MOO_chunk != firstObjBlkP);        \
  440. } /* end END_MAP_OVER_OBJECTS_FROM_START */
  441.  
  442. #endif /************ END PAGED HEAPS ********************/    
  443.  
  444.  
  445. #ifdef DEBUG
  446. #define FILL_CHECK_WORD(p) \
  447. if (obj_len(p) >= ((CHECK_WORD_INDEX + 1) * sizeof(unsigned char *))) { \
  448.     ((unsigned char **)(p))[CHECK_WORD_INDEX] = (unsigned char *) 0x55555555; \
  449. } else ((void) 0)
  450. #else
  451. #define FILL_CHECK_WORD(p) ((void) 0)
  452. #endif
  453.  
  454.  
  455. #ifdef WASTED_SPACE_IN_LEADER
  456. /* following functions defined in gc_md.c: */
  457. void initWastedSpaceInChunk(ChunkBlkP chunk);
  458. void sysCheckWastedSpace(ChunkBlkP chunk);
  459. void clearLocalMarkBits(void);
  460. void* allocMarkBitsLocally(ChunkBlkP blkP);
  461. #else
  462. #define initWastedSpaceInChunk(xxx) ((void)0)
  463. #define sysCheckWastedSpace(xxx) ((void)0)
  464. #define clearLocalMarkBits() ((void)0)
  465. #define allocMarkBitsLocally(xxx) ((void *)0)
  466. #endif
  467.  
  468. #endif /* !_JAVASOFT_GC_H_ */
  469.