/** * 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; }
/** * 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); }
/** {@inheritDoc} */ @Override public long allocatedSize() { return mem.allocatedSize(); }
/** * @throws Exception If failed. */ @Test public void testLong() throws Exception { GridUnsafeMemory mem = new GridUnsafeMemory(64); int size = 32; long addr = mem.allocate(size); try { long l1 = 123456; mem.writeLong(addr, l1); long l2 = mem.readLong(addr); assertEquals(l1, l2); long l3 = 654321; mem.writeLongVolatile(addr, l3); long l4 = 666666; assertTrue(mem.casLong(addr, l3, l4)); assertFalse(mem.casLong(addr, l3, 0)); assertEquals(l4, mem.readLongVolatile(addr)); } finally { mem.release(addr, size); } }
/** * @throws Exception If failed. */ @Test public void testByte() throws Exception { GridUnsafeMemory mem = new GridUnsafeMemory(64); int size = 32; long addr = mem.allocate(size); try { byte b1 = 123; mem.writeByte(addr, b1); byte b2 = mem.readByte(addr); assertEquals(b1, b2); byte b3 = 11; mem.writeByteVolatile(addr, b3); assertEquals(b3, mem.readByteVolatile(addr)); } finally { mem.release(addr, size); } }
/** * @param s String. * @throws Exception If failed. */ public void checkCompare(String s) throws Exception { byte[] bytes = s.getBytes(); int size = bytes.length + 8; GridUnsafeMemory mem = new GridUnsafeMemory(size); for (int i = 0; i < 8; i++) { long addr = mem.allocate(size); long ptr = addr + i; try { mem.writeBytes(ptr, bytes); assert mem.compare(ptr, bytes); byte[] read = mem.readBytes(ptr, bytes.length); assert Arrays.equals(bytes, read); } finally { mem.release(addr, size); } } }
/** * @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); } }
/** * @throws Exception If failed. */ @Test public void testShort() throws Exception { GridUnsafeMemory mem = new GridUnsafeMemory(64); int size = 16; long addr = mem.allocate(size); try { short s1 = (short)56777; mem.writeShort(addr, s1); assertEquals(s1, mem.readShort(addr)); } finally { mem.release(addr, size); } }
/** * */ @Test public void testFloat() { GridUnsafeMemory mem = new GridUnsafeMemory(64); int size = 32; long addr = mem.allocate(size); try { float f1 = 0.23223f; mem.writeFloat(addr, f1); assertEquals(f1, mem.readFloat(addr)); } finally { mem.release(addr, size); } }
/** * */ @Test public void testDouble() { GridUnsafeMemory mem = new GridUnsafeMemory(64); int size = 32; long addr = mem.allocate(size); try { double d1 = 0.2323423; mem.writeDouble(addr, d1); assertEquals(d1, mem.readDouble(addr)); } finally { mem.release(addr, size); } }
GridUnsafeMemory mem = new GridUnsafeMemory(cap); boolean reserved = mem.reserve(block); long addr = mem.allocate(block, true, true); mem.release(addr, block); assertEquals(mem.allocatedSize(), 0);
GridUnsafeMemory mem = new GridUnsafeMemory(0); X.println("Alloc: " + mem.allocatedSize()); assertEquals(0, mem.allocatedSize());
/** * @param ptr Pointer. * @param cnt Count. * @return Byte array. */ public byte[] readBytes(long ptr, int cnt) { return readBytes(ptr, new byte[cnt]); }
/** * @param val String value. * @return List of strings are returned by readLine(). * @throws IOException On error. */ List<String> readLineByHadoopDataInStream(String val) throws IOException { GridUnsafeMemory mem = new GridUnsafeMemory(0); HadoopDataOutStream out = new HadoopDataOutStream(mem); final long ptr = mem.allocate(BUFF_SIZE); out.buffer().set(ptr, BUFF_SIZE); out.write(val.getBytes()); HadoopDataInStream in = new HadoopDataInStream(mem); in.buffer().set(ptr, out.buffer().pointer() - ptr); return readLineStrings(in); }
/** * Reads address of next queue node. * * @param qAddr Queue node address. * @return Address of next queue node. */ private long next(long qAddr) { return mem.readLong(qAddr + 19); }
/** * Reads entry hash code. * * @param qAddr Queue node address. * @return Entry hash code. */ private int hash(long qAddr) { return mem.readInt(qAddr + 6); }
/** * @param jobInfo Job info. * @param mem Memory. */ public HadoopSkipList(HadoopJobInfo jobInfo, GridUnsafeMemory mem) { super(jobInfo, mem); heads = mem.allocate(HEADS_SIZE, true); }