1 /*
   2  * Copyright (c) 2015, 2019, 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 #include "precompiled.hpp"
  25 #include "gc/z/zForwarding.inline.hpp"
  26 #include "gc/z/zPage.inline.hpp"
  27 #include "gc/z/zUtils.inline.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "utilities/debug.hpp"
  30 
  31 ZForwarding* ZForwarding::create(ZPage* page) {
  32   // Allocate table for linear probing. The size of the table must be
  33   // a power of two to allow for quick and inexpensive indexing/masking.
  34   // The table is sized to have a load factor of 50%, i.e. sized to have
  35   // double the number of entries actually inserted.
  36   assert(page->live_objects() > 0, "Invalid value");
  37   const uint32_t nentries = ZUtils::round_up_power_of_2(page->live_objects() * 2);
  38   return ::new (AttachedArray::alloc(nentries)) ZForwarding(page, nentries);
  39 }
  40 
  41 void ZForwarding::destroy(ZForwarding* forwarding) {
  42   AttachedArray::free(forwarding);
  43 }
  44 
  45 ZForwarding::ZForwarding(ZPage* page, uint32_t nentries) :
  46     _virtual(page->virtual_memory()),
  47     _object_alignment_shift(page->object_alignment_shift()),
  48     _entries(nentries),
  49     _page(page),
  50     _refcount(1),
  51     _pinned(false) {}
  52 
  53 void ZForwarding::verify() const {
  54   guarantee(_refcount > 0, "Invalid refcount");
  55   guarantee(_page != NULL, "Invalid page");
  56 
  57   uint32_t live_objects = 0;
  58 
  59   for (ZForwardingCursor i = 0; i < _entries.length(); i++) {
  60     const ZForwardingEntry entry = at(&i);
  61     if (!entry.populated()) {
  62       // Skip empty entries
  63       continue;
  64     }
  65 
  66     // Check from index
  67     guarantee(entry.from_index() < _page->object_max_count(), "Invalid from index");
  68 
  69     // Check for duplicates
  70     for (ZForwardingCursor j = i + 1; j < _entries.length(); j++) {
  71       const ZForwardingEntry other = at(&j);
  72       if (!other.populated()) {
  73         // Skip empty entries
  74         continue;
  75       }
  76 
  77       guarantee(entry.from_index() != other.from_index(), "Duplicate from");
  78       guarantee(entry.to_offset() != other.to_offset(), "Duplicate to");
  79     }
  80 
  81     live_objects++;
  82   }
  83 
  84   // Check number of non-empty entries
  85   guarantee(live_objects == _page->live_objects(), "Invalid number of entries");
  86 }