src/java.base/share/classes/java/nio/Bits.java

Print this page




 550 
 551     // -- Unsafe access --
 552 
 553     private static final Unsafe unsafe = Unsafe.getUnsafe();
 554 
 555     private static byte _get(long a) {
 556         return unsafe.getByte(a);
 557     }
 558 
 559     private static void _put(long a, byte b) {
 560         unsafe.putByte(a, b);
 561     }
 562 
 563     static Unsafe unsafe() {
 564         return unsafe;
 565     }
 566 
 567 
 568     // -- Processor and memory-system properties --
 569 
 570     private static final ByteOrder byteOrder;

 571 
 572     static ByteOrder byteOrder() {
 573         if (byteOrder == null)
 574             throw new Error("Unknown byte order");
 575         return byteOrder;
 576     }
 577 
 578     static {
 579         long a = unsafe.allocateMemory(8);
 580         try {
 581             unsafe.putLong(a, 0x0102030405060708L);
 582             byte b = unsafe.getByte(a);
 583             switch (b) {
 584             case 0x01: byteOrder = ByteOrder.BIG_ENDIAN;     break;
 585             case 0x08: byteOrder = ByteOrder.LITTLE_ENDIAN;  break;
 586             default:
 587                 assert false;
 588                 byteOrder = null;
 589             }
 590         } finally {
 591             unsafe.freeMemory(a);
 592         }
 593     }
 594 
 595 
 596     private static int pageSize = -1;
 597 
 598     static int pageSize() {
 599         if (pageSize == -1)
 600             pageSize = unsafe().pageSize();
 601         return pageSize;
 602     }
 603 
 604     static int pageCount(long size) {
 605         return (int)(size + (long)pageSize() - 1L) / pageSize();
 606     }
 607 
 608     private static boolean unaligned;
 609     private static boolean unalignedKnown = false;
 610 
 611     static boolean unaligned() {
 612         if (unalignedKnown)
 613             return unaligned;
 614         String arch = AccessController.doPrivileged(
 615             new sun.security.action.GetPropertyAction("os.arch"));
 616         unaligned = arch.equals("i386") || arch.equals("x86")
 617             || arch.equals("amd64") || arch.equals("x86_64");
 618         unalignedKnown = true;
 619         return unaligned;
 620     }
 621 
 622 
 623     // -- Direct memory management --
 624 
 625     // A user-settable upper limit on the maximum amount of allocatable
 626     // direct buffer memory.  This value may be changed during VM
 627     // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>".
 628     private static volatile long maxMemory = VM.maxDirectMemory();
 629     private static final AtomicLong reservedMemory = new AtomicLong();
 630     private static final AtomicLong totalCapacity = new AtomicLong();
 631     private static final AtomicLong count = new AtomicLong();
 632     private static volatile boolean memoryLimitSet = false;
 633     // max. number of sleeps during try-reserving with exponentially
 634     // increasing delay before throwing OutOfMemoryError:
 635     // 1, 2, 4, 8, 16, 32, 64, 128, 256 (total 511 ms ~ 0.5 s)
 636     // which means that OOME will be thrown after 0.5 s of trying
 637     private static final int MAX_SLEEPS = 9;




 550 
 551     // -- Unsafe access --
 552 
 553     private static final Unsafe unsafe = Unsafe.getUnsafe();
 554 
 555     private static byte _get(long a) {
 556         return unsafe.getByte(a);
 557     }
 558 
 559     private static void _put(long a, byte b) {
 560         unsafe.putByte(a, b);
 561     }
 562 
 563     static Unsafe unsafe() {
 564         return unsafe;
 565     }
 566 
 567 
 568     // -- Processor and memory-system properties --
 569 
 570     private static final ByteOrder byteOrder
 571         = unsafe.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
 572 
 573     static ByteOrder byteOrder() {
 574         if (byteOrder == null)
 575             throw new Error("Unknown byte order");
 576         return byteOrder;
 577     }
 578 


















 579     private static int pageSize = -1;
 580 
 581     static int pageSize() {
 582         if (pageSize == -1)
 583             pageSize = unsafe().pageSize();
 584         return pageSize;
 585     }
 586 
 587     static int pageCount(long size) {
 588         return (int)(size + (long)pageSize() - 1L) / pageSize();
 589     }
 590 
 591     private static boolean unaligned;
 592     private static boolean unalignedKnown = false;
 593 
 594     static boolean unaligned() {
 595         if (unalignedKnown)
 596             return unaligned;
 597         unaligned = unsafe.unalignedAccess();



 598         unalignedKnown = true;
 599         return unaligned;
 600     }
 601 
 602 
 603     // -- Direct memory management --
 604 
 605     // A user-settable upper limit on the maximum amount of allocatable
 606     // direct buffer memory.  This value may be changed during VM
 607     // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>".
 608     private static volatile long maxMemory = VM.maxDirectMemory();
 609     private static final AtomicLong reservedMemory = new AtomicLong();
 610     private static final AtomicLong totalCapacity = new AtomicLong();
 611     private static final AtomicLong count = new AtomicLong();
 612     private static volatile boolean memoryLimitSet = false;
 613     // max. number of sleeps during try-reserving with exponentially
 614     // increasing delay before throwing OutOfMemoryError:
 615     // 1, 2, 4, 8, 16, 32, 64, 128, 256 (total 511 ms ~ 0.5 s)
 616     // which means that OOME will be thrown after 0.5 s of trying
 617     private static final int MAX_SLEEPS = 9;