< prev index next >

src/hotspot/share/gc/g1/g1Allocator.cpp

Print this page

  1 /*
  2  * Copyright (c) 2014, 2025, 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  *

229          word_size, temp, p2i(result));
230   return result;
231 }
232 
233 HeapWord* G1Allocator::par_allocate_during_gc(G1HeapRegionAttr dest,
234                                               uint node_index,
235                                               size_t min_word_size,
236                                               size_t desired_word_size,
237                                               size_t* actual_word_size) {
238   switch (dest.type()) {
239     case G1HeapRegionAttr::Young:
240       return survivor_attempt_allocation(node_index, min_word_size, desired_word_size, actual_word_size);
241     case G1HeapRegionAttr::Old:
242       return old_attempt_allocation(min_word_size, desired_word_size, actual_word_size);
243     default:
244       ShouldNotReachHere();
245       return nullptr; // Keep some compilers happy
246   }
247 }
248 











249 HeapWord* G1Allocator::survivor_attempt_allocation(uint node_index,
250                                                    size_t min_word_size,
251                                                    size_t desired_word_size,
252                                                    size_t* actual_word_size) {
253   assert(!_g1h->is_humongous(desired_word_size),
254          "we should not be seeing humongous-size allocations in this path");
255 
256   HeapWord* result = survivor_gc_alloc_region(node_index)->attempt_allocation(min_word_size,
257                                                                               desired_word_size,
258                                                                               actual_word_size);
259   if (result == nullptr && !survivor_is_full()) {
260     MutexLocker x(G1FreeList_lock, Mutex::_no_safepoint_check_flag);
261     // Multiple threads may have queued at the FreeList_lock above after checking whether there
262     // actually is still memory available. Redo the check under the lock to avoid unnecessary work;
263     // the memory may have been used up as the threads waited to acquire the lock.
264     if (!survivor_is_full()) {
265       result = survivor_gc_alloc_region(node_index)->attempt_allocation_locked(min_word_size,
266                                                                                desired_word_size,
267                                                                                actual_word_size);
268       if (result == nullptr) {
269         set_survivor_full();
270       }
271     }
272   }
273   return result;
274 }
275 
276 HeapWord* G1Allocator::old_attempt_allocation(size_t min_word_size,
277                                               size_t desired_word_size,
278                                               size_t* actual_word_size) {
279   assert(!_g1h->is_humongous(desired_word_size),
280          "we should not be seeing humongous-size allocations in this path");
281 
282   HeapWord* result = old_gc_alloc_region()->attempt_allocation(min_word_size,
283                                                                desired_word_size,
284                                                                actual_word_size);
285   if (result == nullptr && !old_is_full()) {
286     MutexLocker x(G1FreeList_lock, Mutex::_no_safepoint_check_flag);
287     // Multiple threads may have queued at the FreeList_lock above after checking whether there
288     // actually is still memory available. Redo the check under the lock to avoid unnecessary work;
289     // the memory may have been used up as the threads waited to acquire the lock.
290     if (!old_is_full()) {
291       result = old_gc_alloc_region()->attempt_allocation_locked(min_word_size,
292                                                                 desired_word_size,
293                                                                 actual_word_size);
294       if (result == nullptr) {
295         set_old_full();
296       }
297     }
298   }
299   return result;
300 }

  1 /*
  2  * Copyright (c) 2014, 2026, 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  *

229          word_size, temp, p2i(result));
230   return result;
231 }
232 
233 HeapWord* G1Allocator::par_allocate_during_gc(G1HeapRegionAttr dest,
234                                               uint node_index,
235                                               size_t min_word_size,
236                                               size_t desired_word_size,
237                                               size_t* actual_word_size) {
238   switch (dest.type()) {
239     case G1HeapRegionAttr::Young:
240       return survivor_attempt_allocation(node_index, min_word_size, desired_word_size, actual_word_size);
241     case G1HeapRegionAttr::Old:
242       return old_attempt_allocation(min_word_size, desired_word_size, actual_word_size);
243     default:
244       ShouldNotReachHere();
245       return nullptr; // Keep some compilers happy
246   }
247 }
248 
249 #ifdef ASSERT
250 void G1Allocator::assert_not_humongous(size_t word_size) {
251   // With CompactObjectHeaders, objects can expand during copy to accomodate hashcode.
252   // It's possible this expansion crosses the humongous threshold. In this case, we allow
253   // that and just treat it as not humongous.
254   size_t pre_expansion_size = UseCompactObjectHeaders ? word_size - 1 : word_size;
255   assert(!_g1h->is_humongous(pre_expansion_size),
256           "we should not be seeing humongous-size allocations in this path");
257 }
258 #endif
259 
260 HeapWord* G1Allocator::survivor_attempt_allocation(uint node_index,
261                                                    size_t min_word_size,
262                                                    size_t desired_word_size,
263                                                    size_t* actual_word_size) {
264   assert_not_humongous(desired_word_size);

265 
266   HeapWord* result = survivor_gc_alloc_region(node_index)->attempt_allocation(min_word_size,
267                                                                               desired_word_size,
268                                                                               actual_word_size);
269   if (result == nullptr && !survivor_is_full()) {
270     MutexLocker x(G1FreeList_lock, Mutex::_no_safepoint_check_flag);
271     // Multiple threads may have queued at the FreeList_lock above after checking whether there
272     // actually is still memory available. Redo the check under the lock to avoid unnecessary work;
273     // the memory may have been used up as the threads waited to acquire the lock.
274     if (!survivor_is_full()) {
275       result = survivor_gc_alloc_region(node_index)->attempt_allocation_locked(min_word_size,
276                                                                                desired_word_size,
277                                                                                actual_word_size);
278       if (result == nullptr) {
279         set_survivor_full();
280       }
281     }
282   }
283   return result;
284 }
285 
286 HeapWord* G1Allocator::old_attempt_allocation(size_t min_word_size,
287                                               size_t desired_word_size,
288                                               size_t* actual_word_size) {
289   assert_not_humongous(desired_word_size);

290 
291   HeapWord* result = old_gc_alloc_region()->attempt_allocation(min_word_size,
292                                                                desired_word_size,
293                                                                actual_word_size);
294   if (result == nullptr && !old_is_full()) {
295     MutexLocker x(G1FreeList_lock, Mutex::_no_safepoint_check_flag);
296     // Multiple threads may have queued at the FreeList_lock above after checking whether there
297     // actually is still memory available. Redo the check under the lock to avoid unnecessary work;
298     // the memory may have been used up as the threads waited to acquire the lock.
299     if (!old_is_full()) {
300       result = old_gc_alloc_region()->attempt_allocation_locked(min_word_size,
301                                                                 desired_word_size,
302                                                                 actual_word_size);
303       if (result == nullptr) {
304         set_old_full();
305       }
306     }
307   }
308   return result;
309 }
< prev index next >