1 /*
   2  * Copyright (c) 2015, 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 #include "precompiled.hpp"
  25 #include "gc/z/zForwardingTable.inline.hpp"
  26 #include "gc/z/zUtils.inline.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "utilities/debug.hpp"
  29 
  30 void ZForwardingTable::setup(size_t live_objects) {
  31   assert(is_null(), "Should be empty");
  32   assert(live_objects > 0, "Invalid size");
  33 
  34   // Allocate table for linear probing. The size of the table must be
  35   // a power of two to allow for quick and inexpensive indexing/masking.
  36   // The table is sized to have a load factor of 50%, i.e. sized to have
  37   // double the number of entries actuallly inserted.
  38   _size = ZUtils::round_up_power_of_2(live_objects * 2);
  39   _table = MallocArrayAllocator<ZForwardingTableEntry>::allocate(_size, mtGC);
  40 
  41   // Clear table
  42   memset(_table, ZForwardingTableEntry::empty(), _size * sizeof(ZForwardingTableEntry));
  43 }
  44 
  45 void ZForwardingTable::reset() {
  46   // Free table
  47   MallocArrayAllocator<ZForwardingTableEntry>::free(_table);
  48   _table = NULL;
  49   _size = 0;
  50 }
  51 
  52 void ZForwardingTable::verify(size_t object_max_count, size_t live_objects) const {
  53   size_t count = 0;
  54 
  55   for (size_t i = 0; i < _size; i++) {
  56     const ZForwardingTableEntry entry = _table[i];
  57     if (entry.is_empty()) {
  58       // Skip empty entries
  59       continue;
  60     }
  61 
  62     // Check from index
  63     guarantee(entry.from_index() < object_max_count, "Invalid from index");
  64 
  65     // Check for duplicates
  66     for (size_t j = i + 1; j < _size; j++) {
  67       const ZForwardingTableEntry other = _table[j];
  68       guarantee(entry.from_index() != other.from_index(), "Duplicate from");
  69       guarantee(entry.to_offset() != other.to_offset(), "Duplicate to");
  70     }
  71 
  72     count++;
  73   }
  74 
  75   // Check number of non-null entries
  76   guarantee(live_objects == count, "Count mismatch");
  77 }