Java Code Examples for sun.misc.Unsafe#ARRAY_INT_BASE_OFFSET

The following examples show how to use sun.misc.Unsafe#ARRAY_INT_BASE_OFFSET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcurrentMonoidIntTable.java    From metrics with Apache License 2.0 6 votes vote down vote up
protected void apply(final String[] tags, final long value, final long timestamp) {

    final long index = index(tags, false);

    // Failed to grow table, silently drop the measurement
    if (index == NOT_FOUND) {
      return;
    }

    // Decode table index and slot index from a long.
    // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
    // This logic is replicated in multiple places for performance reasons.
    int tableIndex = (int) ((index & TABLE_MASK) >> 32);
    int slotIndex = (int) (index & SLOT_MASK);
    int[] table = tables.get(tableIndex);

    final long base = Unsafe.ARRAY_INT_BASE_OFFSET + slotIndex * Unsafe.ARRAY_INT_INDEX_SCALE;
    unsafe.putLongVolatile(table, base + Unsafe.ARRAY_LONG_INDEX_SCALE, timestamp);

    combine(table, base, value);
  }
 
Example 2
Source File: BasicHistogramAggregator.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Override
public boolean next() {
  i++;
  if (i >= tagSets.length || tagSets[i] == null) {
    return false;
  }

  long index = index(tagSets[i], true);

  if (NOT_FOUND == index) {
    LOGGER.error("Missing index on Read. Tags: {}. Concurrency error or bug",
        Arrays.asList(tagSets[i]));
    return false;
  }

  // Decode table index and slot index from long.
  // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
  // This logic is replicated in multiple places for performance reasons.
  int tableIndex = (int) ((index & TABLE_MASK) >> 32);
  int slotIndex = (int) (index & SLOT_MASK);

  table = tables.get(tableIndex);
  base = Unsafe.ARRAY_INT_BASE_OFFSET + slotIndex * Unsafe.ARRAY_INT_INDEX_SCALE;
  return true;
}
 
Example 3
Source File: RenderBuffer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 4
Source File: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 5
Source File: RenderBuffer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 6
Source File: RenderBuffer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 7
Source File: RenderBuffer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 8
Source File: RenderBuffer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 9
Source File: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 10
Source File: RenderBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 11
Source File: BitBlt.java    From trufflesqueak with MIT License 5 votes vote down vote up
private boolean loadBitBltDestForm() {
    if (!(isPointers(destForm) && slotSizeOf(destForm) >= 4)) {
        return false;
    }
    final Object destBitsValue = fetchPointerofObject(FORM.BITS, destForm);
    destWidth = fetchIntegerofObject(FORM.WIDTH, destForm);
    destHeight = fetchIntegerofObject(FORM.HEIGHT, destForm);
    if (!(destWidth >= 0 && destHeight >= 0)) {
        return false;
    }
    destDepth = fetchIntegerofObject(FORM.DEPTH, destForm);
    if (!(destMSB = destDepth > 0)) {
        destDepth = 0 - destDepth;
    }
    if (!isWordsOrBytes(destBitsValue)) {
        if (destBitsValue instanceof Long) {
            throw SqueakException.create("Not supported: Query for actual surface dimensions");
        } else {
            return false;
        }
    }
    destPPW = div(32, destDepth);
    destPitch = div(destWidth + destPPW - 1, destPPW) * 4;
    final NativeObject destBitsNative = (NativeObject) destBitsValue;
    final long destBitsSize;
    if (isWords(destBitsNative)) {
        destBits = destBitsNative.getIntStorage();
        destBitsSize = destBitsNative.getIntLength() * Integer.BYTES;
        destBitsBaseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
        destBitsIndexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
    } else {
        destBits = destBitsNative.getByteStorage();
        destBitsSize = destBitsNative.getByteLength();
        destBitsBaseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
        destBitsIndexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE * Integer.BYTES;
    }
    return destBitsSize >= destPitch * destHeight;
}
 
Example 12
Source File: RenderBuffer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 13
Source File: RenderBuffer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 14
Source File: RenderBuffer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 15
Source File: RenderBuffer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example 16
Source File: VirtualArrayNode.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static int entryIndexForOffset(long constantOffset, JavaKind expectedEntryKind, ResolvedJavaType componentType, int length) {
    int baseOffset;
    int indexScale;
    switch (componentType.getJavaKind()) {
        case Boolean:
            baseOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
            break;
        case Byte:
            baseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE;
            break;
        case Short:
            baseOffset = Unsafe.ARRAY_SHORT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_SHORT_INDEX_SCALE;
            break;
        case Char:
            baseOffset = Unsafe.ARRAY_CHAR_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_CHAR_INDEX_SCALE;
            break;
        case Int:
            baseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
            break;
        case Long:
            baseOffset = Unsafe.ARRAY_LONG_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_LONG_INDEX_SCALE;
            break;
        case Float:
            baseOffset = Unsafe.ARRAY_FLOAT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_FLOAT_INDEX_SCALE;
            break;
        case Double:
            baseOffset = Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
            break;
        case Object:
            baseOffset = Unsafe.ARRAY_OBJECT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_OBJECT_INDEX_SCALE;
            break;
        default:
            return -1;
    }
    long offset;
    if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN && componentType.isPrimitive()) {
        // On big endian, we do just get expect the type be right aligned in this memory slot
        offset = constantOffset - (componentType.getJavaKind().getByteCount() - Math.min(componentType.getJavaKind().getByteCount(), 4 + expectedEntryKind.getByteCount()));
    } else {
        offset = constantOffset;
    }
    long index = offset - baseOffset;
    if (index % indexScale != 0) {
        return -1;
    }
    long elementIndex = index / indexScale;
    if (elementIndex < 0 || elementIndex >= length) {
        return -1;
    }
    return (int) elementIndex;
}
 
Example 17
Source File: BitBlt.java    From trufflesqueak with MIT License 4 votes vote down vote up
private boolean loadBitBltSourceForm() {
    if (!(isPointers(sourceForm) && slotSizeOf(sourceForm) >= 4)) {
        return false;
    }
    final Object sourceBitsValue = fetchPointerofObject(FORM.BITS, sourceForm);
    sourceWidth = fetchIntOrFloatofObject(FORM.WIDTH, sourceForm);
    sourceHeight = fetchIntOrFloatofObject(FORM.HEIGHT, sourceForm);
    if (!(sourceWidth >= 0 && sourceHeight >= 0)) {
        return false;
    }
    sourceDepth = fetchIntegerofObject(FORM.DEPTH, sourceForm);
    if (!(sourceMSB = sourceDepth > 0)) {
        sourceDepth = 0 - sourceDepth;
    }
    if (!isWordsOrBytes(sourceBitsValue)) {
        if (sourceBitsValue instanceof Long) {
            throw SqueakException.create("Not supported: Query for actual surface dimensions");
        } else {
            return false;
        }
    }
    sourcePPW = div(32, sourceDepth);
    sourcePitch = div(sourceWidth + sourcePPW - 1, sourcePPW) * 4;
    final NativeObject sourceBitsNative = (NativeObject) sourceBitsValue;
    if (isWords(sourceBitsNative)) {
        final int[] ints = sourceBitsNative.getIntStorage();
        sourceBits = ints;
        sourceBitsBaseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
        sourceBitsIndexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
        return ints.length * Integer.BYTES >= sourcePitch * sourceHeight;
    } else {
        final byte[] bytes = sourceBitsNative.getByteStorage();
        if (bytes.length >= sourcePitch * sourceHeight) {
            sourceBits = bytes;
            sourceBitsBaseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
            sourceBitsIndexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE * Integer.BYTES;
            return true;
        } else {
            return false;
        }
    }
}
 
Example 18
Source File: ConcurrentMonoidIntTable.java    From metrics with Apache License 2.0 4 votes vote down vote up
protected long index(String[] tags, boolean isReading) {
  final long key = hashCode(tags);
  for (int tableIndex = 0; tableIndex < tables.size(); tableIndex++) {
    int[] table = tables.get(tableIndex);
    AtomicInteger recordCount = recordCounts.get(tableIndex);
    int tableCapacity = tableCapacities.get(tableIndex);
    final int slot = getSlot(key, table.length / recordSize);
    final int startIndex = slot * recordSize;
    int slotIndex = startIndex;
    for (; ; ) {
      long offset = Unsafe.ARRAY_INT_BASE_OFFSET + slotIndex * Unsafe.ARRAY_INT_INDEX_SCALE;
      long candidate = unsafe.getLongVolatile(table, offset);

      // check if we found our key
      if (key == candidate) {
        // Encode table index and slot index into a long.
        // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
        // This logic is replicated in multiple places for performance reasons.
        return ((long) tableIndex) << 32 | ((long) slotIndex);
      }

      boolean emptySlot = 0L == candidate;

      if (emptySlot) {

        if (isReading) {
          break; // If the slot is empty while reading, skip to the next table.
        } else if (recordCount.get() >= tableCapacity) {
          break; // we're writing but the table is 70% full
        } else {
          ///CLOVER:OFF
          // No reliable way to test without being able to mock unsafe
          if (unsafe.compareAndSwapLong(table, offset, 0L, key)) { // try to reserve it
            ///CLOVER:ON

            //increment the record count
            recordCount.incrementAndGet();

            // reset timestamp
            unsafe.putLongVolatile(table, offset + Unsafe.ARRAY_LONG_INDEX_SCALE, 0L);
            // It is ok if we lose some data from other threads while writing identity
            for (int i = 0; i < identity.length; i++) {
              unsafe.putLongVolatile(table,
                  offset + (RESERVED_FIELDS + i) * Unsafe.ARRAY_LONG_INDEX_SCALE, identity[i]);
            }

            //increment the total size;
            int tagIndex = unsafe.getAndAddInt(this, usedOffset, 1);
            if (tagIndex >= tagSets.length) {
              // grow tag set
              synchronized (this) {
                if (tagIndex >= tagSets.length) {
                  final int oldLength = tagSets.length;
                  final int newLength =
                      oldLength > TAGSETS_MAX_INCREMENT ? oldLength + TAGSETS_MAX_INCREMENT
                          : oldLength * 2;
                  tagSets = Arrays.copyOf(tagSets, newLength);
                }
              }
            }

            // Store tags in the tag array for iteration purposes only
            tagSets[tagIndex] = Arrays.copyOf(tags, tags.length);

            // Encode table index and slot index into a long.
            // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
            // This logic is replicated in multiple places for performance reasons.
            return ((long) tableIndex) << 32 | ((long) slotIndex);
          }
        }
      } else {
        slotIndex += recordSize;
        if (slotIndex >= table.length) {
          slotIndex = 0;
        }
        if (slotIndex == startIndex) {
          break; // check the next table
        }
      }
    }
  }
  if (isReading) {
    return NOT_FOUND;
  } else {
    if (growTable()) {
      return index(tags, isReading);
    } else {
      return NOT_FOUND;
    }
  }
}
 
Example 19
Source File: HeapBuffer.java    From incubator-hivemall with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an offset in a chunk
 * 
 * @param ptr physical address
 * @return physical offset in a chunk
 */
private long offset(final long ptr) {
    long j = ptr % _chunkBytes;
    return Unsafe.ARRAY_INT_BASE_OFFSET + j;
}