/** * Reads entry hash code. * * @param qAddr Queue node address. * @return Entry hash code. */ private int hash(long qAddr) { return mem.readInt(qAddr + 6); }
/** * @throws Exception If failed. */ @Test public void testInt() throws Exception { GridUnsafeMemory mem = new GridUnsafeMemory(64); int size = 32; long addr = mem.allocate(size); try { int i1 = 123; mem.writeInt(addr, i1); int i2 = mem.readInt(addr); assertEquals(i1, i2); int i3 = 321; mem.writeIntVolatile(addr, i3); int i4 = 222; assertTrue(mem.casInt(addr, i3, i4)); assertFalse(mem.casInt(addr, i3, 0)); assertEquals(i4, mem.readIntVolatile(addr)); } finally { mem.release(addr, size); } }
/** * @param keyPtr Key pointer. * @return Key size. */ private int keySize(long keyPtr) { return mem.readInt(keyPtr); }
/** * @param valPtr Value page pointer. * @return Value size. */ protected int valueSize(long valPtr) { return mem.readInt(valPtr + 8); }
/** * @param meta Meta pointer. * @return Key hash. */ protected int keyHash(long meta) { return mem.readInt(meta); }
/** * @param meta Meta pointer. * @return Key size. */ protected int keySize(long meta) { return mem.readInt(meta + 4); }
/** * @param ptr Pointer. * @param mem Memory. * @return Hash. */ static int hash(long ptr, GridUnsafeMemory mem) { return mem.readInt(ptr + HEADER_HASH_OFF); }
/** * Reads partition entry belongs to. * * @param qAddr Queue node address. * @return Entry partition. */ private int partition(long qAddr) { return mem.readInt(qAddr + 2); }
/** * @param ptr Pointer. * @param mem Memory. * @return Key length. */ static int readKeyLength(long ptr, GridUnsafeMemory mem) { int len = mem.readInt(ptr + HEADER_KEY_SIZE_OFF); assert len >= 0 : "Invalid key length [addr=" + String.format("0x%08x", ptr) + ", len=" + Long.toHexString(len) + ']'; return len; }
/** * @param ptr Pointer. * @param mem Memory. * @return Value length. */ static int readValueLength(long ptr, GridUnsafeMemory mem) { int len = mem.readInt(ptr + HEADER_VALUE_SIZE); assert len >= 0 : "Invalid value length [addr=" + String.format("0x%08x", ptr) + ", len=" + Integer.toHexString(len) + ']'; return len; }
/** {@inheritDoc} */ @Override public int readInt() throws IOException { return mem.readInt(move(4)); }
/** * Releases off-heap memory allocated by {@link #putOffHeap} method. * * @param ptr Optional pointer returned by {@link #putOffHeap}. */ public void removeOffHeap(long ptr) { if (ptr != 0) release(ptr, readInt(ptr) + 5); }
/** * Get value stored in offheap along with a value type. * * @param ptr Pointer to read. * @return Stored byte array and "raw bytes" flag. */ public IgniteBiTuple<byte[], Byte> get(long ptr) { assert ptr != 0; int size = readInt(ptr); byte type = readByte(ptr + 4); byte[] bytes = readBytes(ptr + 5, size); return new IgniteBiTuple<>(bytes, type); }
/** * Stores value to the specified memory location. If specified pointer is {@code 0}, then will * allocate required space. If size of allocated space is not enough to hold given values, will * reallocate memory. * * @param ptr Optional pointer to allocated memory. First 4 bytes in allocated region must contain * size of allocated chunk. * @param val Value to store. * @param type Value type. * @return Pointer. */ public long putOffHeap(long ptr, byte[] val, byte type) { int size = val.length; assert size != 0; int allocated = ptr == 0 ? 0 : readInt(ptr); if (allocated != size) { if (ptr != 0) release(ptr, allocated + 5); ptr = allocate(size + 5); writeInt(ptr, size); } writeByte(ptr + 4, type); writeBytes(ptr + 5, val); return ptr; }
/** * @param ptr Pointer. * @param mem Memory. * @return Hash. */ static int hash(long ptr, GridUnsafeMemory mem) { return mem.readInt(ptr + HEADER_HASH_OFF); }
/** * Reads partition entry belongs to. * * @param qAddr Queue node address. * @return Entry partition. */ private int partition(long qAddr) { return mem.readInt(qAddr + 2); }
/** * Reads entry hash code. * * @param qAddr Queue node address. * @return Entry hash code. */ private int hash(long qAddr) { return mem.readInt(qAddr + 6); }
/** * @param ptr Pointer. * @param mem Memory. * @return Key length. */ static int readKeyLength(long ptr, GridUnsafeMemory mem) { int len = mem.readInt(ptr + HEADER_KEY_SIZE_OFF); assert len >= 0 : "Invalid key length [addr=" + String.format("0x%08x", ptr) + ", len=" + Long.toHexString(len) + ']'; return len; }
/** * @param ptr Pointer. * @param mem Memory. * @return Value length. */ static int readValueLength(long ptr, GridUnsafeMemory mem) { int len = mem.readInt(ptr + HEADER_VALUE_SIZE); assert len >= 0 : "Invalid value length [addr=" + String.format("0x%08x", ptr) + ", len=" + Integer.toHexString(len) + ']'; return len; }
/** * Releases off-heap memory allocated by {@link #putOffHeap} method. * * @param ptr Optional pointer returned by {@link #putOffHeap}. */ public void removeOffHeap(long ptr) { if (ptr != 0) release(ptr, readInt(ptr) + 5); }