1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)blockOffsetTable.hpp 1.57 07/05/05 17:05:43 JVM"
   3 #endif
   4 /*
   5  * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // The CollectedHeap type requires subtypes to implement a method
  29 // "block_start".  For some subtypes, notably generational
  30 // systems using card-table-based write barriers, the efficiency of this
  31 // operation may be important.  Implementations of the "BlockOffsetArray"
  32 // class may be useful in providing such efficient implementations.
  33 //
  34 // BlockOffsetTable (abstract)
  35 //   - BlockOffsetArray (abstract)
  36 //     - BlockOffsetArrayNonContigSpace
  37 //     - BlockOffsetArrayContigSpace
  38 //      
  39 
  40 class ContiguousSpace;
  41 class SerializeOopClosure;
  42 
  43 //////////////////////////////////////////////////////////////////////////
  44 // The BlockOffsetTable "interface"
  45 //////////////////////////////////////////////////////////////////////////
  46 class BlockOffsetTable VALUE_OBJ_CLASS_SPEC {
  47   friend class VMStructs;
  48 protected:
  49   // These members describe the region covered by the table.
  50 
  51   // The space this table is covering.
  52   HeapWord* _bottom;    // == reserved.start
  53   HeapWord* _end;       // End of currently allocated region.
  54 
  55 public:
  56   // Initialize the table to cover the given space.
  57   // The contents of the initial table are undefined.
  58   BlockOffsetTable(HeapWord* bottom, HeapWord* end):
  59     _bottom(bottom), _end(end) {
  60     assert(_bottom <= _end, "arguments out of order");
  61   }
  62 
  63   // Note that the committed size of the covered space may have changed,
  64   // so the table size might also wish to change.
  65   virtual void resize(size_t new_word_size) = 0;
  66 
  67   virtual void set_bottom(HeapWord* new_bottom) {
  68     assert(new_bottom <= _end, "new_bottom > _end");
  69     _bottom = new_bottom;
  70     resize(pointer_delta(_end, _bottom));
  71   }
  72 
  73   // Requires "addr" to be contained by a block, and returns the address of
  74   // the start of that block.
  75   virtual HeapWord* block_start_unsafe(const void* addr) const = 0;
  76 
  77   // Returns the address of the start of the block containing "addr", or
  78   // else "null" if it is covered by no block.
  79   HeapWord* block_start(const void* addr) const;
  80 };
  81 
  82 //////////////////////////////////////////////////////////////////////////
  83 // One implementation of "BlockOffsetTable," the BlockOffsetArray,
  84 // divides the covered region into "N"-word subregions (where
  85 // "N" = 2^"LogN".  An array with an entry for each such subregion
  86 // indicates how far back one must go to find the start of the
  87 // chunk that includes the first word of the subregion.
  88 //
  89 // Each BlockOffsetArray is owned by a Space.  However, the actual array
  90 // may be shared by several BlockOffsetArrays; this is useful
  91 // when a single resizable area (such as a generation) is divided up into
  92 // several spaces in which contiguous allocation takes place.  (Consider,
  93 // for example, the garbage-first generation.)
  94 
  95 // Here is the shared array type.
  96 //////////////////////////////////////////////////////////////////////////
  97 // BlockOffsetSharedArray
  98 //////////////////////////////////////////////////////////////////////////
  99 class BlockOffsetSharedArray: public CHeapObj {
 100   friend class BlockOffsetArray;
 101   friend class BlockOffsetArrayNonContigSpace;
 102   friend class BlockOffsetArrayContigSpace;
 103   friend class VMStructs;
 104 
 105  private:
 106   enum SomePrivateConstants {
 107     LogN = 9,
 108     LogN_words = LogN - LogHeapWordSize,
 109     N_bytes = 1 << LogN,
 110     N_words = 1 << LogN_words
 111   };
 112 
 113   // The reserved region covered by the shared array.
 114   MemRegion _reserved;
 115 
 116   // End of the current committed region.
 117   HeapWord* _end;
 118 
 119   // Array for keeping offsets for retrieving object start fast given an
 120   // address.
 121   VirtualSpace _vs;
 122   u_char* _offset_array;          // byte array keeping backwards offsets
 123 
 124  protected:
 125   // Bounds checking accessors:
 126   // For performance these have to devolve to array accesses in product builds.
 127   u_char offset_array(size_t index) const {
 128     assert(index < _vs.committed_size(), "index out of range");
 129     return _offset_array[index];
 130   }
 131   void set_offset_array(size_t index, u_char offset) {
 132     assert(index < _vs.committed_size(), "index out of range");
 133     _offset_array[index] = offset;
 134   }
 135   void set_offset_array(size_t index, HeapWord* high, HeapWord* low) {
 136     assert(index < _vs.committed_size(), "index out of range");
 137     assert(high >= low, "addresses out of order");
 138     assert(pointer_delta(high, low) <= N_words, "offset too large");
 139     _offset_array[index] = (u_char)pointer_delta(high, low);
 140   }
 141   void set_offset_array(HeapWord* left, HeapWord* right, u_char offset) {
 142     assert(index_for(right - 1) < _vs.committed_size(),
 143            "right address out of range");
 144     assert(left  < right, "Heap addresses out of order");
 145     size_t num_cards = pointer_delta(right, left) >> LogN_words;
 146     memset(&_offset_array[index_for(left)], offset, num_cards);
 147   }
 148 
 149   void set_offset_array(size_t left, size_t right, u_char offset) {
 150     assert(right < _vs.committed_size(), "right address out of range");
 151     assert(left  <= right, "indexes out of order");
 152     size_t num_cards = right - left + 1;
 153     memset(&_offset_array[left], offset, num_cards);
 154   }
 155 
 156   void check_offset_array(size_t index, HeapWord* high, HeapWord* low) const {
 157     assert(index < _vs.committed_size(), "index out of range");
 158     assert(high >= low, "addresses out of order");
 159     assert(pointer_delta(high, low) <= N_words, "offset too large");
 160     assert(_offset_array[index] == pointer_delta(high, low), 
 161            "Wrong offset");
 162   }
 163 
 164   bool is_card_boundary(HeapWord* p) const;
 165 
 166   // Return the number of slots needed for an offset array 
 167   // that covers mem_region_words words.
 168   // We always add an extra slot because if an object 
 169   // ends on a card boundary we put a 0 in the next 
 170   // offset array slot, so we want that slot always 
 171   // to be reserved.
 172  
 173   size_t compute_size(size_t mem_region_words) {
 174     size_t number_of_slots = (mem_region_words / N_words) + 1;
 175     return ReservedSpace::allocation_align_size_up(number_of_slots);
 176   }
 177   
 178 public:
 179   // Initialize the table to cover from "base" to (at least)
 180   // "base + init_word_size".  In the future, the table may be expanded
 181   // (see "resize" below) up to the size of "_reserved" (which must be at
 182   // least "init_word_size".)  The contents of the initial table are
 183   // undefined; it is the responsibility of the constituent
 184   // BlockOffsetTable(s) to initialize cards.
 185   BlockOffsetSharedArray(MemRegion reserved, size_t init_word_size);
 186 
 187   // Notes a change in the committed size of the region covered by the
 188   // table.  The "new_word_size" may not be larger than the size of the
 189   // reserved region this table covers.
 190   void resize(size_t new_word_size);
 191 
 192   void set_bottom(HeapWord* new_bottom);
 193 
 194   // Updates all the BlockOffsetArray's sharing this shared array to
 195   // reflect the current "top"'s of their spaces.
 196   void update_offset_arrays();   // Not yet implemented!
 197 
 198   // Return the appropriate index into "_offset_array" for "p".
 199   size_t index_for(const void* p) const;
 200 
 201   // Return the address indicating the start of the region corresponding to 
 202   // "index" in "_offset_array".
 203   HeapWord* address_for_index(size_t index) const;
 204 
 205   // Shared space support
 206   void serialize(SerializeOopClosure* soc, HeapWord* start, HeapWord* end);
 207 };
 208 
 209 //////////////////////////////////////////////////////////////////////////
 210 // The BlockOffsetArray whose subtypes use the BlockOffsetSharedArray.
 211 //////////////////////////////////////////////////////////////////////////
 212 class BlockOffsetArray: public BlockOffsetTable {
 213   friend class VMStructs;
 214  protected:
 215   // The following enums are used by do_block_internal() below
 216   enum Action {
 217     Action_single,      // BOT records a single block (see single_block())
 218     Action_mark,        // BOT marks the start of a block (see mark_block())
 219     Action_check        // Check that BOT records block correctly
 220                         // (see verify_single_block()).
 221   };
 222 
 223   enum SomePrivateConstants {
 224     N_words = BlockOffsetSharedArray::N_words,
 225     LogN    = BlockOffsetSharedArray::LogN,
 226     // entries "e" of at least N_words mean "go back by Base^(e-N_words)."
 227     // All entries are less than "N_words + N_powers".
 228     LogBase = 4,
 229     Base = (1 << LogBase),
 230     N_powers = 14
 231   };
 232 
 233   static size_t power_to_cards_back(uint i) {
 234     return 1 << (LogBase * i);
 235   }
 236   static size_t power_to_words_back(uint i) {
 237     return power_to_cards_back(i) * N_words;
 238   }
 239   static size_t entry_to_cards_back(u_char entry) {
 240     assert(entry >= N_words, "Precondition");
 241     return power_to_cards_back(entry - N_words);
 242   }
 243   static size_t entry_to_words_back(u_char entry) {
 244     assert(entry >= N_words, "Precondition");
 245     return power_to_words_back(entry - N_words);
 246   }
 247 
 248   // The shared array, which is shared with other BlockOffsetArray's
 249   // corresponding to different spaces within a generation or span of
 250   // memory.
 251   BlockOffsetSharedArray* _array;
 252 
 253   // The space that owns this subregion.
 254   Space* _sp;
 255 
 256   // If true, array entries are initialized to 0; otherwise, they are
 257   // initialized to point backwards to the beginning of the covered region.
 258   bool _init_to_zero;
 259 
 260   // Sets the entries
 261   // corresponding to the cards starting at "start" and ending at "end"
 262   // to point back to the card before "start": the interval [start, end)
 263   // is right-open.
 264   void set_remainder_to_point_to_start(HeapWord* start, HeapWord* end);
 265   // Same as above, except that the args here are a card _index_ interval
 266   // that is closed: [start_index, end_index]
 267   void set_remainder_to_point_to_start_incl(size_t start, size_t end);
 268 
 269   // A helper function for BOT adjustment/verification work
 270   void do_block_internal(HeapWord* blk_start, HeapWord* blk_end, Action action);
 271 
 272  public:
 273   // The space may not have its bottom and top set yet, which is why the
 274   // region is passed as a parameter.  If "init_to_zero" is true, the
 275   // elements of the array are initialized to zero.  Otherwise, they are
 276   // initialized to point backwards to the beginning.
 277   BlockOffsetArray(BlockOffsetSharedArray* array, MemRegion mr,
 278                    bool init_to_zero);
 279 
 280   // Note: this ought to be part of the constructor, but that would require
 281   // "this" to be passed as a parameter to a member constructor for
 282   // the containing concrete subtype of Space.
 283   // This would be legal C++, but MS VC++ doesn't allow it.
 284   void set_space(Space* sp) { _sp = sp; }
 285 
 286   // Resets the covered region to the given "mr".
 287   void set_region(MemRegion mr) {
 288     _bottom = mr.start();
 289     _end = mr.end();
 290   }
 291 
 292   // Note that the committed size of the covered space may have changed,
 293   // so the table size might also wish to change.
 294   virtual void resize(size_t new_word_size) {
 295     HeapWord* new_end = _bottom + new_word_size;
 296     if (_end < new_end && !init_to_zero()) {
 297       // verify that the old and new boundaries are also card boundaries
 298       assert(_array->is_card_boundary(_end),
 299              "_end not a card boundary");
 300       assert(_array->is_card_boundary(new_end),
 301              "new _end would not be a card boundary");
 302       // set all the newly added cards
 303       _array->set_offset_array(_end, new_end, N_words);
 304     }
 305     _end = new_end;  // update _end
 306   }
 307 
 308   // Adjust the BOT to show that it has a single block in the
 309   // range [blk_start, blk_start + size). All necessary BOT
 310   // cards are adjusted, but _unallocated_block isn't.
 311   void single_block(HeapWord* blk_start, HeapWord* blk_end);
 312   void single_block(HeapWord* blk, size_t size) {
 313     single_block(blk, blk + size);
 314   }
 315 
 316   // When the alloc_block() call returns, the block offset table should
 317   // have enough information such that any subsequent block_start() call
 318   // with an argument equal to an address that is within the range
 319   // [blk_start, blk_end) would return the value blk_start, provided
 320   // there have been no calls in between that reset this information
 321   // (e.g. see BlockOffsetArrayNonContigSpace::single_block() call
 322   // for an appropriate range covering the said interval).
 323   // These methods expect to be called with [blk_start, blk_end)
 324   // representing a block of memory in the heap.
 325   virtual void alloc_block(HeapWord* blk_start, HeapWord* blk_end);
 326   void alloc_block(HeapWord* blk, size_t size) {
 327     alloc_block(blk, blk + size);
 328   }
 329 
 330   // If true, initialize array slots with no allocated blocks to zero.
 331   // Otherwise, make them point back to the front.
 332   bool init_to_zero() { return _init_to_zero; }
 333 
 334   // Debugging
 335   // Return the index of the last entry in the "active" region.
 336   virtual size_t last_active_index() const = 0;
 337   // Verify the block offset table
 338   void verify() const;
 339   void check_all_cards(size_t left_card, size_t right_card) const;
 340 };
 341 
 342 ////////////////////////////////////////////////////////////////////////////
 343 // A subtype of BlockOffsetArray that takes advantage of the fact
 344 // that its underlying space is a NonContiguousSpace, so that some
 345 // specialized interfaces can be made available for spaces that
 346 // manipulate the table.
 347 ////////////////////////////////////////////////////////////////////////////
 348 class BlockOffsetArrayNonContigSpace: public BlockOffsetArray {
 349   friend class VMStructs;
 350  private:
 351   // The portion [_unallocated_block, _sp.end()) of the space that
 352   // is a single block known not to contain any objects.
 353   // NOTE: See BlockOffsetArrayUseUnallocatedBlock flag.
 354   HeapWord* _unallocated_block;
 355 
 356  public:
 357   BlockOffsetArrayNonContigSpace(BlockOffsetSharedArray* array, MemRegion mr):
 358     BlockOffsetArray(array, mr, false),
 359     _unallocated_block(_bottom) { }
 360 
 361   // accessor
 362   HeapWord* unallocated_block() const {
 363     assert(BlockOffsetArrayUseUnallocatedBlock,
 364            "_unallocated_block is not being maintained");
 365     return _unallocated_block;
 366   }
 367 
 368   void set_unallocated_block(HeapWord* block) {
 369     assert(BlockOffsetArrayUseUnallocatedBlock,
 370            "_unallocated_block is not being maintained");
 371     assert(block >= _bottom && block <= _end, "out of range");
 372     _unallocated_block = block;
 373   }
 374 
 375   // These methods expect to be called with [blk_start, blk_end)
 376   // representing a block of memory in the heap.
 377   void alloc_block(HeapWord* blk_start, HeapWord* blk_end);
 378   void alloc_block(HeapWord* blk, size_t size) {
 379     alloc_block(blk, blk + size);
 380   }
 381 
 382   // The following methods are useful and optimized for a
 383   // non-contiguous space. 
 384 
 385   // Given a block [blk_start, blk_start + full_blk_size), and
 386   // a left_blk_size < full_blk_size, adjust the BOT to show two
 387   // blocks [blk_start, blk_start + left_blk_size) and
 388   // [blk_start + left_blk_size, blk_start + full_blk_size).
 389   // It is assumed (and verified in the non-product VM) that the
 390   // BOT was correct for the original block.
 391   void split_block(HeapWord* blk_start, size_t full_blk_size,
 392                            size_t left_blk_size);
 393 
 394   // Adjust BOT to show that it has a block in the range
 395   // [blk_start, blk_start + size). Only the first card
 396   // of BOT is touched. It is assumed (and verified in the
 397   // non-product VM) that the remaining cards of the block
 398   // are correct.
 399   void mark_block(HeapWord* blk_start, HeapWord* blk_end);
 400   void mark_block(HeapWord* blk, size_t size) {
 401     mark_block(blk, blk + size);
 402   }
 403 
 404   // Adjust _unallocated_block to indicate that a particular
 405   // block has been newly allocated or freed. It is assumed (and
 406   // verified in the non-product VM) that the BOT is correct for
 407   // the given block.
 408   void allocated(HeapWord* blk_start, HeapWord* blk_end) {
 409     // Verify that the BOT shows [blk, blk + blk_size) to be one block. 
 410     verify_single_block(blk_start, blk_end); 
 411     if (BlockOffsetArrayUseUnallocatedBlock) {
 412       _unallocated_block = MAX2(_unallocated_block, blk_end); 
 413     }
 414   }
 415 
 416   void allocated(HeapWord* blk, size_t size) {
 417     allocated(blk, blk + size);
 418   }
 419 
 420   void freed(HeapWord* blk_start, HeapWord* blk_end);
 421   void freed(HeapWord* blk, size_t size) {
 422     freed(blk, blk + size);
 423   }
 424 
 425   HeapWord* block_start_unsafe(const void* addr) const;
 426 
 427   // Requires "addr" to be the start of a card and returns the
 428   // start of the block that contains the given address.
 429   HeapWord* block_start_careful(const void* addr) const;
 430 
 431 
 432   // Verification & debugging: ensure that the offset table reflects
 433   // the fact that the block [blk_start, blk_end) or [blk, blk + size)
 434   // is a single block of storage. NOTE: can't const this because of
 435   // call to non-const do_block_internal() below.
 436   void verify_single_block(HeapWord* blk_start, HeapWord* blk_end)
 437     PRODUCT_RETURN;
 438   void verify_single_block(HeapWord* blk, size_t size) PRODUCT_RETURN;
 439 
 440   // Verify that the given block is before _unallocated_block
 441   void verify_not_unallocated(HeapWord* blk_start, HeapWord* blk_end)
 442     const PRODUCT_RETURN;
 443   void verify_not_unallocated(HeapWord* blk, size_t size)
 444     const PRODUCT_RETURN;
 445 
 446   // Debugging support
 447   virtual size_t last_active_index() const;
 448 };
 449 
 450 ////////////////////////////////////////////////////////////////////////////
 451 // A subtype of BlockOffsetArray that takes advantage of the fact
 452 // that its underlying space is a ContiguousSpace, so that its "active"
 453 // region can be more efficiently tracked (than for a non-contiguous space).
 454 ////////////////////////////////////////////////////////////////////////////
 455 class BlockOffsetArrayContigSpace: public BlockOffsetArray {
 456   friend class VMStructs;
 457  private:
 458   // allocation boundary at which offset array must be updated
 459   HeapWord* _next_offset_threshold;
 460   size_t    _next_offset_index;      // index corresponding to that boundary
 461 
 462   // Work function when allocation start crosses threshold.
 463   void alloc_block_work(HeapWord* blk_start, HeapWord* blk_end);
 464 
 465  public:
 466   BlockOffsetArrayContigSpace(BlockOffsetSharedArray* array, MemRegion mr):
 467     BlockOffsetArray(array, mr, true) {
 468     _next_offset_threshold = NULL;
 469     _next_offset_index = 0;
 470   }
 471 
 472   void set_contig_space(ContiguousSpace* sp) { set_space((Space*)sp); }
 473 
 474   // Initialize the threshold for an empty heap.
 475   HeapWord* initialize_threshold();
 476   // Zero out the entry for _bottom (offset will be zero)
 477   void      zero_bottom_entry();
 478 
 479   // Return the next threshold, the point at which the table should be
 480   // updated.
 481   HeapWord* threshold() const { return _next_offset_threshold; }
 482 
 483   // In general, these methods expect to be called with
 484   // [blk_start, blk_end) representing a block of memory in the heap.
 485   // In this implementation, however, we are OK even if blk_start and/or
 486   // blk_end are NULL because NULL is represented as 0, and thus
 487   // never exceeds the "_next_offset_threshold".
 488   void alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
 489     if (blk_end > _next_offset_threshold) {
 490       alloc_block_work(blk_start, blk_end);
 491     }
 492   }
 493   void alloc_block(HeapWord* blk, size_t size) {
 494     alloc_block(blk, blk + size);
 495   }
 496 
 497   HeapWord* block_start_unsafe(const void* addr) const;
 498 
 499   void serialize(SerializeOopClosure* soc);
 500 
 501   // Debugging support
 502   virtual size_t last_active_index() const;
 503 };