/** * create a new struct array of same type as template * @param size * @return */ public <X extends FSTStruct> StructArray<X> newArray(int size, X templ) { return newArray(size,templ,alloc); }
/** * allocate a Struct instance from an arbitrary template instance. * * @param aTemplate * @param <S> * @return */ public <S extends FSTStruct> S newStruct(S aTemplate) { return newStruct(aTemplate,alloc); }
/** * @param clazz * @param <C> * @return a newly allocated pointer matching. use baseOn to point it to a meaningful location */ public static <C extends FSTStruct> C newPointer(Class<C> clazz) { try { return (C) allocInstance(clazz); } catch (Exception e) { throw new RuntimeException(e); } }
/** * create a new struct array of same type as template * @param size * @return */ public <X extends FSTStruct> StructArray<X> newArray(int size, X templ, BytezAllocator alloc) { StructArray<X> aTemplate = new StructArray<X>(size, templ); int siz = getFactory().calcStructSize(aTemplate); try { if ( siz < chunkSize ) return newStruct(aTemplate); else { return getFactory().toStruct(aTemplate,alloc); } } catch (Throwable e) { throw new RuntimeException(e); } }
retransTemplate.setSeqNo(-1); packetAllocator = new FSTStructAllocator(10, new MallocBytezAllocator()); readBuffer = packetAllocator.newArray(historySize,template); this.nodeId = packetAllocator.newStruct( new StructString(Packet.MAX_NODE_NAME_LEN) ); this.nodeId.setString(theNodeId); FCLog.log("allocating read buffer for topic '"+topicEntry.getTopicId()+"' of "+(readBuffer.getByteSize()/1024)+" KByte"); retrans = packetAllocator.newStruct(retransTemplate); if ( dGramSize < retrans.getByteSize()+10 ) throw new RuntimeException("datagram size must not be smaller than "+retrans.getByteSize()+10 );
public static DataPacket getTemplate(int packetSize) { int emptyPackSize = new FSTStructAllocator(1).newStruct(new DataPacket()).getByteSize(); DataPacket template = new DataPacket(); int payMaxLen = packetSize - emptyPackSize - 2; template.data = new byte[payMaxLen]; return template; }
template.setTopic(topic); offheapAllocator = new FSTStructAllocator(0, new MallocBytezAllocator()); heapAllocator = new FSTStructAllocator(0); hSize = newHist; history = offheapAllocator.newArray(hSize, template); FCLog.log("allocating send buffer for topic " + topicEntry.getTopicId() + " of " + history.getByteSize() / 1024 / 1024 + " MByte"); historySize = history.size();
DatagramPacket p = new DatagramPacket(receiveBuf,receiveBuf.length); ByteBuffer buff = ByteBuffer.wrap(p.getData(), p.getOffset(), p.getLength()); receivedPacket = alloc.newStruct(new Packet()); long idleNanos = System.nanoTime(); receivedPacket.baseOn(receiveBuf, 0); LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(500)); alloc.free(); for (int i = 0; i < receiver.length; i++) { ReceiveBufferDispatcher receiveBufferDispatcher = receiver[i];
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; } }
public void free() { offheapAllocator.free(); }
void decodePacket(DataPacket packet) { // packet.dumpBytes(); if ( receiver == null ) return; if ( isForeignPacket(packet) ) { return; } else { // System.out.println("received "+packet.getReceiver()); } final long packetSeqNo = packet.getSeqNo(); if ( tmpPacket == null ) { tmpPacket = packetAllocator.newPointer(DataPacket.class); // fixme: move to init } packet.dataPointer(tmpStruct); final Bytez dataPacketBase = tmpStruct.getBase(); final int dataindex = (int) tmpStruct.getOffset(); final int packIndex = (int) packet.getOffset(); decodeMsgBytes(packetSeqNo, dataPacketBase, dataindex, packIndex); }
retransTemplate.setSeqNo(-1); packetAllocator = new FSTStructAllocator(10, new MallocBytezAllocator()); readBuffer = packetAllocator.newArray(historySize,template); this.nodeId = packetAllocator.newStruct( new StructString(Packet.MAX_NODE_NAME_LEN) ); this.nodeId.setString(theNodeId); FCLog.log("allocating read buffer for topic '"+topicEntry.getTopicId()+"' of "+(readBuffer.getByteSize()/1024)+" KByte"); retrans = packetAllocator.newStruct(retransTemplate); if ( dGramSize < retrans.getByteSize()+10 ) throw new RuntimeException("datagram size must not be smaller than "+retrans.getByteSize()+10 );
/** * create a new struct array of same type as template * @param size * @return */ public <X extends FSTStruct> StructArray<X> newArray(int size, X templ, BytezAllocator alloc) { StructArray<X> aTemplate = new StructArray<X>(size, templ); int siz = getFactory().calcStructSize(aTemplate); try { if ( siz < chunkSize ) return newStruct(aTemplate); else { return getFactory().toStruct(aTemplate,alloc); } } catch (Throwable e) { throw new RuntimeException(e); } }
public static DataPacket getTemplate(int packetSize) { int emptyPackSize = new FSTStructAllocator(1).newStruct(new DataPacket()).getByteSize(); DataPacket template = new DataPacket(); int payMaxLen = packetSize - emptyPackSize - 2; template.data = new byte[payMaxLen]; return template; }
template.setTopic(topic); offheapAllocator = new FSTStructAllocator(0, new MallocBytezAllocator()); heapAllocator = new FSTStructAllocator(0); hSize = newHist; history = offheapAllocator.newArray(hSize, template); FCLog.log("allocating send buffer for topic " + topicEntry.getTopicId() + " of " + history.getByteSize() / 1024 / 1024 + " MByte"); historySize = history.size();
DatagramPacket p = new DatagramPacket(receiveBuf,receiveBuf.length); ByteBuffer buff = ByteBuffer.wrap(p.getData(), p.getOffset(), p.getLength()); receivedPacket = alloc.newStruct(new Packet()); long idleNanos = System.nanoTime(); receivedPacket.baseOn(receiveBuf, 0); LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(500)); alloc.free(); for (int i = 0; i < receiver.length; i++) { ReceiveBufferDispatcher receiveBufferDispatcher = receiver[i];
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; } }
public void free() { offheapAllocator.free(); }
void decodePacket(DataPacket packet) { // packet.dumpBytes(); if ( receiver == null ) return; if ( isForeignPacket(packet) ) { return; } else { // System.out.println("received "+packet.getReceiver()); } final long packetSeqNo = packet.getSeqNo(); if ( tmpPacket == null ) { tmpPacket = packetAllocator.newPointer(DataPacket.class); // fixme: move to init } packet.dataPointer(tmpStruct); final Bytez dataPacketBase = tmpStruct.getBase(); final int dataindex = (int) tmpStruct.getOffset(); final int packIndex = (int) packet.getOffset(); decodeMsgBytes(packetSeqNo, dataPacketBase, dataindex, packIndex); }
/** * allocate a Struct instance from an arbitrary template instance. * * @param aTemplate * @param <S> * @return */ public <S extends FSTStruct> S newStruct(S aTemplate) { return newStruct(aTemplate,alloc); }