private void AllocateWriteHeadUnsynchronized(int sizeHint) { _operationState.BeginWrite(); if (_writingHead == null) { // We need to allocate memory to write since nobody has written before BufferSegment newSegment = CreateSegmentUnsynchronized(); newSegment.SetMemory(_pool.Rent(GetSegmentSize(sizeHint))); // Set all the pointers _writingHead = _readHead = _readTail = newSegment; } else { int bytesLeftInBuffer = _writingHead.WritableBytes; if (bytesLeftInBuffer == 0 || bytesLeftInBuffer < sizeHint) { BufferSegment newSegment = CreateSegmentUnsynchronized(); newSegment.SetMemory(_pool.Rent(GetSegmentSize(sizeHint))); _writingHead.SetNext(newSegment); _writingHead = newSegment; } } }
private void AllocateWriteHeadSynchronized(int sizeHint) { lock (_sync) { _operationState.BeginWrite(); if (_writingHead == null) { // We need to allocate memory to write since nobody has written before BufferSegment newSegment = AllocateSegment(sizeHint); // Set all the pointers _writingHead = _readHead = _readTail = _lastExamined = newSegment; } else { int bytesLeftInBuffer = _writingMemory.Length; if (bytesLeftInBuffer == 0 || bytesLeftInBuffer < sizeHint) { if (_buffered > 0) { // Flush buffered data to the segment _writingHead.End += _buffered; _buffered = 0; } BufferSegment newSegment = AllocateSegment(sizeHint); _writingHead.SetNext(newSegment); _writingHead = newSegment; } } } }
private void AllocateWriteHeadSynchronized(int sizeHint) { lock (SyncObj) { _operationState.BeginWrite(); if (_writingHead == null) { // We need to allocate memory to write since nobody has written before BufferSegment newSegment = AllocateSegment(sizeHint); // Set all the pointers _writingHead = _readHead = _readTail = newSegment; _lastExaminedIndex = 0; } else { int bytesLeftInBuffer = _writingHeadMemory.Length; if (bytesLeftInBuffer == 0 || bytesLeftInBuffer < sizeHint) { if (_writingHeadBytesBuffered > 0) { // Flush buffered data to the segment _writingHead.End += _writingHeadBytesBuffered; _writingHeadBytesBuffered = 0; } if (_writingHead.Length == 0) { // If we got here that means Advance was called with 0 bytes or GetMemory was called again without any writes occurring // And, the newly requested memory size is greater than our unused segments internal memory buffer // So we should reuse the BufferSegment and replace the memory it's holding, this way ReadAsync will not receive a buffer with one segment being empty _writingHead.ResetMemory(); RentMemory(_writingHead, sizeHint); } else { BufferSegment newSegment = AllocateSegment(sizeHint); _writingHead.SetNext(newSegment); _writingHead = newSegment; } } } } }