/** * Throw an exception if this chunk is not allocated */ public final void checkIsAllocated() { int originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } }
/** * Throw an exception if this chunk is not allocated */ public void checkIsAllocated() { int originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } }
public void initializeUseCount() { int rawBits; do { rawBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(rawBits)); } int uc = rawBits & REF_COUNT_MASK; if (uc != 0) { throw new IllegalStateException("Expected use count to be zero but it was: " + uc + " rawBits=0x" + Integer.toHexString(rawBits)); } } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, rawBits, rawBits+1)); }
public void initializeUseCount() { int rawBits; do { rawBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(rawBits)); } int uc = rawBits & REF_COUNT_MASK; if (uc != 0) { throw new IllegalStateException("Expected use count to be zero but it was: " + uc + " rawBits=0x" + Integer.toHexString(rawBits)); } } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, rawBits, rawBits+1)); }
public void setSerialized(boolean isSerialized) { if (isSerialized) { int bits; int originalBits; do { originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } bits = originalBits | IS_SERIALIZED_BIT; } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits)); } } public void setCompressed(boolean isCompressed) {
public void setCompressed(boolean isCompressed) { if (isCompressed) { int bits; int originalBits; do { originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } bits = originalBits | IS_COMPRESSED_BIT; } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits)); } } public void setDataSize(int dataSize) { // KIRK
public void setSerialized(boolean isSerialized) { if (isSerialized) { int bits; int originalBits; do { originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } bits = originalBits | IS_SERIALIZED_BIT; } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits)); } } public void setCompressed(boolean isCompressed) {
public void setCompressed(boolean isCompressed) { if (isCompressed) { int bits; int originalBits; do { originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } bits = originalBits | IS_COMPRESSED_BIT; } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits)); } } public void setDataSize(int dataSize) { // KIRK
public void readyForAllocation(ChunkType chunkType) { if (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(getMemoryAddress()+REF_COUNT_OFFSET, 0, MAGIC_NUMBER|chunkType.getSrcType())) { throw new IllegalStateException("Expected 0 but found " + Integer.toHexString(UnsafeMemoryChunk.readAbsoluteIntVolatile(getMemoryAddress()+REF_COUNT_OFFSET))); } } /**
public void readyForAllocation(ChunkType chunkType) { if (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(getMemoryAddress()+REF_COUNT_OFFSET, 0, MAGIC_NUMBER|chunkType.getSrcType())) { throw new IllegalStateException("Expected 0 but found " + Integer.toHexString(UnsafeMemoryChunk.readAbsoluteIntVolatile(getMemoryAddress()+REF_COUNT_OFFSET))); } } /**
public void setDataSize(int dataSize) { // KIRK assert dataSize <= getSize(); int delta = getSize() - dataSize; assert delta <= (DATA_SIZE_DELTA_MASK >> DATA_SIZE_SHIFT); delta <<= DATA_SIZE_SHIFT; int bits; int originalBits; do { originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } bits = originalBits; bits &= ~DATA_SIZE_DELTA_MASK; // clear the old dataSizeDelta bits bits |= delta; // set the dataSizeDelta bits to the new delta value } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits)); }
public void setDataSize(int dataSize) { // KIRK assert dataSize <= getSize(); int delta = getSize() - dataSize; assert delta <= (DATA_SIZE_DELTA_MASK >> DATA_SIZE_SHIFT); delta <<= DATA_SIZE_SHIFT; int bits; int originalBits; do { originalBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET); if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) { throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits)); } bits = originalBits; bits &= ~DATA_SIZE_DELTA_MASK; // clear the old dataSizeDelta bits bits |= delta; // set the dataSizeDelta bits to the new delta value } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits)); }
int retryCount = 0; do { rawBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(memAddr+REF_COUNT_OFFSET); if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) {
public static boolean retain(long memAddr) { SimpleMemoryAllocatorImpl.validateAddress(memAddr); int uc; int rawBits; int retryCount = 0; do { rawBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(memAddr+REF_COUNT_OFFSET); if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) { // same as uc == 0 // TODO MAGIC_NUMBER rethink its use and interaction with compactor fragments return false; } uc = rawBits & REF_COUNT_MASK; if (uc == MAX_REF_COUNT) { throw new IllegalStateException("Maximum use count exceeded. rawBits=" + Integer.toHexString(rawBits)); } else if (uc == 0) { return false; } retryCount++; if (retryCount > 1000) { throw new IllegalStateException("tried to write " + (rawBits+1) + " to @" + Long.toHexString(memAddr) + " 1,000 times."); } } while (!UnsafeMemoryChunk.writeAbsoluteIntVolatile(memAddr+REF_COUNT_OFFSET, rawBits, rawBits+1)); //debugLog("use inced ref count " + (uc+1) + " @" + Long.toHexString(memAddr), true); if (ReferenceCountHelper.trackReferenceCounts()) { ReferenceCountHelper.refCountChanged(memAddr, false, uc+1); } return true; } public static void release(final long memAddr, boolean issueOnReturnCallback) {
do { returnToAllocator = false; rawBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(memAddr+REF_COUNT_OFFSET); if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) { String msg = "It looks like off heap memory @" + Long.toHexString(memAddr) + " was already freed. rawBits=" + Integer.toHexString(rawBits) + " history=" + ReferenceCountHelper.getFreeRefCountInfo(memAddr);
do { returnToAllocator = false; rawBits = UnsafeMemoryChunk.readAbsoluteIntVolatile(memAddr+REF_COUNT_OFFSET); if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) { String msg = "It looks like off heap memory @" + Long.toHexString(memAddr) + " was already freed. rawBits=" + Integer.toHexString(rawBits) + " history=" + getFreeRefCountInfo(memAddr);