mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8223593: Refactor code for reallocating storage
Reviewed-by: prappo, plevart, rriggs, smarks
This commit is contained in:
parent
54d0b2a8d6
commit
218204b1a3
11 changed files with 129 additions and 247 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1994, 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
|
||||
|
@ -32,6 +32,8 @@ import java.util.function.Consumer;
|
|||
import java.util.function.Predicate;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import jdk.internal.util.ArraysSupport;
|
||||
|
||||
/**
|
||||
* The {@code Vector} class implements a growable array of
|
||||
* objects. Like an array, it contains components that can be
|
||||
|
@ -241,14 +243,6 @@ public class Vector<E>
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum size of array to allocate (unless necessary).
|
||||
* 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 to ensure that it can hold at least the
|
||||
* number of elements specified by the minimum capacity argument.
|
||||
|
@ -257,45 +251,18 @@ public class Vector<E>
|
|||
* @throws OutOfMemoryError if minCapacity is less than zero
|
||||
*/
|
||||
private Object[] grow(int minCapacity) {
|
||||
return elementData = Arrays.copyOf(elementData,
|
||||
newCapacity(minCapacity));
|
||||
int oldCapacity = elementData.length;
|
||||
int newCapacity = ArraysSupport.newLength(oldCapacity,
|
||||
minCapacity - oldCapacity, /* minimum growth */
|
||||
capacityIncrement > 0 ? capacityIncrement : oldCapacity
|
||||
/* preferred growth */);
|
||||
return elementData = Arrays.copyOf(elementData, newCapacity);
|
||||
}
|
||||
|
||||
private Object[] grow() {
|
||||
return grow(elementCount + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a capacity at least as large as the given minimum capacity.
|
||||
* Will not return a capacity greater than MAX_ARRAY_SIZE unless
|
||||
* the given minimum capacity is greater than MAX_ARRAY_SIZE.
|
||||
*
|
||||
* @param minCapacity the desired minimum capacity
|
||||
* @throws OutOfMemoryError if minCapacity is less than zero
|
||||
*/
|
||||
private int newCapacity(int minCapacity) {
|
||||
// overflow-conscious code
|
||||
int oldCapacity = elementData.length;
|
||||
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
|
||||
capacityIncrement : oldCapacity);
|
||||
if (newCapacity - minCapacity <= 0) {
|
||||
if (minCapacity < 0) // overflow
|
||||
throw new OutOfMemoryError();
|
||||
return minCapacity;
|
||||
}
|
||||
return (newCapacity - MAX_ARRAY_SIZE <= 0)
|
||||
? newCapacity
|
||||
: hugeCapacity(minCapacity);
|
||||
}
|
||||
|
||||
private static int hugeCapacity(int minCapacity) {
|
||||
if (minCapacity < 0) // overflow
|
||||
throw new OutOfMemoryError();
|
||||
return (minCapacity > MAX_ARRAY_SIZE) ?
|
||||
Integer.MAX_VALUE :
|
||||
MAX_ARRAY_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the size of this vector. If the new size is greater than the
|
||||
* current size, new {@code null} items are added to the end of
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue