Exemple #1
0
        /// <summary>
        /// Creates a new List instance with the selection of all non null cells in the underlying array.
        /// This is a relatively expensive operation, depending on the array length and the AppendIndex value, so
        /// one may consider using the indexer instead.
        /// </summary>
        /// <returns>A read only list of MemoryLane objects.</returns>
        /// <exception cref="ObjectDisposedException">If the MemoryCarriage is disposed.</exception>
        public IReadOnlyList <MemoryLane> GetLanes()
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException("MemoryCarriage");
            }

            return(new List <MemoryLane>(Lanes.NotNullItems()));
        }
Exemple #2
0
 /// <summary>
 /// Calls Dispose to all lanes individually and switches the IsDisposed flag to true,
 /// All methods will throw ObjectDisposedException after that.
 /// </summary>
 public virtual void Dispose()
 {
     if (!Volatile.Read(ref isDisposed))
     {
         try
         {
             if (Lanes != null && Lanes.ItemsCount > 0)
             {
                 foreach (var lane in Lanes.NotNullItems())
                 {
                     lane.Dispose();
                 }
             }
         }
         catch { }
         Volatile.Write(ref isDisposed, true);
     }
 }
Exemple #3
0
        L allocLane(int capacity, bool hidden = false)
        {
            var lanesTotalLength = 0;

            if (Lanes.ItemsCount + 1 > settings.MaxLanesCount)
            {
                if (settings.OnMaxLaneReached != null && settings.OnMaxLaneReached())
                {
                    return(null);
                }
                else
                {
                    throw new MemoryLaneException(MemoryLaneException.Code.MaxLanesCountReached);
                }
            }

            foreach (var l in Lanes.NotNullItems())
            {
                lanesTotalLength += l.LaneCapacity;
            }

            if (lanesTotalLength > settings.MaxTotalAllocatedBytes)
            {
                if (settings.OnMaxTotalBytesReached != null && settings.OnMaxTotalBytesReached())
                {
                    return(null);
                }
                else
                {
                    throw new MemoryLaneException(MemoryLaneException.Code.MaxTotalAllocBytesReached);
                }
            }

            var ml = createLane(capacity);

            if (!hidden)
            {
                Lanes.Append(ml);
            }

            return(ml);
        }
Exemple #4
0
        /// <summary>
        /// Prints all lanes status.
        /// </summary>
        /// <returns>An info string.</returns>
        /// <exception cref="ObjectDisposedException">If the MemoryCarriage is disposed.</exception>
        public virtual string FullTrace()
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException("MemoryCarriage");
            }

            var lc    = Lanes.ItemsCount;
            var lines = new string[lc + 3];
            var i     = 0;

            lines[i++] = $"{this.GetType().Name}";
            lines[i++] = $"Total lanes: {lc}";
            lines[i++] = $"Now-Last allocation tick: {DateTime.Now.Ticks - LastAllocTickAnyLane}";

            foreach (var l in Lanes.NotNullItems())
            {
                lines[i++] = l.FullTrace();
            }

            return(string.Join(Environment.NewLine, lines));
        }