This repository was archived by the owner on Jun 24, 2025. It is now read-only.
forked from krockot/Unity-TaskManager
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathListPool.cs
More file actions
72 lines (62 loc) · 2.04 KB
/
Copy pathListPool.cs
File metadata and controls
72 lines (62 loc) · 2.04 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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace TexDrawLib
{
//Main Class Stack Manager
public static class ListPool<T>
{
// Object pool to avoid allocations.
private static readonly ObjectPool<List<T>> s_ListPool = new ObjectPool<List<T>>();
public static List<T> Get()
{
return s_ListPool.Get();
}
public static void Release(List<T> toRelease)
{
if(toRelease.Count > 0 && toRelease[0] is IFlushable)
{
for (int i = 0; i < toRelease.Count; i++)
{
IFlushable obj = (IFlushable)toRelease[i];
(obj).Flush();
}
}
toRelease.Clear();
s_ListPool.Release(toRelease);
}
//Advanced purposes only
public static void ReleaseNoFlush(List<T> toRelease)
{
toRelease.Clear();
s_ListPool.Release(toRelease);
}
}
internal static class ObjPool<T> where T : class, IFlushable, new()
{
// Object pool to avoid allocations.
private static readonly ObjectPool<T> s_ObjPool = new ObjectPool<T>();
public static T Get()
{
T obj = s_ObjPool.Get();
obj.SetFlushed(false);
return obj;
}
public static void Release(T toRelease)
{
if(toRelease.GetFlushed())
return;
toRelease.SetFlushed(true);
s_ObjPool.Release(toRelease);
}
}
//Interface to get a class to be flushable (flush means to be released to the main class stack
//when it's unused, later if code need a new instance, the main stack will give this class back
//instead of creating a new instance (which later introducing Memory Garbages)).
internal interface IFlushable
{
bool GetFlushed();
void SetFlushed(bool flushed);
void Flush();
}
}