Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.ArenaImpl;
import jdk.internal.foreign.NoInitSegmentAllocator;
import jdk.internal.foreign.SlicingAllocator;
import jdk.internal.foreign.StringSupport;
import jdk.internal.vm.annotation.ForceInline;
Expand Down Expand Up @@ -720,22 +721,22 @@ private static void assertWritable(MemorySegment segment) {

@ForceInline
private MemorySegment allocateNoInit(long byteSize) {
return this instanceof ArenaImpl arenaImpl ?
arenaImpl.allocateNoInit(byteSize, 1) :
return this instanceof NoInitSegmentAllocator noInit ?
noInit.allocateNoInit(byteSize, 1) :
allocate(byteSize);
}

@ForceInline
private MemorySegment allocateNoInit(MemoryLayout layout) {
return this instanceof ArenaImpl arenaImpl ?
arenaImpl.allocateNoInit(layout.byteSize(), layout.byteAlignment()) :
return this instanceof NoInitSegmentAllocator noInit ?
noInit.allocateNoInit(layout.byteSize(), layout.byteAlignment()) :
allocate(layout);
}

@ForceInline
private MemorySegment allocateNoInit(MemoryLayout layout, long size) {
return this instanceof ArenaImpl arenaImpl ?
arenaImpl.allocateNoInit(layout.byteSize() * size, layout.byteAlignment()) :
return this instanceof NoInitSegmentAllocator noInit ?
noInit.allocateNoInit(layout.byteSize() * size, layout.byteAlignment()) :
allocate(layout, size);
}
}
11 changes: 7 additions & 4 deletions src/java.base/share/classes/jdk/internal/foreign/ArenaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

package jdk.internal.foreign;

import jdk.internal.vm.annotation.ForceInline;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment.Scope;

public final class ArenaImpl implements Arena {
public final class ArenaImpl implements Arena, NoInitSegmentAllocator {

private final MemorySessionImpl session;
private final boolean shouldReserveMemory;
Expand All @@ -47,15 +49,16 @@ public void close() {
session.close();
}

@ForceInline
@Override
public NativeMemorySegmentImpl allocateNoInit(long byteSize, long byteAlignment) {
Utils.checkAllocationSizeAndAlign(byteSize, byteAlignment);
return SegmentFactories.allocateSegment(byteSize, byteAlignment, session, shouldReserveMemory);
}

@ForceInline
@Override
public NativeMemorySegmentImpl allocate(long byteSize, long byteAlignment) {
NativeMemorySegmentImpl segment = allocateNoInit(byteSize, byteAlignment);
segment.fill((byte)0);
return segment;
return NoInitSegmentAllocator.super.allocate(byteSize, byteAlignment);
}
}
Loading