1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)barrierSet.hpp       1.18 07/05/05 17:05:43 JVM"
   3 #endif
   4 /*
   5  * Copyright 2000-2008 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 // This class provides the interface between a barrier implementation and
  29 // the rest of the system.
  30 
  31 class BarrierSet: public CHeapObj {
  32   friend class VMStructs;
  33 public:
  34   enum Name {
  35     ModRef,
  36     CardTableModRef,
  37     CardTableExtension,
  38     G1SATBCT,
  39     G1SATBCTLogging,
  40     Other,
  41     Uninit
  42   };
  43 
  44 protected:
  45   int _max_covered_regions;
  46   Name _kind;
  47   
  48 public:
  49 
  50   BarrierSet() { _kind = Uninit; }
  51   // To get around prohibition on RTTI.
  52   BarrierSet::Name kind() { return _kind; }
  53   virtual bool is_a(BarrierSet::Name bsn) = 0;
  54 
  55   // These operations indicate what kind of barriers the BarrierSet has.
  56   virtual bool has_read_ref_barrier() = 0;
  57   virtual bool has_read_prim_barrier() = 0;
  58   virtual bool has_write_ref_barrier() = 0;
  59   virtual bool has_write_ref_pre_barrier() = 0;
  60   virtual bool has_write_prim_barrier() = 0;
  61 
  62   // These functions indicate whether a particular access of the given
  63   // kinds requires a barrier.
  64   virtual bool read_ref_needs_barrier(void* field) = 0;
  65   virtual bool read_prim_needs_barrier(HeapWord* field, size_t bytes) = 0;
  66   virtual bool write_ref_needs_barrier(void* field, oop new_val) = 0;
  67   virtual bool write_prim_needs_barrier(HeapWord* field, size_t bytes,
  68                                         juint val1, juint val2) = 0;
  69 
  70   // The first four operations provide a direct implementation of the
  71   // barrier set.  An interpreter loop, for example, could call these
  72   // directly, as appropriate.
  73 
  74   // Invoke the barrier, if any, necessary when reading the given ref field.
  75   virtual void read_ref_field(void* field) = 0;
  76 
  77   // Invoke the barrier, if any, necessary when reading the given primitive
  78   // "field" of "bytes" bytes in "obj".
  79   virtual void read_prim_field(HeapWord* field, size_t bytes) = 0;
  80 
  81   // Invoke the barrier, if any, necessary when writing "new_val" into the
  82   // ref field at "offset" in "obj".
  83   // (For efficiency reasons, this operation is specialized for certain
  84   // barrier types.  Semantically, it should be thought of as a call to the 
  85   // virtual "_work" function below, which must implement the barrier.)
  86   // First the pre-write versions...
  87   inline void write_ref_field_pre(void* field, oop new_val);
  88 protected:
  89   virtual void write_ref_field_pre_work(void* field, oop new_val) {};
  90 public:
  91 
  92   // ...then the post-write version.
  93   inline void write_ref_field(void* field, oop new_val);
  94 protected:
  95   virtual void write_ref_field_work(void* field, oop new_val) = 0;
  96 public:
  97 
  98   // Invoke the barrier, if any, necessary when writing the "bytes"-byte
  99   // value(s) "val1" (and "val2") into the primitive "field".
 100   virtual void write_prim_field(HeapWord* field, size_t bytes,
 101                                 juint val1, juint val2) = 0;
 102 
 103   // Operations on arrays, or general regions (e.g., for "clone") may be
 104   // optimized by some barriers.
 105 
 106   // The first six operations tell whether such an optimization exists for
 107   // the particular barrier.
 108   virtual bool has_read_ref_array_opt() = 0;
 109   virtual bool has_read_prim_array_opt() = 0;
 110   virtual bool has_write_ref_array_pre_opt() { return true; }
 111   virtual bool has_write_ref_array_opt() = 0;
 112   virtual bool has_write_prim_array_opt() = 0;
 113 
 114   virtual bool has_read_region_opt() = 0;
 115   virtual bool has_write_region_opt() = 0;
 116 
 117   // These operations should assert false unless the correponding operation
 118   // above returns true.  Otherwise, they should perform an appropriate
 119   // barrier for an array whose elements are all in the given memory region.
 120   virtual void read_ref_array(MemRegion mr) = 0;
 121   virtual void read_prim_array(MemRegion mr) = 0;
 122 
 123   virtual void write_ref_array_pre(MemRegion mr) {}
 124   inline void write_ref_array(MemRegion mr);
 125 
 126   // Static versions, suitable for calling from generated code.
 127   static void static_write_ref_array_pre(HeapWord* start, size_t count);
 128   static void static_write_ref_array_post(HeapWord* start, size_t count);
 129 
 130 protected:
 131   virtual void write_ref_array_work(MemRegion mr) = 0;
 132 public:
 133   virtual void write_prim_array(MemRegion mr) = 0;
 134 
 135   virtual void read_region(MemRegion mr) = 0;
 136 
 137   // (For efficiency reasons, this operation is specialized for certain
 138   // barrier types.  Semantically, it should be thought of as a call to the 
 139   // virtual "_work" function below, which must implement the barrier.)
 140   inline void write_region(MemRegion mr);
 141 protected:
 142   virtual void write_region_work(MemRegion mr) = 0;
 143 public:
 144 
 145   // Some barrier sets create tables whose elements correspond to parts of
 146   // the heap; the CardTableModRefBS is an example.  Such barrier sets will
 147   // normally reserve space for such tables, and commit parts of the table
 148   // "covering" parts of the heap that are committed.  The constructor is
 149   // passed the maximum number of independently committable subregions to
 150   // be covered, and the "resize_covoered_region" function allows the
 151   // sub-parts of the heap to inform the barrier set of changes of their
 152   // sizes.
 153   BarrierSet(int max_covered_regions) :
 154     _max_covered_regions(max_covered_regions) {}
 155 
 156   // Inform the BarrierSet that the the covered heap region that starts
 157   // with "base" has been changed to have the given size (possibly from 0,
 158   // for initialization.)
 159   virtual void resize_covered_region(MemRegion new_region) = 0;
 160 
 161   // If the barrier set imposes any alignment restrictions on boundaries
 162   // within the heap, this function tells whether they are met.
 163   virtual bool is_aligned(HeapWord* addr) = 0;
 164 
 165 };