Path: blob/master/src/hotspot/share/memory/allocation.hpp
40950 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(mtNone, "Unknown") \148//end149150#define MEMORY_TYPE_DECLARE_ENUM(type, human_readable) \151type,152153/*154* Memory types155*/156enum class MEMFLAGS {157MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_ENUM)158mt_number_of_types // number of memory types (mtDontTrack159// is not included as validate type)160};161162#define MEMORY_TYPE_SHORTNAME(type, human_readable) \163constexpr MEMFLAGS type = MEMFLAGS::type;164165// Generate short aliases for the enum values. E.g. mtGC instead of MEMFLAGS::mtGC.166MEMORY_TYPES_DO(MEMORY_TYPE_SHORTNAME)167168// Make an int version of the sentinel end value.169constexpr int mt_number_of_types = static_cast<int>(MEMFLAGS::mt_number_of_types);170171#if INCLUDE_NMT172173extern bool NMT_track_callsite;174175#else176177const bool NMT_track_callsite = false;178179#endif // INCLUDE_NMT180181class NativeCallStack;182183184char* AllocateHeap(size_t size,185MEMFLAGS flags,186const NativeCallStack& stack,187AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);188char* AllocateHeap(size_t size,189MEMFLAGS flags,190AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);191192char* ReallocateHeap(char *old,193size_t size,194MEMFLAGS flag,195AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);196197// handles NULL pointers198void FreeHeap(void* p);199200template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {201public:202ALWAYSINLINE void* operator new(size_t size) throw() {203return (void*)AllocateHeap(size, F);204}205206ALWAYSINLINE void* operator new(size_t size,207const NativeCallStack& stack) throw() {208return (void*)AllocateHeap(size, F, stack);209}210211ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t&,212const NativeCallStack& stack) throw() {213return (void*)AllocateHeap(size, F, stack, AllocFailStrategy::RETURN_NULL);214}215216ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t&) throw() {217return (void*)AllocateHeap(size, F, AllocFailStrategy::RETURN_NULL);218}219220ALWAYSINLINE void* operator new[](size_t size) throw() {221return (void*)AllocateHeap(size, F);222}223224ALWAYSINLINE void* operator new[](size_t size,225const NativeCallStack& stack) throw() {226return (void*)AllocateHeap(size, F, stack);227}228229ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t&,230const NativeCallStack& stack) throw() {231return (void*)AllocateHeap(size, F, stack, AllocFailStrategy::RETURN_NULL);232}233234ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t&) throw() {235return (void*)AllocateHeap(size, F, AllocFailStrategy::RETURN_NULL);236}237238void operator delete(void* p) { FreeHeap(p); }239void operator delete [] (void* p) { FreeHeap(p); }240};241242// Base class for objects allocated on the stack only.243// Calling new or delete will result in fatal error.244245class StackObj ALLOCATION_SUPER_CLASS_SPEC {246private:247void* operator new(size_t size) throw();248void* operator new [](size_t size) throw();249void operator delete(void* p);250void operator delete [](void* p);251};252253// Base class for objects stored in Metaspace.254// Calling delete will result in fatal error.255//256// Do not inherit from something with a vptr because this class does257// not introduce one. This class is used to allocate both shared read-only258// and shared read-write classes.259//260261class ClassLoaderData;262class MetaspaceClosure;263264class MetaspaceObj {265friend class VMStructs;266// When CDS is enabled, all shared metaspace objects are mapped267// into a single contiguous memory block, so we can use these268// two pointers to quickly determine if something is in the269// shared metaspace.270// When CDS is not enabled, both pointers are set to NULL.271static void* _shared_metaspace_base; // (inclusive) low address272static void* _shared_metaspace_top; // (exclusive) high address273274public:275276// Returns true if the pointer points to a valid MetaspaceObj. A valid277// MetaspaceObj is MetaWord-aligned and contained within either278// non-shared or shared metaspace.279static bool is_valid(const MetaspaceObj* p);280281static bool is_shared(const MetaspaceObj* p) {282// If no shared metaspace regions are mapped, _shared_metaspace_{base,top} will283// both be NULL and all values of p will be rejected quickly.284return (((void*)p) < _shared_metaspace_top &&285((void*)p) >= _shared_metaspace_base);286}287bool is_shared() const { return MetaspaceObj::is_shared(this); }288289void print_address_on(outputStream* st) const; // nonvirtual address printing290291static void set_shared_metaspace_range(void* base, void* top) {292_shared_metaspace_base = base;293_shared_metaspace_top = top;294}295296static void* shared_metaspace_base() { return _shared_metaspace_base; }297static void* shared_metaspace_top() { return _shared_metaspace_top; }298299#define METASPACE_OBJ_TYPES_DO(f) \300f(Class) \301f(Symbol) \302f(TypeArrayU1) \303f(TypeArrayU2) \304f(TypeArrayU4) \305f(TypeArrayU8) \306f(TypeArrayOther) \307f(Method) \308f(ConstMethod) \309f(MethodData) \310f(ConstantPool) \311f(ConstantPoolCache) \312f(Annotations) \313f(MethodCounters) \314f(RecordComponent)315316#define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,317#define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;318319enum Type {320// Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc321METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)322_number_of_types323};324325static const char * type_name(Type type) {326switch(type) {327METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)328default:329ShouldNotReachHere();330return NULL;331}332}333334static MetaspaceObj::Type array_type(size_t elem_size) {335switch (elem_size) {336case 1: return TypeArrayU1Type;337case 2: return TypeArrayU2Type;338case 4: return TypeArrayU4Type;339case 8: return TypeArrayU8Type;340default:341return TypeArrayOtherType;342}343}344345void* operator new(size_t size, ClassLoaderData* loader_data,346size_t word_size,347Type type, JavaThread* thread) throw();348// can't use TRAPS from this header file.349void* operator new(size_t size, ClassLoaderData* loader_data,350size_t word_size,351Type type) throw();352void operator delete(void* p) { ShouldNotCallThis(); }353354// Declare a *static* method with the same signature in any subclass of MetaspaceObj355// that should be read-only by default. See symbol.hpp for an example. This function356// is used by the templates in metaspaceClosure.hpp357static bool is_read_only_by_default() { return false; }358};359360// Base class for classes that constitute name spaces.361362class Arena;363364extern char* resource_allocate_bytes(size_t size,365AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);366extern char* resource_allocate_bytes(Thread* thread, size_t size,367AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);368extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,369AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);370extern void resource_free_bytes( char *old, size_t size );371372//----------------------------------------------------------------------373// Base class for objects allocated in the resource area per default.374// Optionally, objects may be allocated on the C heap with375// new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)376// ResourceObj's can be allocated within other objects, but don't use377// new or delete (allocation_type is unknown). If new is used to allocate,378// use delete to deallocate.379class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {380public:381enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };382static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;383#ifdef ASSERT384private:385// When this object is allocated on stack the new() operator is not386// called but garbage on stack may look like a valid allocation_type.387// Store negated 'this' pointer when new() is called to distinguish cases.388// Use second array's element for verification value to distinguish garbage.389uintptr_t _allocation_t[2];390bool is_type_set() const;391void initialize_allocation_info();392public:393allocation_type get_allocation_type() const;394bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; }395bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }396bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; }397bool allocated_on_arena() const { return get_allocation_type() == ARENA; }398protected:399ResourceObj(); // default constructor400ResourceObj(const ResourceObj& r); // default copy constructor401ResourceObj& operator=(const ResourceObj& r); // default copy assignment402~ResourceObj();403#endif // ASSERT404405public:406void* operator new(size_t size, allocation_type type, MEMFLAGS flags) throw();407void* operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw();408void* operator new(size_t size, const std::nothrow_t& nothrow_constant,409allocation_type type, MEMFLAGS flags) throw();410void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,411allocation_type type, MEMFLAGS flags) throw();412413void* operator new(size_t size, Arena *arena) throw();414415void* operator new [](size_t size, Arena *arena) throw();416417void* operator new(size_t size) throw() {418address res = (address)resource_allocate_bytes(size);419DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)420return res;421}422423void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {424address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);425DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)426return res;427}428429void* operator new [](size_t size) throw() {430address res = (address)resource_allocate_bytes(size);431DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)432return res;433}434435void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() {436address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);437DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)438return res;439}440441void operator delete(void* p);442void operator delete [](void* p);443};444445// One of the following macros must be used when allocating an array446// or object to determine whether it should reside in the C heap on in447// the resource area.448449#define NEW_RESOURCE_ARRAY(type, size)\450(type*) resource_allocate_bytes((size) * sizeof(type))451452#define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\453(type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)454455#define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\456(type*) resource_allocate_bytes(thread, (size) * sizeof(type))457458#define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\459(type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)460461#define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\462(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))463464#define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\465(type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\466(new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)467468#define FREE_RESOURCE_ARRAY(type, old, size)\469resource_free_bytes((char*)(old), (size) * sizeof(type))470471#define FREE_FAST(old)\472/* nop */473474#define NEW_RESOURCE_OBJ(type)\475NEW_RESOURCE_ARRAY(type, 1)476477#define NEW_RESOURCE_OBJ_RETURN_NULL(type)\478NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)479480#define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\481(type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)482483#define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\484(type*) (AllocateHeap((size) * sizeof(type), memflags, pc))485486#define NEW_C_HEAP_ARRAY(type, size, memflags)\487(type*) (AllocateHeap((size) * sizeof(type), memflags))488489#define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\490NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)491492#define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\493NEW_C_HEAP_ARRAY2(type, (size), memflags, AllocFailStrategy::RETURN_NULL)494495#define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\496(type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))497498#define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\499(type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))500501#define FREE_C_HEAP_ARRAY(type, old) \502FreeHeap((char*)(old))503504// allocate type in heap without calling ctor505#define NEW_C_HEAP_OBJ(type, memflags)\506NEW_C_HEAP_ARRAY(type, 1, memflags)507508#define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\509NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)510511// deallocate obj of type in heap without calling dtor512#define FREE_C_HEAP_OBJ(objname)\513FreeHeap((char*)objname);514515516//------------------------------ReallocMark---------------------------------517// Code which uses REALLOC_RESOURCE_ARRAY should check an associated518// ReallocMark, which is declared in the same scope as the reallocated519// pointer. Any operation that could __potentially__ cause a reallocation520// should check the ReallocMark.521class ReallocMark: public StackObj {522protected:523NOT_PRODUCT(int _nesting;)524525public:526ReallocMark() PRODUCT_RETURN;527void check() PRODUCT_RETURN;528};529530// Helper class to allocate arrays that may become large.531// Uses the OS malloc for allocations smaller than ArrayAllocatorMallocLimit532// and uses mapped memory for larger allocations.533// Most OS mallocs do something similar but Solaris malloc does not revert534// to mapped memory for large allocations. By default ArrayAllocatorMallocLimit535// is set so that we always use malloc except for Solaris where we set the536// limit to get mapped memory.537template <class E>538class ArrayAllocator : public AllStatic {539private:540static bool should_use_malloc(size_t length);541542static E* allocate_malloc(size_t length, MEMFLAGS flags);543static E* allocate_mmap(size_t length, MEMFLAGS flags);544545static void free_malloc(E* addr, size_t length);546static void free_mmap(E* addr, size_t length);547548public:549static E* allocate(size_t length, MEMFLAGS flags);550static E* reallocate(E* old_addr, size_t old_length, size_t new_length, MEMFLAGS flags);551static void free(E* addr, size_t length);552};553554// Uses mmaped memory for all allocations. All allocations are initially555// zero-filled. No pre-touching.556template <class E>557class MmapArrayAllocator : public AllStatic {558private:559static size_t size_for(size_t length);560561public:562static E* allocate_or_null(size_t length, MEMFLAGS flags);563static E* allocate(size_t length, MEMFLAGS flags);564static void free(E* addr, size_t length);565};566567// Uses malloc:ed memory for all allocations.568template <class E>569class MallocArrayAllocator : public AllStatic {570public:571static size_t size_for(size_t length);572573static E* allocate(size_t length, MEMFLAGS flags);574static void free(E* addr);575};576577#endif // SHARE_MEMORY_ALLOCATION_HPP578579580