src/share/vm/memory/space.hpp

Mon, 01 Dec 2008 23:25:24 -0800

author
ysr
date
Mon, 01 Dec 2008 23:25:24 -0800
changeset 892
27a80744a83b
parent 873
122d10c82f3f
child 952
e9be0e04635a
permissions
-rw-r--r--

6778647: snap(), snap_policy() should be renamed setup(), setup_policy()
Summary: Renamed Reference{Policy,Pocessor} methods from snap{,_policy}() to setup{,_policy}()
Reviewed-by: apetrusenko

     1 /*
     2  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // A space is an abstraction for the "storage units" backing
    26 // up the generation abstraction. It includes specific
    27 // implementations for keeping track of free and used space,
    28 // for iterating over objects and free blocks, etc.
    30 // Here's the Space hierarchy:
    31 //
    32 // - Space               -- an asbtract base class describing a heap area
    33 //   - CompactibleSpace  -- a space supporting compaction
    34 //     - CompactibleFreeListSpace -- (used for CMS generation)
    35 //     - ContiguousSpace -- a compactible space in which all free space
    36 //                          is contiguous
    37 //       - EdenSpace     -- contiguous space used as nursery
    38 //         - ConcEdenSpace -- contiguous space with a 'soft end safe' allocation
    39 //       - OffsetTableContigSpace -- contiguous space with a block offset array
    40 //                          that allows "fast" block_start calls
    41 //         - TenuredSpace -- (used for TenuredGeneration)
    42 //         - ContigPermSpace -- an offset table contiguous space for perm gen
    44 // Forward decls.
    45 class Space;
    46 class BlockOffsetArray;
    47 class BlockOffsetArrayContigSpace;
    48 class Generation;
    49 class CompactibleSpace;
    50 class BlockOffsetTable;
    51 class GenRemSet;
    52 class CardTableRS;
    53 class DirtyCardToOopClosure;
    55 // An oop closure that is circumscribed by a filtering memory region.
    56 class SpaceMemRegionOopsIterClosure: public OopClosure {
    57  private:
    58   OopClosure* _cl;
    59   MemRegion   _mr;
    60  protected:
    61   template <class T> void do_oop_work(T* p) {
    62     if (_mr.contains(p)) {
    63       _cl->do_oop(p);
    64     }
    65   }
    66  public:
    67   SpaceMemRegionOopsIterClosure(OopClosure* cl, MemRegion mr):
    68     _cl(cl), _mr(mr) {}
    69   virtual void do_oop(oop* p);
    70   virtual void do_oop(narrowOop* p);
    71 };
    73 // A Space describes a heap area. Class Space is an abstract
    74 // base class.
    75 //
    76 // Space supports allocation, size computation and GC support is provided.
    77 //
    78 // Invariant: bottom() and end() are on page_size boundaries and
    79 // bottom() <= top() <= end()
    80 // top() is inclusive and end() is exclusive.
    82 class Space: public CHeapObj {
    83   friend class VMStructs;
    84  protected:
    85   HeapWord* _bottom;
    86   HeapWord* _end;
    88   // Used in support of save_marks()
    89   HeapWord* _saved_mark_word;
    91   MemRegionClosure* _preconsumptionDirtyCardClosure;
    93   // A sequential tasks done structure. This supports
    94   // parallel GC, where we have threads dynamically
    95   // claiming sub-tasks from a larger parallel task.
    96   SequentialSubTasksDone _par_seq_tasks;
    98   Space():
    99     _bottom(NULL), _end(NULL), _preconsumptionDirtyCardClosure(NULL) { }
   101  public:
   102   // Accessors
   103   HeapWord* bottom() const         { return _bottom; }
   104   HeapWord* end() const            { return _end;    }
   105   virtual void set_bottom(HeapWord* value) { _bottom = value; }
   106   virtual void set_end(HeapWord* value)    { _end = value; }
   108   virtual HeapWord* saved_mark_word() const  { return _saved_mark_word; }
   109   void set_saved_mark_word(HeapWord* p) { _saved_mark_word = p; }
   111   MemRegionClosure* preconsumptionDirtyCardClosure() const {
   112     return _preconsumptionDirtyCardClosure;
   113   }
   114   void setPreconsumptionDirtyCardClosure(MemRegionClosure* cl) {
   115     _preconsumptionDirtyCardClosure = cl;
   116   }
   118   // Returns a subregion of the space containing all the objects in
   119   // the space.
   120   virtual MemRegion used_region() const { return MemRegion(bottom(), end()); }
   122   // Returns a region that is guaranteed to contain (at least) all objects
   123   // allocated at the time of the last call to "save_marks".  If the space
   124   // initializes its DirtyCardToOopClosure's specifying the "contig" option
   125   // (that is, if the space is contiguous), then this region must contain only
   126   // such objects: the memregion will be from the bottom of the region to the
   127   // saved mark.  Otherwise, the "obj_allocated_since_save_marks" method of
   128   // the space must distiguish between objects in the region allocated before
   129   // and after the call to save marks.
   130   virtual MemRegion used_region_at_save_marks() const {
   131     return MemRegion(bottom(), saved_mark_word());
   132   }
   134   // Initialization.
   135   // "initialize" should be called once on a space, before it is used for
   136   // any purpose.  The "mr" arguments gives the bounds of the space, and
   137   // the "clear_space" argument should be true unless the memory in "mr" is
   138   // known to be zeroed.
   139   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
   141   // The "clear" method must be called on a region that may have
   142   // had allocation performed in it, but is now to be considered empty.
   143   virtual void clear(bool mangle_space);
   145   // For detecting GC bugs.  Should only be called at GC boundaries, since
   146   // some unused space may be used as scratch space during GC's.
   147   // Default implementation does nothing. We also call this when expanding
   148   // a space to satisfy an allocation request. See bug #4668531
   149   virtual void mangle_unused_area() {}
   150   virtual void mangle_unused_area_complete() {}
   151   virtual void mangle_region(MemRegion mr) {}
   153   // Testers
   154   bool is_empty() const              { return used() == 0; }
   155   bool not_empty() const             { return used() > 0; }
   157   // Returns true iff the given the space contains the
   158   // given address as part of an allocated object. For
   159   // ceratin kinds of spaces, this might be a potentially
   160   // expensive operation. To prevent performance problems
   161   // on account of its inadvertent use in product jvm's,
   162   // we restrict its use to assertion checks only.
   163   virtual bool is_in(const void* p) const;
   165   // Returns true iff the given reserved memory of the space contains the
   166   // given address.
   167   bool is_in_reserved(const void* p) const { return _bottom <= p && p < _end; }
   169   // Returns true iff the given block is not allocated.
   170   virtual bool is_free_block(const HeapWord* p) const = 0;
   172   // Test whether p is double-aligned
   173   static bool is_aligned(void* p) {
   174     return ((intptr_t)p & (sizeof(double)-1)) == 0;
   175   }
   177   // Size computations.  Sizes are in bytes.
   178   size_t capacity()     const { return byte_size(bottom(), end()); }
   179   virtual size_t used() const = 0;
   180   virtual size_t free() const = 0;
   182   // Iterate over all the ref-containing fields of all objects in the
   183   // space, calling "cl.do_oop" on each.  Fields in objects allocated by
   184   // applications of the closure are not included in the iteration.
   185   virtual void oop_iterate(OopClosure* cl);
   187   // Same as above, restricted to the intersection of a memory region and
   188   // the space.  Fields in objects allocated by applications of the closure
   189   // are not included in the iteration.
   190   virtual void oop_iterate(MemRegion mr, OopClosure* cl) = 0;
   192   // Iterate over all objects in the space, calling "cl.do_object" on
   193   // each.  Objects allocated by applications of the closure are not
   194   // included in the iteration.
   195   virtual void object_iterate(ObjectClosure* blk) = 0;
   197   // Iterate over all objects that intersect with mr, calling "cl->do_object"
   198   // on each.  There is an exception to this: if this closure has already
   199   // been invoked on an object, it may skip such objects in some cases.  This is
   200   // Most likely to happen in an "upwards" (ascending address) iteration of
   201   // MemRegions.
   202   virtual void object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl);
   204   // Iterate over as many initialized objects in the space as possible,
   205   // calling "cl.do_object_careful" on each. Return NULL if all objects
   206   // in the space (at the start of the iteration) were iterated over.
   207   // Return an address indicating the extent of the iteration in the
   208   // event that the iteration had to return because of finding an
   209   // uninitialized object in the space, or if the closure "cl"
   210   // signalled early termination.
   211   virtual HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
   212   virtual HeapWord* object_iterate_careful_m(MemRegion mr,
   213                                              ObjectClosureCareful* cl);
   215   // Create and return a new dirty card to oop closure. Can be
   216   // overriden to return the appropriate type of closure
   217   // depending on the type of space in which the closure will
   218   // operate. ResourceArea allocated.
   219   virtual DirtyCardToOopClosure* new_dcto_cl(OopClosure* cl,
   220                                              CardTableModRefBS::PrecisionStyle precision,
   221                                              HeapWord* boundary = NULL);
   223   // If "p" is in the space, returns the address of the start of the
   224   // "block" that contains "p".  We say "block" instead of "object" since
   225   // some heaps may not pack objects densely; a chunk may either be an
   226   // object or a non-object.  If "p" is not in the space, return NULL.
   227   virtual HeapWord* block_start_const(const void* p) const = 0;
   229   // The non-const version may have benevolent side effects on the data
   230   // structure supporting these calls, possibly speeding up future calls.
   231   // The default implementation, however, is simply to call the const
   232   // version.
   233   inline virtual HeapWord* block_start(const void* p);
   235   // Requires "addr" to be the start of a chunk, and returns its size.
   236   // "addr + size" is required to be the start of a new chunk, or the end
   237   // of the active area of the heap.
   238   virtual size_t block_size(const HeapWord* addr) const = 0;
   240   // Requires "addr" to be the start of a block, and returns "TRUE" iff
   241   // the block is an object.
   242   virtual bool block_is_obj(const HeapWord* addr) const = 0;
   244   // Requires "addr" to be the start of a block, and returns "TRUE" iff
   245   // the block is an object and the object is alive.
   246   virtual bool obj_is_alive(const HeapWord* addr) const;
   248   // Allocation (return NULL if full).  Assumes the caller has established
   249   // mutually exclusive access to the space.
   250   virtual HeapWord* allocate(size_t word_size) = 0;
   252   // Allocation (return NULL if full).  Enforces mutual exclusion internally.
   253   virtual HeapWord* par_allocate(size_t word_size) = 0;
   255   // Returns true if this object has been allocated since a
   256   // generation's "save_marks" call.
   257   virtual bool obj_allocated_since_save_marks(const oop obj) const = 0;
   259   // Mark-sweep-compact support: all spaces can update pointers to objects
   260   // moving as a part of compaction.
   261   virtual void adjust_pointers();
   263   // PrintHeapAtGC support
   264   virtual void print() const;
   265   virtual void print_on(outputStream* st) const;
   266   virtual void print_short() const;
   267   virtual void print_short_on(outputStream* st) const;
   270   // Accessor for parallel sequential tasks.
   271   SequentialSubTasksDone* par_seq_tasks() { return &_par_seq_tasks; }
   273   // IF "this" is a ContiguousSpace, return it, else return NULL.
   274   virtual ContiguousSpace* toContiguousSpace() {
   275     return NULL;
   276   }
   278   // Debugging
   279   virtual void verify(bool allow_dirty) const = 0;
   280 };
   282 // A MemRegionClosure (ResourceObj) whose "do_MemRegion" function applies an
   283 // OopClosure to (the addresses of) all the ref-containing fields that could
   284 // be modified by virtue of the given MemRegion being dirty. (Note that
   285 // because of the imprecise nature of the write barrier, this may iterate
   286 // over oops beyond the region.)
   287 // This base type for dirty card to oop closures handles memory regions
   288 // in non-contiguous spaces with no boundaries, and should be sub-classed
   289 // to support other space types. See ContiguousDCTOC for a sub-class
   290 // that works with ContiguousSpaces.
   292 class DirtyCardToOopClosure: public MemRegionClosureRO {
   293 protected:
   294   OopClosure* _cl;
   295   Space* _sp;
   296   CardTableModRefBS::PrecisionStyle _precision;
   297   HeapWord* _boundary;          // If non-NULL, process only non-NULL oops
   298                                 // pointing below boundary.
   299   HeapWord* _min_done;          // ObjHeadPreciseArray precision requires
   300                                 // a downwards traversal; this is the
   301                                 // lowest location already done (or,
   302                                 // alternatively, the lowest address that
   303                                 // shouldn't be done again.  NULL means infinity.)
   304   NOT_PRODUCT(HeapWord* _last_bottom;)
   305   NOT_PRODUCT(HeapWord* _last_explicit_min_done;)
   307   // Get the actual top of the area on which the closure will
   308   // operate, given where the top is assumed to be (the end of the
   309   // memory region passed to do_MemRegion) and where the object
   310   // at the top is assumed to start. For example, an object may
   311   // start at the top but actually extend past the assumed top,
   312   // in which case the top becomes the end of the object.
   313   virtual HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
   315   // Walk the given memory region from bottom to (actual) top
   316   // looking for objects and applying the oop closure (_cl) to
   317   // them. The base implementation of this treats the area as
   318   // blocks, where a block may or may not be an object. Sub-
   319   // classes should override this to provide more accurate
   320   // or possibly more efficient walking.
   321   virtual void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top);
   323 public:
   324   DirtyCardToOopClosure(Space* sp, OopClosure* cl,
   325                         CardTableModRefBS::PrecisionStyle precision,
   326                         HeapWord* boundary) :
   327     _sp(sp), _cl(cl), _precision(precision), _boundary(boundary),
   328     _min_done(NULL) {
   329     NOT_PRODUCT(_last_bottom = NULL);
   330     NOT_PRODUCT(_last_explicit_min_done = NULL);
   331   }
   333   void do_MemRegion(MemRegion mr);
   335   void set_min_done(HeapWord* min_done) {
   336     _min_done = min_done;
   337     NOT_PRODUCT(_last_explicit_min_done = _min_done);
   338   }
   339 #ifndef PRODUCT
   340   void set_last_bottom(HeapWord* last_bottom) {
   341     _last_bottom = last_bottom;
   342   }
   343 #endif
   344 };
   346 // A structure to represent a point at which objects are being copied
   347 // during compaction.
   348 class CompactPoint : public StackObj {
   349 public:
   350   Generation* gen;
   351   CompactibleSpace* space;
   352   HeapWord* threshold;
   353   CompactPoint(Generation* _gen, CompactibleSpace* _space,
   354                HeapWord* _threshold) :
   355     gen(_gen), space(_space), threshold(_threshold) {}
   356 };
   359 // A space that supports compaction operations.  This is usually, but not
   360 // necessarily, a space that is normally contiguous.  But, for example, a
   361 // free-list-based space whose normal collection is a mark-sweep without
   362 // compaction could still support compaction in full GC's.
   364 class CompactibleSpace: public Space {
   365   friend class VMStructs;
   366   friend class CompactibleFreeListSpace;
   367   friend class CompactingPermGenGen;
   368   friend class CMSPermGenGen;
   369 private:
   370   HeapWord* _compaction_top;
   371   CompactibleSpace* _next_compaction_space;
   373 public:
   374   CompactibleSpace() :
   375    _compaction_top(NULL), _next_compaction_space(NULL) {}
   377   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
   378   virtual void clear(bool mangle_space);
   380   // Used temporarily during a compaction phase to hold the value
   381   // top should have when compaction is complete.
   382   HeapWord* compaction_top() const { return _compaction_top;    }
   384   void set_compaction_top(HeapWord* value) {
   385     assert(value == NULL || (value >= bottom() && value <= end()),
   386       "should point inside space");
   387     _compaction_top = value;
   388   }
   390   // Perform operations on the space needed after a compaction
   391   // has been performed.
   392   virtual void reset_after_compaction() {}
   394   // Returns the next space (in the current generation) to be compacted in
   395   // the global compaction order.  Also is used to select the next
   396   // space into which to compact.
   398   virtual CompactibleSpace* next_compaction_space() const {
   399     return _next_compaction_space;
   400   }
   402   void set_next_compaction_space(CompactibleSpace* csp) {
   403     _next_compaction_space = csp;
   404   }
   406   // MarkSweep support phase2
   408   // Start the process of compaction of the current space: compute
   409   // post-compaction addresses, and insert forwarding pointers.  The fields
   410   // "cp->gen" and "cp->compaction_space" are the generation and space into
   411   // which we are currently compacting.  This call updates "cp" as necessary,
   412   // and leaves the "compaction_top" of the final value of
   413   // "cp->compaction_space" up-to-date.  Offset tables may be updated in
   414   // this phase as if the final copy had occurred; if so, "cp->threshold"
   415   // indicates when the next such action should be taken.
   416   virtual void prepare_for_compaction(CompactPoint* cp);
   417   // MarkSweep support phase3
   418   virtual void adjust_pointers();
   419   // MarkSweep support phase4
   420   virtual void compact();
   422   // The maximum percentage of objects that can be dead in the compacted
   423   // live part of a compacted space ("deadwood" support.)
   424   virtual size_t allowed_dead_ratio() const { return 0; };
   426   // Some contiguous spaces may maintain some data structures that should
   427   // be updated whenever an allocation crosses a boundary.  This function
   428   // returns the first such boundary.
   429   // (The default implementation returns the end of the space, so the
   430   // boundary is never crossed.)
   431   virtual HeapWord* initialize_threshold() { return end(); }
   433   // "q" is an object of the given "size" that should be forwarded;
   434   // "cp" names the generation ("gen") and containing "this" (which must
   435   // also equal "cp->space").  "compact_top" is where in "this" the
   436   // next object should be forwarded to.  If there is room in "this" for
   437   // the object, insert an appropriate forwarding pointer in "q".
   438   // If not, go to the next compaction space (there must
   439   // be one, since compaction must succeed -- we go to the first space of
   440   // the previous generation if necessary, updating "cp"), reset compact_top
   441   // and then forward.  In either case, returns the new value of "compact_top".
   442   // If the forwarding crosses "cp->threshold", invokes the "cross_threhold"
   443   // function of the then-current compaction space, and updates "cp->threshold
   444   // accordingly".
   445   virtual HeapWord* forward(oop q, size_t size, CompactPoint* cp,
   446                     HeapWord* compact_top);
   448   // Return a size with adjusments as required of the space.
   449   virtual size_t adjust_object_size_v(size_t size) const { return size; }
   451 protected:
   452   // Used during compaction.
   453   HeapWord* _first_dead;
   454   HeapWord* _end_of_live;
   456   // Minimum size of a free block.
   457   virtual size_t minimum_free_block_size() const = 0;
   459   // This the function is invoked when an allocation of an object covering
   460   // "start" to "end occurs crosses the threshold; returns the next
   461   // threshold.  (The default implementation does nothing.)
   462   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* the_end) {
   463     return end();
   464   }
   466   // Requires "allowed_deadspace_words > 0", that "q" is the start of a
   467   // free block of the given "word_len", and that "q", were it an object,
   468   // would not move if forwared.  If the size allows, fill the free
   469   // block with an object, to prevent excessive compaction.  Returns "true"
   470   // iff the free region was made deadspace, and modifies
   471   // "allowed_deadspace_words" to reflect the number of available deadspace
   472   // words remaining after this operation.
   473   bool insert_deadspace(size_t& allowed_deadspace_words, HeapWord* q,
   474                         size_t word_len);
   475 };
   477 #define SCAN_AND_FORWARD(cp,scan_limit,block_is_obj,block_size) {            \
   478   /* Compute the new addresses for the live objects and store it in the mark \
   479    * Used by universe::mark_sweep_phase2()                                   \
   480    */                                                                        \
   481   HeapWord* compact_top; /* This is where we are currently compacting to. */ \
   482                                                                              \
   483   /* We're sure to be here before any objects are compacted into this        \
   484    * space, so this is a good time to initialize this:                       \
   485    */                                                                        \
   486   set_compaction_top(bottom());                                              \
   487                                                                              \
   488   if (cp->space == NULL) {                                                   \
   489     assert(cp->gen != NULL, "need a generation");                            \
   490     assert(cp->threshold == NULL, "just checking");                          \
   491     assert(cp->gen->first_compaction_space() == this, "just checking");      \
   492     cp->space = cp->gen->first_compaction_space();                           \
   493     compact_top = cp->space->bottom();                                       \
   494     cp->space->set_compaction_top(compact_top);                              \
   495     cp->threshold = cp->space->initialize_threshold();                       \
   496   } else {                                                                   \
   497     compact_top = cp->space->compaction_top();                               \
   498   }                                                                          \
   499                                                                              \
   500   /* We allow some amount of garbage towards the bottom of the space, so     \
   501    * we don't start compacting before there is a significant gain to be made.\
   502    * Occasionally, we want to ensure a full compaction, which is determined  \
   503    * by the MarkSweepAlwaysCompactCount parameter.                           \
   504    */                                                                        \
   505   int invocations = SharedHeap::heap()->perm_gen()->stat_record()->invocations;\
   506   bool skip_dead = ((invocations % MarkSweepAlwaysCompactCount) != 0);       \
   507                                                                              \
   508   size_t allowed_deadspace = 0;                                              \
   509   if (skip_dead) {                                                           \
   510     const size_t ratio = allowed_dead_ratio();                               \
   511     allowed_deadspace = (capacity() * ratio / 100) / HeapWordSize;           \
   512   }                                                                          \
   513                                                                              \
   514   HeapWord* q = bottom();                                                    \
   515   HeapWord* t = scan_limit();                                                \
   516                                                                              \
   517   HeapWord*  end_of_live= q;    /* One byte beyond the last byte of the last \
   518                                    live object. */                           \
   519   HeapWord*  first_dead = end();/* The first dead object. */                 \
   520   LiveRange* liveRange  = NULL; /* The current live range, recorded in the   \
   521                                    first header of preceding free area. */   \
   522   _first_dead = first_dead;                                                  \
   523                                                                              \
   524   const intx interval = PrefetchScanIntervalInBytes;                         \
   525                                                                              \
   526   while (q < t) {                                                            \
   527     assert(!block_is_obj(q) ||                                               \
   528            oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() ||   \
   529            oop(q)->mark()->has_bias_pattern(),                               \
   530            "these are the only valid states during a mark sweep");           \
   531     if (block_is_obj(q) && oop(q)->is_gc_marked()) {                         \
   532       /* prefetch beyond q */                                                \
   533       Prefetch::write(q, interval);                                          \
   534       /* size_t size = oop(q)->size();  changing this for cms for perm gen */\
   535       size_t size = block_size(q);                                           \
   536       compact_top = cp->space->forward(oop(q), size, cp, compact_top);       \
   537       q += size;                                                             \
   538       end_of_live = q;                                                       \
   539     } else {                                                                 \
   540       /* run over all the contiguous dead objects */                         \
   541       HeapWord* end = q;                                                     \
   542       do {                                                                   \
   543         /* prefetch beyond end */                                            \
   544         Prefetch::write(end, interval);                                      \
   545         end += block_size(end);                                              \
   546       } while (end < t && (!block_is_obj(end) || !oop(end)->is_gc_marked()));\
   547                                                                              \
   548       /* see if we might want to pretend this object is alive so that        \
   549        * we don't have to compact quite as often.                            \
   550        */                                                                    \
   551       if (allowed_deadspace > 0 && q == compact_top) {                       \
   552         size_t sz = pointer_delta(end, q);                                   \
   553         if (insert_deadspace(allowed_deadspace, q, sz)) {                    \
   554           compact_top = cp->space->forward(oop(q), sz, cp, compact_top);     \
   555           q = end;                                                           \
   556           end_of_live = end;                                                 \
   557           continue;                                                          \
   558         }                                                                    \
   559       }                                                                      \
   560                                                                              \
   561       /* otherwise, it really is a free region. */                           \
   562                                                                              \
   563       /* for the previous LiveRange, record the end of the live objects. */  \
   564       if (liveRange) {                                                       \
   565         liveRange->set_end(q);                                               \
   566       }                                                                      \
   567                                                                              \
   568       /* record the current LiveRange object.                                \
   569        * liveRange->start() is overlaid on the mark word.                    \
   570        */                                                                    \
   571       liveRange = (LiveRange*)q;                                             \
   572       liveRange->set_start(end);                                             \
   573       liveRange->set_end(end);                                               \
   574                                                                              \
   575       /* see if this is the first dead region. */                            \
   576       if (q < first_dead) {                                                  \
   577         first_dead = q;                                                      \
   578       }                                                                      \
   579                                                                              \
   580       /* move on to the next object */                                       \
   581       q = end;                                                               \
   582     }                                                                        \
   583   }                                                                          \
   584                                                                              \
   585   assert(q == t, "just checking");                                           \
   586   if (liveRange != NULL) {                                                   \
   587     liveRange->set_end(q);                                                   \
   588   }                                                                          \
   589   _end_of_live = end_of_live;                                                \
   590   if (end_of_live < first_dead) {                                            \
   591     first_dead = end_of_live;                                                \
   592   }                                                                          \
   593   _first_dead = first_dead;                                                  \
   594                                                                              \
   595   /* save the compaction_top of the compaction space. */                     \
   596   cp->space->set_compaction_top(compact_top);                                \
   597 }
   599 #define SCAN_AND_ADJUST_POINTERS(adjust_obj_size) {                             \
   600   /* adjust all the interior pointers to point at the new locations of objects  \
   601    * Used by MarkSweep::mark_sweep_phase3() */                                  \
   602                                                                                 \
   603   HeapWord* q = bottom();                                                       \
   604   HeapWord* t = _end_of_live;  /* Established by "prepare_for_compaction". */   \
   605                                                                                 \
   606   assert(_first_dead <= _end_of_live, "Stands to reason, no?");                 \
   607                                                                                 \
   608   if (q < t && _first_dead > q &&                                               \
   609       !oop(q)->is_gc_marked()) {                                                \
   610     /* we have a chunk of the space which hasn't moved and we've                \
   611      * reinitialized the mark word during the previous pass, so we can't        \
   612      * use is_gc_marked for the traversal. */                                   \
   613     HeapWord* end = _first_dead;                                                \
   614                                                                                 \
   615     while (q < end) {                                                           \
   616       /* I originally tried to conjoin "block_start(q) == q" to the             \
   617        * assertion below, but that doesn't work, because you can't              \
   618        * accurately traverse previous objects to get to the current one         \
   619        * after their pointers (including pointers into permGen) have been       \
   620        * updated, until the actual compaction is done.  dld, 4/00 */            \
   621       assert(block_is_obj(q),                                                   \
   622              "should be at block boundaries, and should be looking at objs");   \
   623                                                                                 \
   624       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q)));     \
   625                                                                                 \
   626       /* point all the oops to the new location */                              \
   627       size_t size = oop(q)->adjust_pointers();                                  \
   628       size = adjust_obj_size(size);                                             \
   629                                                                                 \
   630       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers());           \
   631                                                                                 \
   632       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size));     \
   633                                                                                 \
   634       q += size;                                                                \
   635     }                                                                           \
   636                                                                                 \
   637     if (_first_dead == t) {                                                     \
   638       q = t;                                                                    \
   639     } else {                                                                    \
   640       /* $$$ This is funky.  Using this to read the previously written          \
   641        * LiveRange.  See also use below. */                                     \
   642       q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer();                \
   643     }                                                                           \
   644   }                                                                             \
   645                                                                                 \
   646   const intx interval = PrefetchScanIntervalInBytes;                            \
   647                                                                                 \
   648   debug_only(HeapWord* prev_q = NULL);                                          \
   649   while (q < t) {                                                               \
   650     /* prefetch beyond q */                                                     \
   651     Prefetch::write(q, interval);                                               \
   652     if (oop(q)->is_gc_marked()) {                                               \
   653       /* q is alive */                                                          \
   654       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q)));     \
   655       /* point all the oops to the new location */                              \
   656       size_t size = oop(q)->adjust_pointers();                                  \
   657       size = adjust_obj_size(size);                                             \
   658       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers());           \
   659       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size));     \
   660       debug_only(prev_q = q);                                                   \
   661       q += size;                                                                \
   662     } else {                                                                    \
   663       /* q is not a live object, so its mark should point at the next           \
   664        * live object */                                                         \
   665       debug_only(prev_q = q);                                                   \
   666       q = (HeapWord*) oop(q)->mark()->decode_pointer();                         \
   667       assert(q > prev_q, "we should be moving forward through memory");         \
   668     }                                                                           \
   669   }                                                                             \
   670                                                                                 \
   671   assert(q == t, "just checking");                                              \
   672 }
   674 #define SCAN_AND_COMPACT(obj_size) {                                            \
   675   /* Copy all live objects to their new location                                \
   676    * Used by MarkSweep::mark_sweep_phase4() */                                  \
   677                                                                                 \
   678   HeapWord*       q = bottom();                                                 \
   679   HeapWord* const t = _end_of_live;                                             \
   680   debug_only(HeapWord* prev_q = NULL);                                          \
   681                                                                                 \
   682   if (q < t && _first_dead > q &&                                               \
   683       !oop(q)->is_gc_marked()) {                                                \
   684     debug_only(                                                                 \
   685     /* we have a chunk of the space which hasn't moved and we've reinitialized  \
   686      * the mark word during the previous pass, so we can't use is_gc_marked for \
   687      * the traversal. */                                                        \
   688     HeapWord* const end = _first_dead;                                          \
   689                                                                                 \
   690     while (q < end) {                                                           \
   691       size_t size = obj_size(q);                                                \
   692       assert(!oop(q)->is_gc_marked(),                                           \
   693              "should be unmarked (special dense prefix handling)");             \
   694       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, q));       \
   695       debug_only(prev_q = q);                                                   \
   696       q += size;                                                                \
   697     }                                                                           \
   698     )  /* debug_only */                                                         \
   699                                                                                 \
   700     if (_first_dead == t) {                                                     \
   701       q = t;                                                                    \
   702     } else {                                                                    \
   703       /* $$$ Funky */                                                           \
   704       q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer();               \
   705     }                                                                           \
   706   }                                                                             \
   707                                                                                 \
   708   const intx scan_interval = PrefetchScanIntervalInBytes;                       \
   709   const intx copy_interval = PrefetchCopyIntervalInBytes;                       \
   710   while (q < t) {                                                               \
   711     if (!oop(q)->is_gc_marked()) {                                              \
   712       /* mark is pointer to next marked oop */                                  \
   713       debug_only(prev_q = q);                                                   \
   714       q = (HeapWord*) oop(q)->mark()->decode_pointer();                         \
   715       assert(q > prev_q, "we should be moving forward through memory");         \
   716     } else {                                                                    \
   717       /* prefetch beyond q */                                                   \
   718       Prefetch::read(q, scan_interval);                                         \
   719                                                                                 \
   720       /* size and destination */                                                \
   721       size_t size = obj_size(q);                                                \
   722       HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee();                \
   723                                                                                 \
   724       /* prefetch beyond compaction_top */                                      \
   725       Prefetch::write(compaction_top, copy_interval);                           \
   726                                                                                 \
   727       /* copy object and reinit its mark */                                     \
   728       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size,            \
   729                                                             compaction_top));   \
   730       assert(q != compaction_top, "everything in this pass should be moving");  \
   731       Copy::aligned_conjoint_words(q, compaction_top, size);                    \
   732       oop(compaction_top)->init_mark();                                         \
   733       assert(oop(compaction_top)->klass() != NULL, "should have a class");      \
   734                                                                                 \
   735       debug_only(prev_q = q);                                                   \
   736       q += size;                                                                \
   737     }                                                                           \
   738   }                                                                             \
   739                                                                                 \
   740   /* Let's remember if we were empty before we did the compaction. */           \
   741   bool was_empty = used_region().is_empty();                                    \
   742   /* Reset space after compaction is complete */                                \
   743   reset_after_compaction();                                                     \
   744   /* We do this clear, below, since it has overloaded meanings for some */      \
   745   /* space subtypes.  For example, OffsetTableContigSpace's that were   */      \
   746   /* compacted into will have had their offset table thresholds updated */      \
   747   /* continuously, but those that weren't need to have their thresholds */      \
   748   /* re-initialized.  Also mangles unused area for debugging.           */      \
   749   if (used_region().is_empty()) {                                               \
   750     if (!was_empty) clear(SpaceDecorator::Mangle);                              \
   751   } else {                                                                      \
   752     if (ZapUnusedHeapArea) mangle_unused_area();                                \
   753   }                                                                             \
   754 }
   756 class GenSpaceMangler;
   758 // A space in which the free area is contiguous.  It therefore supports
   759 // faster allocation, and compaction.
   760 class ContiguousSpace: public CompactibleSpace {
   761   friend class OneContigSpaceCardGeneration;
   762   friend class VMStructs;
   763  protected:
   764   HeapWord* _top;
   765   HeapWord* _concurrent_iteration_safe_limit;
   766   // A helper for mangling the unused area of the space in debug builds.
   767   GenSpaceMangler* _mangler;
   769   GenSpaceMangler* mangler() { return _mangler; }
   771   // Allocation helpers (return NULL if full).
   772   inline HeapWord* allocate_impl(size_t word_size, HeapWord* end_value);
   773   inline HeapWord* par_allocate_impl(size_t word_size, HeapWord* end_value);
   775  public:
   776   ContiguousSpace();
   777   ~ContiguousSpace();
   779   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
   780   virtual void clear(bool mangle_space);
   782   // Accessors
   783   HeapWord* top() const            { return _top;    }
   784   void set_top(HeapWord* value)    { _top = value; }
   786   virtual void set_saved_mark()    { _saved_mark_word = top();    }
   787   void reset_saved_mark()          { _saved_mark_word = bottom(); }
   789   WaterMark bottom_mark()     { return WaterMark(this, bottom()); }
   790   WaterMark top_mark()        { return WaterMark(this, top()); }
   791   WaterMark saved_mark()      { return WaterMark(this, saved_mark_word()); }
   792   bool saved_mark_at_top() const { return saved_mark_word() == top(); }
   794   // In debug mode mangle (write it with a particular bit
   795   // pattern) the unused part of a space.
   797   // Used to save the an address in a space for later use during mangling.
   798   void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN;
   799   // Used to save the space's current top for later use during mangling.
   800   void set_top_for_allocations() PRODUCT_RETURN;
   802   // Mangle regions in the space from the current top up to the
   803   // previously mangled part of the space.
   804   void mangle_unused_area() PRODUCT_RETURN;
   805   // Mangle [top, end)
   806   void mangle_unused_area_complete() PRODUCT_RETURN;
   807   // Mangle the given MemRegion.
   808   void mangle_region(MemRegion mr) PRODUCT_RETURN;
   810   // Do some sparse checking on the area that should have been mangled.
   811   void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN;
   812   // Check the complete area that should have been mangled.
   813   // This code may be NULL depending on the macro DEBUG_MANGLING.
   814   void check_mangled_unused_area_complete() PRODUCT_RETURN;
   816   // Size computations: sizes in bytes.
   817   size_t capacity() const        { return byte_size(bottom(), end()); }
   818   size_t used() const            { return byte_size(bottom(), top()); }
   819   size_t free() const            { return byte_size(top(),    end()); }
   821   // Override from space.
   822   bool is_in(const void* p) const;
   824   virtual bool is_free_block(const HeapWord* p) const;
   826   // In a contiguous space we have a more obvious bound on what parts
   827   // contain objects.
   828   MemRegion used_region() const { return MemRegion(bottom(), top()); }
   830   MemRegion used_region_at_save_marks() const {
   831     return MemRegion(bottom(), saved_mark_word());
   832   }
   834   // Allocation (return NULL if full)
   835   virtual HeapWord* allocate(size_t word_size);
   836   virtual HeapWord* par_allocate(size_t word_size);
   838   virtual bool obj_allocated_since_save_marks(const oop obj) const {
   839     return (HeapWord*)obj >= saved_mark_word();
   840   }
   842   // Iteration
   843   void oop_iterate(OopClosure* cl);
   844   void oop_iterate(MemRegion mr, OopClosure* cl);
   845   void object_iterate(ObjectClosure* blk);
   846   void object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl);
   847   // iterates on objects up to the safe limit
   848   HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
   849   inline HeapWord* concurrent_iteration_safe_limit();
   850   // changes the safe limit, all objects from bottom() to the new
   851   // limit should be properly initialized
   852   inline void set_concurrent_iteration_safe_limit(HeapWord* new_limit);
   854 #ifndef SERIALGC
   855   // In support of parallel oop_iterate.
   856   #define ContigSpace_PAR_OOP_ITERATE_DECL(OopClosureType, nv_suffix)  \
   857     void par_oop_iterate(MemRegion mr, OopClosureType* blk);
   859     ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DECL)
   860   #undef ContigSpace_PAR_OOP_ITERATE_DECL
   861 #endif // SERIALGC
   863   // Compaction support
   864   virtual void reset_after_compaction() {
   865     assert(compaction_top() >= bottom() && compaction_top() <= end(), "should point inside space");
   866     set_top(compaction_top());
   867     // set new iteration safe limit
   868     set_concurrent_iteration_safe_limit(compaction_top());
   869   }
   870   virtual size_t minimum_free_block_size() const { return 0; }
   872   // Override.
   873   DirtyCardToOopClosure* new_dcto_cl(OopClosure* cl,
   874                                      CardTableModRefBS::PrecisionStyle precision,
   875                                      HeapWord* boundary = NULL);
   877   // Apply "blk->do_oop" to the addresses of all reference fields in objects
   878   // starting with the _saved_mark_word, which was noted during a generation's
   879   // save_marks and is required to denote the head of an object.
   880   // Fields in objects allocated by applications of the closure
   881   // *are* included in the iteration.
   882   // Updates _saved_mark_word to point to just after the last object
   883   // iterated over.
   884 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix)  \
   885   void oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk);
   887   ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DECL)
   888 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DECL
   890   // Same as object_iterate, but starting from "mark", which is required
   891   // to denote the start of an object.  Objects allocated by
   892   // applications of the closure *are* included in the iteration.
   893   virtual void object_iterate_from(WaterMark mark, ObjectClosure* blk);
   895   // Very inefficient implementation.
   896   virtual HeapWord* block_start_const(const void* p) const;
   897   size_t block_size(const HeapWord* p) const;
   898   // If a block is in the allocated area, it is an object.
   899   bool block_is_obj(const HeapWord* p) const { return p < top(); }
   901   // Addresses for inlined allocation
   902   HeapWord** top_addr() { return &_top; }
   903   HeapWord** end_addr() { return &_end; }
   905   // Overrides for more efficient compaction support.
   906   void prepare_for_compaction(CompactPoint* cp);
   908   // PrintHeapAtGC support.
   909   virtual void print_on(outputStream* st) const;
   911   // Checked dynamic downcasts.
   912   virtual ContiguousSpace* toContiguousSpace() {
   913     return this;
   914   }
   916   // Debugging
   917   virtual void verify(bool allow_dirty) const;
   919   // Used to increase collection frequency.  "factor" of 0 means entire
   920   // space.
   921   void allocate_temporary_filler(int factor);
   923 };
   926 // A dirty card to oop closure that does filtering.
   927 // It knows how to filter out objects that are outside of the _boundary.
   928 class Filtering_DCTOC : public DirtyCardToOopClosure {
   929 protected:
   930   // Override.
   931   void walk_mem_region(MemRegion mr,
   932                        HeapWord* bottom, HeapWord* top);
   934   // Walk the given memory region, from bottom to top, applying
   935   // the given oop closure to (possibly) all objects found. The
   936   // given oop closure may or may not be the same as the oop
   937   // closure with which this closure was created, as it may
   938   // be a filtering closure which makes use of the _boundary.
   939   // We offer two signatures, so the FilteringClosure static type is
   940   // apparent.
   941   virtual void walk_mem_region_with_cl(MemRegion mr,
   942                                        HeapWord* bottom, HeapWord* top,
   943                                        OopClosure* cl) = 0;
   944   virtual void walk_mem_region_with_cl(MemRegion mr,
   945                                        HeapWord* bottom, HeapWord* top,
   946                                        FilteringClosure* cl) = 0;
   948 public:
   949   Filtering_DCTOC(Space* sp, OopClosure* cl,
   950                   CardTableModRefBS::PrecisionStyle precision,
   951                   HeapWord* boundary) :
   952     DirtyCardToOopClosure(sp, cl, precision, boundary) {}
   953 };
   955 // A dirty card to oop closure for contiguous spaces
   956 // (ContiguousSpace and sub-classes).
   957 // It is a FilteringClosure, as defined above, and it knows:
   958 //
   959 // 1. That the actual top of any area in a memory region
   960 //    contained by the space is bounded by the end of the contiguous
   961 //    region of the space.
   962 // 2. That the space is really made up of objects and not just
   963 //    blocks.
   965 class ContiguousSpaceDCTOC : public Filtering_DCTOC {
   966 protected:
   967   // Overrides.
   968   HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
   970   virtual void walk_mem_region_with_cl(MemRegion mr,
   971                                        HeapWord* bottom, HeapWord* top,
   972                                        OopClosure* cl);
   973   virtual void walk_mem_region_with_cl(MemRegion mr,
   974                                        HeapWord* bottom, HeapWord* top,
   975                                        FilteringClosure* cl);
   977 public:
   978   ContiguousSpaceDCTOC(ContiguousSpace* sp, OopClosure* cl,
   979                        CardTableModRefBS::PrecisionStyle precision,
   980                        HeapWord* boundary) :
   981     Filtering_DCTOC(sp, cl, precision, boundary)
   982   {}
   983 };
   986 // Class EdenSpace describes eden-space in new generation.
   988 class DefNewGeneration;
   990 class EdenSpace : public ContiguousSpace {
   991   friend class VMStructs;
   992  private:
   993   DefNewGeneration* _gen;
   995   // _soft_end is used as a soft limit on allocation.  As soft limits are
   996   // reached, the slow-path allocation code can invoke other actions and then
   997   // adjust _soft_end up to a new soft limit or to end().
   998   HeapWord* _soft_end;
  1000  public:
  1001   EdenSpace(DefNewGeneration* gen) :
  1002    _gen(gen), _soft_end(NULL) {}
  1004   // Get/set just the 'soft' limit.
  1005   HeapWord* soft_end()               { return _soft_end; }
  1006   HeapWord** soft_end_addr()         { return &_soft_end; }
  1007   void set_soft_end(HeapWord* value) { _soft_end = value; }
  1009   // Override.
  1010   void clear(bool mangle_space);
  1012   // Set both the 'hard' and 'soft' limits (_end and _soft_end).
  1013   void set_end(HeapWord* value) {
  1014     set_soft_end(value);
  1015     ContiguousSpace::set_end(value);
  1018   // Allocation (return NULL if full)
  1019   HeapWord* allocate(size_t word_size);
  1020   HeapWord* par_allocate(size_t word_size);
  1021 };
  1023 // Class ConcEdenSpace extends EdenSpace for the sake of safe
  1024 // allocation while soft-end is being modified concurrently
  1026 class ConcEdenSpace : public EdenSpace {
  1027  public:
  1028   ConcEdenSpace(DefNewGeneration* gen) : EdenSpace(gen) { }
  1030   // Allocation (return NULL if full)
  1031   HeapWord* par_allocate(size_t word_size);
  1032 };
  1035 // A ContigSpace that Supports an efficient "block_start" operation via
  1036 // a BlockOffsetArray (whose BlockOffsetSharedArray may be shared with
  1037 // other spaces.)  This is the abstract base class for old generation
  1038 // (tenured, perm) spaces.
  1040 class OffsetTableContigSpace: public ContiguousSpace {
  1041   friend class VMStructs;
  1042  protected:
  1043   BlockOffsetArrayContigSpace _offsets;
  1044   Mutex _par_alloc_lock;
  1046  public:
  1047   // Constructor
  1048   OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
  1049                          MemRegion mr);
  1051   void set_bottom(HeapWord* value);
  1052   void set_end(HeapWord* value);
  1054   void clear(bool mangle_space);
  1056   inline HeapWord* block_start_const(const void* p) const;
  1058   // Add offset table update.
  1059   virtual inline HeapWord* allocate(size_t word_size);
  1060   inline HeapWord* par_allocate(size_t word_size);
  1062   // MarkSweep support phase3
  1063   virtual HeapWord* initialize_threshold();
  1064   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
  1066   virtual void print_on(outputStream* st) const;
  1068   // Debugging
  1069   void verify(bool allow_dirty) const;
  1071   // Shared space support
  1072   void serialize_block_offset_array_offsets(SerializeOopClosure* soc);
  1073 };
  1076 // Class TenuredSpace is used by TenuredGeneration
  1078 class TenuredSpace: public OffsetTableContigSpace {
  1079   friend class VMStructs;
  1080  protected:
  1081   // Mark sweep support
  1082   size_t allowed_dead_ratio() const;
  1083  public:
  1084   // Constructor
  1085   TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray,
  1086                MemRegion mr) :
  1087     OffsetTableContigSpace(sharedOffsetArray, mr) {}
  1088 };
  1091 // Class ContigPermSpace is used by CompactingPermGen
  1093 class ContigPermSpace: public OffsetTableContigSpace {
  1094   friend class VMStructs;
  1095  protected:
  1096   // Mark sweep support
  1097   size_t allowed_dead_ratio() const;
  1098  public:
  1099   // Constructor
  1100   ContigPermSpace(BlockOffsetSharedArray* sharedOffsetArray, MemRegion mr) :
  1101     OffsetTableContigSpace(sharedOffsetArray, mr) {}
  1102 };

mercurial