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