/// <summary> /// Used to indicate that this buffer is being abandoned so that its storage may be relcaimed during a future GC cycle. /// Sets the byteArray to null and the byteArraySize to zero. /// </summary> public void Release(QpcTimeStamp qpcTimeStamp, string reason = null) { header = BufferHeaderV1.Empty; bufferPool = null; byteArray = null; byteArraySize = 0; SetState(qpcTimeStamp, Buffers.BufferState.Released, reason); }
public Message(Buffers.BufferPool bufferSourcePool, Logging.IMesgEmitter issueEmitter = null, Logging.IMesgEmitter stateEmitter = null) { this.bufferSourcePool = bufferSourcePool; StateEmitter = stateEmitter ?? bufferSourcePool.BufferStateEmitter ?? Logging.NullMesgEmitter.Instance; IssueEmitter = issueEmitter ?? StateEmitter; }
public override void Write(byte[] buffer, int offset, int count) { if (!buffer.IsSafeIndex(offset, length: count)) { throw new System.ArgumentException("Invalid offset/count combination [bufferSize:{0} offset:{1} count:{2}]".CheckedFormat(buffer.SafeLength(), offset, count)); } Buffers.BufferPool useBufferPool = message.bufferSourcePool ?? fallbackDefaultBufferPool; int getFromOffset = offset; int totalCountTransferred = 0; for (; totalCountTransferred < count;) { if (currentBuffer == null) { QpcTimeStamp tsNow = QpcTimeStamp.Now; currentBuffer = useBufferPool.Acquire(tsNow); if (currentBuffer == null) { throw new System.OutOfMemoryException("Unable to acquire a buffer from the message bufferPool"); } bufferList.Add(currentBuffer); if (message.State == MessageState.Initial) { message.SetState(tsNow, MessageState.Data, "BufferListWriteStream.Write"); } } int iterCopyCount = Math.Min(count - totalCountTransferred, currentBuffer.AvailableSpace); byte[] copyToBuffer = currentBuffer.byteArray; if (iterCopyCount <= 8) { int copyToOffset = currentBuffer.byteCount; for (int idx = 0; idx < iterCopyCount; idx++) { copyToBuffer[copyToOffset++] = buffer[getFromOffset++]; } } else { System.Buffer.BlockCopy(buffer, getFromOffset, copyToBuffer, currentBuffer.byteCount, iterCopyCount); getFromOffset += iterCopyCount; } currentBuffer.byteCount += iterCopyCount; if (currentBuffer.AvailableSpace <= 0) { currentBuffer = null; } totalCountTransferred += iterCopyCount; } }