Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the Buffer class.
        /// </summary>
        public Buffer()
        {
            if (sizeExponent < 0 || 31 < sizeExponent)
            {
                throw new ArgumentOutOfRangeException();
            }
            blocks = new List<Segment>();

            blocks.Add(SegmentPool.Acquire());

            currentIndex = 0;
            current = blocks[currentIndex];
            position = 0;
            back = 0;
            front = 0;

            marker = -1;
        }
Exemplo n.º 2
0
        public Buffer()
        {
            int blockSizeExponent = SegmentPool.SegmentSizeExponent;
            if (blockSizeExponent < 0 || 31 < blockSizeExponent)
            {
                throw new ArgumentOutOfRangeException();
            }
            blocks = new List<Segment>();
            this.blockSizeExponent = blockSizeExponent;
            remainderMask = ~(~0 << blockSizeExponent);

            blocks.Add(SegmentPool.Acquire());

            currentBlockIndex = 0;
            currentBlock = blocks[currentBlockIndex];
            position = 0;
            back = 0;
            front = 0;

            marker = -1;
        }
Exemplo n.º 3
0
 public bool Equals(Segment obj)
 {
     return obj.array == array && obj.offset == offset;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Returns the specified segment back to the pool.
 /// </summary>
 public static void Release(Segment segment)
 {
     using (new ReadLock(rwlock))
     {
         for (int i = 0, count = pools.Count; i < count; ++i)
         {
             if (pools[i].Release(segment))
             {
                 return;
             }
         }
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Acquires an avilable segment from the pool.
 /// </summary>
 public static Segment Acquire()
 {
     Segment result = new Segment();
     using (new UpgradeableReadLock(rwlock))
     {
         for (int i = 0, count = pools.Count; i < count; ++i)
         {
             if (pools[i].Acquire(ref result))
             {
                 return result;
             }
         }
         using (new WriteLock(rwlock))
         {
             for (int i = 0, count = pools.Count; i < count; ++i)
             {
                 if (pools[i].Acquire(ref result))
                 {
                     return result;
                 }
             }
             var pool = new SegmentedBuffer();
             pools.Add(pool);
             pool.Acquire(ref result);
         }
     }
     return result;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Tries to return the specified segment back to the pool.
 /// </summary>
 /// <returns>true if successful, false if the specified segment does not
 /// belong to this pool.</returns>
 public bool Release(Segment segment)
 {
     if (segment.Array != buffer)
     {
         return false;
     }
     lock (available)
     {
         available.Push(segment.Offset);
     }
     return true;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Tries to acquire an available buffer segment.
        /// </summary>
        /// <returns>true if successful, false if there is no available segment.
        /// </returns>
        public bool Acquire(ref Segment segment)
        {
            lock (available)
            {
                if (available.Count > 0)
                {
                    segment = new Segment(buffer, available.Pop());
                    return true;
                }
            }

            int position;
            lock (syncRoot)
            {
                if ((chunkSize - segmentSize) < offset)
                {
                    return false;
                }
                position = offset;
                offset += segmentSize;
            }
            segment = new Segment(buffer, position);
            return true;
        }
Exemplo n.º 8
0
 public bool Release(Segment segment)
 {
     if (segment.Array != buffer)
     {
         return false;
     }
     lock (freeOffsetPool)
     {
         freeOffsetPool.Push(segment.Offset);
     }
     return true;
 }
Exemplo n.º 9
0
        public bool Acquire(ref Segment segment)
        {
            lock (freeOffsetPool)
            {
                if (freeOffsetPool.Count > 0)
                {
                    segment = new Segment(buffer, freeOffsetPool.Pop());
                    return true;
                }
            }

            int offset;
            lock (syncRoot)
            {
                if ((SegmentPool.BufferSize - SegmentPool.SegmentSize) < currentOffset)
                {
                    return false;
                }

                offset = currentOffset;
                currentOffset += SegmentPool.SegmentSize;
            }
            segment = new Segment(buffer, offset);
            return true;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the UdpLink class.
 /// </summary>
 public UdpLink(string name)
     : base(name)
 {
     rxSegment = rxBuffer.FirstSegment;
     txSegment = txBuffer.FirstSegment;
 }
Exemplo n.º 11
0
 private void Cleanup()
 {
     if (blocks.Count == 0)
     {
         return;
     }
     for (int i = 0, count = blocks.Count; i < count; ++i)
     {
         SegmentPool.Release(blocks[i]);
     }
     blocks.Clear();
     current = new Segment();
 }
Exemplo n.º 12
0
 private void BlockFeed()
 {
     if (((position & remainderMask) == 0) &&
         ((position & ~remainderMask) != 0))
     {
         current = blocks[++currentIndex];
     }
 }