-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectPool.h
More file actions
155 lines (133 loc) · 5.65 KB
/
ObjectPool.h
File metadata and controls
155 lines (133 loc) · 5.65 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***********************************************************************
* Software License Agreement (BSD License)
*
* ObjectPool.h
* ObjPool
*
* Copyright 2015 Shashank Hegde (shashank.hegde@icloud.com).
* All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef ObjPool_ObjectPool_h
#define ObjPool_ObjectPool_h
#include <iostream>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
template<size_t MAX_OBJ_SIZE>
class objectPool
{
public:
objectPool(std::size_t maxCapacity, std::size_t minCapacity) :m_maxCapacity(maxCapacity), m_minCapacity(minCapacity) {
t = thread(&objectPool::replenishCapacity, this); // Initialize a seperate thread with replenishCapacity
is_replinshment_complete = false;
}
// default deallocator. standard default_delete would work . Kept debugging.
void voidDelete(void* v_ptr)
{
::operator delete(v_ptr);
}
// clean up the objectPool replenishing thread by setting is_replenshment_complete to true and calling join on the thread.
~objectPool()
{
is_replinshment_complete = true;
if (t.joinable())
t.join();
m_objPool.clear();
delete m_objPool;
}
// used to replenish ObjectPool. Runs on seperate thread. Notified whenever an object is allocated from pool using alloc function.
void replenishCapacity()
{
while(!is_replinshment_complete)
{
std::unique_lock<std::mutex> lk(mutex_ObjPool);
cv.wait(lk);
if (m_objPool.size() <= m_minCapacity) // No of un-allocated objects
{
for (size_t i = 0; i<m_maxCapacity; i++)
{
shared_ptr<void> s_ptr(::operator new(MAX_OBJ_SIZE), std::bind(mem_fn(&objectPool::voidDelete), this, std::placeholders::_1));
m_objPool.push_back(s_ptr);
}
}
lk.unlock();
cv.notify_one();
}
}
// Main alloc calls. Uses obectPool if its not empty. Calls regular new operator if not.
// Ideally sizeOf(OBJECT) < MAX_OBJ_SIZE necessary, but left as description clearly stated this can be assumed.
template< typename OBJECT, typename... ARGS >
void alloc(std::shared_ptr<OBJECT> & object, ARGS ...args)
{
std::unique_lock<std::mutex> locker(mutex_ObjPool);
if (!m_objPool.empty())
{
OBJECT* tmp = new (m_objPool.back().get()) OBJECT(args...);
object = shared_ptr<OBJECT>(tmp,
[&](OBJECT* object) // Will also work on c++11(earlier code worked only on c++14)
{
std::unique_lock<std::mutex> lk1(mutex_ObjPool); // deAlloc functor is not made inline. So make thread safe.
shared_ptr<void> v_ptr = static_cast<shared_ptr<void>>(object);
m_objPool.push_back(v_ptr);
lk1.unlock();
});
m_objPool.pop_back();
}
else
{
object = shared_ptr<OBJECT>(new OBJECT(args...),
[=](OBJECT* object)
{
std::unique_lock<std::mutex> lk1(mutex_ObjPool);
shared_ptr<void> v_ptr = static_cast<shared_ptr<void>>(object);
m_objPool.push_back(v_ptr);
lk1.unlock();
});
}
locker.unlock();
cv.notify_one();
}
mutex getPoolMutex()
{
return mutex_ObjPool;
}
vector<shared_ptr<void>>& getObjPoolVec()
{
return m_objPool;
}
private:
vector<shared_ptr<void>> m_objPool; // Pool Container of void MAX_OBJ_SIZE
const size_t m_maxCapacity;
const size_t m_minCapacity;
bool is_replinshment_complete; // flag to run the objectPool replenishment thread.
std::thread t;
mutex mutex_ObjPool; // Mutex for handling atomic function access to m_objPool
condition_variable cv;
};
#endif