/** * @param ptr Pointer. * @param hash Hash. * @param mem Memory. */ static void hash(long ptr, int hash, GridUnsafeMemory mem) { mem.writeInt(ptr + HEADER_HASH_OFF, hash); }
/** * @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 valPtr Value page pointer. * @param size Size. */ protected void valueSize(long valPtr, int size) { mem.writeInt(valPtr + 8, size); }
/** * Writes key length. * * @param ptr Pointer. * @param len Length. * @param mem Memory. */ static void writeKeyLength(long ptr, int len, GridUnsafeMemory mem) { mem.writeInt(ptr + HEADER_KEY_SIZE_OFF, len); }
/** * Writes entry hash code. * * @param qAddr Queue node address. * @param hash Entry hash code. */ private void hash(long qAddr, int hash) { mem.writeInt(qAddr + 6, hash); }
/** * @param keyPtr Key pointer. * @param keySize Key size. */ private void keySize(long keyPtr, int keySize) { mem.writeInt(keyPtr, keySize); }
/** * Writes value length. * * @param ptr Pointer. * @param len Length. * @param mem Memory. */ static void writeValueLength(long ptr, int len, GridUnsafeMemory mem) { mem.writeInt(ptr + HEADER_VALUE_SIZE, len); }
/** * Writes partition entry belongs to. * * @param qAddr Queue node address. * @param part Entry partition. */ private void partition(long qAddr, int part) { mem.writeInt(qAddr + 2, part); }
/** {@inheritDoc} */ @Override public void writeInt(int v) { mem.writeInt(move(4), v); }
/** * @param keyHash Key hash. * @param keySize Key size. * @param keyPtr Key pointer. * @param valPtr Value page pointer. * @param collisionPtr Pointer to meta with hash collision. * @return Created meta page pointer. */ private long createMeta(int keyHash, int keySize, long keyPtr, long valPtr, long collisionPtr) { long meta = allocate(32); mem.writeInt(meta, keyHash); mem.writeInt(meta + 4, keySize); mem.writeLong(meta + 8, keyPtr); mem.writeLong(meta + 16, valPtr); mem.writeLong(meta + 24, collisionPtr); return meta; }
/** * @param keyHash Key hash. * @param keySize Key size. * @param keyPtr Key pointer. * @param valPtr Value page pointer. * @param collisionPtr Pointer to meta with hash collision. * @param lastVisitedVal Last visited value pointer. * @return Created meta page pointer. */ private long createMeta(int keyHash, int keySize, long keyPtr, long valPtr, long collisionPtr, long lastVisitedVal) { long meta = allocate(40); mem.writeInt(meta, keyHash); mem.writeInt(meta + 4, keySize); mem.writeLong(meta + 8, keyPtr); mem.writeLong(meta + 16, valPtr); mem.writeLong(meta + 24, collisionPtr); mem.writeLong(meta + 32, lastVisitedVal); return meta; }
/** * 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; }
/** * Writes key length. * * @param ptr Pointer. * @param len Length. * @param mem Memory. */ static void writeKeyLength(long ptr, int len, GridUnsafeMemory mem) { mem.writeInt(ptr + HEADER_KEY_SIZE_OFF, len); }
/** * Writes entry hash code. * * @param qAddr Queue node address. * @param hash Entry hash code. */ private void hash(long qAddr, int hash) { mem.writeInt(qAddr + 6, hash); }
/** * Writes value length. * * @param ptr Pointer. * @param len Length. * @param mem Memory. */ static void writeValueLength(long ptr, int len, GridUnsafeMemory mem) { mem.writeInt(ptr + HEADER_VALUE_SIZE, len); }
/** * Writes partition entry belongs to. * * @param qAddr Queue node address. * @param part Entry partition. */ private void partition(long qAddr, int part) { mem.writeInt(qAddr + 2, part); }
/** * @param ptr Pointer. * @param hash Hash. * @param mem Memory. */ static void hash(long ptr, int hash, GridUnsafeMemory mem) { mem.writeInt(ptr + HEADER_HASH_OFF, hash); }
/** * 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; }