jdk/src/share/classes/sun/nio/ch/Util.java
author alanb
Tue, 15 Mar 2011 13:15:31 +0000
changeset 8788 b98f18278dc4
parent 7668 d4a77089c587
child 10137 d92637d3d673
permissions -rw-r--r--
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7025
diff changeset
     2
 * Copyright (c) 2000, 2010, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
    31
import java.io.FileDescriptor;
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.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.Cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // -- Caches --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // The number of temp buffers in our pool
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    48
    private static final int TEMP_BUF_POOL_SIZE = 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    50
    // Per-thread cache of temporary direct buffers
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    51
    private static ThreadLocal<BufferCache> bufferCache =
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    52
        new ThreadLocal<BufferCache>()
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    53
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    54
        @Override
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    55
        protected BufferCache initialValue() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    56
            return new BufferCache();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    57
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    58
    };
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    59
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    60
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    61
     * A simple cache of direct buffers.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    62
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    63
    private static class BufferCache {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    64
        // the array of buffers
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    65
        private ByteBuffer[] buffers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    67
        // the number of buffers in the cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    68
        private int count;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    69
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    70
        // the index of the first valid buffer (undefined if count == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    71
        private int start;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    72
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    73
        private int next(int i) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    74
            return (i + 1) % TEMP_BUF_POOL_SIZE;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    75
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    76
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    77
        BufferCache() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    78
            buffers = new ByteBuffer[TEMP_BUF_POOL_SIZE];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    79
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    80
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    81
        /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    82
         * 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
    83
         * size (or null if no suitable buffer is found).
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    84
         */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    85
        ByteBuffer get(int size) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    86
            if (count == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    87
                return null;  // cache is empty
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    88
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    89
            ByteBuffer[] buffers = this.buffers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    91
            // search for suitable buffer (often the first buffer will do)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    92
            ByteBuffer buf = buffers[start];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    93
            if (buf.capacity() < size) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    94
                buf = null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    95
                int i = start;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    96
                while ((i = next(i)) != start) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    97
                    ByteBuffer bb = buffers[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    98
                    if (bb == null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    99
                        break;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   100
                    if (bb.capacity() >= size) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   101
                        buf = bb;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   102
                        break;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   103
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   104
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   105
                if (buf == null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   106
                    return null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   107
                // move first element to here to avoid re-packing
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   108
                buffers[i] = buffers[start];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   109
            }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   110
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   111
            // remove first element
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   112
            buffers[start] = null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   113
            start = next(start);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   114
            count--;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   115
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   116
            // prepare the buffer and return it
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   117
            buf.rewind();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   118
            buf.limit(size);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   119
            return buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   120
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   121
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   122
        boolean offerFirst(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   123
            if (count >= TEMP_BUF_POOL_SIZE) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   124
                return false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   125
            } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   126
                start = (start + TEMP_BUF_POOL_SIZE - 1) % TEMP_BUF_POOL_SIZE;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   127
                buffers[start] = buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   128
                count++;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   129
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   133
        boolean offerLast(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   134
            if (count >= TEMP_BUF_POOL_SIZE) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   135
                return false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   136
            } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   137
                int next = (start + count) % TEMP_BUF_POOL_SIZE;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   138
                buffers[next] = buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   139
                count++;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   140
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 895
diff changeset
   143
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   144
        boolean isEmpty() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   145
            return count == 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   146
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   147
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   148
        ByteBuffer removeFirst() {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   149
            assert count > 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   150
            ByteBuffer buf = buffers[start];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   151
            buffers[start] = null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   152
            start = next(start);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   153
            count--;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   154
            return buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   155
        }
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
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   159
     * Returns a temporary buffer of at least the given size
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   160
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   161
    static ByteBuffer getTemporaryDirectBuffer(int size) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   162
        BufferCache cache = bufferCache.get();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   163
        ByteBuffer buf = cache.get(size);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   164
        if (buf != null) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   165
            return buf;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   166
        } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   167
            // 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
   168
            // one. To avoid the cache growing then we remove the first
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   169
            // buffer from the cache and free it.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   170
            if (!cache.isEmpty()) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
                buf = cache.removeFirst();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   172
                free(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
            return ByteBuffer.allocateDirect(size);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   175
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   176
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   177
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   178
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   179
     * Releases a temporary buffer by returning to the cache or freeing it.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   180
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   181
    static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   182
        offerFirstTemporaryDirectBuffer(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   183
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   184
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   185
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   186
     * 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
   187
     * 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
   188
     * likely to be returned by a subsequent call to getTemporaryDirectBuffer.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   189
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   190
    static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   191
        assert buf != null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   192
        BufferCache cache = bufferCache.get();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   193
        if (!cache.offerFirst(buf)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   194
            // cache is full
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   195
            free(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   196
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   197
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   198
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   199
    /**
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   200
     * 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
   201
     * 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
   202
     * suitable for scatter/gather operations where the buffers are returned to
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   203
     * cache in same order that they were obtained.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   204
     */
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   205
    static void offerLastTemporaryDirectBuffer(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   206
        assert buf != null;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   207
        BufferCache cache = bufferCache.get();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   208
        if (!cache.offerLast(buf)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   209
            // cache is full
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   210
            free(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   211
        }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   212
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   213
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
     * Frees the memory for the given direct buffer
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
    private static void free(ByteBuffer buf) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   218
        ((DirectBuffer)buf).cleaner().clean();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static class SelectorWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        private SelectorWrapper (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Cleaner.create(this, new Closer(sel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private static class Closer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            private Closer (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    sel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                } catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    throw new Error(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        public Selector get() { return sel;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // Per-thread cached selector
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   244
    private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   245
        = new ThreadLocal<SoftReference<SelectorWrapper>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // Hold a reference to the selWrapper object to prevent it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    // being cleaned when the temporary selector wrapped is on lease.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   248
    private static ThreadLocal<SelectorWrapper> localSelectorWrapper
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   249
        = new ThreadLocal<SelectorWrapper>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // When finished, invoker must ensure that selector is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    // by cancelling any related keys and explicitly releasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    // the selector by invoking releaseTemporarySelector()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    static Selector getTemporarySelector(SelectableChannel sc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   257
        SoftReference<SelectorWrapper> ref = localSelector.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        SelectorWrapper selWrapper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (ref == null
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   261
            || ((selWrapper = ref.get()) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            || ((sel = selWrapper.get()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            || (sel.provider() != sc.provider())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            sel = sc.provider().openSelector();
787
637b139ce684 6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents: 51
diff changeset
   265
            selWrapper = new SelectorWrapper(sel);
637b139ce684 6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents: 51
diff changeset
   266
            localSelector.set(new SoftReference<SelectorWrapper>(selWrapper));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
787
637b139ce684 6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents: 51
diff changeset
   268
        localSelectorWrapper.set(selWrapper);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static void releaseTemporarySelector(Selector sel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Selector should be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        sel.selectNow();                // Flush cancelled keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        assert sel.keys().isEmpty() : "Temporary selector not empty";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        localSelectorWrapper.set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    // -- Random stuff --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if ((offset == 0) && (length == bs.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int n = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        ByteBuffer[] bs2 = new ByteBuffer[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            bs2[i] = bs[offset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return bs2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    static <E> Set<E> ungrowableSet(final Set<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return new Set<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                public int size()                 { return s.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                public boolean isEmpty()          { return s.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                public boolean contains(Object o) { return s.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                public Object[] toArray()         { return s.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                public <T> T[] toArray(T[] a)     { return s.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                public String toString()          { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                public Iterator<E> iterator()     { return s.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                public boolean equals(Object o)   { return s.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                public int hashCode()             { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                public void clear()               { s.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                public boolean remove(Object o)   { return s.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    return s.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    return s.removeAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    return s.retainAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                public boolean add(E o){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    // -- Unsafe access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private static byte _get(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return unsafe.getByte(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private static void _put(long a, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        unsafe.putByte(a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static void erase(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static Unsafe unsafe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private static int pageSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    static int pageSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (pageSize == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            pageSize = unsafe().pageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private static volatile Constructor directByteBufferConstructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private static void initDBBConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   361
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   362
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   364
                        Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        Constructor ctor = cl.getDeclaredConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                          long.class,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   368
                                          FileDescriptor.class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                          Runnable.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        directByteBufferConstructor = ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    } catch (ClassCastException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    static MappedByteBuffer newMappedByteBuffer(int size, long addr,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   386
                                                FileDescriptor fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                                Runnable unmapper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (directByteBufferConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            initDBBConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
              new Object[] { new Integer(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                             new Long(addr),
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   396
                             fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                             unmapper });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private static volatile Constructor directByteBufferRConstructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private static void initDBBRConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   411
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   412
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   414
                        Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        Constructor ctor = cl.getDeclaredConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                          long.class,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   418
                                          FileDescriptor.class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                          Runnable.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        directByteBufferRConstructor = ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    } catch (ClassCastException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    static MappedByteBuffer newMappedByteBufferR(int size, long addr,
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   436
                                                 FileDescriptor fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                                 Runnable unmapper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (directByteBufferRConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            initDBBRConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
              new Object[] { new Integer(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                             new Long(addr),
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6301
diff changeset
   446
                             fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                             unmapper });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    // -- Bug compatibility --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private static volatile String bugLevel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    static boolean atBugLevel(String bl) {              // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (bugLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (!sun.misc.VM.isBooted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            String value = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                new GetPropertyAction("sun.nio.ch.bugLevel"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            bugLevel = (value != null) ? value : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return bugLevel.equals(bl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    // -- Initialization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private static boolean loaded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    static void load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        synchronized (Util.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            loaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                .doPrivileged(new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                .doPrivileged(new sun.security.action.LoadLibraryAction("nio"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // IOUtil must be initialized; Its native methods are called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // other places in native nio code so they must be set up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            IOUtil.initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
}