protected override ISegment CreateEmptySegment() { #if UNMANAGED return(PointerSegment.Create(true)); #else return(BufferSegment.Create()); #endif }
bool ISegmentFactory.ReadNext(Message message) { if (remainingWords == 0) { return(false); } ulong word = ReadUInt64(); int segments = (int)(uint)(word) + 1; // layout is: // [count-1][len0] // [len1][len2] // [len3][len4] // ... // [lenN][padding] // so: we can use count/2 as a sneaky way of knowing how many extra words to expect int origin = offsetBytes + 8 * (segments / 2); message.ResetSegments(segments); var buffer = this.buffer; int totalWords = 0; for (int i = 0; i < segments; i++) { if ((i % 2) == 0) { word >>= 32; } else { word = ReadUInt64(); } var segment = (BufferSegment)message.ReuseExistingSegment() ?? BufferSegment.Create(); int len = (int)(uint)(word); segment.Init(buffer, origin, len, len); message.AddSegment(segment); origin += (len << 3); totalWords += len; } // move the base offset past the data ConsumeWords(totalWords); if (origin != offsetBytes) { throw new InvalidOperationException(string.Format("offset mismatch; craziness; {0} vs {1}", offsetBytes, origin)); } return(true); }
ISegment ISegmentFactory.TryAllocate(Message message, int words) { BufferSegment segment = null; if (remainingWords >= words) { // definitely have enough if (segmentWords > words) { words = segmentWords; // allocate an entire segment if (words > remainingWords) { words = remainingWords; // unless that makes us too big } } segment = (BufferSegment)message.ReuseExistingSegment() ?? BufferSegment.Create(); segment.Init(buffer, offsetBytes, words, 0); ConsumeWords(words); message.AddSegment(segment); } return(segment); }
protected override ISegment CreateEmptySegment() { return(BufferSegment.Create()); }