@@ -20,10 +20,10 @@ namespace KBEngine{
20
20
#define OBJECT_POOL_INIT_SIZE 16
21
21
#define OBJECT_POOL_INIT_MAX_SIZE OBJECT_POOL_INIT_SIZE * 1024
22
22
23
- // 每5分钟检查一次瘦身
23
+ // 每5分钟检查一次瘦身
24
24
#define OBJECT_POOL_REDUCING_TIME_OUT 300 * stampsPerSecondD ()
25
25
26
- // 追踪对象分配处
26
+ // 追踪对象分配处
27
27
#define OBJECTPOOL_POINT fmt::format (" {}#{}" , __FUNCTION__, __LINE__).c_str()
28
28
29
29
template< typename T >
@@ -42,9 +42,9 @@ class ObjectPoolLogPoint
42
42
};
43
43
44
44
/*
45
- 一些对象会非常频繁的被创建, 例如:MemoryStream, Bundle, TCPPacket等等
46
- 这个对象池对通过服务端峰值有效的预估提前创建出一些对象缓存起来,在用到的时候直接从对象池中
47
- 获取一个未被使用的对象即可。
45
+ 一些对象会非常频繁的被创建, 例如:MemoryStream, Bundle, TCPPacket等等
46
+ 这个对象池对通过服务端峰值有效的预估提前创建出一些对象缓存起来,在用到的时候直接从对象池中
47
+ 获取一个未被使用的对象即可。
48
48
*/
49
49
template < typename T, typename THREADMUTEX = KBEngine::thread::ThreadMutexNull >
50
50
class ObjectPool
@@ -134,40 +134,8 @@ class ObjectPool
134
134
}
135
135
136
136
/* *
137
- 强制创建一个指定类型的对象。 如果缓冲里已经创建则返回现有的,否则
138
- 创建一个新的, 这个对象必须是继承自T的。
139
- */
140
- template <typename T1>
141
- T* createObject (const std::string& logPoint)
142
- {
143
- pMutex_->lockMutex ();
144
-
145
- while (true )
146
- {
147
- if (obj_count_ > 0 )
148
- {
149
- T* t = static_cast <T1*>(*objects_.begin ());
150
- objects_.pop_front ();
151
- --obj_count_;
152
- incLogPoint (logPoint);
153
- t->poolObjectCreatePoint (logPoint);
154
- t->onEabledPoolObject ();
155
- t->isEnabledPoolObject (true );
156
- pMutex_->unlockMutex ();
157
- return t;
158
- }
159
-
160
- assignObjs ();
161
- }
162
-
163
- pMutex_->unlockMutex ();
164
-
165
- return NULL ;
166
- }
167
-
168
- /* *
169
- 创建一个对象。 如果缓冲里已经创建则返回现有的,否则
170
- 创建一个新的。
137
+ 创建一个对象。 如果缓冲里已经创建则返回现有的,否则
138
+ 创建一个新的。
171
139
*/
172
140
T* createObject (const std::string& logPoint)
173
141
{
@@ -197,7 +165,7 @@ class ObjectPool
197
165
}
198
166
199
167
/* *
200
- 回收一个对象
168
+ 回收一个对象
201
169
*/
202
170
void reclaimObject (T* obj)
203
171
{
@@ -207,7 +175,7 @@ class ObjectPool
207
175
}
208
176
209
177
/* *
210
- 回收一个对象容器
178
+ 回收一个对象容器
211
179
*/
212
180
void reclaimObject (std::list<T*>& objs)
213
181
{
@@ -225,7 +193,7 @@ class ObjectPool
225
193
}
226
194
227
195
/* *
228
- 回收一个对象容器
196
+ 回收一个对象容器
229
197
*/
230
198
void reclaimObject (std::vector< T* >& objs)
231
199
{
@@ -243,7 +211,7 @@ class ObjectPool
243
211
}
244
212
245
213
/* *
246
- 回收一个对象容器
214
+ 回收一个对象容器
247
215
*/
248
216
void reclaimObject (std::queue<T*>& objs)
249
217
{
@@ -296,15 +264,15 @@ class ObjectPool
296
264
297
265
protected:
298
266
/* *
299
- 回收一个对象
267
+ 回收一个对象
300
268
*/
301
269
void reclaimObject_ (T* obj)
302
270
{
303
271
if (obj != NULL )
304
272
{
305
273
decLogPoint (obj->poolObjectCreatePoint ());
306
274
307
- // 先重置状态
275
+ // 先重置状态
308
276
obj->onReclaimObject ();
309
277
obj->isEnabledPoolObject (false );
310
278
obj->poolObjectCreatePoint (" " );
@@ -325,12 +293,12 @@ class ObjectPool
325
293
326
294
if (obj_count_ <= OBJECT_POOL_INIT_SIZE)
327
295
{
328
- // 小于等于则刷新检查时间
296
+ // 小于等于则刷新检查时间
329
297
lastReducingCheckTime_ = now_timestamp;
330
298
}
331
299
else if (now_timestamp - lastReducingCheckTime_ > OBJECT_POOL_REDUCING_TIME_OUT)
332
300
{
333
- // 长时间大于OBJECT_POOL_INIT_SIZE未使用的对象则开始做清理工作
301
+ // 长时间大于OBJECT_POOL_INIT_SIZE未使用的对象则开始做清理工作
334
302
size_t reducing = std::min (objects_.size (), std::min ((size_t )OBJECT_POOL_INIT_SIZE, (size_t )(obj_count_ - OBJECT_POOL_INIT_SIZE)));
335
303
336
304
// printf("ObjectPool::reclaimObject_(): start reducing..., name=%s, currsize=%d, OBJECT_POOL_INIT_SIZE=%d\n",
@@ -359,28 +327,28 @@ class ObjectPool
359
327
360
328
bool isDestroyed_;
361
329
362
- // 一些原因导致锁还是有必要的
363
- // 例如:dbmgr任务线程中输出log,cellapp中加载navmesh后的线程回调导致的log输出
330
+ // 一些原因导致锁还是有必要的
331
+ // 例如:dbmgr任务线程中输出log,cellapp中加载navmesh后的线程回调导致的log输出
364
332
THREADMUTEX* pMutex_;
365
333
366
334
std::string name_;
367
335
368
336
size_t total_allocs_;
369
337
370
- // Linux环境中,list.size()使用的是std::distance(begin(), end())方式来获得
371
- // 会对性能有影响,这里我们自己对size做一个记录
338
+ // Linux环境中,list.size()使用的是std::distance(begin(), end())方式来获得
339
+ // 会对性能有影响,这里我们自己对size做一个记录
372
340
size_t obj_count_;
373
341
374
- // 最后一次瘦身检查时间
375
- // 如果长达OBJECT_POOL_REDUCING_TIME_OUT大于OBJECT_POOL_INIT_SIZE,则最多瘦身OBJECT_POOL_INIT_SIZE个
342
+ // 最后一次瘦身检查时间
343
+ // 如果长达OBJECT_POOL_REDUCING_TIME_OUT大于OBJECT_POOL_INIT_SIZE,则最多瘦身OBJECT_POOL_INIT_SIZE个
376
344
uint64 lastReducingCheckTime_;
377
345
378
- // 记录的创建位置信息,用于追踪泄露点
346
+ // 记录的创建位置信息,用于追踪泄露点
379
347
std::map<std::string, ObjectPoolLogPoint> logPoints_;
380
348
};
381
349
382
350
/*
383
- 池对象, 所有使用池的对象必须实现回收功能。
351
+ 池对象, 所有使用池的对象必须实现回收功能。
384
352
*/
385
353
class PoolObject
386
354
{
@@ -402,8 +370,8 @@ class PoolObject
402
370
}
403
371
404
372
/* *
405
- 池对象被析构前的通知
406
- 某些对象可以在此做一些工作
373
+ 池对象被析构前的通知
374
+ 某些对象可以在此做一些工作
407
375
*/
408
376
virtual bool destructorPoolObject ()
409
377
{
@@ -432,10 +400,10 @@ class PoolObject
432
400
433
401
protected:
434
402
435
- // 池对象是否处于激活(从池中已经取出)状态
403
+ // 池对象是否处于激活(从池中已经取出)状态
436
404
bool isEnabledPoolObject_;
437
405
438
- // 记录对象创建的位置
406
+ // 记录对象创建的位置
439
407
std::string poolObjectCreatePoint_;
440
408
};
441
409
0 commit comments