comparison src/share/vm/memory/allocation.hpp @ 10271:f9be75d21404

8012902: remove use of global operator new - take 2 Summary: The fix of 8010992, disable use of global operator new and new[] which caused failure on some tests. This takes two of the bugs also add ALLOW_OPERATOR_NEW_USAGE to prevent crash for third party code calling operator new of jvm on certain platforms. Reviewed-by: coleenp, dholmes, zgu Contributed-by: yumin.qi@oracle.com
author minqi
date Tue, 14 May 2013 09:41:12 -0700
parents f258c5828eb8
children a1ebd310d5c1 ce9ecec70f99
comparison
equal deleted inserted replaced
10269:a9270d9ecb13 10271:f9be75d21404
1 /* 1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
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.
193 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { 204 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
194 public: 205 public:
195 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); 206 _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, 207 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant,
197 address caller_pc = 0); 208 address caller_pc = 0);
198 209 _NOINLINE_ void* operator new [](size_t size, address caller_pc = 0);
210 _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
211 address caller_pc = 0);
199 void operator delete(void* p); 212 void operator delete(void* p);
213 void operator delete [] (void* p);
200 }; 214 };
201 215
202 // Base class for objects allocated on the stack only. 216 // Base class for objects allocated on the stack only.
203 // Calling new or delete will result in fatal error. 217 // Calling new or delete will result in fatal error.
204 218
205 class StackObj ALLOCATION_SUPER_CLASS_SPEC { 219 class StackObj ALLOCATION_SUPER_CLASS_SPEC {
206 private: 220 private:
207 void* operator new(size_t size); 221 void* operator new(size_t size);
208 void operator delete(void* p); 222 void operator delete(void* p);
223 void* operator new [](size_t size);
224 void operator delete [](void* p);
209 }; 225 };
210 226
211 // Base class for objects used as value objects. 227 // Base class for objects used as value objects.
212 // Calling new or delete will result in fatal error. 228 // Calling new or delete will result in fatal error.
213 // 229 //
227 // be defined as a an empty string "". 243 // be defined as a an empty string "".
228 // 244 //
229 class _ValueObj { 245 class _ValueObj {
230 private: 246 private:
231 void* operator new(size_t size); 247 void* operator new(size_t size);
232 void operator delete(void* p); 248 void operator delete(void* p);
249 void* operator new [](size_t size);
250 void operator delete [](void* p);
233 }; 251 };
234 252
235 253
236 // Base class for objects stored in Metaspace. 254 // Base class for objects stored in Metaspace.
237 // Calling delete will result in fatal error. 255 // Calling delete will result in fatal error.
508 ~ResourceObj(); 526 ~ResourceObj();
509 #endif // ASSERT 527 #endif // ASSERT
510 528
511 public: 529 public:
512 void* operator new(size_t size, allocation_type type, MEMFLAGS flags); 530 void* operator new(size_t size, allocation_type type, MEMFLAGS flags);
531 void* operator new [](size_t size, allocation_type type, MEMFLAGS flags);
513 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, 532 void* operator new(size_t size, const std::nothrow_t& nothrow_constant,
514 allocation_type type, MEMFLAGS flags); 533 allocation_type type, MEMFLAGS flags);
534 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
535 allocation_type type, MEMFLAGS flags);
536
515 void* operator new(size_t size, Arena *arena) { 537 void* operator new(size_t size, Arena *arena) {
516 address res = (address)arena->Amalloc(size); 538 address res = (address)arena->Amalloc(size);
517 DEBUG_ONLY(set_allocation_type(res, ARENA);) 539 DEBUG_ONLY(set_allocation_type(res, ARENA);)
518 return res; 540 return res;
519 } 541 }
542
543 void* operator new [](size_t size, Arena *arena) {
544 address res = (address)arena->Amalloc(size);
545 DEBUG_ONLY(set_allocation_type(res, ARENA);)
546 return res;
547 }
548
520 void* operator new(size_t size) { 549 void* operator new(size_t size) {
521 address res = (address)resource_allocate_bytes(size); 550 address res = (address)resource_allocate_bytes(size);
522 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) 551 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
523 return res; 552 return res;
524 } 553 }
527 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); 556 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
528 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) 557 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
529 return res; 558 return res;
530 } 559 }
531 560
561 void* operator new [](size_t size) {
562 address res = (address)resource_allocate_bytes(size);
563 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
564 return res;
565 }
566
567 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) {
568 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
569 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
570 return res;
571 }
572
532 void operator delete(void* p); 573 void operator delete(void* p);
574 void operator delete [](void* p);
533 }; 575 };
534 576
535 // One of the following macros must be used when allocating an array 577 // 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 578 // or object to determine whether it should reside in the C heap on in
537 // the resource area. 579 // the resource area.
561 (type*) (AllocateHeap((size) * sizeof(type), memflags)) 603 (type*) (AllocateHeap((size) * sizeof(type), memflags))
562 604
563 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ 605 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
564 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) 606 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags))
565 607
566 #define FREE_C_HEAP_ARRAY(type,old,memflags) \ 608 #define FREE_C_HEAP_ARRAY(type, old, memflags) \
567 FreeHeap((char*)(old), memflags) 609 FreeHeap((char*)(old), memflags)
568 610
611 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
612 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
613
614 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
615 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
616
617 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail) \
618 (type*) AllocateHeap(size * sizeof(type), memflags, pc, allocfail)
619
620 // allocate type in heap without calling ctor
569 #define NEW_C_HEAP_OBJ(type, memflags)\ 621 #define NEW_C_HEAP_OBJ(type, memflags)\
570 NEW_C_HEAP_ARRAY(type, 1, memflags) 622 NEW_C_HEAP_ARRAY(type, 1, memflags)
571 623
572 624 // deallocate obj of type in heap without calling dtor
573 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ 625 #define FREE_C_HEAP_OBJ(objname, memflags)\
574 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) 626 FreeHeap((char*)objname, memflags);
575
576 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
577 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
578
579 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\
580 NEW_C_HEAP_ARRAY2(type, 1, memflags, pc)
581
582
583 extern bool warn_new_operator;
584 627
585 // for statistics 628 // for statistics
586 #ifndef PRODUCT 629 #ifndef PRODUCT
587 class AllocStats : StackObj { 630 class AllocStats : StackObj {
588 julong start_mallocs, start_frees; 631 julong start_mallocs, start_frees;