mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8164900: Add support for O_DIRECT
Add support for Direct I/O in FileChannel Co-authored-by: Volker Simonis <volker.simonis@gmail.com> Reviewed-by: alanb, bpb, alanbur, coffeys, aph, clanger, plevart, mli, psandoz, simonis
This commit is contained in:
parent
97db013bd3
commit
ec1c3bce45
30 changed files with 1523 additions and 45 deletions
|
@ -37,7 +37,7 @@ import java.util.Iterator;
|
|||
import java.util.Set;
|
||||
import jdk.internal.misc.Unsafe;
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Util {
|
||||
|
||||
|
@ -236,6 +236,33 @@ public class Util {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a temporary buffer of at least the given size and
|
||||
* aligned to the alignment
|
||||
*/
|
||||
public static ByteBuffer getTemporaryAlignedDirectBuffer(int size,
|
||||
int alignment) {
|
||||
if (isBufferTooLarge(size)) {
|
||||
return ByteBuffer.allocateDirect(size + alignment - 1)
|
||||
.alignedSlice(alignment);
|
||||
}
|
||||
|
||||
BufferCache cache = bufferCache.get();
|
||||
ByteBuffer buf = cache.get(size);
|
||||
if (buf != null) {
|
||||
if (buf.alignmentOffset(0, alignment) == 0) {
|
||||
return buf;
|
||||
}
|
||||
} else {
|
||||
if (!cache.isEmpty()) {
|
||||
buf = cache.removeFirst();
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return ByteBuffer.allocateDirect(size + alignment - 1)
|
||||
.alignedSlice(alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases a temporary buffer by returning to the cache or freeing it.
|
||||
*/
|
||||
|
@ -459,4 +486,37 @@ public class Util {
|
|||
}
|
||||
return dbb;
|
||||
}
|
||||
|
||||
static void checkBufferPositionAligned(ByteBuffer bb,
|
||||
int pos, int alignment)
|
||||
throws IOException
|
||||
{
|
||||
if (bb.alignmentOffset(pos, alignment) != 0) {
|
||||
throw new IOException("Current location of the bytebuffer ("
|
||||
+ pos + ") is not a multiple of the block size ("
|
||||
+ alignment + ")");
|
||||
}
|
||||
}
|
||||
|
||||
static void checkRemainingBufferSizeAligned(int rem,
|
||||
int alignment)
|
||||
throws IOException
|
||||
{
|
||||
if (rem % alignment != 0) {
|
||||
throw new IOException("Number of remaining bytes ("
|
||||
+ rem + ") is not a multiple of the block size ("
|
||||
+ alignment + ")");
|
||||
}
|
||||
}
|
||||
|
||||
static void checkChannelPositionAligned(long position,
|
||||
int alignment)
|
||||
throws IOException
|
||||
{
|
||||
if (position % alignment != 0) {
|
||||
throw new IOException("Channel position (" + position
|
||||
+ ") is not a multiple of the block size ("
|
||||
+ alignment + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue