< prev index next >

src/java.base/share/classes/sun/nio/ch/Util.java

Print this page




 220             return ByteBuffer.allocateDirect(size);
 221         }
 222 
 223         BufferCache cache = bufferCache.get();
 224         ByteBuffer buf = cache.get(size);
 225         if (buf != null) {
 226             return buf;
 227         } else {
 228             // No suitable buffer in the cache so we need to allocate a new
 229             // one. To avoid the cache growing then we remove the first
 230             // buffer from the cache and free it.
 231             if (!cache.isEmpty()) {
 232                 buf = cache.removeFirst();
 233                 free(buf);
 234             }
 235             return ByteBuffer.allocateDirect(size);
 236         }
 237     }
 238 
 239     /**



























 240      * Releases a temporary buffer by returning to the cache or freeing it.
 241      */
 242     public static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
 243         offerFirstTemporaryDirectBuffer(buf);
 244     }
 245 
 246     /**
 247      * Releases a temporary buffer by returning to the cache or freeing it. If
 248      * returning to the cache then insert it at the start so that it is
 249      * likely to be returned by a subsequent call to getTemporaryDirectBuffer.
 250      */
 251     static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) {
 252         // If the buffer is too large for the cache we don't have to
 253         // check the cache. We'll just free it.
 254         if (isBufferTooLarge(buf)) {
 255             free(buf);
 256             return;
 257         }
 258 
 259         assert buf != null;




 220             return ByteBuffer.allocateDirect(size);
 221         }
 222 
 223         BufferCache cache = bufferCache.get();
 224         ByteBuffer buf = cache.get(size);
 225         if (buf != null) {
 226             return buf;
 227         } else {
 228             // No suitable buffer in the cache so we need to allocate a new
 229             // one. To avoid the cache growing then we remove the first
 230             // buffer from the cache and free it.
 231             if (!cache.isEmpty()) {
 232                 buf = cache.removeFirst();
 233                 free(buf);
 234             }
 235             return ByteBuffer.allocateDirect(size);
 236         }
 237     }
 238 
 239     /**
 240      * Returns a temporary buffer of at least the given size and
 241      * aligned to the alignment
 242      */
 243     public static ByteBuffer getTemporaryAlignedDirectBuffer(int size,
 244                                                              int alignment) {
 245         if (isBufferTooLarge(size)) {
 246             return ByteBuffer.allocateDirect(size + alignment - 1)
 247                     .alignedSlice(alignment);
 248         }
 249 
 250         BufferCache cache = bufferCache.get();
 251         ByteBuffer buf = cache.get(size);
 252         if (buf != null) {
 253             if (buf.alignmentOffset(0, alignment) == 0) {
 254                 return buf;
 255             }
 256         } else {
 257             if (!cache.isEmpty()) {
 258                 buf = cache.removeFirst();
 259                 free(buf);
 260             }
 261         }
 262         return ByteBuffer.allocateDirect(size + alignment - 1)
 263                 .alignedSlice(alignment);
 264     }
 265 
 266     /**
 267      * Releases a temporary buffer by returning to the cache or freeing it.
 268      */
 269     public static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
 270         offerFirstTemporaryDirectBuffer(buf);
 271     }
 272 
 273     /**
 274      * Releases a temporary buffer by returning to the cache or freeing it. If
 275      * returning to the cache then insert it at the start so that it is
 276      * likely to be returned by a subsequent call to getTemporaryDirectBuffer.
 277      */
 278     static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) {
 279         // If the buffer is too large for the cache we don't have to
 280         // check the cache. We'll just free it.
 281         if (isBufferTooLarge(buf)) {
 282             free(buf);
 283             return;
 284         }
 285 
 286         assert buf != null;


< prev index next >