sun.misc.Unsafe Java Examples

The following examples show how to use sun.misc.Unsafe. 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: MappedByteBuffer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example #2
Source File: CopyMemory.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testCopyByteArrayToRawMemory(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for byte[] to raw memory...");
    byte[] b1 = new byte[BUFFER_SIZE];
    long b2 = getMemory(BUFFER_SIZE);
    for (int i = 0; i < N; i++) {
        set(b1, 0, BUFFER_SIZE, FILLER);
        set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,
            null, b2 + ofs2, len);
        check(unsafe, b2, 0, ofs2 - 1, FILLER2);
        check(unsafe, b2, ofs2, len, val);
        check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
Example #3
Source File: ConcurrentMonoidLongTable.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Override
public 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);
  long[] table = tables.get(tableIndex);

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

  combine(table, base, value);
}
 
Example #4
Source File: ConcurrentMonoidLongTable.java    From metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a new double value to the existing value in the given field index.
 *
 * @param table the table containing the values
 * @param baseOffset base offset of the record in the table containing the left hand value
 * @param index index of the field
 * @param value value to be added
 * @return new accumulated value of the field index
 */
protected double add(final long[] table, final long baseOffset, final long index,
    final double value) {
  final long offset = ((RESERVED_FIELDS + index) * Unsafe.ARRAY_LONG_INDEX_SCALE) + baseOffset;
  long old;
  double old_d, new_d;
  do {
    old = unsafe.getLongVolatile(table, offset);
    old_d = Double.longBitsToDouble(old);
    new_d = old_d + value;
    ///CLOVER:OFF
    // No reliable way to test without being able to mock unsafe
  } while (!unsafe.compareAndSwapLong(table, offset, old, Double.doubleToRawLongBits(new_d)));
  ///CLOVER:ON
  return new_d;
}
 
Example #5
Source File: CopyMemory.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testCopyRawMemoryToByteArray(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for raw memory to byte[]...");
    long b1 = getMemory(BUFFER_SIZE);
    byte[] b2 = new byte[BUFFER_SIZE];
    for (int i = 0; i < N; i++) {
        set(unsafe, b1, 0, BUFFER_SIZE, FILLER);
        set(b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(unsafe, b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(null, b1 + ofs,
            b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);
        check(b2, 0, ofs2 - 1, FILLER2);
        check(b2, ofs2, len, val);
        check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
Example #6
Source File: CopyMemory.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void testCopyByteArrayToByteArray(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for byte[] to byte[]...");
    byte[] b1 = new byte[BUFFER_SIZE];
    byte[] b2 = new byte[BUFFER_SIZE];
    for (int i = 0; i < N; i++) {
        set(b1, 0, BUFFER_SIZE, FILLER);
        set(b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,
            b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);
        check(b2, 0, ofs2 - 1, FILLER2);
        check(b2, ofs2, len, val);
        check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
Example #7
Source File: GridUnsafe.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @return Instance of Unsafe class.
 */
private static Unsafe unsafe() {
    try {
        return Unsafe.getUnsafe();
    }
    catch (SecurityException ignored) {
        try {
            return AccessController.doPrivileged(
                new PrivilegedExceptionAction<Unsafe>() {
                    @Override public Unsafe run() throws Exception {
                        Field f = Unsafe.class.getDeclaredField("theUnsafe");

                        f.setAccessible(true);

                        return (Unsafe)f.get(null);
                    }
                });
        }
        catch (PrivilegedActionException e) {
            throw new RuntimeException("Could not initialize intrinsics.", e.getCause());
        }
    }
}
 
Example #8
Source File: MappedByteBuffer.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example #9
Source File: Test7190310_unsafe.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception {
  // Access verification
  System.out.println("referent: " + str);
  Object obj = getRef0(ref);
  if (obj != str) {
    System.out.println("FAILED: weakRef.get() " + obj + " != " + str);
    return false;
  }
  obj = getRef1(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef2(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef3(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  return true;
}
 
Example #10
Source File: Test7190310_unsafe.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception {
  // Access verification
  System.out.println("referent: " + str);
  Object obj = getRef0(ref);
  if (obj != str) {
    System.out.println("FAILED: weakRef.get() " + obj + " != " + str);
    return false;
  }
  obj = getRef1(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef2(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef3(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  return true;
}
 
Example #11
Source File: CopyMemory.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void testCopyRawMemoryToRawMemory(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for raw memory to raw memory...");
    long b1 = getMemory(BUFFER_SIZE);
    long b2 = getMemory(BUFFER_SIZE);
    for (int i = 0; i < N; i++) {
        set(unsafe, b1, 0, BUFFER_SIZE, FILLER);
        set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(unsafe, b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(null, b1 + ofs,
            null, b2 + ofs2, len);
        check(unsafe, b2, 0, ofs2 - 1, FILLER2);
        check(unsafe, b2, ofs2, len, val);
        check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
Example #12
Source File: ConcurrentMonoidLongTable.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_LONG_BASE_OFFSET + slotIndex * Unsafe.ARRAY_LONG_INDEX_SCALE;
  return true;
}
 
Example #13
Source File: ConcurrentMonoidLongTable.java    From metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Set a new double value as minimum if its lower than existing value in the given field index.
 *
 * @param table the table containing the values
 * @param baseOffset base offset of the record in the table containing the left hand value
 * @param index index of the field
 * @param value new value
 */
protected void min(final long[] table, final long baseOffset, final long index,
    final double value) {
  final long offset = baseOffset + (RESERVED_FIELDS + index) * Unsafe.ARRAY_LONG_INDEX_SCALE;
  long old;
  double old_d;
  do {
    old = unsafe.getLong(table, offset);
    old_d = Double.longBitsToDouble(old);
    if (value >= old_d) {
      return;
    }
    ///CLOVER:OFF
    // No reliable way to test without being able to mock unsafe
  } while (!unsafe.compareAndSwapLong(table, offset, old, Double.doubleToRawLongBits(value)));
  ///CLOVER:ON
}
 
Example #14
Source File: Test7190310_unsafe.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception {
  // Access verification
  System.out.println("referent: " + str);
  Object obj = getRef0(ref);
  if (obj != str) {
    System.out.println("FAILED: weakRef.get() " + obj + " != " + str);
    return false;
  }
  obj = getRef1(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef2(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef3(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  return true;
}
 
Example #15
Source File: NativeIO.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * @return the operating system's page size.
 */
static long getOperatingSystemPageSize() {
  try {
    Field f = Unsafe.class.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    Unsafe unsafe = (Unsafe)f.get(null);
    return unsafe.pageSize();
  } catch (Throwable e) {
    LOG.warn("Unable to get operating system page size.  Guessing 4096.", e);
    return 4096;
  }
}
 
Example #16
Source File: RenderBuffer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(float[] x, int offset, int length) {
    // assert (position() % SIZEOF_FLOAT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_FLOAT + Unsafe.ARRAY_FLOAT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_FLOAT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putFloat(x[i]);
        }
    }
    return this;
}
 
Example #17
Source File: UnsafeWrapperMultiCacheValueTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsafeWrapperMultiCacheValue() throws EvictionException {

  final Unsafe unsafe = UnsafeCacheValue._unsafe;
  final long[] addresses = new long[10];
  int chunkSize = 1000;
  int chunks = 10;
  for (int i = 0; i < chunks; i++) {
    addresses[i] = unsafe.allocateMemory(chunkSize);
  }

  int length = addresses.length * chunkSize;

  UnsafeWrapperMultiCacheValue unsafeWrapperMultiCacheValue = new UnsafeWrapperMultiCacheValue(length, addresses,
      chunkSize) {
    @Override
    protected void releaseInternal() {
      for (int i = 0; i < 10; i++) {
        unsafe.freeMemory(addresses[i]);
      }
    }
  };

  Random random = new Random();
  byte[] buf1 = new byte[length];
  random.nextBytes(buf1);

  unsafeWrapperMultiCacheValue.write(0, buf1, 0, length);

  byte[] buf2 = new byte[length];

  unsafeWrapperMultiCacheValue.read(0, buf2, 0, length);

  assertTrue(equals(buf1, buf2));
  assertTrue(Arrays.equals(buf1, buf2));

  unsafeWrapperMultiCacheValue.release();

}
 
Example #18
Source File: CopyMemory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testSetByteArray(Unsafe unsafe) throws Exception {
    System.out.println("Testing setMemory() for byte[]...");
    byte[] b = new byte[BUFFER_SIZE];
    for (int i = 0; i < N; i++) {
        set(b, 0, BUFFER_SIZE, FILLER);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        unsafe.setMemory(b, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, len, (byte)val);
        check(b, 0, ofs - 1, FILLER);
        check(b, ofs, len, val);
        check(b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);
    }
}
 
Example #19
Source File: RenderBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example #20
Source File: UnsafeHolder.java    From es6draft with MIT License 5 votes vote down vote up
private static Unsafe initializeUnsafe() {
    try {
        return AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
            Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            return (Unsafe) f.get(null);
        });
    } catch (PrivilegedActionException e) {
        throw new ExceptionInInitializerError(e.getException());
    }
}
 
Example #21
Source File: RenderBuffer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(short[] x, int offset, int length) {
    // assert (position() % SIZEOF_SHORT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_SHORT + Unsafe.ARRAY_SHORT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_SHORT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putShort(x[i]);
        }
    }
    return this;
}
 
Example #22
Source File: UnsafeSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
ByteFieldSerializer(Field field)
{
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET)
        throw new IllegalStateException();
}
 
Example #23
Source File: UnsafeAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Unsafe initUnsafe() {
    try {
        // Fast path when we are trusted.
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        // Slow path when we are not trusted.
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
Example #24
Source File: Utils.java    From mnemonic with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve an Unsafe object.
 *
 * @throws Exception
 *           Error when get Unsafe object from runtime
 *
 * @return an unsafe object
 */
public static Unsafe getUnsafe() throws Exception {
  if (null == m_unsafe) {
    Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
    field.setAccessible(true);
    m_unsafe = (sun.misc.Unsafe) field.get(null);
  }
  return m_unsafe;
}
 
Example #25
Source File: SPARCArrayEqualsOp.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Unsafe initUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
Example #26
Source File: Utils.java    From deobfuscator with Apache License 2.0 5 votes vote down vote up
public static Unsafe getUnsafe() {
    try {
        initializeUnsafe();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return unsafe;
}
 
Example #27
Source File: Test6968348.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class c = Test6968348.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);
    array_long_base_offset = unsafe.arrayBaseOffset(long[].class);

    for (int n = 0; n < 100000; n++) {
        test();
    }
}
 
Example #28
Source File: TestUtils.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
static Unsafe getUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
 
Example #29
Source File: VMAnonymousClass.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static synchronized Unsafe getUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException("Unable to get Unsafe instance.", e);
    }
}
 
Example #30
Source File: SolarisWatchService.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Calls port_associate to register the given path.
 * Returns pointer to fileobj structure that is allocated for
 * the registration.
 */
long registerImpl(UnixPath dir, int events)
    throws UnixException
{
    // allocate memory for the path (file_obj->fo_name field)
    byte[] path = dir.getByteArrayForSysCalls();
    int len = path.length;
    long name = unsafe.allocateMemory(len+1);
    unsafe.copyMemory(path, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
        name, (long)len);
    unsafe.putByte(name + len, (byte)0);

    // allocate memory for filedatanode structure - this is the object
    // to port_associate
    long object = unsafe.allocateMemory(SIZEOF_FILEOBJ);
    unsafe.setMemory(null, object, SIZEOF_FILEOBJ, (byte)0);
    unsafe.putAddress(object + OFFSET_FO_NAME, name);

    // associate the object with the port
    try {
        portAssociate(port,
                      PORT_SOURCE_FILE,
                      object,
                      events);
    } catch (UnixException x) {
        // debugging
        if (x.errno() == EAGAIN) {
            System.err.println("The maximum number of objects associated "+
                "with the port has been reached");
        }

        unsafe.freeMemory(name);
        unsafe.freeMemory(object);
        throw x;
    }
    return object;
}