1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_MEMORY_SPACE_HPP
  26 #define SHARE_VM_MEMORY_SPACE_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/blockOffsetTable.hpp"
  30 #include "memory/cardTableModRefBS.hpp"
  31 #include "memory/iterator.hpp"
  32 #include "memory/memRegion.hpp"
  33 #include "memory/watermark.hpp"
  34 #include "oops/markOop.hpp"
  35 #include "runtime/mutexLocker.hpp"
  36 #include "utilities/macros.hpp"
  37 #include "utilities/workgroup.hpp"
  38 
  39 // A space is an abstraction for the "storage units" backing
  40 // up the generation abstraction. It includes specific
  41 // implementations for keeping track of free and used space,
  42 // for iterating over objects and free blocks, etc.
  43 
  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;
  54 
  55 // A Space describes a heap area. Class Space is an abstract
  56 // base class.
  57 //
  58 // Space supports allocation, size computation and GC support is provided.
  59 //
  60 // Invariant: bottom() and end() are on page_size boundaries and
  61 // bottom() <= top() <= end()
  62 // top() is inclusive and end() is exclusive.
  63 
  64 class Space: public CHeapObj<mtGC> {
  65   friend class VMStructs;
  66  protected:
  67   HeapWord* _bottom;
  68   HeapWord* _end;
  69 
  70   // Used in support of save_marks()
  71   HeapWord* _saved_mark_word;
  72 
  73   MemRegionClosure* _preconsumptionDirtyCardClosure;
  74 
  75   // A sequential tasks done structure. This supports
  76   // parallel GC, where we have threads dynamically
  77   // claiming sub-tasks from a larger parallel task.
  78   SequentialSubTasksDone _par_seq_tasks;
  79 
  80   Space():
  81     _bottom(NULL), _end(NULL), _preconsumptionDirtyCardClosure(NULL) { }
  82 
  83  public:
  84   // Accessors
  85   HeapWord* bottom() const         { return _bottom; }
  86   HeapWord* end() const            { return _end;    }
  87   virtual void set_bottom(HeapWord* value) { _bottom = value; }
  88   virtual void set_end(HeapWord* value)    { _end = value; }
  89 
  90   virtual HeapWord* saved_mark_word() const  { return _saved_mark_word; }
  91 
  92   void set_saved_mark_word(HeapWord* p) { _saved_mark_word = p; }
  93 
  94   // Returns true if this object has been allocated since a
  95   // generation's "save_marks" call.
  96   virtual bool obj_allocated_since_save_marks(const oop obj) const {
  97     return (HeapWord*)obj >= saved_mark_word();
  98   }
  99 
 100   MemRegionClosure* preconsumptionDirtyCardClosure() const {
 101     return _preconsumptionDirtyCardClosure;
 102   }
 103   void setPreconsumptionDirtyCardClosure(MemRegionClosure* cl) {
 104     _preconsumptionDirtyCardClosure = cl;
 105   }
 106 
 107   // Returns a subregion of the space containing only the allocated objects in
 108   // the space.
 109   virtual MemRegion used_region() const = 0;
 110 
 111   // Returns a region that is guaranteed to contain (at least) all objects
 112   // allocated at the time of the last call to "save_marks".  If the space
 113   // initializes its DirtyCardToOopClosure's specifying the "contig" option
 114   // (that is, if the space is contiguous), then this region must contain only
 115   // such objects: the memregion will be from the bottom of the region to the
 116   // saved mark.  Otherwise, the "obj_allocated_since_save_marks" method of
 117   // the space must distinguish between objects in the region allocated before
 118   // and after the call to save marks.
 119   MemRegion used_region_at_save_marks() const {
 120     return MemRegion(bottom(), saved_mark_word());
 121   }
 122 
 123   // Initialization.
 124   // "initialize" should be called once on a space, before it is used for
 125   // any purpose.  The "mr" arguments gives the bounds of the space, and
 126   // the "clear_space" argument should be true unless the memory in "mr" is
 127   // known to be zeroed.
 128   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 129 
 130   // The "clear" method must be called on a region that may have
 131   // had allocation performed in it, but is now to be considered empty.
 132   virtual void clear(bool mangle_space);
 133 
 134   // For detecting GC bugs.  Should only be called at GC boundaries, since
 135   // some unused space may be used as scratch space during GC's.
 136   // Default implementation does nothing. We also call this when expanding
 137   // a space to satisfy an allocation request. See bug #4668531
 138   virtual void mangle_unused_area() {}
 139   virtual void mangle_unused_area_complete() {}
 140   virtual void mangle_region(MemRegion mr) {}
 141 
 142   // Testers
 143   bool is_empty() const              { return used() == 0; }
 144   bool not_empty() const             { return used() > 0; }
 145 
 146   // Returns true iff the given the space contains the
 147   // given address as part of an allocated object. For
 148   // certain kinds of spaces, this might be a potentially
 149   // expensive operation. To prevent performance problems
 150   // on account of its inadvertent use in product jvm's,
 151   // we restrict its use to assertion checks only.
 152   bool is_in(const void* p) const {
 153     return used_region().contains(p);
 154   }
 155 
 156   // Returns true iff the given reserved memory of the space contains the
 157   // given address.
 158   bool is_in_reserved(const void* p) const { return _bottom <= p && p < _end; }
 159 
 160   // Returns true iff the given block is not allocated.
 161   virtual bool is_free_block(const HeapWord* p) const = 0;
 162 
 163   // Test whether p is double-aligned
 164   static bool is_aligned(void* p) {
 165     return ((intptr_t)p & (sizeof(double)-1)) == 0;
 166   }
 167 
 168   // Size computations.  Sizes are in bytes.
 169   size_t capacity()     const { return byte_size(bottom(), end()); }
 170   virtual size_t used() const = 0;
 171   virtual size_t free() const = 0;
 172 
 173   // Iterate over all the ref-containing fields of all objects in the
 174   // space, calling "cl.do_oop" on each.  Fields in objects allocated by
 175   // applications of the closure are not included in the iteration.
 176   virtual void oop_iterate(ExtendedOopClosure* cl);
 177 
 178   // Iterate over all objects in the space, calling "cl.do_object" on
 179   // each.  Objects allocated by applications of the closure are not
 180   // included in the iteration.
 181   virtual void object_iterate(ObjectClosure* blk) = 0;
 182   // Similar to object_iterate() except only iterates over
 183   // objects whose internal references point to objects in the space.
 184   virtual void safe_object_iterate(ObjectClosure* blk) = 0;
 185 
 186   // Create and return a new dirty card to oop closure. Can be
 187   // overridden to return the appropriate type of closure
 188   // depending on the type of space in which the closure will
 189   // operate. ResourceArea allocated.
 190   virtual DirtyCardToOopClosure* new_dcto_cl(ExtendedOopClosure* cl,
 191                                              CardTableModRefBS::PrecisionStyle precision,
 192                                              HeapWord* boundary = NULL);
 193 
 194   // If "p" is in the space, returns the address of the start of the
 195   // "block" that contains "p".  We say "block" instead of "object" since
 196   // some heaps may not pack objects densely; a chunk may either be an
 197   // object or a non-object.  If "p" is not in the space, return NULL.
 198   virtual HeapWord* block_start_const(const void* p) const = 0;
 199 
 200   // The non-const version may have benevolent side effects on the data
 201   // structure supporting these calls, possibly speeding up future calls.
 202   // The default implementation, however, is simply to call the const
 203   // version.
 204   inline virtual HeapWord* block_start(const void* p);
 205 
 206   // Requires "addr" to be the start of a chunk, and returns its size.
 207   // "addr + size" is required to be the start of a new chunk, or the end
 208   // of the active area of the heap.
 209   virtual size_t block_size(const HeapWord* addr) const = 0;
 210 
 211   // Requires "addr" to be the start of a block, and returns "TRUE" iff
 212   // the block is an object.
 213   virtual bool block_is_obj(const HeapWord* addr) const = 0;
 214 
 215   // Requires "addr" to be the start of a block, and returns "TRUE" iff
 216   // the block is an object and the object is alive.
 217   virtual bool obj_is_alive(const HeapWord* addr) const;
 218 
 219   // Allocation (return NULL if full).  Assumes the caller has established
 220   // mutually exclusive access to the space.
 221   virtual HeapWord* allocate(size_t word_size) = 0;
 222 
 223   // Allocation (return NULL if full).  Enforces mutual exclusion internally.
 224   virtual HeapWord* par_allocate(size_t word_size) = 0;
 225 
 226   // Mark-sweep-compact support: all spaces can update pointers to objects
 227   // moving as a part of compaction.
 228   virtual void adjust_pointers();
 229 
 230   // PrintHeapAtGC support
 231   virtual void print() const;
 232   virtual void print_on(outputStream* st) const;
 233   virtual void print_short() const;
 234   virtual void print_short_on(outputStream* st) const;
 235 
 236 
 237   // Accessor for parallel sequential tasks.
 238   SequentialSubTasksDone* par_seq_tasks() { return &_par_seq_tasks; }
 239 
 240   // IF "this" is a ContiguousSpace, return it, else return NULL.
 241   virtual ContiguousSpace* toContiguousSpace() {
 242     return NULL;
 243   }
 244 
 245   // Debugging
 246   virtual void verify() const = 0;
 247 };
 248 
 249 // A MemRegionClosure (ResourceObj) whose "do_MemRegion" function applies an
 250 // OopClosure to (the addresses of) all the ref-containing fields that could
 251 // be modified by virtue of the given MemRegion being dirty. (Note that
 252 // because of the imprecise nature of the write barrier, this may iterate
 253 // over oops beyond the region.)
 254 // This base type for dirty card to oop closures handles memory regions
 255 // in non-contiguous spaces with no boundaries, and should be sub-classed
 256 // to support other space types. See ContiguousDCTOC for a sub-class
 257 // that works with ContiguousSpaces.
 258 
 259 class DirtyCardToOopClosure: public MemRegionClosureRO {
 260 protected:
 261   ExtendedOopClosure* _cl;
 262   Space* _sp;
 263   CardTableModRefBS::PrecisionStyle _precision;
 264   HeapWord* _boundary;          // If non-NULL, process only non-NULL oops
 265                                 // pointing below boundary.
 266   HeapWord* _min_done;          // ObjHeadPreciseArray precision requires
 267                                 // a downwards traversal; this is the
 268                                 // lowest location already done (or,
 269                                 // alternatively, the lowest address that
 270                                 // shouldn't be done again.  NULL means infinity.)
 271   NOT_PRODUCT(HeapWord* _last_bottom;)
 272   NOT_PRODUCT(HeapWord* _last_explicit_min_done;)
 273 
 274   // Get the actual top of the area on which the closure will
 275   // operate, given where the top is assumed to be (the end of the
 276   // memory region passed to do_MemRegion) and where the object
 277   // at the top is assumed to start. For example, an object may
 278   // start at the top but actually extend past the assumed top,
 279   // in which case the top becomes the end of the object.
 280   virtual HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
 281 
 282   // Walk the given memory region from bottom to (actual) top
 283   // looking for objects and applying the oop closure (_cl) to
 284   // them. The base implementation of this treats the area as
 285   // blocks, where a block may or may not be an object. Sub-
 286   // classes should override this to provide more accurate
 287   // or possibly more efficient walking.
 288   virtual void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top);
 289 
 290 public:
 291   DirtyCardToOopClosure(Space* sp, ExtendedOopClosure* cl,
 292                         CardTableModRefBS::PrecisionStyle precision,
 293                         HeapWord* boundary) :
 294     _sp(sp), _cl(cl), _precision(precision), _boundary(boundary),
 295     _min_done(NULL) {
 296     NOT_PRODUCT(_last_bottom = NULL);
 297     NOT_PRODUCT(_last_explicit_min_done = NULL);
 298   }
 299 
 300   void do_MemRegion(MemRegion mr);
 301 
 302   void set_min_done(HeapWord* min_done) {
 303     _min_done = min_done;
 304     NOT_PRODUCT(_last_explicit_min_done = _min_done);
 305   }
 306 #ifndef PRODUCT
 307   void set_last_bottom(HeapWord* last_bottom) {
 308     _last_bottom = last_bottom;
 309   }
 310 #endif
 311 };
 312 
 313 // A structure to represent a point at which objects are being copied
 314 // during compaction.
 315 class CompactPoint : public StackObj {
 316 public:
 317   Generation* gen;
 318   CompactibleSpace* space;
 319   HeapWord* threshold;
 320 
 321   CompactPoint(Generation* g = NULL) :
 322     gen(g), space(NULL), threshold(0) {}
 323 };
 324 
 325 // A space that supports compaction operations.  This is usually, but not
 326 // necessarily, a space that is normally contiguous.  But, for example, a
 327 // free-list-based space whose normal collection is a mark-sweep without
 328 // compaction could still support compaction in full GC's.
 329 
 330 class CompactibleSpace: public Space {
 331   friend class VMStructs;
 332   friend class CompactibleFreeListSpace;
 333 private:
 334   HeapWord* _compaction_top;
 335   CompactibleSpace* _next_compaction_space;
 336 
 337 public:
 338   CompactibleSpace() :
 339    _compaction_top(NULL), _next_compaction_space(NULL) {}
 340 
 341   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 342   virtual void clear(bool mangle_space);
 343 
 344   // Used temporarily during a compaction phase to hold the value
 345   // top should have when compaction is complete.
 346   HeapWord* compaction_top() const { return _compaction_top;    }
 347 
 348   void set_compaction_top(HeapWord* value) {
 349     assert(value == NULL || (value >= bottom() && value <= end()),
 350       "should point inside space");
 351     _compaction_top = value;
 352   }
 353 
 354   // Perform operations on the space needed after a compaction
 355   // has been performed.
 356   virtual void reset_after_compaction() = 0;
 357 
 358   // Returns the next space (in the current generation) to be compacted in
 359   // the global compaction order.  Also is used to select the next
 360   // space into which to compact.
 361 
 362   virtual CompactibleSpace* next_compaction_space() const {
 363     return _next_compaction_space;
 364   }
 365 
 366   void set_next_compaction_space(CompactibleSpace* csp) {
 367     _next_compaction_space = csp;
 368   }
 369 
 370   // MarkSweep support phase2
 371 
 372   // Start the process of compaction of the current space: compute
 373   // post-compaction addresses, and insert forwarding pointers.  The fields
 374   // "cp->gen" and "cp->compaction_space" are the generation and space into
 375   // which we are currently compacting.  This call updates "cp" as necessary,
 376   // and leaves the "compaction_top" of the final value of
 377   // "cp->compaction_space" up-to-date.  Offset tables may be updated in
 378   // this phase as if the final copy had occurred; if so, "cp->threshold"
 379   // indicates when the next such action should be taken.
 380   virtual void prepare_for_compaction(CompactPoint* cp);
 381   // MarkSweep support phase3
 382   virtual void adjust_pointers();
 383   // MarkSweep support phase4
 384   virtual void compact();
 385 
 386   // The maximum percentage of objects that can be dead in the compacted
 387   // live part of a compacted space ("deadwood" support.)
 388   virtual size_t allowed_dead_ratio() const { return 0; };
 389 
 390   // Some contiguous spaces may maintain some data structures that should
 391   // be updated whenever an allocation crosses a boundary.  This function
 392   // returns the first such boundary.
 393   // (The default implementation returns the end of the space, so the
 394   // boundary is never crossed.)
 395   virtual HeapWord* initialize_threshold() { return end(); }
 396 
 397   // "q" is an object of the given "size" that should be forwarded;
 398   // "cp" names the generation ("gen") and containing "this" (which must
 399   // also equal "cp->space").  "compact_top" is where in "this" the
 400   // next object should be forwarded to.  If there is room in "this" for
 401   // the object, insert an appropriate forwarding pointer in "q".
 402   // If not, go to the next compaction space (there must
 403   // be one, since compaction must succeed -- we go to the first space of
 404   // the previous generation if necessary, updating "cp"), reset compact_top
 405   // and then forward.  In either case, returns the new value of "compact_top".
 406   // If the forwarding crosses "cp->threshold", invokes the "cross_threshold"
 407   // function of the then-current compaction space, and updates "cp->threshold
 408   // accordingly".
 409   virtual HeapWord* forward(oop q, size_t size, CompactPoint* cp,
 410                     HeapWord* compact_top);
 411 
 412   // Return a size with adjustments as required of the space.
 413   virtual size_t adjust_object_size_v(size_t size) const { return size; }
 414 
 415 protected:
 416   // Used during compaction.
 417   HeapWord* _first_dead;
 418   HeapWord* _end_of_live;
 419 
 420   // Minimum size of a free block.
 421   virtual size_t minimum_free_block_size() const { return 0; }
 422 
 423   // This the function is invoked when an allocation of an object covering
 424   // "start" to "end occurs crosses the threshold; returns the next
 425   // threshold.  (The default implementation does nothing.)
 426   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* the_end) {
 427     return end();
 428   }
 429 
 430   // Requires "allowed_deadspace_words > 0", that "q" is the start of a
 431   // free block of the given "word_len", and that "q", were it an object,
 432   // would not move if forwarded.  If the size allows, fill the free
 433   // block with an object, to prevent excessive compaction.  Returns "true"
 434   // iff the free region was made deadspace, and modifies
 435   // "allowed_deadspace_words" to reflect the number of available deadspace
 436   // words remaining after this operation.
 437   bool insert_deadspace(size_t& allowed_deadspace_words, HeapWord* q,
 438                         size_t word_len);
 439 };
 440 
 441 class GenSpaceMangler;
 442 
 443 // A space in which the free area is contiguous.  It therefore supports
 444 // faster allocation, and compaction.
 445 class ContiguousSpace: public CompactibleSpace {
 446   friend class OneContigSpaceCardGeneration;
 447   friend class VMStructs;
 448  protected:
 449   HeapWord* _top;
 450   HeapWord* _concurrent_iteration_safe_limit;
 451   // A helper for mangling the unused area of the space in debug builds.
 452   GenSpaceMangler* _mangler;
 453 
 454   GenSpaceMangler* mangler() { return _mangler; }
 455 
 456   // Allocation helpers (return NULL if full).
 457   inline HeapWord* allocate_impl(size_t word_size);
 458   inline HeapWord* par_allocate_impl(size_t word_size);
 459 
 460  public:
 461   ContiguousSpace();
 462   ~ContiguousSpace();
 463 
 464   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 465   virtual void clear(bool mangle_space);
 466 
 467   // Accessors
 468   HeapWord* top() const            { return _top;    }
 469   void set_top(HeapWord* value)    { _top = value; }
 470 
 471   void set_saved_mark()            { _saved_mark_word = top();    }
 472   void reset_saved_mark()          { _saved_mark_word = bottom(); }
 473 
 474   WaterMark bottom_mark()     { return WaterMark(this, bottom()); }
 475   WaterMark top_mark()        { return WaterMark(this, top()); }
 476   WaterMark saved_mark()      { return WaterMark(this, saved_mark_word()); }
 477   bool saved_mark_at_top() const { return saved_mark_word() == top(); }
 478 
 479   // In debug mode mangle (write it with a particular bit
 480   // pattern) the unused part of a space.
 481 
 482   // Used to save the an address in a space for later use during mangling.
 483   void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN;
 484   // Used to save the space's current top for later use during mangling.
 485   void set_top_for_allocations() PRODUCT_RETURN;
 486 
 487   // Mangle regions in the space from the current top up to the
 488   // previously mangled part of the space.
 489   void mangle_unused_area() PRODUCT_RETURN;
 490   // Mangle [top, end)
 491   void mangle_unused_area_complete() PRODUCT_RETURN;
 492   // Mangle the given MemRegion.
 493   void mangle_region(MemRegion mr) PRODUCT_RETURN;
 494 
 495   // Do some sparse checking on the area that should have been mangled.
 496   void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN;
 497   // Check the complete area that should have been mangled.
 498   // This code may be NULL depending on the macro DEBUG_MANGLING.
 499   void check_mangled_unused_area_complete() PRODUCT_RETURN;
 500 
 501   // Size computations: sizes in bytes.
 502   size_t capacity() const        { return byte_size(bottom(), end()); }
 503   size_t used() const            { return byte_size(bottom(), top()); }
 504   size_t free() const            { return byte_size(top(),    end()); }
 505 
 506   virtual bool is_free_block(const HeapWord* p) const;
 507 
 508   // In a contiguous space we have a more obvious bound on what parts
 509   // contain objects.
 510   MemRegion used_region() const { return MemRegion(bottom(), top()); }
 511 
 512   // Allocation (return NULL if full)
 513   virtual HeapWord* allocate(size_t word_size);
 514   virtual HeapWord* par_allocate(size_t word_size);
 515   HeapWord* allocate_aligned(size_t word_size);
 516 
 517   // Iteration
 518   void oop_iterate(ExtendedOopClosure* cl);
 519   void object_iterate(ObjectClosure* blk);
 520   // For contiguous spaces this method will iterate safely over objects
 521   // in the space (i.e., between bottom and top) when at a safepoint.
 522   void safe_object_iterate(ObjectClosure* blk);
 523 
 524   // Iterate over as many initialized objects in the space as possible,
 525   // calling "cl.do_object_careful" on each. Return NULL if all objects
 526   // in the space (at the start of the iteration) were iterated over.
 527   // Return an address indicating the extent of the iteration in the
 528   // event that the iteration had to return because of finding an
 529   // uninitialized object in the space, or if the closure "cl"
 530   // signaled early termination.
 531   HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
 532   HeapWord* concurrent_iteration_safe_limit() {
 533     assert(_concurrent_iteration_safe_limit <= top(),
 534            "_concurrent_iteration_safe_limit update missed");
 535     return _concurrent_iteration_safe_limit;
 536   }
 537   // changes the safe limit, all objects from bottom() to the new
 538   // limit should be properly initialized
 539   void set_concurrent_iteration_safe_limit(HeapWord* new_limit) {
 540     assert(new_limit <= top(), "uninitialized objects in the safe range");
 541     _concurrent_iteration_safe_limit = new_limit;
 542   }
 543 
 544 
 545 #if INCLUDE_ALL_GCS
 546   // In support of parallel oop_iterate.
 547   #define ContigSpace_PAR_OOP_ITERATE_DECL(OopClosureType, nv_suffix)  \
 548     void par_oop_iterate(MemRegion mr, OopClosureType* blk);
 549 
 550     ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DECL)
 551   #undef ContigSpace_PAR_OOP_ITERATE_DECL
 552 #endif // INCLUDE_ALL_GCS
 553 
 554   // Compaction support
 555   virtual void reset_after_compaction() {
 556     assert(compaction_top() >= bottom() && compaction_top() <= end(), "should point inside space");
 557     set_top(compaction_top());
 558     // set new iteration safe limit
 559     set_concurrent_iteration_safe_limit(compaction_top());
 560   }
 561 
 562   // Override.
 563   DirtyCardToOopClosure* new_dcto_cl(ExtendedOopClosure* cl,
 564                                      CardTableModRefBS::PrecisionStyle precision,
 565                                      HeapWord* boundary = NULL);
 566 
 567   // Apply "blk->do_oop" to the addresses of all reference fields in objects
 568   // starting with the _saved_mark_word, which was noted during a generation's
 569   // save_marks and is required to denote the head of an object.
 570   // Fields in objects allocated by applications of the closure
 571   // *are* included in the iteration.
 572   // Updates _saved_mark_word to point to just after the last object
 573   // iterated over.
 574 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix)  \
 575   void oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk);
 576 
 577   ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DECL)
 578 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DECL
 579 
 580   // Same as object_iterate, but starting from "mark", which is required
 581   // to denote the start of an object.  Objects allocated by
 582   // applications of the closure *are* included in the iteration.
 583   virtual void object_iterate_from(WaterMark mark, ObjectClosure* blk);
 584 
 585   // Very inefficient implementation.
 586   virtual HeapWord* block_start_const(const void* p) const;
 587   size_t block_size(const HeapWord* p) const;
 588   // If a block is in the allocated area, it is an object.
 589   bool block_is_obj(const HeapWord* p) const { return p < top(); }
 590 
 591   // Addresses for inlined allocation
 592   HeapWord** top_addr() { return &_top; }
 593   HeapWord** end_addr() { return &_end; }
 594 
 595   // Overrides for more efficient compaction support.
 596   void prepare_for_compaction(CompactPoint* cp);
 597 
 598   // PrintHeapAtGC support.
 599   virtual void print_on(outputStream* st) const;
 600 
 601   // Checked dynamic downcasts.
 602   virtual ContiguousSpace* toContiguousSpace() {
 603     return this;
 604   }
 605 
 606   // Debugging
 607   virtual void verify() const;
 608 
 609   // Used to increase collection frequency.  "factor" of 0 means entire
 610   // space.
 611   void allocate_temporary_filler(int factor);
 612 
 613 };
 614 
 615 
 616 // A dirty card to oop closure that does filtering.
 617 // It knows how to filter out objects that are outside of the _boundary.
 618 class Filtering_DCTOC : public DirtyCardToOopClosure {
 619 protected:
 620   // Override.
 621   void walk_mem_region(MemRegion mr,
 622                        HeapWord* bottom, HeapWord* top);
 623 
 624   // Walk the given memory region, from bottom to top, applying
 625   // the given oop closure to (possibly) all objects found. The
 626   // given oop closure may or may not be the same as the oop
 627   // closure with which this closure was created, as it may
 628   // be a filtering closure which makes use of the _boundary.
 629   // We offer two signatures, so the FilteringClosure static type is
 630   // apparent.
 631   virtual void walk_mem_region_with_cl(MemRegion mr,
 632                                        HeapWord* bottom, HeapWord* top,
 633                                        ExtendedOopClosure* cl) = 0;
 634   virtual void walk_mem_region_with_cl(MemRegion mr,
 635                                        HeapWord* bottom, HeapWord* top,
 636                                        FilteringClosure* cl) = 0;
 637 
 638 public:
 639   Filtering_DCTOC(Space* sp, ExtendedOopClosure* cl,
 640                   CardTableModRefBS::PrecisionStyle precision,
 641                   HeapWord* boundary) :
 642     DirtyCardToOopClosure(sp, cl, precision, boundary) {}
 643 };
 644 
 645 // A dirty card to oop closure for contiguous spaces
 646 // (ContiguousSpace and sub-classes).
 647 // It is a FilteringClosure, as defined above, and it knows:
 648 //
 649 // 1. That the actual top of any area in a memory region
 650 //    contained by the space is bounded by the end of the contiguous
 651 //    region of the space.
 652 // 2. That the space is really made up of objects and not just
 653 //    blocks.
 654 
 655 class ContiguousSpaceDCTOC : public Filtering_DCTOC {
 656 protected:
 657   // Overrides.
 658   HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
 659 
 660   virtual void walk_mem_region_with_cl(MemRegion mr,
 661                                        HeapWord* bottom, HeapWord* top,
 662                                        ExtendedOopClosure* cl);
 663   virtual void walk_mem_region_with_cl(MemRegion mr,
 664                                        HeapWord* bottom, HeapWord* top,
 665                                        FilteringClosure* cl);
 666 
 667 public:
 668   ContiguousSpaceDCTOC(ContiguousSpace* sp, ExtendedOopClosure* cl,
 669                        CardTableModRefBS::PrecisionStyle precision,
 670                        HeapWord* boundary) :
 671     Filtering_DCTOC(sp, cl, precision, boundary)
 672   {}
 673 };
 674 
 675 // A ContigSpace that Supports an efficient "block_start" operation via
 676 // a BlockOffsetArray (whose BlockOffsetSharedArray may be shared with
 677 // other spaces.)  This is the abstract base class for old generation
 678 // (tenured) spaces.
 679 
 680 class OffsetTableContigSpace: public ContiguousSpace {
 681   friend class VMStructs;
 682  protected:
 683   BlockOffsetArrayContigSpace _offsets;
 684   Mutex _par_alloc_lock;
 685 
 686  public:
 687   // Constructor
 688   OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
 689                          MemRegion mr);
 690 
 691   void set_bottom(HeapWord* value);
 692   void set_end(HeapWord* value);
 693 
 694   void clear(bool mangle_space);
 695 
 696   inline HeapWord* block_start_const(const void* p) const;
 697 
 698   // Add offset table update.
 699   virtual inline HeapWord* allocate(size_t word_size);
 700   inline HeapWord* par_allocate(size_t word_size);
 701 
 702   // MarkSweep support phase3
 703   virtual HeapWord* initialize_threshold();
 704   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
 705 
 706   virtual void print_on(outputStream* st) const;
 707 
 708   // Debugging
 709   void verify() const;
 710 };
 711 
 712 
 713 // Class TenuredSpace is used by TenuredGeneration
 714 
 715 class TenuredSpace: public OffsetTableContigSpace {
 716   friend class VMStructs;
 717  protected:
 718   // Mark sweep support
 719   size_t allowed_dead_ratio() const;
 720  public:
 721   // Constructor
 722   TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray,
 723                MemRegion mr) :
 724     OffsetTableContigSpace(sharedOffsetArray, mr) {}
 725 };
 726 #endif // SHARE_VM_MEMORY_SPACE_HPP