/** * Resizes this {@code DirectStore} to the {@code newSize}. * * <p>If {@code zeroOut} is {@code false}, the memory past the old size is not zeroed out and will * generally be garbage. * * <p>{@code DirectStore} don't keep track of the child {@code DirectBytes} instances, so after * the resize they might point to the wrong memory. Use at your own risk. * * @param newSize new size of this {@code DirectStore} * @param zeroOut if the memory past the old size should be zeroed out on increasing resize * @throws IllegalArgumentException if the {@code newSize} is not positive */ public void resize(long newSize, boolean zeroOut) { if (newSize <= 0) throw new IllegalArgumentException("Given newSize is " + newSize + " but should be positive"); address = deallocator.address = NativeBytes.UNSAFE.reallocateMemory(address, newSize); if (zeroOut && newSize > size) { NativeBytes.UNSAFE.setMemory(address + size, newSize - size, (byte) 0); } size = newSize; }
public DirectStore(ObjectSerializer objectSerializer, long size, boolean zeroOut) { this.objectSerializer = objectSerializer; address = NativeBytes.UNSAFE.allocateMemory(size); // System.out.println("old value " + Integer.toHexString(NativeBytes.UNSAFE.getInt(null, // address))); if (zeroOut) { NativeBytes.UNSAFE.setMemory(address, size, (byte) 0); NativeBytes.UNSAFE.putLongVolatile(null, address, 0L); } this.size = size; deallocator = new Deallocator(address); cleaner = Cleaner.create(this, deallocator); }
static { try { SB_VALUE = Class.forName("java.lang.AbstractStringBuilder").getDeclaredField("value"); SB_VALUE.setAccessible(true); SB_COUNT = Class.forName("java.lang.AbstractStringBuilder").getDeclaredField("count"); SB_COUNT.setAccessible(true); SB_COUNT_OFFSET = NativeBytes.UNSAFE.objectFieldOffset(SB_COUNT); } catch (Exception e) { throw new AssertionError(e); } }
@Override public void run() { if (address == 0) return; NativeBytes.UNSAFE.freeMemory(address); address = 0; }
public static void setCount(StringBuilder sb, int count) { NativeBytes.UNSAFE.putInt(sb, SB_COUNT_OFFSET, count); }