This commit is contained in:
Jesper Wilhelmsson 2022-06-21 22:26:26 +00:00
commit 2bf5c9a687
31 changed files with 260 additions and 166 deletions

View file

@ -883,9 +883,7 @@ public sealed interface MemorySegment extends Addressable permits AbstractMemory
Reflection.ensureNativeAccess(Reflection.getCallerClass(), MemorySegment.class, "ofAddress");
Objects.requireNonNull(address);
Objects.requireNonNull(session);
if (bytesSize < 0) {
throw new IllegalArgumentException("Invalid size : " + bytesSize);
}
Utils.checkAllocationSizeAndAlign(bytesSize, 1);
return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(address, bytesSize, session);
}
@ -957,15 +955,7 @@ public sealed interface MemorySegment extends Addressable permits AbstractMemory
*/
static MemorySegment allocateNative(long bytesSize, long alignmentBytes, MemorySession session) {
Objects.requireNonNull(session);
if (bytesSize < 0) {
throw new IllegalArgumentException("Invalid allocation size : " + bytesSize);
}
if (alignmentBytes <= 0 ||
((alignmentBytes & (alignmentBytes - 1)) != 0L)) {
throw new IllegalArgumentException("Invalid alignment constraint : " + alignmentBytes);
}
Utils.checkAllocationSizeAndAlign(bytesSize, alignmentBytes);
return NativeMemorySegmentImpl.makeNativeSegment(bytesSize, alignmentBytes, session);
}

View file

@ -153,10 +153,7 @@ public abstract non-sealed class AbstractMemorySegmentImpl implements MemorySegm
@Override
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
if (bytesAlignment <= 0 ||
((bytesAlignment & (bytesAlignment - 1)) != 0L)) {
throw new IllegalArgumentException("Invalid alignment constraint : " + bytesAlignment);
}
Utils.checkAllocationSizeAndAlign(bytesSize, bytesAlignment);
return asSlice(0, bytesSize);
}

View file

@ -71,6 +71,7 @@ public final class ArenaAllocator implements SegmentAllocator {
@Override
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
Utils.checkAllocationSizeAndAlign(bytesSize, bytesAlignment);
// try to slice from current segment first...
MemorySegment slice = trySlice(bytesSize, bytesAlignment);
if (slice != null) {

View file

@ -162,4 +162,17 @@ public final class Utils {
throw new IllegalArgumentException(msg);
}
}
public static void checkAllocationSizeAndAlign(long bytesSize, long alignmentBytes) {
// size should be >= 0
if (bytesSize < 0) {
throw new IllegalArgumentException("Invalid allocation size : " + bytesSize);
}
// alignment should be > 0, and power of two
if (alignmentBytes <= 0 ||
((alignmentBytes & (alignmentBytes - 1)) != 0L)) {
throw new IllegalArgumentException("Invalid alignment constraint : " + alignmentBytes);
}
}
}