1 /* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_MEMORY_ALLOCATION_HPP 26 #define SHARE_VM_MEMORY_ALLOCATION_HPP 27 28 #include "runtime/globals.hpp" 29 #include "utilities/globalDefinitions.hpp" 30 #include "utilities/macros.hpp" 31 #ifdef COMPILER1 32 #include "c1/c1_globals.hpp" 33 #endif 34 #ifdef COMPILER2 35 #include "opto/c2_globals.hpp" 36 #endif 37 38 #include <new> 39 40 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1) 41 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1)) 42 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK) 43 44 class AllocFailStrategy { 45 public: 46 enum AllocFailEnum { EXIT_OOM, RETURN_NULL }; 47 }; 48 typedef AllocFailStrategy::AllocFailEnum AllocFailType; 49 50 // All classes in the virtual machine must be subclassed 51 // by one of the following allocation classes: 52 // 53 // For objects allocated in the resource area (see resourceArea.hpp). 54 // - ResourceObj 55 // 56 // For objects allocated in the C-heap (managed by: free & malloc). 57 // - CHeapObj 58 // 59 // For objects allocated on the stack. 60 // - StackObj 61 // 62 // For embedded objects. 63 // - ValueObj 64 // 65 // For classes used as name spaces. 66 // - AllStatic 67 // 68 // For classes in Metaspace (class data) 69 // - MetaspaceObj 70 // 71 // The printable subclasses are used for debugging and define virtual 72 // member functions for printing. Classes that avoid allocating the 73 // vtbl entries in the objects should therefore not be the printable 74 // subclasses. 75 // 76 // The following macros and function should be used to allocate memory 77 // directly in the resource area or in the C-heap, The _OBJ variants 78 // of the NEW/FREE_C_HEAP macros are used for alloc/dealloc simple 79 // objects which are not inherited from CHeapObj, note constructor and 80 // destructor are not called. The preferable way to allocate objects 81 // is using the new operator. 82 // 83 // WARNING: The array variant must only be used for a homogenous array 84 // where all objects are of the exact type specified. If subtypes are 85 // stored in the array then must pay attention to calling destructors 86 // at needed. 87 // 88 // NEW_RESOURCE_ARRAY(type, size) 89 // NEW_RESOURCE_OBJ(type) 90 // NEW_C_HEAP_ARRAY(type, size) 91 // NEW_C_HEAP_OBJ(type, memflags) 92 // FREE_C_HEAP_ARRAY(type, old) 93 // FREE_C_HEAP_OBJ(objname, type, memflags) 94 // char* AllocateHeap(size_t size, const char* name); 95 // void FreeHeap(void* p); 96 // 97 // C-heap allocation can be traced using +PrintHeapAllocation. 98 // malloc and free should therefore never called directly. 99 100 // Base class for objects allocated in the C-heap. 101 102 // In non product mode we introduce a super class for all allocation classes 103 // that supports printing. 104 // We avoid the superclass in product mode since some C++ compilers add 105 // a word overhead for empty super classes. 106 107 #ifdef PRODUCT 108 #define ALLOCATION_SUPER_CLASS_SPEC 109 #else 110 #define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj 111 class AllocatedObj { 112 public: 113 // Printing support 114 void print() const; 115 void print_value() const; 116 117 virtual void print_on(outputStream* st) const; 118 virtual void print_value_on(outputStream* st) const; 119 }; 120 #endif 121 122 123 /* 124 * Memory types 125 */ 126 enum MemoryType { 127 // Memory type by sub systems. It occupies lower byte. 128 mtJavaHeap = 0x00, // Java heap 129 mtClass = 0x01, // memory class for Java classes 130 mtThread = 0x02, // memory for thread objects 131 mtThreadStack = 0x03, 132 mtCode = 0x04, // memory for generated code 133 mtGC = 0x05, // memory for GC 134 mtCompiler = 0x06, // memory for compiler 135 mtInternal = 0x07, // memory used by VM, but does not belong to 136 // any of above categories, and not used for 137 // native memory tracking 138 mtOther = 0x08, // memory not used by VM 139 mtSymbol = 0x09, // symbol 140 mtNMT = 0x0A, // memory used by native memory tracking 141 mtClassShared = 0x0B, // class data sharing 142 mtChunk = 0x0C, // chunk that holds content of arenas 143 mtTest = 0x0D, // Test type for verifying NMT 144 mtTracing = 0x0E, // memory used for Tracing 145 mtLogging = 0x0F, // memory for logging 146 mtArguments = 0x10, // memory for argument processing 147 mtModule = 0x11, // memory for module processing 148 mtNone = 0x12, // undefined 149 mt_number_of_types = 0x13 // number of memory types (mtDontTrack 150 // is not included as validate type) 151 }; 152 153 typedef MemoryType MEMFLAGS; 154 155 156 #if INCLUDE_NMT 157 158 extern bool NMT_track_callsite; 159 160 #else 161 162 const bool NMT_track_callsite = false; 163 164 #endif // INCLUDE_NMT 165 166 class NativeCallStack; 167 168 169 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { 170 public: 171 NOINLINE void* operator new(size_t size, const NativeCallStack& stack) throw(); 172 NOINLINE void* operator new(size_t size) throw(); 173 NOINLINE void* operator new (size_t size, const std::nothrow_t& nothrow_constant, 174 const NativeCallStack& stack) throw(); 175 NOINLINE void* operator new (size_t size, const std::nothrow_t& nothrow_constant) 176 throw(); 177 NOINLINE void* operator new [](size_t size, const NativeCallStack& stack) throw(); 178 NOINLINE void* operator new [](size_t size) throw(); 179 NOINLINE void* operator new [](size_t size, const std::nothrow_t& nothrow_constant, 180 const NativeCallStack& stack) throw(); 181 NOINLINE void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) 182 throw(); 183 void operator delete(void* p); 184 void operator delete [] (void* p); 185 }; 186 187 // Base class for objects allocated on the stack only. 188 // Calling new or delete will result in fatal error. 189 190 class StackObj ALLOCATION_SUPER_CLASS_SPEC { 191 private: 192 void* operator new(size_t size) throw(); 193 void* operator new [](size_t size) throw(); 194 #ifdef __IBMCPP__ 195 public: 196 #endif 197 void operator delete(void* p); 198 void operator delete [](void* p); 199 }; 200 201 // Base class for objects used as value objects. 202 // Calling new or delete will result in fatal error. 203 // 204 // Portability note: Certain compilers (e.g. gcc) will 205 // always make classes bigger if it has a superclass, even 206 // if the superclass does not have any virtual methods or 207 // instance fields. The HotSpot implementation relies on this 208 // not to happen. So never make a ValueObj class a direct subclass 209 // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g., 210 // like this: 211 // 212 // class A VALUE_OBJ_CLASS_SPEC { 213 // ... 214 // } 215 // 216 // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can 217 // be defined as a an empty string "". 218 // 219 class _ValueObj { 220 private: 221 void* operator new(size_t size) throw(); 222 void operator delete(void* p); 223 void* operator new [](size_t size) throw(); 224 void operator delete [](void* p); 225 }; 226 227 228 // Base class for objects stored in Metaspace. 229 // Calling delete will result in fatal error. 230 // 231 // Do not inherit from something with a vptr because this class does 232 // not introduce one. This class is used to allocate both shared read-only 233 // and shared read-write classes. 234 // 235 236 class ClassLoaderData; 237 238 class MetaspaceObj { 239 public: 240 bool is_metaspace_object() const; 241 bool is_shared() const; 242 void print_address_on(outputStream* st) const; // nonvirtual address printing 243 244 #define METASPACE_OBJ_TYPES_DO(f) \ 245 f(Unknown) \ 246 f(Class) \ 247 f(Symbol) \ 248 f(TypeArrayU1) \ 249 f(TypeArrayU2) \ 250 f(TypeArrayU4) \ 251 f(TypeArrayU8) \ 252 f(TypeArrayOther) \ 253 f(Method) \ 254 f(ConstMethod) \ 255 f(MethodData) \ 256 f(ConstantPool) \ 257 f(ConstantPoolCache) \ 258 f(Annotation) \ 259 f(MethodCounters) \ 260 f(Deallocated) 261 262 #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type, 263 #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name; 264 265 enum Type { 266 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc 267 METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE) 268 _number_of_types 269 }; 270 271 static const char * type_name(Type type) { 272 switch(type) { 273 METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE) 274 default: 275 ShouldNotReachHere(); 276 return NULL; 277 } 278 } 279 280 static MetaspaceObj::Type array_type(size_t elem_size) { 281 switch (elem_size) { 282 case 1: return TypeArrayU1Type; 283 case 2: return TypeArrayU2Type; 284 case 4: return TypeArrayU4Type; 285 case 8: return TypeArrayU8Type; 286 default: 287 return TypeArrayOtherType; 288 } 289 } 290 291 void* operator new(size_t size, ClassLoaderData* loader_data, 292 size_t word_size, bool read_only, 293 Type type, Thread* thread) throw(); 294 // can't use TRAPS from this header file. 295 void operator delete(void* p) { ShouldNotCallThis(); } 296 }; 297 298 // Base class for classes that constitute name spaces. 299 300 class AllStatic { 301 public: 302 AllStatic() { ShouldNotCallThis(); } 303 ~AllStatic() { ShouldNotCallThis(); } 304 }; 305 306 307 //------------------------------Chunk------------------------------------------ 308 // Linked list of raw memory chunks 309 class Chunk: CHeapObj<mtChunk> { 310 friend class VMStructs; 311 312 protected: 313 Chunk* _next; // Next Chunk in list 314 const size_t _len; // Size of this Chunk 315 public: 316 void* operator new(size_t size, AllocFailType alloc_failmode, size_t length) throw(); 317 void operator delete(void* p); 318 Chunk(size_t length); 319 320 enum { 321 // default sizes; make them slightly smaller than 2**k to guard against 322 // buddy-system style malloc implementations 323 #ifdef _LP64 324 slack = 40, // [RGV] Not sure if this is right, but make it 325 // a multiple of 8. 326 #else 327 slack = 20, // suspected sizeof(Chunk) + internal malloc headers 328 #endif 329 330 tiny_size = 256 - slack, // Size of first chunk (tiny) 331 init_size = 1*K - slack, // Size of first chunk (normal aka small) 332 medium_size= 10*K - slack, // Size of medium-sized chunk 333 size = 32*K - slack, // Default size of an Arena chunk (following the first) 334 non_pool_size = init_size + 32 // An initial size which is not one of above 335 }; 336 337 void chop(); // Chop this chunk 338 void next_chop(); // Chop next chunk 339 static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); } 340 static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); } 341 342 size_t length() const { return _len; } 343 Chunk* next() const { return _next; } 344 void set_next(Chunk* n) { _next = n; } 345 // Boundaries of data area (possibly unused) 346 char* bottom() const { return ((char*) this) + aligned_overhead_size(); } 347 char* top() const { return bottom() + _len; } 348 bool contains(char* p) const { return bottom() <= p && p <= top(); } 349 350 // Start the chunk_pool cleaner task 351 static void start_chunk_pool_cleaner_task(); 352 353 static void clean_chunk_pool(); 354 }; 355 356 //------------------------------Arena------------------------------------------ 357 // Fast allocation of memory 358 class Arena : public CHeapObj<mtNone> { 359 protected: 360 friend class ResourceMark; 361 friend class HandleMark; 362 friend class NoHandleMark; 363 friend class VMStructs; 364 365 MEMFLAGS _flags; // Memory tracking flags 366 367 Chunk *_first; // First chunk 368 Chunk *_chunk; // current chunk 369 char *_hwm, *_max; // High water mark and max in current chunk 370 // Get a new Chunk of at least size x 371 void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 372 size_t _size_in_bytes; // Size of arena (used for native memory tracking) 373 374 NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start 375 friend class AllocStats; 376 debug_only(void* malloc(size_t size);) 377 debug_only(void* internal_malloc_4(size_t x);) 378 NOT_PRODUCT(void inc_bytes_allocated(size_t x);) 379 380 void signal_out_of_memory(size_t request, const char* whence) const; 381 382 bool check_for_overflow(size_t request, const char* whence, 383 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) const { 384 if (UINTPTR_MAX - request < (uintptr_t)_hwm) { 385 if (alloc_failmode == AllocFailStrategy::RETURN_NULL) { 386 return false; 387 } 388 signal_out_of_memory(request, whence); 389 } 390 return true; 391 } 392 393 public: 394 Arena(MEMFLAGS memflag); 395 Arena(MEMFLAGS memflag, size_t init_size); 396 ~Arena(); 397 void destruct_contents(); 398 char* hwm() const { return _hwm; } 399 400 // new operators 401 void* operator new (size_t size) throw(); 402 void* operator new (size_t size, const std::nothrow_t& nothrow_constant) throw(); 403 404 // dynamic memory type tagging 405 void* operator new(size_t size, MEMFLAGS flags) throw(); 406 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw(); 407 void operator delete(void* p); 408 409 // Fast allocate in the arena. Common case is: pointer test + increment. 410 void* Amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { 411 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2"); 412 x = ARENA_ALIGN(x); 413 debug_only(if (UseMallocOnly) return malloc(x);) 414 if (!check_for_overflow(x, "Arena::Amalloc", alloc_failmode)) 415 return NULL; 416 NOT_PRODUCT(inc_bytes_allocated(x);) 417 if (_hwm + x > _max) { 418 return grow(x, alloc_failmode); 419 } else { 420 char *old = _hwm; 421 _hwm += x; 422 return old; 423 } 424 } 425 // Further assume size is padded out to words 426 void *Amalloc_4(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { 427 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); 428 debug_only(if (UseMallocOnly) return malloc(x);) 429 if (!check_for_overflow(x, "Arena::Amalloc_4", alloc_failmode)) 430 return NULL; 431 NOT_PRODUCT(inc_bytes_allocated(x);) 432 if (_hwm + x > _max) { 433 return grow(x, alloc_failmode); 434 } else { 435 char *old = _hwm; 436 _hwm += x; 437 return old; 438 } 439 } 440 441 // Allocate with 'double' alignment. It is 8 bytes on sparc. 442 // In other cases Amalloc_D() should be the same as Amalloc_4(). 443 void* Amalloc_D(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { 444 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); 445 debug_only(if (UseMallocOnly) return malloc(x);) 446 #if defined(SPARC) && !defined(_LP64) 447 #define DALIGN_M1 7 448 size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm; 449 x += delta; 450 #endif 451 if (!check_for_overflow(x, "Arena::Amalloc_D", alloc_failmode)) 452 return NULL; 453 NOT_PRODUCT(inc_bytes_allocated(x);) 454 if (_hwm + x > _max) { 455 return grow(x, alloc_failmode); // grow() returns a result aligned >= 8 bytes. 456 } else { 457 char *old = _hwm; 458 _hwm += x; 459 #if defined(SPARC) && !defined(_LP64) 460 old += delta; // align to 8-bytes 461 #endif 462 return old; 463 } 464 } 465 466 // Fast delete in area. Common case is: NOP (except for storage reclaimed) 467 void Afree(void *ptr, size_t size) { 468 #ifdef ASSERT 469 if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory 470 if (UseMallocOnly) return; 471 #endif 472 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr; 473 } 474 475 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size, 476 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 477 478 // Move contents of this arena into an empty arena 479 Arena *move_contents(Arena *empty_arena); 480 481 // Determine if pointer belongs to this Arena or not. 482 bool contains( const void *ptr ) const; 483 484 // Total of all chunks in use (not thread-safe) 485 size_t used() const; 486 487 // Total # of bytes used 488 size_t size_in_bytes() const { return _size_in_bytes; }; 489 void set_size_in_bytes(size_t size); 490 491 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN; 492 static void free_all(char** start, char** end) PRODUCT_RETURN; 493 494 private: 495 // Reset this Arena to empty, access will trigger grow if necessary 496 void reset(void) { 497 _first = _chunk = NULL; 498 _hwm = _max = NULL; 499 set_size_in_bytes(0); 500 } 501 }; 502 503 // One of the following macros must be used when allocating 504 // an array or object from an arena 505 #define NEW_ARENA_ARRAY(arena, type, size) \ 506 (type*) (arena)->Amalloc((size) * sizeof(type)) 507 508 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \ 509 (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \ 510 (new_size) * sizeof(type) ) 511 512 #define FREE_ARENA_ARRAY(arena, type, old, size) \ 513 (arena)->Afree((char*)(old), (size) * sizeof(type)) 514 515 #define NEW_ARENA_OBJ(arena, type) \ 516 NEW_ARENA_ARRAY(arena, type, 1) 517 518 519 //%note allocation_1 520 extern char* resource_allocate_bytes(size_t size, 521 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 522 extern char* resource_allocate_bytes(Thread* thread, size_t size, 523 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 524 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size, 525 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 526 extern void resource_free_bytes( char *old, size_t size ); 527 528 //---------------------------------------------------------------------- 529 // Base class for objects allocated in the resource area per default. 530 // Optionally, objects may be allocated on the C heap with 531 // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena) 532 // ResourceObj's can be allocated within other objects, but don't use 533 // new or delete (allocation_type is unknown). If new is used to allocate, 534 // use delete to deallocate. 535 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC { 536 public: 537 enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 }; 538 static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN; 539 #ifdef ASSERT 540 private: 541 // When this object is allocated on stack the new() operator is not 542 // called but garbage on stack may look like a valid allocation_type. 543 // Store negated 'this' pointer when new() is called to distinguish cases. 544 // Use second array's element for verification value to distinguish garbage. 545 uintptr_t _allocation_t[2]; 546 bool is_type_set() const; 547 public: 548 allocation_type get_allocation_type() const; 549 bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; } 550 bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; } 551 bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; } 552 bool allocated_on_arena() const { return get_allocation_type() == ARENA; } 553 ResourceObj(); // default constructor 554 ResourceObj(const ResourceObj& r); // default copy constructor 555 ResourceObj& operator=(const ResourceObj& r); // default copy assignment 556 ~ResourceObj(); 557 #endif // ASSERT 558 559 public: 560 void* operator new(size_t size, allocation_type type, MEMFLAGS flags) throw(); 561 void* operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw(); 562 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, 563 allocation_type type, MEMFLAGS flags) throw(); 564 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant, 565 allocation_type type, MEMFLAGS flags) throw(); 566 567 void* operator new(size_t size, Arena *arena) throw() { 568 address res = (address)arena->Amalloc(size); 569 DEBUG_ONLY(set_allocation_type(res, ARENA);) 570 return res; 571 } 572 573 void* operator new [](size_t size, Arena *arena) throw() { 574 address res = (address)arena->Amalloc(size); 575 DEBUG_ONLY(set_allocation_type(res, ARENA);) 576 return res; 577 } 578 579 void* operator new(size_t size) throw() { 580 address res = (address)resource_allocate_bytes(size); 581 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) 582 return res; 583 } 584 585 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() { 586 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); 587 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) 588 return res; 589 } 590 591 void* operator new [](size_t size) throw() { 592 address res = (address)resource_allocate_bytes(size); 593 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) 594 return res; 595 } 596 597 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() { 598 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); 599 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) 600 return res; 601 } 602 603 void operator delete(void* p); 604 void operator delete [](void* p); 605 }; 606 607 // One of the following macros must be used when allocating an array 608 // or object to determine whether it should reside in the C heap on in 609 // the resource area. 610 611 #define NEW_RESOURCE_ARRAY(type, size)\ 612 (type*) resource_allocate_bytes((size) * sizeof(type)) 613 614 #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\ 615 (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL) 616 617 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\ 618 (type*) resource_allocate_bytes(thread, (size) * sizeof(type)) 619 620 #define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\ 621 (type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL) 622 623 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\ 624 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type)) 625 626 #define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\ 627 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\ 628 (new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL) 629 630 #define FREE_RESOURCE_ARRAY(type, old, size)\ 631 resource_free_bytes((char*)(old), (size) * sizeof(type)) 632 633 #define FREE_FAST(old)\ 634 /* nop */ 635 636 #define NEW_RESOURCE_OBJ(type)\ 637 NEW_RESOURCE_ARRAY(type, 1) 638 639 #define NEW_RESOURCE_OBJ_RETURN_NULL(type)\ 640 NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1) 641 642 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\ 643 (type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail) 644 645 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ 646 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) 647 648 #define NEW_C_HEAP_ARRAY(type, size, memflags)\ 649 (type*) (AllocateHeap((size) * sizeof(type), memflags)) 650 651 #define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\ 652 NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL) 653 654 #define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\ 655 NEW_C_HEAP_ARRAY3(type, (size), memflags, CURRENT_PC, AllocFailStrategy::RETURN_NULL) 656 657 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ 658 (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags)) 659 660 #define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\ 661 (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL)) 662 663 #define FREE_C_HEAP_ARRAY(type, old) \ 664 FreeHeap((char*)(old)) 665 666 // allocate type in heap without calling ctor 667 #define NEW_C_HEAP_OBJ(type, memflags)\ 668 NEW_C_HEAP_ARRAY(type, 1, memflags) 669 670 #define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\ 671 NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags) 672 673 // deallocate obj of type in heap without calling dtor 674 #define FREE_C_HEAP_OBJ(objname)\ 675 FreeHeap((char*)objname); 676 677 // for statistics 678 #ifndef PRODUCT 679 class AllocStats : StackObj { 680 julong start_mallocs, start_frees; 681 julong start_malloc_bytes, start_mfree_bytes, start_res_bytes; 682 public: 683 AllocStats(); 684 685 julong num_mallocs(); // since creation of receiver 686 julong alloc_bytes(); 687 julong num_frees(); 688 julong free_bytes(); 689 julong resource_bytes(); 690 void print(); 691 }; 692 #endif 693 694 695 //------------------------------ReallocMark--------------------------------- 696 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated 697 // ReallocMark, which is declared in the same scope as the reallocated 698 // pointer. Any operation that could __potentially__ cause a reallocation 699 // should check the ReallocMark. 700 class ReallocMark: public StackObj { 701 protected: 702 NOT_PRODUCT(int _nesting;) 703 704 public: 705 ReallocMark() PRODUCT_RETURN; 706 void check() PRODUCT_RETURN; 707 }; 708 709 // Helper class to allocate arrays that may become large. 710 // Uses the OS malloc for allocations smaller than ArrayAllocatorMallocLimit 711 // and uses mapped memory for larger allocations. 712 // Most OS mallocs do something similar but Solaris malloc does not revert 713 // to mapped memory for large allocations. By default ArrayAllocatorMallocLimit 714 // is set so that we always use malloc except for Solaris where we set the 715 // limit to get mapped memory. 716 template <class E> 717 class ArrayAllocator : public AllStatic { 718 private: 719 static bool should_use_malloc(size_t length); 720 721 static E* allocate_malloc(size_t length, MEMFLAGS flags); 722 static E* allocate_mmap(size_t length, MEMFLAGS flags); 723 724 static void free_malloc(E* addr, size_t length); 725 static void free_mmap(E* addr, size_t length); 726 727 public: 728 static E* allocate(size_t length, MEMFLAGS flags); 729 static E* reallocate(E* old_addr, size_t old_length, size_t new_length, MEMFLAGS flags); 730 static void free(E* addr, size_t length); 731 }; 732 733 // Uses mmaped memory for all allocations. All allocations are initially 734 // zero-filled. No pre-touching. 735 template <class E> 736 class MmapArrayAllocator : public AllStatic { 737 private: 738 static size_t size_for(size_t length); 739 740 public: 741 static E* allocate_or_null(size_t length, MEMFLAGS flags); 742 static E* allocate(size_t length, MEMFLAGS flags); 743 static void free(E* addr, size_t length); 744 }; 745 746 // Uses malloc:ed memory for all allocations. 747 template <class E> 748 class MallocArrayAllocator : public AllStatic { 749 public: 750 static size_t size_for(size_t length); 751 752 static E* allocate(size_t length, MEMFLAGS flags); 753 static void free(E* addr, size_t length); 754 }; 755 756 #endif // SHARE_VM_MEMORY_ALLOCATION_HPP