1 /*
   2  * Copyright (c) 2016, 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 "precompiled.hpp"
  26 #include "gc/g1/g1ConcurrentMark.inline.hpp"
  27 #include "gc/g1/g1ConcurrentMarkObjArrayProcessor.inline.hpp"
  28 
  29 oop G1CMObjArrayProcessor::encode_array_slice(HeapWord* addr) {
  30   return oop((void*)((uintptr_t)addr | ArraySliceBit));
  31 }
  32 
  33 HeapWord* G1CMObjArrayProcessor::decode_array_slice(oop value) {
  34   assert(is_array_slice(value), "Given value " PTR_FORMAT " is not an array slice", p2i(value));
  35   return (HeapWord*)((uintptr_t)(void*)value & ~ArraySliceBit);
  36 }
  37 
  38 void G1CMObjArrayProcessor::push_array_slice(HeapWord* what) {
  39   oop obj = encode_array_slice(what);
  40   _task->push(obj);
  41 }
  42 
  43 size_t G1CMObjArrayProcessor::process_array_slice(objArrayOop obj, HeapWord* start_from, size_t remaining) {
  44   size_t words_to_scan = MIN2(remaining, ObjArrayMarkingStride);
  45 
  46   if (remaining > ObjArrayMarkingStride) {
  47     push_array_slice(start_from + ObjArrayMarkingStride);
  48   }
  49 
  50   // Then process current area.
  51   MemRegion mr(start_from, words_to_scan);
  52   return _task->scan_objArray(obj, mr);
  53 }
  54 
  55 size_t G1CMObjArrayProcessor::process_obj(oop obj) {
  56   assert(should_be_sliced(obj), "Must be an array object %d and large " SIZE_FORMAT, obj->is_objArray(), (size_t)obj->size());
  57 
  58   return process_array_slice(objArrayOop(obj), (HeapWord*)obj, (size_t)objArrayOop(obj)->size());
  59 }
  60 
  61 size_t G1CMObjArrayProcessor::process_slice(oop obj) {
  62   HeapWord* const decoded_address = decode_array_slice(obj);
  63 
  64   // Find the start address of the objArrayOop.
  65   // Shortcut the BOT access if the given address is from a humongous object. The BOT
  66   // slide is fast enough for "smaller" objects in non-humongous regions, but is slower
  67   // than directly using heap region table.
  68   G1CollectedHeap* g1h = G1CollectedHeap::heap();
  69   HeapRegion* r = g1h->heap_region_containing(decoded_address);
  70 
  71   HeapWord* const start_address = r->is_humongous() ?
  72                                   r->humongous_start_region()->bottom() :
  73                                   g1h->block_start(decoded_address);
  74 
  75   assert(oop(start_address)->is_objArray(), "Address " PTR_FORMAT " does not refer to an object array ", p2i(start_address));
  76   assert(start_address < decoded_address,
  77          "Object start address " PTR_FORMAT " must be smaller than decoded address " PTR_FORMAT,
  78          p2i(start_address),
  79          p2i(decoded_address));
  80 
  81   objArrayOop objArray = objArrayOop(start_address);
  82 
  83   size_t already_scanned = decoded_address - start_address;
  84   size_t remaining = objArray->size() - already_scanned;
  85 
  86   return process_array_slice(objArray, decoded_address, remaining);
  87 }