1 /* 2 * Copyright (c) 2015, 2018, 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 package java.lang.invoke; 26 27 import jdk.internal.misc.Unsafe; 28 import jdk.internal.util.Preconditions; 29 import jdk.internal.vm.annotation.ForceInline; 30 31 import java.nio.ByteBuffer; 32 import java.nio.ReadOnlyBufferException; 33 import java.util.Objects; 34 35 import static java.lang.invoke.MethodHandleStatics.UNSAFE; 36 37 #warn 38 39 final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase { 40 41 static final int ALIGN = $BoxType$.BYTES - 1; 42 43 #if[floatingPoint] 44 @ForceInline 45 static $rawType$ convEndian(boolean big, $type$ v) { 46 $rawType$ rv = $Type$.$type$ToRaw$RawType$Bits(v); 47 return big == BE ? rv : $RawBoxType$.reverseBytes(rv); 48 } 49 50 @ForceInline 51 static $type$ convEndian(boolean big, $rawType$ rv) { 52 rv = big == BE ? rv : $RawBoxType$.reverseBytes(rv); 53 return $Type$.$rawType$BitsTo$Type$(rv); 54 } 55 #else[floatingPoint] 56 @ForceInline 57 static $type$ convEndian(boolean big, $type$ n) { 58 return big == BE ? n : $BoxType$.reverseBytes(n); 59 } 60 #end[floatingPoint] 61 62 63 private static abstract class ByteArrayViewVarHandle extends VarHandle { 64 final boolean be; 65 66 ByteArrayViewVarHandle(VarForm form, boolean be) { 67 super(form); 68 this.be = be; 69 } 70 } 71 72 static final class ArrayHandle extends ByteArrayViewVarHandle { 73 74 ArrayHandle(boolean be) { 75 super(ArrayHandle.FORM, be); 76 } 77 78 @Override 79 final MethodType accessModeTypeUncached(AccessMode accessMode) { 80 return accessMode.at.accessModeType(byte[].class, $type$.class, int.class); 81 } 82 83 @ForceInline 84 static int index(byte[] ba, int index) { 85 return Preconditions.checkIndex(index, ba.length - ALIGN, null); 86 } 87 88 @ForceInline 89 static long address(byte[] ba, int index) { 90 long address = ((long) index) + Unsafe.ARRAY_BYTE_BASE_OFFSET; 91 if ((address & ALIGN) != 0) 92 throw newIllegalStateExceptionForMisalignedAccess(index); 93 return address; 94 } 95 96 @ForceInline 97 static $type$ get(ArrayHandle handle, Object oba, int index) { 98 byte[] ba = (byte[]) oba; 99 #if[floatingPoint] 100 $rawType$ rawValue = UNSAFE.get$RawType$Unaligned( 101 ba, 102 ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET, 103 handle.be); 104 return $Type$.$rawType$BitsTo$Type$(rawValue); 105 #else[floatingPoint] 106 return UNSAFE.get$Type$Unaligned( 107 ba, 108 ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET, 109 handle.be); 110 #end[floatingPoint] 111 } 112 113 @ForceInline 114 static void set(ArrayHandle handle, Object oba, int index, $type$ value) { 115 byte[] ba = (byte[]) oba; 116 #if[floatingPoint] 117 UNSAFE.put$RawType$Unaligned( 118 ba, 119 ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET, 120 $Type$.$type$ToRaw$RawType$Bits(value), 121 handle.be); 122 #else[floatingPoint] 123 UNSAFE.put$RawType$Unaligned( 124 ba, 125 ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET, 126 value, 127 handle.be); 128 #end[floatingPoint] 129 } 130 131 @ForceInline 132 static $type$ getVolatile(ArrayHandle handle, Object oba, int index) { 133 byte[] ba = (byte[]) oba; 134 return convEndian(handle.be, 135 UNSAFE.get$RawType$Volatile( 136 ba, 137 address(ba, index(ba, index)))); 138 } 139 140 @ForceInline 141 static void setVolatile(ArrayHandle handle, Object oba, int index, $type$ value) { 142 byte[] ba = (byte[]) oba; 143 UNSAFE.put$RawType$Volatile( 144 ba, 145 address(ba, index(ba, index)), 146 convEndian(handle.be, value)); 147 } 148 149 @ForceInline 150 static $type$ getAcquire(ArrayHandle handle, Object oba, int index) { 151 byte[] ba = (byte[]) oba; 152 return convEndian(handle.be, 153 UNSAFE.get$RawType$Acquire( 154 ba, 155 address(ba, index(ba, index)))); 156 } 157 158 @ForceInline 159 static void setRelease(ArrayHandle handle, Object oba, int index, $type$ value) { 160 byte[] ba = (byte[]) oba; 161 UNSAFE.put$RawType$Release( 162 ba, 163 address(ba, index(ba, index)), 164 convEndian(handle.be, value)); 165 } 166 167 @ForceInline 168 static $type$ getOpaque(ArrayHandle handle, Object oba, int index) { 169 byte[] ba = (byte[]) oba; 170 return convEndian(handle.be, 171 UNSAFE.get$RawType$Opaque( 172 ba, 173 address(ba, index(ba, index)))); 174 } 175 176 @ForceInline 177 static void setOpaque(ArrayHandle handle, Object oba, int index, $type$ value) { 178 byte[] ba = (byte[]) oba; 179 UNSAFE.put$RawType$Opaque( 180 ba, 181 address(ba, index(ba, index)), 182 convEndian(handle.be, value)); 183 } 184 #if[CAS] 185 186 @ForceInline 187 static boolean compareAndSet(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 188 byte[] ba = (byte[]) oba; 189 return UNSAFE.compareAndSet$RawType$( 190 ba, 191 address(ba, index(ba, index)), 192 convEndian(handle.be, expected), convEndian(handle.be, value)); 193 } 194 195 @ForceInline 196 static $type$ compareAndExchange(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 197 byte[] ba = (byte[]) oba; 198 return convEndian(handle.be, 199 UNSAFE.compareAndExchange$RawType$( 200 ba, 201 address(ba, index(ba, index)), 202 convEndian(handle.be, expected), convEndian(handle.be, value))); 203 } 204 205 @ForceInline 206 static $type$ compareAndExchangeAcquire(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 207 byte[] ba = (byte[]) oba; 208 return convEndian(handle.be, 209 UNSAFE.compareAndExchange$RawType$Acquire( 210 ba, 211 address(ba, index(ba, index)), 212 convEndian(handle.be, expected), convEndian(handle.be, value))); 213 } 214 215 @ForceInline 216 static $type$ compareAndExchangeRelease(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 217 byte[] ba = (byte[]) oba; 218 return convEndian(handle.be, 219 UNSAFE.compareAndExchange$RawType$Release( 220 ba, 221 address(ba, index(ba, index)), 222 convEndian(handle.be, expected), convEndian(handle.be, value))); 223 } 224 225 @ForceInline 226 static boolean weakCompareAndSetPlain(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 227 byte[] ba = (byte[]) oba; 228 return UNSAFE.weakCompareAndSet$RawType$Plain( 229 ba, 230 address(ba, index(ba, index)), 231 convEndian(handle.be, expected), convEndian(handle.be, value)); 232 } 233 234 @ForceInline 235 static boolean weakCompareAndSet(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 236 byte[] ba = (byte[]) oba; 237 return UNSAFE.weakCompareAndSet$RawType$( 238 ba, 239 address(ba, index(ba, index)), 240 convEndian(handle.be, expected), convEndian(handle.be, value)); 241 } 242 243 @ForceInline 244 static boolean weakCompareAndSetAcquire(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 245 byte[] ba = (byte[]) oba; 246 return UNSAFE.weakCompareAndSet$RawType$Acquire( 247 ba, 248 address(ba, index(ba, index)), 249 convEndian(handle.be, expected), convEndian(handle.be, value)); 250 } 251 252 @ForceInline 253 static boolean weakCompareAndSetRelease(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) { 254 byte[] ba = (byte[]) oba; 255 return UNSAFE.weakCompareAndSet$RawType$Release( 256 ba, 257 address(ba, index(ba, index)), 258 convEndian(handle.be, expected), convEndian(handle.be, value)); 259 } 260 261 @ForceInline 262 static $type$ getAndSet(ArrayHandle handle, Object oba, int index, $type$ value) { 263 byte[] ba = (byte[]) oba; 264 return convEndian(handle.be, 265 UNSAFE.getAndSet$RawType$( 266 ba, 267 address(ba, index(ba, index)), 268 convEndian(handle.be, value))); 269 } 270 271 @ForceInline 272 static $type$ getAndSetAcquire(ArrayHandle handle, Object oba, int index, $type$ value) { 273 byte[] ba = (byte[]) oba; 274 return convEndian(handle.be, 275 UNSAFE.getAndSet$RawType$Acquire( 276 ba, 277 address(ba, index(ba, index)), 278 convEndian(handle.be, value))); 279 } 280 281 @ForceInline 282 static $type$ getAndSetRelease(ArrayHandle handle, Object oba, int index, $type$ value) { 283 byte[] ba = (byte[]) oba; 284 return convEndian(handle.be, 285 UNSAFE.getAndSet$RawType$Release( 286 ba, 287 address(ba, index(ba, index)), 288 convEndian(handle.be, value))); 289 } 290 #end[CAS] 291 #if[AtomicAdd] 292 293 @ForceInline 294 static $type$ getAndAdd(ArrayHandle handle, Object oba, int index, $type$ delta) { 295 byte[] ba = (byte[]) oba; 296 if (handle.be == BE) { 297 return UNSAFE.getAndAdd$RawType$( 298 ba, 299 address(ba, index(ba, index)), 300 delta); 301 } else { 302 return getAndAddConvEndianWithCAS(ba, index, delta); 303 } 304 } 305 306 @ForceInline 307 static $type$ getAndAddAcquire(ArrayHandle handle, Object oba, int index, $type$ delta) { 308 byte[] ba = (byte[]) oba; 309 if (handle.be == BE) { 310 return UNSAFE.getAndAdd$RawType$Acquire( 311 ba, 312 address(ba, index(ba, index)), 313 delta); 314 } else { 315 return getAndAddConvEndianWithCAS(ba, index, delta); 316 } 317 } 318 319 @ForceInline 320 static $type$ getAndAddRelease(ArrayHandle handle, Object oba, int index, $type$ delta) { 321 byte[] ba = (byte[]) oba; 322 if (handle.be == BE) { 323 return UNSAFE.getAndAdd$RawType$Release( 324 ba, 325 address(ba, index(ba, index)), 326 delta); 327 } else { 328 return getAndAddConvEndianWithCAS(ba, index, delta); 329 } 330 } 331 332 @ForceInline 333 static $type$ getAndAddConvEndianWithCAS(byte[] ba, int index, $type$ delta) { 334 $type$ nativeExpectedValue, expectedValue; 335 long offset = address(ba, index(ba, index)); 336 do { 337 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset); 338 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 339 } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset, 340 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue + delta))); 341 return expectedValue; 342 } 343 #end[AtomicAdd] 344 #if[Bitwise] 345 346 @ForceInline 347 static $type$ getAndBitwiseOr(ArrayHandle handle, Object oba, int index, $type$ value) { 348 byte[] ba = (byte[]) oba; 349 if (handle.be == BE) { 350 return UNSAFE.getAndBitwiseOr$RawType$( 351 ba, 352 address(ba, index(ba, index)), 353 value); 354 } else { 355 return getAndBitwiseOrConvEndianWithCAS(ba, index, value); 356 } 357 } 358 359 @ForceInline 360 static $type$ getAndBitwiseOrRelease(ArrayHandle handle, Object oba, int index, $type$ value) { 361 byte[] ba = (byte[]) oba; 362 if (handle.be == BE) { 363 return UNSAFE.getAndBitwiseOr$RawType$Release( 364 ba, 365 address(ba, index(ba, index)), 366 value); 367 } else { 368 return getAndBitwiseOrConvEndianWithCAS(ba, index, value); 369 } 370 } 371 372 @ForceInline 373 static $type$ getAndBitwiseOrAcquire(ArrayHandle handle, Object oba, int index, $type$ value) { 374 byte[] ba = (byte[]) oba; 375 if (handle.be == BE) { 376 return UNSAFE.getAndBitwiseOr$RawType$Acquire( 377 ba, 378 address(ba, index(ba, index)), 379 value); 380 } else { 381 return getAndBitwiseOrConvEndianWithCAS(ba, index, value); 382 } 383 } 384 385 @ForceInline 386 static $type$ getAndBitwiseOrConvEndianWithCAS(byte[] ba, int index, $type$ value) { 387 $type$ nativeExpectedValue, expectedValue; 388 long offset = address(ba, index(ba, index)); 389 do { 390 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset); 391 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 392 } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset, 393 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue | value))); 394 return expectedValue; 395 } 396 397 @ForceInline 398 static $type$ getAndBitwiseAnd(ArrayHandle handle, Object oba, int index, $type$ value) { 399 byte[] ba = (byte[]) oba; 400 if (handle.be == BE) { 401 return UNSAFE.getAndBitwiseAnd$RawType$( 402 ba, 403 address(ba, index(ba, index)), 404 value); 405 } else { 406 return getAndBitwiseAndConvEndianWithCAS(ba, index, value); 407 } 408 } 409 410 @ForceInline 411 static $type$ getAndBitwiseAndRelease(ArrayHandle handle, Object oba, int index, $type$ value) { 412 byte[] ba = (byte[]) oba; 413 if (handle.be == BE) { 414 return UNSAFE.getAndBitwiseAnd$RawType$Release( 415 ba, 416 address(ba, index(ba, index)), 417 value); 418 } else { 419 return getAndBitwiseAndConvEndianWithCAS(ba, index, value); 420 } 421 } 422 423 @ForceInline 424 static $type$ getAndBitwiseAndAcquire(ArrayHandle handle, Object oba, int index, $type$ value) { 425 byte[] ba = (byte[]) oba; 426 if (handle.be == BE) { 427 return UNSAFE.getAndBitwiseAnd$RawType$Acquire( 428 ba, 429 address(ba, index(ba, index)), 430 value); 431 } else { 432 return getAndBitwiseAndConvEndianWithCAS(ba, index, value); 433 } 434 } 435 436 @ForceInline 437 static $type$ getAndBitwiseAndConvEndianWithCAS(byte[] ba, int index, $type$ value) { 438 $type$ nativeExpectedValue, expectedValue; 439 long offset = address(ba, index(ba, index)); 440 do { 441 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset); 442 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 443 } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset, 444 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue & value))); 445 return expectedValue; 446 } 447 448 @ForceInline 449 static $type$ getAndBitwiseXor(ArrayHandle handle, Object oba, int index, $type$ value) { 450 byte[] ba = (byte[]) oba; 451 if (handle.be == BE) { 452 return UNSAFE.getAndBitwiseXor$RawType$( 453 ba, 454 address(ba, index(ba, index)), 455 value); 456 } else { 457 return getAndBitwiseXorConvEndianWithCAS(ba, index, value); 458 } 459 } 460 461 @ForceInline 462 static $type$ getAndBitwiseXorRelease(ArrayHandle handle, Object oba, int index, $type$ value) { 463 byte[] ba = (byte[]) oba; 464 if (handle.be == BE) { 465 return UNSAFE.getAndBitwiseXor$RawType$Release( 466 ba, 467 address(ba, index(ba, index)), 468 value); 469 } else { 470 return getAndBitwiseXorConvEndianWithCAS(ba, index, value); 471 } 472 } 473 474 @ForceInline 475 static $type$ getAndBitwiseXorAcquire(ArrayHandle handle, Object oba, int index, $type$ value) { 476 byte[] ba = (byte[]) oba; 477 if (handle.be == BE) { 478 return UNSAFE.getAndBitwiseXor$RawType$Acquire( 479 ba, 480 address(ba, index(ba, index)), 481 value); 482 } else { 483 return getAndBitwiseXorConvEndianWithCAS(ba, index, value); 484 } 485 } 486 487 @ForceInline 488 static $type$ getAndBitwiseXorConvEndianWithCAS(byte[] ba, int index, $type$ value) { 489 $type$ nativeExpectedValue, expectedValue; 490 long offset = address(ba, index(ba, index)); 491 do { 492 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset); 493 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 494 } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset, 495 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue ^ value))); 496 return expectedValue; 497 } 498 #end[Bitwise] 499 500 static final VarForm FORM = new VarForm(ArrayHandle.class, byte[].class, $type$.class, int.class); 501 } 502 503 504 static final class ByteBufferHandle extends ByteArrayViewVarHandle { 505 506 ByteBufferHandle(boolean be) { 507 super(ByteBufferHandle.FORM, be); 508 } 509 510 @Override 511 final MethodType accessModeTypeUncached(AccessMode accessMode) { 512 return accessMode.at.accessModeType(ByteBuffer.class, $type$.class, int.class); 513 } 514 515 @ForceInline 516 static int index(ByteBuffer bb, int index) { 517 return Preconditions.checkIndex(index, UNSAFE.getInt(bb, BUFFER_LIMIT) - ALIGN, null); 518 } 519 520 @ForceInline 521 static int indexRO(ByteBuffer bb, int index) { 522 if (UNSAFE.getBoolean(bb, BYTE_BUFFER_IS_READ_ONLY)) 523 throw new ReadOnlyBufferException(); 524 return Preconditions.checkIndex(index, UNSAFE.getInt(bb, BUFFER_LIMIT) - ALIGN, null); 525 } 526 527 @ForceInline 528 static long address(ByteBuffer bb, int index) { 529 long address = ((long) index) + UNSAFE.getLong(bb, BUFFER_ADDRESS); 530 if ((address & ALIGN) != 0) 531 throw newIllegalStateExceptionForMisalignedAccess(index); 532 return address; 533 } 534 535 @ForceInline 536 static $type$ get(ByteBufferHandle handle, Object obb, int index) { 537 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 538 #if[floatingPoint] 539 $rawType$ rawValue = UNSAFE.get$RawType$Unaligned( 540 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 541 ((long) index(bb, index)) + UNSAFE.getLong(bb, BUFFER_ADDRESS), 542 handle.be); 543 return $Type$.$rawType$BitsTo$Type$(rawValue); 544 #else[floatingPoint] 545 return UNSAFE.get$Type$Unaligned( 546 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 547 ((long) index(bb, index)) + UNSAFE.getLong(bb, BUFFER_ADDRESS), 548 handle.be); 549 #end[floatingPoint] 550 } 551 552 @ForceInline 553 static void set(ByteBufferHandle handle, Object obb, int index, $type$ value) { 554 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 555 #if[floatingPoint] 556 UNSAFE.put$RawType$Unaligned( 557 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 558 ((long) indexRO(bb, index)) + UNSAFE.getLong(bb, BUFFER_ADDRESS), 559 $Type$.$type$ToRaw$RawType$Bits(value), 560 handle.be); 561 #else[floatingPoint] 562 UNSAFE.put$Type$Unaligned( 563 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 564 ((long) indexRO(bb, index)) + UNSAFE.getLong(bb, BUFFER_ADDRESS), 565 value, 566 handle.be); 567 #end[floatingPoint] 568 } 569 570 @ForceInline 571 static $type$ getVolatile(ByteBufferHandle handle, Object obb, int index) { 572 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 573 return convEndian(handle.be, 574 UNSAFE.get$RawType$Volatile( 575 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 576 address(bb, index(bb, index)))); 577 } 578 579 @ForceInline 580 static void setVolatile(ByteBufferHandle handle, Object obb, int index, $type$ value) { 581 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 582 UNSAFE.put$RawType$Volatile( 583 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 584 address(bb, indexRO(bb, index)), 585 convEndian(handle.be, value)); 586 } 587 588 @ForceInline 589 static $type$ getAcquire(ByteBufferHandle handle, Object obb, int index) { 590 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 591 return convEndian(handle.be, 592 UNSAFE.get$RawType$Acquire( 593 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 594 address(bb, index(bb, index)))); 595 } 596 597 @ForceInline 598 static void setRelease(ByteBufferHandle handle, Object obb, int index, $type$ value) { 599 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 600 UNSAFE.put$RawType$Release( 601 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 602 address(bb, indexRO(bb, index)), 603 convEndian(handle.be, value)); 604 } 605 606 @ForceInline 607 static $type$ getOpaque(ByteBufferHandle handle, Object obb, int index) { 608 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 609 return convEndian(handle.be, 610 UNSAFE.get$RawType$Opaque( 611 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 612 address(bb, index(bb, index)))); 613 } 614 615 @ForceInline 616 static void setOpaque(ByteBufferHandle handle, Object obb, int index, $type$ value) { 617 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 618 UNSAFE.put$RawType$Opaque( 619 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 620 address(bb, indexRO(bb, index)), 621 convEndian(handle.be, value)); 622 } 623 #if[CAS] 624 625 @ForceInline 626 static boolean compareAndSet(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 627 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 628 return UNSAFE.compareAndSet$RawType$( 629 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 630 address(bb, indexRO(bb, index)), 631 convEndian(handle.be, expected), convEndian(handle.be, value)); 632 } 633 634 @ForceInline 635 static $type$ compareAndExchange(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 636 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 637 return convEndian(handle.be, 638 UNSAFE.compareAndExchange$RawType$( 639 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 640 address(bb, indexRO(bb, index)), 641 convEndian(handle.be, expected), convEndian(handle.be, value))); 642 } 643 644 @ForceInline 645 static $type$ compareAndExchangeAcquire(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 646 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 647 return convEndian(handle.be, 648 UNSAFE.compareAndExchange$RawType$Acquire( 649 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 650 address(bb, indexRO(bb, index)), 651 convEndian(handle.be, expected), convEndian(handle.be, value))); 652 } 653 654 @ForceInline 655 static $type$ compareAndExchangeRelease(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 656 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 657 return convEndian(handle.be, 658 UNSAFE.compareAndExchange$RawType$Release( 659 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 660 address(bb, indexRO(bb, index)), 661 convEndian(handle.be, expected), convEndian(handle.be, value))); 662 } 663 664 @ForceInline 665 static boolean weakCompareAndSetPlain(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 666 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 667 return UNSAFE.weakCompareAndSet$RawType$Plain( 668 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 669 address(bb, indexRO(bb, index)), 670 convEndian(handle.be, expected), convEndian(handle.be, value)); 671 } 672 673 @ForceInline 674 static boolean weakCompareAndSet(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 675 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 676 return UNSAFE.weakCompareAndSet$RawType$( 677 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 678 address(bb, indexRO(bb, index)), 679 convEndian(handle.be, expected), convEndian(handle.be, value)); 680 } 681 682 @ForceInline 683 static boolean weakCompareAndSetAcquire(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 684 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 685 return UNSAFE.weakCompareAndSet$RawType$Acquire( 686 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 687 address(bb, indexRO(bb, index)), 688 convEndian(handle.be, expected), convEndian(handle.be, value)); 689 } 690 691 @ForceInline 692 static boolean weakCompareAndSetRelease(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) { 693 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 694 return UNSAFE.weakCompareAndSet$RawType$Release( 695 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 696 address(bb, indexRO(bb, index)), 697 convEndian(handle.be, expected), convEndian(handle.be, value)); 698 } 699 700 @ForceInline 701 static $type$ getAndSet(ByteBufferHandle handle, Object obb, int index, $type$ value) { 702 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 703 return convEndian(handle.be, 704 UNSAFE.getAndSet$RawType$( 705 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 706 address(bb, indexRO(bb, index)), 707 convEndian(handle.be, value))); 708 } 709 710 @ForceInline 711 static $type$ getAndSetAcquire(ByteBufferHandle handle, Object obb, int index, $type$ value) { 712 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 713 return convEndian(handle.be, 714 UNSAFE.getAndSet$RawType$Acquire( 715 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 716 address(bb, indexRO(bb, index)), 717 convEndian(handle.be, value))); 718 } 719 720 @ForceInline 721 static $type$ getAndSetRelease(ByteBufferHandle handle, Object obb, int index, $type$ value) { 722 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 723 return convEndian(handle.be, 724 UNSAFE.getAndSet$RawType$Release( 725 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 726 address(bb, indexRO(bb, index)), 727 convEndian(handle.be, value))); 728 } 729 #end[CAS] 730 #if[AtomicAdd] 731 732 @ForceInline 733 static $type$ getAndAdd(ByteBufferHandle handle, Object obb, int index, $type$ delta) { 734 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 735 if (handle.be == BE) { 736 return UNSAFE.getAndAdd$RawType$( 737 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 738 address(bb, indexRO(bb, index)), 739 delta); 740 } else { 741 return getAndAddConvEndianWithCAS(bb, index, delta); 742 } 743 } 744 745 @ForceInline 746 static $type$ getAndAddAcquire(ByteBufferHandle handle, Object obb, int index, $type$ delta) { 747 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 748 if (handle.be == BE) { 749 return UNSAFE.getAndAdd$RawType$Acquire( 750 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 751 address(bb, indexRO(bb, index)), 752 delta); 753 } else { 754 return getAndAddConvEndianWithCAS(bb, index, delta); 755 } 756 } 757 758 @ForceInline 759 static $type$ getAndAddRelease(ByteBufferHandle handle, Object obb, int index, $type$ delta) { 760 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 761 if (handle.be == BE) { 762 return UNSAFE.getAndAdd$RawType$Release( 763 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 764 address(bb, indexRO(bb, index)), 765 delta); 766 } else { 767 return getAndAddConvEndianWithCAS(bb, index, delta); 768 } 769 } 770 771 @ForceInline 772 static $type$ getAndAddConvEndianWithCAS(ByteBuffer bb, int index, $type$ delta) { 773 $type$ nativeExpectedValue, expectedValue; 774 Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB); 775 long offset = address(bb, indexRO(bb, index)); 776 do { 777 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset); 778 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 779 } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset, 780 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue + delta))); 781 return expectedValue; 782 } 783 #end[AtomicAdd] 784 #if[Bitwise] 785 786 @ForceInline 787 static $type$ getAndBitwiseOr(ByteBufferHandle handle, Object obb, int index, $type$ value) { 788 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 789 if (handle.be == BE) { 790 return UNSAFE.getAndBitwiseOr$RawType$( 791 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 792 address(bb, indexRO(bb, index)), 793 value); 794 } else { 795 return getAndBitwiseOrConvEndianWithCAS(bb, index, value); 796 } 797 } 798 799 @ForceInline 800 static $type$ getAndBitwiseOrRelease(ByteBufferHandle handle, Object obb, int index, $type$ value) { 801 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 802 if (handle.be == BE) { 803 return UNSAFE.getAndBitwiseOr$RawType$Release( 804 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 805 address(bb, indexRO(bb, index)), 806 value); 807 } else { 808 return getAndBitwiseOrConvEndianWithCAS(bb, index, value); 809 } 810 } 811 812 @ForceInline 813 static $type$ getAndBitwiseOrAcquire(ByteBufferHandle handle, Object obb, int index, $type$ value) { 814 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 815 if (handle.be == BE) { 816 return UNSAFE.getAndBitwiseOr$RawType$Acquire( 817 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 818 address(bb, indexRO(bb, index)), 819 value); 820 } else { 821 return getAndBitwiseOrConvEndianWithCAS(bb, index, value); 822 } 823 } 824 825 @ForceInline 826 static $type$ getAndBitwiseOrConvEndianWithCAS(ByteBuffer bb, int index, $type$ value) { 827 $type$ nativeExpectedValue, expectedValue; 828 Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB); 829 long offset = address(bb, indexRO(bb, index)); 830 do { 831 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset); 832 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 833 } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset, 834 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue | value))); 835 return expectedValue; 836 } 837 838 @ForceInline 839 static $type$ getAndBitwiseAnd(ByteBufferHandle handle, Object obb, int index, $type$ value) { 840 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 841 if (handle.be == BE) { 842 return UNSAFE.getAndBitwiseAnd$RawType$( 843 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 844 address(bb, indexRO(bb, index)), 845 value); 846 } else { 847 return getAndBitwiseAndConvEndianWithCAS(bb, index, value); 848 } 849 } 850 851 @ForceInline 852 static $type$ getAndBitwiseAndRelease(ByteBufferHandle handle, Object obb, int index, $type$ value) { 853 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 854 if (handle.be == BE) { 855 return UNSAFE.getAndBitwiseAnd$RawType$Release( 856 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 857 address(bb, indexRO(bb, index)), 858 value); 859 } else { 860 return getAndBitwiseAndConvEndianWithCAS(bb, index, value); 861 } 862 } 863 864 @ForceInline 865 static $type$ getAndBitwiseAndAcquire(ByteBufferHandle handle, Object obb, int index, $type$ value) { 866 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 867 if (handle.be == BE) { 868 return UNSAFE.getAndBitwiseAnd$RawType$Acquire( 869 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 870 address(bb, indexRO(bb, index)), 871 value); 872 } else { 873 return getAndBitwiseAndConvEndianWithCAS(bb, index, value); 874 } 875 } 876 877 @ForceInline 878 static $type$ getAndBitwiseAndConvEndianWithCAS(ByteBuffer bb, int index, $type$ value) { 879 $type$ nativeExpectedValue, expectedValue; 880 Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB); 881 long offset = address(bb, indexRO(bb, index)); 882 do { 883 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset); 884 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 885 } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset, 886 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue & value))); 887 return expectedValue; 888 } 889 890 891 @ForceInline 892 static $type$ getAndBitwiseXor(ByteBufferHandle handle, Object obb, int index, $type$ value) { 893 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 894 if (handle.be == BE) { 895 return UNSAFE.getAndBitwiseXor$RawType$( 896 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 897 address(bb, indexRO(bb, index)), 898 value); 899 } else { 900 return getAndBitwiseXorConvEndianWithCAS(bb, index, value); 901 } 902 } 903 904 @ForceInline 905 static $type$ getAndBitwiseXorRelease(ByteBufferHandle handle, Object obb, int index, $type$ value) { 906 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 907 if (handle.be == BE) { 908 return UNSAFE.getAndBitwiseXor$RawType$Release( 909 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 910 address(bb, indexRO(bb, index)), 911 value); 912 } else { 913 return getAndBitwiseXorConvEndianWithCAS(bb, index, value); 914 } 915 } 916 917 @ForceInline 918 static $type$ getAndBitwiseXorAcquire(ByteBufferHandle handle, Object obb, int index, $type$ value) { 919 ByteBuffer bb = (ByteBuffer) Objects.requireNonNull(obb); 920 if (handle.be == BE) { 921 return UNSAFE.getAndBitwiseXor$RawType$Acquire( 922 UNSAFE.getObject(bb, BYTE_BUFFER_HB), 923 address(bb, indexRO(bb, index)), 924 value); 925 } else { 926 return getAndBitwiseXorConvEndianWithCAS(bb, index, value); 927 } 928 } 929 930 @ForceInline 931 static $type$ getAndBitwiseXorConvEndianWithCAS(ByteBuffer bb, int index, $type$ value) { 932 $type$ nativeExpectedValue, expectedValue; 933 Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB); 934 long offset = address(bb, indexRO(bb, index)); 935 do { 936 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset); 937 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue); 938 } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset, 939 nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue ^ value))); 940 return expectedValue; 941 } 942 #end[Bitwise] 943 944 static final VarForm FORM = new VarForm(ByteBufferHandle.class, ByteBuffer.class, $type$.class, int.class); 945 } 946 }