Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapHandleImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapHandleImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapHandleImpl.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.datasketches.memory.MapHandle;
+import org.apache.datasketches.memory.Memory;
+
+class MapHandleImpl implements MapHandle {
+
+ final AllocateDirectMap dirMap;
+ BaseWritableMemoryImpl wMem;
+
+ MapHandleImpl(final AllocateDirectMap dirMap, final BaseWritableMemoryImpl
wMem) {
+ this.dirMap = dirMap;
+ this.wMem = wMem;
+ }
+
+ @Override
+ public Memory get() {
+ return wMem;
+ }
+
+ @Override
+ public void close() {
+ if (dirMap.doClose("MapHandle")) {
+ wMem = null;
+ }
+ }
+
+ @Override
+ public void load() {
+ dirMap.load();
+ }
+
+ @Override
+ public boolean isLoaded() {
+ return dirMap.isLoaded();
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapHandleImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,102 @@
+/*
+ * 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.nio.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+
+/**
+ * Implementation of {@link WritableBuffer} for map memory, non-native byte
order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl
{
+ private static final int id = BUFFER | NONNATIVE | MAP;
+ private final long nativeBaseOffset; //used to compute cumBaseOffset
+ private final StepBoolean valid; //a reference only
+ private final byte typeId;
+
+ MapNonNativeWritableBufferImpl(
+ final long nativeBaseOffset,
+ final long regionOffset,
+ final long capacityBytes,
+ final int typeId,
+ final StepBoolean valid) {
+ super(null, nativeBaseOffset, regionOffset, capacityBytes);
+ this.nativeBaseOffset = nativeBaseOffset;
+ this.valid = valid;
+ this.typeId = (byte) (id | (typeId & 0x7));
+ }
+
+ @Override
+ BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long
capacityBytes,
+ final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | REGION;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid)
+ : new MapNonNativeWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid);
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder
byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid)
+ : new MapNonNativeWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid);
+ }
+
+ @Override
+ BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final
ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly);
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid)
+ : new MapNonNativeWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid);
+ }
+
+ @Override
+ public MemoryRequestServer getMemoryRequestServer() {
+ return null;
+ }
+
+ @Override
+ long getNativeBaseOffset() {
+ return nativeBaseOffset;
+ }
+
+ @Override
+ int getTypeId() {
+ return typeId & 0xff;
+ }
+
+ @Override
+ public boolean isValid() {
+ return valid.get();
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,92 @@
+/*
+ * 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.nio.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * Implementation of {@link WritableMemory} for map memory, non-native byte
order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl
{
+ private static final int id = MEMORY | NONNATIVE | MAP;
+ private final long nativeBaseOffset; //used to compute cumBaseOffset
+ private final StepBoolean valid; //a reference only
+ private final byte typeId;
+
+ MapNonNativeWritableMemoryImpl(
+ final long nativeBaseOffset,
+ final long regionOffset,
+ final long capacityBytes,
+ final int typeId,
+ final StepBoolean valid) {
+ super(null, nativeBaseOffset, regionOffset, capacityBytes);
+ this.nativeBaseOffset = nativeBaseOffset;
+ this.valid = valid;
+ this.typeId = (byte) (id | (typeId & 0x7));
+ }
+
+ @Override
+ BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long
capacityBytes,
+ final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | REGION;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid)
+ : new MapNonNativeWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid);
+ }
+
+ @Override
+ BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final
ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly);
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid)
+ : new MapNonNativeWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid);
+ }
+
+ @Override
+ public MemoryRequestServer getMemoryRequestServer() {
+ return null;
+ }
+
+ @Override
+ long getNativeBaseOffset() {
+ return nativeBaseOffset;
+ }
+
+ @Override
+ int getTypeId() {
+ return typeId & 0xff;
+ }
+
+ @Override
+ public boolean isValid() {
+ return valid.get();
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,102 @@
+/*
+ * 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.nio.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+
+/**
+ * Implementation of {@link WritableBuffer} for map memory, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapWritableBufferImpl extends NativeWritableBufferImpl {
+ private static final int id = BUFFER | NATIVE | MAP;
+ private final long nativeBaseOffset; //used to compute cumBaseOffset
+ private final StepBoolean valid; //a reference only
+ private final byte typeId;
+
+ MapWritableBufferImpl(
+ final long nativeBaseOffset,
+ final long regionOffset,
+ final long capacityBytes,
+ final int typeId,
+ final StepBoolean valid) {
+ super(null, nativeBaseOffset, regionOffset, capacityBytes);
+ this.nativeBaseOffset = nativeBaseOffset;
+ this.valid = valid;
+ this.typeId = (byte) (id | (typeId & 0x7));
+ }
+
+ @Override
+ BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long
capacityBytes,
+ final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | REGION;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid)
+ : new MapNonNativeWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid);
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder
byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid)
+ : new MapNonNativeWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid);
+ }
+
+ @Override
+ BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final
ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly);
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid)
+ : new MapNonNativeWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid);
+ }
+
+ @Override
+ public MemoryRequestServer getMemoryRequestServer() {
+ return null;
+ }
+
+ @Override
+ long getNativeBaseOffset() {
+ return nativeBaseOffset;
+ }
+
+ @Override
+ int getTypeId() {
+ return typeId & 0xff;
+ }
+
+ @Override
+ public boolean isValid() {
+ return valid.get();
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,92 @@
+/*
+ * 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.nio.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * Implementation of {@link WritableMemory} for map memory, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapWritableMemoryImpl extends NativeWritableMemoryImpl {
+ private static final int id = MEMORY | NATIVE | MAP;
+ private final long nativeBaseOffset; //used to compute cumBaseOffset
+ private final StepBoolean valid; //a reference only
+ private final byte typeId;
+
+ MapWritableMemoryImpl(
+ final long nativeBaseOffset,
+ final long regionOffset,
+ final long capacityBytes,
+ final int typeId,
+ final StepBoolean valid) {
+ super(null, nativeBaseOffset, regionOffset, capacityBytes);
+ this.nativeBaseOffset = nativeBaseOffset;
+ this.valid = valid;
+ this.typeId = (byte) (id | (typeId & 0x7));
+ }
+
+ @Override
+ BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long
capacityBytes,
+ final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | REGION;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid)
+ : new MapNonNativeWritableMemoryImpl(
+ nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes,
type, valid);
+ }
+
+ @Override
+ BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final
ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly);
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid)
+ : new MapNonNativeWritableBufferImpl(
+ nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid);
+ }
+
+ @Override
+ public MemoryRequestServer getMemoryRequestServer() {
+ return null;
+ }
+
+ @Override
+ long getNativeBaseOffset() {
+ return nativeBaseOffset;
+ }
+
+ @Override
+ int getTypeId() {
+ return typeId & 0xff;
+ }
+
+ @Override
+ public boolean isValid() {
+ return valid.get();
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MemoryCleaner.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MemoryCleaner.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MemoryCleaner.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,52 @@
+/*
+ * 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 sun.misc.Cleaner;
+
+/**
+ * Extracts a version-dependent reference to the `sun.misc.Cleaner` into
+ * a standalone class. The package name for Cleaner has 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
+ */
+@SuppressWarnings("restriction")
+public class MemoryCleaner {
+ private final Cleaner cleaner;
+
+ /**
+ * Creates a new `sun.misc.Cleaner`.
+ * @param referent the object to be cleaned
+ * @param deallocator - the cleanup code to be run when the cleaner is
invoked.
+ * return MemoryCleaner
+ */
+ public MemoryCleaner(final Object referent, final Runnable deallocator) {
+ cleaner = Cleaner.create(referent, deallocator);
+ }
+
+ /**
+ * Runs this cleaner, if it has not been run before.
+ */
+ public void clean() {
+ cleaner.clean();
+ }
+}
+
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MemoryCleaner.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
Tue May 21 21:11:49 2024
@@ -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);
+ }
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
Tue May 21 21:11:49 2024
@@ -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);
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
Tue May 21 21:11:49 2024
@@ -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);
+ }
+ }
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java
Tue May 21 21:11:49 2024
@@ -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";
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NioBitsFields.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
Tue May 21 21:11:49 2024
@@ -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));
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
Tue May 21 21:11:49 2024
@@ -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));
+ }
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
------------------------------------------------------------------------------
svn:executable = *
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]