jdk/src/java.base/share/classes/sun/nio/fs/NativeBuffer.java
changeset 36435 0408881ad616
parent 35641 da165fd9c886
child 36438 e13192ef7946
equal deleted inserted replaced
36434:3fd8dee1b158 36435:0408881ad616
    24  */
    24  */
    25 
    25 
    26 package sun.nio.fs;
    26 package sun.nio.fs;
    27 
    27 
    28 import jdk.internal.misc.Unsafe;
    28 import jdk.internal.misc.Unsafe;
    29 import jdk.internal.ref.Cleaner;
    29 import jdk.internal.ref.CleanerFactory;
       
    30 
       
    31 import java.lang.ref.Cleaner;
    30 
    32 
    31 /**
    33 /**
    32  * A light-weight buffer in native memory.
    34  * A light-weight buffer in native memory.
    33  */
    35  */
    34 
    36 
    35 class NativeBuffer {
    37 class NativeBuffer {
    36     private static final Unsafe unsafe = Unsafe.getUnsafe();
    38     private static final Unsafe unsafe = Unsafe.getUnsafe();
    37 
    39 
    38     private final long address;
    40     private final long address;
    39     private final int size;
    41     private final int size;
    40     private final Cleaner cleaner;
    42     private final Cleaner.Cleanable cleanable;
    41 
    43 
    42     // optional "owner" to avoid copying
    44     // optional "owner" to avoid copying
    43     // (only safe for use by thread-local caches)
    45     // (only safe for use by thread-local caches)
    44     private Object owner;
    46     private Object owner;
    45 
    47 
    54     }
    56     }
    55 
    57 
    56     NativeBuffer(int size) {
    58     NativeBuffer(int size) {
    57         this.address = unsafe.allocateMemory(size);
    59         this.address = unsafe.allocateMemory(size);
    58         this.size = size;
    60         this.size = size;
    59         this.cleaner = Cleaner.create(this, new Deallocator(address));
    61         this.cleanable = CleanerFactory.cleaner().register(this, new Deallocator(address));
    60     }
    62     }
    61 
    63 
    62     void release() {
    64     void release() {
    63         NativeBuffers.releaseNativeBuffer(this);
    65         NativeBuffers.releaseNativeBuffer(this);
    64     }
    66     }
    69 
    71 
    70     int size() {
    72     int size() {
    71         return size;
    73         return size;
    72     }
    74     }
    73 
    75 
    74     Cleaner cleaner() {
    76     void free() {
    75         return cleaner;
    77         cleanable.clean();
    76     }
    78     }
    77 
    79 
    78     // not synchronized; only safe for use by thread-local caches
    80     // not synchronized; only safe for use by thread-local caches
    79     void setOwner(Object owner) {
    81     void setOwner(Object owner) {
    80         this.owner = owner;
    82         this.owner = owner;