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
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "XHandle.h"
#include "utils/log.h"
#include <cassert>
#include <mutex>
int CXHandle::m_objectTracker[10] = {};
HANDLE WINAPI GetCurrentProcess(void) {
return (HANDLE)-1; // -1 a special value - pseudo handle
}
CXHandle::CXHandle()
{
Init();
m_objectTracker[m_type]++;
}
CXHandle::CXHandle(HandleType nType)
{
Init();
m_type=nType;
m_objectTracker[m_type]++;
}
CXHandle::CXHandle(const CXHandle &src)
{
// we shouldn't get here EVER. however, if we do - try to make the best. copy what we can
// and most importantly - not share any pointer.
CLog::Log(LOGWARNING, "{}, copy handle.", __FUNCTION__);
Init();
if (src.m_hMutex)
m_hMutex = new CCriticalSection();
fd = src.fd;
m_bManualEvent = src.m_bManualEvent;
m_tmCreation = time(NULL);
m_FindFileResults = src.m_FindFileResults;
m_nFindFileIterator = src.m_nFindFileIterator;
m_FindFileDir = src.m_FindFileDir;
m_iOffset = src.m_iOffset;
m_bCDROM = src.m_bCDROM;
m_objectTracker[m_type]++;
}
CXHandle::~CXHandle()
{
m_objectTracker[m_type]--;
if (RecursionCount > 0) {
CLog::Log(LOGERROR, "{}, destroying handle with recursion count {}", __FUNCTION__,
RecursionCount);
assert(false);
}
if (m_nRefCount > 1) {
CLog::Log(LOGERROR, "{}, destroying handle with ref count {}", __FUNCTION__, m_nRefCount);
assert(false);
}
if (m_hMutex) {
delete m_hMutex;
}
if (m_internalLock) {
delete m_internalLock;
}
if (m_hCond) {
delete m_hCond;
}
if ( fd != 0 ) {
close(fd);
}
}
void CXHandle::Init()
{
fd=0;
m_hMutex=NULL;
m_hCond=NULL;
m_type = HND_NULL;
RecursionCount=0;
m_bManualEvent=false;
m_bEventSet=false;
m_nFindFileIterator=0 ;
m_nRefCount=1;
m_tmCreation = time(NULL);
m_internalLock = new CCriticalSection();
}
void CXHandle::ChangeType(HandleType newType) {
m_objectTracker[m_type]--;
m_type = newType;
m_objectTracker[m_type]++;
}
void CXHandle::DumpObjectTracker() {
for (int i=0; i< 10; i++) {
CLog::Log(LOGDEBUG, "object {} --> {} instances", i, m_objectTracker[i]);
}
}
bool CloseHandle(HANDLE hObject) {
if (!hObject)
return false;
if (hObject == INVALID_HANDLE_VALUE || hObject == (HANDLE)-1)
return true;
bool bDelete = false;
{
std::unique_lock<CCriticalSection> lock((*hObject->m_internalLock));
if (--hObject->m_nRefCount == 0)
bDelete = true;
}
if (bDelete)
delete hObject;
return true;
}
|