|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using Oxide.Pooling; |
2 | 3 |
|
3 | 4 | namespace Oxide.Core |
4 | 5 | { |
5 | 6 | public static class ArrayPool |
6 | 7 | { |
7 | | - private const int MaxArrayLength = 50; |
8 | | - private const int InitialPoolAmount = 64; |
9 | | - private const int MaxPoolAmount = 256; |
10 | | - |
11 | | - private static List<Queue<object[]>> _pooledArrays = new List<Queue<object[]>>(); |
| 8 | + private static readonly IArrayPoolProvider<object> pool; |
12 | 9 |
|
13 | 10 | static ArrayPool() |
14 | 11 | { |
15 | | - for (int i = 0; i < MaxArrayLength; i++) |
16 | | - { |
17 | | - _pooledArrays.Add(new Queue<object[]>()); |
18 | | - SetupArrays(i + 1); |
19 | | - } |
20 | | - } |
21 | | - |
22 | | - public static object[] Get(int length) |
23 | | - { |
24 | | - if (length == 0 || length > MaxArrayLength) |
25 | | - { |
26 | | - return new object[length]; |
27 | | - } |
28 | | - |
29 | | - Queue<object[]> arrays = _pooledArrays[length - 1]; |
30 | | - lock (arrays) |
31 | | - { |
32 | | - if (arrays.Count == 0) |
33 | | - { |
34 | | - SetupArrays(length); |
35 | | - } |
36 | | - return arrays.Dequeue(); |
37 | | - } |
| 12 | + pool = Interface.Oxide.PoolFactory.GetArrayProvider<object>(); |
38 | 13 | } |
39 | 14 |
|
40 | | - public static void Free(object[] array) |
41 | | - { |
42 | | - if (array == null || array.Length == 0 || array.Length > MaxArrayLength) |
43 | | - { |
44 | | - return; |
45 | | - } |
| 15 | + [Obsolete("Use Interface.Oxide.PoolFactory")] |
| 16 | + public static object[] Get(int length) => pool.Take(length); |
46 | 17 |
|
47 | | - // Cleanup array |
48 | | - for (int i = 0; i < array.Length; i++) |
49 | | - { |
50 | | - array[i] = null; |
51 | | - } |
52 | | - Queue<object[]> arrays = _pooledArrays[array.Length - 1]; |
53 | | - lock (arrays) |
54 | | - { |
55 | | - if (arrays.Count > MaxPoolAmount) |
56 | | - { |
57 | | - for (int i = 0; i < InitialPoolAmount; i++) |
58 | | - { |
59 | | - arrays.Dequeue(); |
60 | | - } |
61 | | - return; |
62 | | - } |
63 | | - |
64 | | - arrays.Enqueue(array); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - private static void SetupArrays(int length) |
69 | | - { |
70 | | - Queue<object[]> arrays = _pooledArrays[length - 1]; |
71 | | - for (int i = 0; i < InitialPoolAmount; i++) |
72 | | - { |
73 | | - arrays.Enqueue(new object[length]); |
74 | | - } |
75 | | - } |
| 18 | + [Obsolete("Use Interface.Oxide.PoolFactory")] |
| 19 | + public static void Free(object[] array) => pool.Return(array); |
76 | 20 | } |
77 | 21 | } |
0 commit comments