128 } 129 130 @Test 131 public void testArrayOffHeapDirect() { 132 int size = 10; 133 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 134 try { 135 for (int c = 0; c < ITERS; c++) { 136 for (int i = 0; i < size; i++) { 137 testAccess((((long) i) << ARRAY_SHIFT) + address); 138 } 139 } 140 } finally { 141 UNSAFE.freeMemory(address); 142 } 143 } 144 145 static void testAccess(Object base, long offset) { 146 // Plain 147 { 148 UNSAFE.putByte(base, offset, (byte)1); 149 byte x = UNSAFE.getByte(base, offset); 150 assertEquals(x, (byte)1, "set byte value"); 151 } 152 153 // Volatile 154 { 155 UNSAFE.putByteVolatile(base, offset, (byte)2); 156 byte x = UNSAFE.getByteVolatile(base, offset); 157 assertEquals(x, (byte)2, "putVolatile byte value"); 158 } 159 160 161 // Lazy 162 { 163 UNSAFE.putByteRelease(base, offset, (byte)1); 164 byte x = UNSAFE.getByteAcquire(base, offset); 165 assertEquals(x, (byte)1, "putRelease byte value"); 166 } 167 168 // Opaque 169 { 170 UNSAFE.putByteOpaque(base, offset, (byte)2); 171 byte x = UNSAFE.getByteOpaque(base, offset); 172 assertEquals(x, (byte)2, "putOpaque byte value"); 173 } 174 175 176 177 } 178 179 static void testAccess(long address) { 180 // Plain 181 { 182 UNSAFE.putByte(address, (byte)1); 183 byte x = UNSAFE.getByte(address); 184 assertEquals(x, (byte)1, "set byte value"); 185 } 186 } 187 } 188 | 128 } 129 130 @Test 131 public void testArrayOffHeapDirect() { 132 int size = 10; 133 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 134 try { 135 for (int c = 0; c < ITERS; c++) { 136 for (int i = 0; i < size; i++) { 137 testAccess((((long) i) << ARRAY_SHIFT) + address); 138 } 139 } 140 } finally { 141 UNSAFE.freeMemory(address); 142 } 143 } 144 145 static void testAccess(Object base, long offset) { 146 // Plain 147 { 148 UNSAFE.putByte(base, offset, (byte)0x01); 149 byte x = UNSAFE.getByte(base, offset); 150 assertEquals(x, (byte)0x01, "set byte value"); 151 } 152 153 // Volatile 154 { 155 UNSAFE.putByteVolatile(base, offset, (byte)0x23); 156 byte x = UNSAFE.getByteVolatile(base, offset); 157 assertEquals(x, (byte)0x23, "putVolatile byte value"); 158 } 159 160 161 // Lazy 162 { 163 UNSAFE.putByteRelease(base, offset, (byte)0x01); 164 byte x = UNSAFE.getByteAcquire(base, offset); 165 assertEquals(x, (byte)0x01, "putRelease byte value"); 166 } 167 168 // Opaque 169 { 170 UNSAFE.putByteOpaque(base, offset, (byte)0x23); 171 byte x = UNSAFE.getByteOpaque(base, offset); 172 assertEquals(x, (byte)0x23, "putOpaque byte value"); 173 } 174 175 176 UNSAFE.putByte(base, offset, (byte)0x01); 177 178 // Compare 179 { 180 boolean r = UNSAFE.compareAndSwapByte(base, offset, (byte)0x01, (byte)0x23); 181 assertEquals(r, true, "success compareAndSwap byte"); 182 byte x = UNSAFE.getByte(base, offset); 183 assertEquals(x, (byte)0x23, "success compareAndSwap byte value"); 184 } 185 186 { 187 boolean r = UNSAFE.compareAndSwapByte(base, offset, (byte)0x01, (byte)0x45); 188 assertEquals(r, false, "failing compareAndSwap byte"); 189 byte x = UNSAFE.getByte(base, offset); 190 assertEquals(x, (byte)0x23, "failing compareAndSwap byte value"); 191 } 192 193 // Advanced compare 194 { 195 byte r = UNSAFE.compareAndExchangeByteVolatile(base, offset, (byte)0x23, (byte)0x01); 196 assertEquals(r, (byte)0x23, "success compareAndExchangeVolatile byte"); 197 byte x = UNSAFE.getByte(base, offset); 198 assertEquals(x, (byte)0x01, "success compareAndExchangeVolatile byte value"); 199 } 200 201 { 202 byte r = UNSAFE.compareAndExchangeByteVolatile(base, offset, (byte)0x23, (byte)0x45); 203 assertEquals(r, (byte)0x01, "failing compareAndExchangeVolatile byte"); 204 byte x = UNSAFE.getByte(base, offset); 205 assertEquals(x, (byte)0x01, "failing compareAndExchangeVolatile byte value"); 206 } 207 208 { 209 byte r = UNSAFE.compareAndExchangeByteAcquire(base, offset, (byte)0x01, (byte)0x23); 210 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte"); 211 byte x = UNSAFE.getByte(base, offset); 212 assertEquals(x, (byte)0x23, "success compareAndExchangeAcquire byte value"); 213 } 214 215 { 216 byte r = UNSAFE.compareAndExchangeByteAcquire(base, offset, (byte)0x01, (byte)0x45); 217 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte"); 218 byte x = UNSAFE.getByte(base, offset); 219 assertEquals(x, (byte)0x23, "failing compareAndExchangeAcquire byte value"); 220 } 221 222 { 223 byte r = UNSAFE.compareAndExchangeByteRelease(base, offset, (byte)0x23, (byte)0x01); 224 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte"); 225 byte x = UNSAFE.getByte(base, offset); 226 assertEquals(x, (byte)0x01, "success compareAndExchangeRelease byte value"); 227 } 228 229 { 230 byte r = UNSAFE.compareAndExchangeByteRelease(base, offset, (byte)0x23, (byte)0x45); 231 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte"); 232 byte x = UNSAFE.getByte(base, offset); 233 assertEquals(x, (byte)0x01, "failing compareAndExchangeRelease byte value"); 234 } 235 236 { 237 boolean success = false; 238 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 239 success = UNSAFE.weakCompareAndSwapByte(base, offset, (byte)0x01, (byte)0x23); 240 } 241 assertEquals(success, true, "weakCompareAndSwap byte"); 242 byte x = UNSAFE.getByte(base, offset); 243 assertEquals(x, (byte)0x23, "weakCompareAndSwap byte value"); 244 } 245 246 { 247 boolean success = false; 248 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 249 success = UNSAFE.weakCompareAndSwapByteAcquire(base, offset, (byte)0x23, (byte)0x01); 250 } 251 assertEquals(success, true, "weakCompareAndSwapAcquire byte"); 252 byte x = UNSAFE.getByte(base, offset); 253 assertEquals(x, (byte)0x01, "weakCompareAndSwapAcquire byte"); 254 } 255 256 { 257 boolean success = false; 258 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 259 success = UNSAFE.weakCompareAndSwapByteRelease(base, offset, (byte)0x01, (byte)0x23); 260 } 261 assertEquals(success, true, "weakCompareAndSwapRelease byte"); 262 byte x = UNSAFE.getByte(base, offset); 263 assertEquals(x, (byte)0x23, "weakCompareAndSwapRelease byte"); 264 } 265 266 { 267 boolean success = false; 268 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 269 success = UNSAFE.weakCompareAndSwapByteVolatile(base, offset, (byte)0x23, (byte)0x01); 270 } 271 assertEquals(success, true, "weakCompareAndSwapVolatile byte"); 272 byte x = UNSAFE.getByte(base, offset); 273 assertEquals(x, (byte)0x01, "weakCompareAndSwapVolatile byte"); 274 } 275 276 UNSAFE.putByte(base, offset, (byte)0x23); 277 278 // Compare set and get 279 { 280 byte o = UNSAFE.getAndSetByte(base, offset, (byte)0x01); 281 assertEquals(o, (byte)0x23, "getAndSet byte"); 282 byte x = UNSAFE.getByte(base, offset); 283 assertEquals(x, (byte)0x01, "getAndSet byte value"); 284 } 285 286 UNSAFE.putByte(base, offset, (byte)0x01); 287 288 // get and add, add and get 289 { 290 byte o = UNSAFE.getAndAddByte(base, offset, (byte)0x23); 291 assertEquals(o, (byte)0x01, "getAndAdd byte"); 292 byte x = UNSAFE.getByte(base, offset); 293 assertEquals(x, (byte)((byte)0x01 + (byte)0x23), "getAndAdd byte"); 294 } 295 } 296 297 static void testAccess(long address) { 298 // Plain 299 { 300 UNSAFE.putByte(address, (byte)0x01); 301 byte x = UNSAFE.getByte(address); 302 assertEquals(x, (byte)0x01, "set byte value"); 303 } 304 } 305 } 306 |