/** * Allocates memory of given size in bytes. * * @param size Size of allocated block. * @param init Flag to zero-out the initialized memory or not. * @return Allocated block address. * @throws GridOffHeapOutOfMemoryException If Memory could not be allocated. */ public long allocate(long size, boolean init) throws GridOffHeapOutOfMemoryException { return allocate(size, init, false); }
boolean reserved = mem.reserve(block); long addr = mem.allocate(block, true, true);
/** * @param jobInfo Job info. * @param mem Memory. */ public HadoopSkipList(HadoopJobInfo jobInfo, GridUnsafeMemory mem) { super(jobInfo, mem); heads = mem.allocate(HEADS_SIZE, true); }
/** * Allocates memory of given size in bytes. * * @param size Size of allocated block. * @return Allocated block address. * @throws GridOffHeapOutOfMemoryException If Memory could not be allocated. */ public long allocate(long size) throws GridOffHeapOutOfMemoryException { return allocate(size, false, false); }
/** * Expert: allocate a new buffer. Subclasses can allocate differently. * * @return allocated buffer. */ private long newBuffer() { return dir.memory().allocate(BUFFER_SIZE); }
/** * 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 requestedSize Requested size. * @return Next write pointer. */ private long allocateNextPage(long requestedSize) { int writtenSize = writtenSize(); long newPageSize = nextPageSize(writtenSize + requestedSize); long newPagePtr = mem.allocate(newPageSize); HadoopOffheapBuffer b = out.buffer(); b.set(newPagePtr, newPageSize); if (writtenSize != 0) { mem.copyMemory(writeStart, newPagePtr, writtenSize); b.move(writtenSize); } writeStart = newPagePtr; // At this point old page is not needed, so we release it. Page oldPage = curPage; curPage = new Page(newPagePtr, newPageSize); if (oldPage != null) allPages.add(oldPage); return b.move(requestedSize); }
/** * @throws Exception If failed. */ @Test public void testBytes() throws Exception { GridUnsafeMemory mem = new GridUnsafeMemory(64); String s = "123"; byte[] bytes = s.getBytes(); int size = bytes.length * 2; long addr = mem.allocate(size); try { mem.writeBytes(addr, bytes); byte[] read = mem.readBytes(addr, bytes.length); assert Arrays.equals(bytes, read); } 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); } } }
long addr = mem.allocate(size, false, true);
/** * */ @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); } }
/** * */ @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); } }
/** * @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); } }
/** * @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); }
/** * @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); } }
/** * @throws IOException If failed. */ @Test public void testStreams() 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); write(out); HadoopDataInStream in = new HadoopDataInStream(mem); in.buffer().set(ptr, out.buffer().pointer() - ptr); checkRead(in); }
long addr = mem.allocate(size, false, true);
/** * @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 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); } }
/** * Allocates memory of given size in bytes. * * @param size Size of allocated block. * @param init Flag to zero-out the initialized memory or not. * @return Allocated block address. * @throws GridOffHeapOutOfMemoryException If Memory could not be allocated. */ public long allocate(long size, boolean init) throws GridOffHeapOutOfMemoryException { return allocate(size, init, false); }