Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
pomma89 committed Apr 2, 2017
1 parent 21750fa commit 787a509
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
23 changes: 14 additions & 9 deletions src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ namespace CodeProject.ObjectPool.Core
public sealed class PooledObjectBuffer<T>
where T : PooledObject
{
/// <summary>
/// Used as default value for <see cref="_pooledObjects"/>.
/// </summary>
private static readonly T[] NoObjects = new T[0];

/// <summary>
/// The concurrent buffer containing pooled objects.
/// </summary>
private T[] _pooledObjects;
private T[] _pooledObjects = NoObjects;

public int Capacity => _pooledObjects.Length;

Expand Down Expand Up @@ -107,24 +112,24 @@ public bool TryEnqueue(T pooledObject)
return false;
}

public void ResizeBuffer(int newSize)
public void Resize(int newCapacity)
{
if (_pooledObjects == null)
if (_pooledObjects == NoObjects)
{
_pooledObjects = new T[newSize];
_pooledObjects = new T[newCapacity];
return;
}

var currentSize = _pooledObjects.Length;
if (currentSize == newSize)
var currentCapacity = _pooledObjects.Length;
if (currentCapacity == newCapacity)
{
// Nothing to do.
return;
}

if (currentSize > newSize)
if (currentCapacity > newCapacity)
{
for (var i = newSize; i < currentSize; ++i)
for (var i = newCapacity; i < currentCapacity; ++i)
{
ref var item = ref _pooledObjects[i];
if (item != null)
Expand All @@ -135,7 +140,7 @@ public void ResizeBuffer(int newSize)
}
}

Array.Resize(ref _pooledObjects, newSize);
Array.Resize(ref _pooledObjects, newCapacity);
}
}
}
2 changes: 1 addition & 1 deletion src/CodeProject.ObjectPool/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int MaximumPoolSize
// Preconditions
Raise.ArgumentOutOfRangeException.If(value < 1, nameof(value), ErrorMessages.NegativeOrZeroMaximumPoolSize);

_pooledObjects.ResizeBuffer(value);
_pooledObjects.Resize(value);
}
}

Expand Down
37 changes: 33 additions & 4 deletions src/CodeProject.ObjectPool/PooledObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
*/

using CodeProject.ObjectPool.Core;
using PommaLabs.Thrower.Goodies;
using System;
using System.Collections.Generic;

#if !NET35

Expand All @@ -23,7 +25,7 @@ namespace CodeProject.ObjectPool
/// PooledObject base class.
/// </summary>
[Serializable]
public abstract class PooledObject : IDisposable
public abstract class PooledObject : EquatableObject<PooledObject>, IDisposable
{
#region Logging

Expand Down Expand Up @@ -135,13 +137,10 @@ protected virtual void OnReleaseResources()

#region Returning object to pool - Dispose and Finalizer

#pragma warning disable CC0029 // Disposables Should Call Suppress Finalize

/// <summary>
/// See <see cref="IDisposable"/> docs.
/// </summary>
public void Dispose()
#pragma warning restore CC0029 // Disposables Should Call Suppress Finalize
{
// Returning to pool
HandleReAddingToPool(false);
Expand Down Expand Up @@ -186,5 +185,35 @@ private void HandleReAddingToPool(bool reRegisterForFinalization)
}

#endregion Returning object to pool - Dispose and Finalizer

#region Formatting and equality

/// <summary>
/// Returns all property (or field) values, along with their names, so that they can be
/// used to produce a meaningful <see cref="object.ToString"/>.
/// </summary>
/// <returns>
/// All property (or field) values, along with their names, so that they can be used to
/// produce a meaningful <see cref="object.ToString"/>.
/// </returns>
protected override IEnumerable<KeyValuePair<string, object>> GetFormattingMembers()
{
yield return new KeyValuePair<string, object>(nameof(PooledObjectInfo.Id), PooledObjectInfo.Id);
}

/// <summary>
/// Returns all property (or field) values that should be used inside
/// <see cref="IEquatable{T}.Equals(T)"/> or <see cref="object.GetHashCode"/>.
/// </summary>
/// <returns>
/// All property (or field) values that should be used inside
/// <see cref="IEquatable{T}.Equals(T)"/> or <see cref="object.GetHashCode"/>.
/// </returns>
protected override IEnumerable<object> GetIdentifyingMembers()
{
yield return PooledObjectInfo.Id;
}

#endregion Formatting and equality
}
}

0 comments on commit 787a509

Please sign in to comment.