Skip to content
Permalink
Browse files
Merge pull request #128 from apache/noSplitPackage
No split package
  • Loading branch information
leerho committed May 12, 2021
2 parents 86c4d20 + 1d875ea commit d964c73a6c7f02c7333716090d2f0fe6ca064a43
Show file tree
Hide file tree
Showing 68 changed files with 1,086 additions and 505 deletions.
@@ -242,13 +242,8 @@ private static long map(final FileChannel fileChannel, final boolean resourceRea
}
}

static boolean isFileReadOnly(final File file) {
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
raf.close();
return false;
} catch (final SecurityException | IOException f) { //could not open for write
return true;
}
public static boolean isFileReadOnly(final File file) {
return (!file.canWrite());
}

private static final class Deallocator implements Runnable {
@@ -54,7 +54,7 @@ final class BBNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl {
BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableBufferImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, byteBuf, originMemory)
@@ -66,7 +66,7 @@ unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | DUPLICATE | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableBufferImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(),
type, byteBuf, originMemory)
@@ -53,7 +53,7 @@ final class BBNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl {
BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableMemoryImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, getByteBuffer())
@@ -65,7 +65,7 @@ unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableBufferImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(),
type, byteBuf, this)
@@ -54,7 +54,7 @@ final class BBWritableBufferImpl extends WritableBufferImpl {
BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableBufferImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, byteBuf, originMemory)
@@ -66,7 +66,7 @@ unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | DUPLICATE | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableBufferImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(),
type, byteBuf, originMemory)
@@ -53,7 +53,7 @@ final class BBWritableMemoryImpl extends WritableMemoryImpl {
BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableMemoryImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, getByteBuffer())
@@ -65,7 +65,7 @@ unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableBufferImpl(
unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(),
type, byteBuf, this)
@@ -37,11 +37,6 @@
@SuppressWarnings({"restriction"})
abstract class BaseState {

//Byte Order related
static final ByteOrder nativeByteOrder = ByteOrder.nativeOrder();
static final ByteOrder nonNativeByteOrder = nativeByteOrder == ByteOrder.LITTLE_ENDIAN
? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;

//Monitoring
static final AtomicLong currentDirectMemoryAllocations_ = new AtomicLong();
static final AtomicLong currentDirectMemoryAllocated_ = new AtomicLong();
@@ -100,21 +95,13 @@ abstract class BaseState {

//Byte Order Related

/**
* Returns the Native Byte Order
* @return the Native Byte Order
*/
public static final ByteOrder getNativeByteOrder() {
return nativeByteOrder;
}

/**
* Gets the current Type ByteOrder.
* This may be different from the ByteOrder of the backing resource and of the Native Byte Order.
* @return the current Type ByteOrder.
*/
public final ByteOrder getTypeByteOrder() {
return isNonNativeType() ? BaseState.nonNativeByteOrder : BaseState.nativeByteOrder;
return isNonNativeType() ? Util.nonNativeByteOrder : Util.nativeByteOrder;
}

/**
@@ -126,7 +113,7 @@ static boolean isNativeByteOrder(final ByteOrder byteOrder) {
if (byteOrder == null) {
throw new IllegalArgumentException("ByteOrder parameter cannot be null.");
}
return BaseState.nativeByteOrder == byteOrder;
return Util.nativeByteOrder == byteOrder;
}

/**
@@ -138,7 +125,7 @@ static boolean isNativeByteOrder(final ByteOrder byteOrder) {
*/
public final boolean isByteOrderCompatible(final ByteOrder byteOrder) {
final ByteOrder typeBO = getTypeByteOrder();
return typeBO == getNativeByteOrder() && typeBO == byteOrder;
return typeBO == Util.nativeByteOrder && typeBO == byteOrder;
}

/**
@@ -364,7 +351,7 @@ final void assertValid() {
assert isValid() : "Memory not valid.";
}

final void checkValid() {
void checkValid() {
if (!isValid()) {
throw new IllegalStateException("Memory not valid.");
}
@@ -548,7 +535,7 @@ static final String toHex(final BaseState state, final String preamble, final lo
sb.append("Valid : ").append(state.isValid()).append(LS);
sb.append("Read Only : ").append(state.isReadOnly()).append(LS);
sb.append("Type Byte Order : ").append(state.getTypeByteOrder().toString()).append(LS);
sb.append("Native Byte Order : ").append(nativeByteOrder.toString()).append(LS);
sb.append("Native Byte Order : ").append(Util.nativeByteOrder.toString()).append(LS);
sb.append("JDK Runtime Version : ").append(UnsafeUtil.JDK).append(LS);
//Data detail
sb.append("Data, littleEndian : 0 1 2 3 4 5 6 7");
@@ -395,7 +395,7 @@ public final void fill(final byte value) {
long len = getEnd() - pos;
checkInvariants(getStart(), pos + len, getEnd(), getCapacity());
while (len > 0) {
final long chunk = Math.min(len, CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES);
final long chunk = Math.min(len, Util.UNSAFE_COPY_THRESHOLD_BYTES);
unsafe.setMemory(getUnsafeObject(), getCumulativeOffset(pos), chunk, value);
pos += chunk;
len -= chunk;
@@ -76,7 +76,7 @@ static BaseWritableMemoryImpl wrapHeapArray(final Object arr, final long offsetB
final long lengthBytes, final boolean localReadOnly, final ByteOrder byteOrder) {
if (lengthBytes == 0) { return BaseWritableMemoryImpl.ZERO_SIZE_MEMORY; }
final int typeId = localReadOnly ? READONLY : 0;
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new HeapWritableMemoryImpl(arr, offsetBytes, lengthBytes, typeId)
: new HeapNonNativeWritableMemoryImpl(arr, offsetBytes, lengthBytes, typeId);
}
@@ -88,7 +88,7 @@ static BaseWritableMemoryImpl wrapByteBuffer(
throw new ReadOnlyException("ByteBuffer is Read Only");
}
final int typeId = (abb.resourceReadOnly || localReadOnly) ? READONLY : 0;
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new BBWritableMemoryImpl(abb.unsafeObj, abb.nativeBaseOffset,
abb.regionOffset, abb.capacityBytes, typeId, byteBuf)
: new BBNonNativeWritableMemoryImpl(abb.unsafeObj, abb.nativeBaseOffset,
@@ -106,7 +106,7 @@ static WritableMapHandle wrapMap(final File file, final long fileOffsetBytes,
throw new ReadOnlyException("File is Read Only");
}
final int typeId = (dirWMap.resourceReadOnly || localReadOnly) ? READONLY : 0;
final BaseWritableMemoryImpl wmem = isNativeByteOrder(byteOrder)
final BaseWritableMemoryImpl wmem = Util.isNativeByteOrder(byteOrder)
? new MapWritableMemoryImpl(dirWMap.nativeBaseOffset, 0L, capacityBytes,
typeId, dirWMap.getValid())
: new MapNonNativeWritableMemoryImpl(dirWMap.nativeBaseOffset, 0L, capacityBytes,
@@ -123,7 +123,7 @@ static WritableDirectHandle wrapDirect(final long capacityBytes,
}
final AllocateDirect direct = new AllocateDirect(capacityBytes);
final int typeId = 0; //direct is never read-only on construction
final BaseWritableMemoryImpl wmem = isNativeByteOrder(byteOrder)
final BaseWritableMemoryImpl wmem = Util.isNativeByteOrder(byteOrder)
? new DirectWritableMemoryImpl(direct.getNativeBaseOffset(), 0L, capacityBytes,
typeId, direct.getValid(), memReqSvr)
: new DirectNonNativeWritableMemoryImpl(direct.getNativeBaseOffset(), 0L, capacityBytes,
@@ -450,7 +450,7 @@ public final void fill(final byte value) {
public final void fill(long offsetBytes, long lengthBytes, final byte value) {
checkValidAndBoundsForWrite(offsetBytes, lengthBytes);
while (lengthBytes > 0) {
final long chunk = Math.min(lengthBytes, CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES);
final long chunk = Math.min(lengthBytes, Util.UNSAFE_COPY_THRESHOLD_BYTES);
unsafe.setMemory(getUnsafeObject(), getCumulativeOffset(offsetBytes), chunk, value);
offsetBytes += chunk;
lengthBytes -= chunk;
@@ -482,7 +482,7 @@ private void writeDirectMemoryTo(final long offsetBytes, long lengthBytes,
// or file-backed WritableByteChannel implementations with direct ByteBuffer argument could
// be subject of the same safepoint problems as in Unsafe.copyMemory and Unsafe.setMemory.
while (lengthBytes > 0) {
final int chunk = (int) Math.min(CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES, lengthBytes);
final int chunk = (int) Math.min(Util.UNSAFE_COPY_THRESHOLD_BYTES, lengthBytes);
final ByteBuffer bufToWrite = AccessByteBuffer.getDummyReadOnlyDirectByteBuffer(addr, chunk);
writeFully(bufToWrite, out);
addr += chunk;
@@ -27,25 +27,14 @@
import static org.apache.datasketches.memory.UnsafeUtil.SHORT_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.checkBounds;
import static org.apache.datasketches.memory.UnsafeUtil.unsafe;
import static org.apache.datasketches.memory.Util.UNSAFE_COPY_THRESHOLD_BYTES;

/**
* @author Lee Rhodes
*/
@SuppressWarnings({"restriction"})
final class CompareAndCopy {

/**
* Don't use sun.misc.Unsafe#copyMemory to copy blocks of memory larger than this
* threshold, because internally it doesn't have safepoint polls, that may cause long
* "Time To Safe Point" pauses in the application. This has been fixed in JDK 9 (see
* https://bugs.openjdk.java.net/browse/JDK-8149596 and
* https://bugs.openjdk.java.net/browse/JDK-8141491), but not in JDK 8, so the Memory library
* should keep having this boilerplate as long as it supports Java 8.
*
* <p>A reference to this can be found in java.nio.Bits.</p>
*/
static final int UNSAFE_COPY_THRESHOLD_BYTES = 1024 * 1024;

private CompareAndCopy() { }

static int compare(
@@ -54,7 +54,7 @@ final class DirectNonNativeWritableBufferImpl extends NonNativeWritableBufferImp
BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableBufferImpl(
nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid, memReqSvr, originMemory)
@@ -66,7 +66,7 @@ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | DUPLICATE | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableBufferImpl(
nativeBaseOffset, getRegionOffset(), getCapacity(),
type, valid, memReqSvr, originMemory)
@@ -96,4 +96,11 @@ public boolean isValid() {
return valid.get();
}

@Override
void checkValid() {
if (!this.isValid()) {
throw new IllegalStateException("Memory not valid.");
}
}

}
@@ -52,7 +52,7 @@ final class DirectNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImp
BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableMemoryImpl(
nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid, memReqSvr)
@@ -64,7 +64,7 @@ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableBufferImpl(
nativeBaseOffset, getRegionOffset(), getCapacity(),
type, valid, memReqSvr, this)
@@ -94,4 +94,11 @@ public boolean isValid() {
return valid.get();
}

@Override
void checkValid() {
if (!this.isValid()) {
throw new IllegalStateException("Memory not valid.");
}
}

}
@@ -54,7 +54,7 @@ final class DirectWritableBufferImpl extends WritableBufferImpl {
BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableBufferImpl(
nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid, memReqSvr, originMemory)
@@ -66,7 +66,7 @@ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | DUPLICATE | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableBufferImpl(
nativeBaseOffset, getRegionOffset(), getCapacity(),
type, valid, memReqSvr, originMemory)
@@ -96,4 +96,10 @@ public boolean isValid() {
return valid.get();
}

@Override
void checkValid() {
if (!this.isValid()) {
throw new IllegalStateException("Memory not valid.");
}
}
}
@@ -52,7 +52,7 @@ final class DirectWritableMemoryImpl extends WritableMemoryImpl {
BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableMemoryImpl(
nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid, memReqSvr)
@@ -64,7 +64,7 @@ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new DirectWritableBufferImpl(
nativeBaseOffset, getRegionOffset(), getCapacity(),
type, valid, memReqSvr, this)
@@ -93,5 +93,5 @@ int getTypeId() {
public boolean isValid() {
return valid.get();
}

}
@@ -47,7 +47,7 @@ final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl
BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new HeapWritableBufferImpl(
unsafeObj, getRegionOffset(offsetBytes), capacityBytes,
type, originMemory)
@@ -59,7 +59,7 @@ unsafeObj, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | DUPLICATE | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new HeapWritableBufferImpl(
unsafeObj, getRegionOffset(), getCapacity(),
type, originMemory)
@@ -46,7 +46,7 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl
BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes,
final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | REGION | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new HeapWritableMemoryImpl(
unsafeObj, getRegionOffset(offsetBytes), capacityBytes,
type)
@@ -58,7 +58,7 @@ unsafeObj, getRegionOffset(offsetBytes), capacityBytes,
@Override
BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) {
final int type = typeId | (readOnly ? READONLY : 0);
return isNativeByteOrder(byteOrder)
return Util.isNativeByteOrder(byteOrder)
? new HeapWritableBufferImpl(
unsafeObj, getRegionOffset(), getCapacity(),
type, this)

0 comments on commit d964c73

Please sign in to comment.