Java Code Examples for sun.misc.Cleaner#create()

The following examples show how to use sun.misc.Cleaner#create() . 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: TextClassifierImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private TextClassifierImplNative getNative(LocaleList localeList)
        throws FileNotFoundException {
    synchronized (mLock) {
        localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList;
        final ModelFile bestModel = findBestModelLocked(localeList);
        if (bestModel == null) {
            throw new FileNotFoundException("No model for " + localeList.toLanguageTags());
        }
        if (mNative == null || mNative.isClosed() || !Objects.equals(mModel, bestModel)) {
            Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
            final ParcelFileDescriptor fd = ParcelFileDescriptor.open(
                    new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
            mNative = new TextClassifierImplNative(fd.getFd());
            mNativeCleaner = Cleaner.create(this, new NativeCloser(mNative));
            closeAndLogError(fd);
            mModel = bestModel;
        }
        return mNative;
    }
}
 
Example 2
Source File: AllocateDirectMap.java    From incubator-datasketches-memory with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
AllocateDirectMap(final File file, final long fileOffsetBytes, final long capacityBytes,
    final boolean localReadOnly) {
  this.capacityBytes = capacityBytes;
  resourceReadOnly = isFileReadOnly(file);
  final long fileLength = file.length();
  if ((localReadOnly || resourceReadOnly) && fileOffsetBytes + capacityBytes > fileLength) {
    throw new IllegalArgumentException(
        "Read-only mode and requested map length is greater than current file length: "
        + "Requested Length = " + (fileOffsetBytes + capacityBytes)
        + ", Current File Length = " + fileLength);
  }
  raf = mapper(file, fileOffsetBytes, capacityBytes, resourceReadOnly);
  nativeBaseOffset = map(raf.getChannel(), resourceReadOnly, fileOffsetBytes, capacityBytes);
  deallocator = new Deallocator(nativeBaseOffset, capacityBytes, raf);
  cleaner = Cleaner.create(this, deallocator);
}
 
Example 3
Source File: Platform.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the JVM's
 * MaxDirectMemorySize limit (the default limit is too low and we do not want to require users
 * to increase it).
 */
@SuppressWarnings("unchecked")
public static ByteBuffer allocateDirectBuffer(int size) {
    try {
        Class cls = Class.forName("java.nio.DirectByteBuffer");
        Constructor constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
        constructor.setAccessible(true);
        Field cleanerField = cls.getDeclaredField("cleaner");
        cleanerField.setAccessible(true);
        final long memory = allocateMemory(size);
        ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size);
        Cleaner cleaner = Cleaner.create(buffer, new Runnable() {
            @Override
            public void run() {
                freeMemory(memory);
            }
        });
        cleanerField.set(buffer, cleaner);
        return buffer;
    } catch (Exception e) {
        throwException(e);
    }
    throw new IllegalStateException("unreachable");
}
 
Example 4
Source File: UnsafeHelper.java    From gadtry with Apache License 2.0 6 votes vote down vote up
/**
 * Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the JVM's
 * MaxDirectMemorySize limit (the default limit is too low and we do not want to require users
 * to increase it).
 *
 * @param size allocate mem size
 * @return ByteBuffer
 */
@SuppressWarnings("unchecked")
public static ByteBuffer allocateDirectBuffer(int size)
{
    try {
        Class<?> cls = Class.forName("java.nio.DirectByteBuffer");
        Constructor<?> constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
        constructor.setAccessible(true);
        Field cleanerField = cls.getDeclaredField("cleaner");
        cleanerField.setAccessible(true);
        long memory = _UNSAFE.allocateMemory(size);
        ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size);
        Cleaner cleaner = Cleaner.create(buffer, () -> _UNSAFE.freeMemory(memory));
        cleanerField.set(buffer, cleaner);
        return buffer;
    }
    catch (Exception e) {
        throwException(e);
    }
    throw new IllegalStateException("unreachable");
}
 
Example 5
Source File: Platform.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the JVM's
 * MaxDirectMemorySize limit (the default limit is too low and we do not want to require users
 * to increase it).
 */
@SuppressWarnings("unchecked")
public static ByteBuffer allocateDirectBuffer(int size) {
    try {
        Class cls = Class.forName("java.nio.DirectByteBuffer");
        Constructor constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
        constructor.setAccessible(true);
        Field cleanerField = cls.getDeclaredField("cleaner");
        cleanerField.setAccessible(true);
        final long memory = allocateMemory(size);
        ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size);
        Cleaner cleaner = Cleaner.create(buffer, new Runnable() {
            @Override
            public void run() {
                freeMemory(memory);
            }
        });
        cleanerField.set(buffer, cleaner);
        return buffer;
    } catch (Exception e) {
        throwException(e);
    }
    throw new IllegalStateException("unreachable");
}
 
Example 6
Source File: DirectByteBuffer.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
DirectByteBuffer(int cap) {                   // package-private

        super(-1, 0, cap, cap);
        boolean pa = VM.isDirectMemoryPageAligned();
        int ps = Bits.pageSize();
        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
        Bits.reserveMemory(size, cap);

        long base = 0;
        try {
            base = unsafe.allocateMemory(size);
        } catch (OutOfMemoryError x) {
            Bits.unreserveMemory(size, cap);
            throw x;
        }
        unsafe.setMemory(base, size, (byte) 0);
        if (pa && (base % ps != 0)) {
            // Round up to page boundary
            address = base + ps - (base & (ps - 1));
        } else {
            address = base;
        }
        cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
        att = null;



    }
 
Example 7
Source File: DirectByteBuffer.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
DirectByteBuffer(int cap) {                   // package-private

        super(-1, 0, cap, cap);
        boolean pa = VM.isDirectMemoryPageAligned();
        int ps = Bits.pageSize();
        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
        Bits.reserveMemory(size, cap);

        long base = 0;
        try {
            base = unsafe.allocateMemory(size);
        } catch (OutOfMemoryError x) {
            Bits.unreserveMemory(size, cap);
            throw x;
        }
        unsafe.setMemory(base, size, (byte) 0);
        if (pa && (base % ps != 0)) {
            // Round up to page boundary
            address = base + ps - (base & (ps - 1));
        } else {
            address = base;
        }
        cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
        att = null;



    }
 
Example 8
Source File: DirectByteBuffer.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
DirectByteBuffer(int cap) {                   // package-private

        super(-1, 0, cap, cap);
        boolean pa = VM.isDirectMemoryPageAligned();
        int ps = Bits.pageSize();
        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
        Bits.reserveMemory(size, cap);

        long base = 0;
        try {
            base = unsafe.allocateMemory(size);
        } catch (OutOfMemoryError x) {
            Bits.unreserveMemory(size, cap);
            throw x;
        }
        unsafe.setMemory(base, size, (byte) 0);
        if (pa && (base % ps != 0)) {
            // Round up to page boundary
            address = base + ps - (base & (ps - 1));
        } else {
            address = base;
        }
        cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
        att = null;



    }
 
Example 9
Source File: NativeBuffer.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 10
Source File: NativeBuffer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 11
Source File: NativeBuffer.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 12
Source File: NativeBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 13
Source File: NativeBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 14
Source File: NativeBuffer.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 15
Source File: NativeBuffer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 16
Source File: NativeBuffer.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 17
Source File: NativeBuffer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 18
Source File: NativeBuffer.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
NativeBuffer(int size) {
    this.address = unsafe.allocateMemory(size);
    this.size = size;
    this.cleaner = Cleaner.create(this, new Deallocator(address));
}
 
Example 19
Source File: DirectByteBuffer.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
protected DirectByteBuffer(int cap, long addr,
                                 FileDescriptor fd,
                                 Runnable unmapper)
{

    super(-1, 0, cap, cap, fd);
    address = addr;
    cleaner = Cleaner.create(this, unmapper);
    att = null;



}
 
Example 20
Source File: DirectByteBuffer.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
protected DirectByteBuffer(int cap, long addr,
                                 FileDescriptor fd,
                                 Runnable unmapper)
{

    super(-1, 0, cap, cap, fd);
    address = addr;
    cleaner = Cleaner.create(this, unmapper);
    att = null;



}