You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by dc...@apache.org on 2021/08/19 22:18:24 UTC

svn commit: r49567 [11/14] - in /dev/datasketches/memory/2.0.0-RC1: ./ apache-datasketches-memory-2.0.0-src/ apache-datasketches-memory-2.0.0-src/datasketches-memory-java11/ apache-datasketches-memory-2.0.0-src/datasketches-memory-java11/src/ apache-da...

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,375 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_CHAR_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_INT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_LONG_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_SHORT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.CHAR_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.DOUBLE_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.FLOAT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.INT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.LONG_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.SHORT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.checkBounds;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
+
+import org.apache.datasketches.memory.WritableBuffer;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at
+ * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to
+ * the rest of the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and
+ * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove
+ * them entirely from production runtime code. The offset versions of the light weight methods will
+ * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is
+ * often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableBuffer} for native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NativeWritableBufferImpl extends BaseWritableBufferImpl {
+
+  //Pass-through ctor
+  NativeWritableBufferImpl(final Object unsafeObj, final long nativeBaseOffset, final long regionOffset,
+      final long capacityBytes) {
+    super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes);
+  }
+
+  //PRIMITIVE getX() and getXArray()
+  @Override
+  public char getChar() {
+    return getNativeOrderedChar();
+  }
+
+  @Override
+  public char getChar(final long offsetBytes) {
+    return getNativeOrderedChar(offsetBytes);
+  }
+
+  @Override
+  public void getCharArray(final char[] dstArray, final int dstOffsetChars, final int lengthChars) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    checkBounds(dstOffsetChars, lengthChars, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            dstArray,
+            ARRAY_CHAR_BASE_OFFSET + (((long) dstOffsetChars) << CHAR_SHIFT),
+            copyBytes);
+  }
+
+  @Override
+  public double getDouble() {
+    final long pos = getPosition();
+    incrementAndAssertPositionForRead(pos, ARRAY_DOUBLE_INDEX_SCALE);
+    return unsafe.getDouble(getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public double getDouble(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    return unsafe.getDouble(getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void getDoubleArray(final double[] dstArray, final int dstOffsetDoubles,
+      final int lengthDoubles) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    checkBounds(dstOffsetDoubles, lengthDoubles, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            dstArray,
+            ARRAY_DOUBLE_BASE_OFFSET + (((long) dstOffsetDoubles) << DOUBLE_SHIFT),
+            copyBytes);
+  }
+
+  @Override
+  public float getFloat() {
+    final long pos = getPosition();
+    incrementAndAssertPositionForRead(pos, ARRAY_FLOAT_INDEX_SCALE);
+    return unsafe.getFloat(getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public float getFloat(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    return unsafe.getFloat(getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void getFloatArray(final float[] dstArray, final int dstOffsetFloats,
+      final int lengthFloats) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    checkBounds(dstOffsetFloats, lengthFloats, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            dstArray,
+            ARRAY_FLOAT_BASE_OFFSET + (((long) dstOffsetFloats) << FLOAT_SHIFT),
+            copyBytes);
+  }
+
+  @Override
+  public int getInt() {
+    return getNativeOrderedInt();
+  }
+
+  @Override
+  public int getInt(final long offsetBytes) {
+    return getNativeOrderedInt(offsetBytes);
+  }
+
+  @Override
+  public void getIntArray(final int[] dstArray, final int dstOffsetInts, final int lengthInts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    checkBounds(dstOffsetInts, lengthInts, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            dstArray,
+            ARRAY_INT_BASE_OFFSET + (((long) dstOffsetInts) << INT_SHIFT),
+            copyBytes);
+  }
+
+  @Override
+  public long getLong() {
+    return getNativeOrderedLong();
+  }
+
+  @Override
+  public long getLong(final long offsetBytes) {
+    return getNativeOrderedLong(offsetBytes);
+  }
+
+  @Override
+  public void getLongArray(final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    checkBounds(dstOffsetLongs, lengthLongs, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            dstArray,
+            ARRAY_LONG_BASE_OFFSET + (((long) dstOffsetLongs) << LONG_SHIFT),
+            copyBytes);
+  }
+
+  @Override
+  public short getShort() {
+    return getNativeOrderedShort();
+  }
+
+  @Override
+  public short getShort(final long offsetBytes) {
+    return getNativeOrderedShort(offsetBytes);
+  }
+
+  @Override
+  public void getShortArray(final short[] dstArray, final int dstOffsetShorts,
+      final int lengthShorts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    checkBounds(dstOffsetShorts, lengthShorts, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            dstArray,
+            ARRAY_SHORT_BASE_OFFSET + (((long) dstOffsetShorts) << SHORT_SHIFT),
+            copyBytes);
+  }
+
+  //PRIMITIVE putX() and putXArray()
+  @Override
+  public void putChar(final char value) {
+    putNativeOrderedChar(value);
+  }
+
+  @Override
+  public void putChar(final long offsetBytes, final char value) {
+    putNativeOrderedChar(offsetBytes, value);
+  }
+
+  @Override
+  public void putCharArray(final char[] srcArray, final int srcOffsetChars, final int lengthChars) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    checkBounds(srcOffsetChars, lengthChars, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            srcArray,
+            ARRAY_CHAR_BASE_OFFSET + (((long) srcOffsetChars) << CHAR_SHIFT),
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            copyBytes);
+  }
+
+  @Override
+  public void putDouble(final double value) {
+    final long pos = getPosition();
+    incrementAndAssertPositionForWrite(pos, ARRAY_DOUBLE_INDEX_SCALE);
+    unsafe.putDouble(getUnsafeObject(), getCumulativeOffset(pos), value);
+  }
+
+  @Override
+  public void putDouble(final long offsetBytes, final double value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    unsafe.putDouble(getUnsafeObject(), getCumulativeOffset(offsetBytes), value);
+  }
+
+  @Override
+  public void putDoubleArray(final double[] srcArray, final int srcOffsetDoubles,
+      final int lengthDoubles) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    checkBounds(srcOffsetDoubles, lengthDoubles, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            srcArray,
+            ARRAY_DOUBLE_BASE_OFFSET + (((long) srcOffsetDoubles) << DOUBLE_SHIFT),
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            copyBytes);
+  }
+
+  @Override
+  public void putFloat(final float value) {
+    final long pos = getPosition();
+    incrementAndAssertPositionForWrite(pos, ARRAY_FLOAT_INDEX_SCALE);
+    unsafe.putFloat(getUnsafeObject(), getCumulativeOffset(pos), value);
+  }
+
+  @Override
+  public void putFloat(final long offsetBytes, final float value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    unsafe.putFloat(getUnsafeObject(), getCumulativeOffset(offsetBytes), value);
+  }
+
+  @Override
+  public void putFloatArray(final float[] srcArray, final int srcOffsetFloats,
+      final int lengthFloats) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    checkBounds(srcOffsetFloats, lengthFloats, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            srcArray,
+            ARRAY_FLOAT_BASE_OFFSET + (((long) srcOffsetFloats) << FLOAT_SHIFT),
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            copyBytes);
+  }
+
+  @Override
+  public void putInt(final int value) {
+    putNativeOrderedInt(value);
+  }
+
+  @Override
+  public void putInt(final long offsetBytes, final int value) {
+    putNativeOrderedInt(offsetBytes, value);
+  }
+
+  @Override
+  public void putIntArray(final int[] srcArray, final int srcOffsetInts, final int lengthInts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    checkBounds(srcOffsetInts, lengthInts, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            srcArray,
+            ARRAY_INT_BASE_OFFSET + (((long) srcOffsetInts) << INT_SHIFT),
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            copyBytes);
+  }
+
+  @Override
+  public void putLong(final long value) {
+    putNativeOrderedLong(value);
+  }
+
+  @Override
+  public void putLong(final long offsetBytes, final long value) {
+    putNativeOrderedLong(offsetBytes, value);
+  }
+
+  @Override
+  public void putLongArray(final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    checkBounds(srcOffsetLongs, lengthLongs, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            srcArray,
+            ARRAY_LONG_BASE_OFFSET + (((long) srcOffsetLongs) << LONG_SHIFT),
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            copyBytes);
+  }
+
+  @Override
+  public void putShort(final short value) {
+    putNativeOrderedShort(value);
+  }
+
+  @Override
+  public void putShort(final long offsetBytes, final short value) {
+    putNativeOrderedShort(offsetBytes, value);
+  }
+
+  @Override
+  public void putShortArray(final short[] srcArray, final int srcOffsetShorts,
+      final int lengthShorts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    checkBounds(srcOffsetShorts, lengthShorts, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+            srcArray,
+            ARRAY_SHORT_BASE_OFFSET + (((long) srcOffsetShorts) << SHORT_SHIFT),
+            getUnsafeObject(),
+            getCumulativeOffset(pos),
+            copyBytes);
+  }
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,330 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_CHAR_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_INT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_LONG_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_LONG_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_SHORT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.CHAR_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.DOUBLE_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.FLOAT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.INT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.LONG_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.SHORT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.checkBounds;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
+
+import org.apache.datasketches.memory.WritableMemory;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (checkValid*() and checkBounds()), which execute at runtime and
+ * throw exceptions if violated. The cost of the runtime checks are minor compared to the rest of
+ * the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assertValid*()), which only
+ * execute when asserts are enabled and JIT will remove them entirely from production runtime code.
+ * The light weight methods will simplify to a single unsafe call, which is further simplified by
+ * JIT to an intrinsic that is often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableMemory} for native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NativeWritableMemoryImpl extends BaseWritableMemoryImpl {
+
+  //Pass-through ctor
+  NativeWritableMemoryImpl(final Object unsafeObj, final long nativeBaseOffset,
+      final long regionOffset, final long capacityBytes) {
+    super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes);
+  }
+
+  ///PRIMITIVE getX() and getXArray()
+  @Override
+  public char getChar(final long offsetBytes) {
+    return getNativeOrderedChar(offsetBytes);
+  }
+
+  @Override
+  public void getCharArray(final long offsetBytes, final char[] dstArray, final int dstOffsetChars,
+      final int lengthChars) {
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    checkBounds(dstOffsetChars, lengthChars, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        dstArray,
+        ARRAY_CHAR_BASE_OFFSET + (((long) dstOffsetChars) << CHAR_SHIFT),
+        copyBytes);
+  }
+
+  @Override
+  public double getDouble(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    return unsafe.getDouble(getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void getDoubleArray(final long offsetBytes, final double[] dstArray,
+      final int dstOffsetDoubles, final int lengthDoubles) {
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    checkBounds(dstOffsetDoubles, lengthDoubles, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        dstArray,
+        ARRAY_DOUBLE_BASE_OFFSET + (((long) dstOffsetDoubles) << DOUBLE_SHIFT),
+        copyBytes);
+  }
+
+  @Override
+  public float getFloat(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    return unsafe.getFloat(getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void getFloatArray(final long offsetBytes, final float[] dstArray,
+      final int dstOffsetFloats, final int lengthFloats) {
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    checkBounds(dstOffsetFloats, lengthFloats, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        dstArray,
+        ARRAY_FLOAT_BASE_OFFSET + (((long) dstOffsetFloats) << FLOAT_SHIFT),
+        copyBytes);
+  }
+
+  @Override
+  public int getInt(final long offsetBytes) {
+    return getNativeOrderedInt(offsetBytes);
+  }
+
+  @Override
+  public void getIntArray(final long offsetBytes, final int[] dstArray, final int dstOffsetInts,
+      final int lengthInts) {
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    checkBounds(dstOffsetInts, lengthInts, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        dstArray,
+        ARRAY_INT_BASE_OFFSET + (((long) dstOffsetInts) << INT_SHIFT),
+        copyBytes);
+  }
+
+  @Override
+  public long getLong(final long offsetBytes) {
+    return getNativeOrderedLong(offsetBytes);
+  }
+
+  @Override
+  public void getLongArray(final long offsetBytes, final long[] dstArray,
+      final int dstOffsetLongs, final int lengthLongs) {
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    checkBounds(dstOffsetLongs, lengthLongs, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        dstArray,
+        ARRAY_LONG_BASE_OFFSET + (((long) dstOffsetLongs) << LONG_SHIFT),
+        copyBytes);
+  }
+
+  @Override
+  public short getShort(final long offsetBytes) {
+    return getNativeOrderedShort(offsetBytes);
+  }
+
+  @Override
+  public void getShortArray(final long offsetBytes, final short[] dstArray,
+      final int dstOffsetShorts, final int lengthShorts) {
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    checkBounds(dstOffsetShorts, lengthShorts, dstArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        dstArray,
+        ARRAY_SHORT_BASE_OFFSET + (((long) dstOffsetShorts) << SHORT_SHIFT),
+        copyBytes);
+  }
+
+  //PRIMITIVE putX() and putXArray() implementations
+  @Override
+  public void putChar(final long offsetBytes, final char value) {
+    putNativeOrderedChar(offsetBytes, value);
+  }
+
+  @Override
+  public void putCharArray(final long offsetBytes, final char[] srcArray,
+      final int srcOffsetChars, final int lengthChars) {
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    checkBounds(srcOffsetChars, lengthChars, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        srcArray,
+        ARRAY_CHAR_BASE_OFFSET + (((long) srcOffsetChars) << CHAR_SHIFT),
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        copyBytes
+    );
+  }
+
+  @Override
+  public void putDouble(final long offsetBytes, final double value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    unsafe.putDouble(getUnsafeObject(), getCumulativeOffset(offsetBytes), value);
+  }
+
+  @Override
+  public void putDoubleArray(final long offsetBytes, final double[] srcArray,
+      final int srcOffsetDoubles, final int lengthDoubles) {
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    checkBounds(srcOffsetDoubles, lengthDoubles, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        srcArray,
+        ARRAY_DOUBLE_BASE_OFFSET + (((long) srcOffsetDoubles) << DOUBLE_SHIFT),
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        copyBytes
+    );
+  }
+
+  @Override
+  public void putFloat(final long offsetBytes, final float value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    unsafe.putFloat(getUnsafeObject(), getCumulativeOffset(offsetBytes), value);
+  }
+
+  @Override
+  public void putFloatArray(final long offsetBytes, final float[] srcArray,
+      final int srcOffsetFloats, final int lengthFloats) {
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    checkBounds(srcOffsetFloats, lengthFloats, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        srcArray,
+        ARRAY_FLOAT_BASE_OFFSET + (((long) srcOffsetFloats) << FLOAT_SHIFT),
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        copyBytes
+    );
+  }
+
+  @Override
+  public void putInt(final long offsetBytes, final int value) {
+    putNativeOrderedInt(offsetBytes, value);
+  }
+
+  @Override
+  public void putIntArray(final long offsetBytes, final int[] srcArray, final int srcOffsetInts,
+      final int lengthInts) {
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    checkBounds(srcOffsetInts, lengthInts, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        srcArray,
+        ARRAY_INT_BASE_OFFSET + (((long) srcOffsetInts) << INT_SHIFT),
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        copyBytes
+    );
+  }
+
+  @Override
+  public void putLong(final long offsetBytes, final long value) {
+    putNativeOrderedLong(offsetBytes, value);
+  }
+
+  @Override
+  public void putLongArray(final long offsetBytes, final long[] srcArray, final int srcOffsetLongs,
+      final int lengthLongs) {
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    checkBounds(srcOffsetLongs, lengthLongs, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        srcArray,
+        ARRAY_LONG_BASE_OFFSET + (((long) srcOffsetLongs) << LONG_SHIFT),
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        copyBytes
+    );
+  }
+
+  @Override
+  public void putShort(final long offsetBytes, final short value) {
+    putNativeOrderedShort(offsetBytes, value);
+  }
+
+  @Override
+  public void putShortArray(final long offsetBytes, final short[] srcArray,
+      final int srcOffsetShorts, final int lengthShorts) {
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    checkBounds(srcOffsetShorts, lengthShorts, srcArray.length);
+    CompareAndCopy.copyMemoryCheckingDifferentObject(
+        srcArray,
+        ARRAY_SHORT_BASE_OFFSET + (((long) srcOffsetShorts) << SHORT_SHIFT),
+        getUnsafeObject(),
+        getCumulativeOffset(offsetBytes),
+        copyBytes
+    );
+  }
+
+  //Atomic Write Methods
+  @Override
+  public long getAndAddLong(final long offsetBytes, final long delta) { //JDK 8+
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
+    final long addr = getCumulativeOffset(offsetBytes);
+    return unsafe.getAndAddLong(getUnsafeObject(), addr, delta);
+  }
+
+  @Override
+  public long getAndSetLong(final long offsetBytes, final long newValue) { //JDK 8+
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
+    final long addr = getCumulativeOffset(offsetBytes);
+    return unsafe.getAndSetLong(getUnsafeObject(), addr, newValue);
+  }
+
+  @Override
+  public boolean compareAndSwapLong(final long offsetBytes, final long expect, final long update) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
+    return unsafe.compareAndSwapLong(
+        getUnsafeObject(), getCumulativeOffset(offsetBytes), expect, update);
+  }
+
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Provide linkage to java.nio.Bits.
+ *
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+final class NioBits {
+  private static final Class<?> NIO_BITS_CLASS;
+  private static final Method NIO_BITS_RESERVE_MEMORY_METHOD;
+  private static final Method NIO_BITS_UNRESERVE_MEMORY_METHOD;
+
+  private static final AtomicLong nioBitsCount;
+  private static final AtomicLong nioBitsReservedMemory;
+  private static final AtomicLong nioBitsTotalCapacity;
+
+  private static int pageSize = unsafe.pageSize();
+  private static final long maxDBBMemory;
+  private static final boolean isPageAligned;
+
+  static {
+    try {
+      isPageAligned = VirtualMachineMemory.getIsPageAligned();
+      maxDBBMemory = VirtualMachineMemory.getMaxDBBMemory();
+
+      NIO_BITS_CLASS = Class.forName("java.nio.Bits");
+
+      NIO_BITS_RESERVE_MEMORY_METHOD = NIO_BITS_CLASS
+          .getDeclaredMethod("reserveMemory", long.class, int.class); //JD16 requires (long, long)
+      NIO_BITS_RESERVE_MEMORY_METHOD.setAccessible(true);
+
+      NIO_BITS_UNRESERVE_MEMORY_METHOD = NIO_BITS_CLASS
+          .getDeclaredMethod("unreserveMemory", long.class, int.class); //JD16 requires (long, long)
+      NIO_BITS_UNRESERVE_MEMORY_METHOD.setAccessible(true);
+
+      final Field countField = NIO_BITS_CLASS.getDeclaredField(NioBitsFields.COUNT_FIELD_NAME);
+      countField.setAccessible(true);
+      nioBitsCount = (AtomicLong) (countField.get(null));
+
+      final Field reservedMemoryField = NIO_BITS_CLASS.getDeclaredField(NioBitsFields.RESERVED_MEMORY_FIELD_NAME);
+      reservedMemoryField.setAccessible(true);
+      nioBitsReservedMemory = (AtomicLong) (reservedMemoryField.get(null));
+
+      final Field totalCapacityField = NIO_BITS_CLASS.getDeclaredField(NioBitsFields.TOTAL_CAPACITY_FIELD_NAME);
+      totalCapacityField.setAccessible(true);
+      nioBitsTotalCapacity = (AtomicLong) (totalCapacityField.get(null));
+
+    } catch (final ClassNotFoundException | NoSuchMethodException |  IllegalAccessException
+        | IllegalArgumentException | SecurityException |  NoSuchFieldException e) {
+      throw new RuntimeException("Could not acquire java.nio.Bits class: " + e.getClass());
+    }
+  }
+
+  private NioBits() { }
+
+  static long getDirectAllocationsCount() { //tested via reflection
+    return nioBitsCount.get();
+  }
+
+  static long getReservedMemory() { //tested via reflection
+    return nioBitsReservedMemory.get();
+  }
+
+  static long getTotalCapacity() { //tested via reflection
+    return nioBitsTotalCapacity.get();
+  }
+
+  static int pageSize() {
+    return pageSize;
+  }
+
+  static int pageCount(final long bytes) {
+    return (int)((bytes + pageSize()) - 1L) / pageSize();
+  }
+
+  static long getMaxDirectByteBufferMemory() { //tested via reflection
+    return maxDBBMemory;
+  }
+
+  static boolean isPageAligned() {
+    return isPageAligned;
+  }
+
+  //RESERVE & UNRESERVE BITS MEMORY TRACKING COUNTERS
+  // Comment from java.nio.Bits.java ~ line 705:
+  // -XX:MaxDirectMemorySize limits the total capacity rather than the
+  // actual memory usage, which will differ when buffers are page aligned.
+  static void reserveMemory(final long allocationSize, final long capacity) {
+    reserveUnreserve(allocationSize, capacity, NIO_BITS_RESERVE_MEMORY_METHOD);
+  }
+
+  static void unreserveMemory(final long allocationSize, final long capacity) {
+    reserveUnreserve(allocationSize, capacity, NIO_BITS_UNRESERVE_MEMORY_METHOD);
+  }
+
+  private static void reserveUnreserve(long allocationSize, long capacity, final Method method) {
+    Util.zeroCheck(capacity, "capacity");
+    // 1GB is a pretty "safe" limit.
+    final long chunkSizeLimit = 1L << 30;
+    try {
+      while (capacity > 0) {
+        final long chunk = Math.min(capacity, chunkSizeLimit);
+        if (capacity == chunk) {
+          method.invoke(null, allocationSize, (int) capacity); //JDK 16 remove cast to int
+        } else {
+          method.invoke(null, chunk, (int) chunk); //JDK 16 remove cast to int
+        }
+        capacity -= chunk;
+        allocationSize -= chunk;
+      }
+    } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+      throw new RuntimeException(
+          "Could not invoke java.nio.Bits.unreserveMemory(...) OR java.nio.Bits.reserveMemory(...): "
+          + "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
+    }
+  }
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+/**
+ * Extracts version-dependent field names into standalone class.
+ * Some field names in the VM internal class have changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
+ * For more information, see: https://openjdk.java.net/jeps/238
+ */
+class NioBitsFields {
+    static String COUNT_FIELD_NAME = "count";
+    static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
+    static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.CHAR_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.DOUBLE_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.FLOAT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.INT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.LONG_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.SHORT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
+
+import org.apache.datasketches.memory.WritableBuffer;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at
+ * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to
+ * the rest of the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and
+ * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove
+ * them entirely from production runtime code. The offset versions of the light weight methods will
+ * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is
+ * often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableBuffer} for non-native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NonNativeWritableBufferImpl extends BaseWritableBufferImpl {
+
+  //Pass-through ctor
+  NonNativeWritableBufferImpl(final Object unsafeObj, final long nativeBaseOffset, final long regionOffset,
+      final long capacityBytes) {
+    super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes);
+  }
+
+  //PRIMITIVE getX() and getXArray()
+  @Override
+  public char getChar() {
+    return Character.reverseBytes(getNativeOrderedChar());
+  }
+
+  @Override
+  public char getChar(final long offsetBytes) {
+    return Character.reverseBytes(getNativeOrderedChar(offsetBytes));
+  }
+
+  @Override
+  public void getCharArray(final char[] dstArray, final int dstOffsetChars, final int lengthChars) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    CompareAndCopy.getNonNativeChars(getUnsafeObject(), getCumulativeOffset(pos), copyBytes,
+        dstArray, dstOffsetChars, lengthChars);
+  }
+
+  @Override
+  public double getDouble() {
+    final long pos = getPosition();
+    incrementAndAssertPositionForRead(pos, ARRAY_DOUBLE_INDEX_SCALE);
+    return Double.longBitsToDouble(
+        Long.reverseBytes(unsafe.getLong(getUnsafeObject(), getCumulativeOffset(pos))));
+  }
+
+  @Override
+  public double getDouble(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    return Double.longBitsToDouble(
+        Long.reverseBytes(unsafe.getLong(getUnsafeObject(), getCumulativeOffset(offsetBytes))));
+  }
+
+  @Override
+  public void getDoubleArray(final double[] dstArray, final int dstOffsetDoubles,
+      final int lengthDoubles) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    CompareAndCopy.getNonNativeDoubles(getUnsafeObject(), getCumulativeOffset(pos), copyBytes,
+        dstArray, dstOffsetDoubles, lengthDoubles);
+  }
+
+  @Override
+  public float getFloat() {
+    final long pos = getPosition();
+    incrementAndAssertPositionForRead(pos, ARRAY_FLOAT_INDEX_SCALE);
+    return Float.intBitsToFloat(
+        Integer.reverseBytes(unsafe.getInt(getUnsafeObject(), getCumulativeOffset(pos))));
+  }
+
+  @Override
+  public float getFloat(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    return Float.intBitsToFloat(
+        Integer.reverseBytes(unsafe.getInt(getUnsafeObject(), getCumulativeOffset(offsetBytes))));
+  }
+
+  @Override
+  public void getFloatArray(final float[] dstArray, final int dstOffsetFloats,
+      final int lengthFloats) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    CompareAndCopy.getNonNativeFloats(getUnsafeObject(), getCumulativeOffset(pos), copyBytes,
+        dstArray, dstOffsetFloats, lengthFloats);
+  }
+
+  @Override
+  public int getInt() {
+    return Integer.reverseBytes(getNativeOrderedInt());
+  }
+
+  @Override
+  public int getInt(final long offsetBytes) {
+    return Integer.reverseBytes(getNativeOrderedInt(offsetBytes));
+  }
+
+  @Override
+  public void getIntArray(final int[] dstArray, final int dstOffsetInts, final int lengthInts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    CompareAndCopy.getNonNativeInts(getUnsafeObject(), getCumulativeOffset(pos), copyBytes,
+        dstArray, dstOffsetInts, lengthInts);
+  }
+
+  @Override
+  public long getLong() {
+    return Long.reverseBytes(getNativeOrderedLong());
+  }
+
+  @Override
+  public long getLong(final long offsetBytes) {
+    return Long.reverseBytes(getNativeOrderedLong(offsetBytes));
+  }
+
+  @Override
+  public void getLongArray(final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    CompareAndCopy.getNonNativeLongs(getUnsafeObject(), getCumulativeOffset(pos), copyBytes,
+        dstArray, dstOffsetLongs, lengthLongs);
+  }
+
+  @Override
+  public short getShort() {
+    return Short.reverseBytes(getNativeOrderedShort());
+  }
+
+  @Override
+  public short getShort(final long offsetBytes) {
+    return Short.reverseBytes(getNativeOrderedShort(offsetBytes));
+  }
+
+  @Override
+  public void getShortArray(final short[] dstArray, final int dstOffsetShorts,
+      final int lengthShorts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    incrementAndCheckPositionForRead(pos, copyBytes);
+    CompareAndCopy.getNonNativeShorts(getUnsafeObject(), getCumulativeOffset(pos), copyBytes,
+        dstArray, dstOffsetShorts, lengthShorts);
+  }
+
+  //PRIMITIVE putX() and putXArray()
+  @Override
+  public void putChar(final char value) {
+    putNativeOrderedChar(Character.reverseBytes(value));
+  }
+
+  @Override
+  public void putChar(final long offsetBytes, final char value) {
+    putNativeOrderedChar(offsetBytes, Character.reverseBytes(value));
+  }
+
+  @Override
+  public void putCharArray(final char[] srcArray, final int srcOffsetChars, final int lengthChars) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    CompareAndCopy.putNonNativeChars(srcArray, srcOffsetChars, lengthChars, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public void putDouble(final double value) {
+    final long pos = getPosition();
+    incrementAndAssertPositionForWrite(pos, ARRAY_DOUBLE_INDEX_SCALE);
+    unsafe.putLong(getUnsafeObject(), getCumulativeOffset(pos),
+        Long.reverseBytes(Double.doubleToRawLongBits(value)));
+  }
+
+  @Override
+  public void putDouble(final long offsetBytes, final double value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    unsafe.putLong(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        Long.reverseBytes(Double.doubleToRawLongBits(value)));
+  }
+
+  @Override
+  public void putDoubleArray(final double[] srcArray, final int srcOffsetDoubles,
+      final int lengthDoubles) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    CompareAndCopy.putNonNativeDoubles(srcArray, srcOffsetDoubles, lengthDoubles, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public void putFloat(final float value) {
+    final long pos = getPosition();
+    incrementAndAssertPositionForWrite(pos, ARRAY_FLOAT_INDEX_SCALE);
+    unsafe.putInt(getUnsafeObject(), getCumulativeOffset(pos),
+        Integer.reverseBytes(Float.floatToRawIntBits(value)));
+  }
+
+  @Override
+  public void putFloat(final long offsetBytes, final float value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    unsafe.putInt(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        Integer.reverseBytes(Float.floatToRawIntBits(value)));
+  }
+
+  @Override
+  public void putFloatArray(final float[] srcArray, final int srcOffsetFloats,
+      final int lengthFloats) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    CompareAndCopy.putNonNativeFloats(srcArray, srcOffsetFloats, lengthFloats, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public void putInt(final int value) {
+    putNativeOrderedInt(Integer.reverseBytes(value));
+  }
+
+  @Override
+  public void putInt(final long offsetBytes, final int value) {
+    putNativeOrderedInt(offsetBytes, Integer.reverseBytes(value));
+  }
+
+  @Override
+  public void putIntArray(final int[] srcArray, final int srcOffsetInts, final int lengthInts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    CompareAndCopy.putNonNativeInts(srcArray, srcOffsetInts, lengthInts, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public void putLong(final long value) {
+    putNativeOrderedLong(Long.reverseBytes(value));
+  }
+
+  @Override
+  public void putLong(final long offsetBytes, final long value) {
+    putNativeOrderedLong(offsetBytes, Long.reverseBytes(value));
+  }
+
+  @Override
+  public void putLongArray(final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    CompareAndCopy.putNonNativeLongs(srcArray, srcOffsetLongs, lengthLongs, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+  @Override
+  public void putShort(final short value) {
+    putNativeOrderedShort(Short.reverseBytes(value));
+  }
+
+  @Override
+  public void putShort(final long offsetBytes, final short value) {
+    putNativeOrderedShort(offsetBytes, Short.reverseBytes(value));
+  }
+
+  @Override
+  public void putShortArray(final short[] srcArray, final int srcOffsetShorts,
+      final int lengthShorts) {
+    final long pos = getPosition();
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    incrementAndCheckPositionForWrite(pos, copyBytes);
+    CompareAndCopy.putNonNativeShorts(srcArray, srcOffsetShorts, lengthShorts, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(pos));
+  }
+
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_LONG_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.CHAR_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.DOUBLE_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.FLOAT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.INT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.LONG_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.SHORT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
+
+import org.apache.datasketches.memory.WritableMemory;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (checkValid*() and checkBounds()), which execute at runtime and
+ * throw exceptions if violated. The cost of the runtime checks are minor compared to the rest of
+ * the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assertValid*()), which only
+ * execute when asserts are enabled and JIT will remove them entirely from production runtime code.
+ * The light weight methods will simplify to a single unsafe call, which is further simplified by
+ * JIT to an intrinsic that is often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableMemory} for non-native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NonNativeWritableMemoryImpl extends BaseWritableMemoryImpl {
+
+  //Pass-through ctor
+  NonNativeWritableMemoryImpl(final Object unsafeObj, final long nativeBaseOffset,
+      final long regionOffset, final long capacityBytes) {
+    super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes);
+  }
+
+  ///PRIMITIVE getX() and getXArray()
+  @Override
+  public char getChar(final long offsetBytes) {
+    return Character.reverseBytes(getNativeOrderedChar(offsetBytes));
+  }
+
+  @Override
+  public void getCharArray(final long offsetBytes, final char[] dstArray, final int dstOffsetChars,
+      final int lengthChars) {
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    CompareAndCopy.getNonNativeChars(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        copyBytes, dstArray, dstOffsetChars, lengthChars);
+  }
+
+  @Override
+  public double getDouble(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    return Double.longBitsToDouble(
+        Long.reverseBytes(unsafe.getLong(getUnsafeObject(), getCumulativeOffset(offsetBytes))));
+  }
+
+  @Override
+  public void getDoubleArray(final long offsetBytes, final double[] dstArray,
+      final int dstOffsetDoubles, final int lengthDoubles) {
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    CompareAndCopy.getNonNativeDoubles(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        copyBytes, dstArray, dstOffsetDoubles, lengthDoubles);
+  }
+
+  @Override
+  public float getFloat(final long offsetBytes) {
+    assertValidAndBoundsForRead(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    return Float.intBitsToFloat(
+        Integer.reverseBytes(unsafe.getInt(getUnsafeObject(), getCumulativeOffset(offsetBytes))));
+  }
+
+  @Override
+  public void getFloatArray(final long offsetBytes, final float[] dstArray,
+      final int dstOffsetFloats, final int lengthFloats) {
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    CompareAndCopy.getNonNativeFloats(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        copyBytes, dstArray, dstOffsetFloats, lengthFloats);
+  }
+
+  @Override
+  public int getInt(final long offsetBytes) {
+    return Integer.reverseBytes(getNativeOrderedInt(offsetBytes));
+  }
+
+  @Override
+  public void getIntArray(final long offsetBytes, final int[] dstArray, final int dstOffsetInts,
+      final int lengthInts) {
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    CompareAndCopy.getNonNativeInts(getUnsafeObject(), getCumulativeOffset(offsetBytes), copyBytes,
+        dstArray, dstOffsetInts, lengthInts);
+  }
+
+  @Override
+  public long getLong(final long offsetBytes) {
+    return Long.reverseBytes(getNativeOrderedLong(offsetBytes));
+  }
+
+  @Override
+  public void getLongArray(final long offsetBytes, final long[] dstArray,
+      final int dstOffsetLongs, final int lengthLongs) {
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    CompareAndCopy.getNonNativeLongs(getUnsafeObject(), getCumulativeOffset(offsetBytes), copyBytes,
+        dstArray, dstOffsetLongs, lengthLongs);
+  }
+
+  @Override
+  public short getShort(final long offsetBytes) {
+    return Short.reverseBytes(getNativeOrderedShort(offsetBytes));
+  }
+
+  @Override
+  public void getShortArray(final long offsetBytes, final short[] dstArray,
+      final int dstOffsetShorts, final int lengthShorts) {
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    checkValidAndBounds(offsetBytes, copyBytes);
+    CompareAndCopy.getNonNativeShorts(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        copyBytes, dstArray, dstOffsetShorts, lengthShorts);
+  }
+
+  //PRIMITIVE putX() and putXArray() implementations
+  @Override
+  public void putChar(final long offsetBytes, final char value) {
+    putNativeOrderedChar(offsetBytes, Character.reverseBytes(value));
+  }
+
+  @Override
+  public void putCharArray(final long offsetBytes, final char[] srcArray, final int srcOffsetChars,
+      final int lengthChars) {
+    final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    CompareAndCopy.putNonNativeChars(srcArray, srcOffsetChars, lengthChars, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void putDouble(final long offsetBytes, final double value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_DOUBLE_INDEX_SCALE);
+    unsafe.putLong(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        Long.reverseBytes(Double.doubleToRawLongBits(value)));
+  }
+
+  @Override
+  public void putDoubleArray(final long offsetBytes, final double[] srcArray,
+      final int srcOffsetDoubles, final int lengthDoubles) {
+    final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    CompareAndCopy.putNonNativeDoubles(srcArray, srcOffsetDoubles, lengthDoubles, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void putFloat(final long offsetBytes, final float value) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_FLOAT_INDEX_SCALE);
+    unsafe.putInt(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        Integer.reverseBytes(Float.floatToRawIntBits(value)));
+  }
+
+  @Override
+  public void putFloatArray(final long offsetBytes, final float[] srcArray,
+      final int srcOffsetFloats, final int lengthFloats) {
+    final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    CompareAndCopy.putNonNativeFloats(srcArray, srcOffsetFloats, lengthFloats, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void putInt(final long offsetBytes, final int value) {
+    putNativeOrderedInt(offsetBytes, Integer.reverseBytes(value));
+  }
+
+  @Override
+  public void putIntArray(final long offsetBytes, final int[] srcArray, final int srcOffsetInts,
+      final int lengthInts) {
+    final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    CompareAndCopy.putNonNativeInts(srcArray, srcOffsetInts, lengthInts, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void putLong(final long offsetBytes, final long value) {
+    putNativeOrderedLong(offsetBytes, Long.reverseBytes(value));
+  }
+
+  @Override
+  public void putLongArray(final long offsetBytes, final long[] srcArray, final int srcOffsetLongs,
+      final int lengthLongs) {
+    final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    CompareAndCopy.putNonNativeLongs(srcArray, srcOffsetLongs, lengthLongs, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  @Override
+  public void putShort(final long offsetBytes, final short value) {
+    putNativeOrderedShort(offsetBytes, Short.reverseBytes(value));
+  }
+
+  @Override
+  public void putShortArray(final long offsetBytes, final short[] srcArray,
+      final int srcOffsetShorts, final int lengthShorts) {
+    final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+    checkValidAndBoundsForWrite(offsetBytes, copyBytes);
+    CompareAndCopy.putNonNativeShorts(srcArray, srcOffsetShorts, lengthShorts, copyBytes,
+        getUnsafeObject(), getCumulativeOffset(offsetBytes));
+  }
+
+  //Atomic Write Methods
+  @Override
+  public long getAndAddLong(final long offsetBytes, final long delta) { //JDK 8+
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
+    final long addr = getCumulativeOffset(offsetBytes);
+    long oldValReverseBytes, oldVal, newValReverseBytes;
+    final Object unsafeObj = getUnsafeObject();
+    do {
+      oldValReverseBytes = unsafe.getLongVolatile(unsafeObj, addr);
+      oldVal = Long.reverseBytes(oldValReverseBytes);
+      newValReverseBytes = Long.reverseBytes(oldVal + delta);
+    } while (!unsafe.compareAndSwapLong(unsafeObj, addr, oldValReverseBytes, newValReverseBytes));
+
+    return oldVal;
+  }
+
+  @Override
+  public long getAndSetLong(final long offsetBytes, final long newValue) { //JDK 8+
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
+    final long addr = getCumulativeOffset(offsetBytes);
+    final long newValueReverseBytes = Long.reverseBytes(newValue);
+    return Long.reverseBytes(unsafe.getAndSetLong(getUnsafeObject(), addr, newValueReverseBytes));
+  }
+
+  @Override
+  public boolean compareAndSwapLong(final long offsetBytes, final long expect, final long update) {
+    assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
+    return unsafe.compareAndSwapLong(getUnsafeObject(), getCumulativeOffset(offsetBytes),
+        Long.reverseBytes(expect), Long.reverseBytes(update));
+  }
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Prim.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Prim.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Prim.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_BOOLEAN_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_BOOLEAN_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_BYTE_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_BYTE_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_CHAR_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_CHAR_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_DOUBLE_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_FLOAT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_INT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_INT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_LONG_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_LONG_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_OBJECT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_OBJECT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_SHORT_BASE_OFFSET;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.ARRAY_SHORT_INDEX_SCALE;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.BOOLEAN_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.BYTE_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.CHAR_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.DOUBLE_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.FLOAT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.INT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.LONG_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.OBJECT_SHIFT;
+import static org.apache.datasketches.memory.internal.UnsafeUtil.SHORT_SHIFT;
+
+/**
+ * Creates easy to access association between the major Unsafe constants.
+ *
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("javadoc")
+public enum Prim {
+  BOOLEAN(ARRAY_BOOLEAN_BASE_OFFSET, ARRAY_BOOLEAN_INDEX_SCALE, BOOLEAN_SHIFT),
+  BYTE(ARRAY_BYTE_BASE_OFFSET, ARRAY_BYTE_INDEX_SCALE, BYTE_SHIFT),
+  CHAR(ARRAY_CHAR_BASE_OFFSET, ARRAY_CHAR_INDEX_SCALE, CHAR_SHIFT),
+  SHORT(ARRAY_SHORT_BASE_OFFSET, ARRAY_SHORT_INDEX_SCALE, SHORT_SHIFT),
+  INT(ARRAY_INT_BASE_OFFSET, ARRAY_INT_INDEX_SCALE, INT_SHIFT),
+  LONG(ARRAY_LONG_BASE_OFFSET, ARRAY_LONG_INDEX_SCALE, LONG_SHIFT),
+  FLOAT(ARRAY_FLOAT_BASE_OFFSET, ARRAY_FLOAT_INDEX_SCALE, FLOAT_SHIFT),
+  DOUBLE(ARRAY_DOUBLE_BASE_OFFSET, ARRAY_DOUBLE_INDEX_SCALE, DOUBLE_SHIFT),
+  OBJECT(ARRAY_OBJECT_BASE_OFFSET, ARRAY_OBJECT_INDEX_SCALE, OBJECT_SHIFT);
+
+  private final long arrBaseOff_;
+  private final long arrIdxScale_;
+  private final long sizeShift_;
+
+  private Prim(final long arrBaseOff, final long arrIdxScale, final long sizeShift) {
+    this.arrBaseOff_ = arrBaseOff;
+    this.arrIdxScale_ = arrIdxScale;
+    this.sizeShift_ = sizeShift;
+  }
+
+  public long off() {
+    return arrBaseOff_;
+  }
+
+  public long scale() {
+    return arrIdxScale_;
+  }
+
+  public long shift() {
+    return sizeShift_;
+  }
+
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/StepBoolean.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/StepBoolean.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/StepBoolean.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+
+/**
+ * This is a step boolean function that can change its state only once.
+ *
+ * @author Lee Rhodes
+ */
+public final class StepBoolean {
+  private static final int FALSE = 0;
+  private static final int TRUE = 1;
+  private static final AtomicIntegerFieldUpdater<StepBoolean> STATE_FIELD_UPDATER =
+      AtomicIntegerFieldUpdater.newUpdater(StepBoolean.class, "state");
+
+  private final int initialState;
+  private volatile int state;
+
+  /**
+   * Defines the initial state
+   * @param initialState the given initial state
+   */
+  public StepBoolean(final boolean initialState) {
+    this.initialState = initialState ? TRUE : FALSE;
+    state = this.initialState;
+  }
+
+  /**
+   * Gets the current state.
+   * @return the current state.
+   */
+  public boolean get() {
+    return state == TRUE;
+  }
+
+  /**
+   * This changes the state of this step boolean function if it has not yet changed.
+   * @return true if this call led to the change of the state; false if the state has already been
+   * changed
+   */
+  public boolean change() {
+    final int notInitialState = initialState == TRUE ? FALSE : TRUE;
+    return STATE_FIELD_UPDATER.compareAndSet(this, initialState, notInitialState);
+  }
+
+  /**
+   * Return true if the state has changed from the initial state
+   * @return true if the state has changed from the initial state
+   */
+  public boolean hasChanged() {
+    return state != initialState;
+  }
+}

Added: dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java
==============================================================================
--- dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java (added)
+++ dev/datasketches/memory/2.0.0-RC1/apache-datasketches-memory-2.0.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java Thu Aug 19 22:18:24 2021
@@ -0,0 +1,222 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import sun.misc.Unsafe;
+
+/**
+ * Provides access to the sun.misc.Unsafe class and its key static fields.
+ *
+ * @author Lee Rhodes
+ */
+@SuppressWarnings({"restriction", "javadoc"})
+public final class UnsafeUtil {
+  public static final Unsafe unsafe;
+  public static final String JDK; //must be at least "1.8"
+  public static final int JDK_MAJOR; //8, 9, 10, 11, 12, etc
+
+  //not an indicator of whether compressed references are used.
+  public static final int ADDRESS_SIZE;
+
+  //For 64-bit JVMs: these offsets vary depending on coop: 16 for JVM <= 32GB; 24 for JVM > 32GB.
+  // Making this constant long-typed, rather than int, to exclude possibility of accidental overflow
+  // in expressions like arrayLength * ARRAY_BYTE_BASE_OFFSET, where arrayLength is int-typed.
+  // The same consideration for constants below: ARRAY_*_INDEX_SCALE, ARRAY_*_INDEX_SHIFT.
+  public static final long ARRAY_BOOLEAN_BASE_OFFSET;
+  public static final long ARRAY_BYTE_BASE_OFFSET;
+  public static final long ARRAY_SHORT_BASE_OFFSET;
+  public static final long ARRAY_CHAR_BASE_OFFSET;
+  public static final long ARRAY_INT_BASE_OFFSET;
+  public static final long ARRAY_LONG_BASE_OFFSET;
+  public static final long ARRAY_FLOAT_BASE_OFFSET;
+  public static final long ARRAY_DOUBLE_BASE_OFFSET;
+  public static final long ARRAY_OBJECT_BASE_OFFSET;
+
+  //@formatter:off
+
+  // Setting those values directly instead of using unsafe.arrayIndexScale(), because it may be
+  // beneficial for runtime execution, those values are backed into generated machine code as
+  // constants. E. g. see https://shipilev.net/jvm-anatomy-park/14-constant-variables/
+  public static final int ARRAY_BOOLEAN_INDEX_SCALE = 1;
+  public static final int ARRAY_BYTE_INDEX_SCALE    = 1;
+  public static final long ARRAY_SHORT_INDEX_SCALE  = 2;
+  public static final long ARRAY_CHAR_INDEX_SCALE   = 2;
+  public static final long ARRAY_INT_INDEX_SCALE    = 4;
+  public static final long ARRAY_LONG_INDEX_SCALE   = 8;
+  public static final long ARRAY_FLOAT_INDEX_SCALE  = 4;
+  public static final long ARRAY_DOUBLE_INDEX_SCALE = 8;
+  public static final long ARRAY_OBJECT_INDEX_SCALE;  // varies, 4 or 8 depending on coop
+
+  //Used to convert "type" to bytes:  bytes = longs << LONG_SHIFT
+  public static final int BOOLEAN_SHIFT    = 0;
+  public static final int BYTE_SHIFT       = 0;
+  public static final long SHORT_SHIFT     = 1;
+  public static final long CHAR_SHIFT      = 1;
+  public static final long INT_SHIFT       = 2;
+  public static final long LONG_SHIFT      = 3;
+  public static final long FLOAT_SHIFT     = 2;
+  public static final long DOUBLE_SHIFT    = 3;
+  public static final long OBJECT_SHIFT;     // varies, 2 or 3 depending on coop
+
+  public static final String LS = System.getProperty("line.separator");
+
+  //@formatter:on
+
+  static {
+    try {
+      final Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
+      unsafeConstructor.setAccessible(true);
+      unsafe = unsafeConstructor.newInstance();
+
+      // Alternative, but may not work across different JVMs.
+      //      Field field = Unsafe.class.getDeclaredField("theUnsafe");
+      //      field.setAccessible(true);
+      //      unsafe = (Unsafe) field.get(null);
+
+    } catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
+        | InvocationTargetException | NoSuchMethodException e) {
+      e.printStackTrace();
+      throw new RuntimeException("Unable to acquire Unsafe. " + e);
+    }
+
+    //4 on 32-bit systems. 4 on 64-bit systems < 32GB, otherwise 8.
+    //This alone is not an indicator of compressed ref (coop)
+    ADDRESS_SIZE = unsafe.addressSize();
+
+    ARRAY_BOOLEAN_BASE_OFFSET = unsafe.arrayBaseOffset(boolean[].class);
+    ARRAY_BYTE_BASE_OFFSET = unsafe.arrayBaseOffset(byte[].class);
+    ARRAY_SHORT_BASE_OFFSET = unsafe.arrayBaseOffset(short[].class);
+    ARRAY_CHAR_BASE_OFFSET = unsafe.arrayBaseOffset(char[].class);
+    ARRAY_INT_BASE_OFFSET = unsafe.arrayBaseOffset(int[].class);
+    ARRAY_LONG_BASE_OFFSET = unsafe.arrayBaseOffset(long[].class);
+    ARRAY_FLOAT_BASE_OFFSET = unsafe.arrayBaseOffset(float[].class);
+    ARRAY_DOUBLE_BASE_OFFSET = unsafe.arrayBaseOffset(double[].class);
+    ARRAY_OBJECT_BASE_OFFSET = unsafe.arrayBaseOffset(Object[].class);
+
+    ARRAY_OBJECT_INDEX_SCALE = unsafe.arrayIndexScale(Object[].class);
+    OBJECT_SHIFT = ARRAY_OBJECT_INDEX_SCALE == 4 ? 2 : 3;
+
+    final String jdkVer = System.getProperty("java.version");
+    final int[] p = parseJavaVersion(jdkVer);
+    JDK = p[0] + "." + p[1];
+    JDK_MAJOR = (p[0] == 1) ? p[1] : p[0];
+  }
+
+  private UnsafeUtil() {}
+
+  /**
+   * Returns first two number groups of the java version string.
+   * @param jdkVer the java version string from System.getProperty("java.version").
+   * @return first two number groups of the java version string.
+   */
+  public static int[] parseJavaVersion(final String jdkVer) {
+    final int p0, p1;
+    try {
+      String[] parts = jdkVer.trim().split("[^0-9\\.]");//grab only number groups and "."
+      parts = parts[0].split("\\."); //split out the number groups
+      p0 = Integer.parseInt(parts[0]); //the first number group
+      p1 = (parts.length > 1) ? Integer.parseInt(parts[1]) : 0; //2nd number group, or 0
+    } catch (final NumberFormatException | ArrayIndexOutOfBoundsException  e) {
+      throw new IllegalArgumentException("Improper Java -version string: " + jdkVer + "\n" + e);
+    }
+    //checkJavaVersion(jdkVer, p0, p1); //TODO Optional to omit this.
+    return new int[] {p0, p1};
+  }
+
+  public static void checkJavaVersion(final String jdkVer, final int p0, final int p1) {
+    if ( (p0 < 1) || ((p0 == 1) && (p1 < 8)) || (p0 > 13)  ) {
+      throw new IllegalArgumentException(
+          "Unsupported JDK Major Version, must be one of 1.8, 8, 9, 10, 11, 12, 13: " + jdkVer);
+    }
+  }
+
+  public static long getFieldOffset(final Class<?> c, final String fieldName) {
+    try {
+      return unsafe.objectFieldOffset(c.getDeclaredField(fieldName));
+    } catch (final NoSuchFieldException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
+  /**
+   * Like {@link Unsafe#arrayBaseOffset(Class)}, but caches return values for common array types.
+   * Useful because calling {@link Unsafe#arrayBaseOffset(Class)} directly incurs more overhead.
+   * @param c The given Class&lt;?&gt;.
+   * @return the base-offset
+   */
+  public static long getArrayBaseOffset(final Class<?> c) {
+    // Ordering here is roughly in order of what we expect to be most popular.
+    if (c == byte[].class) {
+      return ARRAY_BYTE_BASE_OFFSET;
+    } else if (c == int[].class) {
+      return ARRAY_INT_BASE_OFFSET;
+    } else if (c == long[].class) {
+      return ARRAY_LONG_BASE_OFFSET;
+    } else if (c == float[].class) {
+      return ARRAY_FLOAT_BASE_OFFSET;
+    } else if (c == double[].class) {
+      return ARRAY_DOUBLE_BASE_OFFSET;
+    } else if (c == boolean[].class) {
+      return ARRAY_BOOLEAN_BASE_OFFSET;
+    } else if (c == short[].class) {
+      return ARRAY_SHORT_BASE_OFFSET;
+    } else if (c == char[].class) {
+      return ARRAY_CHAR_BASE_OFFSET;
+    } else if (c == Object[].class) {
+      return ARRAY_OBJECT_BASE_OFFSET;
+    } else {
+      return unsafe.arrayBaseOffset(c);
+    }
+  }
+
+  /**
+   * Assert the requested offset and length against the allocated size.
+   * The invariants equation is: {@code 0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize}.
+   * If this equation is violated and assertions are enabled, an {@link AssertionError} will
+   * be thrown.
+   * @param reqOff the requested offset
+   * @param reqLen the requested length
+   * @param allocSize the allocated size.
+   */
+  public static void assertBounds(final long reqOff, final long reqLen, final long allocSize) {
+    assert ((reqOff | reqLen | (reqOff + reqLen) | (allocSize - (reqOff + reqLen))) >= 0) :
+      "reqOffset: " + reqOff + ", reqLength: " + reqLen
+      + ", (reqOff + reqLen): " + (reqOff + reqLen) + ", allocSize: " + allocSize;
+  }
+
+  /**
+   * Check the requested offset and length against the allocated size.
+   * The invariants equation is: {@code 0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize}.
+   * If this equation is violated an {@link IllegalArgumentException} will be thrown.
+   * @param reqOff the requested offset
+   * @param reqLen the requested length
+   * @param allocSize the allocated size.
+   */
+  public static void checkBounds(final long reqOff, final long reqLen, final long allocSize) {
+    if ((reqOff | reqLen | (reqOff + reqLen) | (allocSize - (reqOff + reqLen))) < 0) {
+      throw new IllegalArgumentException(
+          "reqOffset: " + reqOff + ", reqLength: " + reqLen
+              + ", (reqOff + reqLen): " + (reqOff + reqLen) + ", allocSize: " + allocSize);
+    }
+  }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org