2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2018-01-16 16:57:53 -08:00
|
|
|
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* 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.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* 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.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#ifndef SHARE_VM_MEMORY_ALLOCATION_HPP
|
|
|
|
#define SHARE_VM_MEMORY_ALLOCATION_HPP
|
|
|
|
|
|
|
|
#include "runtime/globals.hpp"
|
|
|
|
#include "utilities/globalDefinitions.hpp"
|
2012-10-10 14:35:58 -04:00
|
|
|
#include "utilities/macros.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2011-04-27 09:09:57 -04:00
|
|
|
#include <new>
|
|
|
|
|
2012-10-17 17:36:48 +02:00
|
|
|
class AllocFailStrategy {
|
|
|
|
public:
|
|
|
|
enum AllocFailEnum { EXIT_OOM, RETURN_NULL };
|
|
|
|
};
|
|
|
|
typedef AllocFailStrategy::AllocFailEnum AllocFailType;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// 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
|
|
|
|
//
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
// For classes in Metaspace (class data)
|
|
|
|
// - MetaspaceObj
|
|
|
|
//
|
2007-12-01 00:00:00 +00:00
|
|
|
// 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
|
2013-05-14 09:41:12 -07:00
|
|
|
// directly in the resource area or in the C-heap, The _OBJ variants
|
|
|
|
// of the NEW/FREE_C_HEAP macros are used for alloc/dealloc simple
|
|
|
|
// objects which are not inherited from CHeapObj, note constructor and
|
|
|
|
// destructor are not called. The preferable way to allocate objects
|
|
|
|
// is using the new operator.
|
2007-12-01 00:00:00 +00:00
|
|
|
//
|
2013-05-14 09:41:12 -07:00
|
|
|
// WARNING: The array variant must only be used for a homogenous array
|
|
|
|
// where all objects are of the exact type specified. If subtypes are
|
|
|
|
// stored in the array then must pay attention to calling destructors
|
|
|
|
// at needed.
|
|
|
|
//
|
|
|
|
// NEW_RESOURCE_ARRAY(type, size)
|
2007-12-01 00:00:00 +00:00
|
|
|
// NEW_RESOURCE_OBJ(type)
|
2013-05-14 09:41:12 -07:00
|
|
|
// NEW_C_HEAP_ARRAY(type, size)
|
|
|
|
// NEW_C_HEAP_OBJ(type, memflags)
|
2014-12-01 12:16:15 -05:00
|
|
|
// FREE_C_HEAP_ARRAY(type, old)
|
2013-05-14 09:41:12 -07:00
|
|
|
// FREE_C_HEAP_OBJ(objname, type, memflags)
|
2007-12-01 00:00:00 +00:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
virtual void print_on(outputStream* st) const;
|
|
|
|
virtual void print_value_on(outputStream* st) const;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
|
|
|
|
/*
|
2014-08-07 12:18:58 -07:00
|
|
|
* Memory types
|
2012-06-28 17:03:16 -04:00
|
|
|
*/
|
|
|
|
enum MemoryType {
|
|
|
|
// Memory type by sub systems. It occupies lower byte.
|
2014-08-07 12:18:58 -07:00
|
|
|
mtJavaHeap = 0x00, // Java heap
|
|
|
|
mtClass = 0x01, // memory class for Java classes
|
|
|
|
mtThread = 0x02, // memory for thread objects
|
|
|
|
mtThreadStack = 0x03,
|
|
|
|
mtCode = 0x04, // memory for generated code
|
|
|
|
mtGC = 0x05, // memory for GC
|
|
|
|
mtCompiler = 0x06, // memory for compiler
|
|
|
|
mtInternal = 0x07, // memory used by VM, but does not belong to
|
2012-06-28 17:03:16 -04:00
|
|
|
// any of above categories, and not used for
|
|
|
|
// native memory tracking
|
2014-08-07 12:18:58 -07:00
|
|
|
mtOther = 0x08, // memory not used by VM
|
|
|
|
mtSymbol = 0x09, // symbol
|
|
|
|
mtNMT = 0x0A, // memory used by native memory tracking
|
|
|
|
mtClassShared = 0x0B, // class data sharing
|
|
|
|
mtChunk = 0x0C, // chunk that holds content of arenas
|
|
|
|
mtTest = 0x0D, // Test type for verifying NMT
|
|
|
|
mtTracing = 0x0E, // memory used for Tracing
|
2015-09-24 12:36:04 +02:00
|
|
|
mtLogging = 0x0F, // memory for logging
|
2016-04-13 15:53:46 -05:00
|
|
|
mtArguments = 0x10, // memory for argument processing
|
2016-06-01 11:14:58 -04:00
|
|
|
mtModule = 0x11, // memory for module processing
|
|
|
|
mtNone = 0x12, // undefined
|
|
|
|
mt_number_of_types = 0x13 // number of memory types (mtDontTrack
|
2012-10-19 21:40:07 -04:00
|
|
|
// is not included as validate type)
|
2012-06-28 17:03:16 -04:00
|
|
|
};
|
|
|
|
|
2014-08-07 12:18:58 -07:00
|
|
|
typedef MemoryType MEMFLAGS;
|
2012-06-28 17:03:16 -04:00
|
|
|
|
|
|
|
|
2012-10-10 14:35:58 -04:00
|
|
|
#if INCLUDE_NMT
|
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
extern bool NMT_track_callsite;
|
|
|
|
|
2012-10-10 14:35:58 -04:00
|
|
|
#else
|
|
|
|
|
|
|
|
const bool NMT_track_callsite = false;
|
|
|
|
|
|
|
|
#endif // INCLUDE_NMT
|
|
|
|
|
2014-08-07 12:18:58 -07:00
|
|
|
class NativeCallStack;
|
2012-06-28 17:03:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
|
2007-12-01 00:00:00 +00:00
|
|
|
public:
|
2016-03-11 16:39:38 +01:00
|
|
|
NOINLINE void* operator new(size_t size, const NativeCallStack& stack) throw();
|
|
|
|
NOINLINE void* operator new(size_t size) throw();
|
|
|
|
NOINLINE void* operator new (size_t size, const std::nothrow_t& nothrow_constant,
|
2014-08-07 12:18:58 -07:00
|
|
|
const NativeCallStack& stack) throw();
|
2016-03-11 16:39:38 +01:00
|
|
|
NOINLINE void* operator new (size_t size, const std::nothrow_t& nothrow_constant)
|
2014-08-07 12:18:58 -07:00
|
|
|
throw();
|
2016-03-11 16:39:38 +01:00
|
|
|
NOINLINE void* operator new [](size_t size, const NativeCallStack& stack) throw();
|
|
|
|
NOINLINE void* operator new [](size_t size) throw();
|
|
|
|
NOINLINE void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
|
2014-08-07 12:18:58 -07:00
|
|
|
const NativeCallStack& stack) throw();
|
2016-03-11 16:39:38 +01:00
|
|
|
NOINLINE void* operator new [](size_t size, const std::nothrow_t& nothrow_constant)
|
2014-08-07 12:18:58 -07:00
|
|
|
throw();
|
2007-12-01 00:00:00 +00:00
|
|
|
void operator delete(void* p);
|
2013-05-14 09:41:12 -07:00
|
|
|
void operator delete [] (void* p);
|
2007-12-01 00:00:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Base class for objects allocated on the stack only.
|
|
|
|
// Calling new or delete will result in fatal error.
|
|
|
|
|
|
|
|
class StackObj ALLOCATION_SUPER_CLASS_SPEC {
|
2012-12-17 15:25:26 +01:00
|
|
|
private:
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new(size_t size) throw();
|
|
|
|
void* operator new [](size_t size) throw();
|
2013-08-22 09:39:54 -07:00
|
|
|
#ifdef __IBMCPP__
|
|
|
|
public:
|
|
|
|
#endif
|
|
|
|
void operator delete(void* p);
|
2013-05-14 09:41:12 -07:00
|
|
|
void operator delete [](void* p);
|
2007-12-01 00:00:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// like this:
|
|
|
|
//
|
2018-03-09 10:46:02 -05:00
|
|
|
// class A {
|
2007-12-01 00:00:00 +00:00
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// be defined as a an empty string "".
|
|
|
|
//
|
|
|
|
class _ValueObj {
|
2012-12-17 15:25:26 +01:00
|
|
|
private:
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new(size_t size) throw();
|
2013-05-14 09:41:12 -07:00
|
|
|
void operator delete(void* p);
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new [](size_t size) throw();
|
2013-05-14 09:41:12 -07:00
|
|
|
void operator delete [](void* p);
|
2007-12-01 00:00:00 +00:00
|
|
|
};
|
|
|
|
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
|
|
|
|
// Base class for objects stored in Metaspace.
|
|
|
|
// Calling delete will result in fatal error.
|
|
|
|
//
|
|
|
|
// Do not inherit from something with a vptr because this class does
|
|
|
|
// not introduce one. This class is used to allocate both shared read-only
|
|
|
|
// and shared read-write classes.
|
|
|
|
//
|
|
|
|
|
|
|
|
class ClassLoaderData;
|
2017-08-02 18:06:38 -07:00
|
|
|
class MetaspaceClosure;
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
|
|
|
|
class MetaspaceObj {
|
2018-01-16 16:57:53 -08:00
|
|
|
friend class MetaspaceShared;
|
|
|
|
// When CDS is enabled, all shared metaspace objects are mapped
|
|
|
|
// into a single contiguous memory block, so we can use these
|
|
|
|
// two pointers to quickly determine if something is in the
|
|
|
|
// shared metaspace.
|
|
|
|
//
|
|
|
|
// When CDS is not enabled, both pointers are set to NULL.
|
|
|
|
static void* _shared_metaspace_base; // (inclusive) low address
|
|
|
|
static void* _shared_metaspace_top; // (exclusive) high address
|
|
|
|
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
public:
|
2014-01-07 13:26:56 -05:00
|
|
|
bool is_metaspace_object() const;
|
2018-01-16 16:57:53 -08:00
|
|
|
bool is_shared() const {
|
|
|
|
// If no shared metaspace regions are mapped, _shared_metaspace_{base,top} will
|
|
|
|
// both be NULL and all values of p will be rejected quickly.
|
|
|
|
return (((void*)this) < _shared_metaspace_top && ((void*)this) >= _shared_metaspace_base);
|
|
|
|
}
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
void print_address_on(outputStream* st) const; // nonvirtual address printing
|
|
|
|
|
2013-05-28 16:36:19 -07:00
|
|
|
#define METASPACE_OBJ_TYPES_DO(f) \
|
|
|
|
f(Class) \
|
|
|
|
f(Symbol) \
|
|
|
|
f(TypeArrayU1) \
|
|
|
|
f(TypeArrayU2) \
|
|
|
|
f(TypeArrayU4) \
|
|
|
|
f(TypeArrayU8) \
|
|
|
|
f(TypeArrayOther) \
|
|
|
|
f(Method) \
|
|
|
|
f(ConstMethod) \
|
|
|
|
f(MethodData) \
|
|
|
|
f(ConstantPool) \
|
|
|
|
f(ConstantPoolCache) \
|
2017-08-02 18:06:38 -07:00
|
|
|
f(Annotations) \
|
|
|
|
f(MethodCounters)
|
2013-05-28 16:36:19 -07:00
|
|
|
|
|
|
|
#define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,
|
|
|
|
#define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
// Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
|
|
|
|
METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
|
|
|
|
_number_of_types
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * type_name(Type type) {
|
|
|
|
switch(type) {
|
|
|
|
METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
|
|
|
|
default:
|
|
|
|
ShouldNotReachHere();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static MetaspaceObj::Type array_type(size_t elem_size) {
|
|
|
|
switch (elem_size) {
|
|
|
|
case 1: return TypeArrayU1Type;
|
|
|
|
case 2: return TypeArrayU2Type;
|
|
|
|
case 4: return TypeArrayU4Type;
|
|
|
|
case 8: return TypeArrayU8Type;
|
|
|
|
default:
|
|
|
|
return TypeArrayOtherType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
void* operator new(size_t size, ClassLoaderData* loader_data,
|
2017-08-02 18:06:38 -07:00
|
|
|
size_t word_size,
|
2013-08-29 18:56:29 -04:00
|
|
|
Type type, Thread* thread) throw();
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
// can't use TRAPS from this header file.
|
|
|
|
void operator delete(void* p) { ShouldNotCallThis(); }
|
2017-08-02 18:06:38 -07:00
|
|
|
|
|
|
|
// Declare a *static* method with the same signature in any subclass of MetaspaceObj
|
|
|
|
// that should be read-only by default. See symbol.hpp for an example. This function
|
|
|
|
// is used by the templates in metaspaceClosure.hpp
|
|
|
|
static bool is_read_only_by_default() { return false; }
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
};
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Base class for classes that constitute name spaces.
|
|
|
|
|
2017-08-16 11:17:54 -04:00
|
|
|
class Arena;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
class AllStatic {
|
|
|
|
public:
|
|
|
|
AllStatic() { ShouldNotCallThis(); }
|
|
|
|
~AllStatic() { ShouldNotCallThis(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-17 17:36:48 +02:00
|
|
|
extern char* resource_allocate_bytes(size_t size,
|
|
|
|
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
|
|
|
|
extern char* resource_allocate_bytes(Thread* thread, size_t size,
|
|
|
|
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
|
|
|
|
extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
|
|
|
|
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
|
2007-12-01 00:00:00 +00:00
|
|
|
extern void resource_free_bytes( char *old, size_t size );
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// 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.
|
|
|
|
class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
|
|
|
|
public:
|
2010-08-03 15:55:03 -07:00
|
|
|
enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
|
2010-08-09 15:17:05 -07:00
|
|
|
static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
|
2007-12-01 00:00:00 +00:00
|
|
|
#ifdef ASSERT
|
|
|
|
private:
|
2010-08-03 15:55:03 -07:00
|
|
|
// 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.
|
2010-12-10 14:14:02 -08:00
|
|
|
// Use second array's element for verification value to distinguish garbage.
|
|
|
|
uintptr_t _allocation_t[2];
|
|
|
|
bool is_type_set() const;
|
2007-12-01 00:00:00 +00:00
|
|
|
public:
|
2010-08-09 15:17:05 -07:00
|
|
|
allocation_type get_allocation_type() const;
|
|
|
|
bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; }
|
|
|
|
bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
|
|
|
|
bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; }
|
|
|
|
bool allocated_on_arena() const { return get_allocation_type() == ARENA; }
|
2014-01-23 14:47:23 +01:00
|
|
|
ResourceObj(); // default constructor
|
|
|
|
ResourceObj(const ResourceObj& r); // default copy constructor
|
2010-08-03 15:55:03 -07:00
|
|
|
ResourceObj& operator=(const ResourceObj& r); // default copy assignment
|
|
|
|
~ResourceObj();
|
2007-12-01 00:00:00 +00:00
|
|
|
#endif // ASSERT
|
|
|
|
|
|
|
|
public:
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new(size_t size, allocation_type type, MEMFLAGS flags) throw();
|
|
|
|
void* operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw();
|
2012-10-17 17:36:48 +02:00
|
|
|
void* operator new(size_t size, const std::nothrow_t& nothrow_constant,
|
2013-08-29 18:56:29 -04:00
|
|
|
allocation_type type, MEMFLAGS flags) throw();
|
2013-05-14 09:41:12 -07:00
|
|
|
void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
|
2013-08-29 18:56:29 -04:00
|
|
|
allocation_type type, MEMFLAGS flags) throw();
|
2013-05-14 09:41:12 -07:00
|
|
|
|
2017-08-16 11:17:54 -04:00
|
|
|
void* operator new(size_t size, Arena *arena) throw();
|
2013-05-14 09:41:12 -07:00
|
|
|
|
2017-08-16 11:17:54 -04:00
|
|
|
void* operator new [](size_t size, Arena *arena) throw();
|
2013-05-14 09:41:12 -07:00
|
|
|
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new(size_t size) throw() {
|
2007-12-01 00:00:00 +00:00
|
|
|
address res = (address)resource_allocate_bytes(size);
|
2010-08-03 15:55:03 -07:00
|
|
|
DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
|
2007-12-01 00:00:00 +00:00
|
|
|
return res;
|
|
|
|
}
|
2012-10-17 17:36:48 +02:00
|
|
|
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
|
2012-10-17 17:36:48 +02:00
|
|
|
address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
|
|
|
|
DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new [](size_t size) throw() {
|
2013-05-14 09:41:12 -07:00
|
|
|
address res = (address)resource_allocate_bytes(size);
|
|
|
|
DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() {
|
2013-05-14 09:41:12 -07:00
|
|
|
address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
|
|
|
|
DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
void operator delete(void* p);
|
2013-05-14 09:41:12 -07:00
|
|
|
void operator delete [](void* p);
|
2007-12-01 00:00:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#define NEW_RESOURCE_ARRAY(type, size)\
|
|
|
|
(type*) resource_allocate_bytes((size) * sizeof(type))
|
|
|
|
|
2013-03-07 11:49:38 -05:00
|
|
|
#define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\
|
|
|
|
(type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
#define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
|
|
|
|
(type*) resource_allocate_bytes(thread, (size) * sizeof(type))
|
|
|
|
|
2013-06-15 13:17:36 +02:00
|
|
|
#define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\
|
|
|
|
(type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
#define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
|
2013-06-15 13:17:36 +02:00
|
|
|
(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))
|
|
|
|
|
|
|
|
#define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\
|
|
|
|
(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\
|
|
|
|
(new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
#define FREE_RESOURCE_ARRAY(type, old, size)\
|
|
|
|
resource_free_bytes((char*)(old), (size) * sizeof(type))
|
|
|
|
|
|
|
|
#define FREE_FAST(old)\
|
|
|
|
/* nop */
|
|
|
|
|
|
|
|
#define NEW_RESOURCE_OBJ(type)\
|
|
|
|
NEW_RESOURCE_ARRAY(type, 1)
|
|
|
|
|
2013-06-15 13:17:36 +02:00
|
|
|
#define NEW_RESOURCE_OBJ_RETURN_NULL(type)\
|
|
|
|
NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)
|
|
|
|
|
|
|
|
#define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\
|
2013-08-26 09:33:01 +02:00
|
|
|
(type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)
|
2013-06-15 13:17:36 +02:00
|
|
|
|
|
|
|
#define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
|
|
|
|
(type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
|
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
#define NEW_C_HEAP_ARRAY(type, size, memflags)\
|
|
|
|
(type*) (AllocateHeap((size) * sizeof(type), memflags))
|
|
|
|
|
2013-06-15 13:17:36 +02:00
|
|
|
#define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\
|
2013-08-26 09:33:01 +02:00
|
|
|
NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)
|
2013-06-15 13:17:36 +02:00
|
|
|
|
|
|
|
#define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\
|
2014-08-07 12:18:58 -07:00
|
|
|
NEW_C_HEAP_ARRAY3(type, (size), memflags, CURRENT_PC, AllocFailStrategy::RETURN_NULL)
|
2013-06-15 13:17:36 +02:00
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
#define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
|
2013-08-26 09:33:01 +02:00
|
|
|
(type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))
|
2012-06-28 17:03:16 -04:00
|
|
|
|
2013-06-15 13:17:36 +02:00
|
|
|
#define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\
|
2013-08-26 09:33:01 +02:00
|
|
|
(type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))
|
2013-06-15 13:17:36 +02:00
|
|
|
|
2014-12-01 12:16:15 -05:00
|
|
|
#define FREE_C_HEAP_ARRAY(type, old) \
|
|
|
|
FreeHeap((char*)(old))
|
2012-06-28 17:03:16 -04:00
|
|
|
|
2013-05-14 09:41:12 -07:00
|
|
|
// allocate type in heap without calling ctor
|
|
|
|
#define NEW_C_HEAP_OBJ(type, memflags)\
|
|
|
|
NEW_C_HEAP_ARRAY(type, 1, memflags)
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-06-15 13:17:36 +02:00
|
|
|
#define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\
|
|
|
|
NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)
|
|
|
|
|
2013-05-14 09:41:12 -07:00
|
|
|
// deallocate obj of type in heap without calling dtor
|
2014-12-01 12:16:15 -05:00
|
|
|
#define FREE_C_HEAP_OBJ(objname)\
|
|
|
|
FreeHeap((char*)objname);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// for statistics
|
|
|
|
#ifndef PRODUCT
|
|
|
|
class AllocStats : StackObj {
|
2011-02-07 10:34:39 -08:00
|
|
|
julong start_mallocs, start_frees;
|
|
|
|
julong start_malloc_bytes, start_mfree_bytes, start_res_bytes;
|
2007-12-01 00:00:00 +00:00
|
|
|
public:
|
|
|
|
AllocStats();
|
|
|
|
|
2011-02-07 10:34:39 -08:00
|
|
|
julong num_mallocs(); // since creation of receiver
|
|
|
|
julong alloc_bytes();
|
|
|
|
julong num_frees();
|
|
|
|
julong free_bytes();
|
|
|
|
julong resource_bytes();
|
2007-12-01 00:00:00 +00:00
|
|
|
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:
|
|
|
|
ReallocMark() PRODUCT_RETURN;
|
|
|
|
void check() PRODUCT_RETURN;
|
|
|
|
};
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2013-04-08 07:49:28 +02:00
|
|
|
// Helper class to allocate arrays that may become large.
|
|
|
|
// Uses the OS malloc for allocations smaller than ArrayAllocatorMallocLimit
|
|
|
|
// and uses mapped memory for larger allocations.
|
|
|
|
// Most OS mallocs do something similar but Solaris malloc does not revert
|
|
|
|
// to mapped memory for large allocations. By default ArrayAllocatorMallocLimit
|
|
|
|
// is set so that we always use malloc except for Solaris where we set the
|
|
|
|
// limit to get mapped memory.
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
2016-03-09 12:44:12 +01:00
|
|
|
class ArrayAllocator : public AllStatic {
|
|
|
|
private:
|
|
|
|
static bool should_use_malloc(size_t length);
|
2014-04-02 14:17:34 +02:00
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
static E* allocate_malloc(size_t length, MEMFLAGS flags);
|
|
|
|
static E* allocate_mmap(size_t length, MEMFLAGS flags);
|
2016-03-09 12:44:12 +01:00
|
|
|
|
|
|
|
static void free_malloc(E* addr, size_t length);
|
|
|
|
static void free_mmap(E* addr, size_t length);
|
2013-06-18 22:45:32 +02:00
|
|
|
|
2016-03-09 12:44:12 +01:00
|
|
|
public:
|
2017-07-21 21:01:59 -04:00
|
|
|
static E* allocate(size_t length, MEMFLAGS flags);
|
|
|
|
static E* reallocate(E* old_addr, size_t old_length, size_t new_length, MEMFLAGS flags);
|
2016-03-09 12:44:12 +01:00
|
|
|
static void free(E* addr, size_t length);
|
2013-04-08 07:49:28 +02:00
|
|
|
};
|
|
|
|
|
2016-03-09 17:03:04 +01:00
|
|
|
// Uses mmaped memory for all allocations. All allocations are initially
|
|
|
|
// zero-filled. No pre-touching.
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
2016-03-09 17:03:04 +01:00
|
|
|
class MmapArrayAllocator : public AllStatic {
|
|
|
|
private:
|
|
|
|
static size_t size_for(size_t length);
|
|
|
|
|
|
|
|
public:
|
2017-07-21 21:01:59 -04:00
|
|
|
static E* allocate_or_null(size_t length, MEMFLAGS flags);
|
|
|
|
static E* allocate(size_t length, MEMFLAGS flags);
|
2016-03-09 17:03:04 +01:00
|
|
|
static void free(E* addr, size_t length);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Uses malloc:ed memory for all allocations.
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
2016-03-09 17:03:04 +01:00
|
|
|
class MallocArrayAllocator : public AllStatic {
|
|
|
|
public:
|
|
|
|
static size_t size_for(size_t length);
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
static E* allocate(size_t length, MEMFLAGS flags);
|
2016-03-09 17:03:04 +01:00
|
|
|
static void free(E* addr, size_t length);
|
|
|
|
};
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#endif // SHARE_VM_MEMORY_ALLOCATION_HPP
|