0N/A/*
2362N/A * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/A#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
0N/A#define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
0N/A
0N/A#include "gc_implementation/g1/heapRegion.hpp"
0N/A
0N/Aclass G1CollectedHeap;
0N/A
0N/A// 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing
0N/A#define G1_ALLOC_REGION_TRACING 0
0N/A
0N/Aclass ar_ext_msg;
0N/A
0N/A// A class that holds a region that is active in satisfying allocation
0N/A// requests, potentially issued in parallel. When the active region is
0N/A// full it will be retired and replaced with a new one. The
0N/A// implementation assumes that fast-path allocations will be lock-free
0N/A// and a lock will need to be taken when the active region needs to be
5335N/A// replaced.
0N/A
0N/Aclass G1AllocRegion VALUE_OBJ_CLASS_SPEC {
0N/A friend class ar_ext_msg;
0N/A
0N/Aprivate:
0N/A // The active allocating region we are currently allocating out
5335N/A // of. The invariant is that if this object is initialized (i.e.,
0N/A // init() has been called and release() has not) then _alloc_region
0N/A // is either an active allocating region or the dummy region (i.e.,
0N/A // it can never be NULL) and this object can be used to satisfy
0N/A // allocation requests. If this object is not initialized
0N/A // (i.e. init() has not been called or release() has been called)
0N/A // then _alloc_region is NULL and this object should not be used to
0N/A // satisfy allocation requests (it was done this way to force the
0N/A // correct use of init() and release()).
0N/A HeapRegion* _alloc_region;
0N/A
0N/A // It keeps track of the distinct number of regions that are used
5335N/A // for allocation in the active interval of this object, i.e.,
0N/A // between a call to init() and a call to release(). The count
0N/A // mostly includes regions that are freshly allocated, as well as
0N/A // the region that is re-used using the set() method. This count can
0N/A // be used in any heuristics that might want to bound how many
0N/A // distinct regions this object can used during an active interval.
0N/A uint _count;
0N/A
0N/A // When we set up a new active region we save its used bytes in this
0N/A // field so that, when we retire it, we can calculate how much space
0N/A // we allocated in it.
5335N/A size_t _used_bytes_before;
0N/A
0N/A // When true, indicates that allocate calls should do BOT updates.
5335N/A const bool _bot_updates;
0N/A
0N/A // Useful for debugging and tracing.
0N/A const char* _name;
0N/A
0N/A // A dummy region (i.e., it's been allocated specially for this
0N/A // purpose and it is not part of the heap) that is full (i.e., top()
0N/A // == end()). When we don't have a valid active region we make
0N/A // _alloc_region point to this. This allows us to skip checking
5335N/A // whether the _alloc_region is NULL or not.
0N/A static HeapRegion* _dummy_region;
5335N/A
0N/A // Some of the methods below take a bot_updates parameter. Its value
5335N/A // should be the same as the _bot_updates field. The idea is that
0N/A // the parameter will be a constant for a particular alloc region
0N/A // and, given that these methods will be hopefully inlined, the
0N/A // compiler should compile out the test.
0N/A
0N/A // Perform a non-MT-safe allocation out of the given region.
0N/A static inline HeapWord* allocate(HeapRegion* alloc_region,
0N/A size_t word_size,
0N/A bool bot_updates);
0N/A
0N/A // Perform a MT-safe allocation out of the given region.
0N/A static inline HeapWord* par_allocate(HeapRegion* alloc_region,
0N/A size_t word_size,
0N/A bool bot_updates);
0N/A
0N/A // Ensure that the region passed as a parameter has been filled up
0N/A // so that noone else can allocate out of it any more.
0N/A static void fill_up_remaining_space(HeapRegion* alloc_region,
0N/A bool bot_updates);
0N/A
0N/A // Retire the active allocating region. If fill_up is true then make
0N/A // sure that the region is full before we retire it so that noone
0N/A // else can allocate out of it.
0N/A void retire(bool fill_up);
0N/A
0N/A // Allocate a new active region and use it to perform a word_size
0N/A // allocation. The force parameter will be passed on to
0N/A // G1CollectedHeap::allocate_new_alloc_region() and tells it to try
0N/A // to allocate a new region even if the max has been reached.
0N/A HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force);
0N/A
0N/A void fill_in_ext_msg(ar_ext_msg* msg, const char* message);
0N/A
0N/Aprotected:
0N/A // For convenience as subclasses use it.
0N/A static G1CollectedHeap* _g1h;
0N/A
0N/A virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0;
0N/A virtual void retire_region(HeapRegion* alloc_region,
0N/A size_t allocated_bytes) = 0;
0N/A
0N/A G1AllocRegion(const char* name, bool bot_updates);
0N/A
0N/Apublic:
5335N/A static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region);
5335N/A
0N/A HeapRegion* get() const {
0N/A // Make sure that the dummy region does not escape this class.
0N/A return (_alloc_region == _dummy_region) ? NULL : _alloc_region;
0N/A }
0N/A
0N/A uint count() { return _count; }
0N/A
0N/A // The following two are the building blocks for the allocation method.
0N/A
0N/A // First-level allocation: Should be called without holding a
0N/A // lock. It will try to allocate lock-free out of the active region,
0N/A // or return NULL if it was unable to.
0N/A inline HeapWord* attempt_allocation(size_t word_size, bool bot_updates);
0N/A
0N/A // Second-level allocation: Should be called while holding a
0N/A // lock. It will try to first allocate lock-free out of the active
0N/A // region or, if it's unable to, it will try to replace the active
0N/A // alloc region with a new one. We require that the caller takes the
0N/A // appropriate lock before calling this so that it is easier to make
0N/A // it conform to its locking protocol.
0N/A inline HeapWord* attempt_allocation_locked(size_t word_size,
0N/A bool bot_updates);
0N/A
0N/A // Should be called to allocate a new region even if the max of this
0N/A // type of regions has been reached. Should only be called if other
0N/A // allocation attempts have failed and we are not holding a valid
0N/A // active region.
0N/A inline HeapWord* attempt_allocation_force(size_t word_size,
5335N/A bool bot_updates);
0N/A
5335N/A // Should be called before we start using this object.
0N/A void init();
0N/A
0N/A // This can be used to set the active region to a specific
0N/A // region. (Use Example: we try to retain the last old GC alloc
0N/A // region that we've used during a GC and we can use set() to
0N/A // re-instate it at the beginning of the next GC.)
0N/A void set(HeapRegion* alloc_region);
0N/A
0N/A // Should be called when we want to release the active region which
0N/A // is returned after it's been retired.
0N/A HeapRegion* release();
0N/A
0N/A#if G1_ALLOC_REGION_TRACING
0N/A void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL);
0N/A#else // G1_ALLOC_REGION_TRACING
0N/A void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL) { }
0N/A#endif // G1_ALLOC_REGION_TRACING
0N/A};
0N/A
0N/Aclass ar_ext_msg : public err_msg {
0N/Apublic:
0N/A ar_ext_msg(G1AllocRegion* alloc_region, const char *message) : err_msg("") {
0N/A alloc_region->fill_in_ext_msg(this, message);
0N/A }
0N/A};
0N/A
0N/A#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
0N/A