allocation.hpp revision 3863
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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_MEMORY_ALLOCATION_HPP
#define SHARE_VM_MEMORY_ALLOCATION_HPP
#include "runtime/globals.hpp"
#include "utilities/globalDefinitions.hpp"
#ifdef COMPILER1
#include "c1/c1_globals.hpp"
#endif
#ifdef COMPILER2
#include "opto/c2_globals.hpp"
#endif
#include <new>
// noinline attribute
#ifdef _WINDOWS
#else
#define _NOINLINE_
#else
#endif
#endif
// All classes in the virtual machine must be subclassed
// by one of the following allocation classes:
//
// For objects allocated in the resource area (see resourceArea.hpp).
// - ResourceObj
//
// For objects allocated in the C-heap (managed by: free & malloc).
// - CHeapObj
//
// For objects allocated on the stack.
// - StackObj
//
// For embedded objects.
// - ValueObj
//
// For classes used as name spaces.
// - AllStatic
//
// The printable subclasses are used for debugging and define virtual
// member functions for printing. Classes that avoid allocating the
// vtbl entries in the objects should therefore not be the printable
// subclasses.
//
// The following macros and function should be used to allocate memory
// directly in the resource area or in the C-heap:
//
// NEW_RESOURCE_ARRAY(type,size)
// NEW_RESOURCE_OBJ(type)
// NEW_C_HEAP_ARRAY(type,size)
// NEW_C_HEAP_OBJ(type)
// char* AllocateHeap(size_t size, const char* name);
// void FreeHeap(void* p);
//
// C-heap allocation can be traced using +PrintHeapAllocation.
// malloc and free should therefore never called directly.
// Base class for objects allocated in the C-heap.
// In non product mode we introduce a super class for all allocation classes
// that supports printing.
// We avoid the superclass in product mode since some C++ compilers add
// a word overhead for empty super classes.
#ifdef PRODUCT
#define ALLOCATION_SUPER_CLASS_SPEC
#else
#define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj
class AllocatedObj {
public:
// Printing support
void print() const;
void print_value() const;
};
#endif
/*
* MemoryType bitmap layout:
* | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 |
* | memory type | object | reserved |
* | | type | |
*/
enum MemoryType {
// Memory type by sub systems. It occupies lower byte.
mtThreadStack = 0x0300,
// any of above categories, and not used for
// native memory tracking
mt_masks = 0x7F00,
// object type mask
ot_masks = 0x00F0
};
typedef unsigned short MEMFLAGS;
extern bool NMT_track_callsite;
// debug build does not inline
#if defined(_DEBUG_)
#else
#endif
public:
void operator delete(void* p);
};
// Base class for objects allocated on the stack only.
// Calling new or delete will result in fatal error.
class StackObj ALLOCATION_SUPER_CLASS_SPEC {
public:
void operator delete(void* p);
};
// Base class for objects used as value objects.
// Calling new or delete will result in fatal error.
//
// Portability note: Certain compilers (e.g. gcc) will
// always make classes bigger if it has a superclass, even
// if the superclass does not have any virtual methods or
// instance fields. The HotSpot implementation relies on this
// not to happen. So never make a ValueObj class a direct subclass
// of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g.,
// like this:
//
// class A VALUE_OBJ_CLASS_SPEC {
// ...
// }
//
// With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can
// be defined as a an empty string "".
//
class _ValueObj {
public:
void operator delete(void* p);
};
// Base class for classes that constitute name spaces.
class AllStatic {
public:
AllStatic() { ShouldNotCallThis(); }
~AllStatic() { ShouldNotCallThis(); }
};
//------------------------------Chunk------------------------------------------
// Linked list of raw memory chunks
friend class VMStructs;
protected:
public:
void operator delete(void* p);
enum {
// default sizes; make them slightly smaller than 2**k to guard against
// buddy-system style malloc implementations
#ifdef _LP64
// a multiple of 8.
#else
#endif
};
void chop(); // Chop this chunk
void next_chop(); // Chop next chunk
// Boundaries of data area (possibly unused)
char* bottom() const { return ((char*) this) + aligned_overhead_size(); }
// Start the chunk_pool cleaner task
static void start_chunk_pool_cleaner_task();
static void clean_chunk_pool();
};
//------------------------------Arena------------------------------------------
// Fast allocation of memory
protected:
friend class ResourceMark;
friend class HandleMark;
friend class NoHandleMark;
friend class VMStructs;
friend class AllocStats;
}
}
public:
Arena();
~Arena();
void destruct_contents();
// new operators
// dynamic memory type tagging
void operator delete(void* p);
// Fast allocate in the arena. Common case is: pointer test + increment.
x = ARENA_ALIGN(x);
check_for_overflow(x, "Arena::Amalloc");
return grow(x);
} else {
_hwm += x;
return old;
}
}
// Further assume size is padded out to words
check_for_overflow(x, "Arena::Amalloc_4");
return grow(x);
} else {
_hwm += x;
return old;
}
}
// Allocate with 'double' alignment. It is 8 bytes on sparc.
// In other cases Amalloc_D() should be the same as Amalloc_4().
#define DALIGN_M1 7
x += delta;
#endif
check_for_overflow(x, "Arena::Amalloc_D");
return grow(x); // grow() returns a result aligned >= 8 bytes.
} else {
_hwm += x;
#endif
return old;
}
}
// Fast delete in area. Common case is: NOP (except for storage reclaimed)
#ifdef ASSERT
if (UseMallocOnly) return;
#endif
}
// Move contents of this arena into an empty arena
// Determine if pointer belongs to this Arena or not.
// Total of all chunks in use (not thread-safe)
// Total # of bytes used
// how many arena instances
private:
// Reset this Arena to empty, access will trigger grow if necessary
void reset(void) {
}
};
// One of the following macros must be used when allocating
// an array or object from an arena
//%note allocation_1
//----------------------------------------------------------------------
// Base class for objects allocated in the resource area per default.
// Optionally, objects may be allocated on the C heap with
// new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
// ResourceObj's can be allocated within other objects, but don't use
// new or delete (allocation_type is unknown). If new is used to allocate,
// use delete to deallocate.
public:
enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
#ifdef ASSERT
private:
// When this object is allocated on stack the new() operator is not
// called but garbage on stack may look like a valid allocation_type.
// Store negated 'this' pointer when new() is called to distinguish cases.
// Use second array's element for verification value to distinguish garbage.
bool is_type_set() const;
public:
allocation_type get_allocation_type() const;
ResourceObj(); // default construtor
~ResourceObj();
#endif // ASSERT
public:
return res;
}
return res;
}
void operator delete(void* p);
};
// One of the following macros must be used when allocating an array
// or object to determine whether it should reside in the C heap on in
// the resource area.
(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
/* nop */
#define NEW_RESOURCE_OBJ(type)\
extern bool warn_new_operator;
// for statistics
#ifndef PRODUCT
class AllocStats : StackObj {
public:
AllocStats();
julong free_bytes();
void print();
};
#endif
//------------------------------ReallocMark---------------------------------
// Code which uses REALLOC_RESOURCE_ARRAY should check an associated
// ReallocMark, which is declared in the same scope as the reallocated
// pointer. Any operation that could __potentially__ cause a reallocation
// should check the ReallocMark.
class ReallocMark: public StackObj {
protected:
NOT_PRODUCT(int _nesting;)
public:
void check() PRODUCT_RETURN;
};
#endif // SHARE_VM_MEMORY_ALLOCATION_HPP