src/share/vm/runtime/os.cpp

Print this page
rev 7616 : [mq]: realloc
   1 /*
   2  * Copyright (c) 1997, 2014, 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  *


 530 #define paranoid                 0  /* only set to 1 if you suspect checking code has bug */
 531 
 532 #ifdef ASSERT
 533 
 534 static void verify_memory(void* ptr) {
 535   GuardedMemory guarded(ptr);
 536   if (!guarded.verify_guards()) {
 537     tty->print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 538     tty->print_cr("## memory stomp:");
 539     guarded.print_on(tty);
 540     fatal("memory stomping error");
 541   }
 542 }
 543 
 544 #endif
 545 
 546 //
 547 // This function supports testing of the malloc out of memory
 548 // condition without really running the system out of memory.
 549 //
 550 static u_char* testMalloc(size_t alloc_size) {
 551   assert(MallocMaxTestWords > 0, "sanity check");

 552 
 553   if ((cur_malloc_words + (alloc_size / BytesPerWord)) > MallocMaxTestWords) {
 554     return NULL;
 555   }
 556 
 557   u_char* ptr = (u_char*)::malloc(alloc_size);
 558 
 559   if (ptr != NULL) {
 560     Atomic::add(((jint) (alloc_size / BytesPerWord)),
 561                 (volatile jint *) &cur_malloc_words);
 562   }
 563   return ptr;
 564 }
 565 
 566 void* os::malloc(size_t size, MEMFLAGS flags) {
 567   return os::malloc(size, flags, CALLER_PC);
 568 }
 569 
 570 void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 571   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 572   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 573 
 574 #ifdef ASSERT
 575   // checking for the WatcherThread and crash_protection first
 576   // since os::malloc can be called when the libjvm.{dll,so} is
 577   // first loaded and we don't have a thread yet.
 578   // try to find the thread after we see that the watcher thread
 579   // exists and has crash protection.
 580   WatcherThread *wt = WatcherThread::watcher_thread();
 581   if (wt != NULL && wt->has_crash_protection()) {
 582     Thread* thread = ThreadLocalStorage::get_thread_slow();
 583     if (thread == wt) {


 591     // return a valid pointer if size is zero
 592     // if NULL is returned the calling functions assume out of memory.
 593     size = 1;
 594   }
 595 
 596   // NMT support
 597   NMT_TrackingLevel level = MemTracker::tracking_level();
 598   size_t            nmt_header_size = MemTracker::malloc_header_size(level);
 599 
 600 #ifndef ASSERT
 601   const size_t alloc_size = size + nmt_header_size;
 602 #else
 603   const size_t alloc_size = GuardedMemory::get_total_size(size + nmt_header_size);
 604   if (size + nmt_header_size > alloc_size) { // Check for rollover.
 605     return NULL;
 606   }
 607 #endif
 608 
 609   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 610 





 611   u_char* ptr;
 612   if (MallocMaxTestWords > 0) {
 613     ptr = testMalloc(alloc_size);
 614   } else {
 615     ptr = (u_char*)::malloc(alloc_size);
 616   }
 617 
 618 #ifdef ASSERT
 619   if (ptr == NULL) {
 620     return NULL;
 621   }
 622   // Wrap memory with guard
 623   GuardedMemory guarded(ptr, size + nmt_header_size);
 624   ptr = guarded.get_user_ptr();
 625 #endif
 626   if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 627     tty->print_cr("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 628     breakpoint();
 629   }
 630   debug_only(if (paranoid) verify_memory(ptr));
 631   if (PrintMalloc && tty != NULL) {
 632     tty->print_cr("os::malloc " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 633   }
 634 
 635   // we do not track guard memory
 636   return MemTracker::record_malloc((address)ptr, size, memflags, stack, level);
 637 }
 638 
 639 void* os::realloc(void *memblock, size_t size, MEMFLAGS flags) {
 640   return os::realloc(memblock, size, flags, CALLER_PC);
 641 }
 642 
 643 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {





 644 
 645 #ifndef ASSERT
 646   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 647   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 648    // NMT support
 649   void* membase = MemTracker::record_free(memblock);
 650   NMT_TrackingLevel level = MemTracker::tracking_level();
 651   size_t  nmt_header_size = MemTracker::malloc_header_size(level);
 652   void* ptr = ::realloc(membase, size + nmt_header_size);
 653   return MemTracker::record_malloc(ptr, size, memflags, stack, level);
 654 #else
 655   if (memblock == NULL) {
 656     return os::malloc(size, memflags, stack);
 657   }
 658   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 659     tty->print_cr("os::realloc caught " PTR_FORMAT, memblock);
 660     breakpoint();
 661   }
 662   // NMT support
 663   void* membase = MemTracker::malloc_base(memblock);


   1 /*
   2  * Copyright (c) 1997, 2015, 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  *


 530 #define paranoid                 0  /* only set to 1 if you suspect checking code has bug */
 531 
 532 #ifdef ASSERT
 533 
 534 static void verify_memory(void* ptr) {
 535   GuardedMemory guarded(ptr);
 536   if (!guarded.verify_guards()) {
 537     tty->print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 538     tty->print_cr("## memory stomp:");
 539     guarded.print_on(tty);
 540     fatal("memory stomping error");
 541   }
 542 }
 543 
 544 #endif
 545 
 546 //
 547 // This function supports testing of the malloc out of memory
 548 // condition without really running the system out of memory.
 549 //
 550 static bool has_reached_max_malloc_test_peak(size_t alloc_size) {
 551   if (MallocMaxTestWords > 0) {
 552     jint words = (jint)(alloc_size / BytesPerWord);
 553 
 554     if ((cur_malloc_words + words) > MallocMaxTestWords) {
 555       return true;
 556     }
 557     Atomic::add(words, (volatile jint *)&cur_malloc_words);





 558   }
 559   return false;
 560 }
 561 
 562 void* os::malloc(size_t size, MEMFLAGS flags) {
 563   return os::malloc(size, flags, CALLER_PC);
 564 }
 565 
 566 void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 567   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 568   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 569 
 570 #ifdef ASSERT
 571   // checking for the WatcherThread and crash_protection first
 572   // since os::malloc can be called when the libjvm.{dll,so} is
 573   // first loaded and we don't have a thread yet.
 574   // try to find the thread after we see that the watcher thread
 575   // exists and has crash protection.
 576   WatcherThread *wt = WatcherThread::watcher_thread();
 577   if (wt != NULL && wt->has_crash_protection()) {
 578     Thread* thread = ThreadLocalStorage::get_thread_slow();
 579     if (thread == wt) {


 587     // return a valid pointer if size is zero
 588     // if NULL is returned the calling functions assume out of memory.
 589     size = 1;
 590   }
 591 
 592   // NMT support
 593   NMT_TrackingLevel level = MemTracker::tracking_level();
 594   size_t            nmt_header_size = MemTracker::malloc_header_size(level);
 595 
 596 #ifndef ASSERT
 597   const size_t alloc_size = size + nmt_header_size;
 598 #else
 599   const size_t alloc_size = GuardedMemory::get_total_size(size + nmt_header_size);
 600   if (size + nmt_header_size > alloc_size) { // Check for rollover.
 601     return NULL;
 602   }
 603 #endif
 604 
 605   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 606   
 607   // For the test flag -XX:MallocMaxTestWords
 608   if (has_reached_max_malloc_test_peak(size)) {
 609     return NULL;
 610   }
 611 
 612   u_char* ptr;



 613   ptr = (u_char*)::malloc(alloc_size);

 614 
 615 #ifdef ASSERT
 616   if (ptr == NULL) {
 617     return NULL;
 618   }
 619   // Wrap memory with guard
 620   GuardedMemory guarded(ptr, size + nmt_header_size);
 621   ptr = guarded.get_user_ptr();
 622 #endif
 623   if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 624     tty->print_cr("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 625     breakpoint();
 626   }
 627   debug_only(if (paranoid) verify_memory(ptr));
 628   if (PrintMalloc && tty != NULL) {
 629     tty->print_cr("os::malloc " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 630   }
 631 
 632   // we do not track guard memory
 633   return MemTracker::record_malloc((address)ptr, size, memflags, stack, level);
 634 }
 635 
 636 void* os::realloc(void *memblock, size_t size, MEMFLAGS flags) {
 637   return os::realloc(memblock, size, flags, CALLER_PC);
 638 }
 639 
 640 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 641 
 642   // For the test flag -XX:MallocMaxTestWords
 643   if (has_reached_max_malloc_test_peak(size)) {
 644     return NULL;
 645   }
 646 
 647 #ifndef ASSERT
 648   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 649   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 650    // NMT support
 651   void* membase = MemTracker::record_free(memblock);
 652   NMT_TrackingLevel level = MemTracker::tracking_level();
 653   size_t  nmt_header_size = MemTracker::malloc_header_size(level);
 654   void* ptr = ::realloc(membase, size + nmt_header_size);
 655   return MemTracker::record_malloc(ptr, size, memflags, stack, level);
 656 #else
 657   if (memblock == NULL) {
 658     return os::malloc(size, memflags, stack);
 659   }
 660   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 661     tty->print_cr("os::realloc caught " PTR_FORMAT, memblock);
 662     breakpoint();
 663   }
 664   // NMT support
 665   void* membase = MemTracker::malloc_base(memblock);