public FSTStruct createPrimitiveArrayBasePointer(Bytez base, long objectBaseOffset /*offset of object containing array*/, int arrayStructIndex /*position of array header in struct*/) { int arrayElementZeroindex = base.getInt(objectBaseOffset+arrayStructIndex); // int len = unsafe.getInt(base,objectBaseOffset+arrayStructIndex+4); FSTStruct structPointer = new FSTStruct(); structPointer.baseOn(base,objectBaseOffset+arrayElementZeroindex,this); return structPointer; }
/** * @param b * @param index * @return a new allocated pointer matching struct type stored in b[] */ public static FSTStruct createStructPointer(Bytez b, int index) { return FSTStructFactory.getInstance().getStructPointerByOffset(b, index).detach(); }
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { FSTStruct str = (FSTStruct) toWrite; if ( ! str.isOffHeap() ) { str = str.toOffHeap(); int byteSize = str.getByteSize(); out.writeInt(byteSize); if ( COMPRESS ) { int intsiz = byteSize/4; for ( int i=0; i<intsiz;i++ ) { int value = str.getInt(); value = (value << 1) ^ (value >> 31); str.___offset+=4; byte aByte = str.getByte(); out.writeByte(aByte); str.___offset++; } else { byte b[] = new byte[byteSize]; // fixme: cache threadlocal str.getBase().getArr(str.getOffset(), b, 0, byteSize); out.write( b, 0, byteSize);
/** * move offsets and memorybase to given pointer and return the pointer. * Eases dereferencing of nested structs without object creation. * If the given pointer is null, a new one will be created (alloc) * <pre> * MyStruct st = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class); * otherStruct.getEmbeddedMyStruct().to(st); * </pre> */ public <T extends FSTStruct> T detachTo(T pointer) { if ( pointer == null ) { return detach(); } if (isOffHeap()) { pointer.___fac = ___fac; pointer.___bytes = ___bytes; pointer.___elementSize = ___elementSize; pointer.___offset = ___offset; return pointer; } return (T) this; }
public boolean isIdenticTo(FSTStruct other) { return other.getBase().equals(___bytes) && other.getAbsoluteOffset() == ___offset; }
public void applySnapshot(FSTStruct target) { Bytez arr = target.getBase(); int baseIdx = (int) target.getOffset(); int snapIdx = 0; for (int i = 0; i < curIndex; i++) { int changeOffset = changeOffsets[i]; int len = changeLength[i]; for ( int ii = 0; ii < len; ii++) { arr.put(baseIdx+changeOffset+ii,snapshot[snapIdx++]); } } }
currentPacketBytePointer.baseOn(dataPacketBase, dataindex); while (true) { short code = currentPacketBytePointer.getShort(); if (code > DataPacket.MAX_CODE || code < 0) { FCLog.get().warn("foreign traffic or error, maxOrdered " + maxOrderedSeq + " packseq " + packetSeqNo + " highest " + highestSeq); System.exit(1); currentPacketBytePointer.next(2); if (code == DataPacket.EOP) { if (isUnordered()||isUnreliable()) { short len = currentPacketBytePointer.getShort(); currentPacketBytePointer.next(2); if (inInitialSync) { if (code == DataPacket.COMPLETE) { decoder.receiveChunk(packetSeqNo, currentPacketBytePointer.getBase(), (int) currentPacketBytePointer.getOffset(), len, code == DataPacket.COMPLETE); currentPacketBytePointer.next(len);
/** * @return a temporary per thread instance of ByteBuffer pointing to this structs data. The length of the * buffer is same as the length of this struct. * * The instance returned is valid until the currentThread calls this method next time. * * Can be used to feed e.g. NIO api. Use ByteBuffer.duplicate() to obtain a non-temporary instance * * @throws NoSuchFieldException * @throws IllegalAccessException */ public ByteBuffer asByteBufferTemporary() { if ( getBase() instanceof MallocBytez ) { MallocBytez base = (MallocBytez) getBase(); ByteBuffer bb = tmpBuf.get(); try { address.setLong(bb, base.getBaseAdress() + getOffset()); capacity.setInt( bb, getByteSize()); } catch (IllegalAccessException e) { FSTUtil.<RuntimeException>rethrow(e); } bb.limit((int) (getOffset() + getByteSize())); bb.position((int) getOffset()); return tmpBuf.get(); } else { // assume HeapBytez. Allocates return ByteBuffer.wrap(getBase().asByteArray(), (int) getOffset(), getByteSize()); } }
public void getBytes(Bytez target, int startIndexInTarget) { if (!isOffHeap()) { throw new RuntimeException("must be offheap to call this"); } if (target.length() < startIndexInTarget + getByteSize()) { throw new RuntimeException("ArrayIndexOutofBounds byte len:" + target.length() + " start+size:" + (startIndexInTarget + getByteSize())); } // unsafe.copyMemory(___bytes,___offset, target, bufoff+startIndexInTarget, getByteSize()); ___bytes.copyTo(target, startIndexInTarget, ___offset, getByteSize()); }
public void fillPrimitiveArrayBasePointer(FSTStruct result, Bytez base, long objectBaseOffset /*offset of object containing array*/, int arrayStructIndex /*position of array header in struct*/) { int arrayElementZeroindex = base.getInt(objectBaseOffset+arrayStructIndex); result.baseOn(base,objectBaseOffset+arrayElementZeroindex,this); }
public int hashCode() { if (!isOffHeap()) return onHeapHashcode(); return ___bytes.getInt(___offset) ^ ___bytes.getInt(___offset + getByteSize() - 4); }
/** * returns a complete copy of this object allocating a new Bytez capable of holding the data. * * @return */ public FSTStruct createCopy() { if (!isOffHeap()) { throw new RuntimeException("must be offheap to call this"); } byte b[] = new byte[getByteSize()]; HeapBytez res = new HeapBytez(b); getBytes(res, 0); return ___fac.createStructWrapper(res, 0); }
@Override public boolean equals(Object obj) { if (obj instanceof FSTStruct) { FSTStruct other = (FSTStruct) obj; final int len = getByteSize(); if (other.getByteSize() == len) { long ix = getOffset(); long ox = other.getOffset(); for (int i = 0; i < len; i++) { if (___bytes.get(i + ix) != other.___bytes.get(i + ox)) return false; } return true; } else return false; } return super.equals(obj); }
public <S extends FSTStruct> S newStruct(S aTemplate, BytezAllocator alloc) { aTemplate = getFactory().toStruct(aTemplate); if (aTemplate.getByteSize()>=chunkSize) return (S)aTemplate.createCopy(); int byteSize = aTemplate.getByteSize(); synchronized (this) { if (chunk == null || chunkIndex+byteSize >= chunk.length()) { chunk = alloc.alloc(chunkSize); if ( DUMP_ALLOC ) System.out.println("[Allocator] sum allocated "+MallocBytezAllocator.alloced.get()/1024/1024+" MB"); chunkIndex = 0; chunkObjCount = 0; } // FSTStruct.unsafe.copyMemory(aTemplate.___bytes, aTemplate.___offset, chunk, FSTStruct.bufoff + chunkIndex, byteSize); aTemplate.___bytes.copyTo(chunk, chunkIndex, aTemplate.___offset, byteSize); S res = (S) getFactory().createStructWrapper(chunk, chunkIndex ); chunkIndex+=byteSize; chunkObjCount++; return res; } }
@NoAssist public E createPointer(int index) { if ( ! isOffHeap() ) throw new RuntimeException("must be offheap to call this"); E res = (E) elemsPointer(); res.___elementSize = res.getByteSize(); res.___offset+=index*res.___elementSize; return res; }
currentPacketBytePointer.baseOn(dataPacketBase, dataindex); while (true) { short code = currentPacketBytePointer.getShort(); if (code > DataPacket.MAX_CODE || code < 0) { FCLog.get().warn("foreign traffic or error, maxOrdered " + maxOrderedSeq + " packseq " + packetSeqNo + " highest " + highestSeq); System.exit(1); currentPacketBytePointer.next(2); if (code == DataPacket.EOP) { if (isUnordered()||isUnreliable()) { short len = currentPacketBytePointer.getShort(); currentPacketBytePointer.next(2); if (inInitialSync) { if (code == DataPacket.COMPLETE) { decoder.receiveChunk(packetSeqNo, currentPacketBytePointer.getBase(), (int) currentPacketBytePointer.getOffset(), len, code == DataPacket.COMPLETE); currentPacketBytePointer.next(len);
/** * @return a temporary per thread instance of ByteBuffer pointing to this structs data. The length of the * buffer is same as the length of this struct. * * The instance returned is valid until the currentThread calls this method next time. * * Can be used to feed e.g. NIO api. Use ByteBuffer.duplicate() to obtain a non-temporary instance * * @throws NoSuchFieldException * @throws IllegalAccessException */ public ByteBuffer asByteBufferTemporary() { if ( getBase() instanceof MallocBytez ) { MallocBytez base = (MallocBytez) getBase(); ByteBuffer bb = tmpBuf.get(); try { address.setLong(bb, base.getBaseAdress() + getOffset()); capacity.setInt( bb, getByteSize()); } catch (IllegalAccessException e) { FSTUtil.<RuntimeException>rethrow(e); } bb.limit((int) (getOffset() + getByteSize())); bb.position((int) getOffset()); return tmpBuf.get(); } else { // assume HeapBytez. Allocates return ByteBuffer.wrap(getBase().asByteArray(), (int) getOffset(), getByteSize()); } }
public void getBytes(Bytez target, int startIndexInTarget) { if (!isOffHeap()) { throw new RuntimeException("must be offheap to call this"); } if (target.length() < startIndexInTarget + getByteSize()) { throw new RuntimeException("ArrayIndexOutofBounds byte len:" + target.length() + " start+size:" + (startIndexInTarget + getByteSize())); } // unsafe.copyMemory(___bytes,___offset, target, bufoff+startIndexInTarget, getByteSize()); ___bytes.copyTo(target, startIndexInTarget, ___offset, getByteSize()); }
public void fillPrimitiveArrayBasePointer(FSTStruct result, Bytez base, long objectBaseOffset /*offset of object containing array*/, int arrayStructIndex /*position of array header in struct*/) { int arrayElementZeroindex = base.getInt(objectBaseOffset+arrayStructIndex); result.baseOn(base,objectBaseOffset+arrayElementZeroindex,this); }