1 /*
   2  * Copyright (c) 2017, 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 "memory/metaspaceClosure.hpp"
  27 
  28 // Update the reference to point to new_loc.
  29 void MetaspaceClosure::Ref::update(address new_loc) const {
  30   log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT,
  31                  p2i(mpp()), p2i(obj()), p2i(new_loc));
  32   uintx p = (uintx)new_loc;
  33   p |= flag_bits(); // Make sure the flag bits are copied to the new pointer.
  34   *(address*)mpp() = (address)p;
  35 }
  36 
  37 void MetaspaceClosure::push_impl(MetaspaceClosure::Ref* ref, Writability w) {
  38   if (ref->not_null()) {
  39     bool read_only;
  40     switch (w) {
  41     case _writable:
  42       read_only = false;
  43       break;
  44     case _not_writable:
  45       read_only = true;
  46       break;
  47     default:
  48       assert(w == _default, "must be");
  49       read_only = ref->is_read_only_by_default();
  50     }
  51     if (do_ref(ref, read_only)) { // true means we want to iterate the embedded pointer in <ref>
  52       ref->metaspace_pointers_do(this);
  53     }
  54   }
  55 }
  56 
  57 bool UniqueMetaspaceClosure::do_ref(MetaspaceClosure::Ref* ref, bool read_only) {
  58   bool* found = _has_been_visited.get(ref->obj());
  59   if (found != NULL) {
  60     assert(*found == read_only, "must be");
  61     return false; // Already visited: no need to iterate embedded pointers.
  62   } else {
  63     bool isnew = _has_been_visited.put(ref->obj(), read_only);
  64     assert(isnew, "sanity");
  65     do_unique_ref(ref, read_only);
  66     return true;  // Saw this for the first time: iterate the embedded pointers.
  67   }
  68 }