< prev index next >

src/hotspot/share/runtime/os.cpp

Print this page




 651     ls.print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 652     ls.print_cr("## memory stomp:");
 653     guarded.print_on(&ls);
 654     fatal("memory stomping error");
 655   }
 656 }
 657 
 658 #endif
 659 
 660 //
 661 // This function supports testing of the malloc out of memory
 662 // condition without really running the system out of memory.
 663 //
 664 static bool has_reached_max_malloc_test_peak(size_t alloc_size) {
 665   if (MallocMaxTestWords > 0) {
 666     size_t words = (alloc_size / BytesPerWord);
 667 
 668     if ((cur_malloc_words + words) > MallocMaxTestWords) {
 669       return true;
 670     }
 671     Atomic::add(words, &cur_malloc_words);
 672   }
 673   return false;
 674 }
 675 
 676 void* os::malloc(size_t size, MEMFLAGS flags) {
 677   return os::malloc(size, flags, CALLER_PC);
 678 }
 679 
 680 void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 681   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 682   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 683 
 684   // Since os::malloc can be called when the libjvm.{dll,so} is
 685   // first loaded and we don't have a thread yet we must accept NULL also here.
 686   assert(!os::ThreadCrashProtection::is_crash_protected(Thread::current_or_null()),
 687          "malloc() not allowed when crash protection is set");
 688 
 689   if (size == 0) {
 690     // return a valid pointer if size is zero
 691     // if NULL is returned the calling functions assume out of memory.


 838   // if q overflowed, ignore the overflow and increment q
 839   if (lo > m) {
 840     lo &= m;
 841     ++lo;
 842   }
 843   lo += hi >> 15;
 844 
 845   // if (p+q) overflowed, ignore the overflow and increment (p+q)
 846   if (lo > m) {
 847     lo &= m;
 848     ++lo;
 849   }
 850   return lo;
 851 }
 852 
 853 int os::random() {
 854   // Make updating the random seed thread safe.
 855   while (true) {
 856     unsigned int seed = _rand_seed;
 857     unsigned int rand = random_helper(seed);
 858     if (Atomic::cmpxchg(rand, &_rand_seed, seed) == seed) {
 859       return static_cast<int>(rand);
 860     }
 861   }
 862 }
 863 
 864 // The INITIALIZED state is distinguished from the SUSPENDED state because the
 865 // conditions in which a thread is first started are different from those in which
 866 // a suspension is resumed.  These differences make it hard for us to apply the
 867 // tougher checks when starting threads that we want to do when resuming them.
 868 // However, when start_thread is called as a result of Thread.start, on a Java
 869 // thread, the operation is synchronized on the Java Thread object.  So there
 870 // cannot be a race to start the thread and hence for the thread to exit while
 871 // we are working on it.  Non-Java threads that start Java threads either have
 872 // to do so in a context in which races are impossible, or should do appropriate
 873 // locking.
 874 
 875 void os::start_thread(Thread* thread) {
 876   // guard suspend/resume
 877   MutexLocker ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 878   OSThread* osthread = thread->osthread();


1787     result = pd_unmap_memory(addr, bytes);
1788   }
1789   return result;
1790 }
1791 
1792 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
1793   pd_free_memory(addr, bytes, alignment_hint);
1794 }
1795 
1796 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
1797   pd_realign_memory(addr, bytes, alignment_hint);
1798 }
1799 
1800 #ifndef _WINDOWS
1801 /* try to switch state from state "from" to state "to"
1802  * returns the state set after the method is complete
1803  */
1804 os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::State from,
1805                                                          os::SuspendResume::State to)
1806 {
1807   os::SuspendResume::State result = Atomic::cmpxchg(to, &_state, from);
1808   if (result == from) {
1809     // success
1810     return to;
1811   }
1812   return result;
1813 }
1814 #endif
1815 
1816 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1817 // times. Only for use by non-JavaThreads.
1818 void os::naked_sleep(jlong millis) {
1819   assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1820   const jlong limit = 999;
1821   while (millis > limit) {
1822     naked_short_sleep(limit);
1823     millis -= limit;
1824   }
1825   naked_short_sleep(millis);
1826 }


 651     ls.print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 652     ls.print_cr("## memory stomp:");
 653     guarded.print_on(&ls);
 654     fatal("memory stomping error");
 655   }
 656 }
 657 
 658 #endif
 659 
 660 //
 661 // This function supports testing of the malloc out of memory
 662 // condition without really running the system out of memory.
 663 //
 664 static bool has_reached_max_malloc_test_peak(size_t alloc_size) {
 665   if (MallocMaxTestWords > 0) {
 666     size_t words = (alloc_size / BytesPerWord);
 667 
 668     if ((cur_malloc_words + words) > MallocMaxTestWords) {
 669       return true;
 670     }
 671     Atomic::add(&cur_malloc_words, words);
 672   }
 673   return false;
 674 }
 675 
 676 void* os::malloc(size_t size, MEMFLAGS flags) {
 677   return os::malloc(size, flags, CALLER_PC);
 678 }
 679 
 680 void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 681   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 682   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 683 
 684   // Since os::malloc can be called when the libjvm.{dll,so} is
 685   // first loaded and we don't have a thread yet we must accept NULL also here.
 686   assert(!os::ThreadCrashProtection::is_crash_protected(Thread::current_or_null()),
 687          "malloc() not allowed when crash protection is set");
 688 
 689   if (size == 0) {
 690     // return a valid pointer if size is zero
 691     // if NULL is returned the calling functions assume out of memory.


 838   // if q overflowed, ignore the overflow and increment q
 839   if (lo > m) {
 840     lo &= m;
 841     ++lo;
 842   }
 843   lo += hi >> 15;
 844 
 845   // if (p+q) overflowed, ignore the overflow and increment (p+q)
 846   if (lo > m) {
 847     lo &= m;
 848     ++lo;
 849   }
 850   return lo;
 851 }
 852 
 853 int os::random() {
 854   // Make updating the random seed thread safe.
 855   while (true) {
 856     unsigned int seed = _rand_seed;
 857     unsigned int rand = random_helper(seed);
 858     if (Atomic::cmpxchg(&_rand_seed, seed, rand) == seed) {
 859       return static_cast<int>(rand);
 860     }
 861   }
 862 }
 863 
 864 // The INITIALIZED state is distinguished from the SUSPENDED state because the
 865 // conditions in which a thread is first started are different from those in which
 866 // a suspension is resumed.  These differences make it hard for us to apply the
 867 // tougher checks when starting threads that we want to do when resuming them.
 868 // However, when start_thread is called as a result of Thread.start, on a Java
 869 // thread, the operation is synchronized on the Java Thread object.  So there
 870 // cannot be a race to start the thread and hence for the thread to exit while
 871 // we are working on it.  Non-Java threads that start Java threads either have
 872 // to do so in a context in which races are impossible, or should do appropriate
 873 // locking.
 874 
 875 void os::start_thread(Thread* thread) {
 876   // guard suspend/resume
 877   MutexLocker ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 878   OSThread* osthread = thread->osthread();


1787     result = pd_unmap_memory(addr, bytes);
1788   }
1789   return result;
1790 }
1791 
1792 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
1793   pd_free_memory(addr, bytes, alignment_hint);
1794 }
1795 
1796 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
1797   pd_realign_memory(addr, bytes, alignment_hint);
1798 }
1799 
1800 #ifndef _WINDOWS
1801 /* try to switch state from state "from" to state "to"
1802  * returns the state set after the method is complete
1803  */
1804 os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::State from,
1805                                                          os::SuspendResume::State to)
1806 {
1807   os::SuspendResume::State result = Atomic::cmpxchg(&_state, from, to);
1808   if (result == from) {
1809     // success
1810     return to;
1811   }
1812   return result;
1813 }
1814 #endif
1815 
1816 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1817 // times. Only for use by non-JavaThreads.
1818 void os::naked_sleep(jlong millis) {
1819   assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1820   const jlong limit = 999;
1821   while (millis > limit) {
1822     naked_short_sleep(limit);
1823     millis -= limit;
1824   }
1825   naked_short_sleep(millis);
1826 }
< prev index next >