heapRegionSet.inline.hpp 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_INLINE_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_INLINE_HPP

#include "gc_implementation/g1/heapRegionSet.hpp"

//////////////////// HeapRegionSetBase ////////////////////

inline void HeapRegionSetBase::update_for_addition(HeapRegion* hr) {
  // Assumes the caller has already verified the region.

  _length           += 1;
  if (!hr->isHumongous()) {
    _region_num     += 1;
  } else {
    _region_num     += calculate_region_num(hr);
  }
  _total_used_bytes += hr->used();
}

inline void HeapRegionSetBase::add_internal(HeapRegion* hr) {
T
tonyp 已提交
45 46
  hrs_assert_region_ok(this, hr, NULL);
  assert(hr->next() == NULL, hrs_ext_msg(this, "should not already be linked"));
47 48 49 50 51 52 53

  update_for_addition(hr);
  hr->set_containing_set(this);
}

inline void HeapRegionSetBase::update_for_removal(HeapRegion* hr) {
  // Assumes the caller has already verified the region.
T
tonyp 已提交
54
  assert(_length > 0, hrs_ext_msg(this, "pre-condition"));
55 56 57 58 59 60 61 62 63
  _length -= 1;

  size_t region_num_diff;
  if (!hr->isHumongous()) {
    region_num_diff = 1;
  } else {
    region_num_diff = calculate_region_num(hr);
  }
  assert(region_num_diff <= _region_num,
T
tonyp 已提交
64
         hrs_err_msg("[%s] region's region num: "SIZE_FORMAT" "
65 66 67 68 69 70
                     "should be <= region num: "SIZE_FORMAT,
                     name(), region_num_diff, _region_num));
  _region_num -= region_num_diff;

  size_t used_bytes = hr->used();
  assert(used_bytes <= _total_used_bytes,
T
tonyp 已提交
71
         hrs_err_msg("[%s] region's used bytes: "SIZE_FORMAT" "
72 73 74 75 76 77
                     "should be <= used bytes: "SIZE_FORMAT,
                     name(), used_bytes, _total_used_bytes));
  _total_used_bytes -= used_bytes;
}

inline void HeapRegionSetBase::remove_internal(HeapRegion* hr) {
T
tonyp 已提交
78 79
  hrs_assert_region_ok(this, hr, this);
  assert(hr->next() == NULL, hrs_ext_msg(this, "should already be unlinked"));
80 81 82 83 84 85 86 87

  hr->set_containing_set(NULL);
  update_for_removal(hr);
}

//////////////////// HeapRegionSet ////////////////////

inline void HeapRegionSet::add(HeapRegion* hr) {
T
tonyp 已提交
88
  hrs_assert_mt_safety_ok(this);
89 90 91 92 93
  // add_internal() will verify the region.
  add_internal(hr);
}

inline void HeapRegionSet::remove(HeapRegion* hr) {
T
tonyp 已提交
94
  hrs_assert_mt_safety_ok(this);
95 96 97 98 99 100 101 102 103
  // remove_internal() will verify the region.
  remove_internal(hr);
}

inline void HeapRegionSet::remove_with_proxy(HeapRegion* hr,
                                             HeapRegionSet* proxy_set) {
  // No need to fo the MT safety check here given that this method
  // does not update the contents of the set but instead accumulates
  // the changes in proxy_set which is assumed to be thread-local.
T
tonyp 已提交
104 105
  hrs_assert_sets_match(this, proxy_set);
  hrs_assert_region_ok(this, hr, this);
106 107 108 109 110 111 112 113

  hr->set_containing_set(NULL);
  proxy_set->update_for_addition(hr);
}

//////////////////// HeapRegionLinkedList ////////////////////

inline void HeapRegionLinkedList::add_as_tail(HeapRegion* hr) {
T
tonyp 已提交
114
  hrs_assert_mt_safety_ok(this);
115 116
  assert((length() == 0 && _head == NULL && _tail == NULL) ||
         (length() >  0 && _head != NULL && _tail != NULL),
T
tonyp 已提交
117
         hrs_ext_msg(this, "invariant"));
118 119 120 121 122 123 124 125 126 127 128 129 130
  // add_internal() will verify the region.
  add_internal(hr);

  // Now link the region.
  if (_tail != NULL) {
    _tail->set_next(hr);
  } else {
    _head = hr;
  }
  _tail = hr;
}

inline HeapRegion* HeapRegionLinkedList::remove_head() {
T
tonyp 已提交
131 132
  hrs_assert_mt_safety_ok(this);
  assert(!is_empty(), hrs_ext_msg(this, "the list should not be empty"));
133
  assert(length() > 0 && _head != NULL && _tail != NULL,
T
tonyp 已提交
134
         hrs_ext_msg(this, "invariant"));
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

  // We need to unlink it first.
  HeapRegion* hr = _head;
  _head = hr->next();
  if (_head == NULL) {
    _tail = NULL;
  }
  hr->set_next(NULL);

  // remove_internal() will verify the region.
  remove_internal(hr);
  return hr;
}

inline HeapRegion* HeapRegionLinkedList::remove_head_or_null() {
T
tonyp 已提交
150
  hrs_assert_mt_safety_ok(this);
151 152 153 154 155 156 157 158 159

  if (!is_empty()) {
    return remove_head();
  } else {
    return NULL;
  }
}

#endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_INLINE_HPP