< prev index next >

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

Print this page




  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 package java.nio;
  27 
  28 import jdk.internal.misc.JavaNioAccess;
  29 import jdk.internal.misc.SharedSecrets;
  30 import jdk.internal.misc.Unsafe;
  31 import jdk.internal.misc.VM;
  32 import jdk.internal.ref.CleanerFactory;
  33 
  34 import java.util.concurrent.atomic.AtomicLong;
  35 import java.util.function.BooleanSupplier;
  36 
  37 /**
  38  * Access to bits, native and otherwise.
  39  */
  40 
  41 class Bits {                            // package-private
  42 
  43     private Bits() { }
  44 
  45 
  46     // -- Swapping --
  47 
  48     static short swap(short x) {
  49         return Short.reverseBytes(x);
  50     }
  51 
  52     static char swap(char x) {
  53         return Character.reverseBytes(x);
  54     }
  55 


 590 
 591     static boolean unaligned() {
 592         return unaligned;
 593     }
 594 
 595 
 596     // -- Direct memory management --
 597 
 598     // A user-settable upper limit on the maximum amount of allocatable
 599     // direct buffer memory.  This value may be changed during VM
 600     // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>".
 601     private static volatile long maxMemory = VM.maxDirectMemory();
 602     private static final AtomicLong reservedMemory = new AtomicLong();
 603     private static final AtomicLong totalCapacity = new AtomicLong();
 604     private static final AtomicLong count = new AtomicLong();
 605     private static volatile boolean memoryLimitSet;
 606 
 607     // These methods should be called whenever direct memory is allocated or
 608     // freed.  They allow the user to control the amount of direct memory
 609     // which a process may access.  All sizes are specified in bytes.
 610     static void reserveMemory(long size, int cap) {
 611 
 612         if (!memoryLimitSet && VM.initLevel() >= 1) {
 613             maxMemory = VM.maxDirectMemory();
 614             memoryLimitSet = true;
 615         }
 616 
 617         if (!CleanerFactory.dbbCleaner().retryWhileHelpingClean(
 618             new BooleanSupplier() {
 619                 @Override
 620                 public boolean getAsBoolean() {
 621                     return tryReserveMemory(size, cap);
 622                 }
 623             })) {
 624             // no luck
 625             throw new OutOfMemoryError("Direct buffer memory");
 626         }
 627     }
 628 
 629     private static boolean tryReserveMemory(long size, int cap) {
 630 
 631         // -XX:MaxDirectMemorySize limits the total capacity rather than the
 632         // actual memory usage, which will differ when buffers are page
 633         // aligned.
 634         long totalCap;
 635         while (cap <= maxMemory - (totalCap = totalCapacity.get())) {
 636             if (totalCapacity.compareAndSet(totalCap, totalCap + cap)) {
 637                 reservedMemory.addAndGet(size);
 638                 count.incrementAndGet();
 639                 return true;
 640             }
 641         }
 642 
 643         return false;
 644     }
 645 
 646 
 647     static void unreserveMemory(long size, int cap) {
 648         long cnt = count.decrementAndGet();
 649         long reservedMem = reservedMemory.addAndGet(-size);




  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 package java.nio;
  27 
  28 import jdk.internal.misc.JavaNioAccess;
  29 import jdk.internal.misc.SharedSecrets;
  30 import jdk.internal.misc.Unsafe;
  31 import jdk.internal.misc.VM;

  32 
  33 import java.util.concurrent.atomic.AtomicLong;

  34 
  35 /**
  36  * Access to bits, native and otherwise.
  37  */
  38 
  39 class Bits {                            // package-private
  40 
  41     private Bits() { }
  42 
  43 
  44     // -- Swapping --
  45 
  46     static short swap(short x) {
  47         return Short.reverseBytes(x);
  48     }
  49 
  50     static char swap(char x) {
  51         return Character.reverseBytes(x);
  52     }
  53 


 588 
 589     static boolean unaligned() {
 590         return unaligned;
 591     }
 592 
 593 
 594     // -- Direct memory management --
 595 
 596     // A user-settable upper limit on the maximum amount of allocatable
 597     // direct buffer memory.  This value may be changed during VM
 598     // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>".
 599     private static volatile long maxMemory = VM.maxDirectMemory();
 600     private static final AtomicLong reservedMemory = new AtomicLong();
 601     private static final AtomicLong totalCapacity = new AtomicLong();
 602     private static final AtomicLong count = new AtomicLong();
 603     private static volatile boolean memoryLimitSet;
 604 
 605     // These methods should be called whenever direct memory is allocated or
 606     // freed.  They allow the user to control the amount of direct memory
 607     // which a process may access.  All sizes are specified in bytes.
 608     static boolean tryReserveMemory(long size, int cap) {
 609 
 610         if (!memoryLimitSet && VM.initLevel() >= 1) {
 611             maxMemory = VM.maxDirectMemory();
 612             memoryLimitSet = true;
 613         }














 614 
 615         // -XX:MaxDirectMemorySize limits the total capacity rather than the
 616         // actual memory usage, which will differ when buffers are page
 617         // aligned.
 618         long totalCap;
 619         while (cap <= maxMemory - (totalCap = totalCapacity.get())) {
 620             if (totalCapacity.compareAndSet(totalCap, totalCap + cap)) {
 621                 reservedMemory.addAndGet(size);
 622                 count.incrementAndGet();
 623                 return true;
 624             }
 625         }
 626 
 627         return false;
 628     }
 629 
 630 
 631     static void unreserveMemory(long size, int cap) {
 632         long cnt = count.decrementAndGet();
 633         long reservedMem = reservedMemory.addAndGet(-size);


< prev index next >