ThreadSafeFreeList.h
911 字节
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
#pragma once
#include "Baselib.h"
#include "Cpp/mpmc_node_queue.h"
namespace il2cpp
{
namespace utils
{
typedef baselib::mpmc_node ThreadSafeFreeListNode;
/// Lockless allocator that keeps instances of T on a free list.
///
/// T must be derived from ThreadSafeFreeListNode.
///
template<typename T>
struct ThreadSafeFreeList
{
T* Allocate()
{
T* instance = m_NodePool.try_pop_front();
if (!instance)
instance = new T();
return instance;
}
void Release(T* instance)
{
m_NodePool.push_back(instance);
}
~ThreadSafeFreeList()
{
T* instance;
while ((instance = m_NodePool.try_pop_front()) != NULL)
delete instance;
}
private:
baselib::mpmc_node_queue<T> m_NodePool;
};
} /* utils */
} /* il2cpp */