1 | using System;
|
---|
2 |
|
---|
3 | namespace TestPooling.Pool {
|
---|
4 | using System.Collections.Concurrent;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.IO;
|
---|
7 | using System.Linq;
|
---|
8 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
9 |
|
---|
10 | public interface IManagedPool<T> : IDisposable where T : class {
|
---|
11 | T Get();
|
---|
12 | }
|
---|
13 |
|
---|
14 | public class ManagedPoolProvider<T> where T : class {
|
---|
15 | private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
|
---|
16 | private readonly ObjectPool<IManagedPool<T>> managedPools;
|
---|
17 | private T[] DummyPartition;
|
---|
18 |
|
---|
19 | public readonly int PartitionSize;
|
---|
20 | public readonly int MaxParitionCount;
|
---|
21 | public const int DefaultMaxInstanceCount = 16384;
|
---|
22 |
|
---|
23 | public ManagedPoolProvider(int partitionSize, int maxPartitionCount = -1) {
|
---|
24 | PartitionSize = partitionSize;
|
---|
25 |
|
---|
26 | if (maxPartitionCount <= 0) {
|
---|
27 | MaxParitionCount = DefaultMaxInstanceCount / PartitionSize;
|
---|
28 | }
|
---|
29 |
|
---|
30 | managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool2<T>(this));
|
---|
31 | }
|
---|
32 | public void InitDummyPartition(Func<T> factory) {
|
---|
33 | DummyPartition = new T[PartitionSize];
|
---|
34 |
|
---|
35 | for (var i = 0; i < PartitionSize; i++) {
|
---|
36 | DummyPartition[i] = factory();
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public int InstanceCount { get { return partitions.Count * PartitionSize; } }
|
---|
41 |
|
---|
42 | public void ReleasePartitions(params T[][] partition) {
|
---|
43 | if (partition.Length > 0) partitions.PushRange(partition);
|
---|
44 | }
|
---|
45 |
|
---|
46 | private T[] GetPartition() {
|
---|
47 | T[] partition;
|
---|
48 | return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition);
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static T[] DeepArrayCopy(object objectToCopy) {
|
---|
52 | using (var memoryStream = new MemoryStream()) {
|
---|
53 | var binaryFormatter = new BinaryFormatter();
|
---|
54 |
|
---|
55 | binaryFormatter.Serialize(memoryStream, objectToCopy);
|
---|
56 | memoryStream.Seek(0, SeekOrigin.Begin);
|
---|
57 |
|
---|
58 | return (T[])binaryFormatter.Deserialize(memoryStream);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public IManagedPool<T> CreatePool() {
|
---|
63 | return managedPools.Allocate();
|
---|
64 | }
|
---|
65 |
|
---|
66 | private class ManagedPool2<T> : IManagedPool<T> where T : class {
|
---|
67 | private readonly ManagedPoolProvider<T> provider;
|
---|
68 | private readonly IList<T[]> partitions = new List<T[]>();
|
---|
69 | private T[] currentPartition;
|
---|
70 | private int entryIndex;
|
---|
71 |
|
---|
72 | public ManagedPool2(ManagedPoolProvider<T> provider) {
|
---|
73 | this.provider = provider;
|
---|
74 | entryIndex = provider.PartitionSize;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public T Get() {
|
---|
78 | if (entryIndex == provider.PartitionSize) {
|
---|
79 | currentPartition = provider.GetPartition();
|
---|
80 | partitions.Add(currentPartition);
|
---|
81 | entryIndex = 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return currentPartition[entryIndex++];
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void Dispose() {
|
---|
88 | provider.ReleasePartitions(partitions.ToArray());
|
---|
89 | partitions.Clear();
|
---|
90 | provider.managedPools.Free(this);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|