1 |
|
/* |
2 |
< |
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
|
* This code is free software; you can redistribute it and/or modify it |
29 |
|
import java.util.function.Predicate; |
30 |
|
import java.util.function.UnaryOperator; |
31 |
|
// OPENJDK import jdk.internal.access.SharedSecrets; |
32 |
+ |
import jdk.internal.util.ArraysSupport; |
33 |
|
|
34 |
|
/** |
35 |
|
* Resizable-array implementation of the {@code List} interface. Implements |
220 |
|
} |
221 |
|
|
222 |
|
/** |
222 |
– |
* The maximum size of array to allocate (unless necessary). |
223 |
– |
* Some VMs reserve some header words in an array. |
224 |
– |
* Attempts to allocate larger arrays may result in |
225 |
– |
* OutOfMemoryError: Requested array size exceeds VM limit |
226 |
– |
*/ |
227 |
– |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
228 |
– |
|
229 |
– |
/** |
223 |
|
* Increases the capacity to ensure that it can hold at least the |
224 |
|
* number of elements specified by the minimum capacity argument. |
225 |
|
* |
227 |
|
* @throws OutOfMemoryError if minCapacity is less than zero |
228 |
|
*/ |
229 |
|
private Object[] grow(int minCapacity) { |
237 |
– |
return elementData = Arrays.copyOf(elementData, |
238 |
– |
newCapacity(minCapacity)); |
239 |
– |
} |
240 |
– |
|
241 |
– |
private Object[] grow() { |
242 |
– |
return grow(size + 1); |
243 |
– |
} |
244 |
– |
|
245 |
– |
/** |
246 |
– |
* Returns a capacity at least as large as the given minimum capacity. |
247 |
– |
* Returns the current capacity increased by 50% if that suffices. |
248 |
– |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
249 |
– |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
250 |
– |
* |
251 |
– |
* @param minCapacity the desired minimum capacity |
252 |
– |
* @throws OutOfMemoryError if minCapacity is less than zero |
253 |
– |
*/ |
254 |
– |
private int newCapacity(int minCapacity) { |
255 |
– |
// overflow-conscious code |
230 |
|
int oldCapacity = elementData.length; |
231 |
< |
int newCapacity = oldCapacity + (oldCapacity >> 1); |
232 |
< |
if (newCapacity - minCapacity <= 0) { |
233 |
< |
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) |
234 |
< |
return Math.max(DEFAULT_CAPACITY, minCapacity); |
235 |
< |
if (minCapacity < 0) // overflow |
236 |
< |
throw new OutOfMemoryError(); |
237 |
< |
return minCapacity; |
231 |
> |
if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { |
232 |
> |
int newCapacity = ArraysSupport.newLength(oldCapacity, |
233 |
> |
minCapacity - oldCapacity, /* minimum growth */ |
234 |
> |
oldCapacity >> 1 /* preferred growth */); |
235 |
> |
return elementData = Arrays.copyOf(elementData, newCapacity); |
236 |
> |
} else { |
237 |
> |
return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)]; |
238 |
|
} |
265 |
– |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
266 |
– |
? newCapacity |
267 |
– |
: hugeCapacity(minCapacity); |
239 |
|
} |
240 |
|
|
241 |
< |
private static int hugeCapacity(int minCapacity) { |
242 |
< |
if (minCapacity < 0) // overflow |
272 |
< |
throw new OutOfMemoryError(); |
273 |
< |
return (minCapacity > MAX_ARRAY_SIZE) |
274 |
< |
? Integer.MAX_VALUE |
275 |
< |
: MAX_ARRAY_SIZE; |
241 |
> |
private Object[] grow() { |
242 |
> |
return grow(size + 1); |
243 |
|
} |
244 |
|
|
245 |
|
/** |