You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by se...@apache.org on 2017/10/08 19:38:19 UTC
[09/10] flink git commit: [FLINK-7312] [checkstyle] Enable checkstyle
for 'flink/core/memory/*'
[FLINK-7312] [checkstyle] Enable checkstyle for 'flink/core/memory/*'
We deliberately ignore redundant modifiers for now since we want `final`
modifiers on `final` classes for increased future-proofness.
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/c3235c39
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/c3235c39
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/c3235c39
Branch: refs/heads/master
Commit: c3235c395f7bb69b482b85c6832d427100130ca3
Parents: 9e12840
Author: Nico Kruber <ni...@data-artisans.com>
Authored: Mon Jul 31 12:06:14 2017 +0200
Committer: Stephan Ewen <se...@apache.org>
Committed: Fri Oct 6 20:38:02 2017 +0200
----------------------------------------------------------------------
.../apache/flink/core/memory/DataInputView.java | 14 +-
.../core/memory/DataInputViewStreamWrapper.java | 2 +-
.../flink/core/memory/DataOutputView.java | 20 +-
.../memory/DataOutputViewStreamWrapper.java | 4 +-
.../flink/core/memory/HeapMemorySegment.java | 28 +--
.../flink/core/memory/HybridMemorySegment.java | 63 +++---
.../apache/flink/core/memory/MemorySegment.java | 213 +++++++++++--------
.../flink/core/memory/MemorySegmentFactory.java | 45 ++--
.../flink/core/memory/MemorySegmentSource.java | 6 +-
.../apache/flink/core/memory/MemoryUtils.java | 6 +-
.../core/memory/SeekableDataInputView.java | 7 +-
.../core/memory/SeekableDataOutputView.java | 7 +-
.../memory/ByteArrayInputStreamWithPosTest.java | 11 +-
.../ByteArrayOutputStreamWithPosTest.java | 14 +-
.../flink/core/memory/CrossSegmentTypeTest.java | 49 +++--
.../core/memory/EndiannessAccessChecks.java | 19 +-
.../core/memory/HeapMemorySegmentTest.java | 9 +-
.../memory/HybridOffHeapMemorySegmentTest.java | 4 +
.../memory/HybridOnHeapMemorySegmentTest.java | 6 +-
.../core/memory/MemorySegmentChecksTest.java | 14 +-
.../core/memory/MemorySegmentFactoryTest.java | 7 +-
.../core/memory/MemorySegmentTestBase.java | 181 ++++++++--------
.../memory/MemorySegmentUndersizedTest.java | 37 ++--
.../memory/OperationsOnFreedSegmentTest.java | 84 ++++----
.../flink/runtime/memory/MemoryManager.java | 2 +-
tools/maven/suppressions-core.xml | 8 -
26 files changed, 461 insertions(+), 399 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/DataInputView.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/DataInputView.java b/flink-core/src/main/java/org/apache/flink/core/memory/DataInputView.java
index 88d54d9..7da3fc3 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/DataInputView.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/DataInputView.java
@@ -16,10 +16,8 @@
* limitations under the License.
*/
-
package org.apache.flink.core.memory;
-
import org.apache.flink.annotation.Public;
import java.io.DataInput;
@@ -32,17 +30,17 @@ import java.io.IOException;
*/
@Public
public interface DataInputView extends DataInput {
-
+
/**
* Skips {@code numBytes} bytes of memory. In contrast to the {@link #skipBytes(int)} method,
* this method always skips the desired number of bytes or throws an {@link java.io.EOFException}.
- *
+ *
* @param numBytes The number of bytes to skip.
- *
+ *
* @throws IOException Thrown, if any I/O related problem occurred such that the input could not
* be advanced to the desired position.
*/
- public void skipBytesToRead(int numBytes) throws IOException;
+ void skipBytesToRead(int numBytes) throws IOException;
/**
* Reads up to {@code len} bytes of memory and stores it into {@code b} starting at offset {@code off}.
@@ -54,7 +52,7 @@ public interface DataInputView extends DataInput {
* @return the number of actually read bytes of -1 if there is no more data left
* @throws IOException
*/
- public int read(byte[] b, int off, int len) throws IOException;
+ int read(byte[] b, int off, int len) throws IOException;
/**
* Tries to fill the given byte array {@code b}. Returns the actually number of read bytes or -1 if there is no
@@ -64,5 +62,5 @@ public interface DataInputView extends DataInput {
* @return the number of read bytes or -1 if there is no more data left
* @throws IOException
*/
- public int read(byte[] b) throws IOException;
+ int read(byte[] b) throws IOException;
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/DataInputViewStreamWrapper.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/DataInputViewStreamWrapper.java b/flink-core/src/main/java/org/apache/flink/core/memory/DataInputViewStreamWrapper.java
index 19eb285..664dc01 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/DataInputViewStreamWrapper.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/DataInputViewStreamWrapper.java
@@ -34,7 +34,7 @@ public class DataInputViewStreamWrapper extends DataInputStream implements DataI
public DataInputViewStreamWrapper(InputStream in) {
super(in);
}
-
+
@Override
public void skipBytesToRead(int numBytes) throws IOException {
if (skipBytes(numBytes) != numBytes){
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputView.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputView.java b/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputView.java
index d9219b8..3896299 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputView.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputView.java
@@ -16,10 +16,8 @@
* limitations under the License.
*/
-
package org.apache.flink.core.memory;
-
import org.apache.flink.annotation.Public;
import java.io.DataOutput;
@@ -32,26 +30,26 @@ import java.io.IOException;
*/
@Public
public interface DataOutputView extends DataOutput {
-
+
/**
* Skips {@code numBytes} bytes memory. If some program reads the memory that was skipped over, the
- * results are undefined.
- *
+ * results are undefined.
+ *
* @param numBytes The number of bytes to skip.
- *
+ *
* @throws IOException Thrown, if any I/O related problem occurred such that the view could not
* be advanced to the desired position.
*/
- public void skipBytesToWrite(int numBytes) throws IOException;
-
+ void skipBytesToWrite(int numBytes) throws IOException;
+
/**
* Copies {@code numBytes} bytes from the source to this view.
- *
+ *
* @param source The source to copy the bytes from.
* @param numBytes The number of bytes to copy.
- *
+ *
* @throws IOException Thrown, if any I/O related problem occurred, such that either the input view
* could not be read, or the output could not be written.
*/
- public void write(DataInputView source, int numBytes) throws IOException;
+ void write(DataInputView source, int numBytes) throws IOException;
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputViewStreamWrapper.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputViewStreamWrapper.java b/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputViewStreamWrapper.java
index 4e45532..5fee01d 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputViewStreamWrapper.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputViewStreamWrapper.java
@@ -31,7 +31,7 @@ import java.io.OutputStream;
public class DataOutputViewStreamWrapper extends DataOutputStream implements DataOutputView {
private byte[] tempBuffer;
-
+
public DataOutputViewStreamWrapper(OutputStream out) {
super(out);
}
@@ -54,7 +54,7 @@ public class DataOutputViewStreamWrapper extends DataOutputStream implements Dat
if (tempBuffer == null) {
tempBuffer = new byte[4096];
}
-
+
while (numBytes > 0) {
int toCopy = Math.min(numBytes, tempBuffer.length);
source.readFully(tempBuffer, 0, toCopy);
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/HeapMemorySegment.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/HeapMemorySegment.java b/flink-core/src/main/java/org/apache/flink/core/memory/HeapMemorySegment.java
index d136404..9aa81d0 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/HeapMemorySegment.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/HeapMemorySegment.java
@@ -30,18 +30,20 @@ import java.util.Objects;
* This class represents a piece of heap memory managed by Flink.
* The segment is backed by a byte array and features random put and get methods for the basic types,
* as well as compare and swap methods.
- * <p>
- * This class specialized byte access and byte copy calls for heap memory, while reusing the
+ *
+ * <p>This class specializes byte access and byte copy calls for heap memory, while reusing the
* multi-byte type accesses and cross-segment operations from the MemorySegment.
- * <p>
- * Note that memory segments should usually not be allocated manually, but rather through the
+ *
+ * <p>Note that memory segments should usually not be allocated manually, but rather through the
* {@link MemorySegmentFactory}.
*/
@Internal
public final class HeapMemorySegment extends MemorySegment {
- /** An extra reference to the heap memory, so we can let byte array checks fail
- * by the built-in checks automatically without extra checks */
+ /**
+ * An extra reference to the heap memory, so we can let byte array checks fail by the built-in
+ * checks automatically without extra checks.
+ */
private byte[] memory;
/**
@@ -53,7 +55,7 @@ public final class HeapMemorySegment extends MemorySegment {
HeapMemorySegment(byte[] memory) {
this(memory, null);
}
-
+
/**
* Creates a new memory segment that represents the data in the given byte array.
* The memory segment references the given owner.
@@ -65,7 +67,7 @@ public final class HeapMemorySegment extends MemorySegment {
super(Objects.requireNonNull(memory), owner);
this.memory = memory;
}
-
+
// -------------------------------------------------------------------------
// MemorySegment operations
// -------------------------------------------------------------------------
@@ -94,11 +96,11 @@ public final class HeapMemorySegment extends MemorySegment {
public byte[] getArray() {
return this.heapMemory;
}
-
+
// ------------------------------------------------------------------------
// Random Access get() and put() methods
// ------------------------------------------------------------------------
-
+
@Override
public final byte get(int index) {
return this.memory[index];
@@ -198,9 +200,11 @@ public final class HeapMemorySegment extends MemorySegment {
"The MemorySegment factory was not initialized for off-heap memory.");
}
- /** prevent external instantiation */
+ /**
+ * Prevent external instantiation.
+ */
HeapMemorySegmentFactory() {}
- };
+ }
public static final HeapMemorySegmentFactory FACTORY = new HeapMemorySegmentFactory();
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java b/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java
index 1fe37ee..0b23ef3 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java
@@ -31,29 +31,32 @@ import java.nio.ByteBuffer;
/**
* This class represents a piece of memory managed by Flink. The memory can be on-heap or off-heap,
* this is transparently handled by this class.
- * <p>
- * This class specialized byte access and byte copy calls for heap memory, while reusing the
+ *
+ * <p>This class specializes byte access and byte copy calls for heap memory, while reusing the
* multi-byte type accesses and cross-segment operations from the MemorySegment.
- * <p>
- * This class subsumes the functionality of the {@link org.apache.flink.core.memory.HeapMemorySegment},
+ *
+ * <p>This class subsumes the functionality of the {@link org.apache.flink.core.memory.HeapMemorySegment},
* but is a bit less efficient for operations on individual bytes.
- * <p>
- * Note that memory segments should usually not be allocated manually, but rather through the
+ *
+ * <p>Note that memory segments should usually not be allocated manually, but rather through the
* {@link MemorySegmentFactory}.
*/
@Internal
public final class HybridMemorySegment extends MemorySegment {
-
- /** The direct byte buffer that allocated the off-heap memory. This memory segment holds a reference
- * to that buffer, so as long as this memory segment lives, the memory will not be released. */
+
+ /**
+ * The direct byte buffer that allocated the off-heap memory. This memory segment holds a
+ * reference to that buffer, so as long as this memory segment lives, the memory will not be
+ * released.
+ */
private final ByteBuffer offHeapBuffer;
/**
* Creates a new memory segment that represents the memory backing the given direct byte buffer.
* Note that the given ByteBuffer must be direct {@link java.nio.ByteBuffer#allocateDirect(int)},
* otherwise this method with throw an IllegalArgumentException.
- * <p>
- * The owner referenced by this memory segment is null.
+ *
+ * <p>The owner referenced by this memory segment is null.
*
* @param buffer The byte buffer whose memory is represented by this memory segment.
* @throws IllegalArgumentException Thrown, if the given ByteBuffer is not direct.
@@ -61,13 +64,13 @@ public final class HybridMemorySegment extends MemorySegment {
HybridMemorySegment(ByteBuffer buffer) {
this(buffer, null);
}
-
+
/**
* Creates a new memory segment that represents the memory backing the given direct byte buffer.
* Note that the given ByteBuffer must be direct {@link java.nio.ByteBuffer#allocateDirect(int)},
* otherwise this method with throw an IllegalArgumentException.
- * <p>
- * The memory segment references the given owner.
+ *
+ * <p>The memory segment references the given owner.
*
* @param buffer The byte buffer whose memory is represented by this memory segment.
* @param owner The owner references by this memory segment.
@@ -80,8 +83,8 @@ public final class HybridMemorySegment extends MemorySegment {
/**
* Creates a new memory segment that represents the memory of the byte array.
- * <p>
- * The owner referenced by this memory segment is null.
+ *
+ * <p>The owner referenced by this memory segment is null.
*
* @param buffer The byte array whose memory is represented by this memory segment.
*/
@@ -91,8 +94,8 @@ public final class HybridMemorySegment extends MemorySegment {
/**
* Creates a new memory segment that represents the memory of the byte array.
- * <p>
- * The memory segment references the given owner.
+ *
+ * <p>The memory segment references the given owner.
*
* @param buffer The byte array whose memory is represented by this memory segment.
* @param owner The owner references by this memory segment.
@@ -105,7 +108,7 @@ public final class HybridMemorySegment extends MemorySegment {
// -------------------------------------------------------------------------
// MemorySegment operations
// -------------------------------------------------------------------------
-
+
public byte[] getArray() {
if (heapMemory != null) {
return heapMemory;
@@ -153,7 +156,7 @@ public final class HybridMemorySegment extends MemorySegment {
// ------------------------------------------------------------------------
// Random Access get() and put() methods
// ------------------------------------------------------------------------
-
+
@Override
public byte get(int index) {
final long pos = address + index;
@@ -197,7 +200,7 @@ public final class HybridMemorySegment extends MemorySegment {
@Override
public void get(int index, byte[] dst, int offset, int length) {
// check the byte array offset and length and the status
- if ( (offset | length | (offset + length) | (dst.length - (offset + length))) < 0) {
+ if ((offset | length | (offset + length) | (dst.length - (offset + length))) < 0) {
throw new IndexOutOfBoundsException();
}
@@ -221,7 +224,7 @@ public final class HybridMemorySegment extends MemorySegment {
if ((offset | length | (offset + length) | (src.length - (offset + length))) < 0) {
throw new IndexOutOfBoundsException();
}
-
+
final long pos = address + index;
if (index >= 0 && pos <= addressLimit - length) {
@@ -263,7 +266,7 @@ public final class HybridMemorySegment extends MemorySegment {
offset += 8;
length -= 8;
}
-
+
while (length > 0) {
out.writeByte(get(offset));
offset++;
@@ -396,7 +399,9 @@ public final class HybridMemorySegment extends MemorySegment {
// Utilities for native memory accesses and checks
// --------------------------------------------------------------------------------------------
- /** The reflection fields with which we access the off-heap pointer from direct ByteBuffers */
+ /**
+ * The reflection fields with which we access the off-heap pointer from direct ByteBuffers.
+ */
private static final Field ADDRESS_FIELD;
static {
@@ -421,7 +426,7 @@ public final class HybridMemorySegment extends MemorySegment {
throw new RuntimeException("Could not access direct byte buffer address.", t);
}
}
-
+
private static long checkBufferAndGetAddress(ByteBuffer buffer) {
if (buffer == null) {
throw new NullPointerException("buffer is null");
@@ -440,7 +445,7 @@ public final class HybridMemorySegment extends MemorySegment {
* Base factory for hybrid memory segments.
*/
public static final class HybridMemorySegmentFactory implements MemorySegmentFactory.Factory {
-
+
@Override
public HybridMemorySegment wrap(byte[] memory) {
return new HybridMemorySegment(memory);
@@ -461,9 +466,11 @@ public final class HybridMemorySegment extends MemorySegment {
return new HybridMemorySegment(memory, owner);
}
- /** prevent external instantiation */
+ /**
+ * Prevent external instantiation.
+ */
HybridMemorySegmentFactory() {}
- };
+ }
public static final HybridMemorySegmentFactory FACTORY = new HybridMemorySegmentFactory();
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java
index d8315c5..1f3804f 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java
@@ -29,14 +29,14 @@ import java.nio.ByteOrder;
/**
* This class represents a piece of memory managed by Flink.
* The segment may be backed by heap memory (byte array) or by off-heap memory.
- * <p>
- * The methods for individual memory access are specialized in the classes
+ *
+ * <p>The methods for individual memory access are specialized in the classes
* {@link org.apache.flink.core.memory.HeapMemorySegment} and
* {@link org.apache.flink.core.memory.HybridMemorySegment}.
* All methods that operate across two memory segments are implemented in this class,
* to transparently handle the mixing of memory segment types.
- * <p>
- * This class fulfills conceptually a similar purpose as Java's {@link java.nio.ByteBuffer}.
+ *
+ * <p>This class fulfills conceptually a similar purpose as Java's {@link java.nio.ByteBuffer}.
* We add this specialized class for various reasons:
* <ul>
* <li>It offers additional binary compare, swap, and copy methods.</li>
@@ -47,20 +47,20 @@ import java.nio.ByteOrder;
* a byte order.</li>
* <li>It transparently and efficiently moves data between on-heap and off-heap variants.</li>
* </ul>
- *
- * <i>Comments on the implementation</i>:
+ *
+ * <p><i>Comments on the implementation</i>:
* We make heavy use of operations that are supported by native
* instructions, to achieve a high efficiency. Multi byte types (int, long, float, double, ...)
* are read and written with "unsafe" native commands.
- * <p>
- * Below is an example of the code generated for the {@link HeapMemorySegment#putLongBigEndian(int, long)}
+ *
+ * <p>Below is an example of the code generated for the {@link HeapMemorySegment#putLongBigEndian(int, long)}
* function by the just-in-time compiler. The code is grabbed from an Oracle JVM 7 using the
* hotspot disassembler library (hsdis32.dll) and the jvm command
* <i>-XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,*MemorySegment.putLongBigEndian</i>.
* Note that this code realizes both the byte order swapping and the reinterpret cast access to
* get a long from the byte array.
*
- * <pre>
+ * <p><pre>
* [Verified Entry Point]
* 0x00007fc403e19920: sub $0x18,%rsp
* 0x00007fc403e19927: mov %rbp,0x10(%rsp) ;*synchronization entry
@@ -78,16 +78,16 @@ import java.nio.ByteOrder;
* 0x00007fc403e19943: pop %rbp
* 0x00007fc403e19944: test %eax,0x5ba76b6(%rip) # 0x00007fc4099c1000
* ; {poll_return}
- * 0x00007fc403e1994a: retq
+ * 0x00007fc403e1994a: retq
* </pre>
*
- * <i>Note on efficiency</i>:
+ * <p><i>Note on efficiency</i>:
* For best efficiency, the code that uses this class should make sure that only one
* subclass is loaded, or that the methods that are abstract in this class are used only from one of the
- * subclasses (either the {@link org.apache.flink.core.memory.HeapMemorySegment}, or the
+ * subclasses (either the {@link org.apache.flink.core.memory.HeapMemorySegment}, or the
* {@link org.apache.flink.core.memory.HybridMemorySegment}).
- *
- * That way, all the abstract methods in the MemorySegment base class have only one loaded
+ *
+ * <p>That way, all the abstract methods in the MemorySegment base class have only one loaded
* actual implementation. This is easy for the JIT to recognize through class hierarchy analysis,
* or by identifying that the invocations are monomorphic (all go to the same concrete
* method implementation). Under these conditions, the JIT can perfectly inline methods.
@@ -95,43 +95,62 @@ import java.nio.ByteOrder;
@Internal
public abstract class MemorySegment {
- /** The unsafe handle for transparent memory copied (heap / off-heap) */
+ /**
+ * The unsafe handle for transparent memory copied (heap / off-heap).
+ */
@SuppressWarnings("restriction")
protected static final sun.misc.Unsafe UNSAFE = MemoryUtils.UNSAFE;
- /** The beginning of the byte array contents, relative to the byte array object */
+ /**
+ * The beginning of the byte array contents, relative to the byte array object.
+ */
@SuppressWarnings("restriction")
protected static final long BYTE_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
-
- /** Constant that flags the byte order. Because this is a boolean constant,
- * the JIT compiler can use this well to aggressively eliminate the non-applicable code paths */
+
+ /**
+ * Constant that flags the byte order. Because this is a boolean constant, the JIT compiler can
+ * use this well to aggressively eliminate the non-applicable code paths.
+ */
private static final boolean LITTLE_ENDIAN = (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN);
-
+
// ------------------------------------------------------------------------
- /** The heap byte array object relative to which we access the memory. Is non-null if the
- * memory is on the heap, and is null, if the memory if off the heap. If we have this buffer, we
- * must never void this reference, or the memory segment will point to undefined addresses
- * outside the heap and may in out-of-order execution cases cause segmentation faults. */
+ /**
+ * The heap byte array object relative to which we access the memory.
+ *
+ * <p>Is non-<tt>null</tt> if the memory is on the heap, and is <tt>null</tt>, if the memory if
+ * off the heap. If we have this buffer, we must never void this reference, or the memory
+ * segment will point to undefined addresses outside the heap and may in out-of-order execution
+ * cases cause segmentation faults.
+ */
protected final byte[] heapMemory;
- /** The address to the data, relative to the heap memory byte array. If the heap memory byte array
- * is null, this becomes an absolute memory address outside the heap. */
+ /**
+ * The address to the data, relative to the heap memory byte array. If the heap memory byte
+ * array is <tt>null</tt>, this becomes an absolute memory address outside the heap.
+ */
protected long address;
- /** The address one byte after the last addressable byte.
- * This is address + size while the segment is not disposed */
+ /**
+ * The address one byte after the last addressable byte, i.e. <tt>address + size</tt> while the
+ * segment is not disposed.
+ */
protected final long addressLimit;
-
- /** The size in bytes of the memory segment */
+
+ /**
+ * The size in bytes of the memory segment.
+ */
protected final int size;
-
- /** Optional owner of the memory segment */
+
+ /**
+ * Optional owner of the memory segment.
+ */
private final Object owner;
/**
* Creates a new memory segment that represents the memory of the byte array.
- * Since the byte array is backed by on-heap memory, this memory segment holds its
+ *
+ * <p>Since the byte array is backed by on-heap memory, this memory segment holds its
* data on heap. The buffer must be at least of size 8 bytes.
*
* @param buffer The byte array whose memory is represented by this memory segment.
@@ -140,7 +159,7 @@ public abstract class MemorySegment {
if (buffer == null) {
throw new NullPointerException("buffer");
}
-
+
this.heapMemory = buffer;
this.address = BYTE_ARRAY_BASE_OFFSET;
this.size = buffer.length;
@@ -164,20 +183,21 @@ public abstract class MemorySegment {
throw new IllegalArgumentException("Segment initialized with too large address: " + offHeapAddress
+ " ; Max allowed address is " + (Long.MAX_VALUE - Integer.MAX_VALUE - 1));
}
-
+
this.heapMemory = null;
this.address = offHeapAddress;
this.addressLimit = this.address + size;
this.size = size;
this.owner = owner;
}
-
+
// ------------------------------------------------------------------------
// Memory Segment Operations
// ------------------------------------------------------------------------
/**
* Gets the size of the memory segment, in bytes.
+ *
* @return The size of the memory segment.
*/
public int size() {
@@ -186,16 +206,19 @@ public abstract class MemorySegment {
/**
* Checks whether the memory segment was freed.
- * @return True, if the memory segment has been freed, false otherwise.
+ *
+ * @return <tt>true</tt>, if the memory segment has been freed, <tt>false</tt> otherwise.
*/
public boolean isFreed() {
return address > addressLimit;
}
/**
- * Frees this memory segment. After this operation has been called, no further operations are
- * possible on the memory segment and will fail. The actual memory (heap or off-heap) will only
- * be released after this memory segment object has become garbage collected.
+ * Frees this memory segment.
+ *
+ * <p>After this operation has been called, no further operations are possible on the memory
+ * segment and will fail. The actual memory (heap or off-heap) will only be released after this
+ * memory segment object has become garbage collected.
*/
public void free() {
// this ensures we can place no more data and trigger
@@ -205,8 +228,9 @@ public abstract class MemorySegment {
/**
* Checks whether this memory segment is backed by off-heap memory.
- * @return True, if the memory segment is backed by off-heap memory, false if it is backed
- * by heap memory.
+ *
+ * @return <tt>true</tt>, if the memory segment is backed by off-heap memory, <tt>false</tt> if
+ * it is backed by heap memory.
*/
public boolean isOffHeap() {
return heapMemory == null;
@@ -218,6 +242,7 @@ public abstract class MemorySegment {
*
* @param offset The offset in the memory segment.
* @param length The number of bytes to be wrapped as a buffer.
+ *
* @return A <tt>ByteBuffer</tt> backed by the specified portion of the memory segment.
* @throws IndexOutOfBoundsException Thrown, if offset is negative or larger than the memory segment size,
* or if the offset plus the length is larger than the segment size.
@@ -226,13 +251,14 @@ public abstract class MemorySegment {
/**
* Gets the owner of this memory segment. Returns null, if the owner was not set.
+ *
* @return The owner of the memory segment, or null, if it does not have an owner.
*/
public Object getOwner() {
return owner;
}
-
-
+
+
// ------------------------------------------------------------------------
// Random Access get() and put() methods
// ------------------------------------------------------------------------
@@ -241,13 +267,13 @@ public abstract class MemorySegment {
// Notes on the implementation: We try to collapse as many checks as
// possible. We need to obey the following rules to make this safe
// against segfaults:
- //
+ //
// - Grab mutable fields onto the stack before checking and using. This
// guards us against concurrent modifications which invalidate the
// pointers
- // - Use subtrations for range checks, as they are tolerant
+ // - Use subtrations for range checks, as they are tolerant
//------------------------------------------------------------------------
-
+
/**
* Reads the byte at the given position.
*
@@ -277,7 +303,7 @@ public abstract class MemorySegment {
* @param index The position at which the first byte will be read.
* @param dst The memory into which the memory will be copied.
*
- * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large that the data between the
+ * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large that the data between the
* index and the memory segment end is not enough to fill the destination array.
*/
public abstract void get(int index, byte[] dst);
@@ -289,22 +315,22 @@ public abstract class MemorySegment {
* @param index The index in the memory segment array, where the data is put.
* @param src The source array to copy the data from.
*
- * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large such that the array
+ * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large such that the array
* size exceed the amount of memory between the index and the memory
- * segment's end.
+ * segment's end.
*/
public abstract void put(int index, byte[] src);
/**
* Bulk get method. Copies length memory from the specified position to the
- * destination memory, beginning at the given offset
+ * destination memory, beginning at the given offset.
*
* @param index The position at which the first byte will be read.
* @param dst The memory into which the memory will be copied.
* @param offset The copying offset in the destination memory.
* @param length The number of bytes to be copied.
*
- * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large that the requested number of
+ * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large that the requested number of
* bytes exceed the amount of memory between the index and the memory
* segment's end.
*/
@@ -320,7 +346,7 @@ public abstract class MemorySegment {
* @param offset The offset in the source array where the copying is started.
* @param length The number of bytes to copy.
*
- * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large such that the array
+ * @throws IndexOutOfBoundsException Thrown, if the index is negative, or too large such that the array
* portion to copy exceed the amount of memory between the index and the memory
* segment's end.
*/
@@ -377,7 +403,7 @@ public abstract class MemorySegment {
/**
* Reads an character value (16 bit, 2 bytes) from the given position, in little-endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getChar(int)}. For most cases (such as
+ * is possibly slower than {@link #getChar(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getChar(int)} is the preferable choice.
@@ -398,7 +424,7 @@ public abstract class MemorySegment {
/**
* Reads an character value (16 bit, 2 bytes) from the given position, in big-endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getChar(int)}. For most cases (such as
+ * is possibly slower than {@link #getChar(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getChar(int)} is the preferable choice.
@@ -443,7 +469,7 @@ public abstract class MemorySegment {
/**
* Writes the given character (16 bit, 2 bytes) to the given position in little-endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putChar(int, char)}. For most cases (such as
+ * is possibly slower than {@link #putChar(int, char)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putChar(int, char)} is the preferable choice.
@@ -464,7 +490,7 @@ public abstract class MemorySegment {
/**
* Writes the given character (16 bit, 2 bytes) to the given position in big-endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putChar(int, char)}. For most cases (such as
+ * is possibly slower than {@link #putChar(int, char)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putChar(int, char)} is the preferable choice.
@@ -509,7 +535,7 @@ public abstract class MemorySegment {
/**
* Reads an short integer value (16 bit, 2 bytes) from the given position, in little-endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getShort(int)}. For most cases (such as
+ * is possibly slower than {@link #getShort(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getShort(int)} is the preferable choice.
@@ -530,7 +556,7 @@ public abstract class MemorySegment {
/**
* Reads an short integer value (16 bit, 2 bytes) from the given position, in big-endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getShort(int)}. For most cases (such as
+ * is possibly slower than {@link #getShort(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getShort(int)} is the preferable choice.
@@ -575,7 +601,7 @@ public abstract class MemorySegment {
/**
* Writes the given short integer value (16 bit, 2 bytes) to the given position in little-endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putShort(int, short)}. For most cases (such as
+ * is possibly slower than {@link #putShort(int, short)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putShort(int, short)} is the preferable choice.
@@ -596,7 +622,7 @@ public abstract class MemorySegment {
/**
* Writes the given short integer value (16 bit, 2 bytes) to the given position in big-endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putShort(int, short)}. For most cases (such as
+ * is possibly slower than {@link #putShort(int, short)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putShort(int, short)} is the preferable choice.
@@ -618,7 +644,7 @@ public abstract class MemorySegment {
* Reads an int value (32bit, 4 bytes) from the given position, in the system's native byte order.
* This method offers the best speed for integer reading and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -645,7 +671,7 @@ public abstract class MemorySegment {
/**
* Reads an int value (32bit, 4 bytes) from the given position, in little-endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getInt(int)}. For most cases (such as
+ * is possibly slower than {@link #getInt(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getInt(int)} is the preferable choice.
@@ -667,7 +693,7 @@ public abstract class MemorySegment {
/**
* Reads an int value (32bit, 4 bytes) from the given position, in big-endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getInt(int)}. For most cases (such as
+ * is possibly slower than {@link #getInt(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getInt(int)} is the preferable choice.
@@ -690,7 +716,7 @@ public abstract class MemorySegment {
* Writes the given int value (32bit, 4 bytes) to the given position in the system's native
* byte order. This method offers the best speed for integer writing and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -717,7 +743,7 @@ public abstract class MemorySegment {
/**
* Writes the given int value (32bit, 4 bytes) to the given position in little endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putInt(int, int)}. For most cases (such as
+ * is possibly slower than {@link #putInt(int, int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putInt(int, int)} is the preferable choice.
@@ -739,7 +765,7 @@ public abstract class MemorySegment {
/**
* Writes the given int value (32bit, 4 bytes) to the given position in big endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putInt(int, int)}. For most cases (such as
+ * is possibly slower than {@link #putInt(int, int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putInt(int, int)} is the preferable choice.
@@ -762,7 +788,7 @@ public abstract class MemorySegment {
* Reads a long value (64bit, 8 bytes) from the given position, in the system's native byte order.
* This method offers the best speed for long integer reading and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -789,7 +815,7 @@ public abstract class MemorySegment {
/**
* Reads a long integer value (64bit, 8 bytes) from the given position, in little endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getLong(int)}. For most cases (such as
+ * is possibly slower than {@link #getLong(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getLong(int)} is the preferable choice.
@@ -811,7 +837,7 @@ public abstract class MemorySegment {
/**
* Reads a long integer value (64bit, 8 bytes) from the given position, in big endian byte order.
* This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getLong(int)}. For most cases (such as
+ * is possibly slower than {@link #getLong(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getLong(int)} is the preferable choice.
@@ -834,7 +860,7 @@ public abstract class MemorySegment {
* Writes the given long value (64bit, 8 bytes) to the given position in the system's native
* byte order. This method offers the best speed for long integer writing and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -861,7 +887,7 @@ public abstract class MemorySegment {
/**
* Writes the given long value (64bit, 8 bytes) to the given position in little endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putLong(int, long)}. For most cases (such as
+ * is possibly slower than {@link #putLong(int, long)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putLong(int, long)} is the preferable choice.
@@ -883,7 +909,7 @@ public abstract class MemorySegment {
/**
* Writes the given long value (64bit, 8 bytes) to the given position in big endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putLong(int, long)}. For most cases (such as
+ * is possibly slower than {@link #putLong(int, long)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putLong(int, long)} is the preferable choice.
@@ -906,7 +932,7 @@ public abstract class MemorySegment {
* Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in the system's
* native byte order. This method offers the best speed for float reading and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -923,7 +949,7 @@ public abstract class MemorySegment {
/**
* Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in little endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getFloat(int)}. For most cases (such as
+ * is possibly slower than {@link #getFloat(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getFloat(int)} is the preferable choice.
@@ -941,7 +967,7 @@ public abstract class MemorySegment {
/**
* Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in big endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getFloat(int)}. For most cases (such as
+ * is possibly slower than {@link #getFloat(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getFloat(int)} is the preferable choice.
@@ -960,7 +986,7 @@ public abstract class MemorySegment {
* Writes the given single-precision float value (32bit, 4 bytes) to the given position in the system's native
* byte order. This method offers the best speed for float writing and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -977,7 +1003,7 @@ public abstract class MemorySegment {
/**
* Writes the given single-precision float value (32bit, 4 bytes) to the given position in little endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putFloat(int, float)}. For most cases (such as
+ * is possibly slower than {@link #putFloat(int, float)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putFloat(int, float)} is the preferable choice.
@@ -995,7 +1021,7 @@ public abstract class MemorySegment {
/**
* Writes the given single-precision float value (32bit, 4 bytes) to the given position in big endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putFloat(int, float)}. For most cases (such as
+ * is possibly slower than {@link #putFloat(int, float)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putFloat(int, float)} is the preferable choice.
@@ -1014,7 +1040,7 @@ public abstract class MemorySegment {
* Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in the system's
* native byte order. This method offers the best speed for double reading and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -1031,7 +1057,7 @@ public abstract class MemorySegment {
/**
* Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in little endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getDouble(int)}. For most cases (such as
+ * is possibly slower than {@link #getDouble(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getDouble(int)} is the preferable choice.
@@ -1049,7 +1075,7 @@ public abstract class MemorySegment {
/**
* Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in big endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #getDouble(int)}. For most cases (such as
+ * is possibly slower than {@link #getDouble(int)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #getDouble(int)} is the preferable choice.
@@ -1068,7 +1094,7 @@ public abstract class MemorySegment {
* Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position in the
* system's native byte order. This method offers the best speed for double writing and should be used
* unless a specific byte order is required. In most cases, it suffices to know that the
- * byte order in which the value is written is the same as the one in which it is read
+ * byte order in which the value is written is the same as the one in which it is read
* (such as transient storage in memory, or serialization for I/O and network), making this
* method the preferable choice.
*
@@ -1085,7 +1111,7 @@ public abstract class MemorySegment {
/**
* Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position in little endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putDouble(int, double)}. For most cases (such as
+ * is possibly slower than {@link #putDouble(int, double)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putDouble(int, double)} is the preferable choice.
@@ -1103,7 +1129,7 @@ public abstract class MemorySegment {
/**
* Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position in big endian
* byte order. This method's speed depends on the system's native byte order, and it
- * is possibly slower than {@link #putDouble(int, double)}. For most cases (such as
+ * is possibly slower than {@link #putDouble(int, double)}. For most cases (such as
* transient storage in memory or serialization for I/O and network),
* it suffices to know that the byte order in which the value is written is the same as the
* one in which it is read, and {@link #putDouble(int, double)} is the preferable choice.
@@ -1130,7 +1156,7 @@ public abstract class MemorySegment {
*
* @param in The DataInput to get the data from.
* @param offset The position in the memory segment to copy the chunk to.
- * @param length The number of bytes to get.
+ * @param length The number of bytes to get.
*
* @throws IOException Thrown, if the DataInput encountered a problem upon reading,
* such as an End-Of-File.
@@ -1193,9 +1219,8 @@ public abstract class MemorySegment {
final long thisPointer = this.address + offset;
final long otherPointer = target.address + targetOffset;
- if ( (numBytes | offset | targetOffset) >= 0 &&
- thisPointer <= this.addressLimit - numBytes && otherPointer <= target.addressLimit - numBytes)
- {
+ if ((numBytes | offset | targetOffset) >= 0 &&
+ thisPointer <= this.addressLimit - numBytes && otherPointer <= target.addressLimit - numBytes) {
UNSAFE.copyMemory(thisHeapRef, thisPointer, otherHeapRef, otherPointer, numBytes);
}
else if (this.address > this.addressLimit) {
@@ -1262,17 +1287,17 @@ public abstract class MemorySegment {
* @param len Length of the swapped memory region
*/
public final void swapBytes(byte[] tempBuffer, MemorySegment seg2, int offset1, int offset2, int len) {
- if ( (offset1 | offset2 | len | (tempBuffer.length - len) ) >= 0) {
+ if ((offset1 | offset2 | len | (tempBuffer.length - len)) >= 0) {
final long thisPos = this.address + offset1;
final long otherPos = seg2.address + offset2;
-
+
if (thisPos <= this.addressLimit - len && otherPos <= seg2.addressLimit - len) {
// this -> temp buffer
UNSAFE.copyMemory(this.heapMemory, thisPos, tempBuffer, BYTE_ARRAY_BASE_OFFSET, len);
-
+
// other -> this
UNSAFE.copyMemory(seg2.heapMemory, otherPos, this.heapMemory, thisPos, len);
-
+
// temp buffer -> other
UNSAFE.copyMemory(tempBuffer, BYTE_ARRAY_BASE_OFFSET, seg2.heapMemory, otherPos, len);
return;
@@ -1284,7 +1309,7 @@ public abstract class MemorySegment {
throw new IllegalStateException("other memory segment has been freed.");
}
}
-
+
// index is in fact invalid
throw new IndexOutOfBoundsException(
String.format("offset1=%d, offset2=%d, len=%d, bufferSize=%d, address1=%d, address2=%d",
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentFactory.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentFactory.java b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentFactory.java
index 1e5c3ad..5411601 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentFactory.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentFactory.java
@@ -29,14 +29,16 @@ import static org.apache.flink.util.Preconditions.checkNotNull;
* for heap data are of the same type. That way, the runtime does not mix the various specializations
* of the {@link org.apache.flink.core.memory.MemorySegment}. Not mixing them has shown to be beneficial
* to method specialization by the JIT and to overall performance.
- *
+ *
* <p>Note that this factory auto-initialized to use {@link org.apache.flink.core.memory.HeapMemorySegment},
* if a request to create a segment comes before the initialization.
*/
@Internal
public class MemorySegmentFactory {
- /** The factory to use */
+ /**
+ * The factory to use.
+ */
private static volatile Factory factory;
/**
@@ -54,8 +56,8 @@ public class MemorySegmentFactory {
/**
* Allocates some unpooled memory and creates a new memory segment that represents
* that memory.
- * <p>
- * This method is similar to {@link #allocateUnpooledSegment(int, Object)}, but the
+ *
+ * <p>This method is similar to {@link #allocateUnpooledSegment(int, Object)}, but the
* memory segment will have null as the owner.
*
* @param size The size of the memory segment to allocate.
@@ -68,10 +70,10 @@ public class MemorySegmentFactory {
/**
* Allocates some unpooled memory and creates a new memory segment that represents
* that memory.
- * <p>
- * This method is similar to {@link #allocateUnpooledSegment(int)}, but additionally sets
+ *
+ * <p>This method is similar to {@link #allocateUnpooledSegment(int)}, but additionally sets
* the owner of the memory segment.
- *
+ *
* @param size The size of the memory segment to allocate.
* @param owner The owner to associate with the memory segment.
* @return A new memory segment, backed by unpooled heap memory.
@@ -83,11 +85,10 @@ public class MemorySegmentFactory {
/**
* Creates a memory segment that wraps the given byte array.
- * <p>
- * This method is intended to be used for components which pool memory and create
+ *
+ * <p>This method is intended to be used for components which pool memory and create
* memory segments around long-lived memory regions.
*
- *
* @param memory The heap memory to be represented by the memory segment.
* @param owner The owner to associate with the memory segment.
* @return A new memory segment representing the given heap memory.
@@ -99,9 +100,9 @@ public class MemorySegmentFactory {
/**
* Creates a memory segment that wraps the off-heap memory backing the given ByteBuffer.
- * Note that the ByteBuffer needs to be a <i>direct ByteBuffer</i>.
- * <p>
- * This method is intended to be used for components which pool memory and create
+ * Note that the ByteBuffer needs to be a <i>direct ByteBuffer</i>.
+ *
+ * <p>This method is intended to be used for components which pool memory and create
* memory segments around long-lived memory regions.
*
* @param memory The byte buffer with the off-heap memory to be represented by the memory segment.
@@ -140,7 +141,7 @@ public class MemorySegmentFactory {
/**
* Checks whether this memory segment factory has been initialized (with a type to produce).
- *
+ *
* @return True, if the factory has been initialized, false otherwise.
*/
public static boolean isInitialized() {
@@ -149,7 +150,7 @@ public class MemorySegmentFactory {
/**
* Gets the factory. May return null, if the factory has not been initialized.
- *
+ *
* @return The factory, or null, if the factory has not been initialized.
*/
public static Factory getFactory() {
@@ -157,7 +158,7 @@ public class MemorySegmentFactory {
}
/**
- * Sets the factory to the {@link HeapMemorySegment#FACTORY} is no factory has been initialized, yet.
+ * Sets the factory to the {@link HeapMemorySegment#FACTORY} if no factory has been initialized, yet.
*/
private static void ensureInitialized() {
if (factory == null) {
@@ -172,7 +173,7 @@ public class MemorySegmentFactory {
// ------------------------------------------------------------------------
// Internal factory
// ------------------------------------------------------------------------
-
+
/**
* A concrete factory for memory segments.
*/
@@ -198,8 +199,8 @@ public class MemorySegmentFactory {
/**
* Creates a memory segment that wraps the given byte array.
- * <p>
- * This method is intended to be used for components which pool memory and create
+ *
+ * <p>This method is intended to be used for components which pool memory and create
* memory segments around long-lived memory regions.
*
*
@@ -211,9 +212,9 @@ public class MemorySegmentFactory {
/**
* Creates a memory segment that wraps the off-heap memory backing the given ByteBuffer.
- * Note that the ByteBuffer needs to be a <i>direct ByteBuffer</i>.
- * <p>
- * This method is intended to be used for components which pool memory and create
+ * Note that the ByteBuffer needs to be a <i>direct ByteBuffer</i>.
+ *
+ * <p>This method is intended to be used for components which pool memory and create
* memory segments around long-lived memory regions.
*
* @param memory The byte buffer with the off-heap memory to be represented by the memory segment.
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentSource.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentSource.java b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentSource.java
index 8ae8299..2091b89 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentSource.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentSource.java
@@ -25,11 +25,11 @@ import org.apache.flink.annotation.Internal;
*/
@Internal
public interface MemorySegmentSource {
-
+
/**
* Gets the next memory segment. If no more segments are available, it returns null.
- *
+ *
* @return The next memory segment, or null, if none is available.
*/
- public MemorySegment nextSegment();
+ MemorySegment nextSegment();
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/MemoryUtils.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/MemoryUtils.java b/flink-core/src/main/java/org/apache/flink/core/memory/MemoryUtils.java
index b3a04cb..1cb507e 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/MemoryUtils.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/MemoryUtils.java
@@ -28,11 +28,11 @@ import java.nio.ByteOrder;
*/
@Internal
public class MemoryUtils {
-
+
/** The "unsafe", which can be used to perform native memory accesses. */
@SuppressWarnings("restriction")
public static final sun.misc.Unsafe UNSAFE = getUnsafe();
-
+
/** The native byte order of the platform on which the system currently runs. */
public static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder();
@@ -55,6 +55,6 @@ public class MemoryUtils {
}
}
- /** Should not be instantiated */
+ /** Should not be instantiated. */
private MemoryUtils() {}
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataInputView.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataInputView.java b/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataInputView.java
index 2c048de..9dab520 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataInputView.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataInputView.java
@@ -16,7 +16,6 @@
* limitations under the License.
*/
-
package org.apache.flink.core.memory;
import org.apache.flink.annotation.PublicEvolving;
@@ -27,11 +26,11 @@ import org.apache.flink.annotation.PublicEvolving;
*/
@PublicEvolving
public interface SeekableDataInputView extends DataInputView {
-
+
/**
* Sets the read pointer to the given position.
- *
+ *
* @param position The new read position.
*/
- public void setReadPosition(long position);
+ void setReadPosition(long position);
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataOutputView.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataOutputView.java b/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataOutputView.java
index 967cefe..65b71db 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataOutputView.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/SeekableDataOutputView.java
@@ -16,7 +16,6 @@
* limitations under the License.
*/
-
package org.apache.flink.core.memory;
import org.apache.flink.annotation.PublicEvolving;
@@ -27,11 +26,11 @@ import org.apache.flink.annotation.PublicEvolving;
*/
@PublicEvolving
public interface SeekableDataOutputView extends DataOutputView {
-
+
/**
* Sets the write pointer to the given position.
- *
+ *
* @param position The new write position.
*/
- public void setWritePosition(long position);
+ void setWritePosition(long position);
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPosTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPosTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPosTest.java
index 1e1902e..4797c0a 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPosTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPosTest.java
@@ -24,12 +24,15 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+/**
+ * Tests for {@link ByteArrayInputStreamWithPos}.
+ */
public class ByteArrayInputStreamWithPosTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
- private byte[] data = new byte[] {'0','1','2','3','4','5','6','7','8','9'};
+ private byte[] data = new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
private ByteArrayInputStreamWithPos stream;
@@ -39,7 +42,7 @@ public class ByteArrayInputStreamWithPosTest {
}
/**
- * Test setting position on a {@link ByteArrayInputStreamWithPos}
+ * Test setting position on a {@link ByteArrayInputStreamWithPos}.
*/
@Test
public void testSetPosition() throws Exception {
@@ -60,7 +63,7 @@ public class ByteArrayInputStreamWithPosTest {
}
/**
- * Test that the expected position exceeds the capacity of the byte array
+ * Test that the expected position exceeds the capacity of the byte array.
*/
@Test
public void testSetTooLargePosition() throws Exception {
@@ -70,7 +73,7 @@ public class ByteArrayInputStreamWithPosTest {
}
/**
- * Test setting a negative position
+ * Test setting a negative position.
*/
@Test
public void testSetNegativePosition() throws Exception {
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java
index 3c2e361..b076d08 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java
@@ -19,6 +19,7 @@
package org.apache.flink.core.memory;
import org.apache.flink.configuration.ConfigConstants;
+
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
@@ -28,6 +29,9 @@ import org.junit.rules.ExpectedException;
import java.io.IOException;
import java.util.Arrays;
+/**
+ * Tests for {@link ByteArrayOutputStreamWithPos}.
+ */
public class ByteArrayOutputStreamWithPosTest {
private static final int BUFFER_SIZE = 32;
@@ -43,7 +47,7 @@ public class ByteArrayOutputStreamWithPosTest {
}
/**
- * Test setting position which is exactly the same with the buffer size
+ * Test setting position which is exactly the same with the buffer size.
*/
@Test
public void testSetPositionWhenBufferIsFull() throws Exception {
@@ -62,7 +66,7 @@ public class ByteArrayOutputStreamWithPosTest {
}
/**
- * Test setting negative position
+ * Test setting negative position.
*/
@Test
public void testSetNegativePosition() throws Exception {
@@ -74,7 +78,7 @@ public class ByteArrayOutputStreamWithPosTest {
}
/**
- * Test setting position larger than buffer size
+ * Test setting position larger than buffer size.
*/
@Test
public void testSetPositionLargerThanBufferSize() throws Exception {
@@ -89,7 +93,7 @@ public class ByteArrayOutputStreamWithPosTest {
}
/**
- * Test that toString returns a substring of the buffer with range(0, position)
+ * Test that toString returns a substring of the buffer with range(0, position).
*/
@Test
public void testToString() throws IOException {
@@ -100,7 +104,7 @@ public class ByteArrayOutputStreamWithPosTest {
stream.write(data);
Assert.assertArrayEquals(data, stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));
- for (int i = 0 ; i < data.length ; i++) {
+ for (int i = 0; i < data.length; i++) {
stream.setPosition(i);
Assert.assertArrayEquals(Arrays.copyOf(data, i), stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/CrossSegmentTypeTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/CrossSegmentTypeTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/CrossSegmentTypeTest.java
index 0e3fed5..ea144c7 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/CrossSegmentTypeTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/CrossSegmentTypeTest.java
@@ -29,12 +29,16 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+/**
+ * Verifies interoperability between {@link HeapMemorySegment} and {@link HybridMemorySegment} (in
+ * both heap and off-heap modes).
+ */
public class CrossSegmentTypeTest {
- private final int pageSize = 32*1024;
-
+ private final int pageSize = 32 * 1024;
+
// ------------------------------------------------------------------------
-
+
@Test
public void testCompareBytesMixedSegments() {
MemorySegment[] segs1 = {
@@ -57,11 +61,11 @@ public class CrossSegmentTypeTest {
}
}
}
-
+
private void testCompare(MemorySegment seg1, MemorySegment seg2, Random random) {
assertEquals(pageSize, seg1.size());
assertEquals(pageSize, seg2.size());
-
+
final byte[] bytes1 = new byte[pageSize];
final byte[] bytes2 = new byte[pageSize];
@@ -76,7 +80,7 @@ public class CrossSegmentTypeTest {
bytes2[i + shift] = val;
}
}
-
+
seg1.put(0, bytes1);
seg2.put(0, bytes2);
@@ -85,7 +89,7 @@ public class CrossSegmentTypeTest {
int pos2 = random.nextInt(bytes2.length);
int len = Math.min(Math.min(bytes1.length - pos1, bytes2.length - pos2),
- random.nextInt(pageSize / 50 ));
+ random.nextInt(pageSize / 50));
int cmp = seg1.compare(seg2, pos1, pos2, len);
@@ -98,10 +102,9 @@ public class CrossSegmentTypeTest {
}
}
-
@Test
public void testSwapBytesMixedSegments() {
- final int HALF_SIZE = pageSize / 2;
+ final int halfPageSize = pageSize / 2;
MemorySegment[] segs1 = {
new HeapMemorySegment(new byte[pageSize]),
@@ -110,20 +113,20 @@ public class CrossSegmentTypeTest {
};
MemorySegment[] segs2 = {
- new HeapMemorySegment(new byte[HALF_SIZE]),
- new HybridMemorySegment(new byte[HALF_SIZE]),
- new HybridMemorySegment(ByteBuffer.allocateDirect(HALF_SIZE))
+ new HeapMemorySegment(new byte[halfPageSize]),
+ new HybridMemorySegment(new byte[halfPageSize]),
+ new HybridMemorySegment(ByteBuffer.allocateDirect(halfPageSize))
};
Random rnd = new Random();
for (MemorySegment seg1 : segs1) {
for (MemorySegment seg2 : segs2) {
- testSwap(seg1, seg2, rnd, HALF_SIZE);
+ testSwap(seg1, seg2, rnd, halfPageSize);
}
}
}
-
+
private void testSwap(MemorySegment seg1, MemorySegment seg2, Random random, int smallerSize) {
assertEquals(pageSize, seg1.size());
assertEquals(smallerSize, seg2.size());
@@ -132,7 +135,7 @@ public class CrossSegmentTypeTest {
final byte[] bytes2 = new byte[smallerSize];
Arrays.fill(bytes2, (byte) 1);
-
+
seg1.put(0, bytes1);
seg2.put(0, bytes2);
@@ -184,29 +187,29 @@ public class CrossSegmentTypeTest {
byte[] expected = new byte[pageSize];
byte[] actual = new byte[pageSize];
-
+
// zero out the memory
seg1.put(0, expected);
seg2.put(0, expected);
-
+
for (int i = 0; i < 40; i++) {
int numBytes = random.nextInt(pageSize / 20);
byte[] bytes = new byte[numBytes];
random.nextBytes(bytes);
-
+
int thisPos = random.nextInt(pageSize - numBytes);
int otherPos = random.nextInt(pageSize - numBytes);
-
+
// track what we expect
System.arraycopy(bytes, 0, expected, otherPos, numBytes);
-
+
seg1.put(thisPos, bytes);
seg1.copyTo(thisPos, seg2, otherPos, numBytes);
}
-
+
seg2.get(0, actual);
assertArrayEquals(expected, actual);
-
+
// test out of bound conditions
final int[] validOffsets = { 0, 1, pageSize / 10 * 9 };
@@ -229,7 +232,7 @@ public class CrossSegmentTypeTest {
fail("should fail with an IndexOutOfBoundsException");
}
catch (IndexOutOfBoundsException ignored) {}
-
+
try {
seg2.copyTo(off1, seg1, off2, len);
fail("should fail with an IndexOutOfBoundsException");
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/EndiannessAccessChecks.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/EndiannessAccessChecks.java b/flink-core/src/test/java/org/apache/flink/core/memory/EndiannessAccessChecks.java
index c06562a..c04d435 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/EndiannessAccessChecks.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/EndiannessAccessChecks.java
@@ -18,14 +18,19 @@
package org.apache.flink.core.memory;
+import org.junit.Test;
+
import java.nio.ByteBuffer;
import java.util.Random;
-import org.junit.Test;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+/**
+ * Verifies correct accesses with regards to endianness in {@link HeapMemorySegment} and {@link
+ * HybridMemorySegment} (in both heap and off-heap modes).
+ */
public class EndiannessAccessChecks {
-
+
@Test
public void testHeapSegment() {
testBigAndLittleEndianAccessUnaligned(new HeapMemorySegment(new byte[11111]));
@@ -40,19 +45,19 @@ public class EndiannessAccessChecks {
public void testHybridOffHeapSegment() {
testBigAndLittleEndianAccessUnaligned(new HybridMemorySegment(ByteBuffer.allocateDirect(11111)));
}
-
+
private void testBigAndLittleEndianAccessUnaligned(MemorySegment segment) {
final Random rnd = new Random();
-
+
// longs
{
final long seed = rnd.nextLong();
-
+
rnd.setSeed(seed);
for (int i = 0; i < 10000; i++) {
long val = rnd.nextLong();
int pos = rnd.nextInt(segment.size - 7);
-
+
segment.putLongLittleEndian(pos, val);
long r = segment.getLongBigEndian(pos);
assertEquals(val, Long.reverseBytes(r));
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/HeapMemorySegmentTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/HeapMemorySegmentTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/HeapMemorySegmentTest.java
index b2c16d9..90176fd 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/HeapMemorySegmentTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/HeapMemorySegmentTest.java
@@ -24,8 +24,13 @@ import org.junit.runners.Parameterized;
import java.nio.ByteBuffer;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+/**
+ * Tests for the {@link HeapMemorySegment} in off-heap mode.
+ */
@RunWith(Parameterized.class)
public class HeapMemorySegmentTest extends MemorySegmentTestBase {
@@ -42,7 +47,7 @@ public class HeapMemorySegmentTest extends MemorySegmentTestBase {
MemorySegment createSegment(int size, Object owner) {
return new HeapMemorySegment(new byte[size], owner);
}
-
+
@Test
public void testHeapSegmentSpecifics() {
final byte[] buffer = new byte[411];
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/HybridOffHeapMemorySegmentTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/HybridOffHeapMemorySegmentTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/HybridOffHeapMemorySegmentTest.java
index cf2d023..254254c 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/HybridOffHeapMemorySegmentTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/HybridOffHeapMemorySegmentTest.java
@@ -29,6 +29,9 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+/**
+ * Tests for the {@link HybridMemorySegment} in off-heap mode.
+ */
@RunWith(Parameterized.class)
public class HybridOffHeapMemorySegmentTest extends MemorySegmentTestBase {
@@ -57,6 +60,7 @@ public class HybridOffHeapMemorySegmentTest extends MemorySegmentTestBase {
assertTrue(buffer == seg.getOffHeapBuffer());
try {
+ //noinspection ResultOfMethodCallIgnored
seg.getArray();
fail("should throw an exception");
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/HybridOnHeapMemorySegmentTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/HybridOnHeapMemorySegmentTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/HybridOnHeapMemorySegmentTest.java
index 711326f..6672744 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/HybridOnHeapMemorySegmentTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/HybridOnHeapMemorySegmentTest.java
@@ -29,6 +29,9 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+/**
+ * Tests for the {@link HybridMemorySegment} in on-heap mode.
+ */
@RunWith(Parameterized.class)
public class HybridOnHeapMemorySegmentTest extends MemorySegmentTestBase {
@@ -45,7 +48,7 @@ public class HybridOnHeapMemorySegmentTest extends MemorySegmentTestBase {
MemorySegment createSegment(int size, Object owner) {
return new HybridMemorySegment(new byte[size], owner);
}
-
+
@Test
public void testHybridHeapSegmentSpecifics() {
final byte[] buffer = new byte[411];
@@ -57,6 +60,7 @@ public class HybridOnHeapMemorySegmentTest extends MemorySegmentTestBase {
assertTrue(buffer == seg.getArray());
try {
+ //noinspection ResultOfMethodCallIgnored
seg.getOffHeapBuffer();
fail("should throw an exception");
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentChecksTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentChecksTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentChecksTest.java
index f50322c..ccf3a4b 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentChecksTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentChecksTest.java
@@ -28,7 +28,7 @@ import java.nio.ByteBuffer;
* Tests for the sanity checks of the memory segments.
*/
public class MemorySegmentChecksTest {
-
+
@Test(expected = NullPointerException.class)
public void testHeapNullBuffer1() {
new HeapMemorySegment(null);
@@ -66,21 +66,21 @@ public class MemorySegmentChecksTest {
@Test(expected = IllegalArgumentException.class)
public void testZeroAddress(){
- new MockSegment(0L, 4*1024, null);
+ new MockSegment(0L, 4 * 1024, null);
}
@Test(expected = IllegalArgumentException.class)
public void testNegativeAddress(){
- new MockSegment(-1L, 4*1024, null);
+ new MockSegment(-1L, 4 * 1024, null);
}
@Test(expected = IllegalArgumentException.class)
public void testTooLargeAddress(){
- new MockSegment(Long.MAX_VALUE - 8*1024, 4*1024, null);
+ new MockSegment(Long.MAX_VALUE - 8 * 1024, 4 * 1024, null);
}
-
+
// ------------------------------------------------------------------------
-
+
final class MockSegment extends MemorySegment {
MockSegment(long offHeapAddress, int size, Object owner) {
@@ -131,5 +131,5 @@ public class MemorySegmentChecksTest {
@Override
public void put(int offset, ByteBuffer source, int numBytes) {}
- };
+ }
}
http://git-wip-us.apache.org/repos/asf/flink/blob/c3235c39/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentFactoryTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentFactoryTest.java b/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentFactoryTest.java
index 84105cf..3fd2cbe 100644
--- a/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentFactoryTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentFactoryTest.java
@@ -22,8 +22,13 @@ import org.junit.Test;
import java.lang.reflect.Field;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+/**
+ * Tests for the {@link MemorySegmentFactory} in on/off-heap modes.
+ */
public class MemorySegmentFactoryTest {
@Test