13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
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 #warn This file is preprocessed before being compiled
27
28 package java.nio;
29
30 import java.io.FileDescriptor;
31 import jdk.internal.misc.Unsafe;
32 import jdk.internal.misc.VM;
33 import jdk.internal.ref.Cleaner;
34 import sun.nio.ch.DirectBuffer;
35
36
37 class Direct$Type$Buffer$RW$$BO$
38 #if[rw]
39 extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
40 #else[rw]
41 extends Direct$Type$Buffer$BO$
42 #end[rw]
43 implements DirectBuffer
44 {
45
46 #if[rw]
47
48 // Cached unsafe-access object
49 protected static final Unsafe unsafe = Bits.unsafe();
50
51 // Cached array base offset
52 private static final long arrayBaseOffset = (long)unsafe.arrayBaseOffset($type$[].class);
53
81
82 private Deallocator(long address, long size, int capacity) {
83 assert (address != 0);
84 this.address = address;
85 this.size = size;
86 this.capacity = capacity;
87 }
88
89 public void run() {
90 if (address == 0) {
91 // Paranoia
92 return;
93 }
94 unsafe.freeMemory(address);
95 address = 0;
96 Bits.unreserveMemory(size, capacity);
97 }
98
99 }
100
101 private final Cleaner cleaner;
102
103 public Cleaner cleaner() { return cleaner; }
104
105 #else[byte]
106
107 public Cleaner cleaner() { return null; }
108
109 #end[byte]
110
111 #end[rw]
112
113 #if[byte]
114
115 // Primary constructor
116 //
117 Direct$Type$Buffer$RW$(int cap) { // package-private
118 #if[rw]
119 super(-1, 0, cap, cap);
120 boolean pa = VM.isDirectMemoryPageAligned();
121 int ps = Bits.pageSize();
122 long size = Math.max(1L, (long)cap + (pa ? ps : 0));
123 Bits.reserveMemory(size, cap);
124
125 long base = 0;
126 try {
127 base = unsafe.allocateMemory(size);
128 } catch (OutOfMemoryError x) {
129 Bits.unreserveMemory(size, cap);
130 throw x;
131 }
132 unsafe.setMemory(base, size, (byte) 0);
133 if (pa && (base % ps != 0)) {
134 // Round up to page boundary
135 address = base + ps - (base & (ps - 1));
136 } else {
137 address = base;
138 }
139 cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
140 att = null;
141 #else[rw]
142 super(cap);
143 #end[rw]
144 }
145
146 #if[rw]
147
148 // Invoked to construct a direct ByteBuffer referring to the block of
149 // memory. A given arbitrary object may also be attached to the buffer.
150 //
151 Direct$Type$Buffer(long addr, int cap, Object ob) {
152 super(-1, 0, cap, cap);
153 address = addr;
154 cleaner = null;
155 att = ob;
156 }
157
158
159 // Invoked only by JNI: NewDirectByteBuffer(void*, long)
160 //
161 private Direct$Type$Buffer(long addr, int cap) {
162 super(-1, 0, cap, cap);
163 address = addr;
164 cleaner = null;
165 att = null;
166 }
167
168 #end[rw]
169
170 // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
171 //
172 protected Direct$Type$Buffer$RW$(int cap, long addr,
173 FileDescriptor fd,
174 Runnable unmapper)
175 {
176 #if[rw]
177 super(-1, 0, cap, cap, fd);
178 address = addr;
179 cleaner = Cleaner.create(this, unmapper);
180 att = null;
181 #else[rw]
182 super(cap, addr, fd, unmapper);
183 #end[rw]
184 }
185
186 #end[byte]
187
188 // For duplicates and slices
189 //
190 Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private
191 int mark, int pos, int lim, int cap,
192 int off)
193 {
194 #if[rw]
195 super(mark, pos, lim, cap);
196 address = db.address() + off;
197 #if[byte]
198 cleaner = null;
199 #end[byte]
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
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 #warn This file is preprocessed before being compiled
27
28 package java.nio;
29
30 import java.io.FileDescriptor;
31 import jdk.internal.misc.Unsafe;
32 import jdk.internal.misc.VM;
33 import java.lang.ref.Cleaner;
34 import jdk.internal.ref.CleanerFactory;
35 import sun.nio.ch.DirectBuffer;
36
37
38 class Direct$Type$Buffer$RW$$BO$
39 #if[rw]
40 extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
41 #else[rw]
42 extends Direct$Type$Buffer$BO$
43 #end[rw]
44 implements DirectBuffer
45 {
46
47 #if[rw]
48
49 // Cached unsafe-access object
50 protected static final Unsafe unsafe = Bits.unsafe();
51
52 // Cached array base offset
53 private static final long arrayBaseOffset = (long)unsafe.arrayBaseOffset($type$[].class);
54
82
83 private Deallocator(long address, long size, int capacity) {
84 assert (address != 0);
85 this.address = address;
86 this.size = size;
87 this.capacity = capacity;
88 }
89
90 public void run() {
91 if (address == 0) {
92 // Paranoia
93 return;
94 }
95 unsafe.freeMemory(address);
96 address = 0;
97 Bits.unreserveMemory(size, capacity);
98 }
99
100 }
101
102 private final Cleaner.Cleanable cleaner;
103
104 public Cleaner.Cleanable cleaner() { return cleaner; }
105
106 #else[byte]
107
108 public Cleaner.Cleanable cleaner() { return null; }
109
110 #end[byte]
111
112 #end[rw]
113
114 #if[byte]
115
116 // Primary constructor
117 //
118 Direct$Type$Buffer$RW$(int cap) { // package-private
119 #if[rw]
120 super(-1, 0, cap, cap);
121 boolean pa = VM.isDirectMemoryPageAligned();
122 int ps = Bits.pageSize();
123 long size = Math.max(1L, (long)cap + (pa ? ps : 0));
124 Bits.reserveMemory(size, cap);
125
126 long base = 0;
127 try {
128 base = unsafe.allocateMemory(size);
129 } catch (OutOfMemoryError x) {
130 Bits.unreserveMemory(size, cap);
131 throw x;
132 }
133 unsafe.setMemory(base, size, (byte) 0);
134 if (pa && (base % ps != 0)) {
135 // Round up to page boundary
136 address = base + ps - (base & (ps - 1));
137 } else {
138 address = base;
139 }
140 cleaner = CleanerFactory.dbbCleaner().register(this, new Deallocator(base, size, cap));
141 att = null;
142 #else[rw]
143 super(cap);
144 #end[rw]
145 }
146
147 #if[rw]
148
149 // Invoked to construct a direct ByteBuffer referring to the block of
150 // memory. A given arbitrary object may also be attached to the buffer.
151 //
152 Direct$Type$Buffer(long addr, int cap, Object ob) {
153 super(-1, 0, cap, cap);
154 address = addr;
155 cleaner = null;
156 att = ob;
157 }
158
159
160 // Invoked only by JNI: NewDirectByteBuffer(void*, long)
161 //
162 private Direct$Type$Buffer(long addr, int cap) {
163 super(-1, 0, cap, cap);
164 address = addr;
165 cleaner = null;
166 att = null;
167 }
168
169 #end[rw]
170
171 // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
172 //
173 protected Direct$Type$Buffer$RW$(int cap, long addr,
174 FileDescriptor fd,
175 Runnable unmapper)
176 {
177 #if[rw]
178 super(-1, 0, cap, cap, fd);
179 address = addr;
180 cleaner = (unmapper == null)
181 ? null
182 : CleanerFactory.dbbCleaner().register(this, unmapper);
183 att = null;
184 #else[rw]
185 super(cap, addr, fd, unmapper);
186 #end[rw]
187 }
188
189 #end[byte]
190
191 // For duplicates and slices
192 //
193 Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private
194 int mark, int pos, int lim, int cap,
195 int off)
196 {
197 #if[rw]
198 super(mark, pos, lim, cap);
199 address = db.address() + off;
200 #if[byte]
201 cleaner = null;
202 #end[byte]
|