-
Notifications
You must be signed in to change notification settings - Fork 0
/
HCPPObject.cpp
448 lines (401 loc) · 11.1 KB
/
HCPPObject.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "HCPPObject.h"
#include <map>
#include <mutex>
#include <algorithm>
//对象池操作
extern void HCPPObjectPool_ObjectDelete(void *ptr);
namespace HCPPObjectGlobal
{
class HCPPObjectInfo
{
std::thread::id _tid;
public:
HCPPObjectInfo();
HCPPObjectInfo(const HCPPObjectInfo&& other);
HCPPObjectInfo& operator =(const HCPPObjectInfo&& other);
HCPPObjectInfo(const HCPPObjectInfo& other);
HCPPObjectInfo& operator =(const HCPPObjectInfo& other);
std::thread::id& GetThreadId();
bool SetThreadId(std::thread::id _id, bool force_update = false);
};
extern std::map<void *,HCPPObjectInfo> CPPHeapObjPool;
extern std::recursive_mutex CPPHeapObjPoolLock;
}
static void AddHCPPObject(void *ptr)
{
std::lock_guard<std::recursive_mutex> lock(HCPPObjectGlobal::CPPHeapObjPoolLock);
HCPPObjectGlobal::CPPHeapObjPool[ptr]=HCPPObjectGlobal::HCPPObjectInfo();
}
static void RemoveHCPPObject(void *ptr)
{
HCPPObjectPool_ObjectDelete(ptr);
std::lock_guard<std::recursive_mutex> lock(HCPPObjectGlobal::CPPHeapObjPoolLock);
auto it=HCPPObjectGlobal::CPPHeapObjPool.find(ptr);
if(it!=HCPPObjectGlobal::CPPHeapObjPool.end())
{
HCPPObjectGlobal::CPPHeapObjPool.erase(it);
}
}
static void UpdateHCPPObject(void *ptr,HCPPObjectGlobal::HCPPObjectInfo &info)
{
RemoveHCPPObject(ptr);
std::lock_guard<std::recursive_mutex> lock(HCPPObjectGlobal::CPPHeapObjPoolLock);
HCPPObjectGlobal::CPPHeapObjPool[ptr]=info;
}
static bool HasHCPPObject(void *ptr)
{
return HCPPObjectGlobal::CPPHeapObjPool.find(ptr)!=HCPPObjectGlobal::CPPHeapObjPool.end();
}
void HCPPObject::RemoveFromChildList(HCPPObject * child)
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
auto it=std::find(m_child_list.begin(),m_child_list.end(),child);
if(it!=m_child_list.end())
{
m_child_list.erase(it);
}
}
void HCPPObject::AddToChildList(HCPPObject * child)
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
m_child_list.push_back(child);
}
HCPPObject::HCPPObject(HCPPObject *parent):m_parent(parent),m_lock(new std::recursive_mutex)
{
for(size_t i=0; i<sizeof(flags)/sizeof(flags[0]); i++)
{
flags[i]=0;
}
if(m_parent!=NULL)
{
//添加到父对象
m_parent->AddToChildList(this);
}
}
HCPPObject::HCPPObject(HCPPObject &other):m_parent(other.m_parent),m_lock(new std::recursive_mutex)
{
for(size_t i=0; i<sizeof(flags)/sizeof(flags[0]); i++)
{
flags[i]=0;
}
if(m_parent!=NULL)
{
//添加到父对象
m_parent->AddToChildList(this);
}
}
HCPPObject::HCPPObject(HCPPObject &&other):m_parent(other.m_parent),m_lock(new std::recursive_mutex)
{
for(size_t i=0; i<sizeof(flags)/sizeof(flags[0]); i++)
{
flags[i]=0;
}
if(m_parent!=NULL)
{
//添加到父对象
m_parent->AddToChildList(this);
}
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
std::lock_guard<std::recursive_mutex> lock_other(*other.m_lock);
auto m_child_list_copy=other.m_child_list;//由于过程中会修改m_child_list,故使用副本进行遍历
for(std::list<HCPPObject*>::iterator it=m_child_list_copy.begin(); it!=m_child_list_copy.end(); it++)
{
HCPPObject *child=(*it);
if(child!=NULL)
{
child->SetParent(this,true);
}
}
}
{
//移动标志位
for(size_t i=0; i<sizeof(flags); i++)
{
flags[i]=other.flags[i];
}
}
}
HCPPObject & HCPPObject::operator =(HCPPObject &other)
{
if(&other==this)
{
return *this;
}
SetParent(other.m_parent,true);
return *this;
}
HCPPObject & HCPPObject::operator =(HCPPObject &&other)
{
if(&other==this)
{
return *this;
}
SetParent(other.m_parent,true);
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
std::lock_guard<std::recursive_mutex> lock_other(*other.m_lock);
auto m_child_list_copy=other.m_child_list;//由于过程中会修改m_child_list,故使用副本进行遍历
for(std::list<HCPPObject*>::iterator it=m_child_list_copy.begin(); it!=m_child_list_copy.end(); it++)
{
HCPPObject *child=(*it);
if(child!=NULL)
{
child->SetParent(this,true);
}
}
}
{
//移动标志位
for(size_t i=0; i<sizeof(flags); i++)
{
flags[i]=other.flags[i];
}
}
return *this;
}
HCPPObject::~HCPPObject()
{
SetRunable(false);
if(m_parent!=NULL)
{
//从原有父对象删除
m_parent->RemoveFromChildList(this);
m_parent=NULL;
}
{
//清理子对象(从子对象列表中删除子对象)
std::lock_guard<std::recursive_mutex> lock(*m_lock);
auto m_child_list_copy=m_child_list;//由于删除子对象时会修改m_child_list,故使用副本进行遍历
for(std::list<HCPPObject*>::iterator it=m_child_list_copy.begin(); it!=m_child_list_copy.end(); it++)
{
HCPPObject *child=(*it);
if(child!=NULL)
{
if(child->IsInHeap() && !child->HasFlag(HCPPOBJECT_FLAG_NO_DELETE) && !HasFlag(HCPPOBJECT_FLAG_NO_CHILD_DELETE))
{
//删除在堆上分配的子对象
delete child;
}
else
{
//当非堆上分配的对象或不移除子对象,直接移除其父对象指针
std::lock_guard<std::recursive_mutex> lock(*child->m_lock);
child->SetParent(NULL,true);
}
}
}
m_child_list.clear();
}
//删除锁
if(m_lock!=NULL)
{
delete m_lock;
}
}
HCPPObject *HCPPObject::GetParent()
{
return m_parent;
}
HCPPObject *HCPPObject::GetTopHCPPObject()
{
HCPPObject *ret=m_parent;
if(ret!=NULL)
{
HCPPObject *next_parent=ret->m_parent;
while(next_parent!=NULL)
{
ret=next_parent;
next_parent=ret->m_parent;
}
}
if(ret==NULL)
{
ret=this;
}
return ret;
}
bool HCPPObject::SetParent(HCPPObject * parent,bool force_update)
{
if(!force_update && m_parent!=NULL)
{
return false;
}
std::lock_guard<std::recursive_mutex> lock(*m_lock);
if(m_parent!=NULL)
{
//从原有父对象删除
m_parent->RemoveFromChildList(this);
}
m_parent=parent;
if(m_parent!=NULL)
{
//添加到新父对象
m_parent->AddToChildList(this);
}
return true;
}
void HCPPObject::EnumChild(std::function<void(const HCPPObject * const)> OnEnum)
{
//先进行垃圾回收
GC();
if(OnEnum==NULL)
{
return;
}
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
for(std::list<HCPPObject*>::iterator it=m_child_list.begin(); it!=m_child_list.end(); it++)
{
HCPPObject *child=(*it);
if(child!=NULL)
{
OnEnum(child);
}
}
}
}
void HCPPObject::Lock()
{
m_lock->lock();
}
void HCPPObject::UnLock()
{
m_lock->unlock();
}
void *HCPPObject::operator new(std::size_t count)
{
//调用全局的new
void *ptr=::operator new(count);
//记录此对象
AddHCPPObject(ptr);
return ptr;
}
void HCPPObject::operator delete(void *ptr)
{
//删除记录
RemoveHCPPObject(ptr);
//调用全局的delete
::operator delete(ptr);
}
bool HCPPObject::IsInHeap()
{
return HasHCPPObject(GetVoidPtr());
}
bool HCPPObject::IsInThread()
{
if(HasHCPPObject(GetVoidPtr()))
{
return HCPPObjectGlobal::CPPHeapObjPool[GetVoidPtr()].GetThreadId()==std::this_thread::get_id();
}
else
{
return false;
}
}
std::thread::id const * const HCPPObject::GetThreadId()
{
if(HasHCPPObject(GetVoidPtr()))
{
return &HCPPObjectGlobal::CPPHeapObjPool[GetVoidPtr()].GetThreadId();
}
return NULL;
}
bool HCPPObject::SetThreadId(std::thread::id _id,bool force_update)
{
if(HasHCPPObject(GetVoidPtr()))
{
return HCPPObjectGlobal::CPPHeapObjPool[GetVoidPtr()].SetThreadId(_id,force_update);
}
return false;
}
void * HCPPObject::GetVoidPtr()
{
return (void *)getthis();
}
void HCPPObject::SetFlag(Flag flag)
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
if(sizeof(flags)*8 > static_cast<size_t>(flag))
{
flags[static_cast<size_t>(flag)/8] |= (1UL << (static_cast<size_t>(flag)%8));
}
}
void HCPPObject::ClearFlag(Flag flag)
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
if(sizeof(flags)*8 > static_cast<size_t>(flag))
{
flags[static_cast<size_t>(flag)/8] &= (~(1UL << (static_cast<size_t>(flag)%8)));
}
}
bool HCPPObject::HasFlag(Flag flag)
{
if(sizeof(flags)*8 > static_cast<size_t>(flag))
{
return (flags[static_cast<size_t>(flag)/8] & (1UL << (static_cast<size_t>(flag)%8)))!=0;
}
return false;
}
void HCPPObject::GC()
{
std::lock_guard<std::recursive_mutex> lock(*m_lock);
auto m_child_list_copy=m_child_list;//由于删除子对象时会修改m_child_list,故使用副本进行遍历
for(std::list<HCPPObject*>::iterator it=m_child_list_copy.begin(); it!=m_child_list_copy.end(); it++)
{
HCPPObject *child=(*it);
if(child->HasFlag(HCPPOBJECT_FLAG_TO_BE_DELETED))
{
if (child->IsInHeap())
{
//删除子对象
delete child;
}
else
{
//对于非在堆上分配的对象,不能进行删除
child->ClearFlag(HCPPOBJECT_FLAG_TO_BE_DELETED);
//设置对象为不可执行(确保不可执行)
child->SetRunable(false);
}
}
else
{
//调用子对象的GC
child->GC();
}
}
}
void HCPPObject::Run()
{
if(!HasFlag(HCPPOBJECT_FLAG_RUNABLE))
{
//不可执行直接返回
return;
}
std::lock_guard<std::recursive_mutex> lock(*m_lock);
if(!HasFlag(HCPPOBJECT_FLAG_RUN_INIT))
{
//执行初始化
InvokeInit();
//设置已初始化标志
SetFlag(HCPPOBJECT_FLAG_RUN_INIT);
}
else
{
//执行更新
InvokeUpdate();
}
{
//调用子对象的Run()
EnumChild([](const HCPPObject *obj)
{
if(obj != NULL)
{
((HCPPObject *)obj)->Run();
}
}
);
}
}