Example #1
0
        ISegment ISegmentFactory.TryAllocate(Message message, int words)
        {
            if (words <= 0)
            {
                throw new ArgumentOutOfRangeException("words");
            }
            int suggested = SuggestAvailable(message, wordOffset, words, Math.Max(words, defaultSegmentWords));

            if (suggested < words)
            {
                return(null);
            }

            // k, we're doing ok!
            ISegment seg = CreateEmptySegment();

            if (seg != null && InitializeSegment(seg, wordOffset, suggested, 0))
            {
                message.AddSegment(seg);
                wordOffset += suggested;
                return(seg);
            }
            if (seg != null)
            {
                seg.Dispose();
            }
            return(null);
        }