-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathObjectPoolAllocator.hpp
More file actions
89 lines (73 loc) · 1.53 KB
/
ObjectPoolAllocator.hpp
File metadata and controls
89 lines (73 loc) · 1.53 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
#ifndef MS_OBJECT_POOL_ALLOCATOR_HPP
#define MS_OBJECT_POOL_ALLOCATOR_HPP
// #define MS_ALLOCATOR_FREE_ON_RETURN 1
#include "common.hpp"
#include <vector>
namespace Utils
{
// Simple implementation of object pool only for single objects.
// Arrays are allocated as usual.
template<typename T>
class ObjectPoolAllocator
{
std::shared_ptr<std::vector<T*>> pool_data;
public:
typedef T value_type;
thread_local static Utils::ObjectPoolAllocator<T> Pool;
ObjectPoolAllocator()
{
pool_data = std::shared_ptr<std::vector<T*>>(
new std::vector<T*>(),
[](std::vector<T*>* pool)
{
for (auto* ptr : *pool)
{
std::free(ptr);
}
delete pool;
});
}
template<typename U>
ObjectPoolAllocator(const ObjectPoolAllocator<U>& other)
: pool_data(ObjectPoolAllocator<T>::Pool.pool_data)
{
}
~ObjectPoolAllocator()
{
}
T* allocate(size_t n)
{
if (n > 1)
{
return static_cast<T*>(std::malloc(sizeof(T) * n));
}
if (this->pool_data->empty())
{
return static_cast<T*>(std::malloc(sizeof(T)));
}
T* ptr = this->pool_data->back();
this->pool_data->pop_back();
return ptr;
}
void deallocate(T* ptr, size_t n)
{
if (!ptr)
{
return;
}
if (n > 1)
{
std::free(ptr);
return;
}
#ifdef MS_ALLOCATOR_FREE_ON_RETURN
std::free(ptr);
#else
this->pool_data->push_back(ptr);
#endif
}
};
template<typename T>
thread_local Utils::ObjectPoolAllocator<T> Utils::ObjectPoolAllocator<T>::Pool;
} // namespace Utils
#endif