1 /*
   2  * Copyright (c) 2001, 2019, 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_GC_G1_G1CARDTABLE_HPP
  26 #define SHARE_GC_G1_G1CARDTABLE_HPP
  27 
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "gc/shared/cardTable.hpp"
  30 #include "oops/oopsHierarchy.hpp"
  31 #include "utilities/macros.hpp"
  32 
  33 class G1CardTable;
  34 class G1RegionToSpaceMapper;
  35 
  36 class G1CardTableChangedListener : public G1MappingChangedListener {
  37  private:
  38   G1CardTable* _card_table;
  39  public:
  40   G1CardTableChangedListener() : _card_table(NULL) { }
  41 
  42   void set_card_table(G1CardTable* card_table) { _card_table = card_table; }
  43 
  44   virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled);
  45 };
  46 
  47 class G1CardTable: public CardTable {
  48   friend class VMStructs;
  49   friend class G1CardTableChangedListener;
  50 
  51   G1CardTableChangedListener _listener;
  52 
  53   enum G1CardValues {
  54     g1_young_gen = CT_MR_BS_last_reserved << 1
  55   };
  56 
  57 public:
  58   G1CardTable(MemRegion whole_heap): CardTable(whole_heap, /* scanned concurrently */ true), _listener() {
  59     _listener.set_card_table(this);
  60   }
  61   bool is_card_dirty(size_t card_index) {
  62     return _byte_map[card_index] == dirty_card_val();
  63   }
  64 
  65   static CardValue g1_young_card_val() {
  66     assert(!G1FastWriteBarrier, "should not be called");
  67     return g1_young_gen;
  68   }
  69 
  70 /*
  71    Claimed and deferred bits are used together in G1 during the evacuation
  72    pause. These bits can have the following state transitions:
  73    1. The claimed bit can be put over any other card state. Except that
  74       the "dirty -> dirty and claimed" transition is checked for in
  75       G1 code and is not used.
  76    2. Deferred bit can be set only if the previous state of the card
  77       was either clean or claimed. mark_card_deferred() is wait-free.
  78       We do not care if the operation is be successful because if
  79       it does not it will only result in duplicate entry in the update
  80       buffer because of the "cache-miss". So it's not worth spinning.
  81  */
  82 
  83   bool is_card_claimed(size_t card_index) {
  84     CardValue val = _byte_map[card_index];
  85     return (val & (clean_card_mask_val() | claimed_card_val())) == claimed_card_val();
  86   }
  87 
  88   inline void set_card_claimed(size_t card_index);
  89 
  90   void verify_g1_young_region(MemRegion mr) PRODUCT_RETURN;
  91   void verfiy_claimed_dirty_region(MemRegion mr) PRODUCT_RETURN;
  92   void g1_mark_as_young(const MemRegion& mr);
  93 
  94   bool mark_card_deferred(size_t card_index);
  95 
  96   bool is_card_deferred(size_t card_index) {
  97     CardValue val = _byte_map[card_index];
  98     return (val & (clean_card_mask_val() | deferred_card_val())) == deferred_card_val();
  99   }
 100 
 101   static size_t compute_size(size_t mem_region_size_in_words) {
 102     size_t number_of_slots = (mem_region_size_in_words / card_size_in_words);
 103     return ReservedSpace::allocation_align_size_up(number_of_slots);
 104   }
 105 
 106   // Returns how many bytes of the heap a single byte of the Card Table corresponds to.
 107   static size_t heap_map_factor() { return card_size; }
 108 
 109   void initialize() {}
 110   void initialize(G1RegionToSpaceMapper* mapper);
 111 
 112   virtual void resize_covered_region(MemRegion new_region) { ShouldNotReachHere(); }
 113 
 114   virtual bool is_in_young(oop obj) const;
 115 };
 116 
 117 #endif // SHARE_GC_G1_G1CARDTABLE_HPP