8341243: Use ArraySupport.SOFT_MAX_ARRAY_LENGTH for max array size in java.base

Reviewed-by: jpai, smarks
This commit is contained in:
Eirik Bjørsnøs 2024-10-02 01:27:03 +00:00 committed by Jaikiran Pai
parent 8d6d37fea1
commit 0f381137cb
15 changed files with 51 additions and 41 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2024, 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
@ -25,6 +25,8 @@
package java.io;
import jdk.internal.util.ArraysSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -305,12 +307,9 @@ public abstract class InputStream implements Closeable {
}
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
* The maximum size of array to allocate
*/
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
private static final int MAX_BUFFER_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
/**
* Reads all remaining bytes from the input stream. This method blocks until

View file

@ -38,6 +38,7 @@ import java.io.Serializable;
import java.util.function.Consumer;
import java.util.function.Predicate;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ArraysSupport;
/**
* Resizable-array implementation of the {@link Deque} interface. Array
@ -124,12 +125,9 @@ public class ArrayDeque<E> extends AbstractCollection<E>
transient int tail;
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
* The maximum size of array to allocate
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
/**
* Increases the capacity of this deque by at least the given amount.

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2024, 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
@ -1184,7 +1184,7 @@ public class BitSet implements Cloneable, java.io.Serializable {
public String toString() {
checkInvariants();
final int MAX_INITIAL_CAPACITY = Integer.MAX_VALUE - 8;
final int MAX_INITIAL_CAPACITY = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
int numBits = (wordsInUse > 128) ?
cardinality() : wordsInUse * BITS_PER_WORD;
// Avoid overflow in the case of a humongous numBits

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2024, 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
@ -30,6 +30,7 @@ import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.BiFunction;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ArraysSupport;
/**
* This class implements a hash table, which maps keys to values. Any
@ -390,12 +391,9 @@ public class Hashtable<K,V>
}
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
* The maximum size of array to allocate
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
/**
* Increases the capacity of and internally reorganizes this

View file

@ -69,6 +69,7 @@ import java.util.function.ToLongBiFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Stream;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.ArraysSupport;
/**
* A hash table supporting full concurrency of retrievals and
@ -517,7 +518,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* The largest possible (non-power of two) array size.
* Needed by toArray and related methods.
*/
static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
/**
* The default concurrency level for this table. Unused but

View file

@ -1502,8 +1502,8 @@ public final class Pattern
return "\\Q" + s + "\\E";
int lenHint = s.length();
lenHint = (lenHint < Integer.MAX_VALUE - 8 - lenHint) ?
(lenHint << 1) : (Integer.MAX_VALUE - 8);
lenHint = (lenHint < ArraysSupport.SOFT_MAX_ARRAY_LENGTH - lenHint) ?
(lenHint << 1) : ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
StringBuilder sb = new StringBuilder(lenHint);
sb.append("\\Q");

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, 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
@ -24,6 +24,8 @@
*/
package java.util.stream;
import jdk.internal.util.ArraysSupport;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
@ -57,7 +59,7 @@ final class Nodes {
/**
* The maximum size of an array that can be allocated.
*/
static final long MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
static final long MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
// IllegalArgumentException messages
static final String BAD_SIZE = "Stream size exceeds max array size";