< prev index next >

src/java.base/share/classes/java/util/PriorityQueue.java

Print this page

        

*** 1,7 **** /* ! * 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2003, 2018, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 26,36 **** package java.util; import java.util.function.Consumer; import java.util.function.Predicate; import jdk.internal.access.SharedSecrets; - import jdk.internal.util.ArraysSupport; /** * An unbounded priority {@linkplain Queue queue} based on a priority heap. * The elements of the priority queue are ordered according to their * {@linkplain Comparable natural ordering}, or by a {@link Comparator} --- 26,35 ----
*** 281,304 **** initElementsFromCollection(c); heapify(); } /** * Increases the capacity of the array. * * @param minCapacity the desired minimum capacity */ private void grow(int minCapacity) { int oldCapacity = queue.length; // Double size if small; else grow by 50% ! int newCapacity = ArraysSupport.newLength(oldCapacity, ! minCapacity - oldCapacity, /* minimum growth */ ! oldCapacity < 64 ? oldCapacity + 2 : oldCapacity >> 1 ! /* preferred growth */); queue = Arrays.copyOf(queue, newCapacity); } /** * Inserts the specified element into this priority queue. * * @return {@code true} (as specified by {@link Collection#add}) * @throws ClassCastException if the specified element cannot be --- 280,321 ---- initElementsFromCollection(c); heapify(); } /** + * 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 */ 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); 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. * * @return {@code true} (as specified by {@link Collection#add}) * @throws ClassCastException if the specified element cannot be
< prev index next >