forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPooledObject.cs
More file actions
136 lines (113 loc) · 4.26 KB
/
PooledObject.cs
File metadata and controls
136 lines (113 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace ServiceStack.Text.Pools
{
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// this is RAII object to automatically release pooled object when its owning pool
/// </summary>
public struct PooledObject<T> : IDisposable where T : class
{
private readonly Action<ObjectPool<T>, T> _releaser;
private readonly ObjectPool<T> _pool;
private T _pooledObject;
public PooledObject(ObjectPool<T> pool, Func<ObjectPool<T>, T> allocator, Action<ObjectPool<T>, T> releaser) : this()
{
_pool = pool;
_pooledObject = allocator(pool);
_releaser = releaser;
}
public T Object
{
get
{
return _pooledObject;
}
}
public void Dispose()
{
if (_pooledObject != null)
{
_releaser(_pool, _pooledObject);
_pooledObject = null;
}
}
#region factory
public static PooledObject<StringBuilder> Create(ObjectPool<StringBuilder> pool)
{
return new PooledObject<StringBuilder>(pool, Allocator, Releaser);
}
public static PooledObject<Stack<TItem>> Create<TItem>(ObjectPool<Stack<TItem>> pool)
{
return new PooledObject<Stack<TItem>>(pool, Allocator, Releaser);
}
public static PooledObject<Queue<TItem>> Create<TItem>(ObjectPool<Queue<TItem>> pool)
{
return new PooledObject<Queue<TItem>>(pool, Allocator, Releaser);
}
public static PooledObject<HashSet<TItem>> Create<TItem>(ObjectPool<HashSet<TItem>> pool)
{
return new PooledObject<HashSet<TItem>>(pool, Allocator, Releaser);
}
public static PooledObject<Dictionary<TKey, TValue>> Create<TKey, TValue>(ObjectPool<Dictionary<TKey, TValue>> pool)
{
return new PooledObject<Dictionary<TKey, TValue>>(pool, Allocator, Releaser);
}
public static PooledObject<List<TItem>> Create<TItem>(ObjectPool<List<TItem>> pool)
{
return new PooledObject<List<TItem>>(pool, Allocator, Releaser);
}
#endregion
#region allocators and releasers
private static StringBuilder Allocator(ObjectPool<StringBuilder> pool)
{
return pool.AllocateAndClear();
}
private static void Releaser(ObjectPool<StringBuilder> pool, StringBuilder sb)
{
pool.ClearAndFree(sb);
}
private static Stack<TItem> Allocator<TItem>(ObjectPool<Stack<TItem>> pool)
{
return pool.AllocateAndClear();
}
private static void Releaser<TItem>(ObjectPool<Stack<TItem>> pool, Stack<TItem> obj)
{
pool.ClearAndFree(obj);
}
private static Queue<TItem> Allocator<TItem>(ObjectPool<Queue<TItem>> pool)
{
return pool.AllocateAndClear();
}
private static void Releaser<TItem>(ObjectPool<Queue<TItem>> pool, Queue<TItem> obj)
{
pool.ClearAndFree(obj);
}
private static HashSet<TItem> Allocator<TItem>(ObjectPool<HashSet<TItem>> pool)
{
return pool.AllocateAndClear();
}
private static void Releaser<TItem>(ObjectPool<HashSet<TItem>> pool, HashSet<TItem> obj)
{
pool.ClearAndFree(obj);
}
private static Dictionary<TKey, TValue> Allocator<TKey, TValue>(ObjectPool<Dictionary<TKey, TValue>> pool)
{
return pool.AllocateAndClear();
}
private static void Releaser<TKey, TValue>(ObjectPool<Dictionary<TKey, TValue>> pool, Dictionary<TKey, TValue> obj)
{
pool.ClearAndFree(obj);
}
private static List<TItem> Allocator<TItem>(ObjectPool<List<TItem>> pool)
{
return pool.AllocateAndClear();
}
private static void Releaser<TItem>(ObjectPool<List<TItem>> pool, List<TItem> obj)
{
pool.ClearAndFree(obj);
}
#endregion
}
}