< prev index next >

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template

Print this page


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

  32 import jdk.internal.misc.VM;
  33 import jdk.internal.ref.Cleaner;
  34 import sun.nio.ch.DirectBuffer;
  35 
  36 
  37 class Direct$Type$Buffer$RW$$BO$
  38 #if[rw]
  39     extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
  40 #else[rw]
  41     extends Direct$Type$Buffer$BO$
  42 #end[rw]
  43     implements DirectBuffer
  44 {
  45 
  46 #if[rw]
  47 
  48     // Cached array base offset
  49     private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
  50 
  51     // Cached unaligned-access capability


 308                 else
 309 #end[!byte]
 310                     UNSAFE.copyMemory(null,
 311                                       ix(pos),
 312                                       dst,
 313                                       dstOffset,
 314                                       (long)length << $LG_BYTES_PER_VALUE$);
 315             } finally {
 316                 Reference.reachabilityFence(this);
 317             }
 318             position(pos + length);
 319         } else {
 320             super.get(dst, offset, length);
 321         }
 322         return this;
 323 #else[rw]
 324         throw new ReadOnlyBufferException();
 325 #end[rw]
 326     }
 327 



































 328 #end[rw]
 329 
 330     public $Type$Buffer put($type$ x) {
 331 #if[rw]
 332         try {
 333             UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
 334         } finally {
 335             Reference.reachabilityFence(this);
 336         }
 337         return this;
 338 #else[rw]
 339         throw new ReadOnlyBufferException();
 340 #end[rw]
 341     }
 342 
 343     public $Type$Buffer put(int i, $type$ x) {
 344 #if[rw]
 345         try {
 346             UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
 347         } finally {


 423                 else
 424 #end[!byte]
 425                     UNSAFE.copyMemory(src,
 426                                       srcOffset,
 427                                       null,
 428                                       ix(pos),
 429                                       (long)length << $LG_BYTES_PER_VALUE$);
 430             } finally {
 431                 Reference.reachabilityFence(this);
 432             }
 433             position(pos + length);
 434         } else {
 435             super.put(src, offset, length);
 436         }
 437         return this;
 438 #else[rw]
 439         throw new ReadOnlyBufferException();
 440 #end[rw]
 441     }
 442 


































































 443     public $Type$Buffer compact() {
 444 #if[rw]
 445         int pos = position();
 446         int lim = limit();
 447         assert (pos <= lim);
 448         int rem = (pos <= lim ? lim - pos : 0);
 449         try {
 450             UNSAFE.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$);
 451         } finally {
 452             Reference.reachabilityFence(this);
 453         }
 454         position(rem);
 455         limit(capacity());
 456         discardMark();
 457         return this;
 458 #else[rw]
 459         throw new ReadOnlyBufferException();
 460 #end[rw]
 461     }
 462 


   1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #warn This file is preprocessed before being compiled
  27 
  28 package java.nio;
  29 
  30 import java.io.FileDescriptor;
  31 import java.lang.ref.Reference;
  32 import java.util.Objects;
  33 import jdk.internal.misc.VM;
  34 import jdk.internal.ref.Cleaner;
  35 import sun.nio.ch.DirectBuffer;
  36 
  37 
  38 class Direct$Type$Buffer$RW$$BO$
  39 #if[rw]
  40     extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
  41 #else[rw]
  42     extends Direct$Type$Buffer$BO$
  43 #end[rw]
  44     implements DirectBuffer
  45 {
  46 
  47 #if[rw]
  48 
  49     // Cached array base offset
  50     private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
  51 
  52     // Cached unaligned-access capability


 309                 else
 310 #end[!byte]
 311                     UNSAFE.copyMemory(null,
 312                                       ix(pos),
 313                                       dst,
 314                                       dstOffset,
 315                                       (long)length << $LG_BYTES_PER_VALUE$);
 316             } finally {
 317                 Reference.reachabilityFence(this);
 318             }
 319             position(pos + length);
 320         } else {
 321             super.get(dst, offset, length);
 322         }
 323         return this;
 324 #else[rw]
 325         throw new ReadOnlyBufferException();
 326 #end[rw]
 327     }
 328 
 329     public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
 330         //System.out.println("Direct absolute bulk get");
 331 #if[rw]
 332         if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
 333             Objects.checkFromIndexSize(index, length, limit());
 334             Objects.checkFromIndexSize(offset, length, dst.length);
 335 
 336             long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
 337             try {
 338 #if[!byte]
 339                 if (order() != ByteOrder.nativeOrder())
 340                     UNSAFE.copySwapMemory(null,
 341                                           ix(index),
 342                                           dst,
 343                                           dstOffset,
 344                                           (long)length << $LG_BYTES_PER_VALUE$,
 345                                           (long)1 << $LG_BYTES_PER_VALUE$);
 346                 else
 347 #end[!byte]
 348                     UNSAFE.copyMemory(null,
 349                                       ix(index),
 350                                       dst,
 351                                       dstOffset,
 352                                       (long)length << $LG_BYTES_PER_VALUE$);
 353             } finally {
 354                 Reference.reachabilityFence(this);
 355             }
 356         } else {
 357             super.get(index, dst, offset, length);
 358         }
 359         return this;
 360 #else[rw]
 361         throw new ReadOnlyBufferException();
 362 #end[rw]
 363     }
 364 #end[rw]
 365 
 366     public $Type$Buffer put($type$ x) {
 367 #if[rw]
 368         try {
 369             UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
 370         } finally {
 371             Reference.reachabilityFence(this);
 372         }
 373         return this;
 374 #else[rw]
 375         throw new ReadOnlyBufferException();
 376 #end[rw]
 377     }
 378 
 379     public $Type$Buffer put(int i, $type$ x) {
 380 #if[rw]
 381         try {
 382             UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
 383         } finally {


 459                 else
 460 #end[!byte]
 461                     UNSAFE.copyMemory(src,
 462                                       srcOffset,
 463                                       null,
 464                                       ix(pos),
 465                                       (long)length << $LG_BYTES_PER_VALUE$);
 466             } finally {
 467                 Reference.reachabilityFence(this);
 468             }
 469             position(pos + length);
 470         } else {
 471             super.put(src, offset, length);
 472         }
 473         return this;
 474 #else[rw]
 475         throw new ReadOnlyBufferException();
 476 #end[rw]
 477     }
 478 
 479     public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
 480         //System.out.println("Direct absolute bulk put array");
 481 #if[rw]
 482         if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
 483             Objects.checkFromIndexSize(index, length, limit());
 484             Objects.checkFromIndexSize(offset, length, src.length);
 485 
 486 
 487             long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
 488             try {
 489 #if[!byte]
 490                 if (order() != ByteOrder.nativeOrder())
 491                     UNSAFE.copySwapMemory(src,
 492                                           srcOffset,
 493                                           null,
 494                                           ix(index),
 495                                           (long)length << $LG_BYTES_PER_VALUE$,
 496                                           (long)1 << $LG_BYTES_PER_VALUE$);
 497                 else
 498 #end[!byte]
 499                     UNSAFE.copyMemory(src,
 500                                       srcOffset,
 501                                       null,
 502                                       ix(index),
 503                                       (long)length << $LG_BYTES_PER_VALUE$);
 504             } finally {
 505                 Reference.reachabilityFence(this);
 506             }
 507         } else {
 508             super.put(index, src, offset, length);
 509         }
 510         return this;
 511 #else[rw]
 512         throw new ReadOnlyBufferException();
 513 #end[rw]
 514     }
 515 
 516     public $Type$Buffer put(int index, $Type$Buffer src, int srcIndex,
 517         int length) {
 518         //System.out.println("Direct absolute bulk buffer");
 519 #if[rw]
 520         if (src instanceof Direct$Type$Buffer$BO$) {
 521             Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src;
 522 
 523             Objects.checkFromIndexSize(index, length, limit());
 524             Objects.checkFromIndexSize(srcIndex, length, sb.limit());
 525             try {
 526                 UNSAFE.copyMemory(sb.ix(srcIndex), ix(index),
 527                     (long)length << $LG_BYTES_PER_VALUE$);
 528             } finally {
 529                 Reference.reachabilityFence(sb);
 530                 Reference.reachabilityFence(this);
 531             }
 532         } else if (src.hb != null) {
 533             Objects.checkFromIndexSize(index, length, limit());
 534             Objects.checkFromIndexSize(srcIndex, length, src.limit());
 535             put(index, src.hb, srcIndex, length);
 536         } else {
 537             super.put(index, src, srcIndex, length);
 538         }
 539         return this;
 540 #else[rw]
 541         throw new ReadOnlyBufferException();
 542 #end[rw]
 543     }
 544 
 545     public $Type$Buffer compact() {
 546 #if[rw]
 547         int pos = position();
 548         int lim = limit();
 549         assert (pos <= lim);
 550         int rem = (pos <= lim ? lim - pos : 0);
 551         try {
 552             UNSAFE.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$);
 553         } finally {
 554             Reference.reachabilityFence(this);
 555         }
 556         position(rem);
 557         limit(capacity());
 558         discardMark();
 559         return this;
 560 #else[rw]
 561         throw new ReadOnlyBufferException();
 562 #end[rw]
 563     }
 564 


< prev index next >