--- old/src/hotspot/cpu/aarch64/gc/z/zGlobals_aarch64.cpp 2019-11-12 10:07:53.136144575 +0100 +++ new/src/hotspot/cpu/aarch64/gc/z/zGlobals_aarch64.cpp 2019-11-12 10:07:52.936143051 +0100 @@ -144,7 +144,7 @@ const size_t max_address_offset_bits = 44; // 16TB const size_t address_offset = ZUtils::round_up_power_of_2(MaxHeapSize * ZVirtualToPhysicalRatio); const size_t address_offset_bits = log2_intptr(address_offset); - return MIN2(MAX2(address_offset_bits, min_address_offset_bits), max_address_offset_bits); + return clamp(address_offset_bits, min_address_offset_bits, max_address_offset_bits); } size_t ZPlatformAddressMetadataShift() { --- old/src/hotspot/cpu/x86/gc/z/zGlobals_x86.cpp 2019-11-12 10:07:53.812149722 +0100 +++ new/src/hotspot/cpu/x86/gc/z/zGlobals_x86.cpp 2019-11-12 10:07:53.592148048 +0100 @@ -144,7 +144,7 @@ const size_t max_address_offset_bits = 44; // 16TB const size_t address_offset = ZUtils::round_up_power_of_2(MaxHeapSize * ZVirtualToPhysicalRatio); const size_t address_offset_bits = log2_intptr(address_offset); - return MIN2(MAX2(address_offset_bits, min_address_offset_bits), max_address_offset_bits); + return clamp(address_offset_bits, min_address_offset_bits, max_address_offset_bits); } size_t ZPlatformAddressMetadataShift() { --- old/src/hotspot/share/compiler/compilerDefinitions.cpp 2019-11-12 10:07:54.408154262 +0100 +++ new/src/hotspot/share/compiler/compilerDefinitions.cpp 2019-11-12 10:07:54.212152769 +0100 @@ -349,7 +349,7 @@ FLAG_SET_DEFAULT(InitialCodeCacheSize, MAX2(16*M, InitialCodeCacheSize)); } if (FLAG_IS_DEFAULT(MetaspaceSize)) { - FLAG_SET_DEFAULT(MetaspaceSize, MIN2(MAX2(12*M, MetaspaceSize), MaxMetaspaceSize)); + FLAG_SET_DEFAULT(MetaspaceSize, clamp(MetaspaceSize, 12*M, MaxMetaspaceSize)); } if (FLAG_IS_DEFAULT(NewSizeThreadIncrease)) { FLAG_SET_DEFAULT(NewSizeThreadIncrease, MAX2(4*K, NewSizeThreadIncrease)); --- old/src/hotspot/share/gc/epsilon/epsilonHeap.cpp 2019-11-12 10:07:55.036159043 +0100 +++ new/src/hotspot/share/gc/epsilon/epsilonHeap.cpp 2019-11-12 10:07:54.836157521 +0100 @@ -212,7 +212,7 @@ } // Always honor boundaries - size = MAX2(min_size, MIN2(_max_tlab_size, size)); + size = clamp(size, min_size, _max_tlab_size); // Always honor alignment size = align_up(size, MinObjAlignment); --- old/src/hotspot/share/gc/g1/g1Allocator.cpp 2019-11-12 10:07:55.728164313 +0100 +++ new/src/hotspot/share/gc/g1/g1Allocator.cpp 2019-11-12 10:07:55.508162638 +0100 @@ -151,7 +151,7 @@ if (hr == NULL) { return max_tlab; } else { - return MIN2(MAX2(hr->free(), (size_t) MinTLABSize), max_tlab); + return clamp(hr->free(), MinTLABSize, max_tlab); } } --- old/src/hotspot/share/gc/g1/g1ConcurrentMark.cpp 2019-11-12 10:07:56.412169522 +0100 +++ new/src/hotspot/share/gc/g1/g1ConcurrentMark.cpp 2019-11-12 10:07:56.196167877 +0100 @@ -1611,7 +1611,7 @@ // we utilize all the worker threads we can. bool processing_is_mt = rp->processing_is_mt(); uint active_workers = (processing_is_mt ? _g1h->workers()->active_workers() : 1U); - active_workers = MAX2(MIN2(active_workers, _max_num_tasks), 1U); + active_workers = clamp(active_workers, 1u, _max_num_tasks); // Parallel processing task executor. G1CMRefProcTaskExecutor par_task_executor(_g1h, this, --- old/src/hotspot/share/gc/parallel/asPSYoungGen.cpp 2019-11-12 10:07:57.076174579 +0100 +++ new/src/hotspot/share/gc/parallel/asPSYoungGen.cpp 2019-11-12 10:07:56.880173087 +0100 @@ -167,8 +167,7 @@ // Adjust new generation size const size_t eden_plus_survivors = align_up(eden_size + 2 * survivor_size, alignment); - size_t desired_size = MAX2(MIN2(eden_plus_survivors, gen_size_limit()), - min_gen_size()); + size_t desired_size = clamp(eden_plus_survivors, min_gen_size(), gen_size_limit()); assert(desired_size <= gen_size_limit(), "just checking"); if (desired_size > orig_size) { --- old/src/hotspot/share/gc/parallel/psOldGen.cpp 2019-11-12 10:07:57.712179423 +0100 +++ new/src/hotspot/share/gc/parallel/psOldGen.cpp 2019-11-12 10:07:57.504177839 +0100 @@ -355,7 +355,7 @@ new_size = gen_size_limit(); } // Adjust according to our min and max - new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size()); + new_size = clamp(new_size, min_gen_size(), gen_size_limit()); assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?"); new_size = align_up(new_size, alignment); --- old/src/hotspot/share/gc/parallel/psYoungGen.cpp 2019-11-12 10:07:58.324184083 +0100 +++ new/src/hotspot/share/gc/parallel/psYoungGen.cpp 2019-11-12 10:07:58.132182621 +0100 @@ -300,8 +300,7 @@ // Adjust new generation size const size_t eden_plus_survivors = align_up(eden_size + 2 * survivor_size, alignment); - size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()), - min_gen_size()); + size_t desired_size = clamp(eden_plus_survivors, min_gen_size(), max_size()); assert(desired_size <= max_size(), "just checking"); if (desired_size > orig_size) { --- old/src/hotspot/share/gc/serial/defNewGeneration.cpp 2019-11-12 10:07:58.992189171 +0100 +++ new/src/hotspot/share/gc/serial/defNewGeneration.cpp 2019-11-12 10:07:58.796187678 +0100 @@ -393,7 +393,7 @@ size_t desired_new_size = adjust_for_thread_increase(new_size_candidate, new_size_before, alignment); // Adjust new generation size - desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size); + desired_new_size = clamp(desired_new_size, min_new_size, max_new_size); assert(desired_new_size <= max_new_size, "just checking"); bool changed = false; --- old/src/hotspot/share/gc/shared/genArguments.cpp 2019-11-12 10:07:59.672194350 +0100 +++ new/src/hotspot/share/gc/shared/genArguments.cpp 2019-11-12 10:07:59.464192765 +0100 @@ -229,7 +229,7 @@ // yield a size that is too small) and bound it by MaxNewSize above. // Ergonomics plays here by previously calculating the desired // NewSize and MaxNewSize. - max_young_size = MIN2(MAX2(max_young_size, NewSize), MaxNewSize); + max_young_size = clamp(max_young_size, NewSize, MaxNewSize); } // Given the maximum young size, determine the initial and @@ -260,7 +260,7 @@ // NewSize as the floor, because if NewRatio is overly large, the resulting // size can be too small. initial_young_size = - MIN2(max_young_size, MAX2(scale_by_NewRatio_aligned(InitialHeapSize, GenAlignment), NewSize)); + clamp(scale_by_NewRatio_aligned(InitialHeapSize, GenAlignment), NewSize, max_young_size); } } @@ -285,7 +285,7 @@ // the minimum, maximum and initial sizes consistent // with the young sizes and the overall heap sizes. MinOldSize = GenAlignment; - initial_old_size = MIN2(MaxOldSize, MAX2(InitialHeapSize - initial_young_size, MinOldSize)); + initial_old_size = clamp(InitialHeapSize - initial_young_size, MinOldSize, MaxOldSize); // MaxOldSize has already been made consistent above. } else { // OldSize has been explicitly set on the command line. Use it --- old/src/hotspot/share/gc/shared/plab.cpp 2019-11-12 10:08:00.328199346 +0100 +++ new/src/hotspot/share/gc/shared/plab.cpp 2019-11-12 10:08:00.108197670 +0100 @@ -135,7 +135,7 @@ // Calculates plab size for current number of gc worker threads. size_t PLABStats::desired_plab_sz(uint no_of_gc_workers) { - return align_object_size(MIN2(MAX2(min_size(), _desired_net_plab_sz / no_of_gc_workers), max_size())); + return align_object_size(clamp(_desired_net_plab_sz / no_of_gc_workers, min_size(), max_size())); } // Compute desired plab size for one gc worker thread and latch result for later --- old/src/hotspot/share/gc/shared/taskqueue.cpp 2019-11-12 10:08:01.008204524 +0100 +++ new/src/hotspot/share/gc/shared/taskqueue.cpp 2019-11-12 10:08:00.784202819 +0100 @@ -57,7 +57,7 @@ { // Use a width w: 1 <= w <= max_width const unsigned int max_width = 40; - const unsigned int w = MAX2(MIN2(width, max_width), 1U); + const unsigned int w = clamp(width, 1u, max_width); if (line == 0) { // spaces equal in width to the header const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1; --- old/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp 2019-11-12 10:08:01.664209522 +0100 +++ new/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp 2019-11-12 10:08:01.468208029 +0100 @@ -132,7 +132,7 @@ (Universe::heap()->tlab_capacity(thread()) / HeapWordSize)); size_t new_size = alloc / _target_refills; - new_size = MIN2(MAX2(new_size, min_size()), max_size()); + new_size = clamp(new_size, min_size(), max_size()); size_t aligned_new_size = align_object_size(new_size); @@ -251,7 +251,7 @@ (nof_threads * target_refills()); init_sz = align_object_size(init_sz); } - init_sz = MIN2(MAX2(init_sz, min_size()), max_size()); + init_sz = clamp(init_sz, min_size(), max_size()); return init_sz; } --- old/src/hotspot/share/opto/parse2.cpp 2019-11-12 10:08:02.352214761 +0100 +++ new/src/hotspot/share/opto/parse2.cpp 2019-11-12 10:08:02.120212994 +0100 @@ -605,7 +605,7 @@ return PROB_FAIR; } float p = taken_cnt / total_cnt; - return MIN2(MAX2(p, PROB_MIN), PROB_MAX); + return clamp(p, PROB_MIN, PROB_MAX); } static float if_cnt(float cnt) { --- old/src/hotspot/share/utilities/globalDefinitions.hpp 2019-11-12 10:08:03.056220123 +0100 +++ new/src/hotspot/share/utilities/globalDefinitions.hpp 2019-11-12 10:08:02.856218599 +0100 @@ -949,6 +949,12 @@ template inline T ABS(T x) { return (x > 0) ? x : -x; } +// Return the given value clamped to the range [min ... max] +template +inline T clamp(T value, T min, T max) { + return MIN2(MAX2(value, min), max); +} + // true if x is a power of 2, false otherwise inline bool is_power_of_2(intptr_t x) { return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));