1.1 --- a/src/share/vm/runtime/os.cpp Tue Mar 19 13:44:26 2013 +0100 1.2 +++ b/src/share/vm/runtime/os.cpp Tue Mar 19 11:33:11 2013 -0700 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -80,6 +80,8 @@ 1.11 julong os::free_bytes = 0; // # of bytes freed 1.12 #endif 1.13 1.14 +static juint cur_malloc_words = 0; // current size for MallocMaxTestWords 1.15 + 1.16 void os_init_globals() { 1.17 // Called from init_globals(). 1.18 // See Threads::create_vm() in thread.cpp, and init.cpp. 1.19 @@ -570,6 +572,26 @@ 1.20 } 1.21 #endif 1.22 1.23 +// 1.24 +// This function supports testing of the malloc out of memory 1.25 +// condition without really running the system out of memory. 1.26 +// 1.27 +static u_char* testMalloc(size_t alloc_size) { 1.28 + 1.29 + if (MallocMaxTestWords > 0 && 1.30 + (cur_malloc_words + (alloc_size / BytesPerWord)) > MallocMaxTestWords) { 1.31 + return NULL; 1.32 + } 1.33 + 1.34 + u_char* ptr = (u_char*)::malloc(alloc_size); 1.35 + 1.36 + if (MallocMaxTestWords > 0 && (ptr != NULL)) { 1.37 + Atomic::add(((jint) (alloc_size / BytesPerWord)), 1.38 + (volatile jint *) &cur_malloc_words); 1.39 + } 1.40 + return ptr; 1.41 +} 1.42 + 1.43 void* os::malloc(size_t size, MEMFLAGS memflags, address caller) { 1.44 NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1)); 1.45 NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size)); 1.46 @@ -579,11 +601,22 @@ 1.47 // if NULL is returned the calling functions assume out of memory. 1.48 size = 1; 1.49 } 1.50 - if (size > size + space_before + space_after) { // Check for rollover. 1.51 + 1.52 + const size_t alloc_size = size + space_before + space_after; 1.53 + 1.54 + if (size > alloc_size) { // Check for rollover. 1.55 return NULL; 1.56 } 1.57 + 1.58 NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap()); 1.59 - u_char* ptr = (u_char*)::malloc(size + space_before + space_after); 1.60 + 1.61 + u_char* ptr; 1.62 + 1.63 + if (MallocMaxTestWords > 0) { 1.64 + ptr = testMalloc(alloc_size); 1.65 + } else { 1.66 + ptr = (u_char*)::malloc(alloc_size); 1.67 + } 1.68 1.69 #ifdef ASSERT 1.70 if (ptr == NULL) return NULL;