Pool.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. // see SynchronizedPool<T> for a threadsafe implementation
  7. class Pool<T> where T : class
  8. {
  9. T[] items;
  10. int count;
  11. public Pool(int maxCount)
  12. {
  13. items = new T[maxCount];
  14. }
  15. public int Count
  16. {
  17. get { return count; }
  18. }
  19. public T Take()
  20. {
  21. if (count > 0)
  22. {
  23. T item = items[--count];
  24. items[count] = null;
  25. return item;
  26. }
  27. else
  28. {
  29. return null;
  30. }
  31. }
  32. public bool Return(T item)
  33. {
  34. if (count < items.Length)
  35. {
  36. items[count++] = item;
  37. return true;
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. }
  44. public void Clear()
  45. {
  46. for (int i = 0; i < count; i++)
  47. items[i] = null;
  48. count = 0;
  49. }
  50. }
  51. }