/** * @param ptr Pointer. * @param off Offset. * @return Value. */ public static byte readByte(long ptr, int off) { return GridUnsafe.getByte(ptr + off); }
/** * @param arr Array. * @param off Offset. * @return Value. */ public static byte readByte(byte[] arr, int off) { return GridUnsafe.getByte(arr, GridUnsafe.BYTE_ARR_OFF + off); }
/** * @param addr Start address. * @param off Offset. * @return Byte value from given address. */ public static byte getByte(long addr, int off) { assert addr > 0 : addr; assert off >= 0; return GridUnsafe.getByte(addr + off); }
/** * * @param addr Start address. * @param off Offset. * @return Byte value from given address. */ public static int getUnsignedByte(long addr, int off) { assert addr > 0 : addr; assert off >= 0; return GridUnsafe.getByte(addr + off) & 0xFF; }
/** * @param ptr Pointer. * @return Integer value. */ public byte readByte(long ptr) { return GridUnsafe.getByte(ptr); }
/** {@inheritDoc} */ @Override protected byte readByteAndShift() { return GridUnsafe.getByte(ptr + pos++); }
/** {@inheritDoc} */ @Override protected byte readBytePositioned0(int pos) { return GridUnsafe.getByte(ptr + pos); }
/** {@inheritDoc} */ @Override protected byte readBytePositioned0(int pos) { return GridUnsafe.getByte(data, GridUnsafe.BYTE_ARR_OFF + pos); }
/** {@inheritDoc} */ @Override public byte readByte() { lastFinished = buf.remaining() >= 1; if (lastFinished) { int pos = buf.position(); buf.position(pos + 1); return GridUnsafe.getByte(heapArr, baseOff + pos); } else return 0; }
/** {@inheritDoc} */ @Override public byte readByte() { lastFinished = buf.remaining() >= 1; if (lastFinished) { int pos = buf.position(); buf.position(pos + 1); return GridUnsafe.getByte(heapArr, baseOff + pos); } else return 0; }
/** * @param addr pointer in memory * @param len how much byte to read (should divide 8) * * @return hex representation of memory region */ public static String toHexString(long addr, int len) { StringBuilder sb = new StringBuilder(len * 2); for (int i = 0; i < len; i++) // Can not use getLong because on little-endian it produces bs. addByteAsHex(sb, GridUnsafe.getByte(addr + i)); return sb.toString(); }
/** {@inheritDoc} */ @Override public byte readByte() { ensureEnoughData(1); return GridUnsafe.getByte(data + pos++); }
/** {@inheritDoc} */ @Override public byte readByte() throws IOException { checkRange(1); byte res = GridUnsafe.getByte(buf, BYTE_ARR_OFF + pos); pos += 1; return res; }
/** {@inheritDoc} */ @Override public byte readBytePositioned(int pos) { int delta = pos + 1 - this.pos; if (delta > 0) ensureEnoughData(delta); return GridUnsafe.getByte(data + pos); }
/** {@inheritDoc} */ @Override public byte get(int idx) { ensure(idx, 1); return GridUnsafe.getByte(ptr + idx); }
/** {@inheritDoc} */ @Override public int readInt() { lastFinished = false; int val = 0; while (buf.hasRemaining()) { int pos = buf.position(); byte b = GridUnsafe.getByte(heapArr, baseOff + pos); buf.position(pos + 1); prim |= ((long)b & 0x7F) << (7 * primShift); if ((b & 0x80) == 0) { lastFinished = true; val = (int)prim; if (val == Integer.MIN_VALUE) val = Integer.MAX_VALUE; else val--; prim = 0; primShift = 0; break; } else primShift++; } return val; }
/** {@inheritDoc} */ @Override public int compare(Text val1, long val2Ptr, int val2Len) { int len2 = WritableUtils.decodeVIntSize(GridUnsafe.getByte(val2Ptr)); return HadoopUtils.compareBytes(val1.getBytes(), val1.getLength(), val2Ptr + len2, val2Len - len2); } }
/** {@inheritDoc} */ @Override public byte readByte() throws IOException { fromStream(1); return GridUnsafe.getByte(buf, BYTE_ARR_OFF + offset(1)); }
/** * @param ptr Off-heap pointer. * @param forceHeap If {@code true} creates heap-based object. * @return Object. * @throws BinaryObjectException If failed. */ public Object unmarshal(long ptr, boolean forceHeap) throws BinaryObjectException { assert ptr > 0 : ptr; int size = GridUnsafe.getInt(ptr); ptr += 4; byte type = GridUnsafe.getByte(ptr++); if (type != CacheObject.TYPE_BYTE_ARR) { assert size > 0 : size; BinaryInputStream in = new BinaryOffheapInputStream(ptr, size, forceHeap); return binaryMarsh.unmarshal(in); } else return U.copyMemory(ptr, size); }
/** * Test unsafe vs. new. */ private static void testUnsafe() throws InterruptedException { X.println("Testing unsafe..."); int MAX = 1000000; long[] addrs = new long[MAX]; System.gc(); GridTimer t = new GridTimer("unsafe"); int mem = 1024; for (int i = 0; i < MAX; i++) { addrs[i] = GridUnsafe.allocateMemory(mem); GridUnsafe.putByte(addrs[i] + RAND.nextInt(mem), (byte)RAND.nextInt(mem)); v = GridUnsafe.getByte(addrs[i] + RAND.nextInt(mem)); } X.println("Unsafe [time=" + t.stop() + "ms, v=" + v + ']'); Thread.sleep(5000L); for (long l : addrs) GridUnsafe.freeMemory(l); }