1 /* 2 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 55 // Cached unaligned-access capability 56 protected static final boolean unaligned = Bits.unaligned(); 57 58 // Base address, used in all indexing calculations 59 // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress 60 // protected long address; 61 62 // An object attached to this buffer. If this buffer is a view of another 63 // buffer then we use this field to keep a reference to that buffer to 64 // ensure that its memory isn't freed before we are done with it. 65 private final Object att; 66 67 public Object attachment() { 68 return att; 69 } 70 71 #if[byte] 72 73 private static class Deallocator 74 implements Runnable 75 { 76 77 private static Unsafe unsafe = Unsafe.getUnsafe(); 78 79 private long address; 80 private long size; 81 private int capacity; 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.cleaner().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 = CleanerFactory.cleaner().register(this, unmapper); 181 att = null; 182 #else[rw] 183 super(cap, addr, fd, unmapper); 184 #end[rw] 185 } 186 187 #end[byte] 188 189 // For duplicates and slices 190 // 191 Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private 192 int mark, int pos, int lim, int cap, 193 int off) 194 { 195 #if[rw] 196 super(mark, pos, lim, cap); 197 address = db.address() + off; 198 #if[byte] 199 cleaner = null; 200 #end[byte] 201 att = db; 202 #else[rw] 203 super(db, mark, pos, lim, cap, off); 204 #end[rw] 205 } 206 207 public $Type$Buffer slice() { 208 int pos = this.position(); 209 int lim = this.limit(); 210 assert (pos <= lim); 211 int rem = (pos <= lim ? lim - pos : 0); 212 int off = (pos << $LG_BYTES_PER_VALUE$); 213 assert (off >= 0); 214 return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off); 215 } 216 217 public $Type$Buffer duplicate() { 218 return new Direct$Type$Buffer$RW$$BO$(this, 219 this.markValue(), 220 this.position(), 221 this.limit(), 222 this.capacity(), 223 0); 224 } 225 226 public $Type$Buffer asReadOnlyBuffer() { 227 #if[rw] 228 return new Direct$Type$BufferR$BO$(this, 229 this.markValue(), 230 this.position(), 231 this.limit(), 232 this.capacity(), 233 0); 234 #else[rw] 235 return duplicate(); 236 #end[rw] 237 } 238 239 #if[rw] 240 241 public long address() { 242 return address; 243 } 244 245 private long ix(int i) { 246 return address + ((long)i << $LG_BYTES_PER_VALUE$); 247 } 248 249 public $type$ get() { 250 return $fromBits$($swap$(unsafe.get$Swaptype$(ix(nextGetIndex())))); 251 } 252 253 public $type$ get(int i) { 254 return $fromBits$($swap$(unsafe.get$Swaptype$(ix(checkIndex(i))))); 255 } 256 257 #if[streamableType] 258 $type$ getUnchecked(int i) { 259 return $fromBits$($swap$(unsafe.get$Swaptype$(ix(i)))); 260 } 261 #end[streamableType] 262 263 public $Type$Buffer get($type$[] dst, int offset, int length) { 264 #if[rw] 265 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) { 266 checkBounds(offset, length, dst.length); 267 int pos = position(); 268 int lim = limit(); 269 assert (pos <= lim); 270 int rem = (pos <= lim ? lim - pos : 0); 271 if (length > rem) 272 throw new BufferUnderflowException(); 273 274 #if[!byte] 275 if (order() != ByteOrder.nativeOrder()) 276 Bits.copyTo$Memtype$Array(ix(pos), dst, 277 (long)offset << $LG_BYTES_PER_VALUE$, 278 (long)length << $LG_BYTES_PER_VALUE$); 279 else 280 #end[!byte] 281 Bits.copyToArray(ix(pos), dst, arrayBaseOffset, 282 (long)offset << $LG_BYTES_PER_VALUE$, 283 (long)length << $LG_BYTES_PER_VALUE$); 284 position(pos + length); 285 } else { 286 super.get(dst, offset, length); 287 } 288 return this; 289 #else[rw] 290 throw new ReadOnlyBufferException(); 291 #end[rw] 292 } 293 294 #end[rw] 295 296 public $Type$Buffer put($type$ x) { 297 #if[rw] 298 unsafe.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x))); 299 return this; 300 #else[rw] 301 throw new ReadOnlyBufferException(); 302 #end[rw] 303 } 304 305 public $Type$Buffer put(int i, $type$ x) { 306 #if[rw] 307 unsafe.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x))); 308 return this; 309 #else[rw] 310 throw new ReadOnlyBufferException(); 311 #end[rw] 312 } 313 314 public $Type$Buffer put($Type$Buffer src) { 315 #if[rw] 316 if (src instanceof Direct$Type$Buffer$BO$) { 317 if (src == this) 318 throw createSameBufferException(); 319 Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src; 320 321 int spos = sb.position(); 322 int slim = sb.limit(); 323 assert (spos <= slim); 324 int srem = (spos <= slim ? slim - spos : 0); 325 326 int pos = position(); 327 int lim = limit(); 328 assert (pos <= lim); 329 int rem = (pos <= lim ? lim - pos : 0); 330 331 if (srem > rem) 332 throw new BufferOverflowException(); 333 unsafe.copyMemory(sb.ix(spos), ix(pos), (long)srem << $LG_BYTES_PER_VALUE$); 334 sb.position(spos + srem); 335 position(pos + srem); 336 } else if (src.hb != null) { 337 338 int spos = src.position(); 339 int slim = src.limit(); 340 assert (spos <= slim); 341 int srem = (spos <= slim ? slim - spos : 0); 342 343 put(src.hb, src.offset + spos, srem); 344 src.position(spos + srem); 345 346 } else { 347 super.put(src); 348 } 349 return this; 350 #else[rw] 351 throw new ReadOnlyBufferException(); 352 #end[rw] 353 } 354 355 public $Type$Buffer put($type$[] src, int offset, int length) { 356 #if[rw] 357 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) { 358 checkBounds(offset, length, src.length); 359 int pos = position(); 360 int lim = limit(); 361 assert (pos <= lim); 362 int rem = (pos <= lim ? lim - pos : 0); 363 if (length > rem) 364 throw new BufferOverflowException(); 365 366 #if[!byte] 367 if (order() != ByteOrder.nativeOrder()) 368 Bits.copyFrom$Memtype$Array(src, 369 (long)offset << $LG_BYTES_PER_VALUE$, 370 ix(pos), 371 (long)length << $LG_BYTES_PER_VALUE$); 372 else 373 #end[!byte] 374 Bits.copyFromArray(src, arrayBaseOffset, 375 (long)offset << $LG_BYTES_PER_VALUE$, 376 ix(pos), 377 (long)length << $LG_BYTES_PER_VALUE$); 378 position(pos + length); 379 } else { 380 super.put(src, offset, length); 381 } 382 return this; 383 #else[rw] 384 throw new ReadOnlyBufferException(); 385 #end[rw] 386 } 387 388 public $Type$Buffer compact() { 389 #if[rw] 390 int pos = position(); 391 int lim = limit(); 392 assert (pos <= lim); 393 int rem = (pos <= lim ? lim - pos : 0); 394 395 unsafe.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$); 396 position(rem); 397 limit(capacity()); 398 discardMark(); 399 return this; 400 #else[rw] 401 throw new ReadOnlyBufferException(); 402 #end[rw] 403 } 404 405 public boolean isDirect() { 406 return true; 407 } 408 409 public boolean isReadOnly() { 410 return {#if[rw]?false:true}; 411 } 412 413 414 #if[char] 415 416 public String toString(int start, int end) { 417 if ((end > limit()) || (start > end)) 418 throw new IndexOutOfBoundsException(); 419 try { 420 int len = end - start; 421 char[] ca = new char[len]; 422 CharBuffer cb = CharBuffer.wrap(ca); 423 CharBuffer db = this.duplicate(); 424 db.position(start); 425 db.limit(end); 426 cb.put(db); 427 return new String(ca); 428 } catch (StringIndexOutOfBoundsException x) { 429 throw new IndexOutOfBoundsException(); 430 } 431 } 432 433 434 // --- Methods to support CharSequence --- 435 436 public CharBuffer subSequence(int start, int end) { 437 int pos = position(); 438 int lim = limit(); 439 assert (pos <= lim); 440 pos = (pos <= lim ? pos : lim); 441 int len = lim - pos; 442 443 if ((start < 0) || (end > len) || (start > end)) 444 throw new IndexOutOfBoundsException(); 445 return new DirectCharBuffer$RW$$BO$(this, 446 -1, 447 pos + start, 448 pos + end, 449 capacity(), 450 offset); 451 } 452 453 #end[char] 454 455 456 457 #if[!byte] 458 459 public ByteOrder order() { 460 #if[boS] 461 return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) 462 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); 463 #end[boS] 464 #if[boU] 465 return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN) 466 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); 467 #end[boU] 468 } 469 470 #end[!byte] 471 472 473 474 #if[byte] 475 476 byte _get(int i) { // package-private 477 return unsafe.getByte(address + i); 478 } 479 480 void _put(int i, byte b) { // package-private 481 #if[rw] 482 unsafe.putByte(address + i, b); 483 #else[rw] 484 throw new ReadOnlyBufferException(); 485 #end[rw] 486 } 487 488 // #BIN 489 // 490 // Binary-data access methods for short, char, int, long, float, 491 // and double will be inserted here 492 493 #end[byte] 494 495 }