24 |
|
import java.util.function.Consumer; |
25 |
|
import java.util.function.Predicate; |
26 |
|
// OPENJDK import jdk.internal.access.SharedSecrets; |
27 |
+ |
import jdk.internal.util.ArraysSupport; |
28 |
|
|
29 |
|
/** |
30 |
|
* An unbounded {@linkplain BlockingQueue blocking queue} that uses |
109 |
|
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
110 |
|
|
111 |
|
/** |
111 |
– |
* The maximum size of array to allocate. |
112 |
– |
* Some VMs reserve some header words in an array. |
113 |
– |
* Attempts to allocate larger arrays may result in |
114 |
– |
* OutOfMemoryError: Requested array size exceeds VM limit |
115 |
– |
*/ |
116 |
– |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
117 |
– |
|
118 |
– |
/** |
112 |
|
* Priority queue represented as a balanced binary heap: the two |
113 |
|
* children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The |
114 |
|
* priority queue is ordered by comparator, or by the elements' |
262 |
|
if (allocationSpinLock == 0 && |
263 |
|
ALLOCATIONSPINLOCK.compareAndSet(this, 0, 1)) { |
264 |
|
try { |
265 |
< |
int newCap = oldCap + ((oldCap < 64) ? |
266 |
< |
(oldCap + 2) : // grow faster if small |
267 |
< |
(oldCap >> 1)); |
275 |
< |
if (newCap - MAX_ARRAY_SIZE > 0) { // possible overflow |
276 |
< |
int minCap = oldCap + 1; |
277 |
< |
if (minCap < 0 || minCap > MAX_ARRAY_SIZE) |
278 |
< |
throw new OutOfMemoryError(); |
279 |
< |
newCap = MAX_ARRAY_SIZE; |
280 |
< |
} |
281 |
< |
if (newCap > oldCap && queue == array) |
265 |
> |
int growth = oldCap < 64 ? oldCap + 2 : oldCap >> 1; |
266 |
> |
int newCap = ArraysSupport.newLength(oldCap, 1, growth); |
267 |
> |
if (queue == array) |
268 |
|
newArray = new Object[newCap]; |
269 |
|
} finally { |
270 |
|
allocationSpinLock = 0; |