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

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/dev-pomma89'
Browse files Browse the repository at this point in the history
  • Loading branch information
pomma89 committed Apr 2, 2017
2 parents 684e374 + 35b4447 commit c54a20e
Show file tree
Hide file tree
Showing 14 changed files with 361 additions and 115 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for CodeProject.ObjectPool #

### v3.0.2 (2017-04-02) ###

* Moved core pool buffer into dedicated class: Core.PooledObjectBuffer.

### v3.0.1 (2017-03-30) ###

* Breaking change - Pool does not handle minimum capacity anymore.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A generic, concurrent, portable and flexible Object Pool for the .NET Framework,

## Summary ##

* Latest release version: `v3.0.1`
* Latest release version: `v3.0.2`
* Build status on [AppVeyor](https://ci.appveyor.com): [![Build status](https://ci.appveyor.com/api/projects/status/r4qnqaqj9ri6cicn?svg=true)](https://ci.appveyor.com/project/pomma89/objectpool)
* [Doxygen](http://www.stack.nl/~dimitri/doxygen/index.html) documentation:
+ [HTML](https://goo.gl/RVA7mV)
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#---------------------------------#

# version format
version: 3.0.1.{build}
version: 3.0.2.{build}

# branches to build
branches:
Expand All @@ -25,7 +25,7 @@ branches:
assembly_info:
patch: true
file: AssemblyInfo.*
assembly_version: "3.0.1.{build}"
assembly_version: "3.0.2.{build}"
assembly_file_version: "{version}"
assembly_informational_version: "{version}"

Expand Down
15 changes: 12 additions & 3 deletions src/CodeProject.ObjectPool/CodeProject.ObjectPool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<AssemblyName>CodeProject.ObjectPool</AssemblyName>
<AssemblyTitle>Generic and concurrent Object Pool</AssemblyTitle>
<VersionPrefix>3.0.1</VersionPrefix>
<TargetFrameworks>netstandard1.0;netstandard1.1;netstandard1.3;net35;net40;net45</TargetFrameworks>
<VersionPrefix>3.0.2</VersionPrefix>
<TargetFrameworks>netstandard1.0;netstandard1.1;netstandard1.2;netstandard1.3;net35;net40;net45</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>../../pomma89.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand All @@ -22,7 +22,7 @@
<Description>$(Summary) It is completely based on the Code Project article of Ofir Makmal (http://goo.gl/4qig6T). Library is production ready and it is successfully working in real life systems. Original source code has been modified, in order to introduce a Parameterized Object Pool, already drafted by Ofir Makmal in the comments of the article. Moreover, a few unit tests have been added, in order to improve code reliability, and a lot of other small changes have also been applied. Of course, all modified source code is freely available at the project URL of this package. Many thanks to Ofir Makmal for his great work.</Description>
<Authors>Ofir Makmal &lt;ofir.makmal@gmail.com&gt;;Alessio Parma &lt;alessio.parma@gmail.com&gt;</Authors>
<Copyright>Copyright (c) 2013 Ofir Makmal &lt;ofir.makmal@gmail.com&gt;</Copyright>
<PackageReleaseNotes>https://github.com/pomma89/ObjectPool</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/pomma89/ObjectPool/releases</PackageReleaseNotes>
<PackageIconUrl>http://pomma89.altervista.org/objectpool/logo-128.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/pomma89/ObjectPool</PackageProjectUrl>
<PackageLicenseUrl>http://www.codeproject.com/info/cpol10.aspx</PackageLicenseUrl>
Expand All @@ -45,6 +45,11 @@
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.2' ">
<DefineConstants>$(DefineConstants);NETSTD12;LIBLOG_PORTABLE</DefineConstants>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<DefineConstants>$(DefineConstants);NETSTD13;LIBLOG_PORTABLE</DefineConstants>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
Expand Down Expand Up @@ -75,4 +80,8 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
</ItemGroup>

<ItemGroup>
<Folder Include="Extensibility\" />
</ItemGroup>
</Project>
190 changes: 190 additions & 0 deletions src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// File name: PooledObjectBuffer.cs
//
// Author(s): Alessio Parma <alessio.parma@gmail.com>
//
// The MIT License (MIT)
//
// Copyright (c) 2013-2018 Alessio Parma <alessio.parma@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;

namespace CodeProject.ObjectPool.Core
{
/// <summary>
/// A buffer into which pooled objects are stored. This buffer mostly behaves like a queue,
/// even if that behaviour is not stricly respected in order to maximize performance.
/// </summary>
/// <typeparam name="T">
/// The type of the object that which will be managed by the pool. The pooled object have to be
/// a sub-class of PooledObject.
/// </typeparam>
public sealed class PooledObjectBuffer<T> : IEnumerable<T>
where T : PooledObject
{
#if (NET35 || NET40)
private const MethodImplOptions TryToInline = default(MethodImplOptions);
#else
private const MethodImplOptions TryToInline = MethodImplOptions.AggressiveInlining;
#endif

/// <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 = NoObjects;

/// <summary>
/// The maximum capacity of this buffer.
/// </summary>
public int Capacity => _pooledObjects.Length;

/// <summary>
/// The number of objects stored in this buffer.
/// </summary>
public int Count
{
get
{
var count = 0;
for (var i = 0; i < _pooledObjects.Length; ++i)
{
if (_pooledObjects[i] != null) count++;
}
return count;
}
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i <= _pooledObjects.Length; ++i)
{
var item = _pooledObjects[i];
if (item != null)
{
yield return item;
}
}
}

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// Tries to dequeue an object from the buffer.
/// </summary>
/// <param name="pooledObject">Output pooled object.</param>
/// <returns>True if <paramref name="pooledObject"/> has a value, false otherwise.</returns>
[MethodImpl(TryToInline)]
public bool TryDequeue(out T pooledObject)
{
for (var i = 0; i < _pooledObjects.Length; i++)
{
var item = _pooledObjects[i];
if (item != null && Interlocked.CompareExchange(ref _pooledObjects[i], null, item) == item)
{
pooledObject = item;
return true;
}
}
pooledObject = null;
return false;
}

/// <summary>
/// Tries to enqueue given object into the buffer.
/// </summary>
/// <param name="pooledObject">Input pooled object.</param>
/// <returns>True if there was enough space to enqueue given object, false otherwise.</returns>
[MethodImpl(TryToInline)]
public bool TryEnqueue(T pooledObject)
{
for (var i = 0; i < _pooledObjects.Length; i++)
{
ref var item = ref _pooledObjects[i];
if (item == null && Interlocked.CompareExchange(ref item, pooledObject, null) == null)
{
return true;
}
}
return false;
}

/// <summary>
/// Resizes the buffer so that it fits to given capacity. If new capacity is smaller than
/// current capacity, then exceeding items are returned.
/// </summary>
/// <param name="newCapacity">The new capacity of this buffer.</param>
/// <returns>All exceeding items.</returns>
public IList<T> Resize(int newCapacity)
{
if (_pooledObjects == NoObjects)
{
_pooledObjects = new T[newCapacity];
return NoObjects;
}

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

IList<T> exceedingItems = NoObjects;
if (currentCapacity > newCapacity)
{
for (var i = newCapacity; i < currentCapacity; ++i)
{
ref var item = ref _pooledObjects[i];
if (item != null)
{
if (exceedingItems == NoObjects)
{
exceedingItems = new List<T> { item };
}
else
{
exceedingItems.Add(item);
}
item = null;
}
}
}

Array.Resize(ref _pooledObjects, newCapacity);
return exceedingItems;
}
}
}
5 changes: 5 additions & 0 deletions src/CodeProject.ObjectPool/Core/PooledObjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public sealed class PooledObjectInfo
/// </summary>
public int Id { get; internal set; }

/// <summary>
/// Payload which can be used to add custom information to a pooled object.
/// </summary>
public object Payload { get; set; }

/// <summary>
/// Enumeration that is being managed by the pool to describe the object state - primary
/// used to void cases where the resources are being releases twice.
Expand Down
42 changes: 42 additions & 0 deletions src/CodeProject.ObjectPool/ITimedObjectPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// File name: ITimedObjectPool.cs
//
// Author(s): Alessio Parma <alessio.parma@gmail.com>
//
// The MIT License (MIT)
//
// Copyright (c) 2013-2018 Alessio Parma <alessio.parma@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

namespace CodeProject.ObjectPool
{
/// <summary>
/// A pool where objects are automatically removed after a period of inactivity.
/// </summary>
/// <typeparam name="T">
/// The type of the object that which will be managed by the pool. The pooled object have to be
/// a sub-class of PooledObject.
/// </typeparam>
#if NET35
public interface ITimedObjectPool<T> : IObjectPool<T>
#else

public interface ITimedObjectPool<out T> : IObjectPool<T>
#endif
where T : PooledObject
{
}
}
Loading

0 comments on commit c54a20e

Please sign in to comment.