--- old/src/share/vm/runtime/os.cpp 2015-01-06 17:25:27.296648700 -0500 +++ new/src/share/vm/runtime/os.cpp 2015-01-06 17:25:27.060632300 -0500 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -547,20 +547,16 @@ // This function supports testing of the malloc out of memory // condition without really running the system out of memory. // -static u_char* testMalloc(size_t alloc_size) { - assert(MallocMaxTestWords > 0, "sanity check"); - - if ((cur_malloc_words + (alloc_size / BytesPerWord)) > MallocMaxTestWords) { - return NULL; - } - - u_char* ptr = (u_char*)::malloc(alloc_size); +static boolean has_reached_max_malloc_test_peak(size_t alloc_size) { + if (MallocMaxTestWords > 0) { + jint words = (jint)(alloc_size / BytesPerWord); - if (ptr != NULL) { - Atomic::add(((jint) (alloc_size / BytesPerWord)), - (volatile jint *) &cur_malloc_words); + if ((cur_malloc_words + words) > MallocMaxTestWords) { + return true; + } + Atomic::add(words, (volatile jint *)&cur_malloc_words); } - return ptr; + return false; } void* os::malloc(size_t size, MEMFLAGS flags) { @@ -607,13 +603,14 @@ #endif NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap()); + + // For the test flag -XX:MallocMaxTestWords + if (has_reached_max_malloc_test_peak(size)) { + return NULL; + } u_char* ptr; - if (MallocMaxTestWords > 0) { - ptr = testMalloc(alloc_size); - } else { - ptr = (u_char*)::malloc(alloc_size); - } + ptr = (u_char*)::malloc(alloc_size); #ifdef ASSERT if (ptr == NULL) { @@ -642,6 +639,11 @@ void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCallStack& stack) { + // For the test flag -XX:MallocMaxTestWords + if (has_reached_max_malloc_test_peak(size)) { + return NULL; + } + #ifndef ASSERT NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1)); NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));