Skip to content

Commit

Permalink
In Arrays.copyOf and Array.multiNewArray, use reference size and corr…
Browse files Browse the repository at this point in the history
…ect end offset
  • Loading branch information
peter-hofer committed Jul 17, 2018
1 parent 1368704 commit 78cc0b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ private static Object newMultiArrayRecursion(DynamicHub hub, int rank, Pointer d

if (rank > 1) {
UnsignedWord offset = LayoutEncoding.getArrayBaseOffset(hub.getLayoutEncoding());
UnsignedWord size = LayoutEncoding.getArraySize(hub.getLayoutEncoding(), length);
UnsignedWord endOffset = LayoutEncoding.getArrayElementOffset(hub.getLayoutEncoding(), length);

while (offset.belowThan(size)) {
while (offset.belowThan(endOffset)) {
// Each newMultiArrayRecursion could create a cross-generational reference.
BarrieredAccess.writeObject(result, offset,
newMultiArrayRecursion(hub.getComponentHub(), rank - 1, dimensionsStackValue.add(sizeOfDimensionElement), counter));
Expand All @@ -360,32 +360,26 @@ private static Object newMultiArrayRecursion(DynamicHub hub, int rank, Pointer d
public static Object arraysCopyOfSnippet(DynamicHub hub, Object original, int originalLength, int newLength, AllocationCounter counter) {
int layoutEncoding = hub.getLayoutEncoding();
// allocate new array without initializing the new array
Object copy = newArraySnippet(hub, newLength, layoutEncoding, false, counter);

// copy elements from original
Object newArray = newArraySnippet(hub, newLength, layoutEncoding, false, counter);

// The length of the copied section
int length = originalLength < newLength ? originalLength : newLength;
// The size of the copied section(obtained from the length of the copied section)
UnsignedWord size = LayoutEncoding.getArraySize(layoutEncoding, length);
// The size of the new array (obtained from the length of the new array)
UnsignedWord newSize = LayoutEncoding.getArraySize(layoutEncoding, newLength);
int copiedLength = originalLength < newLength ? originalLength : newLength;
UnsignedWord copiedEndOffset = LayoutEncoding.getArrayElementOffset(layoutEncoding, copiedLength);
UnsignedWord newArrayEndOffset = LayoutEncoding.getArrayElementOffset(layoutEncoding, newLength);

// We know that the offset is always word aligned
UnsignedWord offset = LayoutEncoding.getArrayBaseOffset(layoutEncoding);
while (offset.belowThan(size)) {
while (offset.belowThan(copiedEndOffset)) {
Object val = ObjectAccess.readObject(original, offset);
ObjectAccess.writeObject(copy, offset, val, LocationIdentity.INIT_LOCATION);
ObjectAccess.writeObject(newArray, offset, val, LocationIdentity.INIT_LOCATION);

offset = offset.add(ConfigurationValues.getTarget().wordSize);
offset = offset.add(ConfigurationValues.getObjectLayout().getReferenceSize());
}

while (offset.belowThan(newSize)) {
ObjectAccess.writeObject(copy, offset, null, LocationIdentity.INIT_LOCATION);
offset = offset.add(ConfigurationValues.getTarget().wordSize);
while (offset.belowThan(newArrayEndOffset)) {
ObjectAccess.writeObject(newArray, offset, null, LocationIdentity.INIT_LOCATION);
offset = offset.add(ConfigurationValues.getObjectLayout().getReferenceSize());
}

return FixedValueAnchorNode.getObject(copy);
return FixedValueAnchorNode.getObject(newArray);
}

private static final CloneNotSupportedException CLONE_NOT_SUPPORTED_EXCEPTION = new CloneNotSupportedException("Object is not instance of Cloneable.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ static Object createMultiArrayAtIndex(int index, DynamicHub arrayHub, int[] dime
DynamicHub subArrayHub = arrayHub.getComponentHub();

UnsignedWord offset = LayoutEncoding.getArrayBaseOffset(arrayHub.getLayoutEncoding());
UnsignedWord size = LayoutEncoding.getArraySize(arrayHub.getLayoutEncoding(), length);
while (offset.belowThan(size)) {
UnsignedWord endOffset = LayoutEncoding.getArrayElementOffset(arrayHub.getLayoutEncoding(), length);
while (offset.belowThan(endOffset)) {
Object subArray = createMultiArrayAtIndex(nextIndex, subArrayHub, dimensions);
// Each subArray could create a cross-generational reference.
BarrieredAccess.writeObject(result, offset, subArray);
Expand Down

0 comments on commit 78cc0b2

Please sign in to comment.