Direct-X-Buffer.java.template 13.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29
 */

#warn This file is preprocessed before being compiled

package java.nio;

30
import java.io.FileDescriptor;
D
duke 已提交
31 32
import sun.misc.Cleaner;
import sun.misc.Unsafe;
33
import sun.misc.VM;
D
duke 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
import sun.nio.ch.DirectBuffer;


class Direct$Type$Buffer$RW$$BO$
#if[rw]
    extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
#else[rw]
    extends Direct$Type$Buffer$BO$
#end[rw]
    implements DirectBuffer
{

#if[rw]

    // Cached unsafe-access object
    protected static final Unsafe unsafe = Bits.unsafe();

51 52 53
    // Cached array base offset
    private static final long arrayBaseOffset = (long)unsafe.arrayBaseOffset($type$[].class);

D
duke 已提交
54 55 56 57 58 59 60
    // Cached unaligned-access capability
    protected static final boolean unaligned = Bits.unaligned();

    // Base address, used in all indexing calculations
    // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
    //    protected long address;

61 62 63 64
    // An object attached to this buffer. If this buffer is a view of another
    // buffer then we use this field to keep a reference to that buffer to
    // ensure that its memory isn't freed before we are done with it.
    private final Object att;
D
duke 已提交
65

66 67
    public Object attachment() {
        return att;
D
duke 已提交
68 69 70 71 72 73 74 75 76 77 78
    }

#if[byte]

    private static class Deallocator
        implements Runnable
    {

        private static Unsafe unsafe = Unsafe.getUnsafe();

        private long address;
79
        private long size;
D
duke 已提交
80 81
        private int capacity;

82
        private Deallocator(long address, long size, int capacity) {
D
duke 已提交
83 84
            assert (address != 0);
            this.address = address;
85
            this.size = size;
D
duke 已提交
86 87 88 89 90 91 92 93 94 95
            this.capacity = capacity;
        }

        public void run() {
            if (address == 0) {
                // Paranoia
                return;
            }
            unsafe.freeMemory(address);
            address = 0;
96
            Bits.unreserveMemory(size, capacity);
D
duke 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        }

    }

    private final Cleaner cleaner;

    public Cleaner cleaner() { return cleaner; }

#else[byte]

    public Cleaner cleaner() { return null; }

#end[byte]

#end[rw]

#if[byte]

    // Primary constructor
    //
    Direct$Type$Buffer$RW$(int cap) {                   // package-private
#if[rw]
119
        super(-1, 0, cap, cap);
120
        boolean pa = VM.isDirectMemoryPageAligned();
D
duke 已提交
121
        int ps = Bits.pageSize();
122
        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
123 124
        Bits.reserveMemory(size, cap);

D
duke 已提交
125 126
        long base = 0;
        try {
127
            base = unsafe.allocateMemory(size);
D
duke 已提交
128
        } catch (OutOfMemoryError x) {
129
            Bits.unreserveMemory(size, cap);
D
duke 已提交
130 131
            throw x;
        }
132
        unsafe.setMemory(base, size, (byte) 0);
133
        if (pa && (base % ps != 0)) {
D
duke 已提交
134 135 136 137 138
            // Round up to page boundary
            address = base + ps - (base & (ps - 1));
        } else {
            address = base;
        }
139
        cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
140
        att = null;
D
duke 已提交
141 142 143 144 145 146 147
#else[rw]
        super(cap);
#end[rw]
    }

#if[rw]

148 149 150 151 152 153 154 155 156 157 158
    // Invoked to construct a direct ByteBuffer referring to the block of
    // memory. A given arbitrary object may also be attached to the buffer.
    //
    Direct$Type$Buffer(long addr, int cap, Object ob) {
        super(-1, 0, cap, cap);
        address = addr;
        cleaner = null;
        att = ob;
    }


D
duke 已提交
159 160 161
    // Invoked only by JNI: NewDirectByteBuffer(void*, long)
    //
    private Direct$Type$Buffer(long addr, int cap) {
162
        super(-1, 0, cap, cap);
D
duke 已提交
163 164
        address = addr;
        cleaner = null;
165
        att = null;
D
duke 已提交
166 167 168 169 170 171
    }

#end[rw]

    // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
    //
172 173 174 175
    protected Direct$Type$Buffer$RW$(int cap, long addr,
                                     FileDescriptor fd,
                                     Runnable unmapper)
    {
D
duke 已提交
176
#if[rw]
177
        super(-1, 0, cap, cap, fd);
D
duke 已提交
178 179
        address = addr;
        cleaner = Cleaner.create(this, unmapper);
180
        att = null;
D
duke 已提交
181
#else[rw]
182
        super(cap, addr, fd, unmapper);
D
duke 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#end[rw]
    }

#end[byte]

    // For duplicates and slices
    //
    Direct$Type$Buffer$RW$$BO$(DirectBuffer db,         // package-private
                               int mark, int pos, int lim, int cap,
                               int off)
    {
#if[rw]
        super(mark, pos, lim, cap);
        address = db.address() + off;
#if[byte]
        cleaner = null;
#end[byte]
200
        att = db;
D
duke 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
#else[rw]
        super(db, mark, pos, lim, cap, off);
#end[rw]
    }

    public $Type$Buffer slice() {
        int pos = this.position();
        int lim = this.limit();
        assert (pos <= lim);
        int rem = (pos <= lim ? lim - pos : 0);
        int off = (pos << $LG_BYTES_PER_VALUE$);
        assert (off >= 0);
        return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off);
    }

    public $Type$Buffer duplicate() {
        return new Direct$Type$Buffer$RW$$BO$(this,
                                              this.markValue(),
                                              this.position(),
                                              this.limit(),
                                              this.capacity(),
                                              0);
    }

    public $Type$Buffer asReadOnlyBuffer() {
#if[rw]
        return new Direct$Type$BufferR$BO$(this,
                                           this.markValue(),
                                           this.position(),
                                           this.limit(),
                                           this.capacity(),
                                           0);
#else[rw]
        return duplicate();
#end[rw]
    }

#if[rw]

    public long address() {
        return address;
    }

    private long ix(int i) {
B
bpb 已提交
245
        return address + ((long)i << $LG_BYTES_PER_VALUE$);
D
duke 已提交
246 247 248 249 250 251 252 253 254 255
    }

    public $type$ get() {
        return $fromBits$($swap$(unsafe.get$Swaptype$(ix(nextGetIndex()))));
    }

    public $type$ get(int i) {
        return $fromBits$($swap$(unsafe.get$Swaptype$(ix(checkIndex(i)))));
    }

256 257 258 259 260 261
#if[streamableType]
    $type$ getUnchecked(int i) {
        return $fromBits$($swap$(unsafe.get$Swaptype$(ix(i))));
    }
#end[streamableType]

D
duke 已提交
262 263
    public $Type$Buffer get($type$[] dst, int offset, int length) {
#if[rw]
B
bpb 已提交
264
        if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
D
duke 已提交
265 266 267 268 269 270 271 272
            checkBounds(offset, length, dst.length);
            int pos = position();
            int lim = limit();
            assert (pos <= lim);
            int rem = (pos <= lim ? lim - pos : 0);
            if (length > rem)
                throw new BufferUnderflowException();

273
#if[!byte]
D
duke 已提交
274 275
            if (order() != ByteOrder.nativeOrder())
                Bits.copyTo$Memtype$Array(ix(pos), dst,
B
bpb 已提交
276 277
                                          (long)offset << $LG_BYTES_PER_VALUE$,
                                          (long)length << $LG_BYTES_PER_VALUE$);
D
duke 已提交
278
            else
279 280
#end[!byte]
                Bits.copyToArray(ix(pos), dst, arrayBaseOffset,
B
bpb 已提交
281 282
                                 (long)offset << $LG_BYTES_PER_VALUE$,
                                 (long)length << $LG_BYTES_PER_VALUE$);
D
duke 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
            position(pos + length);
        } else {
            super.get(dst, offset, length);
        }
        return this;
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

#end[rw]

    public $Type$Buffer put($type$ x) {
#if[rw]
        unsafe.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
        return this;
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

    public $Type$Buffer put(int i, $type$ x) {
#if[rw]
        unsafe.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
        return this;
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

    public $Type$Buffer put($Type$Buffer src) {
#if[rw]
        if (src instanceof Direct$Type$Buffer$BO$) {
            if (src == this)
                throw new IllegalArgumentException();
            Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src;

            int spos = sb.position();
            int slim = sb.limit();
            assert (spos <= slim);
            int srem = (spos <= slim ? slim - spos : 0);

            int pos = position();
            int lim = limit();
            assert (pos <= lim);
            int rem = (pos <= lim ? lim - pos : 0);

            if (srem > rem)
                throw new BufferOverflowException();
B
bpb 已提交
332
            unsafe.copyMemory(sb.ix(spos), ix(pos), (long)srem << $LG_BYTES_PER_VALUE$);
D
duke 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
            sb.position(spos + srem);
            position(pos + srem);
        } else if (src.hb != null) {

            int spos = src.position();
            int slim = src.limit();
            assert (spos <= slim);
            int srem = (spos <= slim ? slim - spos : 0);

            put(src.hb, src.offset + spos, srem);
            src.position(spos + srem);

        } else {
            super.put(src);
        }
        return this;
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

    public $Type$Buffer put($type$[] src, int offset, int length) {
#if[rw]
B
bpb 已提交
356
        if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
D
duke 已提交
357 358 359 360 361 362 363 364
            checkBounds(offset, length, src.length);
            int pos = position();
            int lim = limit();
            assert (pos <= lim);
            int rem = (pos <= lim ? lim - pos : 0);
            if (length > rem)
                throw new BufferOverflowException();

365
#if[!byte]
D
duke 已提交
366
            if (order() != ByteOrder.nativeOrder())
B
bpb 已提交
367 368 369 370
                Bits.copyFrom$Memtype$Array(src,
                                            (long)offset << $LG_BYTES_PER_VALUE$,
                                            ix(pos),
                                            (long)length << $LG_BYTES_PER_VALUE$);
D
duke 已提交
371
            else
372
#end[!byte]
B
bpb 已提交
373 374 375 376
                Bits.copyFromArray(src, arrayBaseOffset,
                                   (long)offset << $LG_BYTES_PER_VALUE$,
                                   ix(pos),
                                   (long)length << $LG_BYTES_PER_VALUE$);
D
duke 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            position(pos + length);
        } else {
            super.put(src, offset, length);
        }
        return this;
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

    public $Type$Buffer compact() {
#if[rw]
        int pos = position();
        int lim = limit();
        assert (pos <= lim);
        int rem = (pos <= lim ? lim - pos : 0);

B
bpb 已提交
394
        unsafe.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$);
D
duke 已提交
395 396
        position(rem);
        limit(capacity());
397
        discardMark();
D
duke 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        return this;
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

    public boolean isDirect() {
        return true;
    }

    public boolean isReadOnly() {
        return {#if[rw]?false:true};
    }


#if[char]

    public String toString(int start, int end) {
        if ((end > limit()) || (start > end))
            throw new IndexOutOfBoundsException();
        try {
            int len = end - start;
            char[] ca = new char[len];
            CharBuffer cb = CharBuffer.wrap(ca);
            CharBuffer db = this.duplicate();
            db.position(start);
            db.limit(end);
            cb.put(db);
            return new String(ca);
        } catch (StringIndexOutOfBoundsException x) {
            throw new IndexOutOfBoundsException();
        }
    }


    // --- Methods to support CharSequence ---

435
    public CharBuffer subSequence(int start, int end) {
D
duke 已提交
436 437 438 439 440 441 442 443
        int pos = position();
        int lim = limit();
        assert (pos <= lim);
        pos = (pos <= lim ? pos : lim);
        int len = lim - pos;

        if ((start < 0) || (end > len) || (start > end))
            throw new IndexOutOfBoundsException();
444 445 446 447 448 449
        return new DirectCharBuffer$RW$$BO$(this,
                                            -1,
                                            pos + start,
                                            pos + end,
                                            capacity(),
                                            offset);
D
duke 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    }

#end[char]



#if[!byte]

    public ByteOrder order() {
#if[boS]
        return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
                ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
#end[boS]
#if[boU]
        return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
                ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
#end[boU]
    }

#end[!byte]



#if[byte]

    byte _get(int i) {                          // package-private
        return unsafe.getByte(address + i);
    }

    void _put(int i, byte b) {                  // package-private
#if[rw]
        unsafe.putByte(address + i, b);
#else[rw]
        throw new ReadOnlyBufferException();
#end[rw]
    }

    // #BIN
    //
    // Binary-data access methods  for short, char, int, long, float,
    // and double will be inserted here

#end[byte]

}