--- jsr166/src/main/java/util/PriorityQueue.java 2018/05/06 23:29:25 1.128
+++ jsr166/src/main/java/util/PriorityQueue.java 2019/10/10 16:53:08 1.133
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2019, 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
@@ -27,7 +27,8 @@ package java.util;
import java.util.function.Consumer;
import java.util.function.Predicate;
-import jdk.internal.misc.SharedSecrets;
+// OPENJDK import jdk.internal.access.SharedSecrets;
+import jdk.internal.util.ArraysSupport;
/**
* An unbounded priority {@linkplain Queue queue} based on a priority heap.
@@ -75,7 +76,7 @@ import jdk.internal.misc.SharedSecrets;
* ({@code peek}, {@code element}, and {@code size}).
*
*
This class is a member of the
- *
+ *
* Java Collections Framework.
*
* @since 1.5
@@ -86,6 +87,7 @@ import jdk.internal.misc.SharedSecrets;
public class PriorityQueue extends AbstractQueue
implements java.io.Serializable {
+ // OPENJDK @java.io.Serial
private static final long serialVersionUID = -7720805057305804111L;
private static final int DEFAULT_INITIAL_CAPACITY = 11;
@@ -109,6 +111,7 @@ public class PriorityQueue extends Ab
* The comparator, or null if priority queue uses elements'
* natural ordering.
*/
+ @SuppressWarnings("serial") // Conditionally serializable
private final Comparator super E> comparator;
/**
@@ -282,14 +285,6 @@ public class PriorityQueue extends Ab
}
/**
- * 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
- */
- private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
-
- /**
* Increases the capacity of the array.
*
* @param minCapacity the desired minimum capacity
@@ -297,23 +292,13 @@ public class PriorityQueue extends Ab
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
- int newCapacity = oldCapacity + ((oldCapacity < 64) ?
- (oldCapacity + 2) :
- (oldCapacity >> 1));
- // overflow-conscious code
- if (newCapacity - MAX_ARRAY_SIZE > 0)
- newCapacity = hugeCapacity(minCapacity);
+ int newCapacity = ArraysSupport.newLength(oldCapacity,
+ minCapacity - oldCapacity, /* minimum growth */
+ oldCapacity < 64 ? oldCapacity + 2 : oldCapacity >> 1
+ /* preferred growth */);
queue = Arrays.copyOf(queue, newCapacity);
}
- private static int hugeCapacity(int minCapacity) {
- if (minCapacity < 0) // overflow
- throw new OutOfMemoryError();
- return (minCapacity > MAX_ARRAY_SIZE) ?
- Integer.MAX_VALUE :
- MAX_ARRAY_SIZE;
- }
-
/**
* Inserts the specified element into this priority queue.
*
@@ -772,6 +757,7 @@ public class PriorityQueue extends Ab
* emitted (int), followed by all of its elements
* (each an {@code Object}) in the proper order.
*/
+ // OPENJDK @java.io.Serial
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out element count, and any hidden stuff
@@ -795,6 +781,7 @@ public class PriorityQueue extends Ab
* could not be found
* @throws java.io.IOException if an I/O error occurs
*/
+ // OPENJDK @java.io.Serial
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
@@ -803,7 +790,7 @@ public class PriorityQueue extends Ab
// Read in (and discard) array length
s.readInt();
- SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
+ jsr166.Platform.checkArray(s, Object[].class, size);
final Object[] es = queue = new Object[Math.max(size, 1)];
// Read in all elements.