1 /*
   2  * Copyright (c) 2001, 2007, 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 #include "incls/_precompiled.incl"
  26 #include "incls/_g1SATBCardTableModRefBS.cpp.incl"
  27 
  28 G1SATBCardTableModRefBS::G1SATBCardTableModRefBS(MemRegion whole_heap,
  29                                                  int max_covered_regions) :
  30     CardTableModRefBSForCTRS(whole_heap, max_covered_regions)
  31 {
  32   _kind = G1SATBCT;
  33 }
  34 
  35 
  36 void G1SATBCardTableModRefBS::enqueue(oop pre_val) {
  37   assert(pre_val->is_oop_or_null(true), "Error");
  38   if (!JavaThread::satb_mark_queue_set().is_active()) return;
  39   Thread* thr = Thread::current();
  40   if (thr->is_Java_thread()) {
  41     JavaThread* jt = (JavaThread*)thr;
  42     jt->satb_mark_queue().enqueue(pre_val);
  43   } else {
  44     MutexLocker x(Shared_SATB_Q_lock);
  45     JavaThread::satb_mark_queue_set().shared_satb_queue()->enqueue(pre_val);
  46   }
  47 }
  48 
  49 // When we know the current java thread:
  50 template <class T> void
  51 G1SATBCardTableModRefBS::write_ref_field_pre_static(T* field,
  52                                                     oop new_val,
  53                                                     JavaThread* jt) {
  54   if (!JavaThread::satb_mark_queue_set().is_active()) return;
  55   T heap_oop = oopDesc::load_heap_oop(field);
  56   if (!oopDesc::is_null(heap_oop)) {
  57     oop pre_val = oopDesc::decode_heap_oop_not_null(heap_oop);
  58     assert(pre_val->is_oop(true /* ignore mark word */), "Error");
  59     jt->satb_mark_queue().enqueue(pre_val);
  60   }
  61 }
  62 
  63 template <class T> void
  64 G1SATBCardTableModRefBS::write_ref_array_pre_work(T* dst, int count) {
  65   if (!JavaThread::satb_mark_queue_set().is_active()) return;
  66   T* elem_ptr = dst;
  67   for (int i = 0; i < count; i++, elem_ptr++) {
  68     T heap_oop = oopDesc::load_heap_oop(elem_ptr);
  69     if (!oopDesc::is_null(heap_oop)) {
  70       enqueue(oopDesc::decode_heap_oop_not_null(heap_oop));
  71     }
  72   }
  73 }
  74 
  75 G1SATBCardTableLoggingModRefBS::
  76 G1SATBCardTableLoggingModRefBS(MemRegion whole_heap,
  77                                int max_covered_regions) :
  78   G1SATBCardTableModRefBS(whole_heap, max_covered_regions),
  79   _dcqs(JavaThread::dirty_card_queue_set())
  80 {
  81   _kind = G1SATBCTLogging;
  82 }
  83 
  84 void
  85 G1SATBCardTableLoggingModRefBS::write_ref_field_work(void* field,
  86                                                      oop new_val) {
  87   jbyte* byte = byte_for(field);
  88   if (*byte != dirty_card) {
  89     *byte = dirty_card;
  90     Thread* thr = Thread::current();
  91     if (thr->is_Java_thread()) {
  92       JavaThread* jt = (JavaThread*)thr;
  93       jt->dirty_card_queue().enqueue(byte);
  94     } else {
  95       MutexLockerEx x(Shared_DirtyCardQ_lock,
  96                       Mutex::_no_safepoint_check_flag);
  97       _dcqs.shared_dirty_card_queue()->enqueue(byte);
  98     }
  99   }
 100 }
 101 
 102 void
 103 G1SATBCardTableLoggingModRefBS::write_ref_field_static(void* field,
 104                                                        oop new_val) {
 105   uintptr_t field_uint = (uintptr_t)field;
 106   uintptr_t new_val_uint = (uintptr_t)new_val;
 107   uintptr_t comb = field_uint ^ new_val_uint;
 108   comb = comb >> HeapRegion::LogOfHRGrainBytes;
 109   if (comb == 0) return;
 110   if (new_val == NULL) return;
 111   // Otherwise, log it.
 112   G1SATBCardTableLoggingModRefBS* g1_bs =
 113     (G1SATBCardTableLoggingModRefBS*)Universe::heap()->barrier_set();
 114   g1_bs->write_ref_field_work(field, new_val);
 115 }
 116 
 117 void
 118 G1SATBCardTableLoggingModRefBS::invalidate(MemRegion mr, bool whole_heap) {
 119   jbyte* byte = byte_for(mr.start());
 120   jbyte* last_byte = byte_for(mr.last());
 121   Thread* thr = Thread::current();
 122   if (whole_heap) {
 123     while (byte <= last_byte) {
 124       *byte = dirty_card;
 125       byte++;
 126     }
 127   } else {
 128     // Enqueue if necessary.
 129     if (thr->is_Java_thread()) {
 130       JavaThread* jt = (JavaThread*)thr;
 131       while (byte <= last_byte) {
 132         if (*byte != dirty_card) {
 133           *byte = dirty_card;
 134           jt->dirty_card_queue().enqueue(byte);
 135         }
 136         byte++;
 137       }
 138     } else {
 139       MutexLockerEx x(Shared_DirtyCardQ_lock,
 140                       Mutex::_no_safepoint_check_flag);
 141       while (byte <= last_byte) {
 142         if (*byte != dirty_card) {
 143           *byte = dirty_card;
 144           _dcqs.shared_dirty_card_queue()->enqueue(byte);
 145         }
 146         byte++;
 147       }
 148     }
 149   }
 150 }