< prev index next >

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

Print this page




  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 java.util.concurrent.atomic.AtomicLong;
  29 
  30 import jdk.internal.misc.JavaNioAccess;
  31 import jdk.internal.misc.JavaLangRefAccess;
  32 import jdk.internal.misc.SharedSecrets;
  33 import jdk.internal.misc.Unsafe;
  34 import jdk.internal.misc.VM;


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


 612     // These methods should be called whenever direct memory is allocated or
 613     // freed.  They allow the user to control the amount of direct memory
 614     // which a process may access.  All sizes are specified in bytes.
 615     static void reserveMemory(long size, int cap) {
 616 
 617         if (!memoryLimitSet && VM.isBooted()) {
 618             maxMemory = VM.maxDirectMemory();
 619             memoryLimitSet = true;
 620         }
 621 
 622         // optimist!
 623         if (tryReserveMemory(size, cap)) {
 624             return;
 625         }
 626 
 627         final JavaLangRefAccess jlra = SharedSecrets.getJavaLangRefAccess();
 628 
 629         // retry while helping enqueue pending Reference objects
 630         // which includes executing pending Cleaner(s) which includes
 631         // Cleaner(s) that free direct buffer memory
 632         while (jlra.tryHandlePendingReference()) {

 633             if (tryReserveMemory(size, cap)) {
 634                 return;
 635             }
 636         }
 637 
 638         // trigger VM's Reference processing
 639         System.gc();
 640 
 641         // a retry loop with exponential back-off delays
 642         // (this gives VM some time to do it's job)
 643         boolean interrupted = false;
 644         try {
 645             long sleepTime = 1;
 646             int sleeps = 0;
 647             while (true) {
 648                 if (tryReserveMemory(size, cap)) {
 649                     return;
 650                 }
 651                 if (sleeps >= MAX_SLEEPS) {
 652                     break;
 653                 }
 654                 if (!jlra.tryHandlePendingReference()) {

 655                     try {
 656                         Thread.sleep(sleepTime);
 657                         sleepTime <<= 1;
 658                         sleeps++;
 659                     } catch (InterruptedException e) {
 660                         interrupted = true;
 661                     }
 662                 }
 663             }
 664 
 665             // no luck
 666             throw new OutOfMemoryError("Direct buffer memory");
 667 
 668         } finally {
 669             if (interrupted) {
 670                 // don't swallow interrupts
 671                 Thread.currentThread().interrupt();
 672             }
 673         }
 674     }




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


 614     // These methods should be called whenever direct memory is allocated or
 615     // freed.  They allow the user to control the amount of direct memory
 616     // which a process may access.  All sizes are specified in bytes.
 617     static void reserveMemory(long size, int cap) {
 618 
 619         if (!memoryLimitSet && VM.isBooted()) {
 620             maxMemory = VM.maxDirectMemory();
 621             memoryLimitSet = true;
 622         }
 623 
 624         // optimist!
 625         if (tryReserveMemory(size, cap)) {
 626             return;
 627         }
 628 
 629         final JavaLangRefAccess jlra = SharedSecrets.getJavaLangRefAccess();
 630 
 631         // retry while helping enqueue pending Reference objects
 632         // which includes executing pending Cleaner(s) which includes
 633         // Cleaner(s) that free direct buffer memory
 634         while (jlra.tryHandlePendingReference() | // use non-short-circuit or
 635                CleanerImpl.getCleanerImpl(CleanerFactory.cleaner()).drainQueue()) {
 636             if (tryReserveMemory(size, cap)) {
 637                 return;
 638             }
 639         }
 640 
 641         // trigger VM's Reference processing
 642         System.gc();
 643 
 644         // a retry loop with exponential back-off delays
 645         // (this gives VM some time to do it's job)
 646         boolean interrupted = false;
 647         try {
 648             long sleepTime = 1;
 649             int sleeps = 0;
 650             while (true) {
 651                 if (tryReserveMemory(size, cap)) {
 652                     return;
 653                 }
 654                 if (sleeps >= MAX_SLEEPS) {
 655                     break;
 656                 }
 657                 if (!jlra.tryHandlePendingReference() & // use non-short-circuit and
 658                     !CleanerImpl.getCleanerImpl(CleanerFactory.cleaner()).drainQueue()) {
 659                     try {
 660                         Thread.sleep(sleepTime);
 661                         sleepTime <<= 1;
 662                         sleeps++;
 663                     } catch (InterruptedException e) {
 664                         interrupted = true;
 665                     }
 666                 }
 667             }
 668 
 669             // no luck
 670             throw new OutOfMemoryError("Direct buffer memory");
 671 
 672         } finally {
 673             if (interrupted) {
 674                 // don't swallow interrupts
 675                 Thread.currentThread().interrupt();
 676             }
 677         }
 678     }


< prev index next >