duke@435: /* duke@435: * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: inline HeapWord* ThreadLocalAllocBuffer::allocate(size_t size) { duke@435: invariants(); duke@435: HeapWord* obj = top(); duke@435: if (pointer_delta(end(), obj) >= size) { duke@435: // successful thread-local allocation duke@435: duke@435: DEBUG_ONLY(Copy::fill_to_words(obj, size, badHeapWordVal)); duke@435: // This addition is safe because we know that top is duke@435: // at least size below end, so the add can't wrap. duke@435: set_top(obj + size); duke@435: duke@435: invariants(); duke@435: return obj; duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: inline size_t ThreadLocalAllocBuffer::compute_size(size_t obj_size) { duke@435: const size_t aligned_obj_size = align_object_size(obj_size); duke@435: duke@435: // Compute the size for the new TLAB. duke@435: // The "last" tlab may be smaller to reduce fragmentation. duke@435: // unsafe_max_tlab_alloc is just a hint. duke@435: const size_t available_size = Universe::heap()->unsafe_max_tlab_alloc(myThread()) / duke@435: HeapWordSize; duke@435: size_t new_tlab_size = MIN2(available_size, desired_size() + aligned_obj_size); duke@435: duke@435: // Make sure there's enough room for object and filler int[]. duke@435: const size_t obj_plus_filler_size = aligned_obj_size + alignment_reserve(); duke@435: if (new_tlab_size < obj_plus_filler_size) { duke@435: // If there isn't enough room for the allocation, return failure. duke@435: if (PrintTLAB && Verbose) { duke@435: gclog_or_tty->print_cr("ThreadLocalAllocBuffer::compute_size(" SIZE_FORMAT ")" duke@435: " returns failure", duke@435: obj_size); duke@435: } duke@435: return 0; duke@435: } duke@435: if (PrintTLAB && Verbose) { duke@435: gclog_or_tty->print_cr("ThreadLocalAllocBuffer::compute_size(" SIZE_FORMAT ")" duke@435: " returns " SIZE_FORMAT, duke@435: obj_size, new_tlab_size); duke@435: } duke@435: return new_tlab_size; duke@435: } duke@435: duke@435: duke@435: void ThreadLocalAllocBuffer::record_slow_allocation(size_t obj_size) { duke@435: // Raise size required to bypass TLAB next time. Why? Else there's duke@435: // a risk that a thread that repeatedly allocates objects of one duke@435: // size will get stuck on this slow path. duke@435: duke@435: set_refill_waste_limit(refill_waste_limit() + refill_waste_limit_increment()); duke@435: duke@435: _slow_allocations++; duke@435: duke@435: if (PrintTLAB && Verbose) { duke@435: Thread* thrd = myThread(); duke@435: gclog_or_tty->print("TLAB: %s thread: "INTPTR_FORMAT" [id: %2d]" duke@435: " obj: "SIZE_FORMAT duke@435: " free: "SIZE_FORMAT duke@435: " waste: "SIZE_FORMAT"\n", duke@435: "slow", thrd, thrd->osthread()->thread_id(), duke@435: obj_size, free(), refill_waste_limit()); duke@435: } duke@435: }