< prev index next >

src/java.base/share/classes/sun/nio/fs/NativeBuffers.java

Print this page
*** 23,19 ***
   * questions.
   */
  
  package sun.nio.fs;
  
  import jdk.internal.misc.TerminatingThreadLocal;
  import jdk.internal.misc.Unsafe;
  
  /**
   * Factory for native buffers.
   */
  
  class NativeBuffers {
!     private NativeBuffers() { }
  
      private static final Unsafe unsafe = Unsafe.getUnsafe();
  
      private static final int TEMP_BUF_POOL_SIZE = 3;
      private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
--- 23,21 ---
   * questions.
   */
  
  package sun.nio.fs;
  
+ import jdk.internal.access.JavaLangAccess;
+ import jdk.internal.access.SharedSecrets;
  import jdk.internal.misc.TerminatingThreadLocal;
  import jdk.internal.misc.Unsafe;
  
  /**
   * Factory for native buffers.
   */
  
  class NativeBuffers {
!     private static JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  
      private static final Unsafe unsafe = Unsafe.getUnsafe();
  
      private static final int TEMP_BUF_POOL_SIZE = 3;
      private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {

*** 52,10 ***
--- 54,12 ---
                  }
              }
          }
      };
  
+     private NativeBuffers() { }
+ 
      /**
       * Allocates a native buffer, of at least the given size, from the heap.
       */
      static NativeBuffer allocNativeBuffer(int size) {
          // Make a new one of at least 2k

*** 67,11 ***
       * Returns a native buffer, of at least the given size, from the thread
       * local cache.
       */
      static NativeBuffer getNativeBufferFromCache(int size) {
          // return from cache if possible
!         NativeBuffer[] buffers = threadLocal.get();
          if (buffers != null) {
              for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
                  NativeBuffer buffer = buffers[i];
                  if (buffer != null && buffer.size() >= size) {
                      buffers[i] = null;
--- 71,11 ---
       * Returns a native buffer, of at least the given size, from the thread
       * local cache.
       */
      static NativeBuffer getNativeBufferFromCache(int size) {
          // return from cache if possible
!         NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal);
          if (buffers != null) {
              for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
                  NativeBuffer buffer = buffers[i];
                  if (buffer != null && buffer.size() >= size) {
                      buffers[i] = null;

*** 101,15 ***
       * Releases the given buffer. If there is space in the thread local cache
       * then the buffer goes into the cache; otherwise the memory is deallocated.
       */
      static void releaseNativeBuffer(NativeBuffer buffer) {
          // create cache if it doesn't exist
!         NativeBuffer[] buffers = threadLocal.get();
          if (buffers == null) {
              buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
              buffers[0] = buffer;
!             threadLocal.set(buffers);
              return;
          }
          // Put it in an empty slot if such exists
          for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
              if (buffers[i] == null) {
--- 105,15 ---
       * Releases the given buffer. If there is space in the thread local cache
       * then the buffer goes into the cache; otherwise the memory is deallocated.
       */
      static void releaseNativeBuffer(NativeBuffer buffer) {
          // create cache if it doesn't exist
!         NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal);
          if (buffers == null) {
              buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
              buffers[0] = buffer;
!             JLA.setCarrierThreadLocal(threadLocal, buffers);
              return;
          }
          // Put it in an empty slot if such exists
          for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
              if (buffers[i] == null) {
< prev index next >