1 /*
   2  * Copyright (c) 1997, 2018, 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 "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "oops/instanceRefKlass.inline.hpp"
  29 #include "oops/oop.inline.hpp"
  30 
  31 void InstanceRefKlass::update_nonstatic_oop_maps(Klass* k) {
  32   // Clear the nonstatic oop-map entries corresponding to referent
  33   // and discovered fields.  They are treated specially by the
  34   // garbage collector.
  35   InstanceKlass* ik = InstanceKlass::cast(k);
  36 
  37   // Check that we have the right class
  38   debug_only(static bool first_time = true);
  39   assert(k == SystemDictionary::Reference_klass() && first_time,
  40          "Invalid update of maps");
  41   debug_only(first_time = false);
  42   assert(ik->nonstatic_oop_map_count() == 1, "just checking");
  43 
  44   OopMapBlock* map = ik->start_of_nonstatic_oop_maps();
  45 
  46 #ifdef ASSERT
  47   // Verify fields are in the expected places.
  48   int referent_offset = java_lang_ref_Reference::referent_offset;
  49   int queue_offset = java_lang_ref_Reference::queue_offset;
  50   int next_offset = java_lang_ref_Reference::next_offset;
  51   int discovered_offset = java_lang_ref_Reference::discovered_offset;
  52   assert(referent_offset < queue_offset, "just checking");
  53   assert(queue_offset < next_offset, "just checking");
  54   assert(next_offset < discovered_offset, "just checking");
  55   const unsigned int count =
  56     1 + ((discovered_offset - referent_offset) / heapOopSize);
  57   assert(count == 4, "just checking");
  58 #endif // ASSERT
  59 
  60   // Updated map starts at "queue", covers "queue" and "next".
  61   const int new_offset = java_lang_ref_Reference::queue_offset;
  62   const unsigned int new_count = 2; // queue and next
  63 
  64   // Verify existing map is as expected, and update if needed.
  65   if (UseSharedSpaces) {
  66     assert(map->offset() == new_offset, "just checking");
  67     assert(map->count() == new_count, "just checking");
  68   } else {
  69     assert(map->offset() == referent_offset, "just checking");
  70     assert(map->count() == count, "just checking");
  71     map->set_offset(new_offset);
  72     map->set_count(new_count);
  73   }
  74 }
  75 
  76 
  77 // Verification
  78 
  79 void InstanceRefKlass::oop_verify_on(oop obj, outputStream* st) {
  80   InstanceKlass::oop_verify_on(obj, st);
  81   // Verify referent field
  82   oop referent = java_lang_ref_Reference::referent(obj);
  83   if (referent != NULL) {
  84     guarantee(oopDesc::is_oop(referent), "referent field heap failed");
  85   }
  86   // Additional verification for next field, which must be a Reference or null
  87   oop next = java_lang_ref_Reference::next(obj);
  88   if (next != NULL) {
  89     guarantee(oopDesc::is_oop(next), "next field should be an oop");
  90     guarantee(next->is_instance(), "next field should be an instance");
  91     guarantee(InstanceKlass::cast(next->klass())->is_reference_instance_klass(), "next field verify failed");
  92   }
  93 }