allocation.hpp revision 1605
0N/A/*
1472N/A * Copyright (c) 1997, 2005, 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
0N/A * published by the Free Software Foundation.
0N/A *
0N/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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A#define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
0N/A#define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
0N/A#define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
0N/A
0N/A// All classes in the virtual machine must be subclassed
0N/A// by one of the following allocation classes:
0N/A//
0N/A// For objects allocated in the resource area (see resourceArea.hpp).
0N/A// - ResourceObj
0N/A//
0N/A// For objects allocated in the C-heap (managed by: free & malloc).
0N/A// - CHeapObj
0N/A//
0N/A// For objects allocated on the stack.
0N/A// - StackObj
0N/A//
0N/A// For embedded objects.
0N/A// - ValueObj
0N/A//
0N/A// For classes used as name spaces.
0N/A// - AllStatic
0N/A//
0N/A// The printable subclasses are used for debugging and define virtual
0N/A// member functions for printing. Classes that avoid allocating the
0N/A// vtbl entries in the objects should therefore not be the printable
0N/A// subclasses.
0N/A//
0N/A// The following macros and function should be used to allocate memory
0N/A// directly in the resource area or in the C-heap:
0N/A//
0N/A// NEW_RESOURCE_ARRAY(type,size)
0N/A// NEW_RESOURCE_OBJ(type)
0N/A// NEW_C_HEAP_ARRAY(type,size)
0N/A// NEW_C_HEAP_OBJ(type)
0N/A// char* AllocateHeap(size_t size, const char* name);
0N/A// void FreeHeap(void* p);
0N/A//
0N/A// C-heap allocation can be traced using +PrintHeapAllocation.
0N/A// malloc and free should therefore never called directly.
0N/A
0N/A// Base class for objects allocated in the C-heap.
0N/A
0N/A// In non product mode we introduce a super class for all allocation classes
0N/A// that supports printing.
0N/A// We avoid the superclass in product mode since some C++ compilers add
0N/A// a word overhead for empty super classes.
0N/A
0N/A#ifdef PRODUCT
0N/A#define ALLOCATION_SUPER_CLASS_SPEC
0N/A#else
0N/A#define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj
0N/Aclass AllocatedObj {
0N/A public:
0N/A // Printing support
0N/A void print() const;
0N/A void print_value() const;
0N/A
0N/A virtual void print_on(outputStream* st) const;
0N/A virtual void print_value_on(outputStream* st) const;
0N/A};
0N/A#endif
0N/A
0N/Aclass CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
0N/A public:
0N/A void* operator new(size_t size);
0N/A void operator delete(void* p);
0N/A void* new_array(size_t size);
0N/A};
0N/A
0N/A// Base class for objects allocated on the stack only.
0N/A// Calling new or delete will result in fatal error.
0N/A
0N/Aclass StackObj ALLOCATION_SUPER_CLASS_SPEC {
0N/A public:
0N/A void* operator new(size_t size);
0N/A void operator delete(void* p);
0N/A};
0N/A
0N/A// Base class for objects used as value objects.
0N/A// Calling new or delete will result in fatal error.
0N/A//
0N/A// Portability note: Certain compilers (e.g. gcc) will
0N/A// always make classes bigger if it has a superclass, even
0N/A// if the superclass does not have any virtual methods or
0N/A// instance fields. The HotSpot implementation relies on this
0N/A// not to happen. So never make a ValueObj class a direct subclass
0N/A// of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g.,
0N/A// like this:
0N/A//
0N/A// class A VALUE_OBJ_CLASS_SPEC {
0N/A// ...
0N/A// }
0N/A//
0N/A// With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can
0N/A// be defined as a an empty string "".
0N/A//
0N/Aclass _ValueObj {
0N/A public:
0N/A void* operator new(size_t size);
0N/A void operator delete(void* p);
0N/A};
0N/A
0N/A// Base class for classes that constitute name spaces.
0N/A
0N/Aclass AllStatic {
0N/A public:
0N/A AllStatic() { ShouldNotCallThis(); }
0N/A ~AllStatic() { ShouldNotCallThis(); }
0N/A};
0N/A
0N/A
0N/A//------------------------------Chunk------------------------------------------
0N/A// Linked list of raw memory chunks
0N/Aclass Chunk: public CHeapObj {
0N/A protected:
0N/A Chunk* _next; // Next Chunk in list
0N/A const size_t _len; // Size of this Chunk
0N/A public:
0N/A void* operator new(size_t size, size_t length);
0N/A void operator delete(void* p);
0N/A Chunk(size_t length);
0N/A
0N/A enum {
0N/A // default sizes; make them slightly smaller than 2**k to guard against
0N/A // buddy-system style malloc implementations
0N/A#ifdef _LP64
0N/A slack = 40, // [RGV] Not sure if this is right, but make it
0N/A // a multiple of 8.
0N/A#else
0N/A slack = 20, // suspected sizeof(Chunk) + internal malloc headers
0N/A#endif
0N/A
0N/A init_size = 1*K - slack, // Size of first chunk
0N/A medium_size= 10*K - slack, // Size of medium-sized chunk
0N/A size = 32*K - slack, // Default size of an Arena chunk (following the first)
0N/A non_pool_size = init_size + 32 // An initial size which is not one of above
0N/A };
0N/A
0N/A void chop(); // Chop this chunk
0N/A void next_chop(); // Chop next chunk
0N/A static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
0N/A
0N/A size_t length() const { return _len; }
0N/A Chunk* next() const { return _next; }
0N/A void set_next(Chunk* n) { _next = n; }
0N/A // Boundaries of data area (possibly unused)
0N/A char* bottom() const { return ((char*) this) + aligned_overhead_size(); }
0N/A char* top() const { return bottom() + _len; }
0N/A bool contains(char* p) const { return bottom() <= p && p <= top(); }
0N/A
0N/A // Start the chunk_pool cleaner task
0N/A static void start_chunk_pool_cleaner_task();
0N/A};
0N/A
0N/A
0N/A//------------------------------Arena------------------------------------------
0N/A// Fast allocation of memory
0N/Aclass Arena: public CHeapObj {
0N/Aprotected:
0N/A friend class ResourceMark;
0N/A friend class HandleMark;
0N/A friend class NoHandleMark;
0N/A Chunk *_first; // First chunk
0N/A Chunk *_chunk; // current chunk
0N/A char *_hwm, *_max; // High water mark and max in current chunk
0N/A void* grow(size_t x); // Get a new Chunk of at least size x
0N/A NOT_PRODUCT(size_t _size_in_bytes;) // Size of arena (used for memory usage tracing)
0N/A NOT_PRODUCT(static size_t _bytes_allocated;) // total #bytes allocated since start
0N/A friend class AllocStats;
0N/A debug_only(void* malloc(size_t size);)
0N/A debug_only(void* internal_malloc_4(size_t x);)
0N/A public:
0N/A Arena();
0N/A Arena(size_t init_size);
0N/A Arena(Arena *old);
0N/A ~Arena();
0N/A void destruct_contents();
0N/A char* hwm() const { return _hwm; }
0N/A
0N/A // Fast allocate in the arena. Common case is: pointer test + increment.
0N/A void* Amalloc(size_t x) {
0N/A assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
0N/A x = ARENA_ALIGN(x);
0N/A debug_only(if (UseMallocOnly) return malloc(x);)
0N/A NOT_PRODUCT(_bytes_allocated += x);
0N/A if (_hwm + x > _max) {
0N/A return grow(x);
0N/A } else {
0N/A char *old = _hwm;
0N/A _hwm += x;
0N/A return old;
0N/A }
0N/A }
0N/A // Further assume size is padded out to words
0N/A void *Amalloc_4(size_t x) {
0N/A assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
0N/A debug_only(if (UseMallocOnly) return malloc(x);)
0N/A NOT_PRODUCT(_bytes_allocated += x);
0N/A if (_hwm + x > _max) {
0N/A return grow(x);
0N/A } else {
0N/A char *old = _hwm;
0N/A _hwm += x;
0N/A return old;
0N/A }
0N/A }
0N/A
0N/A // Allocate with 'double' alignment. It is 8 bytes on sparc.
0N/A // In other cases Amalloc_D() should be the same as Amalloc_4().
0N/A void* Amalloc_D(size_t x) {
0N/A assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
0N/A debug_only(if (UseMallocOnly) return malloc(x);)
0N/A#if defined(SPARC) && !defined(_LP64)
0N/A#define DALIGN_M1 7
0N/A size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm;
0N/A x += delta;
0N/A#endif
0N/A NOT_PRODUCT(_bytes_allocated += x);
0N/A if (_hwm + x > _max) {
0N/A return grow(x); // grow() returns a result aligned >= 8 bytes.
0N/A } else {
0N/A char *old = _hwm;
0N/A _hwm += x;
0N/A#if defined(SPARC) && !defined(_LP64)
0N/A old += delta; // align to 8-bytes
0N/A#endif
0N/A return old;
0N/A }
0N/A }
0N/A
0N/A // Fast delete in area. Common case is: NOP (except for storage reclaimed)
0N/A void Afree(void *ptr, size_t size) {
0N/A#ifdef ASSERT
0N/A if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
0N/A if (UseMallocOnly) return;
0N/A#endif
0N/A if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
0N/A }
0N/A
0N/A void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
0N/A
0N/A // Move contents of this arena into an empty arena
0N/A Arena *move_contents(Arena *empty_arena);
0N/A
0N/A // Determine if pointer belongs to this Arena or not.
0N/A bool contains( const void *ptr ) const;
0N/A
0N/A // Total of all chunks in use (not thread-safe)
0N/A size_t used() const;
0N/A
0N/A // Total # of bytes used
0N/A size_t size_in_bytes() const NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0;
0N/A void set_size_in_bytes(size_t size) NOT_PRODUCT({ _size_in_bytes = size; }) PRODUCT_RETURN;
0N/A static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;
0N/A static void free_all(char** start, char** end) PRODUCT_RETURN;
0N/A
0N/Aprivate:
0N/A // Reset this Arena to empty, access will trigger grow if necessary
0N/A void reset(void) {
0N/A _first = _chunk = NULL;
0N/A _hwm = _max = NULL;
0N/A }
0N/A};
0N/A
0N/A// One of the following macros must be used when allocating
0N/A// an array or object from an arena
0N/A#define NEW_ARENA_ARRAY(arena, type, size)\
0N/A (type*) arena->Amalloc((size) * sizeof(type))
0N/A
0N/A#define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size)\
0N/A (type*) arena->Arealloc((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
0N/A
0N/A#define FREE_ARENA_ARRAY(arena, type, old, size)\
0N/A arena->Afree((char*)(old), (size) * sizeof(type))
0N/A
0N/A#define NEW_ARENA_OBJ(arena, type)\
0N/A NEW_ARENA_ARRAY(arena, type, 1)
0N/A
0N/A
0N/A//%note allocation_1
0N/Aextern char* resource_allocate_bytes(size_t size);
0N/Aextern char* resource_allocate_bytes(Thread* thread, size_t size);
0N/Aextern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size);
0N/Aextern void resource_free_bytes( char *old, size_t size );
0N/A
0N/A//----------------------------------------------------------------------
0N/A// Base class for objects allocated in the resource area per default.
0N/A// Optionally, objects may be allocated on the C heap with
0N/A// new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
0N/A// ResourceObj's can be allocated within other objects, but don't use
0N/A// new or delete (allocation_type is unknown). If new is used to allocate,
0N/A// use delete to deallocate.
0N/Aclass ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
0N/A public:
1605N/A enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
0N/A#ifdef ASSERT
0N/A private:
1605N/A // When this object is allocated on stack the new() operator is not
1605N/A // called but garbage on stack may look like a valid allocation_type.
1605N/A // Store negated 'this' pointer when new() is called to distinguish cases.
1605N/A uintptr_t _allocation;
0N/A public:
1605N/A static void set_allocation_type(address res, allocation_type type);
1605N/A allocation_type get_allocation_type();
1605N/A bool allocated_on_stack() { return get_allocation_type() == STACK_OR_EMBEDDED; }
1605N/A bool allocated_on_res_area() { return get_allocation_type() == RESOURCE_AREA; }
1605N/A bool allocated_on_C_heap() { return get_allocation_type() == C_HEAP; }
1605N/A bool allocated_on_arena() { return get_allocation_type() == ARENA; }
1605N/A ResourceObj(); // default construtor
1605N/A ResourceObj(const ResourceObj& r); // default copy construtor
1605N/A ResourceObj& operator=(const ResourceObj& r); // default copy assignment
1605N/A ~ResourceObj();
0N/A#endif // ASSERT
0N/A
0N/A public:
0N/A void* operator new(size_t size, allocation_type type);
0N/A void* operator new(size_t size, Arena *arena) {
0N/A address res = (address)arena->Amalloc(size);
1605N/A DEBUG_ONLY(set_allocation_type(res, ARENA);)
0N/A return res;
0N/A }
0N/A void* operator new(size_t size) {
0N/A address res = (address)resource_allocate_bytes(size);
1605N/A DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
0N/A return res;
0N/A }
342N/A void* operator new(size_t size, void* where, allocation_type type) {
1605N/A address res = (address)where;
1605N/A DEBUG_ONLY(set_allocation_type(res, type);)
342N/A return res;
342N/A }
0N/A void operator delete(void* p);
0N/A};
0N/A
0N/A// One of the following macros must be used when allocating an array
0N/A// or object to determine whether it should reside in the C heap on in
0N/A// the resource area.
0N/A
0N/A#define NEW_RESOURCE_ARRAY(type, size)\
0N/A (type*) resource_allocate_bytes((size) * sizeof(type))
0N/A
0N/A#define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
0N/A (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
0N/A
0N/A#define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
0N/A (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
0N/A
0N/A#define FREE_RESOURCE_ARRAY(type, old, size)\
0N/A resource_free_bytes((char*)(old), (size) * sizeof(type))
0N/A
0N/A#define FREE_FAST(old)\
0N/A /* nop */
0N/A
0N/A#define NEW_RESOURCE_OBJ(type)\
0N/A NEW_RESOURCE_ARRAY(type, 1)
0N/A
0N/A#define NEW_C_HEAP_ARRAY(type, size)\
0N/A (type*) (AllocateHeap((size) * sizeof(type), XSTR(type) " in " __FILE__))
0N/A
0N/A#define REALLOC_C_HEAP_ARRAY(type, old, size)\
0N/A (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), XSTR(type) " in " __FILE__))
0N/A
0N/A#define FREE_C_HEAP_ARRAY(type,old) \
0N/A FreeHeap((char*)(old))
0N/A
0N/A#define NEW_C_HEAP_OBJ(type)\
0N/A NEW_C_HEAP_ARRAY(type, 1)
0N/A
0N/Aextern bool warn_new_operator;
0N/A
0N/A// for statistics
0N/A#ifndef PRODUCT
0N/Aclass AllocStats : StackObj {
0N/A int start_mallocs, start_frees;
0N/A size_t start_malloc_bytes, start_res_bytes;
0N/A public:
0N/A AllocStats();
0N/A
0N/A int num_mallocs(); // since creation of receiver
0N/A size_t alloc_bytes();
0N/A size_t resource_bytes();
0N/A int num_frees();
0N/A void print();
0N/A};
0N/A#endif
0N/A
0N/A
0N/A//------------------------------ReallocMark---------------------------------
0N/A// Code which uses REALLOC_RESOURCE_ARRAY should check an associated
0N/A// ReallocMark, which is declared in the same scope as the reallocated
0N/A// pointer. Any operation that could __potentially__ cause a reallocation
0N/A// should check the ReallocMark.
0N/Aclass ReallocMark: public StackObj {
0N/Aprotected:
0N/A NOT_PRODUCT(int _nesting;)
0N/A
0N/Apublic:
0N/A ReallocMark() PRODUCT_RETURN;
0N/A void check() PRODUCT_RETURN;
0N/A};