equal
deleted
inserted
replaced
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.CleanerFactory; |
29 import jdk.internal.ref.Cleaner; |
30 |
|
31 import java.lang.ref.Cleaner; |
|
32 |
30 |
33 /** |
31 /** |
34 * A light-weight buffer in native memory. |
32 * A light-weight buffer in native memory. |
35 */ |
33 */ |
36 |
34 |
37 class NativeBuffer { |
35 class NativeBuffer { |
38 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
36 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
39 |
37 |
40 private final long address; |
38 private final long address; |
41 private final int size; |
39 private final int size; |
42 private final Cleaner.Cleanable cleanable; |
40 private final Cleaner cleaner; |
43 |
41 |
44 // optional "owner" to avoid copying |
42 // optional "owner" to avoid copying |
45 // (only safe for use by thread-local caches) |
43 // (only safe for use by thread-local caches) |
46 private Object owner; |
44 private Object owner; |
47 |
45 |
56 } |
54 } |
57 |
55 |
58 NativeBuffer(int size) { |
56 NativeBuffer(int size) { |
59 this.address = unsafe.allocateMemory(size); |
57 this.address = unsafe.allocateMemory(size); |
60 this.size = size; |
58 this.size = size; |
61 this.cleanable = CleanerFactory.cleaner().register(this, new Deallocator(address)); |
59 this.cleaner = Cleaner.create(this, new Deallocator(address)); |
62 } |
60 } |
63 |
61 |
64 void release() { |
62 void release() { |
65 NativeBuffers.releaseNativeBuffer(this); |
63 NativeBuffers.releaseNativeBuffer(this); |
66 } |
64 } |
72 int size() { |
70 int size() { |
73 return size; |
71 return size; |
74 } |
72 } |
75 |
73 |
76 void free() { |
74 void free() { |
77 cleanable.clean(); |
75 cleaner.clean(); |
78 } |
76 } |
79 |
77 |
80 // not synchronized; only safe for use by thread-local caches |
78 // not synchronized; only safe for use by thread-local caches |
81 void setOwner(Object owner) { |
79 void setOwner(Object owner) { |
82 this.owner = owner; |
80 this.owner = owner; |