1 /* 2 * Copyright (c) 1997, 2012, 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 45 // noinline attribute 46 #ifdef _WINDOWS 47 #define _NOINLINE_ __declspec(noinline) 48 #else 49 #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute 50 #define _NOINLINE_ 51 #else 52 #define _NOINLINE_ __attribute__ ((noinline)) 53 #endif 54 #endif 55 56 // All classes in the virtual machine must be subclassed 57 // by one of the following allocation classes: 58 // 59 // For objects allocated in the resource area (see resourceArea.hpp). 60 // - ResourceObj 61 // 62 // For objects allocated in the C-heap (managed by: free & malloc). 63 // - CHeapObj 64 // 65 // For objects allocated on the stack. 66 // - StackObj 67 // 68 // For embedded objects. 69 // - ValueObj 70 // 71 // For classes used as name spaces. 72 // - AllStatic 73 // 74 // For classes in Metaspace (class data) 75 // - MetaspaceObj 76 // 77 // The printable subclasses are used for debugging and define virtual 78 // member functions for printing. Classes that avoid allocating the 79 // vtbl entries in the objects should therefore not be the printable 80 // subclasses. 81 // 82 // The following macros and function should be used to allocate memory 83 // directly in the resource area or in the C-heap: 84 // 85 // NEW_RESOURCE_ARRAY(type,size) 86 // NEW_RESOURCE_OBJ(type) 87 // NEW_C_HEAP_ARRAY(type,size) 88 // NEW_C_HEAP_OBJ(type) 89 // char* AllocateHeap(size_t size, const char* name); 90 // void FreeHeap(void* p); 91 // 92 // C-heap allocation can be traced using +PrintHeapAllocation. 93 // malloc and free should therefore never called directly. 94 95 // Base class for objects allocated in the C-heap. 96 97 // In non product mode we introduce a super class for all allocation classes 98 // that supports printing. 99 // We avoid the superclass in product mode since some C++ compilers add 100 // a word overhead for empty super classes. 101 102 #ifdef PRODUCT 103 #define ALLOCATION_SUPER_CLASS_SPEC 104 #else 105 #define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj 106 class AllocatedObj { 107 public: 108 // Printing support 109 void print() const; 110 void print_value() const; 111 112 virtual void print_on(outputStream* st) const; 113 virtual void print_value_on(outputStream* st) const; 114 }; 115 #endif 116 117 118 /* 119 * MemoryType bitmap layout: 120 * | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 | 121 * | memory type | object | reserved | 122 * | | type | | 123 */ 124 enum MemoryType { 125 // Memory type by sub systems. It occupies lower byte. 126 mtNone = 0x0000, // undefined 127 mtClass = 0x0100, // memory class for Java classes 128 mtThread = 0x0200, // memory for thread objects 129 mtThreadStack = 0x0300, 130 mtCode = 0x0400, // memory for generated code 131 mtGC = 0x0500, // memory for GC 132 mtCompiler = 0x0600, // memory for compiler 133 mtInternal = 0x0700, // memory used by VM, but does not belong to 134 // any of above categories, and not used for 135 // native memory tracking 136 mtOther = 0x0800, // memory not used by VM 137 mtSymbol = 0x0900, // symbol 138 mtNMT = 0x0A00, // memory used by native memory tracking 139 mtChunk = 0x0B00, // chunk that holds content of arenas 140 mtJavaHeap = 0x0C00, // Java heap 141 mtDontTrack = 0x0D00, // memory we donot or cannot track 142 mt_number_of_types = 0x000C, // number of memory types 143 mt_masks = 0x7F00, 144 145 // object type mask 146 otArena = 0x0010, // an arena object 147 otNMTRecorder = 0x0020, // memory recorder object 148 ot_masks = 0x00F0 149 }; 150 151 #if INCLUDE_NMT 152 #define IS_MEMORY_TYPE(flags, type) ((flags & mt_masks) == type) 153 #define HAS_VALID_MEMORY_TYPE(flags)((flags & mt_masks) != mtNone) 154 #define FLAGS_TO_MEMORY_TYPE(flags) (flags & mt_masks) 155 156 #define IS_ARENA_OBJ(flags) ((flags & ot_masks) == otArena) 157 #define IS_NMT_RECORDER(flags) ((flags & ot_masks) == otNMTRecorder) 158 #define NMT_CAN_TRACK(flags) (!IS_NMT_RECORDER(flags) && !(IS_MEMORY_TYPE(flags, mtDontTrack))) 159 #endif // INCLUDE_NMT 160 161 typedef unsigned short MEMFLAGS; 162 163 #if INCLUDE_NMT 164 165 extern bool NMT_track_callsite; 166 167 #else 168 169 const bool NMT_track_callsite = false; 170 171 #endif // INCLUDE_NMT 172 173 // debug build does not inline 174 #if defined(_DEBUG_) 175 #define CURRENT_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0) 176 #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0) 177 #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(3) : 0) 178 #else 179 #define CURRENT_PC (NMT_track_callsite? os::get_caller_pc(0) : 0) 180 #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0) 181 #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0) 182 #endif 183 184 185 186 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { 187 public: 188 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); 189 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, 190 address caller_pc = 0); 191 192 void operator delete(void* p); 193 }; 194 195 // Base class for objects allocated on the stack only. 196 // Calling new or delete will result in fatal error. 197 198 class StackObj ALLOCATION_SUPER_CLASS_SPEC { 199 public: 200 void* operator new(size_t size); 201 void operator delete(void* p); 202 }; 203 204 // Base class for objects used as value objects. 205 // Calling new or delete will result in fatal error. 206 // 207 // Portability note: Certain compilers (e.g. gcc) will 208 // always make classes bigger if it has a superclass, even 209 // if the superclass does not have any virtual methods or 210 // instance fields. The HotSpot implementation relies on this 211 // not to happen. So never make a ValueObj class a direct subclass 212 // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g., 213 // like this: 214 // 215 // class A VALUE_OBJ_CLASS_SPEC { 216 // ... 217 // } 218 // 219 // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can 220 // be defined as a an empty string "". 221 // 222 class _ValueObj { 223 public: 224 void* operator new(size_t size); 225 void operator delete(void* p); 226 }; 227 228 229 // Base class for objects stored in Metaspace. 230 // Calling delete will result in fatal error. 231 // 232 // Do not inherit from something with a vptr because this class does 233 // not introduce one. This class is used to allocate both shared read-only 234 // and shared read-write classes. 235 // 236 237 class ClassLoaderData; 238 239 class MetaspaceObj { 240 public: 241 bool is_metadata() const; 242 bool is_shared() const; 243 void print_address_on(outputStream* st) const; // nonvirtual address printing 244 245 void* operator new(size_t size, ClassLoaderData* loader_data, 246 size_t word_size, bool read_only, Thread* thread); 247 // can't use TRAPS from this header file. 248 void operator delete(void* p) { ShouldNotCallThis(); } 249 }; 250 251 // Base class for classes that constitute name spaces. 252 253 class AllStatic { 254 public: 255 AllStatic() { ShouldNotCallThis(); } 256 ~AllStatic() { ShouldNotCallThis(); } 257 }; 258 259 260 //------------------------------Chunk------------------------------------------ 261 // Linked list of raw memory chunks 262 class Chunk: CHeapObj<mtChunk> { 263 friend class VMStructs; 264 265 protected: 266 Chunk* _next; // Next Chunk in list 267 const size_t _len; // Size of this Chunk 268 public: 269 void* operator new(size_t size, size_t length); 270 void operator delete(void* p); 271 Chunk(size_t length); 272 273 enum { 274 // default sizes; make them slightly smaller than 2**k to guard against 275 // buddy-system style malloc implementations 276 #ifdef _LP64 277 slack = 40, // [RGV] Not sure if this is right, but make it 278 // a multiple of 8. 279 #else 280 slack = 20, // suspected sizeof(Chunk) + internal malloc headers 281 #endif 282 283 init_size = 1*K - slack, // Size of first chunk 284 medium_size= 10*K - slack, // Size of medium-sized chunk 285 size = 32*K - slack, // Default size of an Arena chunk (following the first) 286 non_pool_size = init_size + 32 // An initial size which is not one of above 287 }; 288 289 void chop(); // Chop this chunk 290 void next_chop(); // Chop next chunk 291 static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); } 292 static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); } 293 294 size_t length() const { return _len; } 295 Chunk* next() const { return _next; } 296 void set_next(Chunk* n) { _next = n; } 297 // Boundaries of data area (possibly unused) 298 char* bottom() const { return ((char*) this) + aligned_overhead_size(); } 299 char* top() const { return bottom() + _len; } 300 bool contains(char* p) const { return bottom() <= p && p <= top(); } 301 302 // Start the chunk_pool cleaner task 303 static void start_chunk_pool_cleaner_task(); 304 305 static void clean_chunk_pool(); 306 }; 307 308 //------------------------------Arena------------------------------------------ 309 // Fast allocation of memory 310 class Arena : public CHeapObj<mtNone|otArena> { 311 protected: 312 friend class ResourceMark; 313 friend class HandleMark; 314 friend class NoHandleMark; 315 friend class VMStructs; 316 317 Chunk *_first; // First chunk 318 Chunk *_chunk; // current chunk 319 char *_hwm, *_max; // High water mark and max in current chunk 320 void* grow(size_t x); // Get a new Chunk of at least size x 321 size_t _size_in_bytes; // Size of arena (used for native memory tracking) 322 323 NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start 324 friend class AllocStats; 325 debug_only(void* malloc(size_t size);) 326 debug_only(void* internal_malloc_4(size_t x);) 327 NOT_PRODUCT(void inc_bytes_allocated(size_t x);) 328 329 void signal_out_of_memory(size_t request, const char* whence) const; 330 331 void check_for_overflow(size_t request, const char* whence) const { 332 if (UINTPTR_MAX - request < (uintptr_t)_hwm) { 333 signal_out_of_memory(request, whence); 334 } 335 } 336 337 public: 338 Arena(); 339 Arena(size_t init_size); 340 Arena(Arena *old); 341 ~Arena(); 342 void destruct_contents(); 343 char* hwm() const { return _hwm; } 344 345 // new operators 346 void* operator new (size_t size); 347 void* operator new (size_t size, const std::nothrow_t& nothrow_constant); 348 349 // dynamic memory type tagging 350 void* operator new(size_t size, MEMFLAGS flags); 351 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags); 352 void operator delete(void* p); 353 354 // Fast allocate in the arena. Common case is: pointer test + increment. 355 void* Amalloc(size_t x) { 356 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2"); 357 x = ARENA_ALIGN(x); 358 debug_only(if (UseMallocOnly) return malloc(x);) 359 check_for_overflow(x, "Arena::Amalloc"); 360 NOT_PRODUCT(inc_bytes_allocated(x);) 361 if (_hwm + x > _max) { 362 return grow(x); 363 } else { 364 char *old = _hwm; 365 _hwm += x; 366 return old; 367 } 368 } 369 // Further assume size is padded out to words 370 void *Amalloc_4(size_t x) { 371 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); 372 debug_only(if (UseMallocOnly) return malloc(x);) 373 check_for_overflow(x, "Arena::Amalloc_4"); 374 NOT_PRODUCT(inc_bytes_allocated(x);) 375 if (_hwm + x > _max) { 376 return grow(x); 377 } else { 378 char *old = _hwm; 379 _hwm += x; 380 return old; 381 } 382 } 383 384 // Allocate with 'double' alignment. It is 8 bytes on sparc. 385 // In other cases Amalloc_D() should be the same as Amalloc_4(). 386 void* Amalloc_D(size_t x) { 387 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); 388 debug_only(if (UseMallocOnly) return malloc(x);) 389 #if defined(SPARC) && !defined(_LP64) 390 #define DALIGN_M1 7 391 size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm; 392 x += delta; 393 #endif 394 check_for_overflow(x, "Arena::Amalloc_D"); 395 NOT_PRODUCT(inc_bytes_allocated(x);) 396 if (_hwm + x > _max) { 397 return grow(x); // grow() returns a result aligned >= 8 bytes. 398 } else { 399 char *old = _hwm; 400 _hwm += x; 401 #if defined(SPARC) && !defined(_LP64) 402 old += delta; // align to 8-bytes 403 #endif 404 return old; 405 } 406 } 407 408 // Fast delete in area. Common case is: NOP (except for storage reclaimed) 409 void Afree(void *ptr, size_t size) { 410 #ifdef ASSERT 411 if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory 412 if (UseMallocOnly) return; 413 #endif 414 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr; 415 } 416 417 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size ); 418 419 // Move contents of this arena into an empty arena 420 Arena *move_contents(Arena *empty_arena); 421 422 // Determine if pointer belongs to this Arena or not. 423 bool contains( const void *ptr ) const; 424 425 // Total of all chunks in use (not thread-safe) 426 size_t used() const; 427 428 // Total # of bytes used 429 size_t size_in_bytes() const { return _size_in_bytes; }; 430 void set_size_in_bytes(size_t size); 431 432 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN; 433 static void free_all(char** start, char** end) PRODUCT_RETURN; 434 435 // how many arena instances 436 NOT_PRODUCT(static volatile jint _instance_count;) 437 private: 438 // Reset this Arena to empty, access will trigger grow if necessary 439 void reset(void) { 440 _first = _chunk = NULL; 441 _hwm = _max = NULL; 442 set_size_in_bytes(0); 443 } 444 }; 445 446 // One of the following macros must be used when allocating 447 // an array or object from an arena 448 #define NEW_ARENA_ARRAY(arena, type, size) \ 449 (type*) (arena)->Amalloc((size) * sizeof(type)) 450 451 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \ 452 (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \ 453 (new_size) * sizeof(type) ) 454 455 #define FREE_ARENA_ARRAY(arena, type, old, size) \ 456 (arena)->Afree((char*)(old), (size) * sizeof(type)) 457 458 #define NEW_ARENA_OBJ(arena, type) \ 459 NEW_ARENA_ARRAY(arena, type, 1) 460 461 462 //%note allocation_1 463 extern char* resource_allocate_bytes(size_t size); 464 extern char* resource_allocate_bytes(Thread* thread, size_t size); 465 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size); 466 extern void resource_free_bytes( char *old, size_t size ); 467 468 //---------------------------------------------------------------------- 469 // Base class for objects allocated in the resource area per default. 470 // Optionally, objects may be allocated on the C heap with 471 // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena) 472 // ResourceObj's can be allocated within other objects, but don't use 473 // new or delete (allocation_type is unknown). If new is used to allocate, 474 // use delete to deallocate. 475 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC { 476 public: 477 enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 }; 478 static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN; 479 #ifdef ASSERT 480 private: 481 // When this object is allocated on stack the new() operator is not 482 // called but garbage on stack may look like a valid allocation_type. 483 // Store negated 'this' pointer when new() is called to distinguish cases. 484 // Use second array's element for verification value to distinguish garbage. 485 uintptr_t _allocation_t[2]; 486 bool is_type_set() const; 487 public: 488 allocation_type get_allocation_type() const; 489 bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; } 490 bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; } 491 bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; } 492 bool allocated_on_arena() const { return get_allocation_type() == ARENA; } 493 ResourceObj(); // default construtor 494 ResourceObj(const ResourceObj& r); // default copy construtor 495 ResourceObj& operator=(const ResourceObj& r); // default copy assignment 496 ~ResourceObj(); 497 #endif // ASSERT 498 499 public: 500 void* operator new(size_t size, allocation_type type, MEMFLAGS flags); 501 void* operator new(size_t size, Arena *arena) { 502 address res = (address)arena->Amalloc(size); 503 DEBUG_ONLY(set_allocation_type(res, ARENA);) 504 return res; 505 } 506 void* operator new(size_t size) { 507 address res = (address)resource_allocate_bytes(size); 508 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) 509 return res; 510 } 511 void operator delete(void* p); 512 }; 513 514 // One of the following macros must be used when allocating an array 515 // or object to determine whether it should reside in the C heap on in 516 // the resource area. 517 518 #define NEW_RESOURCE_ARRAY(type, size)\ 519 (type*) resource_allocate_bytes((size) * sizeof(type)) 520 521 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\ 522 (type*) resource_allocate_bytes(thread, (size) * sizeof(type)) 523 524 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\ 525 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) ) 526 527 #define FREE_RESOURCE_ARRAY(type, old, size)\ 528 resource_free_bytes((char*)(old), (size) * sizeof(type)) 529 530 #define FREE_FAST(old)\ 531 /* nop */ 532 533 #define NEW_RESOURCE_OBJ(type)\ 534 NEW_RESOURCE_ARRAY(type, 1) 535 536 #define NEW_C_HEAP_ARRAY(type, size, memflags)\ 537 (type*) (AllocateHeap((size) * sizeof(type), memflags)) 538 539 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ 540 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) 541 542 #define FREE_C_HEAP_ARRAY(type,old,memflags) \ 543 FreeHeap((char*)(old), memflags) 544 545 #define NEW_C_HEAP_OBJ(type, memflags)\ 546 NEW_C_HEAP_ARRAY(type, 1, memflags) 547 548 549 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ 550 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) 551 552 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\ 553 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc)) 554 555 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\ 556 NEW_C_HEAP_ARRAY2(type, 1, memflags, pc) 557 558 559 extern bool warn_new_operator; 560 561 // for statistics 562 #ifndef PRODUCT 563 class AllocStats : StackObj { 564 julong start_mallocs, start_frees; 565 julong start_malloc_bytes, start_mfree_bytes, start_res_bytes; 566 public: 567 AllocStats(); 568 569 julong num_mallocs(); // since creation of receiver 570 julong alloc_bytes(); 571 julong num_frees(); 572 julong free_bytes(); 573 julong resource_bytes(); 574 void print(); 575 }; 576 #endif 577 578 579 //------------------------------ReallocMark--------------------------------- 580 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated 581 // ReallocMark, which is declared in the same scope as the reallocated 582 // pointer. Any operation that could __potentially__ cause a reallocation 583 // should check the ReallocMark. 584 class ReallocMark: public StackObj { 585 protected: 586 NOT_PRODUCT(int _nesting;) 587 588 public: 589 ReallocMark() PRODUCT_RETURN; 590 void check() PRODUCT_RETURN; 591 }; 592 593 #endif // SHARE_VM_MEMORY_ALLOCATION_HPP