comparison src/share/vm/memory/allocation.hpp @ 10408:836a62f43af9

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/
author Doug Simon <doug.simon@oracle.com>
date Wed, 19 Jun 2013 10:45:56 +0200
parents 43223d3f5dcd f2110083203d
children 6b0fd0964b87
comparison
equal deleted inserted replaced
10086:e0fb8a213650 10408:836a62f43af9
84 // member functions for printing. Classes that avoid allocating the 84 // member functions for printing. Classes that avoid allocating the
85 // vtbl entries in the objects should therefore not be the printable 85 // vtbl entries in the objects should therefore not be the printable
86 // subclasses. 86 // subclasses.
87 // 87 //
88 // The following macros and function should be used to allocate memory 88 // The following macros and function should be used to allocate memory
89 // directly in the resource area or in the C-heap: 89 // directly in the resource area or in the C-heap, The _OBJ variants
90 // 90 // of the NEW/FREE_C_HEAP macros are used for alloc/dealloc simple
91 // NEW_RESOURCE_ARRAY(type,size) 91 // objects which are not inherited from CHeapObj, note constructor and
92 // destructor are not called. The preferable way to allocate objects
93 // is using the new operator.
94 //
95 // WARNING: The array variant must only be used for a homogenous array
96 // where all objects are of the exact type specified. If subtypes are
97 // stored in the array then must pay attention to calling destructors
98 // at needed.
99 //
100 // NEW_RESOURCE_ARRAY(type, size)
92 // NEW_RESOURCE_OBJ(type) 101 // NEW_RESOURCE_OBJ(type)
93 // NEW_C_HEAP_ARRAY(type,size) 102 // NEW_C_HEAP_ARRAY(type, size)
94 // NEW_C_HEAP_OBJ(type) 103 // NEW_C_HEAP_OBJ(type, memflags)
104 // FREE_C_HEAP_ARRAY(type, old, memflags)
105 // FREE_C_HEAP_OBJ(objname, type, memflags)
95 // char* AllocateHeap(size_t size, const char* name); 106 // char* AllocateHeap(size_t size, const char* name);
96 // void FreeHeap(void* p); 107 // void FreeHeap(void* p);
97 // 108 //
98 // C-heap allocation can be traced using +PrintHeapAllocation. 109 // C-heap allocation can be traced using +PrintHeapAllocation.
99 // malloc and free should therefore never called directly. 110 // malloc and free should therefore never called directly.
144 mtNMT = 0x0A00, // memory used by native memory tracking 155 mtNMT = 0x0A00, // memory used by native memory tracking
145 mtChunk = 0x0B00, // chunk that holds content of arenas 156 mtChunk = 0x0B00, // chunk that holds content of arenas
146 mtJavaHeap = 0x0C00, // Java heap 157 mtJavaHeap = 0x0C00, // Java heap
147 mtClassShared = 0x0D00, // class data sharing 158 mtClassShared = 0x0D00, // class data sharing
148 mtTest = 0x0E00, // Test type for verifying NMT 159 mtTest = 0x0E00, // Test type for verifying NMT
149 mt_number_of_types = 0x000E, // number of memory types (mtDontTrack 160 mtTracing = 0x0F00, // memory used for Tracing
161 mt_number_of_types = 0x000F, // number of memory types (mtDontTrack
150 // is not included as validate type) 162 // is not included as validate type)
151 mtDontTrack = 0x0F00, // memory we do not or cannot track 163 mtDontTrack = 0x0F00, // memory we do not or cannot track
152 mt_masks = 0x7F00, 164 mt_masks = 0x7F00,
153 165
154 // object type mask 166 // object type mask
193 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { 205 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
194 public: 206 public:
195 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); 207 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0);
196 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, 208 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant,
197 address caller_pc = 0); 209 address caller_pc = 0);
198 210 _NOINLINE_ void* operator new [](size_t size, address caller_pc = 0);
211 _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
212 address caller_pc = 0);
199 void operator delete(void* p); 213 void operator delete(void* p);
214 void operator delete [] (void* p);
200 }; 215 };
201 216
202 // Base class for objects allocated on the stack only. 217 // Base class for objects allocated on the stack only.
203 // Calling new or delete will result in fatal error. 218 // Calling new or delete will result in fatal error.
204 219
205 class StackObj ALLOCATION_SUPER_CLASS_SPEC { 220 class StackObj ALLOCATION_SUPER_CLASS_SPEC {
206 private: 221 private:
207 void* operator new(size_t size); 222 void* operator new(size_t size);
208 void operator delete(void* p); 223 void operator delete(void* p);
224 void* operator new [](size_t size);
225 void operator delete [](void* p);
209 }; 226 };
210 227
211 // Base class for objects used as value objects. 228 // Base class for objects used as value objects.
212 // Calling new or delete will result in fatal error. 229 // Calling new or delete will result in fatal error.
213 // 230 //
227 // be defined as a an empty string "". 244 // be defined as a an empty string "".
228 // 245 //
229 class _ValueObj { 246 class _ValueObj {
230 private: 247 private:
231 void* operator new(size_t size); 248 void* operator new(size_t size);
232 void operator delete(void* p); 249 void operator delete(void* p);
250 void* operator new [](size_t size);
251 void operator delete [](void* p);
233 }; 252 };
234 253
235 254
236 // Base class for objects stored in Metaspace. 255 // Base class for objects stored in Metaspace.
237 // Calling delete will result in fatal error. 256 // Calling delete will result in fatal error.
248 bool is_metadata() const; 267 bool is_metadata() const;
249 bool is_metaspace_object() const; // more specific test but slower 268 bool is_metaspace_object() const; // more specific test but slower
250 bool is_shared() const; 269 bool is_shared() const;
251 void print_address_on(outputStream* st) const; // nonvirtual address printing 270 void print_address_on(outputStream* st) const; // nonvirtual address printing
252 271
272 #define METASPACE_OBJ_TYPES_DO(f) \
273 f(Unknown) \
274 f(Class) \
275 f(Symbol) \
276 f(TypeArrayU1) \
277 f(TypeArrayU2) \
278 f(TypeArrayU4) \
279 f(TypeArrayU8) \
280 f(TypeArrayOther) \
281 f(Method) \
282 f(ConstMethod) \
283 f(MethodData) \
284 f(ConstantPool) \
285 f(ConstantPoolCache) \
286 f(Annotation) \
287 f(MethodCounters)
288
289 #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,
290 #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
291
292 enum Type {
293 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
294 METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
295 _number_of_types
296 };
297
298 static const char * type_name(Type type) {
299 switch(type) {
300 METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
301 default:
302 ShouldNotReachHere();
303 return NULL;
304 }
305 }
306
307 static MetaspaceObj::Type array_type(size_t elem_size) {
308 switch (elem_size) {
309 case 1: return TypeArrayU1Type;
310 case 2: return TypeArrayU2Type;
311 case 4: return TypeArrayU4Type;
312 case 8: return TypeArrayU8Type;
313 default:
314 return TypeArrayOtherType;
315 }
316 }
317
253 void* operator new(size_t size, ClassLoaderData* loader_data, 318 void* operator new(size_t size, ClassLoaderData* loader_data,
254 size_t word_size, bool read_only, Thread* thread); 319 size_t word_size, bool read_only,
320 Type type, Thread* thread);
255 // can't use TRAPS from this header file. 321 // can't use TRAPS from this header file.
256 void operator delete(void* p) { ShouldNotCallThis(); } 322 void operator delete(void* p) { ShouldNotCallThis(); }
257 }; 323 };
258 324
259 // Base class for classes that constitute name spaces. 325 // Base class for classes that constitute name spaces.
508 ~ResourceObj(); 574 ~ResourceObj();
509 #endif // ASSERT 575 #endif // ASSERT
510 576
511 public: 577 public:
512 void* operator new(size_t size, allocation_type type, MEMFLAGS flags); 578 void* operator new(size_t size, allocation_type type, MEMFLAGS flags);
579 void* operator new [](size_t size, allocation_type type, MEMFLAGS flags);
513 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, 580 void* operator new(size_t size, const std::nothrow_t& nothrow_constant,
514 allocation_type type, MEMFLAGS flags); 581 allocation_type type, MEMFLAGS flags);
582 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
583 allocation_type type, MEMFLAGS flags);
584
515 void* operator new(size_t size, Arena *arena) { 585 void* operator new(size_t size, Arena *arena) {
516 address res = (address)arena->Amalloc(size); 586 address res = (address)arena->Amalloc(size);
517 DEBUG_ONLY(set_allocation_type(res, ARENA);) 587 DEBUG_ONLY(set_allocation_type(res, ARENA);)
518 return res; 588 return res;
519 } 589 }
590
591 void* operator new [](size_t size, Arena *arena) {
592 address res = (address)arena->Amalloc(size);
593 DEBUG_ONLY(set_allocation_type(res, ARENA);)
594 return res;
595 }
596
520 void* operator new(size_t size) { 597 void* operator new(size_t size) {
521 address res = (address)resource_allocate_bytes(size); 598 address res = (address)resource_allocate_bytes(size);
522 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) 599 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
523 return res; 600 return res;
524 } 601 }
527 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); 604 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
528 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) 605 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
529 return res; 606 return res;
530 } 607 }
531 608
609 void* operator new [](size_t size) {
610 address res = (address)resource_allocate_bytes(size);
611 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
612 return res;
613 }
614
615 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) {
616 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
617 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
618 return res;
619 }
620
532 void operator delete(void* p); 621 void operator delete(void* p);
622 void operator delete [](void* p);
533 }; 623 };
534 624
535 // One of the following macros must be used when allocating an array 625 // One of the following macros must be used when allocating an array
536 // or object to determine whether it should reside in the C heap on in 626 // or object to determine whether it should reside in the C heap on in
537 // the resource area. 627 // the resource area.
538 628
539 #define NEW_RESOURCE_ARRAY(type, size)\ 629 #define NEW_RESOURCE_ARRAY(type, size)\
540 (type*) resource_allocate_bytes((size) * sizeof(type)) 630 (type*) resource_allocate_bytes((size) * sizeof(type))
541 631
632 #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\
633 (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
634
542 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\ 635 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
543 (type*) resource_allocate_bytes(thread, (size) * sizeof(type)) 636 (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
544 637
545 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\ 638 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
546 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) ) 639 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
558 (type*) (AllocateHeap((size) * sizeof(type), memflags)) 651 (type*) (AllocateHeap((size) * sizeof(type), memflags))
559 652
560 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ 653 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
561 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) 654 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags))
562 655
563 #define FREE_C_HEAP_ARRAY(type,old,memflags) \ 656 #define FREE_C_HEAP_ARRAY(type, old, memflags) \
564 FreeHeap((char*)(old), memflags) 657 FreeHeap((char*)(old), memflags)
565 658
659 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
660 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
661
662 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
663 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
664
665 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail) \
666 (type*) AllocateHeap(size * sizeof(type), memflags, pc, allocfail)
667
668 // allocate type in heap without calling ctor
566 #define NEW_C_HEAP_OBJ(type, memflags)\ 669 #define NEW_C_HEAP_OBJ(type, memflags)\
567 NEW_C_HEAP_ARRAY(type, 1, memflags) 670 NEW_C_HEAP_ARRAY(type, 1, memflags)
568 671
569 672 // deallocate obj of type in heap without calling dtor
570 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ 673 #define FREE_C_HEAP_OBJ(objname, memflags)\
571 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) 674 FreeHeap((char*)objname, memflags);
572
573 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
574 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
575
576 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\
577 NEW_C_HEAP_ARRAY2(type, 1, memflags, pc)
578
579
580 extern bool warn_new_operator;
581 675
582 // for statistics 676 // for statistics
583 #ifndef PRODUCT 677 #ifndef PRODUCT
584 class AllocStats : StackObj { 678 class AllocStats : StackObj {
585 julong start_mallocs, start_frees; 679 julong start_mallocs, start_frees;