1 /*
   2  * Copyright (c) 2017, 2020, 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) {
  38   if (_nest_level < MAX_NEST_LEVEL) {
  39     do_push(ref);
  40     if (!ref->keep_after_pushing()) {
  41       delete ref;
  42     }
  43   } else {
  44     do_pending_ref(ref);
  45     ref->set_next(_pending_refs);
  46     _pending_refs = ref;
  47   }
  48 }
  49 
  50 void MetaspaceClosure::do_push(MetaspaceClosure::Ref* ref) {
  51   if (ref->not_null()) {
  52     bool read_only;
  53     Writability w = ref->writability();
  54     switch (w) {
  55     case _writable:
  56       read_only = false;
  57       break;
  58     case _not_writable:
  59       read_only = true;
  60       break;
  61     default:
  62       assert(w == _default, "must be");
  63       read_only = ref->is_read_only_by_default();
  64     }
  65     if (_nest_level == 0) {
  66       assert(_enclosing_ref == NULL, "must be");
  67     }
  68     _nest_level ++;
  69     if (do_ref(ref, read_only)) { // true means we want to iterate the embedded pointer in <ref>
  70       Ref* saved = _enclosing_ref;
  71       _enclosing_ref = ref;
  72       ref->metaspace_pointers_do(this);
  73       _enclosing_ref = saved;
  74     }
  75     _nest_level --;
  76   }
  77 }
  78 
  79 void MetaspaceClosure::finish() {
  80   assert(_nest_level == 0, "must be");
  81   while (_pending_refs != NULL) {
  82     Ref* ref = _pending_refs;
  83     _pending_refs = _pending_refs->next();
  84     do_push(ref);
  85     if (!ref->keep_after_pushing()) {
  86       delete ref;
  87     }
  88   }
  89 }
  90 
  91 MetaspaceClosure::~MetaspaceClosure() {
  92   assert(_pending_refs == NULL,
  93          "you must explicitly call MetaspaceClosure::finish() to process all refs!");
  94 }
  95 
  96 bool UniqueMetaspaceClosure::do_ref(MetaspaceClosure::Ref* ref, bool read_only) {
  97   bool* found = _has_been_visited.lookup(ref->obj());
  98   if (found != NULL) {
  99     assert(*found == read_only, "must be");
 100     return false; // Already visited: no need to iterate embedded pointers.
 101   } else {
 102     _has_been_visited.add(ref->obj(), read_only);
 103     if (_has_been_visited.maybe_grow(MAX_TABLE_SIZE)) {
 104       log_info(cds, hashtables)("Expanded _has_been_visited table to %d", _has_been_visited.table_size());
 105     }
 106     return do_unique_ref(ref, read_only);
 107   }
 108 }