All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.datasketches.memory.WritableBufferImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/*
 * 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;

import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_CHAR_BASE_OFFSET;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_DOUBLE_BASE_OFFSET;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_DOUBLE_INDEX_SCALE;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_FLOAT_BASE_OFFSET;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_FLOAT_INDEX_SCALE;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_INT_BASE_OFFSET;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_LONG_BASE_OFFSET;
import static org.apache.datasketches.memory.UnsafeUtil.ARRAY_SHORT_BASE_OFFSET;
import static org.apache.datasketches.memory.UnsafeUtil.CHAR_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.DOUBLE_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.FLOAT_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.INT_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.LONG_SHIFT;
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;

/*
 * 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.
 *
 * 

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. Non-native variant is * {@link NonNativeWritableBufferImpl}. * @author Roman Leventov * @author Lee Rhodes */ abstract class WritableBufferImpl extends BaseWritableBufferImpl { //Pass-through ctor WritableBufferImpl(final Object unsafeObj, final long nativeBaseOffset, final long regionOffset, final long capacityBytes, final BaseWritableMemoryImpl originMemory) { super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes, originMemory); } //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); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy