You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
using System;
|
|
|
|
using ProtoCSStruct;
|
|
|
|
namespace Sog
|
|
{
|
|
|
|
/// <summary>
|
|
/// cache一下结构体消息,每次在栈上new的话有个问题,c#new的时候会memset整个结构体,效率极低
|
|
/// 这么用要注意的情况是线程不安全且同时多个使用的时候有数量限制
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
public class CSStructPool<T> : Singleton<CSStructPool<T>> where T : struct, IStructMessage<T>
|
|
{
|
|
class WrapperObj<S> where S : struct, IStructMessage<S>
|
|
{
|
|
S _object;
|
|
|
|
public ref S GetObj()
|
|
{
|
|
return ref _object;
|
|
}
|
|
}
|
|
|
|
int _index;
|
|
const int MaxPoolCount = 2;
|
|
WrapperObj<T>[] _objects = new WrapperObj<T>[MaxPoolCount];
|
|
|
|
public CSStructPool()
|
|
{
|
|
for (int i = 0; i < _objects.Length; i++)
|
|
{
|
|
_objects[i] = new WrapperObj<T>();
|
|
}
|
|
}
|
|
|
|
|
|
public ref T GetObjRef()
|
|
{
|
|
ref T obj = ref _objects[_index].GetObj();
|
|
obj.Clear();
|
|
|
|
_index++;
|
|
|
|
if(_index >= MaxPoolCount)
|
|
{
|
|
_index = 0;
|
|
}
|
|
|
|
return ref obj;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|