1 /*
   2  * Copyright (c) 2001, 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 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1Arguments.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1ConcurrentRefine.hpp"
  29 #include "gc/g1/g1NUMAStats.hpp"
  30 #include "gc/g1/heapRegion.hpp"
  31 #include "gc/g1/heapRegionManager.inline.hpp"
  32 #include "gc/g1/heapRegionSet.inline.hpp"
  33 #include "gc/g1/heterogeneousHeapRegionManager.hpp"
  34 #include "logging/logStream.hpp"
  35 #include "memory/allocation.hpp"
  36 #include "utilities/bitMap.inline.hpp"
  37 
  38 class MasterFreeRegionListChecker : public HeapRegionSetChecker {
  39 public:
  40   void check_mt_safety() {
  41     // Master Free List MT safety protocol:
  42     // (a) If we're at a safepoint, operations on the master free list
  43     // should be invoked by either the VM thread (which will serialize
  44     // them) or by the GC workers while holding the
  45     // FreeList_lock.
  46     // (b) If we're not at a safepoint, operations on the master free
  47     // list should be invoked while holding the Heap_lock.
  48 
  49     if (SafepointSynchronize::is_at_safepoint()) {
  50       guarantee(Thread::current()->is_VM_thread() ||
  51                 FreeList_lock->owned_by_self(), "master free list MT safety protocol at a safepoint");
  52     } else {
  53       guarantee(Heap_lock->owned_by_self(), "master free list MT safety protocol outside a safepoint");
  54     }
  55   }
  56   bool is_correct_type(HeapRegion* hr) { return hr->is_free(); }
  57   const char* get_description() { return "Free Regions"; }
  58 };
  59 
  60 HeapRegionManager::HeapRegionManager() :
  61   _bot_mapper(NULL),
  62   _cardtable_mapper(NULL),
  63   _card_counts_mapper(NULL),
  64   _available_map(mtGC),
  65   _num_committed(0),
  66   _allocated_heapregions_length(0),
  67   _regions(), _heap_mapper(NULL),
  68   _prev_bitmap_mapper(NULL),
  69   _next_bitmap_mapper(NULL),
  70   _free_list("Free list", new MasterFreeRegionListChecker())
  71 { }
  72 
  73 HeapRegionManager* HeapRegionManager::create_manager(G1CollectedHeap* heap) {
  74   if (G1Arguments::is_heterogeneous_heap()) {
  75     return new HeterogeneousHeapRegionManager((uint)(G1Arguments::heap_max_size_bytes() / HeapRegion::GrainBytes) /*heap size as num of regions*/);
  76   }
  77   return new HeapRegionManager();
  78 }
  79 
  80 void HeapRegionManager::initialize(G1RegionToSpaceMapper* heap_storage,
  81                                G1RegionToSpaceMapper* prev_bitmap,
  82                                G1RegionToSpaceMapper* next_bitmap,
  83                                G1RegionToSpaceMapper* bot,
  84                                G1RegionToSpaceMapper* cardtable,
  85                                G1RegionToSpaceMapper* card_counts) {
  86   _allocated_heapregions_length = 0;
  87 
  88   _heap_mapper = heap_storage;
  89 
  90   _prev_bitmap_mapper = prev_bitmap;
  91   _next_bitmap_mapper = next_bitmap;
  92 
  93   _bot_mapper = bot;
  94   _cardtable_mapper = cardtable;
  95 
  96   _card_counts_mapper = card_counts;
  97 
  98   MemRegion reserved = heap_storage->reserved();
  99   _regions.initialize(reserved.start(), reserved.end(), HeapRegion::GrainBytes);
 100 
 101   _available_map.initialize(_regions.length());
 102 }
 103 
 104 bool HeapRegionManager::is_available(uint region) const {
 105   return _available_map.at(region);
 106 }
 107 
 108 HeapRegion* HeapRegionManager::allocate_free_region(HeapRegionType type, uint requested_node_index) {
 109   HeapRegion* hr = NULL;
 110   bool from_head = !type.is_young();
 111   G1NUMA* numa = G1NUMA::numa();
 112 
 113   if (requested_node_index != G1NUMA::AnyNodeIndex && numa->is_enabled()) {
 114     // Try to allocate with requested node index.
 115     hr = _free_list.remove_region_with_node_index(from_head, requested_node_index);
 116   }
 117 
 118   if (hr == NULL) {
 119     // If there's a single active node or we did not get a region from our requested node,
 120     // try without requested node index.
 121     hr = _free_list.remove_region(from_head);
 122   }
 123 
 124   if (hr != NULL) {
 125     assert(hr->next() == NULL, "Single region should not have next");
 126     assert(is_available(hr->hrm_index()), "Must be committed");
 127 
 128     if (numa->is_enabled() && hr->node_index() < numa->num_active_nodes()) {
 129       numa->update_statistics(G1NUMAStats::NewRegionAlloc, requested_node_index, hr->node_index());
 130     }
 131   }
 132 
 133   return hr;
 134 }
 135 
 136 #ifdef ASSERT
 137 bool HeapRegionManager::is_free(HeapRegion* hr) const {
 138   return _free_list.contains(hr);
 139 }
 140 #endif
 141 
 142 HeapRegion* HeapRegionManager::new_heap_region(uint hrm_index) {
 143   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 144   HeapWord* bottom = g1h->bottom_addr_for_region(hrm_index);
 145   MemRegion mr(bottom, bottom + HeapRegion::GrainWords);
 146   assert(reserved().contains(mr), "invariant");
 147   return g1h->new_heap_region(hrm_index, mr);
 148 }
 149 
 150 void HeapRegionManager::commit_regions(uint index, size_t num_regions, WorkGang* pretouch_gang) {
 151   guarantee(num_regions > 0, "Must commit more than zero regions");
 152   guarantee(_num_committed + num_regions <= max_length(), "Cannot commit more than the maximum amount of regions");
 153 
 154   _num_committed += (uint)num_regions;
 155 
 156   _heap_mapper->commit_regions(index, num_regions, pretouch_gang);
 157 
 158   // Also commit auxiliary data
 159   _prev_bitmap_mapper->commit_regions(index, num_regions, pretouch_gang);
 160   _next_bitmap_mapper->commit_regions(index, num_regions, pretouch_gang);
 161 
 162   _bot_mapper->commit_regions(index, num_regions, pretouch_gang);
 163   _cardtable_mapper->commit_regions(index, num_regions, pretouch_gang);
 164 
 165   _card_counts_mapper->commit_regions(index, num_regions, pretouch_gang);
 166 }
 167 
 168 void HeapRegionManager::uncommit_regions(uint start, size_t num_regions) {
 169   guarantee(num_regions >= 1, "Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start);
 170   guarantee(_num_committed >= num_regions, "pre-condition");
 171 
 172   // Reset node index to distinguish with committed regions.
 173   for (uint i = start; i < start + num_regions; i++) {
 174     at(i)->set_node_index(G1NUMA::UnknownNodeIndex);
 175   }
 176 
 177   // Print before uncommitting.
 178   if (G1CollectedHeap::heap()->hr_printer()->is_active()) {
 179     for (uint i = start; i < start + num_regions; i++) {
 180       HeapRegion* hr = at(i);
 181       G1CollectedHeap::heap()->hr_printer()->uncommit(hr);
 182     }
 183   }
 184 
 185   _num_committed -= (uint)num_regions;
 186 
 187   _available_map.par_clear_range(start, start + num_regions, BitMap::unknown_range);
 188   _heap_mapper->uncommit_regions(start, num_regions);
 189 
 190   // Also uncommit auxiliary data
 191   _prev_bitmap_mapper->uncommit_regions(start, num_regions);
 192   _next_bitmap_mapper->uncommit_regions(start, num_regions);
 193 
 194   _bot_mapper->uncommit_regions(start, num_regions);
 195   _cardtable_mapper->uncommit_regions(start, num_regions);
 196 
 197   _card_counts_mapper->uncommit_regions(start, num_regions);
 198 }
 199 
 200 void HeapRegionManager::make_regions_available(uint start, uint num_regions, WorkGang* pretouch_gang) {
 201   guarantee(num_regions > 0, "No point in calling this for zero regions");
 202   commit_regions(start, num_regions, pretouch_gang);
 203   for (uint i = start; i < start + num_regions; i++) {
 204     if (_regions.get_by_index(i) == NULL) {
 205       HeapRegion* new_hr = new_heap_region(i);
 206       OrderAccess::storestore();
 207       _regions.set_by_index(i, new_hr);
 208       _allocated_heapregions_length = MAX2(_allocated_heapregions_length, i + 1);
 209     }
 210   }
 211 
 212   _available_map.par_set_range(start, start + num_regions, BitMap::unknown_range);
 213 
 214   for (uint i = start; i < start + num_regions; i++) {
 215     assert(is_available(i), "Just made region %u available but is apparently not.", i);
 216     HeapRegion* hr = at(i);
 217     if (G1CollectedHeap::heap()->hr_printer()->is_active()) {
 218       G1CollectedHeap::heap()->hr_printer()->commit(hr);
 219     }
 220     HeapWord* bottom = G1CollectedHeap::heap()->bottom_addr_for_region(i);
 221     MemRegion mr(bottom, bottom + HeapRegion::GrainWords);
 222 
 223     hr->initialize(mr);
 224     hr->set_node_index(G1NUMA::numa()->index_for_region(hr));
 225     insert_into_free_list(at(i));
 226   }
 227 }
 228 
 229 MemoryUsage HeapRegionManager::get_auxiliary_data_memory_usage() const {
 230   size_t used_sz =
 231     _prev_bitmap_mapper->committed_size() +
 232     _next_bitmap_mapper->committed_size() +
 233     _bot_mapper->committed_size() +
 234     _cardtable_mapper->committed_size() +
 235     _card_counts_mapper->committed_size();
 236 
 237   size_t committed_sz =
 238     _prev_bitmap_mapper->reserved_size() +
 239     _next_bitmap_mapper->reserved_size() +
 240     _bot_mapper->reserved_size() +
 241     _cardtable_mapper->reserved_size() +
 242     _card_counts_mapper->reserved_size();
 243 
 244   return MemoryUsage(0, used_sz, committed_sz, committed_sz);
 245 }
 246 
 247 uint HeapRegionManager::expand_by(uint num_regions, WorkGang* pretouch_workers) {
 248   return expand_at(0, num_regions, pretouch_workers);
 249 }
 250 
 251 uint HeapRegionManager::expand_at(uint start, uint num_regions, WorkGang* pretouch_workers) {
 252   if (num_regions == 0) {
 253     return 0;
 254   }
 255 
 256   uint cur = start;
 257   uint idx_last_found = 0;
 258   uint num_last_found = 0;
 259 
 260   uint expanded = 0;
 261 
 262   while (expanded < num_regions &&
 263          (num_last_found = find_unavailable_from_idx(cur, &idx_last_found)) > 0) {
 264     uint to_expand = MIN2(num_regions - expanded, num_last_found);
 265     make_regions_available(idx_last_found, to_expand, pretouch_workers);
 266     expanded += to_expand;
 267     cur = idx_last_found + num_last_found + 1;
 268   }
 269 
 270   verify_optional();
 271   return expanded;
 272 }
 273 
 274 uint HeapRegionManager::expand_on_preferred_node(uint preferred_index) {
 275   uint expand_candidate = UINT_MAX;
 276   for (uint i = 0; i < max_length(); i++) {
 277     if (is_available(i)) {
 278       // Already in use continue
 279       continue;
 280     }
 281     // Always save the candidate so we can expand later on.
 282     expand_candidate = i;
 283     if (is_on_preferred_index(expand_candidate, preferred_index)) {
 284       // We have found a candidate on the preffered node, break.
 285       break;
 286     }
 287   }
 288 
 289   if (expand_candidate == UINT_MAX) {
 290      // No regions left, expand failed.
 291     return 0;
 292   }
 293 
 294   make_regions_available(expand_candidate, 1, NULL);
 295   return 1;
 296 }
 297 
 298 bool HeapRegionManager::is_on_preferred_index(uint region_index, uint preferred_node_index) {
 299   uint region_node_index = G1NUMA::numa()->preferred_node_index_for_index(region_index);
 300   return region_node_index == preferred_node_index;
 301 }
 302 
 303 uint HeapRegionManager::find_contiguous(size_t num, bool empty_only) {
 304   uint found = 0;
 305   size_t length_found = 0;
 306   uint cur = 0;
 307 
 308   while (length_found < num && cur < max_length()) {
 309     HeapRegion* hr = _regions.get_by_index(cur);
 310     if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) {
 311       // This region is a potential candidate for allocation into.
 312       length_found++;
 313     } else {
 314       // This region is not a candidate. The next region is the next possible one.
 315       found = cur + 1;
 316       length_found = 0;
 317     }
 318     cur++;
 319   }
 320 
 321   if (length_found == num) {
 322     for (uint i = found; i < (found + num); i++) {
 323       HeapRegion* hr = _regions.get_by_index(i);
 324       // sanity check
 325       guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()),
 326                 "Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT
 327                 " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr));
 328     }
 329     return found;
 330   } else {
 331     return G1_NO_HRM_INDEX;
 332   }
 333 }
 334 
 335 HeapRegion* HeapRegionManager::next_region_in_heap(const HeapRegion* r) const {
 336   guarantee(r != NULL, "Start region must be a valid region");
 337   guarantee(is_available(r->hrm_index()), "Trying to iterate starting from region %u which is not in the heap", r->hrm_index());
 338   for (uint i = r->hrm_index() + 1; i < _allocated_heapregions_length; i++) {
 339     HeapRegion* hr = _regions.get_by_index(i);
 340     if (is_available(i)) {
 341       return hr;
 342     }
 343   }
 344   return NULL;
 345 }
 346 
 347 void HeapRegionManager::iterate(HeapRegionClosure* blk) const {
 348   uint len = max_length();
 349 
 350   for (uint i = 0; i < len; i++) {
 351     if (!is_available(i)) {
 352       continue;
 353     }
 354     guarantee(at(i) != NULL, "Tried to access region %u that has a NULL HeapRegion*", i);
 355     bool res = blk->do_heap_region(at(i));
 356     if (res) {
 357       blk->set_incomplete();
 358       return;
 359     }
 360   }
 361 }
 362 
 363 uint HeapRegionManager::find_unavailable_from_idx(uint start_idx, uint* res_idx) const {
 364   guarantee(res_idx != NULL, "checking");
 365   guarantee(start_idx <= (max_length() + 1), "checking");
 366 
 367   uint num_regions = 0;
 368 
 369   uint cur = start_idx;
 370   while (cur < max_length() && is_available(cur)) {
 371     cur++;
 372   }
 373   if (cur == max_length()) {
 374     return num_regions;
 375   }
 376   *res_idx = cur;
 377   while (cur < max_length() && !is_available(cur)) {
 378     cur++;
 379   }
 380   num_regions = cur - *res_idx;
 381 #ifdef ASSERT
 382   for (uint i = *res_idx; i < (*res_idx + num_regions); i++) {
 383     assert(!is_available(i), "just checking");
 384   }
 385   assert(cur == max_length() || num_regions == 0 || is_available(cur),
 386          "The region at the current position %u must be available or at the end of the heap.", cur);
 387 #endif
 388   return num_regions;
 389 }
 390 
 391 uint HeapRegionManager::find_highest_free(bool* expanded) {
 392   // Loop downwards from the highest region index, looking for an
 393   // entry which is either free or not yet committed.  If not yet
 394   // committed, expand_at that index.
 395   uint curr = max_length() - 1;
 396   while (true) {
 397     HeapRegion *hr = _regions.get_by_index(curr);
 398     if (hr == NULL || !is_available(curr)) {
 399       uint res = expand_at(curr, 1, NULL);
 400       if (res == 1) {
 401         *expanded = true;
 402         return curr;
 403       }
 404     } else {
 405       if (hr->is_free()) {
 406         *expanded = false;
 407         return curr;
 408       }
 409     }
 410     if (curr == 0) {
 411       return G1_NO_HRM_INDEX;
 412     }
 413     curr--;
 414   }
 415 }
 416 
 417 bool HeapRegionManager::allocate_containing_regions(MemRegion range, size_t* commit_count, WorkGang* pretouch_workers) {
 418   size_t commits = 0;
 419   uint start_index = (uint)_regions.get_index_by_address(range.start());
 420   uint last_index = (uint)_regions.get_index_by_address(range.last());
 421 
 422   // Ensure that each G1 region in the range is free, returning false if not.
 423   // Commit those that are not yet available, and keep count.
 424   for (uint curr_index = start_index; curr_index <= last_index; curr_index++) {
 425     if (!is_available(curr_index)) {
 426       commits++;
 427       expand_at(curr_index, 1, pretouch_workers);
 428     }
 429     HeapRegion* curr_region  = _regions.get_by_index(curr_index);
 430     if (!curr_region->is_free()) {
 431       return false;
 432     }
 433   }
 434 
 435   allocate_free_regions_starting_at(start_index, (last_index - start_index) + 1);
 436   *commit_count = commits;
 437   return true;
 438 }
 439 
 440 void HeapRegionManager::par_iterate(HeapRegionClosure* blk, HeapRegionClaimer* hrclaimer, const uint start_index) const {
 441   // Every worker will actually look at all regions, skipping over regions that
 442   // are currently not committed.
 443   // This also (potentially) iterates over regions newly allocated during GC. This
 444   // is no problem except for some extra work.
 445   const uint n_regions = hrclaimer->n_regions();
 446   for (uint count = 0; count < n_regions; count++) {
 447     const uint index = (start_index + count) % n_regions;
 448     assert(index < n_regions, "sanity");
 449     // Skip over unavailable regions
 450     if (!is_available(index)) {
 451       continue;
 452     }
 453     HeapRegion* r = _regions.get_by_index(index);
 454     // We'll ignore regions already claimed.
 455     // However, if the iteration is specified as concurrent, the values for
 456     // is_starts_humongous and is_continues_humongous can not be trusted,
 457     // and we should just blindly iterate over regions regardless of their
 458     // humongous status.
 459     if (hrclaimer->is_region_claimed(index)) {
 460       continue;
 461     }
 462     // OK, try to claim it
 463     if (!hrclaimer->claim_region(index)) {
 464       continue;
 465     }
 466     bool res = blk->do_heap_region(r);
 467     if (res) {
 468       return;
 469     }
 470   }
 471 }
 472 
 473 uint HeapRegionManager::shrink_by(uint num_regions_to_remove) {
 474   assert(length() > 0, "the region sequence should not be empty");
 475   assert(length() <= _allocated_heapregions_length, "invariant");
 476   assert(_allocated_heapregions_length > 0, "we should have at least one region committed");
 477   assert(num_regions_to_remove < length(), "We should never remove all regions");
 478 
 479   if (num_regions_to_remove == 0) {
 480     return 0;
 481   }
 482 
 483   uint removed = 0;
 484   uint cur = _allocated_heapregions_length - 1;
 485   uint idx_last_found = 0;
 486   uint num_last_found = 0;
 487 
 488   while ((removed < num_regions_to_remove) &&
 489       (num_last_found = find_empty_from_idx_reverse(cur, &idx_last_found)) > 0) {
 490     uint to_remove = MIN2(num_regions_to_remove - removed, num_last_found);
 491 
 492     shrink_at(idx_last_found + num_last_found - to_remove, to_remove);
 493 
 494     cur = idx_last_found;
 495     removed += to_remove;
 496   }
 497 
 498   verify_optional();
 499 
 500   return removed;
 501 }
 502 
 503 void HeapRegionManager::shrink_at(uint index, size_t num_regions) {
 504 #ifdef ASSERT
 505   for (uint i = index; i < (index + num_regions); i++) {
 506     assert(is_available(i), "Expected available region at index %u", i);
 507     assert(at(i)->is_empty(), "Expected empty region at index %u", i);
 508     assert(at(i)->is_free(), "Expected free region at index %u", i);
 509   }
 510 #endif
 511   uncommit_regions(index, num_regions);
 512 }
 513 
 514 uint HeapRegionManager::find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const {
 515   guarantee(start_idx < _allocated_heapregions_length, "checking");
 516   guarantee(res_idx != NULL, "checking");
 517 
 518   uint num_regions_found = 0;
 519 
 520   jlong cur = start_idx;
 521   while (cur != -1 && !(is_available(cur) && at(cur)->is_empty())) {
 522     cur--;
 523   }
 524   if (cur == -1) {
 525     return num_regions_found;
 526   }
 527   jlong old_cur = cur;
 528   // cur indexes the first empty region
 529   while (cur != -1 && is_available(cur) && at(cur)->is_empty()) {
 530     cur--;
 531   }
 532   *res_idx = cur + 1;
 533   num_regions_found = old_cur - cur;
 534 
 535 #ifdef ASSERT
 536   for (uint i = *res_idx; i < (*res_idx + num_regions_found); i++) {
 537     assert(at(i)->is_empty(), "just checking");
 538   }
 539 #endif
 540   return num_regions_found;
 541 }
 542 
 543 void HeapRegionManager::verify() {
 544   guarantee(length() <= _allocated_heapregions_length,
 545             "invariant: _length: %u _allocated_length: %u",
 546             length(), _allocated_heapregions_length);
 547   guarantee(_allocated_heapregions_length <= max_length(),
 548             "invariant: _allocated_length: %u _max_length: %u",
 549             _allocated_heapregions_length, max_length());
 550 
 551   bool prev_committed = true;
 552   uint num_committed = 0;
 553   HeapWord* prev_end = heap_bottom();
 554   for (uint i = 0; i < _allocated_heapregions_length; i++) {
 555     if (!is_available(i)) {
 556       prev_committed = false;
 557       continue;
 558     }
 559     num_committed++;
 560     HeapRegion* hr = _regions.get_by_index(i);
 561     guarantee(hr != NULL, "invariant: i: %u", i);
 562     guarantee(!prev_committed || hr->bottom() == prev_end,
 563               "invariant i: %u " HR_FORMAT " prev_end: " PTR_FORMAT,
 564               i, HR_FORMAT_PARAMS(hr), p2i(prev_end));
 565     guarantee(hr->hrm_index() == i,
 566               "invariant: i: %u hrm_index(): %u", i, hr->hrm_index());
 567     // Asserts will fire if i is >= _length
 568     HeapWord* addr = hr->bottom();
 569     guarantee(addr_to_region(addr) == hr, "sanity");
 570     // We cannot check whether the region is part of a particular set: at the time
 571     // this method may be called, we have only completed allocation of the regions,
 572     // but not put into a region set.
 573     prev_committed = true;
 574     prev_end = hr->end();
 575   }
 576   for (uint i = _allocated_heapregions_length; i < max_length(); i++) {
 577     guarantee(_regions.get_by_index(i) == NULL, "invariant i: %u", i);
 578   }
 579 
 580   guarantee(num_committed == _num_committed, "Found %u committed regions, but should be %u", num_committed, _num_committed);
 581   _free_list.verify();
 582 }
 583 
 584 #ifndef PRODUCT
 585 void HeapRegionManager::verify_optional() {
 586   verify();
 587 }
 588 #endif // PRODUCT
 589 
 590 HeapRegionClaimer::HeapRegionClaimer(uint n_workers) :
 591     _n_workers(n_workers), _n_regions(G1CollectedHeap::heap()->_hrm->_allocated_heapregions_length), _claims(NULL) {
 592   assert(n_workers > 0, "Need at least one worker.");
 593   uint* new_claims = NEW_C_HEAP_ARRAY(uint, _n_regions, mtGC);
 594   memset(new_claims, Unclaimed, sizeof(*_claims) * _n_regions);
 595   _claims = new_claims;
 596 }
 597 
 598 HeapRegionClaimer::~HeapRegionClaimer() {
 599   FREE_C_HEAP_ARRAY(uint, _claims);
 600 }
 601 
 602 uint HeapRegionClaimer::offset_for_worker(uint worker_id) const {
 603   assert(worker_id < _n_workers, "Invalid worker_id.");
 604   return _n_regions * worker_id / _n_workers;
 605 }
 606 
 607 bool HeapRegionClaimer::is_region_claimed(uint region_index) const {
 608   assert(region_index < _n_regions, "Invalid index.");
 609   return _claims[region_index] == Claimed;
 610 }
 611 
 612 bool HeapRegionClaimer::claim_region(uint region_index) {
 613   assert(region_index < _n_regions, "Invalid index.");
 614   uint old_val = Atomic::cmpxchg(Claimed, &_claims[region_index], Unclaimed);
 615   return old_val == Unclaimed;
 616 }