Path: blob/master/src/hotspot/share/memory/allocation.hpp
64440 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_MEMORY_ALLOCATION_HPP25#define SHARE_MEMORY_ALLOCATION_HPP2627#include "memory/allStatic.hpp"28#include "utilities/globalDefinitions.hpp"29#include "utilities/macros.hpp"3031#include <new>3233class outputStream;34class Thread;35class JavaThread;3637class AllocFailStrategy {38public:39enum AllocFailEnum { EXIT_OOM, RETURN_NULL };40};41typedef AllocFailStrategy::AllocFailEnum AllocFailType;4243// The virtual machine must never call one of the implicitly declared44// global allocation or deletion functions. (Such calls may result in45// link-time or run-time errors.) For convenience and documentation of46// intended use, classes in the virtual machine may be derived from one47// of the following allocation classes, some of which define allocation48// and deletion functions.49// Note: std::malloc and std::free should never called directly.5051//52// For objects allocated in the resource area (see resourceArea.hpp).53// - ResourceObj54//55// For objects allocated in the C-heap (managed by: free & malloc and tracked with NMT)56// - CHeapObj57//58// For objects allocated on the stack.59// - StackObj60//61// For classes used as name spaces.62// - AllStatic63//64// For classes in Metaspace (class data)65// - MetaspaceObj66//67// The printable subclasses are used for debugging and define virtual68// member functions for printing. Classes that avoid allocating the69// vtbl entries in the objects should therefore not be the printable70// subclasses.71//72// The following macros and function should be used to allocate memory73// directly in the resource area or in the C-heap, The _OBJ variants74// of the NEW/FREE_C_HEAP macros are used for alloc/dealloc simple75// objects which are not inherited from CHeapObj, note constructor and76// destructor are not called. The preferable way to allocate objects77// is using the new operator.78//79// WARNING: The array variant must only be used for a homogenous array80// where all objects are of the exact type specified. If subtypes are81// stored in the array then must pay attention to calling destructors82// at needed.83//84// NEW_RESOURCE_ARRAY*85// REALLOC_RESOURCE_ARRAY*86// FREE_RESOURCE_ARRAY*87// NEW_RESOURCE_OBJ*88// NEW_C_HEAP_ARRAY*89// REALLOC_C_HEAP_ARRAY*90// FREE_C_HEAP_ARRAY*91// NEW_C_HEAP_OBJ*92// FREE_C_HEAP_OBJ93//94// char* AllocateHeap(size_t size, MEMFLAGS flags, const NativeCallStack& stack, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);95// char* AllocateHeap(size_t size, MEMFLAGS flags, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);96// char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);97// void FreeHeap(void* p);98//99// In non product mode we introduce a super class for all allocation classes100// that supports printing.101// We avoid the superclass in product mode to save space.102103#ifdef PRODUCT104#define ALLOCATION_SUPER_CLASS_SPEC105#else106#define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj107class AllocatedObj {108public:109// Printing support110void print() const;111void print_value() const;112113virtual void print_on(outputStream* st) const;114virtual void print_value_on(outputStream* st) const;115};116#endif117118#define MEMORY_TYPES_DO(f) \119/* Memory type by sub systems. It occupies lower byte. */ \120f(mtJavaHeap, "Java Heap") /* Java heap */ \121f(mtClass, "Class") /* Java classes */ \122f(mtThread, "Thread") /* thread objects */ \123f(mtThreadStack, "Thread Stack") \124f(mtCode, "Code") /* generated code */ \125f(mtGC, "GC") \126f(mtCompiler, "Compiler") \127f(mtJVMCI, "JVMCI") \128f(mtInternal, "Internal") /* memory used by VM, but does not belong to */ \129/* any of above categories, and not used by */ \130/* NMT */ \131f(mtOther, "Other") /* memory not used by VM */ \132f(mtSymbol, "Symbol") \133f(mtNMT, "Native Memory Tracking") /* memory used by NMT */ \134f(mtClassShared, "Shared class space") /* class data sharing */ \135f(mtChunk, "Arena Chunk") /* chunk that holds content of arenas */ \136f(mtTest, "Test") /* Test type for verifying NMT */ \137f(mtTracing, "Tracing") \138f(mtLogging, "Logging") \139f(mtStatistics, "Statistics") \140f(mtArguments, "Arguments") \141f(mtModule, "Module") \142f(mtSafepoint, "Safepoint") \143f(mtSynchronizer, "Synchronization") \144f(mtServiceability, "Serviceability") \145f(mtMetaspace, "Metaspace") \146f(mtStringDedup, "String Deduplication") \147f(mtObjectMonitor, "Object Monitors") \148f(mtNone, "Unknown") \149//end150151#define MEMORY_TYPE_DECLARE_ENUM(type, human_readable) \152type,153154/*155* Memory types156*/157enum class MEMFLAGS {158MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_ENUM)159mt_number_of_types // number of memory types (mtDontTrack160// is not included as validate type)161};162163#define MEMORY_TYPE_SHORTNAME(type, human_readable) \164constexpr MEMFLAGS type = MEMFLAGS::type;165166// Generate short aliases for the enum values. E.g. mtGC instead of MEMFLAGS::mtGC.167MEMORY_TYPES_DO(MEMORY_TYPE_SHORTNAME)168169// Make an int version of the sentinel end value.170constexpr int mt_number_of_types = static_cast<int>(MEMFLAGS::mt_number_of_types);171172#if INCLUDE_NMT173174extern bool NMT_track_callsite;175176#else177178const bool NMT_track_callsite = false;179180#endif // INCLUDE_NMT181182class NativeCallStack;183184185char* AllocateHeap(size_t size,186MEMFLAGS flags,187const NativeCallStack& stack,188AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);189char* AllocateHeap(size_t size,190MEMFLAGS flags,191AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);192193char* ReallocateHeap(char *old,194size_t size,195MEMFLAGS flag,196AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);197198// handles NULL pointers199void FreeHeap(void* p);200201template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {202public:203ALWAYSINLINE void* operator new(size_t size) throw() {204return (void*)AllocateHeap(size, F);205}206207ALWAYSINLINE void* operator new(size_t size,208const NativeCallStack& stack) throw() {209return (void*)AllocateHeap(size, F, stack);210}211212ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t&,213const NativeCallStack& stack) throw() {214return (void*)AllocateHeap(size, F, stack, AllocFailStrategy::RETURN_NULL);215}216217ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t&) throw() {218return (void*)AllocateHeap(size, F, AllocFailStrategy::RETURN_NULL);219}220221ALWAYSINLINE void* operator new[](size_t size) throw() {222return (void*)AllocateHeap(size, F);223}224225ALWAYSINLINE void* operator new[](size_t size,226const NativeCallStack& stack) throw() {227return (void*)AllocateHeap(size, F, stack);228}229230ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t&,231const NativeCallStack& stack) throw() {232return (void*)AllocateHeap(size, F, stack, AllocFailStrategy::RETURN_NULL);233}234235ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t&) throw() {236return (void*)AllocateHeap(size, F, AllocFailStrategy::RETURN_NULL);237}238239void operator delete(void* p) { FreeHeap(p); }240void operator delete [] (void* p) { FreeHeap(p); }241};242243// Base class for objects allocated on the stack only.244// Calling new or delete will result in fatal error.245246class StackObj ALLOCATION_SUPER_CLASS_SPEC {247private:248void* operator new(size_t size) throw();249void* operator new [](size_t size) throw();250void operator delete(void* p);251void operator delete [](void* p);252};253254// Base class for objects stored in Metaspace.255// Calling delete will result in fatal error.256//257// Do not inherit from something with a vptr because this class does258// not introduce one. This class is used to allocate both shared read-only259// and shared read-write classes.260//261262class ClassLoaderData;263class MetaspaceClosure;264265class MetaspaceObj {266// There are functions that all subtypes of MetaspaceObj are expected267// to implement, so that templates which are defined for this class hierarchy268// can work uniformly. Within the sub-hierarchy of Metadata, these are virtuals.269// Elsewhere in the hierarchy of MetaspaceObj, type(), size(), and/or on_stack()270// can be static if constant.271//272// The following functions are required by MetaspaceClosure:273// void metaspace_pointers_do(MetaspaceClosure* it) { <walk my refs> }274// int size() const { return align_up(sizeof(<This>), wordSize) / wordSize; }275// MetaspaceObj::Type type() const { return <This>Type; }276//277// The following functions are required by MetadataFactory::free_metadata():278// bool on_stack() { return false; }279// void deallocate_contents(ClassLoaderData* loader_data);280281friend class VMStructs;282// When CDS is enabled, all shared metaspace objects are mapped283// into a single contiguous memory block, so we can use these284// two pointers to quickly determine if something is in the285// shared metaspace.286// When CDS is not enabled, both pointers are set to NULL.287static void* _shared_metaspace_base; // (inclusive) low address288static void* _shared_metaspace_top; // (exclusive) high address289290public:291292// Returns true if the pointer points to a valid MetaspaceObj. A valid293// MetaspaceObj is MetaWord-aligned and contained within either294// non-shared or shared metaspace.295static bool is_valid(const MetaspaceObj* p);296297static bool is_shared(const MetaspaceObj* p) {298// If no shared metaspace regions are mapped, _shared_metaspace_{base,top} will299// both be NULL and all values of p will be rejected quickly.300return (((void*)p) < _shared_metaspace_top &&301((void*)p) >= _shared_metaspace_base);302}303bool is_shared() const { return MetaspaceObj::is_shared(this); }304305void print_address_on(outputStream* st) const; // nonvirtual address printing306307static void set_shared_metaspace_range(void* base, void* top) {308_shared_metaspace_base = base;309_shared_metaspace_top = top;310}311312static void* shared_metaspace_base() { return _shared_metaspace_base; }313static void* shared_metaspace_top() { return _shared_metaspace_top; }314315#define METASPACE_OBJ_TYPES_DO(f) \316f(Class) \317f(Symbol) \318f(TypeArrayU1) \319f(TypeArrayU2) \320f(TypeArrayU4) \321f(TypeArrayU8) \322f(TypeArrayOther) \323f(Method) \324f(ConstMethod) \325f(MethodData) \326f(ConstantPool) \327f(ConstantPoolCache) \328f(Annotations) \329f(MethodCounters) \330f(RecordComponent)331332#define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,333#define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;334335enum Type {336// Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc337METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)338_number_of_types339};340341static const char * type_name(Type type) {342switch(type) {343METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)344default:345ShouldNotReachHere();346return NULL;347}348}349350static MetaspaceObj::Type array_type(size_t elem_size) {351switch (elem_size) {352case 1: return TypeArrayU1Type;353case 2: return TypeArrayU2Type;354case 4: return TypeArrayU4Type;355case 8: return TypeArrayU8Type;356default:357return TypeArrayOtherType;358}359}360361void* operator new(size_t size, ClassLoaderData* loader_data,362size_t word_size,363Type type, JavaThread* thread) throw();364// can't use TRAPS from this header file.365void* operator new(size_t size, ClassLoaderData* loader_data,366size_t word_size,367Type type) throw();368void operator delete(void* p) { ShouldNotCallThis(); }369370// Declare a *static* method with the same signature in any subclass of MetaspaceObj371// that should be read-only by default. See symbol.hpp for an example. This function372// is used by the templates in metaspaceClosure.hpp373static bool is_read_only_by_default() { return false; }374};375376// Base class for classes that constitute name spaces.377378class Arena;379380extern char* resource_allocate_bytes(size_t size,381AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);382extern char* resource_allocate_bytes(Thread* thread, size_t size,383AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);384extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,385AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);386extern void resource_free_bytes( char *old, size_t size );387388//----------------------------------------------------------------------389// Base class for objects allocated in the resource area per default.390// Optionally, objects may be allocated on the C heap with391// new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)392// ResourceObj's can be allocated within other objects, but don't use393// new or delete (allocation_type is unknown). If new is used to allocate,394// use delete to deallocate.395class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {396public:397enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };398static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;399#ifdef ASSERT400private:401// When this object is allocated on stack the new() operator is not402// called but garbage on stack may look like a valid allocation_type.403// Store negated 'this' pointer when new() is called to distinguish cases.404// Use second array's element for verification value to distinguish garbage.405uintptr_t _allocation_t[2];406bool is_type_set() const;407void initialize_allocation_info();408public:409allocation_type get_allocation_type() const;410bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; }411bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }412bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; }413bool allocated_on_arena() const { return get_allocation_type() == ARENA; }414protected:415ResourceObj(); // default constructor416ResourceObj(const ResourceObj& r); // default copy constructor417ResourceObj& operator=(const ResourceObj& r); // default copy assignment418~ResourceObj();419#endif // ASSERT420421public:422void* operator new(size_t size, allocation_type type, MEMFLAGS flags) throw();423void* operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw();424void* operator new(size_t size, const std::nothrow_t& nothrow_constant,425allocation_type type, MEMFLAGS flags) throw();426void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,427allocation_type type, MEMFLAGS flags) throw();428429void* operator new(size_t size, Arena *arena) throw();430431void* operator new [](size_t size, Arena *arena) throw();432433void* operator new(size_t size) throw() {434address res = (address)resource_allocate_bytes(size);435DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)436return res;437}438439void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {440address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);441DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)442return res;443}444445void* operator new [](size_t size) throw() {446address res = (address)resource_allocate_bytes(size);447DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)448return res;449}450451void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() {452address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);453DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)454return res;455}456457void operator delete(void* p);458void operator delete [](void* p);459};460461// One of the following macros must be used when allocating an array462// or object to determine whether it should reside in the C heap on in463// the resource area.464465#define NEW_RESOURCE_ARRAY(type, size)\466(type*) resource_allocate_bytes((size) * sizeof(type))467468#define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\469(type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)470471#define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\472(type*) resource_allocate_bytes(thread, (size) * sizeof(type))473474#define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\475(type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)476477#define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\478(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))479480#define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\481(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\482(new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)483484#define FREE_RESOURCE_ARRAY(type, old, size)\485resource_free_bytes((char*)(old), (size) * sizeof(type))486487#define FREE_FAST(old)\488/* nop */489490#define NEW_RESOURCE_OBJ(type)\491NEW_RESOURCE_ARRAY(type, 1)492493#define NEW_RESOURCE_OBJ_RETURN_NULL(type)\494NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)495496#define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\497(type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)498499#define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\500(type*) (AllocateHeap((size) * sizeof(type), memflags, pc))501502#define NEW_C_HEAP_ARRAY(type, size, memflags)\503(type*) (AllocateHeap((size) * sizeof(type), memflags))504505#define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\506NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)507508#define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\509NEW_C_HEAP_ARRAY2(type, (size), memflags, AllocFailStrategy::RETURN_NULL)510511#define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\512(type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))513514#define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\515(type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))516517#define FREE_C_HEAP_ARRAY(type, old) \518FreeHeap((char*)(old))519520// allocate type in heap without calling ctor521#define NEW_C_HEAP_OBJ(type, memflags)\522NEW_C_HEAP_ARRAY(type, 1, memflags)523524#define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\525NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)526527// deallocate obj of type in heap without calling dtor528#define FREE_C_HEAP_OBJ(objname)\529FreeHeap((char*)objname);530531532//------------------------------ReallocMark---------------------------------533// Code which uses REALLOC_RESOURCE_ARRAY should check an associated534// ReallocMark, which is declared in the same scope as the reallocated535// pointer. Any operation that could __potentially__ cause a reallocation536// should check the ReallocMark.537class ReallocMark: public StackObj {538protected:539NOT_PRODUCT(int _nesting;)540541public:542ReallocMark() PRODUCT_RETURN;543void check() PRODUCT_RETURN;544};545546// Helper class to allocate arrays that may become large.547// Uses the OS malloc for allocations smaller than ArrayAllocatorMallocLimit548// and uses mapped memory for larger allocations.549// Most OS mallocs do something similar but Solaris malloc does not revert550// to mapped memory for large allocations. By default ArrayAllocatorMallocLimit551// is set so that we always use malloc except for Solaris where we set the552// limit to get mapped memory.553template <class E>554class ArrayAllocator : public AllStatic {555private:556static bool should_use_malloc(size_t length);557558static E* allocate_malloc(size_t length, MEMFLAGS flags);559static E* allocate_mmap(size_t length, MEMFLAGS flags);560561static void free_malloc(E* addr, size_t length);562static void free_mmap(E* addr, size_t length);563564public:565static E* allocate(size_t length, MEMFLAGS flags);566static E* reallocate(E* old_addr, size_t old_length, size_t new_length, MEMFLAGS flags);567static void free(E* addr, size_t length);568};569570// Uses mmaped memory for all allocations. All allocations are initially571// zero-filled. No pre-touching.572template <class E>573class MmapArrayAllocator : public AllStatic {574private:575static size_t size_for(size_t length);576577public:578static E* allocate_or_null(size_t length, MEMFLAGS flags);579static E* allocate(size_t length, MEMFLAGS flags);580static void free(E* addr, size_t length);581};582583// Uses malloc:ed memory for all allocations.584template <class E>585class MallocArrayAllocator : public AllStatic {586public:587static size_t size_for(size_t length);588589static E* allocate(size_t length, MEMFLAGS flags);590static void free(E* addr);591};592593#endif // SHARE_MEMORY_ALLOCATION_HPP594595596