private static void ReclaimSlice(PooledBuffer slice) { switch (slice.Data.Length) { case 16: //most likely lock (SixteenByteCache) SixteenByteCache.Push(slice); return; case 2: lock (TwoByteCache) TwoByteCache.Push(slice); return; case 4: lock (FourByteCache) FourByteCache.Push(slice); return; default: lock (SlowCache) { if (SlowCache.Count > 0) { LinkedListNode <PooledBuffer> node = SlowCache.First; int size = slice.Data.Length; while (node.Next != null) { if (node.Value.Data.Length > size) { node = node.Next; continue; } break; } if (node.Value.Data.Length >= size) { SlowCache.AddAfter(node, slice); } else { SlowCache.AddBefore(node, slice); } } else { SlowCache.AddLast(slice); } } return; } }
public static void ClearCache() { lock (TwoByteCache) TwoByteCache.Clear(); lock (FourByteCache) FourByteCache.Clear(); lock (SixteenByteCache) SixteenByteCache.Clear(); lock (SlowCache) SlowCache.Clear(); }
public static PooledBuffer Get(int size) { switch (size) { case 16: //most likely lock (SixteenByteCache) return(SixteenByteCache.Count > 0 ? SixteenByteCache.Pop() : new PooledBuffer(size)); case 2: lock (TwoByteCache) return(TwoByteCache.Count > 0 ? TwoByteCache.Pop() : new PooledBuffer(size)); case 4: lock (FourByteCache) return(FourByteCache.Count > 0 ? FourByteCache.Pop() : new PooledBuffer(size)); case 0: throw new ArgumentOutOfRangeException(nameof(size)); default: lock (SlowCache) { if (SlowCache.Count > 0) { LinkedListNode <PooledBuffer> node = SlowCache.First; if (node.Value.Data.Length >= size) { SlowCache.Remove(node); node.Value.Reset(size); return(node.Value); } } return(new PooledBuffer(size)); } } }