hotspot/src/share/vm/memory/allocation.cpp
changeset 18063 3a0a5b11ab14
parent 18061 cd92ca4c1331
parent 17376 4ee999c3c007
child 18065 7d53ab28f937
equal deleted inserted replaced
18062:b16b51f1de87 18063:3a0a5b11ab14
    47 #endif
    47 #endif
    48 #ifdef TARGET_OS_FAMILY_bsd
    48 #ifdef TARGET_OS_FAMILY_bsd
    49 # include "os_bsd.inline.hpp"
    49 # include "os_bsd.inline.hpp"
    50 #endif
    50 #endif
    51 
    51 
    52 void* StackObj::operator new(size_t size)  { ShouldNotCallThis(); return 0; };
    52 void* StackObj::operator new(size_t size)       { ShouldNotCallThis(); return 0; }
    53 void  StackObj::operator delete(void* p)   { ShouldNotCallThis(); };
    53 void  StackObj::operator delete(void* p)        { ShouldNotCallThis(); }
    54 void* _ValueObj::operator new(size_t size)  { ShouldNotCallThis(); return 0; };
    54 void* StackObj::operator new [](size_t size)    { ShouldNotCallThis(); return 0; }
    55 void  _ValueObj::operator delete(void* p)   { ShouldNotCallThis(); };
    55 void  StackObj::operator delete [](void* p)     { ShouldNotCallThis(); }
       
    56 
       
    57 void* _ValueObj::operator new(size_t size)      { ShouldNotCallThis(); return 0; }
       
    58 void  _ValueObj::operator delete(void* p)       { ShouldNotCallThis(); }
       
    59 void* _ValueObj::operator new [](size_t size)   { ShouldNotCallThis(); return 0; }
       
    60 void  _ValueObj::operator delete [](void* p)    { ShouldNotCallThis(); }
    56 
    61 
    57 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,
    62 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,
    58                                 size_t word_size, bool read_only, TRAPS) {
    63                                 size_t word_size, bool read_only, TRAPS) {
    59   // Klass has it's own operator new
    64   // Klass has it's own operator new
    60   return Metaspace::allocate(loader_data, word_size, read_only,
    65   return Metaspace::allocate(loader_data, word_size, read_only,
    78 }
    83 }
    79 
    84 
    80 void MetaspaceObj::print_address_on(outputStream* st) const {
    85 void MetaspaceObj::print_address_on(outputStream* st) const {
    81   st->print(" {"INTPTR_FORMAT"}", this);
    86   st->print(" {"INTPTR_FORMAT"}", this);
    82 }
    87 }
    83 
       
    84 
    88 
    85 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) {
    89 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) {
    86   address res;
    90   address res;
    87   switch (type) {
    91   switch (type) {
    88    case C_HEAP:
    92    case C_HEAP:
    95     break;
    99     break;
    96    default:
   100    default:
    97     ShouldNotReachHere();
   101     ShouldNotReachHere();
    98   }
   102   }
    99   return res;
   103   return res;
       
   104 }
       
   105 
       
   106 void* ResourceObj::operator new [](size_t size, allocation_type type, MEMFLAGS flags) {
       
   107   return (address) operator new(size, type, flags);
   100 }
   108 }
   101 
   109 
   102 void* ResourceObj::operator new(size_t size, const std::nothrow_t&  nothrow_constant,
   110 void* ResourceObj::operator new(size_t size, const std::nothrow_t&  nothrow_constant,
   103     allocation_type type, MEMFLAGS flags) {
   111     allocation_type type, MEMFLAGS flags) {
   104   //should only call this with std::nothrow, use other operator new() otherwise
   112   //should only call this with std::nothrow, use other operator new() otherwise
   116     ShouldNotReachHere();
   124     ShouldNotReachHere();
   117   }
   125   }
   118   return res;
   126   return res;
   119 }
   127 }
   120 
   128 
       
   129 void* ResourceObj::operator new [](size_t size, const std::nothrow_t&  nothrow_constant,
       
   130     allocation_type type, MEMFLAGS flags) {
       
   131   return (address)operator new(size, nothrow_constant, type, flags);
       
   132 }
   121 
   133 
   122 void ResourceObj::operator delete(void* p) {
   134 void ResourceObj::operator delete(void* p) {
   123   assert(((ResourceObj *)p)->allocated_on_C_heap(),
   135   assert(((ResourceObj *)p)->allocated_on_C_heap(),
   124          "delete only allowed for C_HEAP objects");
   136          "delete only allowed for C_HEAP objects");
   125   DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;)
   137   DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;)
   126   FreeHeap(p);
   138   FreeHeap(p);
       
   139 }
       
   140 
       
   141 void ResourceObj::operator delete [](void* p) {
       
   142   operator delete(p);
   127 }
   143 }
   128 
   144 
   129 #ifdef ASSERT
   145 #ifdef ASSERT
   130 void ResourceObj::set_allocation_type(address res, allocation_type type) {
   146 void ResourceObj::set_allocation_type(address res, allocation_type type) {
   131     // Set allocation type in the resource object
   147     // Set allocation type in the resource object
   213 void trace_heap_free(void* p) {
   229 void trace_heap_free(void* p) {
   214   // A lock is not needed here - tty uses a lock internally
   230   // A lock is not needed here - tty uses a lock internally
   215   tty->print_cr("Heap free   " INTPTR_FORMAT, p);
   231   tty->print_cr("Heap free   " INTPTR_FORMAT, p);
   216 }
   232 }
   217 
   233 
   218 bool warn_new_operator = false; // see vm_main
       
   219 
       
   220 //--------------------------------------------------------------------------------------
   234 //--------------------------------------------------------------------------------------
   221 // ChunkPool implementation
   235 // ChunkPool implementation
   222 
   236 
   223 // MT-safe pool of chunks to reduce malloc/free thrashing
   237 // MT-safe pool of chunks to reduce malloc/free thrashing
   224 // NB: not using Mutex because pools are used before Threads are initialized
   238 // NB: not using Mutex because pools are used before Threads are initialized
   358 // Chunk implementation
   372 // Chunk implementation
   359 
   373 
   360 void* Chunk::operator new (size_t requested_size, AllocFailType alloc_failmode, size_t length) {
   374 void* Chunk::operator new (size_t requested_size, AllocFailType alloc_failmode, size_t length) {
   361   // requested_size is equal to sizeof(Chunk) but in order for the arena
   375   // requested_size is equal to sizeof(Chunk) but in order for the arena
   362   // allocations to come out aligned as expected the size must be aligned
   376   // allocations to come out aligned as expected the size must be aligned
   363   // to expected arean alignment.
   377   // to expected arena alignment.
   364   // expect requested_size but if sizeof(Chunk) doesn't match isn't proper size we must align it.
   378   // expect requested_size but if sizeof(Chunk) doesn't match isn't proper size we must align it.
   365   assert(ARENA_ALIGN(requested_size) == aligned_overhead_size(), "Bad alignment");
   379   assert(ARENA_ALIGN(requested_size) == aligned_overhead_size(), "Bad alignment");
   366   size_t bytes = ARENA_ALIGN(requested_size) + length;
   380   size_t bytes = ARENA_ALIGN(requested_size) + length;
   367   switch (length) {
   381   switch (length) {
   368    case Chunk::size:        return ChunkPool::large_pool()->allocate(bytes, alloc_failmode);
   382    case Chunk::size:        return ChunkPool::large_pool()->allocate(bytes, alloc_failmode);
   665 #ifndef PRODUCT
   679 #ifndef PRODUCT
   666 // The global operator new should never be called since it will usually indicate
   680 // The global operator new should never be called since it will usually indicate
   667 // a memory leak.  Use CHeapObj as the base class of such objects to make it explicit
   681 // a memory leak.  Use CHeapObj as the base class of such objects to make it explicit
   668 // that they're allocated on the C heap.
   682 // that they're allocated on the C heap.
   669 // Commented out in product version to avoid conflicts with third-party C++ native code.
   683 // Commented out in product version to avoid conflicts with third-party C++ native code.
   670 // %% note this is causing a problem on solaris debug build. the global
   684 // On certain platforms, such as Mac OS X (Darwin), in debug version, new is being called
   671 // new is being called from jdk source and causing data corruption.
   685 // from jdk source and causing data corruption. Such as
   672 // src/share/native/sun/awt/font/fontmanager/textcache/hsMemory.cpp::hsSoftNew
   686 //  Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
   673 // define CATCH_OPERATOR_NEW_USAGE if you want to use this.
   687 // define ALLOW_OPERATOR_NEW_USAGE for platform on which global operator new allowed.
   674 #ifdef CATCH_OPERATOR_NEW_USAGE
   688 //
       
   689 #ifndef ALLOW_OPERATOR_NEW_USAGE
   675 void* operator new(size_t size){
   690 void* operator new(size_t size){
   676   static bool warned = false;
   691   assert(false, "Should not call global operator new");
   677   if (!warned && warn_new_operator)
   692   return 0;
   678     warning("should not call global (default) operator new");
   693 }
   679   warned = true;
   694 
   680   return (void *) AllocateHeap(size, "global operator new");
   695 void* operator new [](size_t size){
   681 }
   696   assert(false, "Should not call global operator new[]");
   682 #endif
   697   return 0;
       
   698 }
       
   699 
       
   700 void* operator new(size_t size, const std::nothrow_t&  nothrow_constant){
       
   701   assert(false, "Should not call global operator new");
       
   702   return 0;
       
   703 }
       
   704 
       
   705 void* operator new [](size_t size, std::nothrow_t&  nothrow_constant){
       
   706   assert(false, "Should not call global operator new[]");
       
   707   return 0;
       
   708 }
       
   709 
       
   710 void operator delete(void* p) {
       
   711   assert(false, "Should not call global delete");
       
   712 }
       
   713 
       
   714 void operator delete [](void* p) {
       
   715   assert(false, "Should not call global delete []");
       
   716 }
       
   717 #endif // ALLOW_OPERATOR_NEW_USAGE
   683 
   718 
   684 void AllocatedObj::print() const       { print_on(tty); }
   719 void AllocatedObj::print() const       { print_on(tty); }
   685 void AllocatedObj::print_value() const { print_value_on(tty); }
   720 void AllocatedObj::print_value() const { print_value_on(tty); }
   686 
   721 
   687 void AllocatedObj::print_on(outputStream* st) const {
   722 void AllocatedObj::print_on(outputStream* st) const {