src/java.base/share/classes/sun/nio/ch/Util.java
author michaelm
Fri, 25 Oct 2019 15:56:35 +0100
branchunixdomainchannels
changeset 58801 119ac9128c1b
parent 57804 9b7b9f16dfd9
child 58848 c3df0f8b6d93
permissions -rw-r--r--
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
    28
import java.io.FileDescriptor;
50719
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    29
import java.io.IOException;
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 37781
diff changeset
    30
import java.lang.reflect.Constructor;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 37781
diff changeset
    31
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.MappedByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedAction;
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 37781
diff changeset
    36
import java.util.Collection;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 37781
diff changeset
    37
import java.util.Iterator;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 37781
diff changeset
    38
import java.util.Set;
50719
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    39
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    40
import jdk.internal.misc.TerminatingThreadLocal;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 25859
diff changeset
    41
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10882
diff changeset
    44
public class Util {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // -- Caches --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // The number of temp buffers in our pool
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12559
diff changeset
    49
    private static final int TEMP_BUF_POOL_SIZE = IOUtil.IOV_MAX;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    51
    // The max size allowed for a cached temp buffer, in bytes
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    52
    private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    53
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    54
    // Per-thread cache of temporary direct buffers
50719
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    55
    private static ThreadLocal<BufferCache> bufferCache = new TerminatingThreadLocal<>() {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    56
        @Override
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    57
        protected BufferCache initialValue() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    58
            return new BufferCache();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    59
        }
50719
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    60
        @Override
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    61
        protected void threadTerminated(BufferCache cache) { // will never be null
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    62
            while (!cache.isEmpty()) {
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    63
                ByteBuffer bb = cache.removeFirst();
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    64
                free(bb);
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    65
            }
106dc156ce6b 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
plevart
parents: 47428
diff changeset
    66
        }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    67
    };
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    68
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    69
    /**
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    70
     * Returns the max size allowed for a cached temp buffers, in
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    71
     * bytes. It defaults to Long.MAX_VALUE. It can be set with the
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    72
     * jdk.nio.maxCachedBufferSize property. Even though
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    73
     * ByteBuffer.capacity() returns an int, we're using a long here
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    74
     * for potential future-proofing.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    75
     */
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    76
    private static long getMaxCachedBufferSize() {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    77
        String s = GetPropertyAction
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    78
                .privilegedGetProperty("jdk.nio.maxCachedBufferSize");
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    79
        if (s != null) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    80
            try {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    81
                long m = Long.parseLong(s);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    82
                if (m >= 0) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    83
                    return m;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    84
                } else {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    85
                    // if it's negative, ignore the system property
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    86
                }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    87
            } catch (NumberFormatException e) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    88
                // if the string is not well formed, ignore the system property
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    89
            }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    90
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    91
        return Long.MAX_VALUE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    92
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    93
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    94
    /**
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    95
     * Returns true if a buffer of this size is too large to be
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    96
     * added to the buffer cache, false otherwise.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    97
     */
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    98
    private static boolean isBufferTooLarge(int size) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
    99
        return size > MAX_CACHED_BUFFER_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   100
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   101
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   102
    /**
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   103
     * Returns true if the buffer is too large to be added to the
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   104
     * buffer cache, false otherwise.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   105
     */
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   106
    private static boolean isBufferTooLarge(ByteBuffer buf) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   107
        return isBufferTooLarge(buf.capacity());
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   108
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   109
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   110
    /**
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   111
     * A simple cache of direct buffers.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   112
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   113
    private static class BufferCache {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   114
        // the array of buffers
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   115
        private ByteBuffer[] buffers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   117
        // the number of buffers in the cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   118
        private int count;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   119
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   120
        // the index of the first valid buffer (undefined if count == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   121
        private int start;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   122
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   123
        private int next(int i) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   124
            return (i + 1) % TEMP_BUF_POOL_SIZE;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   125
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   126
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   127
        BufferCache() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   128
            buffers = new ByteBuffer[TEMP_BUF_POOL_SIZE];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   129
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   130
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   131
        /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   132
         * Removes and returns a buffer from the cache of at least the given
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   133
         * size (or null if no suitable buffer is found).
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   134
         */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   135
        ByteBuffer get(int size) {
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   136
            // Don't call this if the buffer would be too large.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   137
            assert !isBufferTooLarge(size);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   138
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   139
            if (count == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   140
                return null;  // cache is empty
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
   141
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   142
            ByteBuffer[] buffers = this.buffers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   144
            // search for suitable buffer (often the first buffer will do)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   145
            ByteBuffer buf = buffers[start];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   146
            if (buf.capacity() < size) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   147
                buf = null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   148
                int i = start;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   149
                while ((i = next(i)) != start) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   150
                    ByteBuffer bb = buffers[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   151
                    if (bb == null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   152
                        break;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   153
                    if (bb.capacity() >= size) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   154
                        buf = bb;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   155
                        break;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   156
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   157
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   158
                if (buf == null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   159
                    return null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   160
                // move first element to here to avoid re-packing
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   161
                buffers[i] = buffers[start];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   162
            }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   163
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   164
            // remove first element
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   165
            buffers[start] = null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   166
            start = next(start);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   167
            count--;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   168
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   169
            // prepare the buffer and return it
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   170
            buf.rewind();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
            buf.limit(size);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   172
            return buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   173
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   174
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   175
        boolean offerFirst(ByteBuffer buf) {
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   176
            // Don't call this if the buffer is too large.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   177
            assert !isBufferTooLarge(buf);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   178
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   179
            if (count >= TEMP_BUF_POOL_SIZE) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   180
                return false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   181
            } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   182
                start = (start + TEMP_BUF_POOL_SIZE - 1) % TEMP_BUF_POOL_SIZE;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   183
                buffers[start] = buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   184
                count++;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   185
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   189
        boolean offerLast(ByteBuffer buf) {
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   190
            // Don't call this if the buffer is too large.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   191
            assert !isBufferTooLarge(buf);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   192
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   193
            if (count >= TEMP_BUF_POOL_SIZE) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   194
                return false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   195
            } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   196
                int next = (start + count) % TEMP_BUF_POOL_SIZE;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   197
                buffers[next] = buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   198
                count++;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   199
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 895
diff changeset
   202
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   203
        boolean isEmpty() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   204
            return count == 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   205
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   206
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   207
        ByteBuffer removeFirst() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   208
            assert count > 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   209
            ByteBuffer buf = buffers[start];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   210
            buffers[start] = null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   211
            start = next(start);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   212
            count--;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   213
            return buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   214
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   215
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   216
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   217
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   218
     * Returns a temporary buffer of at least the given size
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   219
     */
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10882
diff changeset
   220
    public static ByteBuffer getTemporaryDirectBuffer(int size) {
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   221
        // If a buffer of this size is too large for the cache, there
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   222
        // should not be a buffer in the cache that is at least as
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   223
        // large. So we'll just create a new one. Also, we don't have
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   224
        // to remove the buffer from the cache (as this method does
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   225
        // below) given that we won't put the new buffer in the cache.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   226
        if (isBufferTooLarge(size)) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   227
            return ByteBuffer.allocateDirect(size);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   228
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   229
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   230
        BufferCache cache = bufferCache.get();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   231
        ByteBuffer buf = cache.get(size);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   232
        if (buf != null) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   233
            return buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   234
        } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   235
            // No suitable buffer in the cache so we need to allocate a new
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   236
            // one. To avoid the cache growing then we remove the first
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   237
            // buffer from the cache and free it.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   238
            if (!cache.isEmpty()) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   239
                buf = cache.removeFirst();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   240
                free(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   241
            }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   242
            return ByteBuffer.allocateDirect(size);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   243
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   244
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   245
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   246
    /**
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   247
     * Returns a temporary buffer of at least the given size and
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   248
     * aligned to the alignment
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   249
     */
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   250
    public static ByteBuffer getTemporaryAlignedDirectBuffer(int size,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   251
                                                             int alignment) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   252
        if (isBufferTooLarge(size)) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   253
            return ByteBuffer.allocateDirect(size + alignment - 1)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   254
                    .alignedSlice(alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   255
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   256
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   257
        BufferCache cache = bufferCache.get();
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   258
        ByteBuffer buf = cache.get(size);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   259
        if (buf != null) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   260
            if (buf.alignmentOffset(0, alignment) == 0) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   261
                return buf;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   262
            }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   263
        } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   264
            if (!cache.isEmpty()) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   265
                buf = cache.removeFirst();
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   266
                free(buf);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   267
            }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   268
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   269
        return ByteBuffer.allocateDirect(size + alignment - 1)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   270
                .alignedSlice(alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   271
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   272
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   273
    /**
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   274
     * Releases a temporary buffer by returning to the cache or freeing it.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   275
     */
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10882
diff changeset
   276
    public static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   277
        offerFirstTemporaryDirectBuffer(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   278
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   279
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   280
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   281
     * Releases a temporary buffer by returning to the cache or freeing it. If
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   282
     * returning to the cache then insert it at the start so that it is
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   283
     * likely to be returned by a subsequent call to getTemporaryDirectBuffer.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   284
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 57804
diff changeset
   285
    public static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) {
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   286
        // If the buffer is too large for the cache we don't have to
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   287
        // check the cache. We'll just free it.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   288
        if (isBufferTooLarge(buf)) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   289
            free(buf);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   290
            return;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   291
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   292
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   293
        assert buf != null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   294
        BufferCache cache = bufferCache.get();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   295
        if (!cache.offerFirst(buf)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   296
            // cache is full
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   297
            free(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   298
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   299
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   300
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   301
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   302
     * Releases a temporary buffer by returning to the cache or freeing it. If
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   303
     * returning to the cache then insert it at the end. This makes it
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   304
     * suitable for scatter/gather operations where the buffers are returned to
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   305
     * cache in same order that they were obtained.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   306
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   307
    static void offerLastTemporaryDirectBuffer(ByteBuffer buf) {
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   308
        // If the buffer is too large for the cache we don't have to
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   309
        // check the cache. We'll just free it.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   310
        if (isBufferTooLarge(buf)) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   311
            free(buf);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   312
            return;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   313
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents: 34882
diff changeset
   314
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   315
        assert buf != null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   316
        BufferCache cache = bufferCache.get();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   317
        if (!cache.offerLast(buf)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   318
            // cache is full
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   319
            free(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   320
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   321
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   322
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   323
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   324
     * Frees the memory for the given direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   325
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   326
    private static void free(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   327
        ((DirectBuffer)buf).cleaner().clean();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    // -- Random stuff --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if ((offset == 0) && (length == bs.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int n = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        ByteBuffer[] bs2 = new ByteBuffer[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            bs2[i] = bs[offset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return bs2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static <E> Set<E> ungrowableSet(final Set<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return new Set<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                public int size()                 { return s.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                public boolean isEmpty()          { return s.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                public boolean contains(Object o) { return s.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                public Object[] toArray()         { return s.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                public <T> T[] toArray(T[] a)     { return s.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                public String toString()          { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                public Iterator<E> iterator()     { return s.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                public boolean equals(Object o)   { return s.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                public int hashCode()             { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                public void clear()               { s.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                public boolean remove(Object o)   { return s.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    return s.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    return s.removeAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    return s.retainAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                public boolean add(E o){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    // -- Unsafe access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private static Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private static byte _get(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return unsafe.getByte(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private static void _put(long a, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        unsafe.putByte(a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    static void erase(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    static Unsafe unsafe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private static int pageSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    static int pageSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (pageSize == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            pageSize = unsafe().pageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 33674
diff changeset
   407
    private static volatile Constructor<?> directByteBufferConstructor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private static void initDBBConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   410
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   411
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   413
                        Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   414
                        Constructor<?> ctor = cl.getDeclaredConstructor(
10882
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10419
diff changeset
   415
                            new Class<?>[] { int.class,
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10419
diff changeset
   416
                                             long.class,
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10419
diff changeset
   417
                                             FileDescriptor.class,
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   418
                                             Runnable.class,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   419
                                             boolean.class });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        directByteBufferConstructor = ctor;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   422
                    } catch (ClassNotFoundException   |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   423
                             NoSuchMethodException    |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   424
                             IllegalArgumentException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   425
                             ClassCastException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10137
diff changeset
   426
                        throw new InternalError(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    static MappedByteBuffer newMappedByteBuffer(int size, long addr,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   433
                                                FileDescriptor fd,
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   434
                                                Runnable unmapper,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   435
                                                boolean isSync)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (directByteBufferConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            initDBBConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   442
              new Object[] { size,
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 23010
diff changeset
   443
                             addr,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   444
                             fd,
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   445
                             unmapper,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   446
                             isSync});
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   447
        } catch (InstantiationException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   448
                 IllegalAccessException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   449
                 InvocationTargetException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10137
diff changeset
   450
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 33674
diff changeset
   455
    private static volatile Constructor<?> directByteBufferRConstructor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private static void initDBBRConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   458
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   459
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   461
                        Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   462
                        Constructor<?> ctor = cl.getDeclaredConstructor(
10882
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10419
diff changeset
   463
                            new Class<?>[] { int.class,
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10419
diff changeset
   464
                                             long.class,
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10419
diff changeset
   465
                                             FileDescriptor.class,
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   466
                                             Runnable.class,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   467
                                             boolean.class });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        directByteBufferRConstructor = ctor;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   470
                    } catch (ClassNotFoundException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   471
                             NoSuchMethodException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   472
                             IllegalArgumentException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   473
                             ClassCastException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10137
diff changeset
   474
                        throw new InternalError(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    static MappedByteBuffer newMappedByteBufferR(int size, long addr,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   481
                                                 FileDescriptor fd,
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   482
                                                 Runnable unmapper,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   483
                                                 boolean isSync)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (directByteBufferRConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            initDBBRConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   490
              new Object[] { size,
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 23010
diff changeset
   491
                             addr,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   492
                             fd,
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   493
                             unmapper,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 50719
diff changeset
   494
                             isSync});
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   495
        } catch (InstantiationException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   496
                 IllegalAccessException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   497
                 InvocationTargetException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10137
diff changeset
   498
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   502
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   503
    static void checkBufferPositionAligned(ByteBuffer bb,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   504
                                                     int pos, int alignment)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   505
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   506
    {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   507
        if (bb.alignmentOffset(pos, alignment) != 0) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   508
            throw new IOException("Current location of the bytebuffer ("
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   509
                + pos + ") is not a multiple of the block size ("
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   510
                + alignment + ")");
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   511
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   512
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   513
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   514
    static void checkRemainingBufferSizeAligned(int rem,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   515
                                                          int alignment)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   516
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   517
    {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   518
        if (rem % alignment != 0) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   519
            throw new IOException("Number of remaining bytes ("
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   520
                + rem + ") is not a multiple of the block size ("
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   521
                + alignment + ")");
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   522
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   523
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   524
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   525
    static void checkChannelPositionAligned(long position,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   526
                                                      int alignment)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   527
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   528
    {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   529
        if (position % alignment != 0) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   530
           throw new IOException("Channel position (" + position
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   531
               + ") is not a multiple of the block size ("
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   532
               + alignment + ")");
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   533
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   534
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}